test_superblocks.toml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. # simple formatting test
  2. [cases.test_superblocks_format]
  3. code = '''
  4. lfs_t lfs;
  5. lfs_format(&lfs, cfg) => 0;
  6. '''
  7. # mount/unmount
  8. [cases.test_superblocks_mount]
  9. code = '''
  10. lfs_t lfs;
  11. lfs_format(&lfs, cfg) => 0;
  12. lfs_mount(&lfs, cfg) => 0;
  13. lfs_unmount(&lfs) => 0;
  14. '''
  15. # mount/unmount from interpretting a previous superblock block_count
  16. [cases.test_superblocks_mount_unknown_block_count]
  17. code = '''
  18. lfs_t lfs;
  19. lfs_format(&lfs, cfg) => 0;
  20. memset(&lfs, 0, sizeof(lfs));
  21. struct lfs_config tweaked_cfg = *cfg;
  22. tweaked_cfg.block_count = 0;
  23. lfs_mount(&lfs, &tweaked_cfg) => 0;
  24. assert(lfs.block_count == cfg->block_count);
  25. lfs_unmount(&lfs) => 0;
  26. '''
  27. # reentrant format
  28. [cases.test_superblocks_reentrant_format]
  29. reentrant = true
  30. defines.POWERLOSS_BEHAVIOR = [
  31. 'LFS_EMUBD_POWERLOSS_NOOP',
  32. 'LFS_EMUBD_POWERLOSS_OOO',
  33. ]
  34. code = '''
  35. lfs_t lfs;
  36. int err = lfs_mount(&lfs, cfg);
  37. if (err) {
  38. lfs_format(&lfs, cfg) => 0;
  39. lfs_mount(&lfs, cfg) => 0;
  40. }
  41. lfs_unmount(&lfs) => 0;
  42. '''
  43. # invalid mount
  44. [cases.test_superblocks_invalid_mount]
  45. code = '''
  46. lfs_t lfs;
  47. lfs_mount(&lfs, cfg) => LFS_ERR_CORRUPT;
  48. '''
  49. # test we can read superblock info through lfs_fs_stat
  50. [cases.test_superblocks_stat]
  51. if = 'DISK_VERSION == 0'
  52. code = '''
  53. lfs_t lfs;
  54. lfs_format(&lfs, cfg) => 0;
  55. // test we can mount and read fsinfo
  56. lfs_mount(&lfs, cfg) => 0;
  57. struct lfs_fsinfo fsinfo;
  58. lfs_fs_stat(&lfs, &fsinfo) => 0;
  59. assert(fsinfo.disk_version == LFS_DISK_VERSION);
  60. assert(fsinfo.name_max == LFS_NAME_MAX);
  61. assert(fsinfo.file_max == LFS_FILE_MAX);
  62. assert(fsinfo.attr_max == LFS_ATTR_MAX);
  63. lfs_unmount(&lfs) => 0;
  64. '''
  65. [cases.test_superblocks_stat_tweaked]
  66. if = 'DISK_VERSION == 0'
  67. defines.TWEAKED_NAME_MAX = 63
  68. defines.TWEAKED_FILE_MAX = '(1 << 16)-1'
  69. defines.TWEAKED_ATTR_MAX = 512
  70. code = '''
  71. // create filesystem with tweaked params
  72. struct lfs_config tweaked_cfg = *cfg;
  73. tweaked_cfg.name_max = TWEAKED_NAME_MAX;
  74. tweaked_cfg.file_max = TWEAKED_FILE_MAX;
  75. tweaked_cfg.attr_max = TWEAKED_ATTR_MAX;
  76. lfs_t lfs;
  77. lfs_format(&lfs, &tweaked_cfg) => 0;
  78. // test we can mount and read these params with the original config
  79. lfs_mount(&lfs, cfg) => 0;
  80. struct lfs_fsinfo fsinfo;
  81. lfs_fs_stat(&lfs, &fsinfo) => 0;
  82. assert(fsinfo.disk_version == LFS_DISK_VERSION);
  83. assert(fsinfo.name_max == TWEAKED_NAME_MAX);
  84. assert(fsinfo.file_max == TWEAKED_FILE_MAX);
  85. assert(fsinfo.attr_max == TWEAKED_ATTR_MAX);
  86. lfs_unmount(&lfs) => 0;
  87. '''
  88. # expanding superblock
  89. [cases.test_superblocks_expand]
  90. defines.BLOCK_CYCLES = [32, 33, 1]
  91. defines.N = [10, 100, 1000]
  92. code = '''
  93. lfs_t lfs;
  94. lfs_format(&lfs, cfg) => 0;
  95. lfs_mount(&lfs, cfg) => 0;
  96. for (int i = 0; i < N; i++) {
  97. lfs_file_t file;
  98. lfs_file_open(&lfs, &file, "dummy",
  99. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
  100. lfs_file_close(&lfs, &file) => 0;
  101. struct lfs_info info;
  102. lfs_stat(&lfs, "dummy", &info) => 0;
  103. assert(strcmp(info.name, "dummy") == 0);
  104. assert(info.type == LFS_TYPE_REG);
  105. lfs_remove(&lfs, "dummy") => 0;
  106. }
  107. lfs_unmount(&lfs) => 0;
  108. // one last check after power-cycle
  109. lfs_mount(&lfs, cfg) => 0;
  110. lfs_file_t file;
  111. lfs_file_open(&lfs, &file, "dummy",
  112. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
  113. lfs_file_close(&lfs, &file) => 0;
  114. struct lfs_info info;
  115. lfs_stat(&lfs, "dummy", &info) => 0;
  116. assert(strcmp(info.name, "dummy") == 0);
  117. assert(info.type == LFS_TYPE_REG);
  118. lfs_unmount(&lfs) => 0;
  119. '''
  120. # expanding superblock with power cycle
  121. [cases.test_superblocks_expand_power_cycle]
  122. defines.BLOCK_CYCLES = [32, 33, 1]
  123. defines.N = [10, 100, 1000]
  124. code = '''
  125. lfs_t lfs;
  126. lfs_format(&lfs, cfg) => 0;
  127. for (int i = 0; i < N; i++) {
  128. lfs_mount(&lfs, cfg) => 0;
  129. // remove lingering dummy?
  130. struct lfs_info info;
  131. int err = lfs_stat(&lfs, "dummy", &info);
  132. assert(err == 0 || (err == LFS_ERR_NOENT && i == 0));
  133. if (!err) {
  134. assert(strcmp(info.name, "dummy") == 0);
  135. assert(info.type == LFS_TYPE_REG);
  136. lfs_remove(&lfs, "dummy") => 0;
  137. }
  138. lfs_file_t file;
  139. lfs_file_open(&lfs, &file, "dummy",
  140. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
  141. lfs_file_close(&lfs, &file) => 0;
  142. lfs_stat(&lfs, "dummy", &info) => 0;
  143. assert(strcmp(info.name, "dummy") == 0);
  144. assert(info.type == LFS_TYPE_REG);
  145. lfs_unmount(&lfs) => 0;
  146. }
  147. // one last check after power-cycle
  148. lfs_mount(&lfs, cfg) => 0;
  149. struct lfs_info info;
  150. lfs_stat(&lfs, "dummy", &info) => 0;
  151. assert(strcmp(info.name, "dummy") == 0);
  152. assert(info.type == LFS_TYPE_REG);
  153. lfs_unmount(&lfs) => 0;
  154. '''
  155. # reentrant expanding superblock
  156. [cases.test_superblocks_reentrant_expand]
  157. defines.BLOCK_CYCLES = [2, 1]
  158. defines.N = 24
  159. reentrant = true
  160. defines.POWERLOSS_BEHAVIOR = [
  161. 'LFS_EMUBD_POWERLOSS_NOOP',
  162. 'LFS_EMUBD_POWERLOSS_OOO',
  163. ]
  164. code = '''
  165. lfs_t lfs;
  166. int err = lfs_mount(&lfs, cfg);
  167. if (err) {
  168. lfs_format(&lfs, cfg) => 0;
  169. lfs_mount(&lfs, cfg) => 0;
  170. }
  171. for (int i = 0; i < N; i++) {
  172. // remove lingering dummy?
  173. struct lfs_info info;
  174. err = lfs_stat(&lfs, "dummy", &info);
  175. assert(err == 0 || (err == LFS_ERR_NOENT && i == 0));
  176. if (!err) {
  177. assert(strcmp(info.name, "dummy") == 0);
  178. assert(info.type == LFS_TYPE_REG);
  179. lfs_remove(&lfs, "dummy") => 0;
  180. }
  181. lfs_file_t file;
  182. lfs_file_open(&lfs, &file, "dummy",
  183. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
  184. lfs_file_close(&lfs, &file) => 0;
  185. lfs_stat(&lfs, "dummy", &info) => 0;
  186. assert(strcmp(info.name, "dummy") == 0);
  187. assert(info.type == LFS_TYPE_REG);
  188. }
  189. lfs_unmount(&lfs) => 0;
  190. // one last check after power-cycle
  191. lfs_mount(&lfs, cfg) => 0;
  192. struct lfs_info info;
  193. lfs_stat(&lfs, "dummy", &info) => 0;
  194. assert(strcmp(info.name, "dummy") == 0);
  195. assert(info.type == LFS_TYPE_REG);
  196. lfs_unmount(&lfs) => 0;
  197. '''
  198. # mount with unknown block_count
  199. [cases.test_superblocks_unknown_blocks]
  200. code = '''
  201. lfs_t lfs;
  202. lfs_format(&lfs, cfg) => 0;
  203. // known block_size/block_count
  204. cfg->block_size = BLOCK_SIZE;
  205. cfg->block_count = BLOCK_COUNT;
  206. lfs_mount(&lfs, cfg) => 0;
  207. struct lfs_fsinfo fsinfo;
  208. lfs_fs_stat(&lfs, &fsinfo) => 0;
  209. assert(fsinfo.block_size == BLOCK_SIZE);
  210. assert(fsinfo.block_count == BLOCK_COUNT);
  211. lfs_unmount(&lfs) => 0;
  212. // unknown block_count
  213. cfg->block_size = BLOCK_SIZE;
  214. cfg->block_count = 0;
  215. lfs_mount(&lfs, cfg) => 0;
  216. lfs_fs_stat(&lfs, &fsinfo) => 0;
  217. assert(fsinfo.block_size == BLOCK_SIZE);
  218. assert(fsinfo.block_count == BLOCK_COUNT);
  219. lfs_unmount(&lfs) => 0;
  220. // do some work
  221. lfs_mount(&lfs, cfg) => 0;
  222. lfs_fs_stat(&lfs, &fsinfo) => 0;
  223. assert(fsinfo.block_size == BLOCK_SIZE);
  224. assert(fsinfo.block_count == BLOCK_COUNT);
  225. lfs_file_t file;
  226. lfs_file_open(&lfs, &file, "test",
  227. LFS_O_CREAT | LFS_O_EXCL | LFS_O_WRONLY) => 0;
  228. lfs_file_write(&lfs, &file, "hello!", 6) => 6;
  229. lfs_file_close(&lfs, &file) => 0;
  230. lfs_unmount(&lfs) => 0;
  231. lfs_mount(&lfs, cfg) => 0;
  232. lfs_fs_stat(&lfs, &fsinfo) => 0;
  233. assert(fsinfo.block_size == BLOCK_SIZE);
  234. assert(fsinfo.block_count == BLOCK_COUNT);
  235. lfs_file_open(&lfs, &file, "test", LFS_O_RDONLY) => 0;
  236. uint8_t buffer[256];
  237. lfs_file_read(&lfs, &file, buffer, sizeof(buffer)) => 6;
  238. lfs_file_close(&lfs, &file) => 0;
  239. assert(memcmp(buffer, "hello!", 6) == 0);
  240. lfs_unmount(&lfs) => 0;
  241. '''
  242. # mount with blocks fewer than the erase_count
  243. [cases.test_superblocks_fewer_blocks]
  244. defines.BLOCK_COUNT = ['ERASE_COUNT/2', 'ERASE_COUNT/4', '2']
  245. code = '''
  246. lfs_t lfs;
  247. lfs_format(&lfs, cfg) => 0;
  248. // known block_size/block_count
  249. cfg->block_size = BLOCK_SIZE;
  250. cfg->block_count = BLOCK_COUNT;
  251. lfs_mount(&lfs, cfg) => 0;
  252. struct lfs_fsinfo fsinfo;
  253. lfs_fs_stat(&lfs, &fsinfo) => 0;
  254. assert(fsinfo.block_size == BLOCK_SIZE);
  255. assert(fsinfo.block_count == BLOCK_COUNT);
  256. lfs_unmount(&lfs) => 0;
  257. // incorrect block_count
  258. cfg->block_size = BLOCK_SIZE;
  259. cfg->block_count = ERASE_COUNT;
  260. lfs_mount(&lfs, cfg) => LFS_ERR_INVAL;
  261. // unknown block_count
  262. cfg->block_size = BLOCK_SIZE;
  263. cfg->block_count = 0;
  264. lfs_mount(&lfs, cfg) => 0;
  265. lfs_fs_stat(&lfs, &fsinfo) => 0;
  266. assert(fsinfo.block_size == BLOCK_SIZE);
  267. assert(fsinfo.block_count == BLOCK_COUNT);
  268. lfs_unmount(&lfs) => 0;
  269. // do some work
  270. lfs_mount(&lfs, cfg) => 0;
  271. lfs_fs_stat(&lfs, &fsinfo) => 0;
  272. assert(fsinfo.block_size == BLOCK_SIZE);
  273. assert(fsinfo.block_count == BLOCK_COUNT);
  274. lfs_file_t file;
  275. lfs_file_open(&lfs, &file, "test",
  276. LFS_O_CREAT | LFS_O_EXCL | LFS_O_WRONLY) => 0;
  277. lfs_file_write(&lfs, &file, "hello!", 6) => 6;
  278. lfs_file_close(&lfs, &file) => 0;
  279. lfs_unmount(&lfs) => 0;
  280. lfs_mount(&lfs, cfg) => 0;
  281. lfs_fs_stat(&lfs, &fsinfo) => 0;
  282. assert(fsinfo.block_size == BLOCK_SIZE);
  283. assert(fsinfo.block_count == BLOCK_COUNT);
  284. lfs_file_open(&lfs, &file, "test", LFS_O_RDONLY) => 0;
  285. uint8_t buffer[256];
  286. lfs_file_read(&lfs, &file, buffer, sizeof(buffer)) => 6;
  287. lfs_file_close(&lfs, &file) => 0;
  288. assert(memcmp(buffer, "hello!", 6) == 0);
  289. lfs_unmount(&lfs) => 0;
  290. '''
  291. # mount with more blocks than the erase_count
  292. [cases.test_superblocks_more_blocks]
  293. defines.FORMAT_BLOCK_COUNT = '2*ERASE_COUNT'
  294. in = 'lfs.c'
  295. code = '''
  296. lfs_t lfs;
  297. lfs_init(&lfs, cfg) => 0;
  298. lfs.block_count = BLOCK_COUNT;
  299. lfs_mdir_t root = {
  300. .pair = {0, 0}, // make sure this goes into block 0
  301. .rev = 0,
  302. .off = sizeof(uint32_t),
  303. .etag = 0xffffffff,
  304. .count = 0,
  305. .tail = {LFS_BLOCK_NULL, LFS_BLOCK_NULL},
  306. .erased = false,
  307. .split = false,
  308. };
  309. lfs_superblock_t superblock = {
  310. .version = LFS_DISK_VERSION,
  311. .block_size = BLOCK_SIZE,
  312. .block_count = FORMAT_BLOCK_COUNT,
  313. .name_max = LFS_NAME_MAX,
  314. .file_max = LFS_FILE_MAX,
  315. .attr_max = LFS_ATTR_MAX,
  316. };
  317. lfs_superblock_tole32(&superblock);
  318. lfs_dir_commit(&lfs, &root, LFS_MKATTRS(
  319. {LFS_MKTAG(LFS_TYPE_CREATE, 0, 0), NULL},
  320. {LFS_MKTAG(LFS_TYPE_SUPERBLOCK, 0, 8), "littlefs"},
  321. {LFS_MKTAG(LFS_TYPE_INLINESTRUCT, 0, sizeof(superblock)),
  322. &superblock})) => 0;
  323. lfs_deinit(&lfs) => 0;
  324. // known block_size/block_count
  325. cfg->block_size = BLOCK_SIZE;
  326. cfg->block_count = BLOCK_COUNT;
  327. lfs_mount(&lfs, cfg) => LFS_ERR_INVAL;
  328. '''
  329. # mount and grow the filesystem
  330. [cases.test_superblocks_grow]
  331. defines.BLOCK_COUNT = ['ERASE_COUNT/2', 'ERASE_COUNT/4', '2']
  332. defines.BLOCK_COUNT_2 = 'ERASE_COUNT'
  333. defines.KNOWN_BLOCK_COUNT = [true, false]
  334. code = '''
  335. lfs_t lfs;
  336. lfs_format(&lfs, cfg) => 0;
  337. if (KNOWN_BLOCK_COUNT) {
  338. cfg->block_count = BLOCK_COUNT;
  339. } else {
  340. cfg->block_count = 0;
  341. }
  342. // mount with block_size < erase_size
  343. lfs_mount(&lfs, cfg) => 0;
  344. struct lfs_fsinfo fsinfo;
  345. lfs_fs_stat(&lfs, &fsinfo) => 0;
  346. assert(fsinfo.block_size == BLOCK_SIZE);
  347. assert(fsinfo.block_count == BLOCK_COUNT);
  348. lfs_unmount(&lfs) => 0;
  349. // same size is a noop
  350. lfs_mount(&lfs, cfg) => 0;
  351. lfs_fs_grow(&lfs, BLOCK_COUNT) => 0;
  352. lfs_fs_stat(&lfs, &fsinfo) => 0;
  353. assert(fsinfo.block_size == BLOCK_SIZE);
  354. assert(fsinfo.block_count == BLOCK_COUNT);
  355. lfs_unmount(&lfs) => 0;
  356. lfs_mount(&lfs, cfg) => 0;
  357. lfs_fs_stat(&lfs, &fsinfo) => 0;
  358. assert(fsinfo.block_size == BLOCK_SIZE);
  359. assert(fsinfo.block_count == BLOCK_COUNT);
  360. lfs_unmount(&lfs) => 0;
  361. // grow to new size
  362. lfs_mount(&lfs, cfg) => 0;
  363. lfs_fs_grow(&lfs, BLOCK_COUNT_2) => 0;
  364. lfs_fs_stat(&lfs, &fsinfo) => 0;
  365. assert(fsinfo.block_size == BLOCK_SIZE);
  366. assert(fsinfo.block_count == BLOCK_COUNT_2);
  367. lfs_unmount(&lfs) => 0;
  368. if (KNOWN_BLOCK_COUNT) {
  369. cfg->block_count = BLOCK_COUNT_2;
  370. } else {
  371. cfg->block_count = 0;
  372. }
  373. lfs_mount(&lfs, cfg) => 0;
  374. lfs_fs_stat(&lfs, &fsinfo) => 0;
  375. assert(fsinfo.block_size == BLOCK_SIZE);
  376. assert(fsinfo.block_count == BLOCK_COUNT_2);
  377. lfs_unmount(&lfs) => 0;
  378. // mounting with the previous size should fail
  379. cfg->block_count = BLOCK_COUNT;
  380. lfs_mount(&lfs, cfg) => LFS_ERR_INVAL;
  381. if (KNOWN_BLOCK_COUNT) {
  382. cfg->block_count = BLOCK_COUNT_2;
  383. } else {
  384. cfg->block_count = 0;
  385. }
  386. // same size is a noop
  387. lfs_mount(&lfs, cfg) => 0;
  388. lfs_fs_grow(&lfs, BLOCK_COUNT_2) => 0;
  389. lfs_fs_stat(&lfs, &fsinfo) => 0;
  390. assert(fsinfo.block_size == BLOCK_SIZE);
  391. assert(fsinfo.block_count == BLOCK_COUNT_2);
  392. lfs_unmount(&lfs) => 0;
  393. lfs_mount(&lfs, cfg) => 0;
  394. lfs_fs_stat(&lfs, &fsinfo) => 0;
  395. assert(fsinfo.block_size == BLOCK_SIZE);
  396. assert(fsinfo.block_count == BLOCK_COUNT_2);
  397. lfs_unmount(&lfs) => 0;
  398. // do some work
  399. lfs_mount(&lfs, cfg) => 0;
  400. lfs_fs_stat(&lfs, &fsinfo) => 0;
  401. assert(fsinfo.block_size == BLOCK_SIZE);
  402. assert(fsinfo.block_count == BLOCK_COUNT_2);
  403. lfs_file_t file;
  404. lfs_file_open(&lfs, &file, "test",
  405. LFS_O_CREAT | LFS_O_EXCL | LFS_O_WRONLY) => 0;
  406. lfs_file_write(&lfs, &file, "hello!", 6) => 6;
  407. lfs_file_close(&lfs, &file) => 0;
  408. lfs_unmount(&lfs) => 0;
  409. lfs_mount(&lfs, cfg) => 0;
  410. lfs_fs_stat(&lfs, &fsinfo) => 0;
  411. assert(fsinfo.block_size == BLOCK_SIZE);
  412. assert(fsinfo.block_count == BLOCK_COUNT_2);
  413. lfs_file_open(&lfs, &file, "test", LFS_O_RDONLY) => 0;
  414. uint8_t buffer[256];
  415. lfs_file_read(&lfs, &file, buffer, sizeof(buffer)) => 6;
  416. lfs_file_close(&lfs, &file) => 0;
  417. assert(memcmp(buffer, "hello!", 6) == 0);
  418. lfs_unmount(&lfs) => 0;
  419. '''