@@ -46,6 +46,8 @@ class StressInterImpl: public StressGenInter {
4646 /* own */
4747 int own_id_min = 0 ;
4848 int own_id_max = UINT32_MAX / 3 ;
49+ /* dir or file name */
50+ std::map<int , std::set<std::string>> name_map;
4951
5052 /* generate file content in build phase */
5153 bool build_gen_content (StressNode *node, StressHostFile *file) override {
@@ -180,6 +182,25 @@ class StressInterImpl: public StressGenInter {
180182 node->own = std::to_string (st.st_uid ) + std::to_string (st.st_gid );
181183 return true ;
182184 }
185+
186+ /* generate a random dir or file name in the current layer */
187+ std::string generate_name (int idx, int depth, std::string root_path, NODE_TYPE type) override {
188+ std::string res;
189+ int cnt = 0 ;
190+
191+ res = get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true );
192+ if (name_map.find (idx) ==name_map.end ())
193+ name_map[idx] = std::set<std::string>();
194+ while (name_map[idx].find (res) != name_map[idx].end ()) {
195+ res = get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true );
196+ cnt ++;
197+ /* try up to 1000 times */
198+ if (cnt > 1000 )
199+ LOG_ERROR_RETURN (-1 , " " , " fail to generate a random name" );
200+ }
201+ name_map[idx].insert (res);
202+ return res;
203+ }
183204};
184205
185206/*
@@ -190,7 +211,7 @@ class StressInterImpl: public StressGenInter {
190211 *
191212 * A simple test for verifying the integrity of the FS tree.
192213 */
193- class StressCase001 : public StressBase {
214+ class StressCase001 : public StressBase , public StressInterImpl {
194215public:
195216 StressCase001 (std::string path, int layers): StressBase(path, layers) {}
196217
@@ -207,8 +228,9 @@ class StressCase001: public StressBase {
207228 EROFS_STRESS_UNIMPLEMENTED_FUNC (bool , verify_gen_content(StressNode *node, photon::fs::IFile *erofs_file), true )
208229
209230 /* simplely generate random dir and file names */
210- EROFS_STRESS_UNIMPLEMENTED_FUNC (std::string, generate_name(int idx, int depth, std::string _prefix, NODE_TYPE type), \
211- get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true ))
231+ std::string generate_name (int idx, int depth, std::string root_path, NODE_TYPE type) override {
232+ return StressInterImpl::generate_name (idx, depth, root_path, type);
233+ }
212234
213235 /*
214236 * each layer has two dirs:
@@ -253,8 +275,9 @@ class StressCase002: public StressBase, public StressInterImpl {
253275 }
254276
255277 /* simplely generate random dir and file names */
256- EROFS_STRESS_UNIMPLEMENTED_FUNC (std::string, generate_name(int idx, int depth, std::string _prefix, NODE_TYPE type), \
257- get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true ))
278+ std::string generate_name (int idx, int depth, std::string root_path, NODE_TYPE type) override {
279+ return StressInterImpl::generate_name (idx, depth, root_path, type);
280+ }
258281
259282 /*
260283 * each layer has two dirs:
@@ -298,8 +321,9 @@ class StressCase003: public StressBase, public StressInterImpl {
298321 }
299322
300323 /* simplely generate random dir and file names */
301- EROFS_STRESS_UNIMPLEMENTED_FUNC (std::string, generate_name(int idx, int depth, std::string _prefix, NODE_TYPE type), \
302- get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true ))
324+ std::string generate_name (int idx, int depth, std::string root_path, NODE_TYPE type) override {
325+ return StressInterImpl::generate_name (idx, depth, root_path, type);
326+ }
303327
304328 std::vector<int > layer_dirs (int idx) {
305329 std::vector<int > ret;
@@ -339,8 +363,9 @@ class StressCase004: public StressBase, public StressInterImpl {
339363 }
340364
341365 /* simplely generate random dir and file names */
342- EROFS_STRESS_UNIMPLEMENTED_FUNC (std::string, generate_name(int idx, int depth, std::string _prefix, NODE_TYPE type), \
343- get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true ))
366+ std::string generate_name (int idx, int depth, std::string root_path, NODE_TYPE type) override {
367+ return StressInterImpl::generate_name (idx, depth, root_path, type);
368+ }
344369
345370 std::vector<int > layer_dirs (int idx) {
346371 std::vector<int > ret;
@@ -379,8 +404,9 @@ class StressCase005: public StressBase, public StressInterImpl {
379404 }
380405
381406 /* simplely generate random dir and file names */
382- EROFS_STRESS_UNIMPLEMENTED_FUNC (std::string, generate_name(int idx, int depth, std::string _prefix, NODE_TYPE type), \
383- get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true ))
407+ std::string generate_name (int idx, int depth, std::string root_path, NODE_TYPE type) override {
408+ return StressInterImpl::generate_name (idx, depth, root_path, type);
409+ }
384410
385411 std::vector<int > layer_dirs (int idx) {
386412 std::vector<int > ret;
@@ -431,8 +457,9 @@ class StressCase006: public StressBase, public StressInterImpl {
431457 }
432458
433459 /* simplely generate random dir and file names */
434- EROFS_STRESS_UNIMPLEMENTED_FUNC (std::string, generate_name(int idx, int depth, std::string _prefix, NODE_TYPE type), \
435- get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true ))
460+ std::string generate_name (int idx, int depth, std::string root_path, NODE_TYPE type) override {
461+ return StressInterImpl::generate_name (idx, depth, root_path, type);
462+ }
436463
437464 std::vector<int > layer_dirs (int idx) {
438465 std::vector<int > ret;
@@ -443,6 +470,82 @@ class StressCase006: public StressBase, public StressInterImpl {
443470 }
444471};
445472
473+ /*
474+ * TC007
475+ *
476+ * Create layers, each layer contains 10 dirs,
477+ * each dir contains 10 files.
478+ *
479+ * Test the scenario where the upper layer and lower
480+ * layer contain files or directories with the same name.
481+ */
482+ class StressCase007 : public StressBase , public StressInterImpl {
483+ private:
484+ std::map<int , std::set<std::string>> mp;
485+ public:
486+ StressCase007 (std::string path, int layers): StressBase(path, layers) {}
487+
488+ bool build_gen_mod (StressNode *node, StressHostFile *file) override {
489+ return StressInterImpl::build_gen_mod (node, file);
490+ }
491+ bool build_gen_own (StressNode *node, StressHostFile *file) override {
492+ return StressInterImpl::build_gen_own (node, file);
493+ }
494+ bool build_gen_xattrs (StressNode *node, StressHostFile *file) override {
495+ return StressInterImpl::build_gen_xattrs (node, file);
496+ }
497+ bool build_gen_content (StressNode *node, StressHostFile *file) override {
498+ return StressInterImpl::build_gen_content (node, file);
499+ }
500+
501+ bool verify_gen_mod (StressNode *node, photon::fs::IFile *erofs_file) override {
502+ return StressInterImpl::verify_gen_mod (node, erofs_file);
503+ }
504+ bool verify_gen_own (StressNode *node, photon::fs::IFile *erofs_file) override {
505+ return StressInterImpl::verify_gen_own (node, erofs_file);
506+ }
507+ bool verify_gen_xattrs (StressNode *node, photon::fs::IFile *erofs_file) override {
508+ return StressInterImpl::verify_gen_xattrs (node, erofs_file);
509+ }
510+ bool verify_gen_content (StressNode *node, photon::fs::IFile *erofs_file) override {
511+ return StressInterImpl::verify_gen_content (node, erofs_file);
512+ }
513+
514+ std::string generate_name (int idx, int depth, std::string root_path, NODE_TYPE type) override {
515+ std::string res;
516+ int cnt = 0 ;
517+
518+ if (idx < 1 ) {
519+ res = get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true );
520+ goto out;
521+ }
522+ res = tree->get_same_name (idx, depth, root_path, type);
523+ /* fall back to a random name */
524+ if (res.length () == 0 )
525+ res = get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true );
526+ if (mp.find (idx) == mp.end ())
527+ mp[idx] = std::set<std::string>();
528+ /* already used in this layer, fall back to a random name */
529+ while (mp[idx].find (res) != mp[idx].end ()) {
530+ res = get_randomstr (type ? MAX_FILE_NAME : MAX_DIR_NAME , true );
531+ cnt ++;
532+ if (cnt > 1000 )
533+ LOG_ERROR_RETURN (-1 , " " , " fail to gernate name in TC007" );
534+ }
535+ mp[idx].insert (res);
536+ out:
537+ return res;
538+ }
539+
540+ std::vector<int > layer_dirs (int idx) {
541+ std::vector<int > ret;
542+
543+ for (int i = 0 ; i < 10 ; i ++)
544+ ret.emplace_back (30 );
545+ return ret;
546+ }
547+ };
548+
446549TEST (ErofsStressTest, TC001 ) {
447550 std::srand (static_cast <unsigned int >(std::time (0 )));
448551 StressCase001 *tc001 = new StressCase001 (" ./erofs_stress_001" , 20 );
@@ -491,6 +594,15 @@ TEST(ErofsStressTest, TC006) {
491594 delete tc006;
492595}
493596
597+ TEST (ErofsStressTest, TC007 ) {
598+ std::srand (static_cast <unsigned int >(std::time (0 )));
599+ /* 50 layers */
600+ StressCase007 *tc007 = new StressCase007 (" ./erofs_stress_007" , 50 );
601+
602+ ASSERT_EQ (tc007->run (), true );
603+ delete tc007;
604+ }
605+
494606int main (int argc, char **argv) {
495607
496608 ::testing::InitGoogleTest (&argc, argv);
0 commit comments