summaryrefslogtreecommitdiffstats
path: root/tests/ctrl/ctrl_test.c
blob: b8425c7eac3bee4f91e2b8b9f0e82c240b07dec8 (plain)
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
#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>

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)
{
	char *rc;
	size_t len = strlen(str) + 1;
	/* ctrl_cmd_parse() appends a '\0' to the msgb, allow one more byte. */
	struct msgb *msg = msgb_alloc(len + 1, str);
	msg->l2h = msg->head;
	rc = (char*)msgb_put(msg, len);
	OSMO_ASSERT(rc == (char*)msg->l2h);
	strcpy(rc, str);
	return msg;
}

static void *ctx = NULL;

void print_escaped(const char *str)
{
	if (!str) {
		printf("NULL");
		return;
	}

	printf("'");
	for (;*str; str++) {
		switch (*str) {
		case '\n':
			printf("\\n");
			break;
		case '\r':
			printf("\\r");
			break;
		case '\t':
			printf("\\t");
			break;
		default:
			printf("%c", *str);
			break;
		}
	}
	printf("'");
}

void assert_same_str(const char *label, const char *expect, const char *got)
{
	if ((expect == got) || (expect && got && (strcmp(expect, got) == 0))) {
		printf("%s = ", label);
		print_escaped(got);
		printf("\n");
		return;
	}

	printf("MISMATCH for '%s':\ngot:      ", label); print_escaped(got);
	printf("\nexpected: "); print_escaped(expect);
	printf("\n");
	OSMO_ASSERT(expect == got);
}

static void assert_parsing(const char *str, const struct ctrl_cmd *expect)
{
	struct ctrl_cmd *cmd;
	struct msgb *msg = msgb_from_string(str);

	printf("test parsing: ");
	print_escaped(str);
	printf("\n");

	cmd = ctrl_cmd_parse(ctx, msg);
	OSMO_ASSERT(cmd);

	OSMO_ASSERT(expect->type == cmd->type);

#define ASSERT_SAME_STR(field) \
	assert_same_str(#field, expect->field, cmd->field)

	ASSERT_SAME_STR(id);
	ASSERT_SAME_STR(variable);
	ASSERT_SAME_STR(value);
	ASSERT_SAME_STR(reply);

	talloc_free(cmd);
	msgb_free(msg);

	printf("ok\n");
}

struct one_parsing_test {
	const char *cmd_str;
	struct ctrl_cmd expect;
};

static const struct one_parsing_test test_parsing_list[] = {
	{ "GET 1 variable",
		{
			.type = CTRL_TYPE_GET,
			.id = "1",
			.variable = "variable",
		}
	},
	{ "GET 1 variable\n",
		{
			.type = CTRL_TYPE_GET,
			.id = "1",
			.variable = "variable\n", /* current bug */
		}
	},
	{ "GET 1 var\ni\nable",
		{
			.type = CTRL_TYPE_GET,
			.id = "1",
			.variable = "var\ni\nable", /* current bug */
		}
	},
	{ "GET 1 variable value",
		{
			.type = CTRL_TYPE_GET,
			.id = "1",
			.variable = "variable",
			.value = NULL,
		}
	},
	{ "GET 1 variable value\n",
		{
			.type = CTRL_TYPE_GET,
			.id = "1",
			.variable = "variable",
			.value = NULL,
		}
	},
	{ "GET 1 variable multiple value tokens",
		{
			.type = CTRL_TYPE_GET,
			.id = "1",
			.variable = "variable",
			.value = NULL,
		}
	},
	{ "GET 1 variable multiple value tokens\n",
		{
			.type = CTRL_TYPE_GET,
			.id = "1",
			.variable = "variable",
			.value = NULL,
		}
	},
	{ "SET 1 variable value",
		{
			.type = CTRL_TYPE_SET,
			.id = "1",
			.variable = "variable",
			.value = "value",
		}
	},
	{ "SET 1 variable value\n",
		{
			.type = CTRL_TYPE_SET,
			.id = "1",
			.variable = "variable",
			.value = "value",
		}
	},
	{ "SET weird_id variable value",
		{
			.type = CTRL_TYPE_SET,
			.id = "weird_id",
			.variable = "variable",
			.value = "value",
		}
	},
	{ "SET weird_id variable value\n",
		{
			.type = CTRL_TYPE_SET,
			.id = "weird_id",
			.variable = "variable",
			.value = "value",
		}
	},
	{ "SET 1 variable multiple value tokens",
		{
			.type = CTRL_TYPE_SET,
			.id = "1",
			.variable = "variable",
			.value = "multiple value tokens",
		}
	},
	{ "SET 1 variable multiple value tokens\n",
		{
			.type = CTRL_TYPE_SET,
			.id = "1",
			.variable = "variable",
			.value = "multiple value tokens",
		}
	},
	{ "SET 1 variable value_with_trailing_spaces  ",
		{
			.type = CTRL_TYPE_SET,
			.id = "1",
			.variable = "variable",
			.value = "value_with_trailing_spaces  ",
		}
	},
	{ "SET 1 variable value_with_trailing_spaces  \n",
		{
			.type = CTRL_TYPE_SET,
			.id = "1",
			.variable = "variable",
			.value = "value_with_trailing_spaces  ",
		}
	},
	{ "SET \n special_char_id value",
		{
			.type = CTRL_TYPE_SET,
			.id = "\n",
			.variable = "special_char_id",
			.value = "value",
		}
	},
	{ "SET \t special_char_id value",
		{
			.type = CTRL_TYPE_SET,
			.id = "\t",
			.variable = "special_char_id",
			.value = "value",
		}
	},
};

static void test_parsing()
{
	int i;

	for (i = 0; i < ARRAY_SIZE(test_parsing_list); i++)
		assert_parsing(test_parsing_list[i].cmd_str,
			       &test_parsing_list[i].expect);
}

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_parsing();

	return 0;
}