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