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 uint8_t val8;
14 uint16_t val16;
15 uint64_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
c1_handle_get(const char * name,char * val,int val_len_max)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
c1_handle_set(const char * name,size_t len,settings_read_cb read_cb,void * cb_arg)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
c1_handle_commit(void)107 int c1_handle_commit(void)
108 {
109 test_commit_called = 1;
110 return 0;
111 }
112
c1_handle_export(int (* cb)(const char * name,const void * value,size_t val_len))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
ctest_clear_call_state(void)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
ctest_get_call_state(void)136 int ctest_get_call_state(void)
137 {
138 return test_get_called + test_set_called + test_commit_called;
139 }
140
config_wipe_srcs(void)141 void config_wipe_srcs(void)
142 {
143 sys_slist_init(&settings_load_srcs);
144 settings_save_dst = NULL;
145 }
146
fsutil_read_file(const char * path,off_t offset,size_t len,void * dst,size_t * out_len)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 fs_file_t_init(&file);
155
156 rc = fs_open(&file, path, FS_O_CREATE | FS_O_RDWR);
157 if (rc != 0) {
158 return rc;
159 }
160
161 r_len = fs_read(&file, dst, len);
162 if (r_len < 0) {
163 rc = -EIO;
164 } else {
165 *out_len = r_len;
166 }
167
168 fs_close(&file);
169 return rc;
170 }
171
fsutil_write_file(const char * path,const void * data,size_t len)172 int fsutil_write_file(const char *path, const void *data, size_t len)
173 {
174 struct fs_file_t file;
175 int rc;
176
177 fs_file_t_init(&file);
178
179 rc = fs_open(&file, path, FS_O_CREATE | FS_O_RDWR);
180 if (rc != 0) {
181 return rc;
182 }
183
184 if (fs_write(&file, data, len) != len) {
185 rc = -EIO;
186 }
187
188 fs_close(&file);
189 return rc;
190 }
191
memmem(char const * mem,size_t mem_len,char const * sub,size_t sub_len)192 char const *memmem(char const *mem, size_t mem_len, char const *sub,
193 size_t sub_len)
194 {
195 int i;
196
197 if (sub_len <= mem_len && sub_len > 0) {
198 for (i = 0; i <= mem_len - sub_len; i++) {
199 if (!memcmp(&mem[i], sub, sub_len)) {
200 return &mem[i];
201 }
202 }
203 }
204
205 return NULL;
206 }
207
settings_test_file_strstr(const char * fname,char const * string,size_t str_len)208 int settings_test_file_strstr(const char *fname, char const *string,
209 size_t str_len)
210 {
211 int rc;
212 uint32_t len;
213 size_t rlen;
214 char *buf;
215 struct fs_dirent entry;
216
217 rc = fs_stat(fname, &entry);
218 if (rc) {
219 return rc;
220 }
221
222 len = entry.size;
223 buf = (char *)k_malloc(len + 1);
224 zassert_not_null(buf, "out of memory");
225
226 rc = fsutil_read_file(fname, 0, len, buf, &rlen);
227 zassert_true(rc == 0, "can't access the file\n'");
228 zassert_true(rc == 0, "not enough data read\n'");
229 buf[rlen] = '\0';
230
231 if (memmem(buf, len, string, str_len)) {
232 return 0;
233 }
234
235 return -1;
236 }
237