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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
#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;
bool parse_failed;
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_parse3(ctx, msg, &parse_failed);
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);
} else {
printf("type = '%s'%s\n", get_value_string(ctrl_type_vals, cmd->type),
cmd->type != CTRL_TYPE_ERROR ? "" :
(parse_failed ? " (parse failure)" : " (error received)"));
}
#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",
},
},
{ "ERROR 1 some error message",
{
.type = CTRL_TYPE_ERROR,
.id = "1",
.reply = "some error message",
},
},
{ "ERROR err some error message",
{
.type = CTRL_TYPE_ERROR,
.id = "err",
.reply = "some error message",
},
},
};
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);
}
CTRL_CMD_DEFINE(test_defer, "test-defer");
static void ctrl_test_defer_cb(void *data)
{
struct ctrl_cmd_def *cd = data;
struct ctrl_cmd *cmd = cd->cmd;
static int i = 0;
printf("%s called\n", __func__);
if (ctrl_cmd_def_is_zombie(cd)) {
printf("Is Zombie\n");
return;
}
cmd->reply = talloc_asprintf(cmd, "Test Defer #%d", i++);
ctrl_cmd_def_send(cd);
return;
}
static struct ctrl_cmd_def *test_defer_cd = NULL;
static int get_test_defer(struct ctrl_cmd *cmd, void *data)
{
void *ctx = cmd->node;
printf("%s called\n", __func__);
test_defer_cd = ctrl_cmd_def_make(ctx, cmd, NULL, 10);
return CTRL_CMD_HANDLED;
}
static int set_test_defer(struct ctrl_cmd *cmd, void *data)
{
return CTRL_CMD_HANDLED;
}
static int verify_test_defer(struct ctrl_cmd *cmd, const char *value, void *data)
{
return 0;
}
static void test_deferred_cmd()
{
struct ctrl_handle *ctrl;
struct ctrl_connection *ccon;
struct msgb *msg;
int result;
int ctx_size_was;
int ctx_size_before_defer;
printf("\n%s\n", __func__);
ctx_size_was = talloc_total_size(ctx);
ctrl = ctrl_handle_alloc2(ctx, NULL, NULL, 0);
ccon = talloc_zero(ctx, struct ctrl_connection);
INIT_LLIST_HEAD(&ccon->def_cmds);
osmo_wqueue_init(&ccon->write_queue, 1);
ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_test_defer);
ctx_size_before_defer = talloc_total_size(ctx);
msg = msgb_from_string("GET 123 test-defer");
result = ctrl_handle_msg(ctrl, ccon, msg);
printf("ctrl_handle_msg() returned %d\n", result);
OSMO_ASSERT(result == CTRL_CMD_HANDLED);
talloc_free(msg);
/* Expecting a ctrl_cmd_def as well as the cmd to still be allocated */
if (talloc_total_size(ctx) <= ctx_size_before_defer) {
printf("deferred command apparently deallocated too soon\n");
talloc_report_full(ctx, stdout);
OSMO_ASSERT(false);
}
printf("invoking ctrl_test_defer_cb() asynchronously\n");
ctrl_test_defer_cb(test_defer_cd);
/* simulate sending of the reply */
osmo_wqueue_clear(&ccon->write_queue);
/* And now the deferred cmd should be cleaned up completely. */
if (talloc_total_size(ctx) != ctx_size_before_defer) {
printf("mem leak!\n");
talloc_report_full(ctx, stdout);
OSMO_ASSERT(false);
}
talloc_free(ccon);
talloc_free(ctrl);
if (talloc_total_size(ctx) != ctx_size_was) {
printf("mem leak!\n");
talloc_report_full(ctx, stdout);
OSMO_ASSERT(false);
}
printf("success\n");
}
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_logging2(ctx, &info);
msgb_talloc_ctx_init(ctx, 0);
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();
test_deferred_cmd();
/* Expecting root ctx + msgb root ctx + 5 logging elements */
if (talloc_total_blocks(ctx) != 7) {
talloc_report_full(ctx, stdout);
OSMO_ASSERT(false);
}
return 0;
}
|