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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <osmocom/core/utils.h>
#include <osmocom/ctrl/control_cmd.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/application.h>
#include <osmocom/gsm/protocol/ipaccess.h>
#include <osmocom/ctrl/control_if.h>
static void check_type(enum ctrl_type c)
{
const char *t = get_value_string(ctrl_type_vals, c);
int v = get_string_value(ctrl_type_vals, t);
printf("ctrl type %d is %s ", c, t);
if (v < 0)
printf("[PARSE FAILED]\n");
else
printf("-> %d %s\n", v, c != v ? "FAIL" : "OK");
}
struct msgb *msgb_from_string(const char *str)
{
struct ipaccess_head *iph;
struct ipaccess_head_ext *ipx;
char *str_msg;
size_t len = strlen(str) + 1;
struct msgb *msg = msgb_alloc(1024, str);
iph = (void*)msgb_put(msg, sizeof(*iph));
iph->proto = IPAC_PROTO_OSMO;
ipx = (void*)msgb_put(msg, sizeof(*ipx));
ipx->proto = IPAC_PROTO_EXT_CTRL;
str_msg = (char*)msgb_put(msg, len);
msg->l2h = (void*)str_msg;
osmo_strlcpy(str_msg, str, len);
iph->len = msgb_length(msg);
return msg;
}
static void *ctx = NULL;
struct one_test {
const char *cmd_str;
struct ctrl_cmd expect_parsed;
const char *reply_str;
};
void assert_same_str(const char *label, const char *expect, const char *got)
{
if ((expect == got) || (expect && got && (strcmp(expect, got) == 0))) {
printf("%s = '%s'\n", label, osmo_escape_str(got, -1));
return;
}
printf("MISMATCH for '%s':\ngot: %s\n", label, osmo_escape_str(got, -1));
printf("expected: %s\n", osmo_escape_str(expect, -1));
OSMO_ASSERT(expect == got);
}
static void assert_test(struct ctrl_handle *ctrl, struct ctrl_connection *ccon, const struct one_test *t)
{
struct ctrl_cmd *cmd;
struct msgb *msg = msgb_from_string(t->cmd_str);
int ctx_size_was;
printf("test: '%s'\n", osmo_escape_str(t->cmd_str, -1));
printf("parsing:\n");
cmd = ctrl_cmd_parse2(ctx, msg);
OSMO_ASSERT(cmd);
if (t->expect_parsed.type != cmd->type) {
printf("type mismatch: got %s\n", get_value_string(ctrl_type_vals, cmd->type));
OSMO_ASSERT(t->expect_parsed.type == cmd->type);
}
#define ASSERT_SAME_STR(field) \
assert_same_str(#field, t->expect_parsed.field, cmd->field)
ASSERT_SAME_STR(id);
if (t->expect_parsed.type != CTRL_TYPE_ERROR) {
ASSERT_SAME_STR(variable);
ASSERT_SAME_STR(value);
}
ASSERT_SAME_STR(reply);
talloc_free(cmd);
msgb_free(msg);
printf("handling:\n");
ctx_size_was = talloc_total_size(ctx);
msg = msgb_from_string(t->cmd_str);
ctrl_handle_msg(ctrl, ccon, msg);
if (llist_empty(&ccon->write_queue.msg_queue)) {
if (t->reply_str) {
printf("Got no reply, but expected \"%s\"\n", osmo_escape_str(t->reply_str, -1));
OSMO_ASSERT(!t->reply_str);
}
} else {
struct msgb *sent_msg = msgb_dequeue(&ccon->write_queue.msg_queue);
OSMO_ASSERT(sent_msg);
msgb_put_u8(sent_msg, 0);
printf("replied: '%s'\n", osmo_escape_str((char*)msgb_l2(sent_msg), -1));
OSMO_ASSERT(t->reply_str);
OSMO_ASSERT(!strcmp(t->reply_str, (char*)msgb_l2(sent_msg)))
msgb_free(sent_msg);
}
osmo_wqueue_clear(&ccon->write_queue);
msgb_free(msg);
if (talloc_total_size(ctx) != ctx_size_was) {
printf("mem leak!\n");
talloc_report_full(ctx, stdout);
OSMO_ASSERT(false);
}
printf("ok\n");
}
static const struct one_test test_messages_list[] = {
{ "GET 1 variable",
{
.type = CTRL_TYPE_GET,
.id = "1",
.variable = "variable",
},
"ERROR 1 Command not found",
},
{ "GET 1 variable\n",
{
.type = CTRL_TYPE_GET,
.id = "1",
.variable = "variable",
},
"ERROR 1 Command not found",
},
{ "GET 1 var\ni\nable",
{
.type = CTRL_TYPE_ERROR,
.id = "1",
.reply = "GET with trailing characters",
},
"ERROR 1 GET with trailing characters",
},
{ "GET 1 var\ti\table",
{
.type = CTRL_TYPE_ERROR,
.id = "1",
.reply = "GET variable contains invalid characters",
},
"ERROR 1 GET variable contains invalid characters",
},
{ "GET 1 var\ri\rable",
{
.type = CTRL_TYPE_ERROR,
.id = "1",
.reply = "GET variable contains invalid characters",
},
"ERROR 1 GET variable contains invalid characters",
},
{ "GET 1 variable value",
{
.type = CTRL_TYPE_ERROR,
.id = "1",
.reply = "GET with trailing characters",
},
"ERROR 1 GET with trailing characters",
},
{ "GET 1 variable value\n",
{
.type = CTRL_TYPE_ERROR,
.id = "1",
.reply = "GET with trailing characters",
},
"ERROR 1 GET with trailing characters",
},
{ "GET 1 variable multiple value tokens",
{
.type = CTRL_TYPE_ERROR,
.id = "1",
.reply = "GET with trailing characters",
},
"ERROR 1 GET with trailing characters",
},
{ "GET 1 variable multiple value tokens\n",
{
.type = CTRL_TYPE_ERROR,
.id = "1",
.reply = "GET with trailing characters",
},
"ERROR 1 GET with trailing characters",
},
{ "SET 1 variable value",
{
.type = CTRL_TYPE_SET,
.id = "1",
.variable = "variable",
.value = "value",
},
"ERROR 1 Command not found",
},
{ "SET 1 variable value\n",
{
.type = CTRL_TYPE_SET,
.id = "1",
.variable = "variable",
.value = "value",
},
"ERROR 1 Command not found",
},
{ "SET weird_id variable value",
{
.type = CTRL_TYPE_ERROR,
.id = "err",
.reply = "Invalid message ID number",
},
"ERROR err Invalid message ID number",
},
{ "SET weird_id variable value\n",
{
.type = CTRL_TYPE_ERROR,
.id = "err",
.reply = "Invalid message ID number",
},
"ERROR err Invalid message ID number",
},
{ "SET 1 variable multiple value tokens",
{
.type = CTRL_TYPE_SET,
.id = "1",
.variable = "variable",
.value = "multiple value tokens",
},
"ERROR 1 Command not found",
},
{ "SET 1 variable multiple value tokens\n",
{
.type = CTRL_TYPE_SET,
.id = "1",
.variable = "variable",
.value = "multiple value tokens",
},
"ERROR 1 Command not found",
},
{ "SET 1 variable value_with_trailing_spaces ",
{
.type = CTRL_TYPE_SET,
.id = "1",
.variable = "variable",
.value = "value_with_trailing_spaces ",
},
"ERROR 1 Command not found",
},
{ "SET 1 variable value_with_trailing_spaces \n",
{
.type = CTRL_TYPE_SET,
.id = "1",
.variable = "variable",
.value = "value_with_trailing_spaces ",
},
"ERROR 1 Command not found",
},
{ "SET \n special_char_id value",
{
.type = CTRL_TYPE_ERROR,
.id = "err",
.reply = "Invalid message ID number",
},
"ERROR err Invalid message ID number",
},
{ "SET \t special_char_id value",
{
.type = CTRL_TYPE_ERROR,
.id = "err",
.reply = "Invalid message ID number",
},
"ERROR err Invalid message ID number",
},
{ "GET_REPLY 1 variable OK",
{
.type = CTRL_TYPE_GET_REPLY,
.id = "1",
.variable = "variable",
.reply = "OK",
},
},
{ "SET_REPLY 1 variable OK",
{
.type = CTRL_TYPE_SET_REPLY,
.id = "1",
.variable = "variable",
.reply = "OK",
},
},
};
static void test_messages()
{
struct ctrl_handle *ctrl;
struct ctrl_connection *ccon;
int i;
ctrl = ctrl_handle_alloc2(ctx, NULL, NULL, 0);
ccon = talloc_zero(ctx, struct ctrl_connection);
osmo_wqueue_init(&ccon->write_queue, 1);
for (i = 0; i < ARRAY_SIZE(test_messages_list); i++)
assert_test(ctrl, ccon, &test_messages_list[i]);
talloc_free(ccon);
talloc_free(ctrl);
}
static struct log_info_cat test_categories[] = {
};
static struct log_info info = {
.cat = test_categories,
.num_cat = ARRAY_SIZE(test_categories),
};
int main(int argc, char **argv)
{
ctx = talloc_named_const(NULL, 1, "ctrl_test");
osmo_init_logging(&info);
printf("Checking ctrl types...\n");
check_type(CTRL_TYPE_UNKNOWN);
check_type(CTRL_TYPE_GET);
check_type(CTRL_TYPE_SET);
check_type(CTRL_TYPE_GET_REPLY);
check_type(CTRL_TYPE_SET_REPLY);
check_type(CTRL_TYPE_TRAP);
check_type(CTRL_TYPE_ERROR);
check_type(64);
test_messages();
return 0;
}
|