test_relocations.toml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. # specific corner cases worth explicitly testing for
  2. [[case]] # dangling split dir test
  3. define.ITERATIONS = 20
  4. define.COUNT = 10
  5. define.LFS_BLOCK_CYCLES = [8, 1]
  6. code = '''
  7. lfs_format(&lfs, &cfg) => 0;
  8. // fill up filesystem so only ~16 blocks are left
  9. lfs_mount(&lfs, &cfg) => 0;
  10. lfs_file_open(&lfs, &file, "padding", LFS_O_CREAT | LFS_O_WRONLY) => 0;
  11. memset(buffer, 0, 512);
  12. while (LFS_BLOCK_COUNT - lfs_fs_size(&lfs) > 16) {
  13. lfs_file_write(&lfs, &file, buffer, 512) => 512;
  14. }
  15. lfs_file_close(&lfs, &file) => 0;
  16. // make a child dir to use in bounded space
  17. lfs_mkdir(&lfs, "child") => 0;
  18. lfs_unmount(&lfs) => 0;
  19. lfs_mount(&lfs, &cfg) => 0;
  20. for (int j = 0; j < ITERATIONS; j++) {
  21. for (int i = 0; i < COUNT; i++) {
  22. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  23. lfs_file_open(&lfs, &file, path, LFS_O_CREAT | LFS_O_WRONLY) => 0;
  24. lfs_file_close(&lfs, &file) => 0;
  25. }
  26. lfs_dir_open(&lfs, &dir, "child") => 0;
  27. lfs_dir_read(&lfs, &dir, &info) => 1;
  28. lfs_dir_read(&lfs, &dir, &info) => 1;
  29. for (int i = 0; i < COUNT; i++) {
  30. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  31. lfs_dir_read(&lfs, &dir, &info) => 1;
  32. strcmp(info.name, path) => 0;
  33. }
  34. lfs_dir_read(&lfs, &dir, &info) => 0;
  35. lfs_dir_close(&lfs, &dir) => 0;
  36. if (j == ITERATIONS-1) {
  37. break;
  38. }
  39. for (int i = 0; i < COUNT; i++) {
  40. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  41. lfs_remove(&lfs, path) => 0;
  42. }
  43. }
  44. lfs_unmount(&lfs) => 0;
  45. lfs_mount(&lfs, &cfg) => 0;
  46. lfs_dir_open(&lfs, &dir, "child") => 0;
  47. lfs_dir_read(&lfs, &dir, &info) => 1;
  48. lfs_dir_read(&lfs, &dir, &info) => 1;
  49. for (int i = 0; i < COUNT; i++) {
  50. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  51. lfs_dir_read(&lfs, &dir, &info) => 1;
  52. strcmp(info.name, path) => 0;
  53. }
  54. lfs_dir_read(&lfs, &dir, &info) => 0;
  55. lfs_dir_close(&lfs, &dir) => 0;
  56. for (int i = 0; i < COUNT; i++) {
  57. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  58. lfs_remove(&lfs, path) => 0;
  59. }
  60. lfs_unmount(&lfs) => 0;
  61. '''
  62. [[case]] # outdated head test
  63. define.ITERATIONS = 20
  64. define.COUNT = 10
  65. define.LFS_BLOCK_CYCLES = [8, 1]
  66. code = '''
  67. lfs_format(&lfs, &cfg) => 0;
  68. // fill up filesystem so only ~16 blocks are left
  69. lfs_mount(&lfs, &cfg) => 0;
  70. lfs_file_open(&lfs, &file, "padding", LFS_O_CREAT | LFS_O_WRONLY) => 0;
  71. memset(buffer, 0, 512);
  72. while (LFS_BLOCK_COUNT - lfs_fs_size(&lfs) > 16) {
  73. lfs_file_write(&lfs, &file, buffer, 512) => 512;
  74. }
  75. lfs_file_close(&lfs, &file) => 0;
  76. // make a child dir to use in bounded space
  77. lfs_mkdir(&lfs, "child") => 0;
  78. lfs_unmount(&lfs) => 0;
  79. lfs_mount(&lfs, &cfg) => 0;
  80. for (int j = 0; j < ITERATIONS; j++) {
  81. for (int i = 0; i < COUNT; i++) {
  82. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  83. lfs_file_open(&lfs, &file, path, LFS_O_CREAT | LFS_O_WRONLY) => 0;
  84. lfs_file_close(&lfs, &file) => 0;
  85. }
  86. lfs_dir_open(&lfs, &dir, "child") => 0;
  87. lfs_dir_read(&lfs, &dir, &info) => 1;
  88. lfs_dir_read(&lfs, &dir, &info) => 1;
  89. for (int i = 0; i < COUNT; i++) {
  90. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  91. lfs_dir_read(&lfs, &dir, &info) => 1;
  92. strcmp(info.name, path) => 0;
  93. info.size => 0;
  94. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  95. lfs_file_open(&lfs, &file, path, LFS_O_WRONLY) => 0;
  96. lfs_file_write(&lfs, &file, "hi", 2) => 2;
  97. lfs_file_close(&lfs, &file) => 0;
  98. }
  99. lfs_dir_read(&lfs, &dir, &info) => 0;
  100. lfs_dir_rewind(&lfs, &dir) => 0;
  101. lfs_dir_read(&lfs, &dir, &info) => 1;
  102. lfs_dir_read(&lfs, &dir, &info) => 1;
  103. for (int i = 0; i < COUNT; i++) {
  104. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  105. lfs_dir_read(&lfs, &dir, &info) => 1;
  106. strcmp(info.name, path) => 0;
  107. info.size => 2;
  108. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  109. lfs_file_open(&lfs, &file, path, LFS_O_WRONLY) => 0;
  110. lfs_file_write(&lfs, &file, "hi", 2) => 2;
  111. lfs_file_close(&lfs, &file) => 0;
  112. }
  113. lfs_dir_read(&lfs, &dir, &info) => 0;
  114. lfs_dir_rewind(&lfs, &dir) => 0;
  115. lfs_dir_read(&lfs, &dir, &info) => 1;
  116. lfs_dir_read(&lfs, &dir, &info) => 1;
  117. for (int i = 0; i < COUNT; i++) {
  118. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  119. lfs_dir_read(&lfs, &dir, &info) => 1;
  120. strcmp(info.name, path) => 0;
  121. info.size => 2;
  122. }
  123. lfs_dir_read(&lfs, &dir, &info) => 0;
  124. lfs_dir_close(&lfs, &dir) => 0;
  125. for (int i = 0; i < COUNT; i++) {
  126. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  127. lfs_remove(&lfs, path) => 0;
  128. }
  129. }
  130. lfs_unmount(&lfs) => 0;
  131. '''
  132. [[case]] # reentrant testing for relocations, this is the same as the
  133. # orphan testing, except here we also set block_cycles so that
  134. # almost every tree operation needs a relocation
  135. reentrant = true
  136. define = [
  137. {FILES=6, DEPTH=1, CYCLES=50, LFS_BLOCK_CYCLES=1},
  138. {FILES=26, DEPTH=1, CYCLES=50, LFS_BLOCK_CYCLES=1},
  139. {FILES=3, DEPTH=3, CYCLES=50, LFS_BLOCK_CYCLES=1},
  140. ]
  141. code = '''
  142. err = lfs_mount(&lfs, &cfg);
  143. if (err) {
  144. lfs_format(&lfs, &cfg) => 0;
  145. lfs_mount(&lfs, &cfg) => 0;
  146. }
  147. srand(1);
  148. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  149. for (int i = 0; i < CYCLES; i++) {
  150. // create random path
  151. char full_path[256];
  152. for (int d = 0; d < DEPTH; d++) {
  153. sprintf(&full_path[2*d], "/%c", alpha[rand() % FILES]);
  154. }
  155. // if it does not exist, we create it, else we destroy
  156. int res = lfs_stat(&lfs, full_path, &info);
  157. if (res == LFS_ERR_NOENT) {
  158. // create each directory in turn, ignore if dir already exists
  159. for (int d = 0; d < DEPTH; d++) {
  160. strcpy(path, full_path);
  161. path[2*d+2] = '\0';
  162. err = lfs_mkdir(&lfs, path);
  163. assert(!err || err == LFS_ERR_EXIST);
  164. }
  165. for (int d = 0; d < DEPTH; d++) {
  166. strcpy(path, full_path);
  167. path[2*d+2] = '\0';
  168. lfs_stat(&lfs, path, &info) => 0;
  169. assert(strcmp(info.name, &path[2*d+1]) == 0);
  170. assert(info.type == LFS_TYPE_DIR);
  171. }
  172. } else {
  173. // is valid dir?
  174. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  175. assert(info.type == LFS_TYPE_DIR);
  176. // try to delete path in reverse order, ignore if dir is not empty
  177. for (int d = DEPTH-1; d >= 0; d--) {
  178. strcpy(path, full_path);
  179. path[2*d+2] = '\0';
  180. err = lfs_remove(&lfs, path);
  181. assert(!err || err == LFS_ERR_NOTEMPTY);
  182. }
  183. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  184. }
  185. }
  186. lfs_unmount(&lfs) => 0;
  187. '''
  188. [[case]] # reentrant testing for relocations, but now with random renames!
  189. reentrant = true
  190. define = [
  191. {FILES=6, DEPTH=1, CYCLES=50, LFS_BLOCK_CYCLES=1},
  192. {FILES=26, DEPTH=1, CYCLES=50, LFS_BLOCK_CYCLES=1},
  193. {FILES=3, DEPTH=3, CYCLES=50, LFS_BLOCK_CYCLES=1},
  194. ]
  195. code = '''
  196. err = lfs_mount(&lfs, &cfg);
  197. if (err) {
  198. lfs_format(&lfs, &cfg) => 0;
  199. lfs_mount(&lfs, &cfg) => 0;
  200. }
  201. srand(1);
  202. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  203. for (int i = 0; i < CYCLES; i++) {
  204. // create random path
  205. char full_path[256];
  206. for (int d = 0; d < DEPTH; d++) {
  207. sprintf(&full_path[2*d], "/%c", alpha[rand() % FILES]);
  208. }
  209. // if it does not exist, we create it, else we destroy
  210. int res = lfs_stat(&lfs, full_path, &info);
  211. assert(!res || res == LFS_ERR_NOENT);
  212. if (res == LFS_ERR_NOENT) {
  213. // create each directory in turn, ignore if dir already exists
  214. for (int d = 0; d < DEPTH; d++) {
  215. strcpy(path, full_path);
  216. path[2*d+2] = '\0';
  217. err = lfs_mkdir(&lfs, path);
  218. assert(!err || err == LFS_ERR_EXIST);
  219. }
  220. for (int d = 0; d < DEPTH; d++) {
  221. strcpy(path, full_path);
  222. path[2*d+2] = '\0';
  223. lfs_stat(&lfs, path, &info) => 0;
  224. assert(strcmp(info.name, &path[2*d+1]) == 0);
  225. assert(info.type == LFS_TYPE_DIR);
  226. }
  227. } else {
  228. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  229. assert(info.type == LFS_TYPE_DIR);
  230. // create new random path
  231. char new_path[256];
  232. for (int d = 0; d < DEPTH; d++) {
  233. sprintf(&new_path[2*d], "/%c", alpha[rand() % FILES]);
  234. }
  235. // if new path does not exist, rename, otherwise destroy
  236. res = lfs_stat(&lfs, new_path, &info);
  237. assert(!res || res == LFS_ERR_NOENT);
  238. if (res == LFS_ERR_NOENT) {
  239. // stop once some dir is renamed
  240. for (int d = 0; d < DEPTH; d++) {
  241. strcpy(&path[2*d], &full_path[2*d]);
  242. path[2*d+2] = '\0';
  243. strcpy(&path[128+2*d], &new_path[2*d]);
  244. path[128+2*d+2] = '\0';
  245. err = lfs_rename(&lfs, path, path+128);
  246. assert(!err || err == LFS_ERR_NOTEMPTY);
  247. if (!err) {
  248. strcpy(path, path+128);
  249. }
  250. }
  251. for (int d = 0; d < DEPTH; d++) {
  252. strcpy(path, new_path);
  253. path[2*d+2] = '\0';
  254. lfs_stat(&lfs, path, &info) => 0;
  255. assert(strcmp(info.name, &path[2*d+1]) == 0);
  256. assert(info.type == LFS_TYPE_DIR);
  257. }
  258. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  259. } else {
  260. // try to delete path in reverse order,
  261. // ignore if dir is not empty
  262. for (int d = DEPTH-1; d >= 0; d--) {
  263. strcpy(path, full_path);
  264. path[2*d+2] = '\0';
  265. err = lfs_remove(&lfs, path);
  266. assert(!err || err == LFS_ERR_NOTEMPTY);
  267. }
  268. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  269. }
  270. }
  271. }
  272. lfs_unmount(&lfs) => 0;
  273. '''