test_spiffs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <time.h>
  18. #include <sys/time.h>
  19. #include <sys/unistd.h>
  20. #include "unity.h"
  21. #include "test_utils.h"
  22. #include "esp_log.h"
  23. #include "esp_system.h"
  24. #include "esp_vfs.h"
  25. #include "esp_spiffs.h"
  26. #include "freertos/FreeRTOS.h"
  27. #include "freertos/task.h"
  28. #include "freertos/queue.h"
  29. #include "freertos/semphr.h"
  30. #include "esp_partition.h"
  31. const char* spiffs_test_hello_str = "Hello, World!\n";
  32. const char* spiffs_test_partition_label = "flash_test";
  33. void test_spiffs_create_file_with_text(const char* name, const char* text)
  34. {
  35. FILE* f = fopen(name, "wb");
  36. TEST_ASSERT_NOT_NULL(f);
  37. TEST_ASSERT_TRUE(fputs(text, f) != EOF);
  38. TEST_ASSERT_EQUAL(0, fclose(f));
  39. }
  40. void test_spiffs_overwrite_append(const char* filename)
  41. {
  42. /* Create new file with 'aaaa' */
  43. test_spiffs_create_file_with_text(filename, "aaaa");
  44. /* Append 'bbbb' to file */
  45. FILE *f_a = fopen(filename, "a");
  46. TEST_ASSERT_NOT_NULL(f_a);
  47. TEST_ASSERT_NOT_EQUAL(EOF, fputs("bbbb", f_a));
  48. TEST_ASSERT_EQUAL(0, fclose(f_a));
  49. /* Read back 8 bytes from file, verify it's 'aaaabbbb' */
  50. char buf[10] = { 0 };
  51. FILE *f_r = fopen(filename, "r");
  52. TEST_ASSERT_NOT_NULL(f_r);
  53. TEST_ASSERT_EQUAL(8, fread(buf, 1, 8, f_r));
  54. TEST_ASSERT_EQUAL_STRING_LEN("aaaabbbb", buf, 8);
  55. /* Be sure we're at end of file */
  56. TEST_ASSERT_EQUAL(0, fread(buf, 1, 8, f_r));
  57. TEST_ASSERT_EQUAL(0, fclose(f_r));
  58. /* Overwrite file with 'cccc' */
  59. test_spiffs_create_file_with_text(filename, "cccc");
  60. /* Verify file now only contains 'cccc' */
  61. f_r = fopen(filename, "r");
  62. TEST_ASSERT_NOT_NULL(f_r);
  63. bzero(buf, sizeof(buf));
  64. TEST_ASSERT_EQUAL(4, fread(buf, 1, 8, f_r)); // trying to read 8 bytes, only expecting 4
  65. TEST_ASSERT_EQUAL_STRING_LEN("cccc", buf, 4);
  66. TEST_ASSERT_EQUAL(0, fclose(f_r));
  67. }
  68. void test_spiffs_read_file(const char* filename)
  69. {
  70. FILE* f = fopen(filename, "r");
  71. TEST_ASSERT_NOT_NULL(f);
  72. char buf[32] = { 0 };
  73. int cb = fread(buf, 1, sizeof(buf), f);
  74. TEST_ASSERT_EQUAL(strlen(spiffs_test_hello_str), cb);
  75. TEST_ASSERT_EQUAL(0, strcmp(spiffs_test_hello_str, buf));
  76. TEST_ASSERT_EQUAL(0, fclose(f));
  77. }
  78. void test_spiffs_open_max_files(const char* filename_prefix, size_t files_count)
  79. {
  80. FILE** files = calloc(files_count, sizeof(FILE*));
  81. for (size_t i = 0; i < files_count; ++i) {
  82. char name[32];
  83. snprintf(name, sizeof(name), "%s_%d.txt", filename_prefix, i);
  84. files[i] = fopen(name, "w");
  85. TEST_ASSERT_NOT_NULL(files[i]);
  86. }
  87. /* close everything and clean up */
  88. for (size_t i = 0; i < files_count; ++i) {
  89. fclose(files[i]);
  90. }
  91. free(files);
  92. }
  93. void test_spiffs_lseek(const char* filename)
  94. {
  95. FILE* f = fopen(filename, "wb+");
  96. TEST_ASSERT_NOT_NULL(f);
  97. TEST_ASSERT_EQUAL(11, fprintf(f, "0123456789\n"));
  98. TEST_ASSERT_EQUAL(0, fseek(f, -2, SEEK_CUR));
  99. TEST_ASSERT_EQUAL('9', fgetc(f));
  100. TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_SET));
  101. TEST_ASSERT_EQUAL('3', fgetc(f));
  102. TEST_ASSERT_EQUAL(0, fseek(f, -3, SEEK_END));
  103. TEST_ASSERT_EQUAL('8', fgetc(f));
  104. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  105. TEST_ASSERT_EQUAL(11, ftell(f));
  106. TEST_ASSERT_EQUAL(4, fprintf(f, "abc\n"));
  107. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  108. TEST_ASSERT_EQUAL(15, ftell(f));
  109. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_SET));
  110. char buf[20];
  111. TEST_ASSERT_EQUAL(15, fread(buf, 1, sizeof(buf), f));
  112. const char ref_buf[] = "0123456789\nabc\n";
  113. TEST_ASSERT_EQUAL_INT8_ARRAY(ref_buf, buf, sizeof(ref_buf) - 1);
  114. TEST_ASSERT_EQUAL(0, fclose(f));
  115. }
  116. void test_spiffs_stat(const char* filename)
  117. {
  118. test_spiffs_create_file_with_text(filename, "foo\n");
  119. struct stat st;
  120. TEST_ASSERT_EQUAL(0, stat(filename, &st));
  121. TEST_ASSERT(st.st_mode & S_IFREG);
  122. TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
  123. }
  124. void test_spiffs_unlink(const char* filename)
  125. {
  126. test_spiffs_create_file_with_text(filename, "unlink\n");
  127. TEST_ASSERT_EQUAL(0, unlink(filename));
  128. TEST_ASSERT_NULL(fopen(filename, "r"));
  129. }
  130. void test_spiffs_rename(const char* filename_prefix)
  131. {
  132. char name_dst[64];
  133. char name_src[64];
  134. snprintf(name_dst, sizeof(name_dst), "%s_dst.txt", filename_prefix);
  135. snprintf(name_src, sizeof(name_src), "%s_src.txt", filename_prefix);
  136. unlink(name_dst);
  137. unlink(name_src);
  138. FILE* f = fopen(name_src, "w+");
  139. TEST_ASSERT_NOT_NULL(f);
  140. const char* str = "0123456789";
  141. for (int i = 0; i < 400; ++i) {
  142. TEST_ASSERT_NOT_EQUAL(EOF, fputs(str, f));
  143. }
  144. TEST_ASSERT_EQUAL(0, fclose(f));
  145. TEST_ASSERT_EQUAL(0, rename(name_src, name_dst));
  146. TEST_ASSERT_NULL(fopen(name_src, "r"));
  147. FILE* fdst = fopen(name_dst, "r");
  148. TEST_ASSERT_NOT_NULL(fdst);
  149. TEST_ASSERT_EQUAL(0, fseek(fdst, 0, SEEK_END));
  150. TEST_ASSERT_EQUAL(4000, ftell(fdst));
  151. TEST_ASSERT_EQUAL(0, fclose(fdst));
  152. }
  153. void test_spiffs_can_opendir(const char* path)
  154. {
  155. char name_dir_file[64];
  156. const char * file_name = "test_opd.txt";
  157. snprintf(name_dir_file, sizeof(name_dir_file), "%s/%s", path, file_name);
  158. unlink(name_dir_file);
  159. test_spiffs_create_file_with_text(name_dir_file, "test_opendir\n");
  160. DIR* dir = opendir(path);
  161. TEST_ASSERT_NOT_NULL(dir);
  162. bool found = false;
  163. while (true) {
  164. struct dirent* de = readdir(dir);
  165. if (!de) {
  166. break;
  167. }
  168. if (strcasecmp(de->d_name, file_name) == 0) {
  169. found = true;
  170. break;
  171. }
  172. }
  173. TEST_ASSERT_TRUE(found);
  174. TEST_ASSERT_EQUAL(0, closedir(dir));
  175. unlink(name_dir_file);
  176. }
  177. void test_spiffs_opendir_readdir_rewinddir(const char* dir_prefix)
  178. {
  179. char name_dir_inner_file[64];
  180. char name_dir_inner[64];
  181. char name_dir_file3[64];
  182. char name_dir_file2[64];
  183. char name_dir_file1[64];
  184. snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/inner/3.txt", dir_prefix);
  185. snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/inner", dir_prefix);
  186. snprintf(name_dir_file3, sizeof(name_dir_file2), "%s/boo.bin", dir_prefix);
  187. snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/2.txt", dir_prefix);
  188. snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/1.txt", dir_prefix);
  189. unlink(name_dir_inner_file);
  190. rmdir(name_dir_inner);
  191. unlink(name_dir_file1);
  192. unlink(name_dir_file2);
  193. unlink(name_dir_file3);
  194. rmdir(dir_prefix);
  195. test_spiffs_create_file_with_text(name_dir_file1, "1\n");
  196. test_spiffs_create_file_with_text(name_dir_file2, "2\n");
  197. test_spiffs_create_file_with_text(name_dir_file3, "\01\02\03");
  198. test_spiffs_create_file_with_text(name_dir_inner_file, "3\n");
  199. DIR* dir = opendir(dir_prefix);
  200. TEST_ASSERT_NOT_NULL(dir);
  201. int count = 0;
  202. const char* names[4];
  203. while(count < 4) {
  204. struct dirent* de = readdir(dir);
  205. if (!de) {
  206. break;
  207. }
  208. printf("found '%s'\n", de->d_name);
  209. if (strcasecmp(de->d_name, "1.txt") == 0) {
  210. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  211. names[count] = "1.txt";
  212. ++count;
  213. } else if (strcasecmp(de->d_name, "2.txt") == 0) {
  214. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  215. names[count] = "2.txt";
  216. ++count;
  217. } else if (strcasecmp(de->d_name, "inner/3.txt") == 0) {
  218. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  219. names[count] = "inner/3.txt";
  220. ++count;
  221. } else if (strcasecmp(de->d_name, "boo.bin") == 0) {
  222. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  223. names[count] = "boo.bin";
  224. ++count;
  225. } else {
  226. TEST_FAIL_MESSAGE("unexpected directory entry");
  227. }
  228. }
  229. TEST_ASSERT_EQUAL(count, 4);
  230. rewinddir(dir);
  231. struct dirent* de = readdir(dir);
  232. TEST_ASSERT_NOT_NULL(de);
  233. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
  234. seekdir(dir, 3);
  235. de = readdir(dir);
  236. TEST_ASSERT_NOT_NULL(de);
  237. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
  238. seekdir(dir, 1);
  239. de = readdir(dir);
  240. TEST_ASSERT_NOT_NULL(de);
  241. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
  242. seekdir(dir, 2);
  243. de = readdir(dir);
  244. TEST_ASSERT_NOT_NULL(de);
  245. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
  246. TEST_ASSERT_EQUAL(0, closedir(dir));
  247. }
  248. typedef struct {
  249. const char* filename;
  250. bool write;
  251. size_t word_count;
  252. int seed;
  253. SemaphoreHandle_t done;
  254. int result;
  255. } read_write_test_arg_t;
  256. #define READ_WRITE_TEST_ARG_INIT(name, seed_) \
  257. { \
  258. .filename = name, \
  259. .seed = seed_, \
  260. .word_count = 4096, \
  261. .write = true, \
  262. .done = xSemaphoreCreateBinary() \
  263. }
  264. static void read_write_task(void* param)
  265. {
  266. read_write_test_arg_t* args = (read_write_test_arg_t*) param;
  267. FILE* f = fopen(args->filename, args->write ? "wb" : "rb");
  268. if (f == NULL) {
  269. args->result = ESP_ERR_NOT_FOUND;
  270. goto done;
  271. }
  272. srand(args->seed);
  273. for (size_t i = 0; i < args->word_count; ++i) {
  274. uint32_t val = rand();
  275. if (args->write) {
  276. int cnt = fwrite(&val, sizeof(val), 1, f);
  277. if (cnt != 1) {
  278. ets_printf("E(w): i=%d, cnt=%d val=%d\n\n", i, cnt, val);
  279. args->result = ESP_FAIL;
  280. goto close;
  281. }
  282. } else {
  283. uint32_t rval;
  284. int cnt = fread(&rval, sizeof(rval), 1, f);
  285. if (cnt != 1) {
  286. ets_printf("E(r): i=%d, cnt=%d rval=%d\n\n", i, cnt, rval);
  287. args->result = ESP_FAIL;
  288. goto close;
  289. }
  290. }
  291. }
  292. args->result = ESP_OK;
  293. close:
  294. fclose(f);
  295. done:
  296. xSemaphoreGive(args->done);
  297. vTaskDelay(1);
  298. vTaskDelete(NULL);
  299. }
  300. void test_spiffs_concurrent(const char* filename_prefix)
  301. {
  302. char names[4][64];
  303. for (size_t i = 0; i < 4; ++i) {
  304. snprintf(names[i], sizeof(names[i]), "%s%d", filename_prefix, i + 1);
  305. unlink(names[i]);
  306. }
  307. read_write_test_arg_t args1 = READ_WRITE_TEST_ARG_INIT(names[0], 1);
  308. read_write_test_arg_t args2 = READ_WRITE_TEST_ARG_INIT(names[1], 2);
  309. printf("writing f1 and f2\n");
  310. const int cpuid_0 = 0;
  311. const int cpuid_1 = portNUM_PROCESSORS - 1;
  312. xTaskCreatePinnedToCore(&read_write_task, "rw1", 2048, &args1, 3, NULL, cpuid_0);
  313. xTaskCreatePinnedToCore(&read_write_task, "rw2", 2048, &args2, 3, NULL, cpuid_1);
  314. xSemaphoreTake(args1.done, portMAX_DELAY);
  315. printf("f1 done\n");
  316. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  317. xSemaphoreTake(args2.done, portMAX_DELAY);
  318. printf("f2 done\n");
  319. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  320. args1.write = false;
  321. args2.write = false;
  322. read_write_test_arg_t args3 = READ_WRITE_TEST_ARG_INIT(names[2], 3);
  323. read_write_test_arg_t args4 = READ_WRITE_TEST_ARG_INIT(names[3], 4);
  324. printf("reading f1 and f2, writing f3 and f4\n");
  325. xTaskCreatePinnedToCore(&read_write_task, "rw3", 2048, &args3, 3, NULL, cpuid_1);
  326. xTaskCreatePinnedToCore(&read_write_task, "rw4", 2048, &args4, 3, NULL, cpuid_0);
  327. xTaskCreatePinnedToCore(&read_write_task, "rw1", 2048, &args1, 3, NULL, cpuid_0);
  328. xTaskCreatePinnedToCore(&read_write_task, "rw2", 2048, &args2, 3, NULL, cpuid_1);
  329. xSemaphoreTake(args1.done, portMAX_DELAY);
  330. printf("f1 done\n");
  331. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  332. xSemaphoreTake(args2.done, portMAX_DELAY);
  333. printf("f2 done\n");
  334. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  335. xSemaphoreTake(args3.done, portMAX_DELAY);
  336. printf("f3 done\n");
  337. TEST_ASSERT_EQUAL(ESP_OK, args3.result);
  338. xSemaphoreTake(args4.done, portMAX_DELAY);
  339. printf("f4 done\n");
  340. TEST_ASSERT_EQUAL(ESP_OK, args4.result);
  341. vSemaphoreDelete(args1.done);
  342. vSemaphoreDelete(args2.done);
  343. vSemaphoreDelete(args3.done);
  344. vSemaphoreDelete(args4.done);
  345. }
  346. static void test_setup()
  347. {
  348. esp_vfs_spiffs_conf_t conf = {
  349. .base_path = "/spiffs",
  350. .partition_label = spiffs_test_partition_label,
  351. .max_files = 5,
  352. .format_if_mount_failed = true
  353. };
  354. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  355. }
  356. static void test_teardown()
  357. {
  358. TEST_ESP_OK(esp_vfs_spiffs_unregister(spiffs_test_partition_label));
  359. }
  360. TEST_CASE("can initialize SPIFFS in erased partition", "[spiffs]")
  361. {
  362. const esp_partition_t* part = get_test_data_partition();
  363. TEST_ASSERT_NOT_NULL(part);
  364. TEST_ESP_OK(esp_partition_erase_range(part, 0, part->size));
  365. test_setup();
  366. size_t total = 0, used = 0;
  367. TEST_ESP_OK(esp_spiffs_info(spiffs_test_partition_label, &total, &used));
  368. printf("total: %d, used: %d\n", total, used);
  369. TEST_ASSERT_EQUAL(0, used);
  370. test_teardown();
  371. }
  372. TEST_CASE("can format mounted partition", "[spiffs]")
  373. {
  374. // Mount SPIFFS, create file, format, check that the file does not exist.
  375. const esp_partition_t* part = get_test_data_partition();
  376. TEST_ASSERT_NOT_NULL(part);
  377. test_setup();
  378. const char* filename = "/spiffs/hello.txt";
  379. test_spiffs_create_file_with_text(filename, spiffs_test_hello_str);
  380. esp_spiffs_format(part->label);
  381. FILE* f = fopen(filename, "r");
  382. TEST_ASSERT_NULL(f);
  383. test_teardown();
  384. }
  385. TEST_CASE("can format unmounted partition", "[spiffs]")
  386. {
  387. // Mount SPIFFS, create file, unmount. Format. Mount again, check that
  388. // the file does not exist.
  389. const esp_partition_t* part = get_test_data_partition();
  390. TEST_ASSERT_NOT_NULL(part);
  391. test_setup();
  392. const char* filename = "/spiffs/hello.txt";
  393. test_spiffs_create_file_with_text(filename, spiffs_test_hello_str);
  394. test_teardown();
  395. esp_spiffs_format(part->label);
  396. // Don't use test_setup here, need to mount without formatting
  397. esp_vfs_spiffs_conf_t conf = {
  398. .base_path = "/spiffs",
  399. .partition_label = spiffs_test_partition_label,
  400. .max_files = 5,
  401. .format_if_mount_failed = false
  402. };
  403. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  404. FILE* f = fopen(filename, "r");
  405. TEST_ASSERT_NULL(f);
  406. test_teardown();
  407. }
  408. TEST_CASE("can create and write file", "[spiffs]")
  409. {
  410. test_setup();
  411. test_spiffs_create_file_with_text("/spiffs/hello.txt", spiffs_test_hello_str);
  412. test_teardown();
  413. }
  414. TEST_CASE("can read file", "[spiffs]")
  415. {
  416. test_setup();
  417. test_spiffs_create_file_with_text("/spiffs/hello.txt", spiffs_test_hello_str);
  418. test_spiffs_read_file("/spiffs/hello.txt");
  419. test_teardown();
  420. }
  421. TEST_CASE("can open maximum number of files", "[spiffs]")
  422. {
  423. size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */
  424. esp_vfs_spiffs_conf_t conf = {
  425. .base_path = "/spiffs",
  426. .partition_label = spiffs_test_partition_label,
  427. .format_if_mount_failed = true,
  428. .max_files = max_files
  429. };
  430. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  431. test_spiffs_open_max_files("/spiffs/f", max_files);
  432. TEST_ESP_OK(esp_vfs_spiffs_unregister(spiffs_test_partition_label));
  433. }
  434. TEST_CASE("overwrite and append file", "[spiffs]")
  435. {
  436. test_setup();
  437. test_spiffs_overwrite_append("/spiffs/hello.txt");
  438. test_teardown();
  439. }
  440. TEST_CASE("can lseek", "[spiffs]")
  441. {
  442. test_setup();
  443. test_spiffs_lseek("/spiffs/seek.txt");
  444. test_teardown();
  445. }
  446. TEST_CASE("stat returns correct values", "[spiffs]")
  447. {
  448. test_setup();
  449. test_spiffs_stat("/spiffs/stat.txt");
  450. test_teardown();
  451. }
  452. TEST_CASE("unlink removes a file", "[spiffs]")
  453. {
  454. test_setup();
  455. test_spiffs_unlink("/spiffs/unlink.txt");
  456. test_teardown();
  457. }
  458. TEST_CASE("rename moves a file", "[spiffs]")
  459. {
  460. test_setup();
  461. test_spiffs_rename("/spiffs/move");
  462. test_teardown();
  463. }
  464. TEST_CASE("can opendir root directory of FS", "[spiffs]")
  465. {
  466. test_setup();
  467. test_spiffs_can_opendir("/spiffs");
  468. test_teardown();
  469. }
  470. TEST_CASE("opendir, readdir, rewinddir, seekdir work as expected", "[spiffs]")
  471. {
  472. test_setup();
  473. test_spiffs_opendir_readdir_rewinddir("/spiffs/dir");
  474. test_teardown();
  475. }
  476. TEST_CASE("multiple tasks can use same volume", "[spiffs]")
  477. {
  478. test_setup();
  479. test_spiffs_concurrent("/spiffs/f");
  480. test_teardown();
  481. }
  482. #ifdef CONFIG_SPIFFS_USE_MTIME
  483. TEST_CASE("mtime is updated when file is opened", "[spiffs]")
  484. {
  485. /* Open a file, check that mtime is set correctly */
  486. const char* filename = "/spiffs/time";
  487. test_setup();
  488. time_t t_before_create = time(NULL);
  489. test_spiffs_create_file_with_text(filename, "\n");
  490. time_t t_after_create = time(NULL);
  491. struct stat st;
  492. TEST_ASSERT_EQUAL(0, stat(filename, &st));
  493. printf("mtime=%d\n", (int) st.st_mtime);
  494. TEST_ASSERT(st.st_mtime >= t_before_create
  495. && st.st_mtime <= t_after_create);
  496. /* Wait a bit, open again, check that mtime is updated */
  497. vTaskDelay(2000 / portTICK_PERIOD_MS);
  498. time_t t_before_open = time(NULL);
  499. FILE *f = fopen(filename, "a");
  500. time_t t_after_open = time(NULL);
  501. TEST_ASSERT_EQUAL(0, fstat(fileno(f), &st));
  502. printf("mtime=%d\n", (int) st.st_mtime);
  503. TEST_ASSERT(st.st_mtime >= t_before_open
  504. && st.st_mtime <= t_after_open);
  505. fclose(f);
  506. /* Wait a bit, open for reading, check that mtime is not updated */
  507. vTaskDelay(2000 / portTICK_PERIOD_MS);
  508. time_t t_before_open_ro = time(NULL);
  509. f = fopen(filename, "r");
  510. TEST_ASSERT_EQUAL(0, fstat(fileno(f), &st));
  511. printf("mtime=%d\n", (int) st.st_mtime);
  512. TEST_ASSERT(t_before_open_ro > t_after_open
  513. && st.st_mtime >= t_before_open
  514. && st.st_mtime <= t_after_open);
  515. fclose(f);
  516. test_teardown();
  517. }
  518. #endif // CONFIG_SPIFFS_USE_MTIME