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