test_spiffs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. void test_spiffs_readdir_many_files(const char* dir_prefix)
  249. {
  250. const int n_files = 40;
  251. const int n_folders = 4;
  252. unsigned char file_count[n_files * n_folders];
  253. memset(file_count, 0, sizeof(file_count)/sizeof(file_count[0]));
  254. char file_name[ESP_VFS_PATH_MAX + CONFIG_SPIFFS_OBJ_NAME_LEN];
  255. /* clean stale files before the test */
  256. DIR* dir = opendir(dir_prefix);
  257. if (dir) {
  258. while (true) {
  259. struct dirent* de = readdir(dir);
  260. if (!de) {
  261. break;
  262. }
  263. int len = snprintf(file_name, sizeof(file_name), "%s/%s", dir_prefix, de->d_name);
  264. assert(len < sizeof(file_name));
  265. unlink(file_name);
  266. }
  267. }
  268. /* create files */
  269. for (int d = 0; d < n_folders; ++d) {
  270. printf("filling directory %d\n", d);
  271. for (int f = 0; f < n_files; ++f) {
  272. snprintf(file_name, sizeof(file_name), "%s/%d/%d.txt", dir_prefix, d, f);
  273. test_spiffs_create_file_with_text(file_name, file_name);
  274. }
  275. }
  276. /* list files */
  277. for (int d = 0; d < n_folders; ++d) {
  278. printf("listing files in directory %d\n", d);
  279. snprintf(file_name, sizeof(file_name), "%s/%d", dir_prefix, d);
  280. dir = opendir(file_name);
  281. TEST_ASSERT_NOT_NULL(dir);
  282. while (true) {
  283. struct dirent* de = readdir(dir);
  284. if (!de) {
  285. break;
  286. }
  287. int file_id;
  288. TEST_ASSERT_EQUAL(1, sscanf(de->d_name, "%d.txt", &file_id));
  289. file_count[file_id + d * n_files]++;
  290. }
  291. closedir(dir);
  292. }
  293. /* check that all created files have been seen */
  294. for (int d = 0; d < n_folders; ++d) {
  295. printf("checking that all files have been found in directory %d\n", d);
  296. for (int f = 0; f < n_files; ++f) {
  297. TEST_ASSERT_EQUAL(1, file_count[f + d * n_files]);
  298. }
  299. }
  300. }
  301. typedef struct {
  302. const char* filename;
  303. bool write;
  304. size_t word_count;
  305. int seed;
  306. SemaphoreHandle_t done;
  307. int result;
  308. } read_write_test_arg_t;
  309. #define READ_WRITE_TEST_ARG_INIT(name, seed_) \
  310. { \
  311. .filename = name, \
  312. .seed = seed_, \
  313. .word_count = 4096, \
  314. .write = true, \
  315. .done = xSemaphoreCreateBinary() \
  316. }
  317. static void read_write_task(void* param)
  318. {
  319. read_write_test_arg_t* args = (read_write_test_arg_t*) param;
  320. FILE* f = fopen(args->filename, args->write ? "wb" : "rb");
  321. if (f == NULL) {
  322. args->result = ESP_ERR_NOT_FOUND;
  323. goto done;
  324. }
  325. srand(args->seed);
  326. for (size_t i = 0; i < args->word_count; ++i) {
  327. uint32_t val = rand();
  328. if (args->write) {
  329. int cnt = fwrite(&val, sizeof(val), 1, f);
  330. if (cnt != 1) {
  331. ets_printf("E(w): i=%d, cnt=%d val=%d\n\n", i, cnt, val);
  332. args->result = ESP_FAIL;
  333. goto close;
  334. }
  335. } else {
  336. uint32_t rval;
  337. int cnt = fread(&rval, sizeof(rval), 1, f);
  338. if (cnt != 1) {
  339. ets_printf("E(r): i=%d, cnt=%d rval=%d\n\n", i, cnt, rval);
  340. args->result = ESP_FAIL;
  341. goto close;
  342. }
  343. }
  344. }
  345. args->result = ESP_OK;
  346. close:
  347. fclose(f);
  348. done:
  349. xSemaphoreGive(args->done);
  350. vTaskDelay(1);
  351. vTaskDelete(NULL);
  352. }
  353. void test_spiffs_concurrent(const char* filename_prefix)
  354. {
  355. char names[4][64];
  356. for (size_t i = 0; i < 4; ++i) {
  357. snprintf(names[i], sizeof(names[i]), "%s%d", filename_prefix, i + 1);
  358. unlink(names[i]);
  359. }
  360. read_write_test_arg_t args1 = READ_WRITE_TEST_ARG_INIT(names[0], 1);
  361. read_write_test_arg_t args2 = READ_WRITE_TEST_ARG_INIT(names[1], 2);
  362. printf("writing f1 and f2\n");
  363. const int cpuid_0 = 0;
  364. const int cpuid_1 = portNUM_PROCESSORS - 1;
  365. xTaskCreatePinnedToCore(&read_write_task, "rw1", 2048, &args1, 3, NULL, cpuid_0);
  366. xTaskCreatePinnedToCore(&read_write_task, "rw2", 2048, &args2, 3, NULL, cpuid_1);
  367. xSemaphoreTake(args1.done, portMAX_DELAY);
  368. printf("f1 done\n");
  369. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  370. xSemaphoreTake(args2.done, portMAX_DELAY);
  371. printf("f2 done\n");
  372. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  373. args1.write = false;
  374. args2.write = false;
  375. read_write_test_arg_t args3 = READ_WRITE_TEST_ARG_INIT(names[2], 3);
  376. read_write_test_arg_t args4 = READ_WRITE_TEST_ARG_INIT(names[3], 4);
  377. printf("reading f1 and f2, writing f3 and f4\n");
  378. xTaskCreatePinnedToCore(&read_write_task, "rw3", 2048, &args3, 3, NULL, cpuid_1);
  379. xTaskCreatePinnedToCore(&read_write_task, "rw4", 2048, &args4, 3, NULL, cpuid_0);
  380. xTaskCreatePinnedToCore(&read_write_task, "rw1", 2048, &args1, 3, NULL, cpuid_0);
  381. xTaskCreatePinnedToCore(&read_write_task, "rw2", 2048, &args2, 3, NULL, cpuid_1);
  382. xSemaphoreTake(args1.done, portMAX_DELAY);
  383. printf("f1 done\n");
  384. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  385. xSemaphoreTake(args2.done, portMAX_DELAY);
  386. printf("f2 done\n");
  387. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  388. xSemaphoreTake(args3.done, portMAX_DELAY);
  389. printf("f3 done\n");
  390. TEST_ASSERT_EQUAL(ESP_OK, args3.result);
  391. xSemaphoreTake(args4.done, portMAX_DELAY);
  392. printf("f4 done\n");
  393. TEST_ASSERT_EQUAL(ESP_OK, args4.result);
  394. vSemaphoreDelete(args1.done);
  395. vSemaphoreDelete(args2.done);
  396. vSemaphoreDelete(args3.done);
  397. vSemaphoreDelete(args4.done);
  398. }
  399. static void test_setup(void)
  400. {
  401. esp_vfs_spiffs_conf_t conf = {
  402. .base_path = "/spiffs",
  403. .partition_label = spiffs_test_partition_label,
  404. .max_files = 5,
  405. .format_if_mount_failed = true
  406. };
  407. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  408. }
  409. static void test_teardown(void)
  410. {
  411. TEST_ESP_OK(esp_vfs_spiffs_unregister(spiffs_test_partition_label));
  412. }
  413. TEST_CASE("can initialize SPIFFS in erased partition", "[spiffs]")
  414. {
  415. const esp_partition_t* part = get_test_data_partition();
  416. TEST_ASSERT_NOT_NULL(part);
  417. TEST_ESP_OK(esp_partition_erase_range(part, 0, part->size));
  418. test_setup();
  419. size_t total = 0, used = 0;
  420. TEST_ESP_OK(esp_spiffs_info(spiffs_test_partition_label, &total, &used));
  421. printf("total: %d, used: %d\n", total, used);
  422. TEST_ASSERT_EQUAL(0, used);
  423. test_teardown();
  424. }
  425. TEST_CASE("can format mounted partition", "[spiffs]")
  426. {
  427. // Mount SPIFFS, create file, format, check that the file does not exist.
  428. const esp_partition_t* part = get_test_data_partition();
  429. TEST_ASSERT_NOT_NULL(part);
  430. test_setup();
  431. const char* filename = "/spiffs/hello.txt";
  432. test_spiffs_create_file_with_text(filename, spiffs_test_hello_str);
  433. esp_spiffs_format(part->label);
  434. FILE* f = fopen(filename, "r");
  435. TEST_ASSERT_NULL(f);
  436. test_teardown();
  437. }
  438. TEST_CASE("can format unmounted partition", "[spiffs]")
  439. {
  440. // Mount SPIFFS, create file, unmount. Format. Mount again, check that
  441. // the file does not exist.
  442. const esp_partition_t* part = get_test_data_partition();
  443. TEST_ASSERT_NOT_NULL(part);
  444. test_setup();
  445. const char* filename = "/spiffs/hello.txt";
  446. test_spiffs_create_file_with_text(filename, spiffs_test_hello_str);
  447. test_teardown();
  448. esp_spiffs_format(part->label);
  449. // Don't use test_setup here, need to mount without formatting
  450. esp_vfs_spiffs_conf_t conf = {
  451. .base_path = "/spiffs",
  452. .partition_label = spiffs_test_partition_label,
  453. .max_files = 5,
  454. .format_if_mount_failed = false
  455. };
  456. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  457. FILE* f = fopen(filename, "r");
  458. TEST_ASSERT_NULL(f);
  459. test_teardown();
  460. }
  461. TEST_CASE("can create and write file", "[spiffs]")
  462. {
  463. test_setup();
  464. test_spiffs_create_file_with_text("/spiffs/hello.txt", spiffs_test_hello_str);
  465. test_teardown();
  466. }
  467. TEST_CASE("can read file", "[spiffs]")
  468. {
  469. test_setup();
  470. test_spiffs_create_file_with_text("/spiffs/hello.txt", spiffs_test_hello_str);
  471. test_spiffs_read_file("/spiffs/hello.txt");
  472. test_teardown();
  473. }
  474. TEST_CASE("can open maximum number of files", "[spiffs]")
  475. {
  476. size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */
  477. esp_vfs_spiffs_conf_t conf = {
  478. .base_path = "/spiffs",
  479. .partition_label = spiffs_test_partition_label,
  480. .format_if_mount_failed = true,
  481. .max_files = max_files
  482. };
  483. TEST_ESP_OK(esp_vfs_spiffs_register(&conf));
  484. test_spiffs_open_max_files("/spiffs/f", max_files);
  485. TEST_ESP_OK(esp_vfs_spiffs_unregister(spiffs_test_partition_label));
  486. }
  487. TEST_CASE("overwrite and append file", "[spiffs]")
  488. {
  489. test_setup();
  490. test_spiffs_overwrite_append("/spiffs/hello.txt");
  491. test_teardown();
  492. }
  493. TEST_CASE("can lseek", "[spiffs]")
  494. {
  495. test_setup();
  496. test_spiffs_lseek("/spiffs/seek.txt");
  497. test_teardown();
  498. }
  499. TEST_CASE("stat returns correct values", "[spiffs]")
  500. {
  501. test_setup();
  502. test_spiffs_stat("/spiffs/stat.txt");
  503. test_teardown();
  504. }
  505. TEST_CASE("unlink removes a file", "[spiffs]")
  506. {
  507. test_setup();
  508. test_spiffs_unlink("/spiffs/unlink.txt");
  509. test_teardown();
  510. }
  511. TEST_CASE("rename moves a file", "[spiffs]")
  512. {
  513. test_setup();
  514. test_spiffs_rename("/spiffs/move");
  515. test_teardown();
  516. }
  517. TEST_CASE("can opendir root directory of FS", "[spiffs]")
  518. {
  519. test_setup();
  520. test_spiffs_can_opendir("/spiffs");
  521. test_teardown();
  522. }
  523. TEST_CASE("opendir, readdir, rewinddir, seekdir work as expected", "[spiffs]")
  524. {
  525. test_setup();
  526. test_spiffs_opendir_readdir_rewinddir("/spiffs/dir");
  527. test_teardown();
  528. }
  529. TEST_CASE("readdir with large number of files", "[spiffs][timeout=30]")
  530. {
  531. test_setup();
  532. test_spiffs_readdir_many_files("/spiffs/dir2");
  533. test_teardown();
  534. }
  535. TEST_CASE("multiple tasks can use same volume", "[spiffs]")
  536. {
  537. test_setup();
  538. test_spiffs_concurrent("/spiffs/f");
  539. test_teardown();
  540. }
  541. #ifdef CONFIG_SPIFFS_USE_MTIME
  542. TEST_CASE("mtime is updated when file is opened", "[spiffs]")
  543. {
  544. /* Open a file, check that mtime is set correctly */
  545. const char* filename = "/spiffs/time";
  546. test_setup();
  547. time_t t_before_create = time(NULL);
  548. test_spiffs_create_file_with_text(filename, "\n");
  549. time_t t_after_create = time(NULL);
  550. struct stat st;
  551. TEST_ASSERT_EQUAL(0, stat(filename, &st));
  552. printf("mtime=%d\n", (int) st.st_mtime);
  553. TEST_ASSERT(st.st_mtime >= t_before_create
  554. && st.st_mtime <= t_after_create);
  555. /* Wait a bit, open again, check that mtime is updated */
  556. vTaskDelay(2000 / portTICK_PERIOD_MS);
  557. time_t t_before_open = time(NULL);
  558. FILE *f = fopen(filename, "a");
  559. time_t t_after_open = time(NULL);
  560. TEST_ASSERT_EQUAL(0, fstat(fileno(f), &st));
  561. printf("mtime=%d\n", (int) st.st_mtime);
  562. TEST_ASSERT(st.st_mtime >= t_before_open
  563. && st.st_mtime <= t_after_open);
  564. fclose(f);
  565. /* Wait a bit, open for reading, check that mtime is not updated */
  566. vTaskDelay(2000 / portTICK_PERIOD_MS);
  567. time_t t_before_open_ro = time(NULL);
  568. f = fopen(filename, "r");
  569. TEST_ASSERT_EQUAL(0, fstat(fileno(f), &st));
  570. printf("mtime=%d\n", (int) st.st_mtime);
  571. TEST_ASSERT(t_before_open_ro > t_after_open
  572. && st.st_mtime >= t_before_open
  573. && st.st_mtime <= t_after_open);
  574. fclose(f);
  575. test_teardown();
  576. }
  577. TEST_CASE("utime() works well", "[spiffs]")
  578. {
  579. const char filename[] = "/spiffs/utime.txt";
  580. struct stat achieved_stat;
  581. struct tm desired_tm;
  582. struct utimbuf desired_time = {
  583. .actime = 0, // access time is not supported
  584. .modtime = 0,
  585. };
  586. time_t false_now = 0;
  587. memset(&desired_tm, 0, sizeof(struct tm));
  588. test_setup();
  589. {
  590. // Setting up a false actual time - used when the file is created and for modification with the current time
  591. desired_tm.tm_mon = 10 - 1;
  592. desired_tm.tm_mday = 31;
  593. desired_tm.tm_year = 2018 - 1900;
  594. desired_tm.tm_hour = 10;
  595. desired_tm.tm_min = 35;
  596. desired_tm.tm_sec = 23;
  597. false_now = mktime(&desired_tm);
  598. struct timeval now = { .tv_sec = false_now };
  599. settimeofday(&now, NULL);
  600. }
  601. test_spiffs_create_file_with_text(filename, "");
  602. // 00:00:00. January 1st, 1900
  603. desired_tm.tm_mon = 1 - 1;
  604. desired_tm.tm_mday = 1;
  605. desired_tm.tm_year = 0;
  606. desired_tm.tm_hour = 0;
  607. desired_tm.tm_min = 0;
  608. desired_tm.tm_sec = 0;
  609. printf("Testing mod. time: %s", asctime(&desired_tm));
  610. desired_time.modtime = mktime(&desired_tm);
  611. TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
  612. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  613. TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
  614. // 23:59:08. December 31st, 2145
  615. desired_tm.tm_mon = 12 - 1;
  616. desired_tm.tm_mday = 31;
  617. desired_tm.tm_year = 2145 - 1900;
  618. desired_tm.tm_hour = 23;
  619. desired_tm.tm_min = 59;
  620. desired_tm.tm_sec = 8;
  621. printf("Testing mod. time: %s", asctime(&desired_tm));
  622. desired_time.modtime = mktime(&desired_tm);
  623. TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
  624. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  625. TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
  626. // Current time
  627. TEST_ASSERT_EQUAL(0, utime(filename, NULL));
  628. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  629. printf("Mod. time changed to (false actual time): %s", ctime(&achieved_stat.st_mtime));
  630. TEST_ASSERT_NOT_EQUAL(desired_time.modtime, achieved_stat.st_mtime);
  631. TEST_ASSERT(false_now - achieved_stat.st_mtime <= 2); // two seconds of tolerance are given
  632. test_teardown();
  633. }
  634. #endif // CONFIG_SPIFFS_USE_MTIME