test_superblocks.toml 14 KB

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