-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.c
375 lines (300 loc) · 11.9 KB
/
main.c
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2017, Erik Moqvist
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is part of the Simba project.
*/
#include "simba.h"
static char qbuf[512];
static struct queue_t queue;
static int test_reset(struct harness_t *harness_p)
{
BTASSERTI(settings_reset(), ==, 0);
return (0);
}
static int test_cmd_list(struct harness_t *harness_p)
{
#if CONFIG_FS_CMD_SETTINGS_LIST == 1
char buf[64];
/* Call the list command and validate the output. */
strcpy(buf, "oam/settings/list");
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, 0);
BTASSERTI(harness_expect(&queue,
"NAME TYPE SIZE VALUE\r\n"
"int32 int32_t 4 -2\r\n"
"string string_t 4 y\r\n"
"blob blob_t 2 1112\r\n"
"blob_with_empty_default_data blob_t 4 ffffffff\r\n"
"max_name_length_40_123456789012345678901 string_t 4 \r\n",
NULL), ==, 380);
return (0);
#else
return (1);
#endif
}
static int test_cmd_read(struct harness_t *harness_p)
{
#if CONFIG_FS_CMD_SETTINGS_READ == 1
char buf[64];
/* Bad number of arguments. */
std_sprintf(buf, FSTR("oam/settings/read"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"Usage: read <name>\r\n",
NULL), ==, 20);
/* Bad setting name. */
std_sprintf(buf, FSTR("oam/settings/read missing"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"missing: setting not found\r\n",
NULL), ==, 28);
return (0);
#else
return (1);
#endif
}
static int test_cmd_write(struct harness_t *harness_p)
{
#if CONFIG_FS_CMD_SETTINGS_READ == 1
char buf[128];
/* Write a string that is too long. */
std_sprintf(buf,
FSTR("oam/settings/write string ThisStringIsTooLong"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"ThisStringIsTooLong: string too long\r\n",
NULL), ==, 38);
/* Write a blob that is too long. */
std_sprintf(buf,
FSTR("oam/settings/write blob 010203"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"3: bad blob data length\r\n",
NULL), ==, 25);
/* Write a blob that is too short. */
std_sprintf(buf,
FSTR("oam/settings/write blob 01"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"1: bad blob data length\r\n",
NULL), ==, 25);
/* Write a blob that contains invalid data. */
std_sprintf(buf,
FSTR("oam/settings/write blob 01XX"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"01XX: bad blob data\r\n",
NULL), ==, 21);
/* Write a blob that contains invalid data. */
std_sprintf(buf,
FSTR("oam/settings/write blob foo1"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"foo1: bad blob data\r\n",
NULL), ==, 21);
/* Bad number of arguments. */
std_sprintf(buf, FSTR("oam/settings/write"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"Usage: write <name> <value>\r\n",
NULL), ==, 29);
/* Bad setting name. */
std_sprintf(buf, FSTR("oam/settings/write missing 1"));
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, -EINVAL);
BTASSERTI(harness_expect(&queue,
"missing: setting not found\r\n",
NULL), ==, 28);
return (0);
#else
return (1);
#endif
}
static int test_cmd_read_write_read(struct harness_t *harness_p)
{
#if CONFIG_FS_CMD_SETTINGS_READ == 1
int i;
char buf[128];
char response[64];
char *names_p[] = {
"int32", "string", "blob"
};
char *values_before_p[] = {
"-2", "y", "1112"
};
char *values_after_p[] = {
"12345678", "hi", "ffff"
};
for (i = 0; i < membersof(names_p); i++) {
/* Read the value. */
std_sprintf(buf, FSTR("oam/settings/read %s"), names_p[i]);
std_printf(FSTR("%s\r\n"), buf);
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, 0);
std_sprintf(&response[0], FSTR("%s\r\n"), values_before_p[i]);
BTASSERTI(harness_expect(&queue, &response[0], NULL),
==,
strlen(values_before_p[i]) + 2);
/* Write a new value. */
std_sprintf(buf,
FSTR("oam/settings/write %s %s"),
names_p[i],
values_after_p[i]);
std_printf(FSTR("%s\r\n"), buf);
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, 0);
/* Read the value again to verify that the write command
works. */
std_sprintf(buf, FSTR("oam/settings/read %s"), names_p[i]);
std_printf(FSTR("%s\r\n"), buf);
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, 0);
std_sprintf(&response[0], FSTR("%s\r\n"), values_after_p[i]);
BTASSERTI(harness_expect(&queue, &response[0], NULL),
==,
strlen(values_after_p[i]) + 2);
}
return (0);
#else
return (1);
#endif
}
static int test_cmd_reset(struct harness_t *harness_p)
{
#if CONFIG_FS_CMD_SETTINGS_RESET == 1
char buf[64];
/* Call the list command and validate the output. */
strcpy(buf, "oam/settings/reset");
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, 0);
/* Check the list output. */
strcpy(buf, "oam/settings/list");
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, 0);
BTASSERTI(harness_expect(&queue,
"NAME TYPE SIZE VALUE\r\n"
"int32 int32_t 4 -2\r\n"
"string string_t 4 y\r\n"
"blob blob_t 2 1112\r\n"
"blob_with_empty_default_data blob_t 4 ffffffff\r\n"
"max_name_length_40_123456789012345678901 string_t 4 \r\n",
NULL), ==, 380);
return (0);
#else
return (1);
#endif
}
static int test_integer(struct harness_t *harness_p)
{
int32_t int32;
BTASSERTI(settings_read(&int32,
SETTING_INT32_ADDR,
SETTING_INT32_SIZE), ==, SETTING_INT32_SIZE);
BTASSERTI(int32, ==, SETTING_INT32_VALUE);
int32 = 46;
BTASSERTI(settings_write(SETTING_INT32_ADDR,
&int32,
SETTING_INT32_SIZE), ==, SETTING_INT32_SIZE);
int32 = 0;
BTASSERTI(settings_read(&int32,
SETTING_INT32_ADDR,
SETTING_INT32_SIZE), ==, SETTING_INT32_SIZE);
BTASSERTI(int32, ==, 46);
return (0);
}
static int test_string(struct harness_t *harness_p)
{
char string[SETTING_STRING_SIZE];
/* Read the default value. */
memset(string, 0, sizeof(string));
BTASSERTI(settings_read(string,
SETTING_STRING_ADDR,
SETTING_STRING_SIZE), ==, SETTING_STRING_SIZE);
BTASSERTM(string, "y", 1);
/* Overwrite the default value with "x". */
string[0] = 'x';
string[1] = '\0';
BTASSERTI(settings_write(SETTING_STRING_ADDR,
string,
SETTING_STRING_SIZE), ==, SETTING_STRING_SIZE);
/* Read the overwritten value. */
memset(string, 0, sizeof(string));
BTASSERTI(settings_read(string,
SETTING_STRING_ADDR,
SETTING_STRING_SIZE), ==, SETTING_STRING_SIZE);
BTASSERTM(string, "x", 1);
return (0);
}
static int test_read_write_by_name(struct harness_t *harness_p)
{
int32_t int32;
int32 = 10;
BTASSERTI(settings_write_by_name("int32",
&int32,
sizeof(int32)), ==, sizeof(int32));
int32 = 0;
BTASSERTI(settings_read_by_name("int32",
&int32,
sizeof(int32)), ==, sizeof(int32));
BTASSERTI(int32, ==, 10);
return (0);
}
static int test_cmd_list_after_updates(struct harness_t *harness_p)
{
#if CONFIG_FS_CMD_SETTINGS_LIST == 1
char buf[64];
/* Call the list command and validate the output. */
strcpy(buf, "oam/settings/list");
BTASSERTI(fs_call(buf, NULL, &queue, NULL), ==, 0);
BTASSERTI(harness_expect(&queue,
"NAME TYPE SIZE VALUE\r\n"
"int32 int32_t 4 10\r\n"
"string string_t 4 x\r\n"
"blob blob_t 2 1112\r\n"
"blob_with_empty_default_data blob_t 4 ffffffff\r\n"
"max_name_length_40_123456789012345678901 string_t 4 \r\n",
NULL), ==, 380);
return (0);
#else
return (1);
#endif
}
int main()
{
struct harness_t harness;
struct harness_testcase_t harness_testcases[] = {
{ test_reset, "test_reset" },
{ test_cmd_list, "test_cmd_list" },
{ test_cmd_read, "test_cmd_read" },
{ test_cmd_write, "test_cmd_write" },
{ test_cmd_read_write_read, "test_cmd_read_write_read" },
{ test_cmd_reset, "test_cmd_reset" },
{ test_integer, "test_integer" },
{ test_string, "test_string" },
{ test_read_write_by_name, "test_read_write_by_name" },
{ test_cmd_list_after_updates, "test_cmd_list_after_updates" },
{ NULL, NULL }
};
sys_start();
queue_init(&queue, qbuf, sizeof(qbuf));
harness_init(&harness);
harness_run(&harness, harness_testcases);
return (0);
}