test_spiffs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <sys/time.h>
  11. #include <sys/unistd.h>
  12. #include "unity.h"
  13. #include "test_utils.h"
  14. #include "esp_log.h"
  15. #include "esp_system.h"
  16. #include "esp_vfs.h"
  17. #include "esp_spiffs.h"
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/task.h"
  20. #include "freertos/queue.h"
  21. #include "freertos/semphr.h"
  22. #include "esp_partition.h"
  23. #include "esp_rom_sys.h"
  24. const char* spiffs_test_hello_str = "Hello, World!\n";
  25. const char* spiffs_test_partition_label = "flash_test";
  26. void test_spiffs_create_file_with_text(const char* name, const char* text)
  27. {
  28. FILE* f = fopen(name, "wb");
  29. TEST_ASSERT_NOT_NULL(f);
  30. TEST_ASSERT_TRUE(fputs(text, f) != EOF);
  31. TEST_ASSERT_EQUAL(0, fclose(f));
  32. }
  33. void test_spiffs_overwrite_append(const char* filename)
  34. {
  35. /* Create new file with 'aaaa' */
  36. test_spiffs_create_file_with_text(filename, "aaaa");
  37. /* Append 'bbbb' to file */
  38. FILE *f_a = fopen(filename, "a");
  39. TEST_ASSERT_NOT_NULL(f_a);
  40. TEST_ASSERT_NOT_EQUAL(EOF, fputs("bbbb", f_a));
  41. TEST_ASSERT_EQUAL(0, fclose(f_a));
  42. /* Read back 8 bytes from file, verify it's 'aaaabbbb' */
  43. char buf[10] = { 0 };
  44. FILE *f_r = fopen(filename, "r");
  45. TEST_ASSERT_NOT_NULL(f_r);
  46. TEST_ASSERT_EQUAL(8, fread(buf, 1, 8, f_r));
  47. TEST_ASSERT_EQUAL_STRING_LEN("aaaabbbb", buf, 8);
  48. /* Be sure we're at end of file */
  49. TEST_ASSERT_EQUAL(0, fread(buf, 1, 8, f_r));
  50. TEST_ASSERT_EQUAL(0, fclose(f_r));
  51. /* Overwrite file with 'cccc' */
  52. test_spiffs_create_file_with_text(filename, "cccc");
  53. /* Verify file now only contains 'cccc' */
  54. f_r = fopen(filename, "r");
  55. TEST_ASSERT_NOT_NULL(f_r);
  56. bzero(buf, sizeof(buf));
  57. TEST_ASSERT_EQUAL(4, fread(buf, 1, 8, f_r)); // trying to read 8 bytes, only expecting 4
  58. TEST_ASSERT_EQUAL_STRING_LEN("cccc", buf, 4);
  59. TEST_ASSERT_EQUAL(0, fclose(f_r));
  60. }
  61. void test_spiffs_read_file(const char* filename)
  62. {
  63. FILE* f = fopen(filename, "r");
  64. TEST_ASSERT_NOT_NULL(f);
  65. char buf[32] = { 0 };
  66. int cb = fread(buf, 1, sizeof(buf), f);
  67. TEST_ASSERT_EQUAL(strlen(spiffs_test_hello_str), cb);
  68. TEST_ASSERT_EQUAL(0, strcmp(spiffs_test_hello_str, buf));
  69. TEST_ASSERT_EQUAL(0, fclose(f));
  70. }
  71. void test_spiffs_open_max_files(const char* filename_prefix, size_t files_count)
  72. {
  73. FILE** files = calloc(files_count, sizeof(FILE*));
  74. for (size_t i = 0; i < files_count; ++i) {
  75. char name[32];
  76. snprintf(name, sizeof(name), "%s_%d.txt", filename_prefix, i);
  77. files[i] = fopen(name, "w");
  78. TEST_ASSERT_NOT_NULL(files[i]);
  79. }
  80. /* close everything and clean up */
  81. for (size_t i = 0; i < files_count; ++i) {
  82. fclose(files[i]);
  83. }
  84. free(files);
  85. }
  86. void test_spiffs_lseek(const char* filename)
  87. {
  88. FILE* f = fopen(filename, "wb+");
  89. TEST_ASSERT_NOT_NULL(f);
  90. TEST_ASSERT_EQUAL(11, fprintf(f, "0123456789\n"));
  91. TEST_ASSERT_EQUAL(0, fseek(f, -2, SEEK_CUR));
  92. TEST_ASSERT_EQUAL('9', fgetc(f));
  93. TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_SET));
  94. TEST_ASSERT_EQUAL('3', fgetc(f));
  95. TEST_ASSERT_EQUAL(0, fseek(f, -3, SEEK_END));
  96. TEST_ASSERT_EQUAL('8', fgetc(f));
  97. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  98. TEST_ASSERT_EQUAL(11, ftell(f));
  99. TEST_ASSERT_EQUAL(4, fprintf(f, "abc\n"));
  100. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  101. TEST_ASSERT_EQUAL(15, ftell(f));
  102. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_SET));
  103. char buf[20];
  104. TEST_ASSERT_EQUAL(15, fread(buf, 1, sizeof(buf), f));
  105. const char ref_buf[] = "0123456789\nabc\n";
  106. TEST_ASSERT_EQUAL_INT8_ARRAY(ref_buf, buf, sizeof(ref_buf) - 1);
  107. TEST_ASSERT_EQUAL(0, fclose(f));
  108. }
  109. void test_spiffs_stat(const char* filename)
  110. {
  111. test_spiffs_create_file_with_text(filename, "foo\n");
  112. struct stat st;
  113. TEST_ASSERT_EQUAL(0, stat(filename, &st));
  114. TEST_ASSERT(st.st_mode & S_IFREG);
  115. TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
  116. }
  117. void test_spiffs_unlink(const char* filename)
  118. {
  119. test_spiffs_create_file_with_text(filename, "unlink\n");
  120. TEST_ASSERT_EQUAL(0, unlink(filename));
  121. TEST_ASSERT_NULL(fopen(filename, "r"));
  122. }
  123. void test_spiffs_rename(const char* filename_prefix)
  124. {
  125. char name_dst[64];
  126. char name_src[64];
  127. snprintf(name_dst, sizeof(name_dst), "%s_dst.txt", filename_prefix);
  128. snprintf(name_src, sizeof(name_src), "%s_src.txt", filename_prefix);
  129. unlink(name_dst);
  130. unlink(name_src);
  131. FILE* f = fopen(name_src, "w+");
  132. TEST_ASSERT_NOT_NULL(f);
  133. const char* str = "0123456789";
  134. for (int i = 0; i < 400; ++i) {
  135. TEST_ASSERT_NOT_EQUAL(EOF, fputs(str, f));
  136. }
  137. TEST_ASSERT_EQUAL(0, fclose(f));
  138. TEST_ASSERT_EQUAL(0, rename(name_src, name_dst));
  139. TEST_ASSERT_NULL(fopen(name_src, "r"));
  140. FILE* fdst = fopen(name_dst, "r");
  141. TEST_ASSERT_NOT_NULL(fdst);
  142. TEST_ASSERT_EQUAL(0, fseek(fdst, 0, SEEK_END));
  143. TEST_ASSERT_EQUAL(4000, ftell(fdst));
  144. TEST_ASSERT_EQUAL(0, fclose(fdst));
  145. }
  146. void test_spiffs_can_opendir(const char* path)
  147. {
  148. char name_dir_file[64];
  149. const char * file_name = "test_opd.txt";
  150. snprintf(name_dir_file, sizeof(name_dir_file), "%s/%s", path, file_name);
  151. unlink(name_dir_file);
  152. test_spiffs_create_file_with_text(name_dir_file, "test_opendir\n");
  153. DIR* dir = opendir(path);
  154. TEST_ASSERT_NOT_NULL(dir);
  155. bool found = false;
  156. while (true) {
  157. struct dirent* de = readdir(dir);
  158. if (!de) {
  159. break;
  160. }
  161. if (strcasecmp(de->d_name, file_name) == 0) {
  162. found = true;
  163. break;
  164. }
  165. }
  166. TEST_ASSERT_TRUE(found);
  167. TEST_ASSERT_EQUAL(0, closedir(dir));
  168. unlink(name_dir_file);
  169. }
  170. void test_spiffs_opendir_readdir_rewinddir(const char* dir_prefix)
  171. {
  172. char name_dir_inner_file[64];
  173. char name_dir_inner[64];
  174. char name_dir_file3[64];
  175. char name_dir_file2[64];
  176. char name_dir_file1[64];
  177. snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/inner/3.txt", dir_prefix);
  178. snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/inner", dir_prefix);
  179. snprintf(name_dir_file3, sizeof(name_dir_file2), "%s/boo.bin", dir_prefix);
  180. snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/2.txt", dir_prefix);
  181. snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/1.txt", dir_prefix);
  182. unlink(name_dir_inner_file);
  183. rmdir(name_dir_inner);
  184. unlink(name_dir_file1);
  185. unlink(name_dir_file2);
  186. unlink(name_dir_file3);
  187. rmdir(dir_prefix);
  188. test_spiffs_create_file_with_text(name_dir_file1, "1\n");
  189. test_spiffs_create_file_with_text(name_dir_file2, "2\n");
  190. test_spiffs_create_file_with_text(name_dir_file3, "\01\02\03");
  191. test_spiffs_create_file_with_text(name_dir_inner_file, "3\n");
  192. DIR* dir = opendir(dir_prefix);
  193. TEST_ASSERT_NOT_NULL(dir);
  194. int count = 0;
  195. const char* names[4];
  196. while(count < 4) {
  197. struct dirent* de = readdir(dir);
  198. if (!de) {
  199. break;
  200. }
  201. printf("found '%s'\n", de->d_name);
  202. if (strcasecmp(de->d_name, "1.txt") == 0) {
  203. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  204. names[count] = "1.txt";
  205. ++count;
  206. } else if (strcasecmp(de->d_name, "2.txt") == 0) {
  207. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  208. names[count] = "2.txt";
  209. ++count;
  210. } else if (strcasecmp(de->d_name, "inner/3.txt") == 0) {
  211. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  212. names[count] = "inner/3.txt";
  213. ++count;
  214. } else if (strcasecmp(de->d_name, "boo.bin") == 0) {
  215. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  216. names[count] = "boo.bin";
  217. ++count;
  218. } else {
  219. TEST_FAIL_MESSAGE("unexpected directory entry");
  220. }
  221. }
  222. TEST_ASSERT_EQUAL(count, 4);
  223. rewinddir(dir);
  224. struct dirent* de = readdir(dir);
  225. TEST_ASSERT_NOT_NULL(de);
  226. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
  227. seekdir(dir, 3);
  228. de = readdir(dir);
  229. TEST_ASSERT_NOT_NULL(de);
  230. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
  231. seekdir(dir, 1);
  232. de = readdir(dir);
  233. TEST_ASSERT_NOT_NULL(de);
  234. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
  235. seekdir(dir, 2);
  236. de = readdir(dir);
  237. TEST_ASSERT_NOT_NULL(de);
  238. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
  239. TEST_ASSERT_EQUAL(0, closedir(dir));
  240. }
  241. void test_spiffs_readdir_many_files(const char* dir_prefix)
  242. {
  243. const int n_files = 40;
  244. const int n_folders = 4;
  245. unsigned char file_count[n_files * n_folders];
  246. memset(file_count, 0, sizeof(file_count)/sizeof(file_count[0]));
  247. char file_name[ESP_VFS_PATH_MAX + CONFIG_SPIFFS_OBJ_NAME_LEN];
  248. /* clean stale files before the test */
  249. DIR* dir = opendir(dir_prefix);
  250. if (dir) {
  251. while (true) {
  252. struct dirent* de = readdir(dir);
  253. if (!de) {
  254. break;
  255. }
  256. int len = snprintf(file_name, sizeof(file_name), "%s/%s", dir_prefix, de->d_name);
  257. assert(len < sizeof(file_name));
  258. unlink(file_name);
  259. }
  260. }
  261. /* create files */
  262. for (int d = 0; d < n_folders; ++d) {
  263. printf("filling directory %d\n", d);
  264. for (int f = 0; f < n_files; ++f) {
  265. snprintf(file_name, sizeof(file_name), "%s/%d/%d.txt", dir_prefix, d, f);
  266. test_spiffs_create_file_with_text(file_name, file_name);
  267. }
  268. }
  269. /* list files */
  270. for (int d = 0; d < n_folders; ++d) {
  271. printf("listing files in directory %d\n", d);
  272. snprintf(file_name, sizeof(file_name), "%s/%d", dir_prefix, d);
  273. dir = opendir(file_name);
  274. TEST_ASSERT_NOT_NULL(dir);
  275. while (true) {
  276. struct dirent* de = readdir(dir);
  277. if (!de) {
  278. break;
  279. }
  280. int file_id;
  281. TEST_ASSERT_EQUAL(1, sscanf(de->d_name, "%d.txt", &file_id));
  282. file_count[file_id + d * n_files]++;
  283. }
  284. closedir(dir);
  285. }
  286. /* check that all created files have been seen */
  287. for (int d = 0; d < n_folders; ++d) {
  288. printf("checking that all files have been found in directory %d\n", d);
  289. for (int f = 0; f < n_files; ++f) {
  290. TEST_ASSERT_EQUAL(1, file_count[f + d * n_files]);
  291. }
  292. }
  293. }
  294. typedef struct {
  295. const char* filename;
  296. bool write;
  297. size_t word_count;
  298. int seed;
  299. SemaphoreHandle_t done;
  300. int result;
  301. } read_write_test_arg_t;
  302. #define READ_WRITE_TEST_ARG_INIT(name, seed_) \
  303. { \
  304. .filename = name, \
  305. .seed = seed_, \
  306. .word_count = 4096, \
  307. .write = true, \
  308. .done = xSemaphoreCreateBinary() \
  309. }
  310. static void read_write_task(void* param)
  311. {
  312. read_write_test_arg_t* args = (read_write_test_arg_t*) param;
  313. FILE* f = fopen(args->filename, args->write ? "wb" : "rb");
  314. if (f == NULL) {
  315. args->result = ESP_ERR_NOT_FOUND;
  316. goto done;
  317. }
  318. srand(args->seed);
  319. for (size_t i = 0; i < args->word_count; ++i) {
  320. uint32_t val = rand();
  321. if (args->write) {
  322. int cnt = fwrite(&val, sizeof(val), 1, f);
  323. if (cnt != 1) {
  324. esp_rom_printf("E(w): i=%d, cnt=%d val=%d\n\n", i, cnt, val);
  325. args->result = ESP_FAIL;
  326. goto close;
  327. }
  328. } else {
  329. uint32_t rval;
  330. int cnt = fread(&rval, sizeof(rval), 1, f);
  331. if (cnt != 1) {
  332. esp_rom_printf("E(r): i=%d, cnt=%d rval=%d\n\n", i, cnt, rval);
  333. args->result = ESP_FAIL;
  334. goto close;
  335. }
  336. }
  337. }
  338. args->result = ESP_OK;
  339. close:
  340. fclose(f);
  341. done:
  342. xSemaphoreGive(args->done);
  343. vTaskDelay(1);
  344. vTaskDelete(NULL);
  345. }
  346. void test_spiffs_concurrent(const char* filename_prefix)
  347. {
  348. char names[4][64];
  349. for (size_t i = 0; i < 4; ++i) {
  350. snprintf(names[i], sizeof(names[i]), "%s%d", filename_prefix, i + 1);
  351. unlink(names[i]);
  352. }
  353. read_write_test_arg_t args1 = READ_WRITE_TEST_ARG_INIT(names[0], 1);
  354. read_write_test_arg_t args2 = READ_WRITE_TEST_ARG_INIT(names[1], 2);
  355. printf("writing f1 and f2\n");
  356. const int cpuid_0 = 0;
  357. const int cpuid_1 = portNUM_PROCESSORS - 1;
  358. xTaskCreatePinnedToCore(&read_write_task, "rw1", 2048, &args1, 3, NULL, cpuid_0);
  359. xTaskCreatePinnedToCore(&read_write_task, "rw2", 2048, &args2, 3, NULL, cpuid_1);
  360. xSemaphoreTake(args1.done, portMAX_DELAY);
  361. printf("f1 done\n");
  362. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  363. xSemaphoreTake(args2.done, portMAX_DELAY);
  364. printf("f2 done\n");
  365. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  366. args1.write = false;
  367. args2.write = false;
  368. read_write_test_arg_t args3 = READ_WRITE_TEST_ARG_INIT(names[2], 3);
  369. read_write_test_arg_t args4 = READ_WRITE_TEST_ARG_INIT(names[3], 4);
  370. printf("reading f1 and f2, writing f3 and f4\n");
  371. xTaskCreatePinnedToCore(&read_write_task, "rw3", 2048, &args3, 3, NULL, cpuid_1);
  372. xTaskCreatePinnedToCore(&read_write_task, "rw4", 2048, &args4, 3, NULL, cpuid_0);
  373. xTaskCreatePinnedToCore(&read_write_task, "rw1", 2048, &args1, 3, NULL, cpuid_0);
  374. xTaskCreatePinnedToCore(&read_write_task, "rw2", 2048, &args2, 3, NULL, cpuid_1);
  375. xSemaphoreTake(args1.done, portMAX_DELAY);
  376. printf("f1 done\n");
  377. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  378. xSemaphoreTake(args2.done, portMAX_DELAY);
  379. printf("f2 done\n");
  380. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  381. xSemaphoreTake(args3.done, portMAX_DELAY);
  382. printf("f3 done\n");
  383. TEST_ASSERT_EQUAL(ESP_OK, args3.result);
  384. xSemaphoreTake(args4.done, portMAX_DELAY);
  385. printf("f4 done\n");
  386. TEST_ASSERT_EQUAL(ESP_OK, args4.result);
  387. vSemaphoreDelete(args1.done);
  388. vSemaphoreDelete(args2.done);
  389. vSemaphoreDelete(args3.done);
  390. vSemaphoreDelete(args4.done);
  391. }
  392. static void test_setup(void)
  393. {
  394. esp_vfs_spiffs_conf_t conf = {
  395. .base_path = "/spiffs",
  396. .partition_label = spiffs_test_partition_label,
  397. .max_files = 5,
  398. .format_if_mount_failed = true
  399. };
  400. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  401. }
  402. static void test_teardown(void)
  403. {
  404. TEST_ESP_OK(esp_vfs_spiffs_unregister(spiffs_test_partition_label));
  405. }
  406. TEST_CASE("can initialize SPIFFS in erased partition", "[spiffs]")
  407. {
  408. const esp_partition_t* part = get_test_data_partition();
  409. TEST_ASSERT_NOT_NULL(part);
  410. TEST_ESP_OK(esp_partition_erase_range(part, 0, part->size));
  411. test_setup();
  412. size_t total = 0, used = 0;
  413. TEST_ESP_OK(esp_spiffs_info(spiffs_test_partition_label, &total, &used));
  414. printf("total: %d, used: %d\n", total, used);
  415. TEST_ASSERT_EQUAL(0, used);
  416. test_teardown();
  417. }
  418. TEST_CASE("can format mounted partition", "[spiffs]")
  419. {
  420. // Mount SPIFFS, create file, format, check that the file does not exist.
  421. const esp_partition_t* part = get_test_data_partition();
  422. TEST_ASSERT_NOT_NULL(part);
  423. test_setup();
  424. const char* filename = "/spiffs/hello.txt";
  425. test_spiffs_create_file_with_text(filename, spiffs_test_hello_str);
  426. esp_spiffs_format(part->label);
  427. FILE* f = fopen(filename, "r");
  428. TEST_ASSERT_NULL(f);
  429. test_teardown();
  430. }
  431. TEST_CASE("can format unmounted partition", "[spiffs]")
  432. {
  433. // Mount SPIFFS, create file, unmount. Format. Mount again, check that
  434. // the file does not exist.
  435. const esp_partition_t* part = get_test_data_partition();
  436. TEST_ASSERT_NOT_NULL(part);
  437. test_setup();
  438. const char* filename = "/spiffs/hello.txt";
  439. test_spiffs_create_file_with_text(filename, spiffs_test_hello_str);
  440. test_teardown();
  441. esp_spiffs_format(part->label);
  442. // Don't use test_setup here, need to mount without formatting
  443. esp_vfs_spiffs_conf_t conf = {
  444. .base_path = "/spiffs",
  445. .partition_label = spiffs_test_partition_label,
  446. .max_files = 5,
  447. .format_if_mount_failed = false
  448. };
  449. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  450. FILE* f = fopen(filename, "r");
  451. TEST_ASSERT_NULL(f);
  452. test_teardown();
  453. }
  454. TEST_CASE("can create and write file", "[spiffs]")
  455. {
  456. test_setup();
  457. test_spiffs_create_file_with_text("/spiffs/hello.txt", spiffs_test_hello_str);
  458. test_teardown();
  459. }
  460. TEST_CASE("can read file", "[spiffs]")
  461. {
  462. test_setup();
  463. test_spiffs_create_file_with_text("/spiffs/hello.txt", spiffs_test_hello_str);
  464. test_spiffs_read_file("/spiffs/hello.txt");
  465. test_teardown();
  466. }
  467. TEST_CASE("can open maximum number of files", "[spiffs]")
  468. {
  469. size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */
  470. esp_vfs_spiffs_conf_t conf = {
  471. .base_path = "/spiffs",
  472. .partition_label = spiffs_test_partition_label,
  473. .format_if_mount_failed = true,
  474. .max_files = max_files
  475. };
  476. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  477. test_spiffs_open_max_files("/spiffs/f", max_files);
  478. TEST_ESP_OK(esp_vfs_spiffs_unregister(spiffs_test_partition_label));
  479. }
  480. TEST_CASE("overwrite and append file", "[spiffs]")
  481. {
  482. test_setup();
  483. test_spiffs_overwrite_append("/spiffs/hello.txt");
  484. test_teardown();
  485. }
  486. TEST_CASE("can lseek", "[spiffs]")
  487. {
  488. test_setup();
  489. test_spiffs_lseek("/spiffs/seek.txt");
  490. test_teardown();
  491. }
  492. TEST_CASE("stat returns correct values", "[spiffs]")
  493. {
  494. test_setup();
  495. test_spiffs_stat("/spiffs/stat.txt");
  496. test_teardown();
  497. }
  498. TEST_CASE("unlink removes a file", "[spiffs]")
  499. {
  500. test_setup();
  501. test_spiffs_unlink("/spiffs/unlink.txt");
  502. test_teardown();
  503. }
  504. TEST_CASE("rename moves a file", "[spiffs]")
  505. {
  506. test_setup();
  507. test_spiffs_rename("/spiffs/move");
  508. test_teardown();
  509. }
  510. TEST_CASE("can opendir root directory of FS", "[spiffs]")
  511. {
  512. test_setup();
  513. test_spiffs_can_opendir("/spiffs");
  514. test_teardown();
  515. }
  516. TEST_CASE("opendir, readdir, rewinddir, seekdir work as expected", "[spiffs]")
  517. {
  518. test_setup();
  519. test_spiffs_opendir_readdir_rewinddir("/spiffs/dir");
  520. test_teardown();
  521. }
  522. TEST_CASE("readdir with large number of files", "[spiffs][timeout=30]")
  523. {
  524. test_setup();
  525. test_spiffs_readdir_many_files("/spiffs/dir2");
  526. test_teardown();
  527. }
  528. TEST_CASE("multiple tasks can use same volume", "[spiffs]")
  529. {
  530. test_setup();
  531. test_spiffs_concurrent("/spiffs/f");
  532. test_teardown();
  533. }
  534. #ifdef CONFIG_SPIFFS_USE_MTIME
  535. TEST_CASE("mtime is updated when file is opened", "[spiffs]")
  536. {
  537. /* Open a file, check that mtime is set correctly */
  538. const char* filename = "/spiffs/time";
  539. test_setup();
  540. time_t t_before_create = time(NULL);
  541. test_spiffs_create_file_with_text(filename, "\n");
  542. time_t t_after_create = time(NULL);
  543. struct stat st;
  544. TEST_ASSERT_EQUAL(0, stat(filename, &st));
  545. printf("mtime=%d\n", (int) st.st_mtime);
  546. TEST_ASSERT(st.st_mtime >= t_before_create
  547. && st.st_mtime <= t_after_create);
  548. /* Wait a bit, open again, check that mtime is updated */
  549. vTaskDelay(2000 / portTICK_PERIOD_MS);
  550. time_t t_before_open = time(NULL);
  551. FILE *f = fopen(filename, "a");
  552. time_t t_after_open = time(NULL);
  553. TEST_ASSERT_EQUAL(0, fstat(fileno(f), &st));
  554. printf("mtime=%d\n", (int) st.st_mtime);
  555. TEST_ASSERT(st.st_mtime >= t_before_open
  556. && st.st_mtime <= t_after_open);
  557. fclose(f);
  558. /* Wait a bit, open for reading, check that mtime is not updated */
  559. vTaskDelay(2000 / portTICK_PERIOD_MS);
  560. time_t t_before_open_ro = time(NULL);
  561. f = fopen(filename, "r");
  562. TEST_ASSERT_EQUAL(0, fstat(fileno(f), &st));
  563. printf("mtime=%d\n", (int) st.st_mtime);
  564. TEST_ASSERT(t_before_open_ro > t_after_open
  565. && st.st_mtime >= t_before_open
  566. && st.st_mtime <= t_after_open);
  567. fclose(f);
  568. test_teardown();
  569. }
  570. TEST_CASE("utime() works well", "[spiffs]")
  571. {
  572. const char filename[] = "/spiffs/utime.txt";
  573. struct stat achieved_stat;
  574. struct tm desired_tm;
  575. struct utimbuf desired_time = {
  576. .actime = 0, // access time is not supported
  577. .modtime = 0,
  578. };
  579. time_t false_now = 0;
  580. memset(&desired_tm, 0, sizeof(struct tm));
  581. test_setup();
  582. {
  583. // Setting up a false actual time - used when the file is created and for modification with the current time
  584. desired_tm.tm_mon = 10 - 1;
  585. desired_tm.tm_mday = 31;
  586. desired_tm.tm_year = 2018 - 1900;
  587. desired_tm.tm_hour = 10;
  588. desired_tm.tm_min = 35;
  589. desired_tm.tm_sec = 23;
  590. false_now = mktime(&desired_tm);
  591. struct timeval now = { .tv_sec = false_now };
  592. settimeofday(&now, NULL);
  593. }
  594. test_spiffs_create_file_with_text(filename, "");
  595. // 00:00:00. January 1st, 1900
  596. desired_tm.tm_mon = 1 - 1;
  597. desired_tm.tm_mday = 1;
  598. desired_tm.tm_year = 0;
  599. desired_tm.tm_hour = 0;
  600. desired_tm.tm_min = 0;
  601. desired_tm.tm_sec = 0;
  602. printf("Testing mod. time: %s", asctime(&desired_tm));
  603. desired_time.modtime = mktime(&desired_tm);
  604. TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
  605. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  606. TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
  607. // 23:59:08. December 31st, 2145
  608. desired_tm.tm_mon = 12 - 1;
  609. desired_tm.tm_mday = 31;
  610. desired_tm.tm_year = 2145 - 1900;
  611. desired_tm.tm_hour = 23;
  612. desired_tm.tm_min = 59;
  613. desired_tm.tm_sec = 8;
  614. printf("Testing mod. time: %s", asctime(&desired_tm));
  615. desired_time.modtime = mktime(&desired_tm);
  616. TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
  617. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  618. TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
  619. // Current time
  620. TEST_ASSERT_EQUAL(0, utime(filename, NULL));
  621. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  622. printf("Mod. time changed to (false actual time): %s", ctime(&achieved_stat.st_mtime));
  623. TEST_ASSERT_NOT_EQUAL(desired_time.modtime, achieved_stat.st_mtime);
  624. TEST_ASSERT(false_now - achieved_stat.st_mtime <= 2); // two seconds of tolerance are given
  625. test_teardown();
  626. }
  627. #endif // CONFIG_SPIFFS_USE_MTIME