1 // Copyright 2015-2018 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
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19 #include <sys/time.h>
20 #include <sys/unistd.h>
21 #include "unity.h"
22 #include "test_utils.h"
23 #include "esp_log.h"
24 #include "esp_system.h"
25 #include "esp_vfs.h"
26 #include "esp_vfs_fat.h"
27 #include "freertos/FreeRTOS.h"
28 #include "freertos/task.h"
29 #include "test_fatfs_common.h"
30 #include "esp_partition.h"
31 #include "ff.h"
32 #include "esp_rom_sys.h"
33
34
test_setup(size_t max_files)35 static void test_setup(size_t max_files)
36 {
37 extern const char fatfs_start[] asm("_binary_fatfs_img_start");
38 extern const char fatfs_end[] asm("_binary_fatfs_img_end");
39 esp_vfs_fat_sdmmc_mount_config_t mount_config = {
40 .format_if_mount_failed = false,
41 .max_files = max_files
42 };
43 const esp_partition_t* part = get_test_data_partition();
44
45 TEST_ASSERT(part->size == (fatfs_end - fatfs_start - 1));
46
47 spi_flash_mmap_handle_t mmap_handle;
48 const void* mmap_ptr;
49 TEST_ESP_OK(esp_partition_mmap(part, 0, part->size, SPI_FLASH_MMAP_DATA, &mmap_ptr, &mmap_handle));
50 bool content_valid = memcmp(fatfs_start, mmap_ptr, part->size) == 0;
51 spi_flash_munmap(mmap_handle);
52
53 if (!content_valid) {
54 printf("Copying fatfs.img into test partition...\n");
55 esp_partition_erase_range(part, 0, part->size);
56 for (int i = 0; i < part->size; i+= SPI_FLASH_SEC_SIZE) {
57 ESP_ERROR_CHECK( esp_partition_write(part, i, fatfs_start + i, SPI_FLASH_SEC_SIZE) );
58 }
59 }
60
61 TEST_ESP_OK(esp_vfs_fat_rawflash_mount("/spiflash", "flash_test", &mount_config));
62 }
63
test_teardown(void)64 static void test_teardown(void)
65 {
66 TEST_ESP_OK(esp_vfs_fat_rawflash_unmount("/spiflash","flash_test"));
67 }
68
69 TEST_CASE("(raw) can read file", "[fatfs]")
70 {
71 test_setup(5);
72 FILE* f = fopen("/spiflash/hello.txt", "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 test_teardown();
80 }
81
82 TEST_CASE("(raw) can open maximum number of files", "[fatfs]")
83 {
84 size_t max_files = FOPEN_MAX - 3; /* account for stdin, stdout, stderr */
85 test_setup(max_files);
86
87 FILE** files = calloc(max_files, sizeof(FILE*));
88 for (size_t i = 0; i < max_files; ++i) {
89 char name[32];
90 snprintf(name, sizeof(name), "/spiflash/f/%d.txt", i + 1);
91 files[i] = fopen(name, "r");
92 TEST_ASSERT_NOT_NULL(files[i]);
93 }
94 /* close everything and clean up */
95 for (size_t i = 0; i < max_files; ++i) {
96 fclose(files[i]);
97 }
98 free(files);
99 test_teardown();
100
101 }
102
103
104 TEST_CASE("(raw) can lseek", "[fatfs]")
105 {
106 test_setup(5);
107 FILE* f = fopen("/spiflash/hello.txt", "r");
108 TEST_ASSERT_NOT_NULL(f);
109 TEST_ASSERT_EQUAL(0, fseek(f, 2, SEEK_CUR));
110 TEST_ASSERT_EQUAL('l', fgetc(f));
111 TEST_ASSERT_EQUAL(0, fseek(f, 4, SEEK_SET));
112 TEST_ASSERT_EQUAL('o', fgetc(f));
113 TEST_ASSERT_EQUAL(0, fseek(f, -5, SEEK_END));
114 TEST_ASSERT_EQUAL('r', fgetc(f));
115 TEST_ASSERT_EQUAL(0, fseek(f, 3, SEEK_END));
116 TEST_ASSERT_EQUAL(17, ftell(f));
117
118 TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_END));
119 TEST_ASSERT_EQUAL(14, ftell(f));
120 TEST_ASSERT_EQUAL(0, fseek(f, 0, SEEK_SET));
121 test_teardown();
122 }
123
124 TEST_CASE("(raw) stat returns correct values", "[fatfs]")
125 {
126 test_setup(5);
127 struct tm tm;
128 tm.tm_year = 2018 - 1900;
129 tm.tm_mon = 5; // Note: month can be 0-11 & not 1-12
130 tm.tm_mday = 13;
131 tm.tm_hour = 11;
132 tm.tm_min = 2;
133 tm.tm_sec = 10;
134 time_t t = mktime(&tm);
135 printf("Reference time: %s", asctime(&tm));
136
137 struct stat st;
138 TEST_ASSERT_EQUAL(0, stat("/spiflash/stat.txt", &st));
139
140 time_t mtime = st.st_mtime;
141 struct tm mtm;
142 localtime_r(&mtime, &mtm);
143 printf("File time: %s", asctime(&mtm));
144 TEST_ASSERT(mtime > t); // Modification time should be in future wrt ref time
145
146 TEST_ASSERT(st.st_mode & S_IFREG);
147 TEST_ASSERT_FALSE(st.st_mode & S_IFDIR);
148
149 memset(&st, 0, sizeof(st));
150 TEST_ASSERT_EQUAL(0, stat("/spiflash", &st));
151 TEST_ASSERT(st.st_mode & S_IFDIR);
152 TEST_ASSERT_FALSE(st.st_mode & S_IFREG);
153
154 test_teardown();
155 }
156
157
158
159 TEST_CASE("(raw) can opendir root directory of FS", "[fatfs]")
160 {
161 test_setup(5);
162 DIR* dir = opendir("/spiflash");
163 TEST_ASSERT_NOT_NULL(dir);
164 bool found = false;
165 while (true) {
166 struct dirent* de = readdir(dir);
167 if (!de) {
168 break;
169 }
170 if (strcasecmp(de->d_name, "test_opd.txt") == 0) {
171 found = true;
172 break;
173 }
174 }
175 TEST_ASSERT_TRUE(found);
176 TEST_ASSERT_EQUAL(0, closedir(dir));
177
178 test_teardown();
179 }
180 TEST_CASE("(raw) opendir, readdir, rewinddir, seekdir work as expected", "[fatfs]")
181 {
182 test_setup(5);
183
184 DIR* dir = opendir("/spiflash/dir");
185 TEST_ASSERT_NOT_NULL(dir);
186 int count = 0;
187 const char* names[4];
188 while(count < 4) {
189 struct dirent* de = readdir(dir);
190 if (!de) {
191 break;
192 }
193 printf("found '%s'\n", de->d_name);
194 if (strcasecmp(de->d_name, "1.txt") == 0) {
195 TEST_ASSERT_TRUE(de->d_type == DT_REG);
196 names[count] = "1.txt";
197 ++count;
198 } else if (strcasecmp(de->d_name, "2.txt") == 0) {
199 TEST_ASSERT_TRUE(de->d_type == DT_REG);
200 names[count] = "2.txt";
201 ++count;
202 } else if (strcasecmp(de->d_name, "inner") == 0) {
203 TEST_ASSERT_TRUE(de->d_type == DT_DIR);
204 names[count] = "inner";
205 ++count;
206 } else if (strcasecmp(de->d_name, "boo.bin") == 0) {
207 TEST_ASSERT_TRUE(de->d_type == DT_REG);
208 names[count] = "boo.bin";
209 ++count;
210 } else {
211 TEST_FAIL_MESSAGE("unexpected directory entry");
212 }
213 }
214 TEST_ASSERT_EQUAL(count, 4);
215
216 rewinddir(dir);
217 struct dirent* de = readdir(dir);
218 TEST_ASSERT_NOT_NULL(de);
219 TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[0]));
220 seekdir(dir, 3);
221 de = readdir(dir);
222 TEST_ASSERT_NOT_NULL(de);
223 TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[3]));
224 seekdir(dir, 1);
225 de = readdir(dir);
226 TEST_ASSERT_NOT_NULL(de);
227 TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[1]));
228 seekdir(dir, 2);
229 de = readdir(dir);
230 TEST_ASSERT_NOT_NULL(de);
231 TEST_ASSERT_EQUAL(0, strcasecmp(de->d_name, names[2]));
232
233 TEST_ASSERT_EQUAL(0, closedir(dir));
234
235 test_teardown();
236 }
237
238
239 typedef struct {
240 const char* filename;
241 size_t word_count;
242 int seed;
243 int val;
244 SemaphoreHandle_t done;
245 int result;
246 } read_test_arg_t;
247
248 #define READ_TEST_ARG_INIT(name, seed_, val_) \
249 { \
250 .filename = name, \
251 .seed = seed_, \
252 .word_count = 8000, \
253 .val = val_, \
254 .done = xSemaphoreCreateBinary() \
255 }
256
read_task(void * param)257 static void read_task(void* param)
258 {
259 read_test_arg_t* args = (read_test_arg_t*) param;
260 FILE* f = fopen(args->filename, "rb");
261 if (f == NULL) {
262 args->result = ESP_ERR_NOT_FOUND;
263 goto done;
264 }
265
266 srand(args->seed);
267 for (size_t i = 0; i < args->word_count; ++i) {
268 uint32_t rval;
269 int cnt = fread(&rval, sizeof(rval), 1, f);
270 if (cnt != 1 || rval != args->val) {
271 esp_rom_printf("E(r): i=%d, cnt=%d rval=%d val=%d\n\n", i, cnt, rval, args->val);
272 args->result = ESP_FAIL;
273 goto close;
274 }
275 }
276 args->result = ESP_OK;
277
278 close:
279 fclose(f);
280
281 done:
282 xSemaphoreGive(args->done);
283 vTaskDelay(1);
284 vTaskDelete(NULL);
285 }
286
287
288 TEST_CASE("(raw) multiple tasks can use same volume", "[fatfs]")
289 {
290 test_setup(5);
291 char names[4][64];
292 for (size_t i = 0; i < 4; ++i) {
293 snprintf(names[i], sizeof(names[i]), "/spiflash/ccrnt/%d.txt", i + 1);
294 }
295
296 read_test_arg_t args1 = READ_TEST_ARG_INIT(names[0], 1, 0x31313131);
297 read_test_arg_t args2 = READ_TEST_ARG_INIT(names[1], 2, 0x32323232);
298 read_test_arg_t args3 = READ_TEST_ARG_INIT(names[2], 3, 0x33333333);
299 read_test_arg_t args4 = READ_TEST_ARG_INIT(names[3], 4, 0x34343434);
300
301 const int cpuid_0 = 0;
302 const int cpuid_1 = portNUM_PROCESSORS - 1;
303 const int stack_size = 4096;
304
305 printf("reading files 1.txt 2.txt 3.txt 4.txt \n");
306
307 xTaskCreatePinnedToCore(&read_task, "r1", stack_size, &args1, 3, NULL, cpuid_1);
308 xTaskCreatePinnedToCore(&read_task, "r2", stack_size, &args2, 3, NULL, cpuid_0);
309 xTaskCreatePinnedToCore(&read_task, "r3", stack_size, &args3, 3, NULL, cpuid_0);
310 xTaskCreatePinnedToCore(&read_task, "r4", stack_size, &args4, 3, NULL, cpuid_1);
311
312 xSemaphoreTake(args1.done, portMAX_DELAY);
313 printf("1.txt done\n");
314 TEST_ASSERT_EQUAL(ESP_OK, args1.result);
315 xSemaphoreTake(args2.done, portMAX_DELAY);
316 printf("2.txt done\n");
317 TEST_ASSERT_EQUAL(ESP_OK, args2.result);
318 xSemaphoreTake(args3.done, portMAX_DELAY);
319 printf("3.txt done\n");
320 TEST_ASSERT_EQUAL(ESP_OK, args3.result);
321 xSemaphoreTake(args4.done, portMAX_DELAY);
322 printf("4.txt done\n");
323 TEST_ASSERT_EQUAL(ESP_OK, args4.result);
324
325 vSemaphoreDelete(args1.done);
326 vSemaphoreDelete(args2.done);
327 vSemaphoreDelete(args3.done);
328 vSemaphoreDelete(args4.done);
329 test_teardown();
330 }
331
332 TEST_CASE("(raw) read speed test", "[fatfs][timeout=60]")
333 {
334 test_setup(5);
335
336 const size_t buf_size = 16 * 1024;
337 uint32_t* buf = (uint32_t*) calloc(1, buf_size);
338 const size_t file_size = 256 * 1024;
339 const char* file = "/spiflash/256k.bin";
340
341 test_fatfs_rw_speed(file, buf, 4 * 1024, file_size, false);
342 test_fatfs_rw_speed(file, buf, 8 * 1024, file_size, false);
343 test_fatfs_rw_speed(file, buf, 16 * 1024, file_size, false);
344
345 free(buf);
346 test_teardown();
347 }
348