1 /*
2 * Copyright (c) 2016 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7
8 #include "test_fat.h"
9 #include <string.h>
10
test_file_open(void)11 static int test_file_open(void)
12 {
13 int res;
14
15 TC_PRINT("\nOpen tests:\n");
16
17 if (check_file_dir_exists(TEST_FILE)) {
18 TC_PRINT("Opening existing file %s\n", TEST_FILE);
19 } else {
20 TC_PRINT("Creating new file %s\n", TEST_FILE);
21 }
22
23 /* Verify fs_open() */
24 res = fs_open(&filep, TEST_FILE, FS_O_CREATE | FS_O_RDWR);
25 if (res) {
26 TC_PRINT("Failed opening file [%d]\n", res);
27 return res;
28 }
29
30 TC_PRINT("Opened file %s\n", TEST_FILE);
31
32 if (check_file_dir_exists(TEST_FILE)) {
33 TC_PRINT("File now exists %s\n", TEST_FILE);
34 }
35
36 return res;
37 }
38
test_file_write(void)39 int test_file_write(void)
40 {
41 ssize_t brw;
42 int res;
43
44 TC_PRINT("\nWrite tests:\n");
45
46 /* Verify fs_seek() */
47 res = fs_seek(&filep, 0, FS_SEEK_SET);
48 if (res) {
49 TC_PRINT("fs_seek failed [%d]\n", res);
50 fs_close(&filep);
51 return res;
52 }
53
54 TC_PRINT("Data written:\"%s\"\n\n", test_str);
55
56 /* Verify fs_write() */
57 brw = fs_write(&filep, (char *)test_str, strlen(test_str));
58 if (brw < 0) {
59 TC_PRINT("Failed writing to file [%zd]\n", brw);
60 fs_close(&filep);
61 return brw;
62 }
63
64 if (brw < strlen(test_str)) {
65 TC_PRINT("Unable to complete write. Volume full.\n");
66 TC_PRINT("Number of bytes written: [%zd]\n", brw);
67 fs_close(&filep);
68 return TC_FAIL;
69 }
70
71 TC_PRINT("Data successfully written!\n");
72
73 return res;
74 }
75
test_file_sync(void)76 static int test_file_sync(void)
77 {
78 int res;
79
80 TC_PRINT("\nSync tests:\n");
81
82 /* Verify fs_sync() */
83 res = fs_sync(&filep);
84 if (res) {
85 TC_PRINT("Error syncing file [%d]\n", res);
86 return res;
87 }
88
89 return res;
90 }
91
test_file_read(void)92 static int test_file_read(void)
93 {
94 ssize_t brw;
95 int res;
96 char read_buff[80];
97 size_t sz = strlen(test_str);
98
99 TC_PRINT("\nRead tests:\n");
100
101 res = fs_seek(&filep, 0, FS_SEEK_SET);
102 if (res) {
103 TC_PRINT("fs_seek failed [%d]\n", res);
104 fs_close(&filep);
105 return res;
106 }
107
108 /* Verify fs_read() */
109 brw = fs_read(&filep, read_buff, sz);
110 if (brw < 0) {
111 TC_PRINT("Failed reading file [%zd]\n", brw);
112 fs_close(&filep);
113 return brw;
114 }
115
116 read_buff[brw] = 0;
117
118 TC_PRINT("Data read:\"%s\"\n\n", read_buff);
119
120 if (strcmp(test_str, read_buff)) {
121 TC_PRINT("Error - Data read does not match data written\n");
122 TC_PRINT("Data read:\"%s\"\n\n", read_buff);
123 return TC_FAIL;
124 }
125
126 TC_PRINT("Data read matches data written\n");
127
128 return res;
129 }
130
test_file_truncate(void)131 static int test_file_truncate(void)
132 {
133 int res;
134 off_t orig_pos;
135 char read_buff[80];
136 ssize_t brw;
137
138 TC_PRINT("\nTruncate tests:\n");
139
140 /* Verify fs_truncate() */
141 /* Test truncating to 0 size */
142 TC_PRINT("\nTesting shrink to 0 size\n");
143 res = fs_truncate(&filep, 0);
144 if (res) {
145 TC_PRINT("fs_truncate failed [%d]\n", res);
146 fs_close(&filep);
147 return res;
148 }
149
150 fs_seek(&filep, 0, FS_SEEK_END);
151 /* Verify fs_tell() */
152 if (fs_tell(&filep) > 0) {
153 TC_PRINT("Failed truncating to size 0\n");
154 fs_close(&filep);
155 return TC_FAIL;
156 }
157
158 TC_PRINT("Testing write after truncating\n");
159 res = test_file_write();
160 if (res) {
161 TC_PRINT("Write failed after truncating\n");
162 return res;
163 }
164
165 fs_seek(&filep, 0, FS_SEEK_END);
166
167 orig_pos = fs_tell(&filep);
168 TC_PRINT("Original size of file = %ld\n", (long) orig_pos);
169
170 /* Test shrinking file */
171 TC_PRINT("\nTesting shrinking\n");
172 res = fs_truncate(&filep, orig_pos - 5);
173 if (res) {
174 TC_PRINT("fs_truncate failed [%d]\n", res);
175 fs_close(&filep);
176 return res;
177 }
178
179 fs_seek(&filep, 0, FS_SEEK_END);
180 TC_PRINT("File size after shrinking by 5 bytes = %ld\n",
181 (long) fs_tell(&filep));
182 if (fs_tell(&filep) != (orig_pos - 5)) {
183 TC_PRINT("File size after fs_truncate not as expected\n");
184 fs_close(&filep);
185 return TC_FAIL;
186 }
187
188 /* Test expanding file */
189 TC_PRINT("\nTesting expanding\n");
190 fs_seek(&filep, 0, FS_SEEK_END);
191 orig_pos = fs_tell(&filep);
192 res = fs_truncate(&filep, orig_pos + 10);
193 if (res) {
194 TC_PRINT("fs_truncate failed [%d]\n", res);
195 fs_close(&filep);
196 return res;
197 }
198
199 fs_seek(&filep, 0, FS_SEEK_END);
200 TC_PRINT("File size after expanding by 10 bytes = %ld\n",
201 (long) fs_tell(&filep));
202 if (fs_tell(&filep) != (orig_pos + 10)) {
203 TC_PRINT("File size after fs_truncate not as expected\n");
204 fs_close(&filep);
205 return TC_FAIL;
206 }
207
208 /* Check if expanded regions are zeroed */
209 TC_PRINT("Testing for zeroes in expanded region\n");
210 fs_seek(&filep, -5, FS_SEEK_END);
211
212 brw = fs_read(&filep, read_buff, 5);
213
214 if (brw < 5) {
215 TC_PRINT("Read failed after truncating\n");
216 fs_close(&filep);
217 return -1;
218 }
219
220 for (int i = 0; i < 5; i++) {
221 if (read_buff[i]) {
222 TC_PRINT("Expanded regions are not zeroed\n");
223 fs_close(&filep);
224 return TC_FAIL;
225 }
226 }
227
228 return res;
229 }
230
test_file_close(void)231 int test_file_close(void)
232 {
233 int res;
234
235 TC_PRINT("\nClose tests:\n");
236
237 res = fs_close(&filep);
238 if (res) {
239 TC_PRINT("Error closing file [%d]\n", res);
240 return res;
241 }
242
243 TC_PRINT("Closed file %s\n", TEST_FILE);
244
245 return res;
246 }
247
test_file_delete(void)248 static int test_file_delete(void)
249 {
250 int res;
251
252 TC_PRINT("\nDelete tests:\n");
253
254 /* Verify fs_unlink() */
255 res = fs_unlink(TEST_FILE);
256 if (res) {
257 TC_PRINT("Error deleting file [%d]\n", res);
258 return res;
259 }
260
261 /* Check if file was deleted */
262 if (check_file_dir_exists(TEST_FILE)) {
263 TC_PRINT("Failed deleting %s\n", TEST_FILE);
264 return TC_FAIL;
265 }
266
267 TC_PRINT("File (%s) deleted successfully!\n", TEST_FILE);
268
269 return res;
270 }
271
test_fat_file(void)272 void test_fat_file(void)
273 {
274 zassert_true(test_file_open() == TC_PASS);
275 zassert_true(test_file_write() == TC_PASS);
276 zassert_true(test_file_sync() == TC_PASS);
277 zassert_true(test_file_read() == TC_PASS);
278 zassert_true(test_file_truncate() == TC_PASS);
279 zassert_true(test_file_close() == TC_PASS);
280 zassert_true(test_file_delete() == TC_PASS);
281 }
282