tests-spiffs.c
8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (C) 2016 OTA keys S.A.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*/
#include "fs/spiffs_fs.h"
#include "vfs.h"
#include "mtd.h"
#include "board.h"
#include <fcntl.h>
#include <errno.h>
#include "embUnit/embUnit.h"
#include "tests-spiffs.h"
/* Define MTD_0 in board.h to use the board mtd if any */
#ifdef MTD_0
#define _dev (MTD_0)
#else
/* Test mock object implementing a simple RAM-based mtd */
#ifndef SECTOR_COUNT
#define SECTOR_COUNT 4
#endif
#ifndef PAGE_PER_SECTOR
#define PAGE_PER_SECTOR 8
#endif
#ifndef PAGE_SIZE
#define PAGE_SIZE 128
#endif
static uint8_t dummy_memory[PAGE_PER_SECTOR * PAGE_SIZE * SECTOR_COUNT];
static int _init(mtd_dev_t *dev)
{
(void)dev;
memset(dummy_memory, 0xff, sizeof(dummy_memory));
return 0;
}
static int _read(mtd_dev_t *dev, void *buff, uint32_t addr, uint32_t size)
{
(void)dev;
if (addr + size > sizeof(dummy_memory)) {
return -EOVERFLOW;
}
memcpy(buff, dummy_memory + addr, size);
return size;
}
static int _write(mtd_dev_t *dev, const void *buff, uint32_t addr, uint32_t size)
{
(void)dev;
if (addr + size > sizeof(dummy_memory)) {
return -EOVERFLOW;
}
if (size > PAGE_SIZE) {
return -EOVERFLOW;
}
memcpy(dummy_memory + addr, buff, size);
return size;
}
static int _erase(mtd_dev_t *dev, uint32_t addr, uint32_t size)
{
(void)dev;
if (size % (PAGE_PER_SECTOR * PAGE_SIZE) != 0) {
return -EOVERFLOW;
}
if (addr % (PAGE_PER_SECTOR * PAGE_SIZE) != 0) {
return -EOVERFLOW;
}
if (addr + size > sizeof(dummy_memory)) {
return -EOVERFLOW;
}
memset(dummy_memory + addr, 0xff, size);
return 0;
}
static int _power(mtd_dev_t *dev, enum mtd_power_state power)
{
(void)dev;
(void)power;
return 0;
}
static const mtd_desc_t driver = {
.init = _init,
.read = _read,
.write = _write,
.erase = _erase,
.power = _power,
};
static mtd_dev_t dev = {
.driver = &driver,
.sector_count = SECTOR_COUNT,
.pages_per_sector = PAGE_PER_SECTOR,
.page_size = PAGE_SIZE,
};
static mtd_dev_t *_dev = (mtd_dev_t*) &dev;
#endif /* MTD_0 */
static struct spiffs_desc spiffs_desc = {
.lock = MUTEX_INIT,
};
static vfs_mount_t _test_spiffs_mount = {
.fs = &spiffs_file_system,
.mount_point = "/test-spiffs",
.private_data = &spiffs_desc,
};
static void test_spiffs_setup(void)
{
#if SPIFFS_HAL_CALLBACK_EXTRA == 1
spiffs_desc.dev = _dev;
#endif
vfs_mount(&_test_spiffs_mount);
}
static void test_spiffs_teardown(void)
{
vfs_unlink("/test-spiffs/test.txt");
vfs_unlink("/test-spiffs/test0.txt");
vfs_unlink("/test-spiffs/test1.txt");
vfs_unlink("/test-spiffs/a/test2.txt");
vfs_umount(&_test_spiffs_mount);
}
static void tests_spiffs_mount_umount(void)
{
int res;
res = vfs_umount(&_test_spiffs_mount);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_mount(&_test_spiffs_mount);
TEST_ASSERT_EQUAL_INT(0, res);
}
static void tests_spiffs_open_close(void)
{
int res;
res = vfs_open("/test-spiffs/test.txt", O_CREAT | O_RDWR, 0);
TEST_ASSERT(res >= 0);
res = vfs_close(res);
TEST_ASSERT_EQUAL_INT(0, res);
}
static void tests_spiffs_write(void)
{
const char buf[] = "TESTSTRING";
char r_buf[2 * sizeof(buf)];
int res;
int fd = vfs_open("/test-spiffs/test.txt", O_CREAT | O_RDWR, 0);
TEST_ASSERT(fd >= 0);
res = vfs_write(fd, buf, sizeof(buf));
TEST_ASSERT_EQUAL_INT(sizeof(buf), res);
res = vfs_lseek(fd, 0, SEEK_SET);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_read(fd, r_buf, sizeof(r_buf));
TEST_ASSERT_EQUAL_INT(sizeof(buf), res);
TEST_ASSERT_EQUAL_STRING(&buf[0], &r_buf[0]);
res = vfs_close(fd);
TEST_ASSERT_EQUAL_INT(0, res);
fd = vfs_open("/test-spiffs/test.txt", O_RDONLY, 0);
TEST_ASSERT(fd >= 0);
res = vfs_read(fd, r_buf, sizeof(r_buf));
TEST_ASSERT_EQUAL_INT(sizeof(buf), res);
TEST_ASSERT_EQUAL_STRING(&buf[0], &r_buf[0]);
res = vfs_close(fd);
TEST_ASSERT_EQUAL_INT(0, res);
}
static void tests_spiffs_unlink(void)
{
const char buf[] = "TESTSTRING";
int res;
int fd = vfs_open("/test-spiffs/test.txt", O_CREAT | O_RDWR, 0);
TEST_ASSERT(fd >= 0);
res = vfs_write(fd, buf, sizeof(buf));
TEST_ASSERT_EQUAL_INT(sizeof(buf), res);
res = vfs_close(fd);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_unlink("/test-spiffs/test.txt");
TEST_ASSERT_EQUAL_INT(0, res);
}
static void tests_spiffs_readdir(void)
{
const char buf0[] = "TESTSTRING";
const char buf1[] = "TESTTESTSTRING";
const char buf2[] = "TESTSTRINGSTRING";
int res;
int fd0 = vfs_open("/test-spiffs/test0.txt", O_CREAT | O_RDWR, 0);
TEST_ASSERT(fd0 >= 0);
int fd1 = vfs_open("/test-spiffs/test1.txt", O_CREAT | O_RDWR, 0);
TEST_ASSERT(fd1 >= 0);
int fd2 = vfs_open("/test-spiffs/a/test2.txt", O_CREAT | O_RDWR, 0);
TEST_ASSERT(fd2 >= 0);
res = vfs_write(fd0, buf0, sizeof(buf0));
TEST_ASSERT_EQUAL_INT(sizeof(buf0), res);
res = vfs_write(fd1, buf1, sizeof(buf1));
TEST_ASSERT_EQUAL_INT(sizeof(buf1), res);
res = vfs_write(fd2, buf2, sizeof(buf2));
TEST_ASSERT_EQUAL_INT(sizeof(buf2), res);
res = vfs_close(fd0);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_close(fd1);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_close(fd2);
TEST_ASSERT_EQUAL_INT(0, res);
vfs_DIR dirp;
res = vfs_opendir(&dirp, "/test-spiffs");
TEST_ASSERT_EQUAL_INT(0, res);
vfs_dirent_t entry;
int nb_files = 0;
do {
res = vfs_readdir(&dirp, &entry);
if (res == 1 && (strcmp("/test0.txt", &(entry.d_name[0])) == 0 ||
strcmp("/test1.txt", &(entry.d_name[0])) == 0 ||
strcmp("/a/test2.txt", &(entry.d_name[0])) == 0)) {
nb_files++;
}
} while (res == 1);
TEST_ASSERT_EQUAL_INT(3, nb_files);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_closedir(&dirp);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_unlink("/test-spiffs/test0.txt");
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_unlink("/test-spiffs/test1.txt");
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_unlink("/test-spiffs/a/test2.txt");
TEST_ASSERT_EQUAL_INT(0, res);
}
static void tests_spiffs_rename(void)
{
const char buf[] = "TESTSTRING";
char r_buf[2 * sizeof(buf)];
int res;
int fd = vfs_open("/test-spiffs/test.txt", O_CREAT | O_RDWR, 0);
TEST_ASSERT(fd >= 0);
res = vfs_write(fd, buf, sizeof(buf));
TEST_ASSERT(res == sizeof(buf));
res = vfs_lseek(fd, 0, SEEK_SET);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_read(fd, r_buf, sizeof(r_buf));
TEST_ASSERT(res == sizeof(buf));
TEST_ASSERT_EQUAL_STRING(&buf[0], &r_buf[0]);
res = vfs_close(fd);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_rename("/test-spiffs/test.txt", "/test-spiffs/test1.txt");
TEST_ASSERT_EQUAL_INT(0, res);
fd = vfs_open("/test-spiffs/test.txt", O_RDONLY, 0);
TEST_ASSERT(fd < 0);
fd = vfs_open("/test-spiffs/test1.txt", O_RDONLY, 0);
TEST_ASSERT(fd >= 0);
res = vfs_lseek(fd, 0, SEEK_SET);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_read(fd, r_buf, sizeof(r_buf));
TEST_ASSERT(res == sizeof(buf));
TEST_ASSERT_EQUAL_STRING(&buf[0], &r_buf[0]);
res = vfs_close(fd);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_unlink("/test-spiffs/test1.txt");
TEST_ASSERT_EQUAL_INT(0, res);
}
Test *tests_spiffs_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(tests_spiffs_mount_umount),
new_TestFixture(tests_spiffs_open_close),
new_TestFixture(tests_spiffs_write),
new_TestFixture(tests_spiffs_unlink),
new_TestFixture(tests_spiffs_readdir),
new_TestFixture(tests_spiffs_rename),
};
EMB_UNIT_TESTCALLER(spiffs_tests, test_spiffs_setup, test_spiffs_teardown, fixtures);
return (Test *)&spiffs_tests;
}
void tests_spiffs(void)
{
TESTS_RUN(tests_spiffs_tests());
}
/** @} */