test_spiffs.c 29 KB

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