test_spiffs.c 29 KB

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