test_relocations.toml 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. # specific corner cases worth explicitly testing for
  2. [cases.test_relocations_dangling_split_dir]
  3. defines.ITERATIONS = 20
  4. defines.COUNT = 10
  5. defines.BLOCK_CYCLES = [8, 1]
  6. code = '''
  7. lfs_t lfs;
  8. lfs_format(&lfs, cfg) => 0;
  9. // fill up filesystem so only ~16 blocks are left
  10. lfs_mount(&lfs, cfg) => 0;
  11. lfs_file_t file;
  12. lfs_file_open(&lfs, &file, "padding", LFS_O_CREAT | LFS_O_WRONLY) => 0;
  13. uint8_t buffer[512];
  14. memset(buffer, 0, 512);
  15. while (BLOCK_COUNT - lfs_fs_size(&lfs) > 16) {
  16. lfs_file_write(&lfs, &file, buffer, 512) => 512;
  17. }
  18. lfs_file_close(&lfs, &file) => 0;
  19. // make a child dir to use in bounded space
  20. lfs_mkdir(&lfs, "child") => 0;
  21. lfs_unmount(&lfs) => 0;
  22. lfs_mount(&lfs, cfg) => 0;
  23. for (unsigned j = 0; j < ITERATIONS; j++) {
  24. for (unsigned i = 0; i < COUNT; i++) {
  25. char path[1024];
  26. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  27. lfs_file_open(&lfs, &file, path, LFS_O_CREAT | LFS_O_WRONLY) => 0;
  28. lfs_file_close(&lfs, &file) => 0;
  29. }
  30. lfs_dir_t dir;
  31. struct lfs_info info;
  32. lfs_dir_open(&lfs, &dir, "child") => 0;
  33. lfs_dir_read(&lfs, &dir, &info) => 1;
  34. lfs_dir_read(&lfs, &dir, &info) => 1;
  35. for (unsigned i = 0; i < COUNT; i++) {
  36. char path[1024];
  37. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  38. lfs_dir_read(&lfs, &dir, &info) => 1;
  39. strcmp(info.name, path) => 0;
  40. }
  41. lfs_dir_read(&lfs, &dir, &info) => 0;
  42. lfs_dir_close(&lfs, &dir) => 0;
  43. if (j == (unsigned)ITERATIONS-1) {
  44. break;
  45. }
  46. for (unsigned i = 0; i < COUNT; i++) {
  47. char path[1024];
  48. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  49. lfs_remove(&lfs, path) => 0;
  50. }
  51. }
  52. lfs_unmount(&lfs) => 0;
  53. lfs_mount(&lfs, cfg) => 0;
  54. lfs_dir_t dir;
  55. struct lfs_info info;
  56. lfs_dir_open(&lfs, &dir, "child") => 0;
  57. lfs_dir_read(&lfs, &dir, &info) => 1;
  58. lfs_dir_read(&lfs, &dir, &info) => 1;
  59. for (unsigned i = 0; i < COUNT; i++) {
  60. char path[1024];
  61. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  62. lfs_dir_read(&lfs, &dir, &info) => 1;
  63. strcmp(info.name, path) => 0;
  64. }
  65. lfs_dir_read(&lfs, &dir, &info) => 0;
  66. lfs_dir_close(&lfs, &dir) => 0;
  67. for (unsigned i = 0; i < COUNT; i++) {
  68. char path[1024];
  69. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  70. lfs_remove(&lfs, path) => 0;
  71. }
  72. lfs_unmount(&lfs) => 0;
  73. '''
  74. [cases.test_relocations_outdated_head]
  75. defines.ITERATIONS = 20
  76. defines.COUNT = 10
  77. defines.BLOCK_CYCLES = [8, 1]
  78. code = '''
  79. lfs_t lfs;
  80. lfs_format(&lfs, cfg) => 0;
  81. // fill up filesystem so only ~16 blocks are left
  82. lfs_mount(&lfs, cfg) => 0;
  83. lfs_file_t file;
  84. lfs_file_open(&lfs, &file, "padding", LFS_O_CREAT | LFS_O_WRONLY) => 0;
  85. uint8_t buffer[512];
  86. memset(buffer, 0, 512);
  87. while (BLOCK_COUNT - lfs_fs_size(&lfs) > 16) {
  88. lfs_file_write(&lfs, &file, buffer, 512) => 512;
  89. }
  90. lfs_file_close(&lfs, &file) => 0;
  91. // make a child dir to use in bounded space
  92. lfs_mkdir(&lfs, "child") => 0;
  93. lfs_unmount(&lfs) => 0;
  94. lfs_mount(&lfs, cfg) => 0;
  95. for (unsigned j = 0; j < ITERATIONS; j++) {
  96. for (unsigned i = 0; i < COUNT; i++) {
  97. char path[1024];
  98. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  99. lfs_file_open(&lfs, &file, path, LFS_O_CREAT | LFS_O_WRONLY) => 0;
  100. lfs_file_close(&lfs, &file) => 0;
  101. }
  102. lfs_dir_t dir;
  103. struct lfs_info info;
  104. lfs_dir_open(&lfs, &dir, "child") => 0;
  105. lfs_dir_read(&lfs, &dir, &info) => 1;
  106. lfs_dir_read(&lfs, &dir, &info) => 1;
  107. for (unsigned i = 0; i < COUNT; i++) {
  108. char path[1024];
  109. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  110. lfs_dir_read(&lfs, &dir, &info) => 1;
  111. strcmp(info.name, path) => 0;
  112. info.size => 0;
  113. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  114. lfs_file_open(&lfs, &file, path, LFS_O_WRONLY) => 0;
  115. lfs_file_write(&lfs, &file, "hi", 2) => 2;
  116. lfs_file_close(&lfs, &file) => 0;
  117. }
  118. lfs_dir_read(&lfs, &dir, &info) => 0;
  119. lfs_dir_rewind(&lfs, &dir) => 0;
  120. lfs_dir_read(&lfs, &dir, &info) => 1;
  121. lfs_dir_read(&lfs, &dir, &info) => 1;
  122. for (unsigned i = 0; i < COUNT; i++) {
  123. char path[1024];
  124. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  125. lfs_dir_read(&lfs, &dir, &info) => 1;
  126. strcmp(info.name, path) => 0;
  127. info.size => 2;
  128. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  129. lfs_file_open(&lfs, &file, path, LFS_O_WRONLY) => 0;
  130. lfs_file_write(&lfs, &file, "hi", 2) => 2;
  131. lfs_file_close(&lfs, &file) => 0;
  132. }
  133. lfs_dir_read(&lfs, &dir, &info) => 0;
  134. lfs_dir_rewind(&lfs, &dir) => 0;
  135. lfs_dir_read(&lfs, &dir, &info) => 1;
  136. lfs_dir_read(&lfs, &dir, &info) => 1;
  137. for (unsigned i = 0; i < COUNT; i++) {
  138. char path[1024];
  139. sprintf(path, "test%03d_loooooooooooooooooong_name", i);
  140. lfs_dir_read(&lfs, &dir, &info) => 1;
  141. strcmp(info.name, path) => 0;
  142. info.size => 2;
  143. }
  144. lfs_dir_read(&lfs, &dir, &info) => 0;
  145. lfs_dir_close(&lfs, &dir) => 0;
  146. for (unsigned i = 0; i < COUNT; i++) {
  147. char path[1024];
  148. sprintf(path, "child/test%03d_loooooooooooooooooong_name", i);
  149. lfs_remove(&lfs, path) => 0;
  150. }
  151. }
  152. lfs_unmount(&lfs) => 0;
  153. '''
  154. # reentrant testing for relocations, this is the same as the
  155. # orphan testing, except here we also set block_cycles so that
  156. # almost every tree operation needs a relocation
  157. [cases.test_relocations_reentrant]
  158. reentrant = true
  159. # TODO fix this case, caused by non-DAG trees
  160. # NOTE the second condition is required
  161. if = '!(DEPTH == 3 && CACHE_SIZE != 64) && 2*FILES < BLOCK_COUNT'
  162. defines = [
  163. {FILES=6, DEPTH=1, CYCLES=20, BLOCK_CYCLES=1},
  164. {FILES=26, DEPTH=1, CYCLES=20, BLOCK_CYCLES=1},
  165. {FILES=3, DEPTH=3, CYCLES=20, BLOCK_CYCLES=1},
  166. ]
  167. code = '''
  168. lfs_t lfs;
  169. int err = lfs_mount(&lfs, cfg);
  170. if (err) {
  171. lfs_format(&lfs, cfg) => 0;
  172. lfs_mount(&lfs, cfg) => 0;
  173. }
  174. uint32_t prng = 1;
  175. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  176. for (unsigned i = 0; i < CYCLES; i++) {
  177. // create random path
  178. char full_path[256];
  179. for (unsigned d = 0; d < DEPTH; d++) {
  180. sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  181. }
  182. // if it does not exist, we create it, else we destroy
  183. struct lfs_info info;
  184. int res = lfs_stat(&lfs, full_path, &info);
  185. if (res == LFS_ERR_NOENT) {
  186. // create each directory in turn, ignore if dir already exists
  187. for (unsigned d = 0; d < DEPTH; d++) {
  188. char path[1024];
  189. strcpy(path, full_path);
  190. path[2*d+2] = '\0';
  191. err = lfs_mkdir(&lfs, path);
  192. assert(!err || err == LFS_ERR_EXIST);
  193. }
  194. for (unsigned d = 0; d < DEPTH; d++) {
  195. char path[1024];
  196. strcpy(path, full_path);
  197. path[2*d+2] = '\0';
  198. lfs_stat(&lfs, path, &info) => 0;
  199. assert(strcmp(info.name, &path[2*d+1]) == 0);
  200. assert(info.type == LFS_TYPE_DIR);
  201. }
  202. } else {
  203. // is valid dir?
  204. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  205. assert(info.type == LFS_TYPE_DIR);
  206. // try to delete path in reverse order, ignore if dir is not empty
  207. for (unsigned d = DEPTH-1; d+1 > 0; d--) {
  208. char path[1024];
  209. strcpy(path, full_path);
  210. path[2*d+2] = '\0';
  211. err = lfs_remove(&lfs, path);
  212. assert(!err || err == LFS_ERR_NOTEMPTY);
  213. }
  214. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  215. }
  216. }
  217. lfs_unmount(&lfs) => 0;
  218. '''
  219. # reentrant testing for relocations, but now with random renames!
  220. [cases.test_relocations_reentrant_renames]
  221. reentrant = true
  222. # TODO fix this case, caused by non-DAG trees
  223. # NOTE the second condition is required
  224. if = '!(DEPTH == 3 && CACHE_SIZE != 64) && 2*FILES < BLOCK_COUNT'
  225. defines = [
  226. {FILES=6, DEPTH=1, CYCLES=20, BLOCK_CYCLES=1},
  227. {FILES=26, DEPTH=1, CYCLES=20, BLOCK_CYCLES=1},
  228. {FILES=3, DEPTH=3, CYCLES=20, BLOCK_CYCLES=1},
  229. ]
  230. code = '''
  231. lfs_t lfs;
  232. int err = lfs_mount(&lfs, cfg);
  233. if (err) {
  234. lfs_format(&lfs, cfg) => 0;
  235. lfs_mount(&lfs, cfg) => 0;
  236. }
  237. uint32_t prng = 1;
  238. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  239. for (unsigned i = 0; i < CYCLES; i++) {
  240. // create random path
  241. char full_path[256];
  242. for (unsigned d = 0; d < DEPTH; d++) {
  243. sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  244. }
  245. // if it does not exist, we create it, else we destroy
  246. struct lfs_info info;
  247. int res = lfs_stat(&lfs, full_path, &info);
  248. assert(!res || res == LFS_ERR_NOENT);
  249. if (res == LFS_ERR_NOENT) {
  250. // create each directory in turn, ignore if dir already exists
  251. for (unsigned d = 0; d < DEPTH; d++) {
  252. char path[1024];
  253. strcpy(path, full_path);
  254. path[2*d+2] = '\0';
  255. err = lfs_mkdir(&lfs, path);
  256. assert(!err || err == LFS_ERR_EXIST);
  257. }
  258. for (unsigned d = 0; d < DEPTH; d++) {
  259. char path[1024];
  260. strcpy(path, full_path);
  261. path[2*d+2] = '\0';
  262. lfs_stat(&lfs, path, &info) => 0;
  263. assert(strcmp(info.name, &path[2*d+1]) == 0);
  264. assert(info.type == LFS_TYPE_DIR);
  265. }
  266. } else {
  267. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  268. assert(info.type == LFS_TYPE_DIR);
  269. // create new random path
  270. char new_path[256];
  271. for (unsigned d = 0; d < DEPTH; d++) {
  272. sprintf(&new_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  273. }
  274. // if new path does not exist, rename, otherwise destroy
  275. res = lfs_stat(&lfs, new_path, &info);
  276. assert(!res || res == LFS_ERR_NOENT);
  277. if (res == LFS_ERR_NOENT) {
  278. // stop once some dir is renamed
  279. for (unsigned d = 0; d < DEPTH; d++) {
  280. char path[1024];
  281. strcpy(&path[2*d], &full_path[2*d]);
  282. path[2*d+2] = '\0';
  283. strcpy(&path[128+2*d], &new_path[2*d]);
  284. path[128+2*d+2] = '\0';
  285. err = lfs_rename(&lfs, path, path+128);
  286. assert(!err || err == LFS_ERR_NOTEMPTY);
  287. if (!err) {
  288. strcpy(path, path+128);
  289. }
  290. }
  291. for (unsigned d = 0; d < DEPTH; d++) {
  292. char path[1024];
  293. strcpy(path, new_path);
  294. path[2*d+2] = '\0';
  295. lfs_stat(&lfs, path, &info) => 0;
  296. assert(strcmp(info.name, &path[2*d+1]) == 0);
  297. assert(info.type == LFS_TYPE_DIR);
  298. }
  299. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  300. } else {
  301. // try to delete path in reverse order,
  302. // ignore if dir is not empty
  303. for (unsigned d = DEPTH-1; d+1 > 0; d--) {
  304. char path[1024];
  305. strcpy(path, full_path);
  306. path[2*d+2] = '\0';
  307. err = lfs_remove(&lfs, path);
  308. assert(!err || err == LFS_ERR_NOTEMPTY);
  309. }
  310. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  311. }
  312. }
  313. }
  314. lfs_unmount(&lfs) => 0;
  315. '''
  316. # non-reentrant testing for orphans, this is the same as reentrant
  317. # testing, but we test way more states than we could under powerloss
  318. [cases.test_relocations_nonreentrant]
  319. # TODO fix this case, caused by non-DAG trees
  320. # NOTE the second condition is required
  321. if = '!(DEPTH == 3 && CACHE_SIZE != 64) && 2*FILES < BLOCK_COUNT'
  322. defines = [
  323. {FILES=6, DEPTH=1, CYCLES=2000, BLOCK_CYCLES=1},
  324. {FILES=26, DEPTH=1, CYCLES=2000, BLOCK_CYCLES=1},
  325. {FILES=3, DEPTH=3, CYCLES=2000, BLOCK_CYCLES=1},
  326. ]
  327. code = '''
  328. lfs_t lfs;
  329. lfs_format(&lfs, cfg) => 0;
  330. lfs_mount(&lfs, cfg) => 0;
  331. uint32_t prng = 1;
  332. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  333. for (unsigned i = 0; i < CYCLES; i++) {
  334. // create random path
  335. char full_path[256];
  336. for (unsigned d = 0; d < DEPTH; d++) {
  337. sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  338. }
  339. // if it does not exist, we create it, else we destroy
  340. struct lfs_info info;
  341. int res = lfs_stat(&lfs, full_path, &info);
  342. if (res == LFS_ERR_NOENT) {
  343. // create each directory in turn, ignore if dir already exists
  344. for (unsigned d = 0; d < DEPTH; d++) {
  345. char path[1024];
  346. strcpy(path, full_path);
  347. path[2*d+2] = '\0';
  348. int err = lfs_mkdir(&lfs, path);
  349. assert(!err || err == LFS_ERR_EXIST);
  350. }
  351. for (unsigned d = 0; d < DEPTH; d++) {
  352. char path[1024];
  353. strcpy(path, full_path);
  354. path[2*d+2] = '\0';
  355. lfs_stat(&lfs, path, &info) => 0;
  356. assert(strcmp(info.name, &path[2*d+1]) == 0);
  357. assert(info.type == LFS_TYPE_DIR);
  358. }
  359. } else {
  360. // is valid dir?
  361. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  362. assert(info.type == LFS_TYPE_DIR);
  363. // try to delete path in reverse order, ignore if dir is not empty
  364. for (unsigned d = DEPTH-1; d+1 > 0; d--) {
  365. char path[1024];
  366. strcpy(path, full_path);
  367. path[2*d+2] = '\0';
  368. int err = lfs_remove(&lfs, path);
  369. assert(!err || err == LFS_ERR_NOTEMPTY);
  370. }
  371. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  372. }
  373. }
  374. lfs_unmount(&lfs) => 0;
  375. '''
  376. # non-reentrant testing for relocations, but now with random renames!
  377. [cases.test_relocations_nonreentrant_renames]
  378. # TODO fix this case, caused by non-DAG trees
  379. # NOTE the second condition is required
  380. if = '!(DEPTH == 3 && CACHE_SIZE != 64) && 2*FILES < BLOCK_COUNT'
  381. defines = [
  382. {FILES=6, DEPTH=1, CYCLES=2000, BLOCK_CYCLES=1},
  383. {FILES=26, DEPTH=1, CYCLES=2000, BLOCK_CYCLES=1},
  384. {FILES=3, DEPTH=3, CYCLES=2000, BLOCK_CYCLES=1},
  385. ]
  386. code = '''
  387. lfs_t lfs;
  388. lfs_format(&lfs, cfg) => 0;
  389. lfs_mount(&lfs, cfg) => 0;
  390. uint32_t prng = 1;
  391. const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
  392. for (unsigned i = 0; i < CYCLES; i++) {
  393. // create random path
  394. char full_path[256];
  395. for (unsigned d = 0; d < DEPTH; d++) {
  396. sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  397. }
  398. // if it does not exist, we create it, else we destroy
  399. struct lfs_info info;
  400. int res = lfs_stat(&lfs, full_path, &info);
  401. assert(!res || res == LFS_ERR_NOENT);
  402. if (res == LFS_ERR_NOENT) {
  403. // create each directory in turn, ignore if dir already exists
  404. for (unsigned d = 0; d < DEPTH; d++) {
  405. char path[1024];
  406. strcpy(path, full_path);
  407. path[2*d+2] = '\0';
  408. int err = lfs_mkdir(&lfs, path);
  409. assert(!err || err == LFS_ERR_EXIST);
  410. }
  411. for (unsigned d = 0; d < DEPTH; d++) {
  412. char path[1024];
  413. strcpy(path, full_path);
  414. path[2*d+2] = '\0';
  415. lfs_stat(&lfs, path, &info) => 0;
  416. assert(strcmp(info.name, &path[2*d+1]) == 0);
  417. assert(info.type == LFS_TYPE_DIR);
  418. }
  419. } else {
  420. assert(strcmp(info.name, &full_path[2*(DEPTH-1)+1]) == 0);
  421. assert(info.type == LFS_TYPE_DIR);
  422. // create new random path
  423. char new_path[256];
  424. for (unsigned d = 0; d < DEPTH; d++) {
  425. sprintf(&new_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
  426. }
  427. // if new path does not exist, rename, otherwise destroy
  428. res = lfs_stat(&lfs, new_path, &info);
  429. assert(!res || res == LFS_ERR_NOENT);
  430. if (res == LFS_ERR_NOENT) {
  431. // stop once some dir is renamed
  432. for (unsigned d = 0; d < DEPTH; d++) {
  433. char path[1024];
  434. strcpy(&path[2*d], &full_path[2*d]);
  435. path[2*d+2] = '\0';
  436. strcpy(&path[128+2*d], &new_path[2*d]);
  437. path[128+2*d+2] = '\0';
  438. int err = lfs_rename(&lfs, path, path+128);
  439. assert(!err || err == LFS_ERR_NOTEMPTY);
  440. if (!err) {
  441. strcpy(path, path+128);
  442. }
  443. }
  444. for (unsigned d = 0; d < DEPTH; d++) {
  445. char path[1024];
  446. strcpy(path, new_path);
  447. path[2*d+2] = '\0';
  448. lfs_stat(&lfs, path, &info) => 0;
  449. assert(strcmp(info.name, &path[2*d+1]) == 0);
  450. assert(info.type == LFS_TYPE_DIR);
  451. }
  452. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  453. } else {
  454. // try to delete path in reverse order,
  455. // ignore if dir is not empty
  456. for (unsigned d = DEPTH-1; d+1 > 0; d--) {
  457. char path[1024];
  458. strcpy(path, full_path);
  459. path[2*d+2] = '\0';
  460. int err = lfs_remove(&lfs, path);
  461. assert(!err || err == LFS_ERR_NOTEMPTY);
  462. }
  463. lfs_stat(&lfs, full_path, &info) => LFS_ERR_NOENT;
  464. }
  465. }
  466. }
  467. lfs_unmount(&lfs) => 0;
  468. '''