Oliver Smith | 3a9f267 | 2019-11-20 10:56:35 +0100 | [diff] [blame] | 1 | /* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <assert.h> |
| 21 | #include <errno.h> |
| 22 | #include <string.h> |
| 23 | #include <osmocom/core/application.h> |
| 24 | #include <osmocom/core/logging.h> |
| 25 | #include <osmocom/core/utils.h> |
| 26 | #include <osmocom/mslookup/mdns_rfc.h> |
| 27 | #include <osmocom/mslookup/mdns_msg.h> |
| 28 | |
| 29 | struct qname_enc_dec_test { |
| 30 | const char *domain; |
| 31 | const char *qname; |
| 32 | size_t qname_max_len; /* default: strlen(qname) + 1 */ |
| 33 | }; |
| 34 | |
| 35 | static const struct qname_enc_dec_test qname_enc_dec_test_data[] = { |
| 36 | { |
| 37 | /* OK: typical mslookup domain */ |
| 38 | .domain = "hlr.1234567.imsi", |
| 39 | .qname = "\x03" "hlr" "\x07" "1234567" "\x04" "imsi", |
| 40 | }, |
| 41 | { |
| 42 | /* Wrong format: double dot */ |
| 43 | .domain = "hlr..imsi", |
| 44 | .qname = NULL, |
| 45 | }, |
| 46 | { |
| 47 | /* Wrong format: double dot */ |
| 48 | .domain = "hlr", |
| 49 | .qname = "\x03hlr\0\x03imsi", |
| 50 | }, |
| 51 | { |
| 52 | /* Wrong format: dot at end */ |
| 53 | .domain = "hlr.", |
| 54 | .qname = NULL, |
| 55 | }, |
| 56 | { |
| 57 | /* Wrong format: dot at start */ |
| 58 | .domain = ".hlr", |
| 59 | .qname = NULL, |
| 60 | }, |
| 61 | { |
| 62 | /* Wrong format: empty */ |
| 63 | .domain = "", |
| 64 | .qname = NULL, |
| 65 | }, |
| 66 | { |
| 67 | /* OK: maximum length */ |
| 68 | .domain = |
| 69 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 70 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 71 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 72 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 73 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 74 | "12345" |
| 75 | , |
| 76 | .qname = |
| 77 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 78 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 79 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 80 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 81 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 82 | "\x05" "12345" |
| 83 | }, |
| 84 | { |
| 85 | /* Error: too long domain */ |
| 86 | .domain = |
| 87 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 88 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 89 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 90 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 91 | "123456789." "123456789." "123456789." "123456789." "123456789." |
| 92 | "12345toolong" |
| 93 | , |
| 94 | .qname = NULL, |
| 95 | }, |
| 96 | { |
| 97 | /* Error: too long qname */ |
| 98 | .domain = NULL, |
| 99 | .qname = |
| 100 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 101 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 102 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 103 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 104 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 105 | "\t123456789\t123456789\t123456789\t123456789\t123456789" |
| 106 | }, |
| 107 | { |
| 108 | /* Error: wrong token length in qname */ |
| 109 | .domain = NULL, |
| 110 | .qname = "\x03" "hlr" "\x07" "1234567" "\x05" "imsi", |
| 111 | }, |
| 112 | { |
| 113 | /* Error: wrong token length in qname */ |
| 114 | .domain = NULL, |
| 115 | .qname = "\x02" "hlr" "\x07" "1234567" "\x04" "imsi", |
| 116 | }, |
| 117 | { |
| 118 | /* Wrong format: token length at end of qname */ |
| 119 | .domain = NULL, |
| 120 | .qname = "\x03hlr\x03", |
| 121 | }, |
| 122 | { |
| 123 | /* Error: overflow in label length */ |
| 124 | .domain = NULL, |
| 125 | .qname = "\x03" "hlr" "\x07" "1234567" "\x04" "imsi", |
| 126 | .qname_max_len = 17, |
| 127 | }, |
| 128 | }; |
| 129 | |
| 130 | void test_enc_dec_rfc_qname(void *ctx) |
| 131 | { |
| 132 | char quote_buf[300]; |
| 133 | int i; |
| 134 | |
| 135 | fprintf(stderr, "-- %s --\n", __func__); |
| 136 | |
| 137 | for (i = 0; i < ARRAY_SIZE(qname_enc_dec_test_data); i++) { |
| 138 | const struct qname_enc_dec_test *t = &qname_enc_dec_test_data[i]; |
| 139 | char *res; |
| 140 | |
| 141 | if (t->domain) { |
| 142 | fprintf(stderr, "domain: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), t->domain, -1)); |
| 143 | fprintf(stderr, "exp: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), t->qname, -1)); |
| 144 | res = osmo_mdns_rfc_qname_encode(ctx, t->domain); |
| 145 | fprintf(stderr, "res: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), res, -1)); |
| 146 | if (t->qname == res || (t->qname && res && strcmp(t->qname, res) == 0)) |
| 147 | fprintf(stderr, "=> OK\n"); |
| 148 | else |
| 149 | fprintf(stderr, "=> ERROR\n"); |
| 150 | if (res) |
| 151 | talloc_free(res); |
| 152 | fprintf(stderr, "\n"); |
| 153 | } |
| 154 | |
| 155 | if (t->qname) { |
| 156 | size_t qname_max_len = t->qname_max_len; |
| 157 | if (qname_max_len) |
| 158 | fprintf(stderr, "qname_max_len: %lu\n", qname_max_len); |
| 159 | else |
| 160 | qname_max_len = strlen(t->qname) + 1; |
| 161 | |
| 162 | fprintf(stderr, "qname: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), t->qname, -1)); |
| 163 | fprintf(stderr, "exp: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), t->domain, -1)); |
| 164 | res = osmo_mdns_rfc_qname_decode(ctx, t->qname, qname_max_len); |
| 165 | fprintf(stderr, "res: %s\n", osmo_quote_str_buf2(quote_buf, sizeof(quote_buf), res, -1)); |
| 166 | if (t->domain == res || (t->domain && res && strcmp(t->domain, res) == 0)) |
| 167 | fprintf(stderr, "=> OK\n"); |
| 168 | else |
| 169 | fprintf(stderr, "=> ERROR\n"); |
| 170 | if (res) |
| 171 | talloc_free(res); |
| 172 | fprintf(stderr, "\n"); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | #define PRINT_HDR(hdr, name) \ |
| 178 | fprintf(stderr, "header %s:\n" \ |
| 179 | ".id = %i\n" \ |
| 180 | ".qr = %i\n" \ |
| 181 | ".opcode = %x\n" \ |
| 182 | ".aa = %i\n" \ |
| 183 | ".tc = %i\n" \ |
| 184 | ".rd = %i\n" \ |
| 185 | ".ra = %i\n" \ |
| 186 | ".z = %x\n" \ |
| 187 | ".rcode = %x\n" \ |
| 188 | ".qdcount = %u\n" \ |
| 189 | ".ancount = %u\n" \ |
| 190 | ".nscount = %u\n" \ |
| 191 | ".arcount = %u\n", \ |
| 192 | name, hdr.id, hdr.qr, hdr.opcode, hdr.aa, hdr.tc, hdr.rd, hdr.ra, hdr.z, hdr.rcode, hdr.qdcount, \ |
| 193 | hdr.ancount, hdr.nscount, hdr.arcount) |
| 194 | |
| 195 | static const struct osmo_mdns_rfc_header header_enc_dec_test_data[] = { |
| 196 | { |
| 197 | /* Typical use case for mslookup */ |
| 198 | .id = 1337, |
| 199 | .qdcount = 1, |
| 200 | }, |
| 201 | { |
| 202 | /* Fill out everything */ |
| 203 | .id = 42, |
| 204 | .qr = 1, |
| 205 | .opcode = 0x02, |
| 206 | .aa = 1, |
| 207 | .tc = 1, |
| 208 | .rd = 1, |
| 209 | .ra = 1, |
| 210 | .z = 0x02, |
| 211 | .rcode = 0x03, |
| 212 | .qdcount = 1234, |
| 213 | .ancount = 1111, |
| 214 | .nscount = 2222, |
| 215 | .arcount = 3333, |
| 216 | }, |
| 217 | }; |
| 218 | |
| 219 | void test_enc_dec_rfc_header() |
| 220 | { |
| 221 | int i; |
| 222 | |
| 223 | fprintf(stderr, "-- %s --\n", __func__); |
| 224 | for (i = 0; i< ARRAY_SIZE(header_enc_dec_test_data); i++) { |
| 225 | const struct osmo_mdns_rfc_header in = header_enc_dec_test_data[i]; |
| 226 | struct osmo_mdns_rfc_header out = {0}; |
| 227 | struct msgb *msg = msgb_alloc(4096, "dns_test"); |
| 228 | |
| 229 | PRINT_HDR(in, "in"); |
| 230 | osmo_mdns_rfc_header_encode(msg, &in); |
| 231 | fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg))); |
| 232 | assert(osmo_mdns_rfc_header_decode(msgb_data(msg), msgb_length(msg), &out) == 0); |
| 233 | PRINT_HDR(out, "out"); |
| 234 | |
| 235 | fprintf(stderr, "in (hexdump): %s\n", osmo_hexdump((unsigned char *)&in, sizeof(in))); |
| 236 | fprintf(stderr, "out (hexdump): %s\n", osmo_hexdump((unsigned char *)&out, sizeof(out))); |
| 237 | assert(memcmp(&in, &out, sizeof(in)) == 0); |
| 238 | |
| 239 | fprintf(stderr, "=> OK\n\n"); |
| 240 | msgb_free(msg); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | void test_enc_dec_rfc_header_einval() |
| 245 | { |
| 246 | struct osmo_mdns_rfc_header out = {0}; |
| 247 | struct msgb *msg = msgb_alloc(4096, "dns_test"); |
| 248 | fprintf(stderr, "-- %s --\n", __func__); |
| 249 | |
| 250 | assert(osmo_mdns_rfc_header_decode(msgb_data(msg), 11, &out) == -EINVAL); |
| 251 | fprintf(stderr, "=> OK\n\n"); |
| 252 | |
| 253 | msgb_free(msg); |
| 254 | } |
| 255 | |
| 256 | #define PRINT_QST(qst, name) \ |
| 257 | fprintf(stderr, "question %s:\n" \ |
| 258 | ".domain = %s\n" \ |
| 259 | ".qtype = %i\n" \ |
| 260 | ".qclass = %i\n", \ |
| 261 | name, (qst)->domain, (qst)->qtype, (qst)->qclass) |
| 262 | |
| 263 | static const struct osmo_mdns_rfc_question question_enc_dec_test_data[] = { |
| 264 | { |
| 265 | .domain = "hlr.1234567.imsi", |
| 266 | .qtype = OSMO_MDNS_RFC_RECORD_TYPE_ALL, |
| 267 | .qclass = OSMO_MDNS_RFC_CLASS_IN, |
| 268 | }, |
| 269 | { |
| 270 | .domain = "hlr.1234567.imsi", |
| 271 | .qtype = OSMO_MDNS_RFC_RECORD_TYPE_A, |
| 272 | .qclass = OSMO_MDNS_RFC_CLASS_ALL, |
| 273 | }, |
| 274 | { |
| 275 | .domain = "hlr.1234567.imsi", |
| 276 | .qtype = OSMO_MDNS_RFC_RECORD_TYPE_AAAA, |
| 277 | .qclass = OSMO_MDNS_RFC_CLASS_ALL, |
| 278 | }, |
| 279 | }; |
| 280 | |
| 281 | void test_enc_dec_rfc_question(void *ctx) |
| 282 | { |
| 283 | int i; |
| 284 | |
| 285 | fprintf(stderr, "-- %s --\n", __func__); |
| 286 | for (i = 0; i< ARRAY_SIZE(question_enc_dec_test_data); i++) { |
| 287 | const struct osmo_mdns_rfc_question in = question_enc_dec_test_data[i]; |
| 288 | struct osmo_mdns_rfc_question *out; |
| 289 | struct msgb *msg = msgb_alloc(4096, "dns_test"); |
| 290 | |
| 291 | PRINT_QST(&in, "in"); |
| 292 | assert(osmo_mdns_rfc_question_encode(ctx, msg, &in) == 0); |
| 293 | fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg))); |
| 294 | out = osmo_mdns_rfc_question_decode(ctx, msgb_data(msg), msgb_length(msg)); |
| 295 | assert(out); |
| 296 | PRINT_QST(out, "out"); |
| 297 | |
| 298 | if (strcmp(in.domain, out->domain) != 0) |
| 299 | fprintf(stderr, "=> ERROR: domain does not match\n"); |
| 300 | else if (in.qtype != out->qtype) |
| 301 | fprintf(stderr, "=> ERROR: qtype does not match\n"); |
| 302 | else if (in.qclass != out->qclass) |
| 303 | fprintf(stderr, "=> ERROR: qclass does not match\n"); |
| 304 | else |
| 305 | fprintf(stderr, "=> OK\n"); |
| 306 | |
| 307 | fprintf(stderr, "\n"); |
| 308 | msgb_free(msg); |
| 309 | talloc_free(out); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void test_enc_dec_rfc_question_null(void *ctx) |
| 314 | { |
| 315 | uint8_t data[5] = {0}; |
| 316 | |
| 317 | fprintf(stderr, "-- %s --\n", __func__); |
| 318 | assert(osmo_mdns_rfc_question_decode(ctx, data, sizeof(data)) == NULL); |
| 319 | fprintf(stderr, "=> OK\n\n"); |
| 320 | } |
| 321 | |
| 322 | #define PRINT_REC(rec, name) \ |
| 323 | fprintf(stderr, "question %s:\n" \ |
| 324 | ".domain = %s\n" \ |
| 325 | ".type = %i\n" \ |
| 326 | ".class = %i\n" \ |
| 327 | ".ttl = %i\n" \ |
| 328 | ".rdlength = %i\n" \ |
| 329 | ".rdata = %s\n", \ |
| 330 | name, (rec)->domain, (rec)->type, (rec)->class, (rec)->ttl, (rec)->rdlength, \ |
| 331 | osmo_quote_str((char *)(rec)->rdata, (rec)->rdlength)) |
| 332 | |
| 333 | static const struct osmo_mdns_rfc_record record_enc_dec_test_data[] = { |
| 334 | { |
| 335 | .domain = "hlr.1234567.imsi", |
| 336 | .type = OSMO_MDNS_RFC_RECORD_TYPE_A, |
| 337 | .class = OSMO_MDNS_RFC_CLASS_IN, |
| 338 | .ttl = 1234, |
| 339 | .rdlength = 9, |
| 340 | .rdata = (uint8_t *)"10.42.2.1", |
| 341 | }, |
| 342 | }; |
| 343 | |
| 344 | void test_enc_dec_rfc_record(void *ctx) |
| 345 | { |
| 346 | int i; |
| 347 | |
| 348 | fprintf(stderr, "-- %s --\n", __func__); |
| 349 | for (i=0; i< ARRAY_SIZE(record_enc_dec_test_data); i++) { |
| 350 | const struct osmo_mdns_rfc_record in = record_enc_dec_test_data[i]; |
| 351 | struct osmo_mdns_rfc_record *out; |
| 352 | struct msgb *msg = msgb_alloc(4096, "dns_test"); |
| 353 | size_t record_len; |
| 354 | |
| 355 | PRINT_REC(&in, "in"); |
| 356 | assert(osmo_mdns_rfc_record_encode(ctx, msg, &in) == 0); |
| 357 | fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg))); |
| 358 | out = osmo_mdns_rfc_record_decode(ctx, msgb_data(msg), msgb_length(msg), &record_len); |
| 359 | fprintf(stderr, "record_len: %lu\n", record_len); |
| 360 | assert(out); |
| 361 | PRINT_REC(out, "out"); |
| 362 | |
| 363 | if (strcmp(in.domain, out->domain) != 0) |
| 364 | fprintf(stderr, "=> ERROR: domain does not match\n"); |
| 365 | else if (in.type != out->type) |
| 366 | fprintf(stderr, "=> ERROR: type does not match\n"); |
| 367 | else if (in.class != out->class) |
| 368 | fprintf(stderr, "=> ERROR: class does not match\n"); |
| 369 | else if (in.ttl != out->ttl) |
| 370 | fprintf(stderr, "=> ERROR: ttl does not match\n"); |
| 371 | else if (in.rdlength != out->rdlength) |
| 372 | fprintf(stderr, "=> ERROR: rdlength does not match\n"); |
| 373 | else if (memcmp(in.rdata, out->rdata, in.rdlength) != 0) |
| 374 | fprintf(stderr, "=> ERROR: rdata does not match\n"); |
| 375 | else |
| 376 | fprintf(stderr, "=> OK\n"); |
| 377 | |
| 378 | fprintf(stderr, "\n"); |
| 379 | msgb_free(msg); |
| 380 | talloc_free(out); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static uint8_t ip_v4_n[] = {23, 42, 47, 11}; |
| 385 | static uint8_t ip_v6_n[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, |
| 386 | 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00}; |
| 387 | |
| 388 | |
| 389 | enum test_records { |
| 390 | RECORD_NONE, |
| 391 | RECORD_A, |
| 392 | RECORD_AAAA, |
| 393 | RECORD_TXT_AGE, |
| 394 | RECORD_TXT_PORT_444, |
| 395 | RECORD_TXT_PORT_666, |
| 396 | RECORD_TXT_INVALID_KEY, |
| 397 | RECORD_TXT_INVALID_NO_KEY_VALUE, |
| 398 | RECORD_INVALID, |
| 399 | }; |
| 400 | struct result_from_answer_test { |
| 401 | const char *desc; |
| 402 | const enum test_records records[5]; |
| 403 | bool error; |
| 404 | const struct osmo_mslookup_result res; |
| 405 | }; |
| 406 | |
| 407 | static void test_result_from_answer(void *ctx) |
| 408 | { |
| 409 | void *print_ctx = talloc_named_const(ctx, 0, __func__); |
| 410 | struct osmo_sockaddr_str test_host_v4 = {.af = AF_INET, .port=444, .ip = "23.42.47.11"}; |
| 411 | struct osmo_sockaddr_str test_host_v6 = {.af = AF_INET6, .port=666, |
| 412 | .ip = "1122:3344:5566:7788:99aa:bbcc:ddee:ff00"}; |
| 413 | struct osmo_mslookup_result test_result_v4 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3, |
| 414 | .host_v4 = test_host_v4}; |
| 415 | struct osmo_mslookup_result test_result_v6 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3, |
| 416 | .host_v6 = test_host_v6}; |
| 417 | struct osmo_mslookup_result test_result_v4_v6 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3, |
| 418 | .host_v4 = test_host_v4, .host_v6 = test_host_v6}; |
| 419 | struct result_from_answer_test result_from_answer_data[] = { |
| 420 | { |
| 421 | .desc = "IPv4", |
| 422 | .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444}, |
| 423 | .res = test_result_v4 |
| 424 | }, |
| 425 | { |
| 426 | .desc = "IPv6", |
| 427 | .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_666}, |
| 428 | .res = test_result_v6 |
| 429 | }, |
| 430 | { |
| 431 | .desc = "IPv4 + IPv6", |
| 432 | .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444, RECORD_AAAA, RECORD_TXT_PORT_666}, |
| 433 | .res = test_result_v4_v6 |
| 434 | }, |
| 435 | { |
| 436 | .desc = "A twice", |
| 437 | .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444, RECORD_A}, |
| 438 | .error = true |
| 439 | }, |
| 440 | { |
| 441 | .desc = "AAAA twice", |
| 442 | .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_444, RECORD_AAAA}, |
| 443 | .error = true |
| 444 | }, |
| 445 | { |
| 446 | .desc = "invalid TXT: no key/value pair", |
| 447 | .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_INVALID_NO_KEY_VALUE}, |
| 448 | .error = true |
| 449 | }, |
| 450 | { |
| 451 | .desc = "age twice", |
| 452 | .records = {RECORD_TXT_AGE, RECORD_TXT_AGE}, |
| 453 | .error = true |
| 454 | }, |
| 455 | { |
| 456 | .desc = "port as first record", |
| 457 | .records = {RECORD_TXT_PORT_444}, |
| 458 | .error = true |
| 459 | }, |
| 460 | { |
| 461 | .desc = "port without previous ip record", |
| 462 | .records = {RECORD_TXT_AGE, RECORD_TXT_PORT_444}, |
| 463 | .error = true |
| 464 | }, |
| 465 | { |
| 466 | .desc = "invalid TXT: invalid key", |
| 467 | .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_INVALID_KEY}, |
| 468 | .error = true |
| 469 | }, |
| 470 | { |
| 471 | .desc = "unexpected record type", |
| 472 | .records = {RECORD_TXT_AGE, RECORD_INVALID}, |
| 473 | .error = true |
| 474 | }, |
| 475 | { |
| 476 | .desc = "missing record: age", |
| 477 | .records = {RECORD_A, RECORD_TXT_PORT_444}, |
| 478 | .error = true |
| 479 | }, |
| 480 | { |
| 481 | .desc = "missing record: port for ipv4", |
| 482 | .records = {RECORD_TXT_AGE, RECORD_A}, |
| 483 | .error = true |
| 484 | }, |
| 485 | { |
| 486 | .desc = "missing record: port for ipv4 #2", |
| 487 | .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_666, RECORD_A}, |
| 488 | .error = true |
| 489 | }, |
| 490 | }; |
| 491 | int i = 0; |
| 492 | int j = 0; |
| 493 | |
| 494 | fprintf(stderr, "-- %s --\n", __func__); |
| 495 | for (i = 0; i < ARRAY_SIZE(result_from_answer_data); i++) { |
| 496 | struct result_from_answer_test *t = &result_from_answer_data[i]; |
| 497 | struct osmo_mdns_msg_answer ans = {0}; |
| 498 | struct osmo_mslookup_result res = {0}; |
| 499 | void *ctx_test = talloc_named_const(ctx, 0, t->desc); |
| 500 | bool is_error; |
| 501 | |
| 502 | fprintf(stderr, "---\n"); |
| 503 | fprintf(stderr, "test: %s\n", t->desc); |
| 504 | fprintf(stderr, "error: %s\n", t->error ? "true" : "false"); |
| 505 | fprintf(stderr, "records:\n"); |
| 506 | /* Build records list */ |
| 507 | INIT_LLIST_HEAD(&ans.records); |
| 508 | for (j = 0; j < ARRAY_SIZE(t->records); j++) { |
| 509 | struct osmo_mdns_record *rec = NULL; |
| 510 | |
| 511 | switch (t->records[j]) { |
| 512 | case RECORD_NONE: |
| 513 | break; |
| 514 | case RECORD_A: |
| 515 | fprintf(stderr, "- A 42.42.42.42\n"); |
| 516 | rec = talloc_zero(ctx_test, struct osmo_mdns_record); |
| 517 | rec->type = OSMO_MDNS_RFC_RECORD_TYPE_A; |
| 518 | rec->data = ip_v4_n; |
| 519 | rec->length = sizeof(ip_v4_n); |
| 520 | break; |
| 521 | case RECORD_AAAA: |
| 522 | fprintf(stderr, "- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00\n"); |
| 523 | rec = talloc_zero(ctx_test, struct osmo_mdns_record); |
| 524 | rec->type = OSMO_MDNS_RFC_RECORD_TYPE_AAAA; |
| 525 | rec->data = ip_v6_n; |
| 526 | rec->length = sizeof(ip_v6_n); |
| 527 | break; |
| 528 | case RECORD_TXT_AGE: |
| 529 | fprintf(stderr, "- TXT age=3\n"); |
| 530 | rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "age", "3"); |
| 531 | break; |
| 532 | case RECORD_TXT_PORT_444: |
| 533 | fprintf(stderr, "- TXT port=444\n"); |
| 534 | rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "port", "444"); |
| 535 | break; |
| 536 | case RECORD_TXT_PORT_666: |
| 537 | fprintf(stderr, "- TXT port=666\n"); |
| 538 | rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "port", "666"); |
| 539 | break; |
| 540 | case RECORD_TXT_INVALID_KEY: |
| 541 | fprintf(stderr, "- TXT hello=world\n"); |
| 542 | rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "hello", "world"); |
| 543 | break; |
| 544 | case RECORD_TXT_INVALID_NO_KEY_VALUE: |
| 545 | fprintf(stderr, "- TXT 12345\n"); |
| 546 | rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "12", "45"); |
| 547 | rec->data[3] = '3'; |
| 548 | break; |
| 549 | case RECORD_INVALID: |
| 550 | fprintf(stderr, "- (invalid)\n"); |
| 551 | rec = talloc_zero(ctx, struct osmo_mdns_record); |
| 552 | rec->type = OSMO_MDNS_RFC_RECORD_TYPE_UNKNOWN; |
| 553 | break; |
| 554 | } |
| 555 | |
| 556 | if (rec) |
| 557 | llist_add_tail(&rec->list, &ans.records); |
| 558 | } |
| 559 | |
| 560 | /* Verify output */ |
| 561 | is_error = (osmo_mdns_result_from_answer(&res, &ans) != 0); |
| 562 | if (t->error != is_error) { |
| 563 | fprintf(stderr, "got %s\n", is_error ? "error" : "no error"); |
| 564 | OSMO_ASSERT(false); |
| 565 | } |
| 566 | if (!t->error) { |
| 567 | fprintf(stderr, "exp: %s\n", osmo_mslookup_result_name_c(print_ctx, NULL, &t->res)); |
| 568 | fprintf(stderr, "res: %s\n", osmo_mslookup_result_name_c(print_ctx, NULL, &res)); |
| 569 | OSMO_ASSERT(t->res.rc == res.rc); |
| 570 | OSMO_ASSERT(!osmo_sockaddr_str_cmp(&t->res.host_v4, &res.host_v4)); |
| 571 | OSMO_ASSERT(!osmo_sockaddr_str_cmp(&t->res.host_v6, &res.host_v6)); |
| 572 | OSMO_ASSERT(t->res.age == res.age); |
| 573 | OSMO_ASSERT(t->res.last == res.last); |
| 574 | } |
| 575 | |
| 576 | talloc_free(ctx_test); |
| 577 | fprintf(stderr, "=> OK\n"); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | int main() |
| 582 | { |
| 583 | void *ctx = talloc_named_const(NULL, 0, "main"); |
| 584 | osmo_init_logging2(ctx, NULL); |
| 585 | |
| 586 | log_set_print_filename(osmo_stderr_target, 0); |
| 587 | log_set_print_level(osmo_stderr_target, 1); |
| 588 | log_set_print_category(osmo_stderr_target, 1); |
| 589 | log_set_print_category_hex(osmo_stderr_target, 0); |
| 590 | log_set_use_color(osmo_stderr_target, 0); |
| 591 | |
| 592 | test_enc_dec_rfc_qname(ctx); |
| 593 | test_enc_dec_rfc_header(); |
| 594 | test_enc_dec_rfc_header_einval(); |
| 595 | test_enc_dec_rfc_question(ctx); |
| 596 | test_enc_dec_rfc_question_null(ctx); |
| 597 | test_enc_dec_rfc_record(ctx); |
| 598 | |
| 599 | test_result_from_answer(ctx); |
| 600 | |
| 601 | return 0; |
| 602 | } |