test_orphans.toml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. [cases.test_orphans_normal]
  2. in = "lfs.c"
  3. if = 'PROG_SIZE <= 0x3fe' # only works with one crc per commit
  4. code = '''
  5. lfs_t lfs;
  6. lfs_format(&lfs, cfg) => 0;
  7. lfs_mount(&lfs, cfg) => 0;
  8. lfs_mkdir(&lfs, "parent") => 0;
  9. lfs_mkdir(&lfs, "parent/orphan") => 0;
  10. lfs_mkdir(&lfs, "parent/child") => 0;
  11. lfs_remove(&lfs, "parent/orphan") => 0;
  12. lfs_unmount(&lfs) => 0;
  13. // corrupt the child's most recent commit, this should be the update
  14. // to the linked-list entry, which should orphan the orphan. Note this
  15. // makes a lot of assumptions about the remove operation.
  16. lfs_mount(&lfs, cfg) => 0;
  17. lfs_dir_t dir;
  18. lfs_dir_open(&lfs, &dir, "parent/child") => 0;
  19. lfs_block_t block = dir.m.pair[0];
  20. lfs_dir_close(&lfs, &dir) => 0;
  21. lfs_unmount(&lfs) => 0;
  22. uint8_t buffer[BLOCK_SIZE];
  23. cfg->read(cfg, block, 0, buffer, BLOCK_SIZE) => 0;
  24. int off = BLOCK_SIZE-1;
  25. while (off >= 0 && buffer[off] == ERASE_VALUE) {
  26. off -= 1;
  27. }
  28. memset(&buffer[off-3], BLOCK_SIZE, 3);
  29. cfg->erase(cfg, block) => 0;
  30. cfg->prog(cfg, block, 0, buffer, BLOCK_SIZE) => 0;
  31. cfg->sync(cfg) => 0;
  32. lfs_mount(&lfs, cfg) => 0;
  33. struct lfs_info info;
  34. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  35. lfs_stat(&lfs, "parent/child", &info) => 0;
  36. lfs_fs_size(&lfs) => 8;
  37. lfs_unmount(&lfs) => 0;
  38. lfs_mount(&lfs, cfg) => 0;
  39. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  40. lfs_stat(&lfs, "parent/child", &info) => 0;
  41. lfs_fs_size(&lfs) => 8;
  42. // this mkdir should both create a dir and deorphan, so size
  43. // should be unchanged
  44. lfs_mkdir(&lfs, "parent/otherchild") => 0;
  45. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  46. lfs_stat(&lfs, "parent/child", &info) => 0;
  47. lfs_stat(&lfs, "parent/otherchild", &info) => 0;
  48. lfs_fs_size(&lfs) => 8;
  49. lfs_unmount(&lfs) => 0;
  50. lfs_mount(&lfs, cfg) => 0;
  51. lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
  52. lfs_stat(&lfs, "parent/child", &info) => 0;
  53. lfs_stat(&lfs, "parent/otherchild", &info) => 0;
  54. lfs_fs_size(&lfs) => 8;
  55. lfs_unmount(&lfs) => 0;
  56. '''
  57. # reentrant testing for orphans, basically just spam mkdir/remove
  58. [cases.test_orphans_reentrant]
  59. reentrant = true
  60. # TODO fix this case, caused by non-DAG trees
  61. if = '!(DEPTH == 3 && CACHE_SIZE != 64)'
  62. defines = [
  63. {FILES=6, DEPTH=1, CYCLES=20},
  64. {FILES=26, DEPTH=1, CYCLES=20},
  65. {FILES=3, DEPTH=3, CYCLES=20},
  66. ]
  67. code = '''
  68. lfs_t lfs;
  69. int err = lfs_mount(&lfs, cfg);
  70. if (err) {
  71. lfs_format(&lfs, cfg) => 0;
  72. lfs_mount(&lfs, cfg) => 0;
  73. }
  74. srand(1);
  75. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  76. for (unsigned i = 0; i < CYCLES; i++) {
  77. // create random path
  78. char full_path[256];
  79. for (unsigned d = 0; d < DEPTH; d++) {
  80. sprintf(&full_path[2*d], "/%c", alpha[rand() % FILES]);
  81. }
  82. // if it does not exist, we create it, else we destroy
  83. struct lfs_info info;
  84. int res = lfs_stat(&lfs, full_path, &info);
  85. if (res == LFS_ERR_NOENT) {
  86. // create each directory in turn, ignore if dir already exists
  87. for (unsigned d = 0; d < DEPTH; d++) {
  88. char path[1024];
  89. strcpy(path, full_path);
  90. path[2*d+2] = '\0';
  91. err = lfs_mkdir(&lfs, path);
  92. assert(!err || err == LFS_ERR_EXIST);
  93. }
  94. for (unsigned d = 0; d < DEPTH; d++) {
  95. char path[1024];
  96. strcpy(path, full_path);
  97. path[2*d+2] = '\0';
  98. lfs_stat(&lfs, path, &info) => 0;
  99. assert(strcmp(info.name, &path[2*d+1]) == 0);
  100. assert(info.type == LFS_TYPE_DIR);
  101. }
  102. } else {
  103. // is valid dir?
  104. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  105. assert(info.type == LFS_TYPE_DIR);
  106. // try to delete path in reverse order, ignore if dir is not empty
  107. for (int d = DEPTH-1; d >= 0; d--) {
  108. char path[1024];
  109. strcpy(path, full_path);
  110. path[2*d+2] = '\0';
  111. err = lfs_remove(&lfs, path);
  112. assert(!err || err == LFS_ERR_NOTEMPTY);
  113. }
  114. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  115. }
  116. }
  117. lfs_unmount(&lfs) => 0;
  118. '''