test_fatfs_common.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 <sys/stat.h>
  14. #include <errno.h>
  15. #include <utime.h>
  16. #include "unity.h"
  17. #include "esp_log.h"
  18. #include "esp_system.h"
  19. #include "esp_vfs.h"
  20. #include "esp_vfs_fat.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/task.h"
  23. #include "ff.h"
  24. #include "test_fatfs_common.h"
  25. #include "esp_rom_sys.h"
  26. const char* fatfs_test_hello_str = "Hello, World!\n";
  27. const char* fatfs_test_hello_str_utf = "世界,你好!\n";
  28. void test_fatfs_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_fatfs_overwrite_append(const char* filename)
  36. {
  37. /* Create new file with 'aaaa' */
  38. test_fatfs_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_fatfs_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_fatfs_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(fatfs_test_hello_str), cb);
  70. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str, buf));
  71. TEST_ASSERT_EQUAL(0, fclose(f));
  72. }
  73. void test_fatfs_read_file_utf_8(const char* filename)
  74. {
  75. FILE* f = fopen(filename, "r");
  76. TEST_ASSERT_NOT_NULL(f);
  77. char buf[64] = { 0 }; //Doubled buffer size to allow for longer UTF-8 strings
  78. int cb = fread(buf, 1, sizeof(buf), f);
  79. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str_utf), cb);
  80. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str_utf, buf));
  81. TEST_ASSERT_EQUAL(0, fclose(f));
  82. }
  83. void test_fatfs_pread_file(const char* filename)
  84. {
  85. char buf[32] = { 0 };
  86. const int fd = open(filename, O_RDONLY);
  87. TEST_ASSERT_NOT_EQUAL(-1, fd);
  88. int r = pread(fd, buf, sizeof(buf), 0); // it is a regular read() with offset==0
  89. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str, buf));
  90. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str), r);
  91. memset(buf, 0, sizeof(buf));
  92. r = pread(fd, buf, sizeof(buf), 1); // offset==1
  93. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str + 1, buf));
  94. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str) - 1, r);
  95. memset(buf, 0, sizeof(buf));
  96. r = pread(fd, buf, sizeof(buf), 5); // offset==5
  97. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str + 5, buf));
  98. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str) - 5, r);
  99. // regular read() should work now because pread() should not affect the current position in file
  100. memset(buf, 0, sizeof(buf));
  101. r = read(fd, buf, sizeof(buf)); // note that this is read() and not pread()
  102. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str, buf));
  103. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str), r);
  104. memset(buf, 0, sizeof(buf));
  105. r = pread(fd, buf, sizeof(buf), 10); // offset==10
  106. TEST_ASSERT_EQUAL(0, strcmp(fatfs_test_hello_str + 10, buf));
  107. TEST_ASSERT_EQUAL(strlen(fatfs_test_hello_str) - 10, r);
  108. memset(buf, 0, sizeof(buf));
  109. r = pread(fd, buf, sizeof(buf), strlen(fatfs_test_hello_str) + 1); // offset to EOF
  110. TEST_ASSERT_EQUAL(0, r);
  111. TEST_ASSERT_EQUAL(0, close(fd));
  112. }
  113. static void test_pwrite(const char *filename, off_t offset, const char *msg)
  114. {
  115. const int fd = open(filename, O_WRONLY);
  116. TEST_ASSERT_NOT_EQUAL(-1, fd);
  117. const off_t current_pos = lseek(fd, 0, SEEK_END); // O_APPEND is not the same - jumps to the end only before write()
  118. const int r = pwrite(fd, msg, strlen(msg), offset);
  119. TEST_ASSERT_EQUAL(strlen(msg), r);
  120. TEST_ASSERT_EQUAL(current_pos, lseek(fd, 0, SEEK_CUR)); // pwrite should not move the pointer
  121. TEST_ASSERT_EQUAL(0, close(fd));
  122. }
  123. static void test_file_content(const char *filename, const char *msg)
  124. {
  125. char buf[32] = { 0 };
  126. const int fd = open(filename, O_RDONLY);
  127. TEST_ASSERT_NOT_EQUAL(-1, fd);
  128. int r = read(fd, buf, sizeof(buf));
  129. TEST_ASSERT_NOT_EQUAL(-1, r);
  130. TEST_ASSERT_EQUAL(0, strcmp(msg, buf));
  131. TEST_ASSERT_EQUAL(0, close(fd));
  132. }
  133. void test_fatfs_pwrite_file(const char *filename)
  134. {
  135. int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
  136. TEST_ASSERT_NOT_EQUAL(-1, fd);
  137. TEST_ASSERT_EQUAL(0, close(fd));
  138. test_pwrite(filename, 0, "Hello");
  139. test_file_content(filename, "Hello");
  140. test_pwrite(filename, strlen("Hello"), ", world!");
  141. test_file_content(filename, "Hello, world!");
  142. test_pwrite(filename, strlen("Hello, "), "Dolly");
  143. test_file_content(filename, "Hello, Dolly!");
  144. }
  145. void test_fatfs_open_max_files(const char* filename_prefix, size_t files_count)
  146. {
  147. FILE** files = calloc(files_count, sizeof(FILE*));
  148. for (size_t i = 0; i < files_count; ++i) {
  149. char name[32];
  150. snprintf(name, sizeof(name), "%s_%d.txt", filename_prefix, i);
  151. files[i] = fopen(name, "w");
  152. TEST_ASSERT_NOT_NULL(files[i]);
  153. }
  154. /* close everything and clean up */
  155. for (size_t i = 0; i < files_count; ++i) {
  156. fclose(files[i]);
  157. }
  158. free(files);
  159. }
  160. void test_fatfs_lseek(const char* filename)
  161. {
  162. FILE* f = fopen(filename, "wb+");
  163. TEST_ASSERT_NOT_NULL(f);
  164. TEST_ASSERT_EQUAL(11, fprintf(f, "0123456789\n"));
  165. TEST_ASSERT_EQUAL(0, fseek(f, -2, SEEK_CUR));
  166. TEST_ASSERT_EQUAL('9', fgetc(f));
  167. TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_SET));
  168. TEST_ASSERT_EQUAL('3', fgetc(f));
  169. TEST_ASSERT_EQUAL(0, fseek(f, -3, SEEK_END));
  170. TEST_ASSERT_EQUAL('8', fgetc(f));
  171. TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_END));
  172. TEST_ASSERT_EQUAL(14, ftell(f));
  173. TEST_ASSERT_EQUAL(4, fprintf(f, "abc\n"));
  174. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  175. TEST_ASSERT_EQUAL(18, ftell(f));
  176. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_SET));
  177. char buf[20];
  178. TEST_ASSERT_EQUAL(18, fread(buf, 1, sizeof(buf), f));
  179. const char ref_buf[] = "0123456789\n\0\0\0abc\n";
  180. TEST_ASSERT_EQUAL_INT8_ARRAY(ref_buf, buf, sizeof(ref_buf) - 1);
  181. TEST_ASSERT_EQUAL(0, fclose(f));
  182. #ifdef CONFIG_FATFS_USE_FASTSEEK
  183. f = fopen(filename, "rb+");
  184. TEST_ASSERT_NOT_NULL(f);
  185. TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
  186. TEST_ASSERT_EQUAL(18, ftell(f));
  187. TEST_ASSERT_EQUAL(0, fseek(f, -4, SEEK_CUR));
  188. TEST_ASSERT_EQUAL(14, ftell(f));
  189. TEST_ASSERT_EQUAL(0, fseek(f, -14, SEEK_CUR));
  190. TEST_ASSERT_EQUAL(0, ftell(f));
  191. TEST_ASSERT_EQUAL(18, fread(buf, 1, sizeof(buf), f));
  192. TEST_ASSERT_EQUAL_INT8_ARRAY(ref_buf, buf, sizeof(ref_buf) - 1);
  193. TEST_ASSERT_EQUAL(0, fclose(f));
  194. #endif
  195. }
  196. void test_fatfs_truncate_file(const char* filename)
  197. {
  198. int read = 0;
  199. int truncated_len = 0;
  200. const char input[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  201. char output[sizeof(input)];
  202. FILE* f = fopen(filename, "wb");
  203. TEST_ASSERT_NOT_NULL(f);
  204. TEST_ASSERT_EQUAL(strlen(input), fprintf(f, input));
  205. TEST_ASSERT_EQUAL(0, fclose(f));
  206. // Extending file beyond size is not supported
  207. TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input) + 1));
  208. TEST_ASSERT_EQUAL(errno, EPERM);
  209. TEST_ASSERT_EQUAL(-1, truncate(filename, -1));
  210. TEST_ASSERT_EQUAL(errno, EINVAL);
  211. // Truncating should succeed
  212. const char truncated_1[] = "ABCDEFGHIJ";
  213. truncated_len = strlen(truncated_1);
  214. TEST_ASSERT_EQUAL(0, truncate(filename, truncated_len));
  215. f = fopen(filename, "rb");
  216. TEST_ASSERT_NOT_NULL(f);
  217. memset(output, 0, sizeof(output));
  218. read = fread(output, 1, sizeof(output), f);
  219. TEST_ASSERT_EQUAL(truncated_len, read);
  220. TEST_ASSERT_EQUAL_STRING_LEN(truncated_1, output, truncated_len);
  221. TEST_ASSERT_EQUAL(0, fclose(f));
  222. // Once truncated, the new file size should be the basis
  223. // whether truncation should succeed or not
  224. TEST_ASSERT_EQUAL(-1, truncate(filename, truncated_len + 1));
  225. TEST_ASSERT_EQUAL(EPERM, errno);
  226. TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input)));
  227. TEST_ASSERT_EQUAL(EPERM, errno);
  228. TEST_ASSERT_EQUAL(-1, truncate(filename, strlen(input) + 1));
  229. TEST_ASSERT_EQUAL(EPERM, errno);
  230. TEST_ASSERT_EQUAL(-1, truncate(filename, -1));
  231. TEST_ASSERT_EQUAL(EINVAL, errno);
  232. // Truncating a truncated file should succeed
  233. const char truncated_2[] = "ABCDE";
  234. truncated_len = strlen(truncated_2);
  235. TEST_ASSERT_EQUAL(0, truncate(filename, truncated_len));
  236. f = fopen(filename, "rb");
  237. TEST_ASSERT_NOT_NULL(f);
  238. memset(output, 0, sizeof(output));
  239. read = fread(output, 1, sizeof(output), f);
  240. TEST_ASSERT_EQUAL(truncated_len, read);
  241. TEST_ASSERT_EQUAL_STRING_LEN(truncated_2, output, truncated_len);
  242. TEST_ASSERT_EQUAL(0, fclose(f));
  243. }
  244. void test_fatfs_ftruncate_file(const char* filename)
  245. {
  246. int truncated_len = 0;
  247. const char input[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  248. char output[sizeof(input)];
  249. int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
  250. TEST_ASSERT_NOT_EQUAL(-1, fd);
  251. TEST_ASSERT_EQUAL(strlen(input), write(fd, input, strlen(input)));
  252. // Extending file beyond size is not supported
  253. TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input) + 1));
  254. TEST_ASSERT_EQUAL(errno, EPERM);
  255. TEST_ASSERT_EQUAL(-1, ftruncate(fd, -1));
  256. TEST_ASSERT_EQUAL(errno, EINVAL);
  257. // Truncating should succeed
  258. const char truncated_1[] = "ABCDEFGHIJ";
  259. truncated_len = strlen(truncated_1);
  260. TEST_ASSERT_EQUAL(0, ftruncate(fd, truncated_len));
  261. TEST_ASSERT_EQUAL(0, close(fd));
  262. // open file for reading and validate the content
  263. fd = open(filename, O_RDONLY);
  264. TEST_ASSERT_NOT_EQUAL(-1, fd);
  265. memset(output, 0, sizeof(output));
  266. TEST_ASSERT_EQUAL(truncated_len, read(fd, output, sizeof(output)));
  267. TEST_ASSERT_EQUAL_STRING_LEN(truncated_1, output, truncated_len);
  268. TEST_ASSERT_EQUAL(0, close(fd));
  269. // further truncate the file
  270. fd = open(filename, O_WRONLY);
  271. TEST_ASSERT_NOT_EQUAL(-1, fd);
  272. // Once truncated, the new file size should be the basis
  273. // whether truncation should succeed or not
  274. TEST_ASSERT_EQUAL(-1, ftruncate(fd, truncated_len + 1));
  275. TEST_ASSERT_EQUAL(EPERM, errno);
  276. TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input)));
  277. TEST_ASSERT_EQUAL(EPERM, errno);
  278. TEST_ASSERT_EQUAL(-1, ftruncate(fd, strlen(input) + 1));
  279. TEST_ASSERT_EQUAL(EPERM, errno);
  280. TEST_ASSERT_EQUAL(-1, ftruncate(fd, -1));
  281. TEST_ASSERT_EQUAL(EINVAL, errno);
  282. // Truncating a truncated file should succeed
  283. const char truncated_2[] = "ABCDE";
  284. truncated_len = strlen(truncated_2);
  285. TEST_ASSERT_EQUAL(0, ftruncate(fd, truncated_len));
  286. TEST_ASSERT_EQUAL(0, close(fd));
  287. // open file for reading and validate the content
  288. fd = open(filename, O_RDONLY);
  289. TEST_ASSERT_NOT_EQUAL(-1, fd);
  290. memset(output, 0, sizeof(output));
  291. TEST_ASSERT_EQUAL(truncated_len, read(fd, output, sizeof(output)));
  292. TEST_ASSERT_EQUAL_STRING_LEN(truncated_2, output, truncated_len);
  293. TEST_ASSERT_EQUAL(0, close(fd));
  294. }
  295. void test_fatfs_stat(const char* filename, const char* root_dir)
  296. {
  297. struct tm tm = {
  298. .tm_year = 2017 - 1900,
  299. .tm_mon = 11,
  300. .tm_mday = 8,
  301. .tm_hour = 19,
  302. .tm_min = 51,
  303. .tm_sec = 10
  304. };
  305. time_t t = mktime(&tm);
  306. printf("Setting time: %s", asctime(&tm));
  307. struct timeval now = { .tv_sec = t };
  308. settimeofday(&now, NULL);
  309. test_fatfs_create_file_with_text(filename, "foo\n");
  310. struct stat st;
  311. TEST_ASSERT_EQUAL(0, stat(filename, &st));
  312. time_t mtime = st.st_mtime;
  313. struct tm mtm;
  314. localtime_r(&mtime, &mtm);
  315. printf("File time: %s", asctime(&mtm));
  316. TEST_ASSERT(llabs(mtime - t) < 2); // fatfs library stores time with 2 second precision
  317. TEST_ASSERT(st.st_mode & S_IFREG);
  318. TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
  319. memset(&st, 0, sizeof(st));
  320. TEST_ASSERT_EQUAL(0, stat(root_dir, &st));
  321. TEST_ASSERT(st.st_mode & S_IFDIR);
  322. TEST_ASSERT_FALSE(st.st_mode & S_IFREG);
  323. }
  324. void test_fatfs_mtime_dst(const char* filename, const char* root_dir)
  325. {
  326. struct timeval tv = { 1653638041, 0 };
  327. settimeofday(&tv, NULL);
  328. setenv("TZ", "MST7MDT,M3.2.0,M11.1.0", 1);
  329. tzset();
  330. struct tm tm;
  331. time_t sys_time = tv.tv_sec;
  332. localtime_r(&sys_time, &tm);
  333. printf("Setting time: %s", asctime(&tm));
  334. test_fatfs_create_file_with_text(filename, "foo\n");
  335. struct stat st;
  336. TEST_ASSERT_EQUAL(0, stat(filename, &st));
  337. time_t mtime = st.st_mtime;
  338. struct tm mtm;
  339. localtime_r(&mtime, &mtm);
  340. printf("File time: %s", asctime(&mtm));
  341. TEST_ASSERT(llabs(mtime - sys_time) < 2); // fatfs library stores time with 2 second precision
  342. unsetenv("TZ");
  343. tzset();
  344. }
  345. void test_fatfs_utime(const char* filename, const char* root_dir)
  346. {
  347. struct stat achieved_stat;
  348. struct tm desired_tm;
  349. struct utimbuf desired_time = {
  350. .actime = 0, // access time is not supported
  351. .modtime = 0,
  352. };
  353. time_t false_now = 0;
  354. memset(&desired_tm, 0, sizeof(struct tm));
  355. {
  356. // Setting up a false actual time - used when the file is created and for modification with the current time
  357. desired_tm.tm_mon = 10 - 1;
  358. desired_tm.tm_mday = 31;
  359. desired_tm.tm_year = 2018 - 1900;
  360. desired_tm.tm_hour = 10;
  361. desired_tm.tm_min = 35;
  362. desired_tm.tm_sec = 23;
  363. false_now = mktime(&desired_tm);
  364. struct timeval now = { .tv_sec = false_now };
  365. settimeofday(&now, NULL);
  366. }
  367. test_fatfs_create_file_with_text(filename, "");
  368. // 00:00:00. January 1st, 1980 - FATFS cannot handle earlier dates
  369. desired_tm.tm_mon = 1 - 1;
  370. desired_tm.tm_mday = 1;
  371. desired_tm.tm_year = 1980 - 1900;
  372. desired_tm.tm_hour = 0;
  373. desired_tm.tm_min = 0;
  374. desired_tm.tm_sec = 0;
  375. printf("Testing mod. time: %s", asctime(&desired_tm));
  376. desired_time.modtime = mktime(&desired_tm);
  377. TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
  378. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  379. TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
  380. // current time
  381. TEST_ASSERT_EQUAL(0, utime(filename, NULL));
  382. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  383. printf("Mod. time changed to (false actual time): %s", ctime(&achieved_stat.st_mtime));
  384. TEST_ASSERT_NOT_EQUAL(desired_time.modtime, achieved_stat.st_mtime);
  385. TEST_ASSERT(false_now - achieved_stat.st_mtime <= 2); // two seconds of tolerance are given
  386. // 23:59:08. December 31st, 2037
  387. desired_tm.tm_mon = 12 - 1;
  388. desired_tm.tm_mday = 31;
  389. desired_tm.tm_year = 2037 - 1900;
  390. desired_tm.tm_hour = 23;
  391. desired_tm.tm_min = 59;
  392. desired_tm.tm_sec = 8;
  393. printf("Testing mod. time: %s", asctime(&desired_tm));
  394. desired_time.modtime = mktime(&desired_tm);
  395. TEST_ASSERT_EQUAL(0, utime(filename, &desired_time));
  396. TEST_ASSERT_EQUAL(0, stat(filename, &achieved_stat));
  397. TEST_ASSERT_EQUAL_UINT32(desired_time.modtime, achieved_stat.st_mtime);
  398. //WARNING: it has the Unix Millenium bug (Y2K38)
  399. // 00:00:00. January 1st, 1970 - FATFS cannot handle years before 1980
  400. desired_tm.tm_mon = 1 - 1;
  401. desired_tm.tm_mday = 1;
  402. desired_tm.tm_year = 1970 - 1900;
  403. desired_tm.tm_hour = 0;
  404. desired_tm.tm_min = 0;
  405. desired_tm.tm_sec = 0;
  406. printf("Testing mod. time: %s", asctime(&desired_tm));
  407. desired_time.modtime = mktime(&desired_tm);
  408. TEST_ASSERT_EQUAL(-1, utime(filename, &desired_time));
  409. TEST_ASSERT_EQUAL(EINVAL, errno);
  410. }
  411. void test_fatfs_unlink(const char* filename)
  412. {
  413. test_fatfs_create_file_with_text(filename, "unlink\n");
  414. TEST_ASSERT_EQUAL(0, unlink(filename));
  415. TEST_ASSERT_NULL(fopen(filename, "r"));
  416. }
  417. void test_fatfs_link_rename(const char* filename_prefix)
  418. {
  419. char name_copy[64];
  420. char name_dst[64];
  421. char name_src[64];
  422. snprintf(name_copy, sizeof(name_copy), "%s_cpy.txt", filename_prefix);
  423. snprintf(name_dst, sizeof(name_dst), "%s_dst.txt", filename_prefix);
  424. snprintf(name_src, sizeof(name_src), "%s_src.txt", filename_prefix);
  425. unlink(name_copy);
  426. unlink(name_dst);
  427. unlink(name_src);
  428. FILE* f = fopen(name_src, "w+");
  429. TEST_ASSERT_NOT_NULL(f);
  430. const char* str = "0123456789";
  431. for (int i = 0; i < 4000; ++i) {
  432. TEST_ASSERT_NOT_EQUAL(EOF, fputs(str, f));
  433. }
  434. TEST_ASSERT_EQUAL(0, fclose(f));
  435. TEST_ASSERT_EQUAL(0, link(name_src, name_copy));
  436. FILE* fcopy = fopen(name_copy, "r");
  437. TEST_ASSERT_NOT_NULL(fcopy);
  438. TEST_ASSERT_EQUAL(0, fseek(fcopy, 0, SEEK_END));
  439. TEST_ASSERT_EQUAL(40000, ftell(fcopy));
  440. TEST_ASSERT_EQUAL(0, fclose(fcopy));
  441. TEST_ASSERT_EQUAL(0, rename(name_copy, name_dst));
  442. TEST_ASSERT_NULL(fopen(name_copy, "r"));
  443. FILE* fdst = fopen(name_dst, "r");
  444. TEST_ASSERT_NOT_NULL(fdst);
  445. TEST_ASSERT_EQUAL(0, fseek(fdst, 0, SEEK_END));
  446. TEST_ASSERT_EQUAL(40000, ftell(fdst));
  447. TEST_ASSERT_EQUAL(0, fclose(fdst));
  448. }
  449. void test_fatfs_mkdir_rmdir(const char* filename_prefix)
  450. {
  451. char name_dir1[64];
  452. char name_dir2[64];
  453. char name_dir2_file[64];
  454. snprintf(name_dir1, sizeof(name_dir1), "%s1", filename_prefix);
  455. snprintf(name_dir2, sizeof(name_dir2), "%s2", filename_prefix);
  456. snprintf(name_dir2_file, sizeof(name_dir2_file), "%s2/1.txt", filename_prefix);
  457. TEST_ASSERT_EQUAL(0, mkdir(name_dir1, 0755));
  458. struct stat st;
  459. TEST_ASSERT_EQUAL(0, stat(name_dir1, &st));
  460. TEST_ASSERT_TRUE(st.st_mode & S_IFDIR);
  461. TEST_ASSERT_FALSE(st.st_mode & S_IFREG);
  462. TEST_ASSERT_EQUAL(0, rmdir(name_dir1));
  463. TEST_ASSERT_EQUAL(-1, stat(name_dir1, &st));
  464. TEST_ASSERT_EQUAL(0, mkdir(name_dir2, 0755));
  465. test_fatfs_create_file_with_text(name_dir2_file, "foo\n");
  466. TEST_ASSERT_EQUAL(0, stat(name_dir2, &st));
  467. TEST_ASSERT_TRUE(st.st_mode & S_IFDIR);
  468. TEST_ASSERT_FALSE(st.st_mode & S_IFREG);
  469. TEST_ASSERT_EQUAL(0, stat(name_dir2_file, &st));
  470. TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
  471. TEST_ASSERT_TRUE(st.st_mode & S_IFREG);
  472. TEST_ASSERT_EQUAL(-1, rmdir(name_dir2));
  473. TEST_ASSERT_EQUAL(0, unlink(name_dir2_file));
  474. TEST_ASSERT_EQUAL(0, rmdir(name_dir2));
  475. }
  476. void test_fatfs_can_opendir(const char* path)
  477. {
  478. char name_dir_file[64];
  479. const char * file_name = "test_opd.txt";
  480. snprintf(name_dir_file, sizeof(name_dir_file), "%s/%s", path, file_name);
  481. unlink(name_dir_file);
  482. test_fatfs_create_file_with_text(name_dir_file, "test_opendir\n");
  483. DIR* dir = opendir(path);
  484. TEST_ASSERT_NOT_NULL(dir);
  485. bool found = false;
  486. while (true) {
  487. struct dirent* de = readdir(dir);
  488. if (!de) {
  489. break;
  490. }
  491. if (strcasecmp(de->d_name, file_name) == 0) {
  492. found = true;
  493. break;
  494. }
  495. }
  496. TEST_ASSERT_TRUE(found);
  497. TEST_ASSERT_EQUAL(0, closedir(dir));
  498. unlink(name_dir_file);
  499. }
  500. void test_fatfs_opendir_readdir_rewinddir(const char* dir_prefix)
  501. {
  502. char name_dir_inner_file[64];
  503. char name_dir_inner[64];
  504. char name_dir_file3[64];
  505. char name_dir_file2[64];
  506. char name_dir_file1[64];
  507. snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/inner/3.txt", dir_prefix);
  508. snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/inner", dir_prefix);
  509. snprintf(name_dir_file3, sizeof(name_dir_file2), "%s/boo.bin", dir_prefix);
  510. snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/2.txt", dir_prefix);
  511. snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/1.txt", dir_prefix);
  512. unlink(name_dir_inner_file);
  513. rmdir(name_dir_inner);
  514. unlink(name_dir_file1);
  515. unlink(name_dir_file2);
  516. unlink(name_dir_file3);
  517. rmdir(dir_prefix);
  518. TEST_ASSERT_EQUAL(0, mkdir(dir_prefix, 0755));
  519. test_fatfs_create_file_with_text(name_dir_file1, "1\n");
  520. test_fatfs_create_file_with_text(name_dir_file2, "2\n");
  521. test_fatfs_create_file_with_text(name_dir_file3, "\01\02\03");
  522. TEST_ASSERT_EQUAL(0, mkdir(name_dir_inner, 0755));
  523. test_fatfs_create_file_with_text(name_dir_inner_file, "3\n");
  524. DIR* dir = opendir(dir_prefix);
  525. TEST_ASSERT_NOT_NULL(dir);
  526. int count = 0;
  527. const char* names[4];
  528. while(count < 4) {
  529. struct dirent* de = readdir(dir);
  530. if (!de) {
  531. break;
  532. }
  533. printf("found '%s'\n", de->d_name);
  534. if (strcasecmp(de->d_name, "1.txt") == 0) {
  535. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  536. names[count] = "1.txt";
  537. ++count;
  538. } else if (strcasecmp(de->d_name, "2.txt") == 0) {
  539. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  540. names[count] = "2.txt";
  541. ++count;
  542. } else if (strcasecmp(de->d_name, "inner") == 0) {
  543. TEST_ASSERT_TRUE(de->d_type == DT_DIR);
  544. names[count] = "inner";
  545. ++count;
  546. } else if (strcasecmp(de->d_name, "boo.bin") == 0) {
  547. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  548. names[count] = "boo.bin";
  549. ++count;
  550. } else {
  551. TEST_FAIL_MESSAGE("unexpected directory entry");
  552. }
  553. }
  554. TEST_ASSERT_EQUAL(count, 4);
  555. rewinddir(dir);
  556. struct dirent* de = readdir(dir);
  557. TEST_ASSERT_NOT_NULL(de);
  558. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
  559. seekdir(dir, 3);
  560. de = readdir(dir);
  561. TEST_ASSERT_NOT_NULL(de);
  562. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
  563. seekdir(dir, 1);
  564. de = readdir(dir);
  565. TEST_ASSERT_NOT_NULL(de);
  566. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
  567. seekdir(dir, 2);
  568. de = readdir(dir);
  569. TEST_ASSERT_NOT_NULL(de);
  570. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
  571. TEST_ASSERT_EQUAL(0, closedir(dir));
  572. }
  573. void test_fatfs_opendir_readdir_rewinddir_utf_8(const char* dir_prefix)
  574. {
  575. char name_dir_inner_file[64];
  576. char name_dir_inner[64];
  577. char name_dir_file3[64];
  578. char name_dir_file2[64];
  579. char name_dir_file1[64];
  580. snprintf(name_dir_inner_file, sizeof(name_dir_inner_file), "%s/内部目录/内部文件.txt", dir_prefix);
  581. snprintf(name_dir_inner, sizeof(name_dir_inner), "%s/内部目录", dir_prefix);
  582. snprintf(name_dir_file3, sizeof(name_dir_file3), "%s/文件三.bin", dir_prefix);
  583. snprintf(name_dir_file2, sizeof(name_dir_file2), "%s/文件二.txt", dir_prefix);
  584. snprintf(name_dir_file1, sizeof(name_dir_file1), "%s/文件一.txt", dir_prefix);
  585. unlink(name_dir_inner_file);
  586. rmdir(name_dir_inner);
  587. unlink(name_dir_file1);
  588. unlink(name_dir_file2);
  589. unlink(name_dir_file3);
  590. rmdir(dir_prefix);
  591. TEST_ASSERT_EQUAL(0, mkdir(dir_prefix, 0755));
  592. test_fatfs_create_file_with_text(name_dir_file1, "一号\n");
  593. test_fatfs_create_file_with_text(name_dir_file2, "二号\n");
  594. test_fatfs_create_file_with_text(name_dir_file3, "\0一\0二\0三");
  595. TEST_ASSERT_EQUAL(0, mkdir(name_dir_inner, 0755));
  596. test_fatfs_create_file_with_text(name_dir_inner_file, "三号\n");
  597. DIR* dir = opendir(dir_prefix);
  598. TEST_ASSERT_NOT_NULL(dir);
  599. int count = 0;
  600. const char* names[4];
  601. while(count < 4) {
  602. struct dirent* de = readdir(dir);
  603. if (!de) {
  604. break;
  605. }
  606. printf("found '%s'\n", de->d_name);
  607. if (strcasecmp(de->d_name, "文件一.txt") == 0) {
  608. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  609. names[count] = "文件一.txt";
  610. ++count;
  611. } else if (strcasecmp(de->d_name, "文件二.txt") == 0) {
  612. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  613. names[count] = "文件二.txt";
  614. ++count;
  615. } else if (strcasecmp(de->d_name, "内部目录") == 0) {
  616. TEST_ASSERT_TRUE(de->d_type == DT_DIR);
  617. names[count] = "内部目录";
  618. ++count;
  619. } else if (strcasecmp(de->d_name, "文件三.bin") == 0) {
  620. TEST_ASSERT_TRUE(de->d_type == DT_REG);
  621. names[count] = "文件三.bin";
  622. ++count;
  623. } else {
  624. TEST_FAIL_MESSAGE("unexpected directory entry");
  625. }
  626. }
  627. TEST_ASSERT_EQUAL(count, 4);
  628. rewinddir(dir);
  629. struct dirent* de = readdir(dir);
  630. TEST_ASSERT_NOT_NULL(de);
  631. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
  632. seekdir(dir, 3);
  633. de = readdir(dir);
  634. TEST_ASSERT_NOT_NULL(de);
  635. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
  636. seekdir(dir, 1);
  637. de = readdir(dir);
  638. TEST_ASSERT_NOT_NULL(de);
  639. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
  640. seekdir(dir, 2);
  641. de = readdir(dir);
  642. TEST_ASSERT_NOT_NULL(de);
  643. TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
  644. TEST_ASSERT_EQUAL(0, closedir(dir));
  645. }
  646. typedef struct {
  647. const char* filename;
  648. bool write;
  649. size_t word_count;
  650. int seed;
  651. SemaphoreHandle_t done;
  652. int result;
  653. } read_write_test_arg_t;
  654. #define READ_WRITE_TEST_ARG_INIT(name, seed_) \
  655. { \
  656. .filename = name, \
  657. .seed = seed_, \
  658. .word_count = 8192, \
  659. .write = true, \
  660. .done = xSemaphoreCreateBinary() \
  661. }
  662. static void read_write_task(void* param)
  663. {
  664. read_write_test_arg_t* args = (read_write_test_arg_t*) param;
  665. FILE* f = fopen(args->filename, args->write ? "wb" : "rb");
  666. if (f == NULL) {
  667. args->result = ESP_ERR_NOT_FOUND;
  668. goto done;
  669. }
  670. srand(args->seed);
  671. for (size_t i = 0; i < args->word_count; ++i) {
  672. uint32_t val = rand();
  673. if (args->write) {
  674. int cnt = fwrite(&val, sizeof(val), 1, f);
  675. if (cnt != 1) {
  676. esp_rom_printf("E(w): i=%d, cnt=%d val=%d\n\n", i, cnt, val);
  677. args->result = ESP_FAIL;
  678. goto close;
  679. }
  680. } else {
  681. uint32_t rval;
  682. int cnt = fread(&rval, sizeof(rval), 1, f);
  683. if (cnt != 1 || rval != val) {
  684. esp_rom_printf("E(r): i=%d, cnt=%d rval=%d val=%d\n\n", i, cnt, rval, val);
  685. args->result = ESP_FAIL;
  686. goto close;
  687. }
  688. }
  689. }
  690. args->result = ESP_OK;
  691. close:
  692. fclose(f);
  693. done:
  694. xSemaphoreGive(args->done);
  695. vTaskDelay(1);
  696. vTaskDelete(NULL);
  697. }
  698. void test_fatfs_concurrent(const char* filename_prefix)
  699. {
  700. char names[4][64];
  701. for (size_t i = 0; i < 4; ++i) {
  702. snprintf(names[i], sizeof(names[i]), "%s%d", filename_prefix, i + 1);
  703. unlink(names[i]);
  704. }
  705. read_write_test_arg_t args1 = READ_WRITE_TEST_ARG_INIT(names[0], 1);
  706. read_write_test_arg_t args2 = READ_WRITE_TEST_ARG_INIT(names[1], 2);
  707. printf("writing f1 and f2\n");
  708. const int cpuid_0 = 0;
  709. const int cpuid_1 = portNUM_PROCESSORS - 1;
  710. const int stack_size = 4096;
  711. xTaskCreatePinnedToCore(&read_write_task, "rw1", stack_size, &args1, 3, NULL, cpuid_0);
  712. xTaskCreatePinnedToCore(&read_write_task, "rw2", stack_size, &args2, 3, NULL, cpuid_1);
  713. xSemaphoreTake(args1.done, portMAX_DELAY);
  714. printf("f1 done\n");
  715. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  716. xSemaphoreTake(args2.done, portMAX_DELAY);
  717. printf("f2 done\n");
  718. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  719. args1.write = false;
  720. args2.write = false;
  721. read_write_test_arg_t args3 = READ_WRITE_TEST_ARG_INIT(names[2], 3);
  722. read_write_test_arg_t args4 = READ_WRITE_TEST_ARG_INIT(names[3], 4);
  723. printf("reading f1 and f2, writing f3 and f4\n");
  724. xTaskCreatePinnedToCore(&read_write_task, "rw3", stack_size, &args3, 3, NULL, cpuid_1);
  725. xTaskCreatePinnedToCore(&read_write_task, "rw4", stack_size, &args4, 3, NULL, cpuid_0);
  726. xTaskCreatePinnedToCore(&read_write_task, "rw1", stack_size, &args1, 3, NULL, cpuid_0);
  727. xTaskCreatePinnedToCore(&read_write_task, "rw2", stack_size, &args2, 3, NULL, cpuid_1);
  728. xSemaphoreTake(args1.done, portMAX_DELAY);
  729. printf("f1 done\n");
  730. TEST_ASSERT_EQUAL(ESP_OK, args1.result);
  731. xSemaphoreTake(args2.done, portMAX_DELAY);
  732. printf("f2 done\n");
  733. TEST_ASSERT_EQUAL(ESP_OK, args2.result);
  734. xSemaphoreTake(args3.done, portMAX_DELAY);
  735. printf("f3 done\n");
  736. TEST_ASSERT_EQUAL(ESP_OK, args3.result);
  737. xSemaphoreTake(args4.done, portMAX_DELAY);
  738. printf("f4 done\n");
  739. TEST_ASSERT_EQUAL(ESP_OK, args4.result);
  740. vSemaphoreDelete(args1.done);
  741. vSemaphoreDelete(args2.done);
  742. vSemaphoreDelete(args3.done);
  743. vSemaphoreDelete(args4.done);
  744. }
  745. void test_fatfs_rw_speed(const char* filename, void* buf, size_t buf_size, size_t file_size, bool is_write)
  746. {
  747. const size_t buf_count = file_size / buf_size;
  748. FILE* f = fopen(filename, (is_write) ? "wb" : "rb");
  749. TEST_ASSERT_NOT_NULL(f);
  750. struct timeval tv_start;
  751. gettimeofday(&tv_start, NULL);
  752. for (size_t n = 0; n < buf_count; ++n) {
  753. if (is_write) {
  754. TEST_ASSERT_EQUAL(buf_size, write(fileno(f), buf, buf_size));
  755. } else {
  756. if (read(fileno(f), buf, buf_size) != buf_size) {
  757. printf("reading at n=%d, eof=%d", n, feof(f));
  758. TEST_FAIL();
  759. }
  760. }
  761. }
  762. struct timeval tv_end;
  763. gettimeofday(&tv_end, NULL);
  764. TEST_ASSERT_EQUAL(0, fclose(f));
  765. float t_s = tv_end.tv_sec - tv_start.tv_sec + 1e-6f * (tv_end.tv_usec - tv_start.tv_usec);
  766. printf("%s %d bytes (block size %d) in %.3fms (%.3f MB/s)\n",
  767. (is_write)?"Wrote":"Read", file_size, buf_size, t_s * 1e3,
  768. file_size / (1024.0f * 1024.0f * t_s));
  769. }
  770. void test_fatfs_info(const char* base_path, const char* filepath)
  771. {
  772. // Empty FS
  773. uint64_t total_bytes = 0;
  774. uint64_t free_bytes = 0;
  775. TEST_ASSERT_EQUAL(ESP_OK, esp_vfs_fat_info(base_path, &total_bytes, &free_bytes));
  776. ESP_LOGD("fatfs info", "total_bytes=%llu, free_bytes=%llu", total_bytes, free_bytes);
  777. TEST_ASSERT_NOT_EQUAL(0, total_bytes);
  778. // FS with a file
  779. FILE* f = fopen(filepath, "wb");
  780. TEST_ASSERT_NOT_NULL(f);
  781. TEST_ASSERT_TRUE(fputs(fatfs_test_hello_str, f) != EOF);
  782. TEST_ASSERT_EQUAL(0, fclose(f));
  783. uint64_t free_bytes_new = 0;
  784. TEST_ASSERT_EQUAL(ESP_OK, esp_vfs_fat_info(base_path, &total_bytes, &free_bytes_new));
  785. ESP_LOGD("fatfs info", "total_bytes=%llu, free_bytes_new=%llu", total_bytes, free_bytes_new);
  786. TEST_ASSERT_NOT_EQUAL(free_bytes, free_bytes_new);
  787. // File removed
  788. TEST_ASSERT_EQUAL(0, remove(filepath));
  789. TEST_ASSERT_EQUAL(ESP_OK, esp_vfs_fat_info(base_path, &total_bytes, &free_bytes_new));
  790. ESP_LOGD("fatfs info", "total_bytes=%llu, free_bytes_after_delete=%llu", total_bytes, free_bytes_new);
  791. TEST_ASSERT_EQUAL(free_bytes, free_bytes_new);
  792. }