Skip to content

Commit a70a926

Browse files
de-nordicnashif
authored andcommitted
tests: settings: Reducing duplicated code in FS tests
Common code has been moved out of FS specific source into common source files. Signed-off-by: Dominik Ermel <[email protected]>
1 parent 5f629c8 commit a70a926

File tree

8 files changed

+308
-552
lines changed

8 files changed

+308
-552
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef _SETTINGS_TEST_FS_H
8+
#define _SETTINGS_TEST_FS_H
9+
10+
#include <stdio.h>
11+
#include <string.h>
12+
#include <ztest.h>
13+
#include <fs/fs.h>
14+
15+
#include "settings/settings.h"
16+
17+
#ifdef __cplusplus
18+
#extern "C" {
19+
#endif
20+
21+
extern u8_t val8;
22+
extern u16_t val16;
23+
extern u32_t val32;
24+
extern u64_t val64;
25+
26+
extern int test_get_called;
27+
extern int test_set_called;
28+
extern int test_commit_called;
29+
extern int test_export_block;
30+
31+
extern int c2_var_count;
32+
33+
extern struct settings_handler c_test_handlers[];
34+
35+
void ctest_clear_call_state(void);
36+
int ctest_get_call_state(void);
37+
38+
void config_wipe_srcs(void);
39+
40+
int fsutil_read_file(const char *path, off_t offset, size_t len, void *dst,
41+
size_t *out_len);
42+
int fsutil_write_file(const char *path, const void *data, size_t len);
43+
int settings_test_file_strstr(const char *fname, char const *string,
44+
size_t str_len);
45+
46+
47+
void config_empty_lookups(void);
48+
void test_config_insert(void);
49+
void test_config_getset_unknown(void);
50+
void test_config_getset_int(void);
51+
void test_config_getset_int64(void);
52+
void test_config_commit(void);
53+
54+
void test_config_empty_file(void);
55+
void test_config_small_file(void);
56+
void test_config_multiple_in_file(void);
57+
void test_config_save_in_file(void);
58+
void test_config_save_one_file(void);
59+
void test_config_compress_file(void);
60+
61+
#ifdef __cplusplus
62+
}
63+
#endif
64+
65+
#endif /* _SETTINGS_TEST_FS_H */
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
* Copyright (c) 2019 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include <stdlib.h>
7+
#include <string.h>
8+
9+
#include "settings_test.h"
10+
#include "settings_priv.h"
11+
12+
13+
u8_t val8;
14+
u16_t val16;
15+
u64_t val64;
16+
17+
int test_get_called;
18+
int test_set_called;
19+
int test_commit_called;
20+
int test_export_block;
21+
22+
int c2_var_count = 1;
23+
24+
int c1_handle_get(const char *name, char *val, int val_len_max);
25+
int c1_handle_set(const char *name, size_t len, settings_read_cb read_cb,
26+
void *cb_arg);
27+
int c1_handle_commit(void);
28+
int c1_handle_export(int (*cb)(const char *name,
29+
const void *value, size_t val_len));
30+
31+
struct settings_handler c_test_handlers[] = {
32+
{
33+
.name = "myfoo",
34+
.h_get = c1_handle_get,
35+
.h_set = c1_handle_set,
36+
.h_commit = c1_handle_commit,
37+
.h_export = c1_handle_export
38+
},
39+
};
40+
41+
42+
43+
int c1_handle_get(const char *name, char *val, int val_len_max)
44+
{
45+
test_get_called = 1;
46+
const char *next;
47+
48+
if (settings_name_steq(name, "mybar", &next) && !next) {
49+
val_len_max = MIN(val_len_max, sizeof(val8));
50+
memcpy(val, &val8, MIN(val_len_max, sizeof(val8)));
51+
return val_len_max;
52+
}
53+
54+
if (settings_name_steq(name, "mybar16", &next) && !next) {
55+
val_len_max = MIN(val_len_max, sizeof(val16));
56+
memcpy(val, &val16, MIN(val_len_max, sizeof(val16)));
57+
return val_len_max;
58+
}
59+
60+
if (settings_name_steq(name, "mybar64", &next) && !next) {
61+
val_len_max = MIN(val_len_max, sizeof(val64));
62+
memcpy(val, &val64, MIN(val_len_max, sizeof(val64)));
63+
return val_len_max;
64+
}
65+
66+
return -ENOENT;
67+
}
68+
69+
int c1_handle_set(const char *name, size_t len, settings_read_cb read_cb,
70+
void *cb_arg)
71+
{
72+
int rc;
73+
const char *next;
74+
size_t val_len;
75+
76+
test_set_called = 1;
77+
if (settings_name_steq(name, "mybar", &next) && !next) {
78+
val_len = len;
79+
zassert_true(val_len == 1, "bad set-value size");
80+
81+
rc = read_cb(cb_arg, &val8, sizeof(val8));
82+
zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
83+
return 0;
84+
}
85+
86+
if (settings_name_steq(name, "mybar16", &next) && !next) {
87+
val_len = len;
88+
zassert_true(val_len == 2, "bad set-value size");
89+
90+
rc = read_cb(cb_arg, &val16, sizeof(val16));
91+
zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
92+
return 0;
93+
}
94+
95+
if (settings_name_steq(name, "mybar64", &next) && !next) {
96+
val_len = len;
97+
zassert_true(val_len == 8, "bad set-value size");
98+
99+
rc = read_cb(cb_arg, &val64, sizeof(val64));
100+
zassert_true(rc >= 0, "SETTINGS_VALUE_SET callback");
101+
return 0;
102+
}
103+
104+
return -ENOENT;
105+
}
106+
107+
int c1_handle_commit(void)
108+
{
109+
test_commit_called = 1;
110+
return 0;
111+
}
112+
113+
int c1_handle_export(int (*cb)(const char *name,
114+
const void *value, size_t val_len))
115+
{
116+
if (test_export_block) {
117+
return 0;
118+
}
119+
120+
(void)cb("myfoo/mybar", &val8, sizeof(val8));
121+
122+
(void)cb("myfoo/mybar16", &val16, sizeof(val16));
123+
124+
(void)cb("myfoo/mybar64", &val64, sizeof(val64));
125+
126+
return 0;
127+
}
128+
129+
void ctest_clear_call_state(void)
130+
{
131+
test_get_called = 0;
132+
test_set_called = 0;
133+
test_commit_called = 0;
134+
}
135+
136+
int ctest_get_call_state(void)
137+
{
138+
return test_get_called + test_set_called + test_commit_called;
139+
}
140+
141+
void config_wipe_srcs(void)
142+
{
143+
sys_slist_init(&settings_load_srcs);
144+
settings_save_dst = NULL;
145+
}
146+
147+
int fsutil_read_file(const char *path, off_t offset, size_t len, void *dst,
148+
size_t *out_len)
149+
{
150+
struct fs_file_t file;
151+
int rc;
152+
ssize_t r_len = 0;
153+
154+
rc = fs_open(&file, path);
155+
if (rc != 0) {
156+
return rc;
157+
}
158+
159+
r_len = fs_read(&file, dst, len);
160+
if (r_len < 0) {
161+
rc = -EIO;
162+
} else {
163+
*out_len = r_len;
164+
}
165+
166+
fs_close(&file);
167+
return rc;
168+
}
169+
170+
int fsutil_write_file(const char *path, const void *data, size_t len)
171+
{
172+
struct fs_file_t file;
173+
int rc;
174+
175+
rc = fs_open(&file, path);
176+
if (rc != 0) {
177+
return rc;
178+
}
179+
180+
if (fs_write(&file, data, len) != len) {
181+
rc = -EIO;
182+
}
183+
184+
fs_close(&file);
185+
return rc;
186+
}
187+
188+
char const *memmem(char const *mem, size_t mem_len, char const *sub,
189+
size_t sub_len)
190+
{
191+
int i;
192+
193+
if (sub_len <= mem_len && sub_len > 0) {
194+
for (i = 0; i <= mem_len - sub_len; i++) {
195+
if (!memcmp(&mem[i], sub, sub_len)) {
196+
return &mem[i];
197+
}
198+
}
199+
}
200+
201+
return NULL;
202+
}
203+
204+
int settings_test_file_strstr(const char *fname, char const *string,
205+
size_t str_len)
206+
{
207+
int rc;
208+
u32_t len;
209+
size_t rlen;
210+
char *buf;
211+
struct fs_dirent entry;
212+
213+
rc = fs_stat(fname, &entry);
214+
if (rc) {
215+
return rc;
216+
}
217+
218+
len = entry.size;
219+
buf = (char *)k_malloc(len + 1);
220+
zassert_not_null(buf, "out of memory");
221+
222+
rc = fsutil_read_file(fname, 0, len, buf, &rlen);
223+
zassert_true(rc == 0, "can't access the file\n'");
224+
zassert_true(rc == 0, "not enough data read\n'");
225+
buf[rlen] = '\0';
226+
227+
if (memmem(buf, len, string, str_len)) {
228+
return 0;
229+
}
230+
231+
return -1;
232+
}

