test_fatfs_common.c 27 KB

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