Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdint.h> |
| 4 | #include <string.h> |
| 5 | #include <stdbool.h> |
| 6 | |
| 7 | #include <osmocom/core/utils.h> |
| 8 | #include <osmocom/ctrl/control_cmd.h> |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 9 | #include <osmocom/core/logging.h> |
| 10 | #include <osmocom/core/msgb.h> |
| 11 | #include <osmocom/core/application.h> |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 12 | #include <osmocom/gsm/protocol/ipaccess.h> |
| 13 | #include <osmocom/ctrl/control_if.h> |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 14 | |
Vadim Yanitskiy | c2afe81 | 2017-06-13 01:43:23 +0700 | [diff] [blame] | 15 | static void check_type(enum ctrl_type c) |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 16 | { |
| 17 | const char *t = get_value_string(ctrl_type_vals, c); |
| 18 | int v = get_string_value(ctrl_type_vals, t); |
| 19 | |
| 20 | printf("ctrl type %d is %s ", c, t); |
| 21 | if (v < 0) |
| 22 | printf("[PARSE FAILED]\n"); |
| 23 | else |
| 24 | printf("-> %d %s\n", v, c != v ? "FAIL" : "OK"); |
| 25 | } |
| 26 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 27 | struct msgb *msgb_from_string(const char *str) |
| 28 | { |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 29 | struct ipaccess_head *iph; |
| 30 | struct ipaccess_head_ext *ipx; |
| 31 | char *str_msg; |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 32 | size_t len = strlen(str) + 1; |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 33 | |
| 34 | struct msgb *msg = msgb_alloc(1024, str); |
| 35 | |
| 36 | iph = (void*)msgb_put(msg, sizeof(*iph)); |
| 37 | iph->proto = IPAC_PROTO_OSMO; |
| 38 | |
| 39 | ipx = (void*)msgb_put(msg, sizeof(*ipx)); |
| 40 | ipx->proto = IPAC_PROTO_EXT_CTRL; |
| 41 | |
| 42 | str_msg = (char*)msgb_put(msg, len); |
| 43 | msg->l2h = (void*)str_msg; |
| 44 | osmo_strlcpy(str_msg, str, len); |
| 45 | |
| 46 | iph->len = msgb_length(msg); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 47 | return msg; |
| 48 | } |
| 49 | |
| 50 | static void *ctx = NULL; |
| 51 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 52 | struct one_test { |
| 53 | const char *cmd_str; |
| 54 | struct ctrl_cmd expect_parsed; |
| 55 | const char *reply_str; |
| 56 | }; |
| 57 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 58 | void assert_same_str(const char *label, const char *expect, const char *got) |
| 59 | { |
| 60 | if ((expect == got) || (expect && got && (strcmp(expect, got) == 0))) { |
Neels Hofmeyr | 0ab6eca | 2017-12-16 01:03:37 +0100 | [diff] [blame] | 61 | printf("%s = '%s'\n", label, osmo_escape_str(got, -1)); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
Neels Hofmeyr | 0ab6eca | 2017-12-16 01:03:37 +0100 | [diff] [blame] | 65 | printf("MISMATCH for '%s':\ngot: %s\n", label, osmo_escape_str(got, -1)); |
| 66 | printf("expected: %s\n", osmo_escape_str(expect, -1)); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 67 | OSMO_ASSERT(expect == got); |
| 68 | } |
| 69 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 70 | static void assert_test(struct ctrl_handle *ctrl, struct ctrl_connection *ccon, const struct one_test *t) |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 71 | { |
| 72 | struct ctrl_cmd *cmd; |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 73 | struct msgb *msg = msgb_from_string(t->cmd_str); |
| 74 | int ctx_size_was; |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 75 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 76 | printf("test: '%s'\n", osmo_escape_str(t->cmd_str, -1)); |
| 77 | printf("parsing:\n"); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 78 | |
| 79 | cmd = ctrl_cmd_parse(ctx, msg); |
| 80 | OSMO_ASSERT(cmd); |
| 81 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 82 | OSMO_ASSERT(t->expect_parsed.type == cmd->type); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 83 | |
| 84 | #define ASSERT_SAME_STR(field) \ |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 85 | assert_same_str(#field, t->expect_parsed.field, cmd->field) |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 86 | |
| 87 | ASSERT_SAME_STR(id); |
| 88 | ASSERT_SAME_STR(variable); |
| 89 | ASSERT_SAME_STR(value); |
| 90 | ASSERT_SAME_STR(reply); |
| 91 | |
| 92 | talloc_free(cmd); |
| 93 | msgb_free(msg); |
| 94 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 95 | printf("handling:\n"); |
| 96 | |
| 97 | ctx_size_was = talloc_total_size(ctx); |
| 98 | |
| 99 | msg = msgb_from_string(t->cmd_str); |
| 100 | ctrl_handle_msg(ctrl, ccon, msg); |
| 101 | |
| 102 | if (llist_empty(&ccon->write_queue.msg_queue)) { |
| 103 | if (t->reply_str) { |
| 104 | printf("Got no reply, but expected \"%s\"\n", osmo_escape_str(t->reply_str, -1)); |
| 105 | OSMO_ASSERT(!t->reply_str); |
| 106 | } |
| 107 | } else { |
| 108 | struct msgb *sent_msg = msgb_dequeue(&ccon->write_queue.msg_queue); |
| 109 | OSMO_ASSERT(sent_msg); |
| 110 | msgb_put_u8(sent_msg, 0); |
| 111 | |
| 112 | printf("replied: '%s'\n", osmo_escape_str((char*)msgb_l2(sent_msg), -1)); |
| 113 | OSMO_ASSERT(t->reply_str); |
| 114 | OSMO_ASSERT(!strcmp(t->reply_str, (char*)msgb_l2(sent_msg))) |
| 115 | msgb_free(sent_msg); |
| 116 | } |
| 117 | osmo_wqueue_clear(&ccon->write_queue); |
| 118 | |
| 119 | msgb_free(msg); |
| 120 | |
| 121 | if (talloc_total_size(ctx) != ctx_size_was) { |
| 122 | printf("mem leak!\n"); |
| 123 | talloc_report_full(ctx, stdout); |
| 124 | OSMO_ASSERT(false); |
| 125 | } |
| 126 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 127 | printf("ok\n"); |
| 128 | } |
| 129 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 130 | static const struct one_test test_messages_list[] = { |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 131 | { "GET 1 variable", |
| 132 | { |
| 133 | .type = CTRL_TYPE_GET, |
| 134 | .id = "1", |
| 135 | .variable = "variable", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 136 | }, |
| 137 | "ERROR 1 Command not found", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 138 | }, |
| 139 | { "GET 1 variable\n", |
| 140 | { |
| 141 | .type = CTRL_TYPE_GET, |
| 142 | .id = "1", |
| 143 | .variable = "variable\n", /* current bug */ |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 144 | }, |
| 145 | "ERROR 1 Command not found", |
| 146 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 147 | }, |
| 148 | { "GET 1 var\ni\nable", |
| 149 | { |
| 150 | .type = CTRL_TYPE_GET, |
| 151 | .id = "1", |
| 152 | .variable = "var\ni\nable", /* current bug */ |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 153 | }, |
| 154 | "ERROR 1 Command not found", |
| 155 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 156 | }, |
| 157 | { "GET 1 variable value", |
| 158 | { |
| 159 | .type = CTRL_TYPE_GET, |
| 160 | .id = "1", |
| 161 | .variable = "variable", |
| 162 | .value = NULL, |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 163 | }, |
| 164 | "ERROR 1 Command not found", |
| 165 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 166 | }, |
| 167 | { "GET 1 variable value\n", |
| 168 | { |
| 169 | .type = CTRL_TYPE_GET, |
| 170 | .id = "1", |
| 171 | .variable = "variable", |
| 172 | .value = NULL, |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 173 | }, |
| 174 | "ERROR 1 Command not found", |
| 175 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 176 | }, |
| 177 | { "GET 1 variable multiple value tokens", |
| 178 | { |
| 179 | .type = CTRL_TYPE_GET, |
| 180 | .id = "1", |
| 181 | .variable = "variable", |
| 182 | .value = NULL, |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 183 | }, |
| 184 | "ERROR 1 Command not found", |
| 185 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 186 | }, |
| 187 | { "GET 1 variable multiple value tokens\n", |
| 188 | { |
| 189 | .type = CTRL_TYPE_GET, |
| 190 | .id = "1", |
| 191 | .variable = "variable", |
| 192 | .value = NULL, |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 193 | }, |
| 194 | "ERROR 1 Command not found", |
| 195 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 196 | }, |
| 197 | { "SET 1 variable value", |
| 198 | { |
| 199 | .type = CTRL_TYPE_SET, |
| 200 | .id = "1", |
| 201 | .variable = "variable", |
| 202 | .value = "value", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 203 | }, |
| 204 | "ERROR 1 Command not found", |
| 205 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 206 | }, |
| 207 | { "SET 1 variable value\n", |
| 208 | { |
| 209 | .type = CTRL_TYPE_SET, |
| 210 | .id = "1", |
| 211 | .variable = "variable", |
| 212 | .value = "value", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 213 | }, |
| 214 | "ERROR 1 Command not found", |
| 215 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 216 | }, |
| 217 | { "SET weird_id variable value", |
| 218 | { |
| 219 | .type = CTRL_TYPE_SET, |
| 220 | .id = "weird_id", |
| 221 | .variable = "variable", |
| 222 | .value = "value", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 223 | }, |
| 224 | "ERROR weird_id Command not found", |
| 225 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 226 | }, |
| 227 | { "SET weird_id variable value\n", |
| 228 | { |
| 229 | .type = CTRL_TYPE_SET, |
| 230 | .id = "weird_id", |
| 231 | .variable = "variable", |
| 232 | .value = "value", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 233 | }, |
| 234 | "ERROR weird_id Command not found", |
| 235 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 236 | }, |
| 237 | { "SET 1 variable multiple value tokens", |
| 238 | { |
| 239 | .type = CTRL_TYPE_SET, |
| 240 | .id = "1", |
| 241 | .variable = "variable", |
| 242 | .value = "multiple value tokens", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 243 | }, |
| 244 | "ERROR 1 Command not found", |
| 245 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 246 | }, |
| 247 | { "SET 1 variable multiple value tokens\n", |
| 248 | { |
| 249 | .type = CTRL_TYPE_SET, |
| 250 | .id = "1", |
| 251 | .variable = "variable", |
| 252 | .value = "multiple value tokens", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 253 | }, |
| 254 | "ERROR 1 Command not found", |
| 255 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 256 | }, |
| 257 | { "SET 1 variable value_with_trailing_spaces ", |
| 258 | { |
| 259 | .type = CTRL_TYPE_SET, |
| 260 | .id = "1", |
| 261 | .variable = "variable", |
| 262 | .value = "value_with_trailing_spaces ", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 263 | }, |
| 264 | "ERROR 1 Command not found", |
| 265 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 266 | }, |
| 267 | { "SET 1 variable value_with_trailing_spaces \n", |
| 268 | { |
| 269 | .type = CTRL_TYPE_SET, |
| 270 | .id = "1", |
| 271 | .variable = "variable", |
| 272 | .value = "value_with_trailing_spaces ", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 273 | }, |
| 274 | "ERROR 1 Command not found", |
| 275 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 276 | }, |
| 277 | { "SET \n special_char_id value", |
| 278 | { |
| 279 | .type = CTRL_TYPE_SET, |
| 280 | .id = "\n", |
| 281 | .variable = "special_char_id", |
| 282 | .value = "value", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 283 | }, |
| 284 | "ERROR \n Command not found", |
| 285 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 286 | }, |
| 287 | { "SET \t special_char_id value", |
| 288 | { |
| 289 | .type = CTRL_TYPE_SET, |
| 290 | .id = "\t", |
| 291 | .variable = "special_char_id", |
| 292 | .value = "value", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 293 | }, |
| 294 | "ERROR \t Command not found", |
| 295 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 296 | }, |
| 297 | }; |
| 298 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 299 | static void test_messages() |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 300 | { |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 301 | struct ctrl_handle *ctrl; |
| 302 | struct ctrl_connection *ccon; |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 303 | int i; |
| 304 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 305 | ctrl = ctrl_handle_alloc2(ctx, NULL, NULL, 0); |
| 306 | ccon = talloc_zero(ctx, struct ctrl_connection); |
| 307 | |
| 308 | osmo_wqueue_init(&ccon->write_queue, 1); |
| 309 | |
| 310 | for (i = 0; i < ARRAY_SIZE(test_messages_list); i++) |
| 311 | assert_test(ctrl, ccon, &test_messages_list[i]); |
| 312 | |
| 313 | talloc_free(ccon); |
| 314 | talloc_free(ctrl); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static struct log_info_cat test_categories[] = { |
| 318 | }; |
| 319 | |
| 320 | static struct log_info info = { |
| 321 | .cat = test_categories, |
| 322 | .num_cat = ARRAY_SIZE(test_categories), |
| 323 | }; |
| 324 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 325 | int main(int argc, char **argv) |
| 326 | { |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 327 | ctx = talloc_named_const(NULL, 1, "ctrl_test"); |
| 328 | osmo_init_logging(&info); |
| 329 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 330 | printf("Checking ctrl types...\n"); |
| 331 | |
| 332 | check_type(CTRL_TYPE_UNKNOWN); |
| 333 | check_type(CTRL_TYPE_GET); |
| 334 | check_type(CTRL_TYPE_SET); |
| 335 | check_type(CTRL_TYPE_GET_REPLY); |
| 336 | check_type(CTRL_TYPE_SET_REPLY); |
| 337 | check_type(CTRL_TYPE_TRAP); |
| 338 | check_type(CTRL_TYPE_ERROR); |
| 339 | check_type(64); |
| 340 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame^] | 341 | test_messages(); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 342 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 343 | return 0; |
| 344 | } |