tests/subsys/settings/littlefs/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ target_link_libraries(settings_littlefs_test PRIVATE settings_test_fs)
1313
zephyr_include_directories(
1414
$ENV{ZEPHYR_BASE}/subsys/settings/include
1515
$ENV{ZEPHYR_BASE}/subsys/settings/src
16+
$ENV{ZEPHYR_BASE}/tests/subsys/settings/fs/include
1617
$ENV{ZEPHYR_BASE}/tests/subsys/settings/littlefs/src
1718
)
1819

tests/subsys/settings/littlefs/src/settings_test.h

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,13 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#ifndef _SETTINGS_FS_TEST_H
8-
#define _SETTINGS_FS_TEST_H
7+
#ifndef _SETTINGS_TEST_H
8+
#define _SETTINGS_TEST_H
99

10-
#include <stdio.h>
11-
#include <string.h>
12-
#include <ztest.h>
13-
#include <fs/fs.h>
14-
15-
#include "settings/settings.h"
16-
17-
#ifdef __cplusplus
18-
#extern "C" {
19-
#endif
10+
#include "settings_test_fs.h"
2011

2112
#define TEST_FS_MPTR "/littlefs"
2213
#define TEST_CONFIG_DIR TEST_FS_MPTR""CONFIG_SETTINGS_FS_DIR
2314

24-
extern u8_t val8;
25-
extern u16_t val16;
26-
extern u32_t val32;
27-
extern u64_t val64;
28-
29-
extern int test_get_called;
30-
extern int test_set_called;
31-
extern int test_commit_called;
32-
extern int test_export_block;
33-
34-
extern int c2_var_count;
35-
36-
extern struct settings_handler c_test_handlers[];
37-
38-
void ctest_clear_call_state(void);
39-
int ctest_get_call_state(void);
40-
41-
void config_wipe_srcs(void);
42-
43-
int fsutil_read_file(const char *path, off_t offset, size_t len, void *dst,
44-
size_t *out_len);
45-
int fsutil_write_file(const char *path, const void *data, size_t len);
46-
int settings_test_file_strstr(const char *fname, char const *string,
47-
size_t str_len);
48-
49-
#ifdef __cplusplus
50-
}
51-
#endif
5215

53-
#endif /* _SETTINGS_FS_TEST_H */
16+
#endif /* _SETTINGS_TEST_H */

0 commit comments

Comments
 (0)