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 | |
Neels Hofmeyr | c0b0b62 | 2017-12-16 01:12:35 +0100 | [diff] [blame] | 79 | cmd = ctrl_cmd_parse2(ctx, msg); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 80 | OSMO_ASSERT(cmd); |
| 81 | |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 82 | if (t->expect_parsed.type != cmd->type) { |
| 83 | printf("type mismatch: got %s\n", get_value_string(ctrl_type_vals, cmd->type)); |
| 84 | OSMO_ASSERT(t->expect_parsed.type == cmd->type); |
| 85 | } |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 86 | |
| 87 | #define ASSERT_SAME_STR(field) \ |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 88 | assert_same_str(#field, t->expect_parsed.field, cmd->field) |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 89 | |
| 90 | ASSERT_SAME_STR(id); |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 91 | if (t->expect_parsed.type != CTRL_TYPE_ERROR) { |
| 92 | ASSERT_SAME_STR(variable); |
| 93 | ASSERT_SAME_STR(value); |
| 94 | } |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 95 | ASSERT_SAME_STR(reply); |
| 96 | |
| 97 | talloc_free(cmd); |
| 98 | msgb_free(msg); |
| 99 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 100 | printf("handling:\n"); |
| 101 | |
| 102 | ctx_size_was = talloc_total_size(ctx); |
| 103 | |
| 104 | msg = msgb_from_string(t->cmd_str); |
| 105 | ctrl_handle_msg(ctrl, ccon, msg); |
| 106 | |
| 107 | if (llist_empty(&ccon->write_queue.msg_queue)) { |
| 108 | if (t->reply_str) { |
| 109 | printf("Got no reply, but expected \"%s\"\n", osmo_escape_str(t->reply_str, -1)); |
| 110 | OSMO_ASSERT(!t->reply_str); |
| 111 | } |
| 112 | } else { |
| 113 | struct msgb *sent_msg = msgb_dequeue(&ccon->write_queue.msg_queue); |
| 114 | OSMO_ASSERT(sent_msg); |
| 115 | msgb_put_u8(sent_msg, 0); |
| 116 | |
| 117 | printf("replied: '%s'\n", osmo_escape_str((char*)msgb_l2(sent_msg), -1)); |
| 118 | OSMO_ASSERT(t->reply_str); |
| 119 | OSMO_ASSERT(!strcmp(t->reply_str, (char*)msgb_l2(sent_msg))) |
| 120 | msgb_free(sent_msg); |
| 121 | } |
| 122 | osmo_wqueue_clear(&ccon->write_queue); |
| 123 | |
| 124 | msgb_free(msg); |
| 125 | |
| 126 | if (talloc_total_size(ctx) != ctx_size_was) { |
| 127 | printf("mem leak!\n"); |
Neels Hofmeyr | f2c10f1 | 2017-12-16 04:05:21 +0100 | [diff] [blame] | 128 | talloc_report_full(ctx, stdout); |
| 129 | OSMO_ASSERT(false); |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 132 | printf("ok\n"); |
| 133 | } |
| 134 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 135 | static const struct one_test test_messages_list[] = { |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 136 | { "GET 1 variable", |
| 137 | { |
| 138 | .type = CTRL_TYPE_GET, |
| 139 | .id = "1", |
| 140 | .variable = "variable", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 141 | }, |
| 142 | "ERROR 1 Command not found", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 143 | }, |
| 144 | { "GET 1 variable\n", |
| 145 | { |
| 146 | .type = CTRL_TYPE_GET, |
| 147 | .id = "1", |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 148 | .variable = "variable", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 149 | }, |
| 150 | "ERROR 1 Command not found", |
| 151 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 152 | }, |
| 153 | { "GET 1 var\ni\nable", |
| 154 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 155 | .type = CTRL_TYPE_ERROR, |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 156 | .id = "1", |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 157 | .reply = "GET with trailing characters", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 158 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 159 | "ERROR 1 GET with trailing characters", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 160 | }, |
Neels Hofmeyr | 1b8b152 | 2017-12-15 20:41:28 +0100 | [diff] [blame] | 161 | { "GET 1 var\ti\table", |
| 162 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 163 | .type = CTRL_TYPE_ERROR, |
Neels Hofmeyr | 1b8b152 | 2017-12-15 20:41:28 +0100 | [diff] [blame] | 164 | .id = "1", |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 165 | .reply = "GET variable contains invalid characters", |
Neels Hofmeyr | 1b8b152 | 2017-12-15 20:41:28 +0100 | [diff] [blame] | 166 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 167 | "ERROR 1 GET variable contains invalid characters", |
Neels Hofmeyr | 1b8b152 | 2017-12-15 20:41:28 +0100 | [diff] [blame] | 168 | }, |
| 169 | { "GET 1 var\ri\rable", |
| 170 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 171 | .type = CTRL_TYPE_ERROR, |
Neels Hofmeyr | 1b8b152 | 2017-12-15 20:41:28 +0100 | [diff] [blame] | 172 | .id = "1", |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 173 | .reply = "GET variable contains invalid characters", |
Neels Hofmeyr | 1b8b152 | 2017-12-15 20:41:28 +0100 | [diff] [blame] | 174 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 175 | "ERROR 1 GET variable contains invalid characters", |
Neels Hofmeyr | 1b8b152 | 2017-12-15 20:41:28 +0100 | [diff] [blame] | 176 | }, |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 177 | { "GET 1 variable value", |
| 178 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 179 | .type = CTRL_TYPE_ERROR, |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 180 | .id = "1", |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 181 | .reply = "GET with trailing characters", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 182 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 183 | "ERROR 1 GET with trailing characters", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 184 | }, |
| 185 | { "GET 1 variable value\n", |
| 186 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 187 | .type = CTRL_TYPE_ERROR, |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 188 | .id = "1", |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 189 | .reply = "GET with trailing characters", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 190 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 191 | "ERROR 1 GET with trailing characters", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 192 | }, |
| 193 | { "GET 1 variable multiple value tokens", |
| 194 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 195 | .type = CTRL_TYPE_ERROR, |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 196 | .id = "1", |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 197 | .reply = "GET with trailing characters", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 198 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 199 | "ERROR 1 GET with trailing characters", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 200 | }, |
| 201 | { "GET 1 variable multiple value tokens\n", |
| 202 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 203 | .type = CTRL_TYPE_ERROR, |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 204 | .id = "1", |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 205 | .reply = "GET with trailing characters", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 206 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 207 | "ERROR 1 GET with trailing characters", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 208 | }, |
| 209 | { "SET 1 variable value", |
| 210 | { |
| 211 | .type = CTRL_TYPE_SET, |
| 212 | .id = "1", |
| 213 | .variable = "variable", |
| 214 | .value = "value", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 215 | }, |
| 216 | "ERROR 1 Command not found", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 217 | }, |
| 218 | { "SET 1 variable value\n", |
| 219 | { |
| 220 | .type = CTRL_TYPE_SET, |
| 221 | .id = "1", |
| 222 | .variable = "variable", |
| 223 | .value = "value", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 224 | }, |
| 225 | "ERROR 1 Command not found", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 226 | }, |
| 227 | { "SET weird_id variable value", |
| 228 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 229 | .type = CTRL_TYPE_ERROR, |
| 230 | .id = "err", |
| 231 | .reply = "Invalid message ID number", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 232 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 233 | "ERROR err Invalid message ID number", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 234 | }, |
| 235 | { "SET weird_id variable value\n", |
| 236 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 237 | .type = CTRL_TYPE_ERROR, |
| 238 | .id = "err", |
| 239 | .reply = "Invalid message ID number", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 240 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 241 | "ERROR err Invalid message ID number", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 242 | }, |
| 243 | { "SET 1 variable multiple value tokens", |
| 244 | { |
| 245 | .type = CTRL_TYPE_SET, |
| 246 | .id = "1", |
| 247 | .variable = "variable", |
| 248 | .value = "multiple value tokens", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 249 | }, |
| 250 | "ERROR 1 Command not found", |
| 251 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 252 | }, |
| 253 | { "SET 1 variable multiple value tokens\n", |
| 254 | { |
| 255 | .type = CTRL_TYPE_SET, |
| 256 | .id = "1", |
| 257 | .variable = "variable", |
| 258 | .value = "multiple value tokens", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 259 | }, |
| 260 | "ERROR 1 Command not found", |
| 261 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 262 | }, |
| 263 | { "SET 1 variable value_with_trailing_spaces ", |
| 264 | { |
| 265 | .type = CTRL_TYPE_SET, |
| 266 | .id = "1", |
| 267 | .variable = "variable", |
| 268 | .value = "value_with_trailing_spaces ", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 269 | }, |
| 270 | "ERROR 1 Command not found", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 271 | }, |
| 272 | { "SET 1 variable value_with_trailing_spaces \n", |
| 273 | { |
| 274 | .type = CTRL_TYPE_SET, |
| 275 | .id = "1", |
| 276 | .variable = "variable", |
| 277 | .value = "value_with_trailing_spaces ", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 278 | }, |
| 279 | "ERROR 1 Command not found", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 280 | }, |
| 281 | { "SET \n special_char_id value", |
| 282 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 283 | .type = CTRL_TYPE_ERROR, |
| 284 | .id = "err", |
| 285 | .reply = "Invalid message ID number", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 286 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 287 | "ERROR err Invalid message ID number", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 288 | }, |
| 289 | { "SET \t special_char_id value", |
| 290 | { |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 291 | .type = CTRL_TYPE_ERROR, |
| 292 | .id = "err", |
| 293 | .reply = "Invalid message ID number", |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 294 | }, |
Neels Hofmeyr | 3da9aa6 | 2017-09-26 14:21:44 +0200 | [diff] [blame] | 295 | "ERROR err Invalid message ID number", |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 296 | }, |
Neels Hofmeyr | 6769ad6 | 2017-12-16 04:01:54 +0100 | [diff] [blame] | 297 | { "GET_REPLY 1 variable OK", |
| 298 | { |
| 299 | .type = CTRL_TYPE_GET_REPLY, |
| 300 | .id = "1", |
| 301 | .variable = "variable", |
| 302 | .reply = "OK", |
| 303 | }, |
Neels Hofmeyr | 6769ad6 | 2017-12-16 04:01:54 +0100 | [diff] [blame] | 304 | }, |
| 305 | { "SET_REPLY 1 variable OK", |
| 306 | { |
| 307 | .type = CTRL_TYPE_SET_REPLY, |
| 308 | .id = "1", |
| 309 | .variable = "variable", |
| 310 | .reply = "OK", |
| 311 | }, |
Neels Hofmeyr | 6769ad6 | 2017-12-16 04:01:54 +0100 | [diff] [blame] | 312 | }, |
| 313 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 314 | }; |
| 315 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 316 | static void test_messages() |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 317 | { |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 318 | struct ctrl_handle *ctrl; |
| 319 | struct ctrl_connection *ccon; |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 320 | int i; |
| 321 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 322 | ctrl = ctrl_handle_alloc2(ctx, NULL, NULL, 0); |
| 323 | ccon = talloc_zero(ctx, struct ctrl_connection); |
| 324 | |
| 325 | osmo_wqueue_init(&ccon->write_queue, 1); |
| 326 | |
| 327 | for (i = 0; i < ARRAY_SIZE(test_messages_list); i++) |
| 328 | assert_test(ctrl, ccon, &test_messages_list[i]); |
| 329 | |
| 330 | talloc_free(ccon); |
| 331 | talloc_free(ctrl); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Neels Hofmeyr | 6882b80 | 2018-04-05 02:14:12 +0200 | [diff] [blame] | 334 | CTRL_CMD_DEFINE(test_defer, "test-defer"); |
| 335 | static void ctrl_test_defer_cb(void *data) |
| 336 | { |
| 337 | struct ctrl_cmd_def *cd = data; |
| 338 | struct ctrl_cmd *cmd = cd->cmd; |
| 339 | static int i = 0; |
| 340 | printf("%s called\n", __func__); |
| 341 | |
| 342 | if (ctrl_cmd_def_is_zombie(cd)) { |
| 343 | printf("Is Zombie\n"); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | cmd->reply = talloc_asprintf(cmd, "Test Defer #%d", i++); |
| 348 | |
| 349 | ctrl_cmd_def_send(cd); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | static struct ctrl_cmd_def *test_defer_cd = NULL; |
| 354 | static int get_test_defer(struct ctrl_cmd *cmd, void *data) |
| 355 | { |
| 356 | void *ctx = cmd->node; |
| 357 | printf("%s called\n", __func__); |
| 358 | |
| 359 | test_defer_cd = ctrl_cmd_def_make(ctx, cmd, NULL, 10); |
| 360 | |
| 361 | return CTRL_CMD_HANDLED; |
| 362 | } |
| 363 | static int set_test_defer(struct ctrl_cmd *cmd, void *data) |
| 364 | { |
| 365 | return CTRL_CMD_HANDLED; |
| 366 | } |
| 367 | static int verify_test_defer(struct ctrl_cmd *cmd, const char *value, void *data) |
| 368 | { |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static void test_deferred_cmd() |
| 373 | { |
| 374 | struct ctrl_handle *ctrl; |
| 375 | struct ctrl_connection *ccon; |
| 376 | struct msgb *msg; |
| 377 | int result; |
| 378 | int ctx_size_was; |
| 379 | int ctx_size_before_defer; |
| 380 | |
| 381 | printf("\n%s\n", __func__); |
| 382 | ctx_size_was = talloc_total_size(ctx); |
| 383 | |
| 384 | ctrl = ctrl_handle_alloc2(ctx, NULL, NULL, 0); |
| 385 | ccon = talloc_zero(ctx, struct ctrl_connection); |
| 386 | INIT_LLIST_HEAD(&ccon->def_cmds); |
| 387 | |
| 388 | osmo_wqueue_init(&ccon->write_queue, 1); |
| 389 | |
| 390 | ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_test_defer); |
| 391 | |
| 392 | ctx_size_before_defer = talloc_total_size(ctx); |
| 393 | |
| 394 | msg = msgb_from_string("GET 123 test-defer"); |
| 395 | |
| 396 | result = ctrl_handle_msg(ctrl, ccon, msg); |
| 397 | printf("ctrl_handle_msg() returned %d\n", result); |
| 398 | |
| 399 | OSMO_ASSERT(result == CTRL_CMD_HANDLED); |
Neels Hofmeyr | a8b6cc4 | 2018-04-05 18:11:08 +0200 | [diff] [blame] | 400 | talloc_free(msg); |
Neels Hofmeyr | 6882b80 | 2018-04-05 02:14:12 +0200 | [diff] [blame] | 401 | |
| 402 | /* Expecting a ctrl_cmd_def as well as the cmd to still be allocated */ |
| 403 | if (talloc_total_size(ctx) <= ctx_size_before_defer) { |
| 404 | printf("deferred command apparently deallocated too soon\n"); |
Neels Hofmeyr | 6882b80 | 2018-04-05 02:14:12 +0200 | [diff] [blame] | 405 | talloc_report_full(ctx, stdout); |
| 406 | OSMO_ASSERT(false); |
| 407 | } |
| 408 | |
| 409 | printf("invoking ctrl_test_defer_cb() asynchronously\n"); |
| 410 | ctrl_test_defer_cb(test_defer_cd); |
| 411 | |
Neels Hofmeyr | a8b6cc4 | 2018-04-05 18:11:08 +0200 | [diff] [blame] | 412 | /* simulate sending of the reply */ |
| 413 | osmo_wqueue_clear(&ccon->write_queue); |
| 414 | |
Neels Hofmeyr | 6882b80 | 2018-04-05 02:14:12 +0200 | [diff] [blame] | 415 | /* And now the deferred cmd should be cleaned up completely. */ |
| 416 | if (talloc_total_size(ctx) != ctx_size_before_defer) { |
| 417 | printf("mem leak!\n"); |
| 418 | talloc_report_full(ctx, stdout); |
| 419 | OSMO_ASSERT(false); |
| 420 | } |
| 421 | |
Neels Hofmeyr | 6882b80 | 2018-04-05 02:14:12 +0200 | [diff] [blame] | 422 | talloc_free(ccon); |
| 423 | talloc_free(ctrl); |
| 424 | |
| 425 | if (talloc_total_size(ctx) != ctx_size_was) { |
| 426 | printf("mem leak!\n"); |
| 427 | talloc_report_full(ctx, stdout); |
| 428 | OSMO_ASSERT(false); |
| 429 | } |
| 430 | |
| 431 | printf("success\n"); |
| 432 | } |
| 433 | |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 434 | static struct log_info_cat test_categories[] = { |
| 435 | }; |
| 436 | |
| 437 | static struct log_info info = { |
| 438 | .cat = test_categories, |
| 439 | .num_cat = ARRAY_SIZE(test_categories), |
| 440 | }; |
| 441 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 442 | int main(int argc, char **argv) |
| 443 | { |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 444 | ctx = talloc_named_const(NULL, 1, "ctrl_test"); |
Neels Hofmeyr | a8b6cc4 | 2018-04-05 18:11:08 +0200 | [diff] [blame] | 445 | osmo_init_logging2(ctx, &info); |
| 446 | msgb_talloc_ctx_init(ctx, 0); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 447 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 448 | printf("Checking ctrl types...\n"); |
| 449 | |
| 450 | check_type(CTRL_TYPE_UNKNOWN); |
| 451 | check_type(CTRL_TYPE_GET); |
| 452 | check_type(CTRL_TYPE_SET); |
| 453 | check_type(CTRL_TYPE_GET_REPLY); |
| 454 | check_type(CTRL_TYPE_SET_REPLY); |
| 455 | check_type(CTRL_TYPE_TRAP); |
| 456 | check_type(CTRL_TYPE_ERROR); |
| 457 | check_type(64); |
| 458 | |
Neels Hofmeyr | 83aee83 | 2017-12-16 05:38:37 +0100 | [diff] [blame] | 459 | test_messages(); |
Neels Hofmeyr | 505c965 | 2017-09-26 15:24:58 +0200 | [diff] [blame] | 460 | |
Neels Hofmeyr | 6882b80 | 2018-04-05 02:14:12 +0200 | [diff] [blame] | 461 | test_deferred_cmd(); |
| 462 | |
Neels Hofmeyr | a8b6cc4 | 2018-04-05 18:11:08 +0200 | [diff] [blame] | 463 | /* Expecting root ctx + msgb root ctx + 5 logging elements */ |
| 464 | if (talloc_total_blocks(ctx) != 7) { |
| 465 | talloc_report_full(ctx, stdout); |
| 466 | OSMO_ASSERT(false); |
| 467 | } |
| 468 | |
Max | 70c7d41 | 2017-02-24 13:59:14 +0100 | [diff] [blame] | 469 | return 0; |
| 470 | } |