test_spiffs.c 26 KB

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