1 // Copyright 2015-2016 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 <unistd.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include "unity.h"
20 #include "driver/uart.h"
21 #include "esp_vfs.h"
22 #include "esp_vfs_dev.h"
23 #include "esp_vfs_fat.h"
24 #include "wear_levelling.h"
25 
26 static wl_handle_t test_wl_handle;
27 
28 TEST_CASE("Can use access() for UART", "[vfs]")
29 {
30     const char *uarts[] = {
31         "/dev/uart/0",
32         "/dev/uart/1",
33 #if SOC_UART_NUM > 2
34         "/dev/uart/2"
35 #endif
36         };
37 
38     uart_driver_install(UART_NUM_0, 256, 0, 0, NULL, 0);
39     uart_driver_install(UART_NUM_1, 256, 0, 0, NULL, 0);
40 #if SOC_UART_NUM > 2
41     uart_driver_install(UART_NUM_2, 256, 0, 0, NULL, 0);
42 #endif
43 
44     for (size_t i = 0; i < sizeof(uarts)/sizeof(uarts[0]); ++i) {
45         TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], F_OK), 0, uarts[i]);
46 
47         TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK), 0, uarts[i]);
48         TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], W_OK), 0, uarts[i]);
49         TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], X_OK), -1, uarts[i]);
50         TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
51 
52         TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | W_OK), 0, uarts[i]);
53         TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | X_OK), -1, uarts[i]);
54         TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
55         TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], W_OK | X_OK), -1, uarts[i]);
56         TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
57         TEST_ASSERT_EQUAL_MESSAGE(access(uarts[i], R_OK | W_OK | X_OK), -1, uarts[i]);
58         TEST_ASSERT_EQUAL_MESSAGE(errno, EACCES, uarts[i]);
59     }
60 
61     TEST_ASSERT_EQUAL(access("/dev/uart/3", F_OK), -1);
62     TEST_ASSERT_EQUAL(errno, ENOENT);
63 
64     uart_driver_delete(UART_NUM_0);
65     uart_driver_delete(UART_NUM_1);
66 #if SOC_UART_NUM > 2
67     uart_driver_delete(UART_NUM_2);
68 #endif
69 }
70 
test_spi_flash_setup(void)71 static inline void test_spi_flash_setup(void)
72 {
73     esp_vfs_fat_sdmmc_mount_config_t mount_config = {
74         .format_if_mount_failed = true,
75         .max_files = 5
76     };
77 
78     TEST_ESP_OK(esp_vfs_fat_spiflash_mount("/spiflash", NULL, &mount_config, &test_wl_handle));
79 }
80 
test_spi_flash_teardown(void)81 static inline void test_spi_flash_teardown(void)
82 {
83     TEST_ESP_OK(esp_vfs_fat_spiflash_unmount("/spiflash", test_wl_handle));
84 }
85 
test_fatfs_create_file(const char * name)86 static inline void test_fatfs_create_file(const char *name)
87 {
88     int fd = open(name, O_WRONLY | O_CREAT | O_TRUNC);
89     TEST_ASSERT_NOT_EQUAL(fd, -1);
90     TEST_ASSERT_EQUAL(0, close(fd));
91 }
92 
test_fatfs_delete_file(const char * name)93 static inline void test_fatfs_delete_file(const char *name)
94 {
95     int ret = unlink(name);
96     TEST_ASSERT_EQUAL(ret, 0);
97 }
98 
99 TEST_CASE("Can use access() for FATFS", "[vfs][fatfs][wear_levelling]")
100 {
101     const char *path = "/spiflash/access.txt";
102 
103     test_spi_flash_setup();
104 
105     {
106         int ret = access(path, F_OK);
107 
108         if (ret != -1) {
109             // it wasn't deleted before so we delete it now to pass the test
110             // case the next time
111             test_fatfs_delete_file(path);
112         }
113 
114         TEST_ASSERT_EQUAL(ret, -1);
115         TEST_ASSERT_EQUAL(errno, ENOENT);
116     }
117 
118     test_fatfs_create_file(path);
119     TEST_ASSERT_EQUAL(access(path, F_OK), 0);
120 
121     TEST_ASSERT_EQUAL(access(path, R_OK), 0);
122     TEST_ASSERT_EQUAL(access(path, W_OK), 0);
123     TEST_ASSERT_EQUAL(access(path, X_OK), 0);
124     TEST_ASSERT_EQUAL(access(path, R_OK | W_OK), 0);
125     TEST_ASSERT_EQUAL(access(path, R_OK | X_OK), 0);
126     TEST_ASSERT_EQUAL(access(path, W_OK | X_OK), 0);
127     TEST_ASSERT_EQUAL(access(path, R_OK | W_OK | X_OK), 0);
128 
129     //TODO f_chmod the file and re-test the access rights (this requires
130     // f_chmod support to be implemented in VFS)
131 
132     test_fatfs_delete_file(path);
133     TEST_ASSERT_EQUAL(access(path, F_OK), -1);
134     TEST_ASSERT_EQUAL(errno, ENOENT);
135 
136     test_spi_flash_teardown();
137 }
138