test_spiffs.c 22 KB

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