blob: e9fc1fbca0a0d151c127684b4383e49f0650fc32 [file] [log] [blame]
Oliver Smith3a9f2672019-11-20 10:56:35 +01001/* 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
29struct qname_enc_dec_test {
30 const char *domain;
31 const char *qname;
32 size_t qname_max_len; /* default: strlen(qname) + 1 */
33};
34
Oliver Smith3a9f2672019-11-20 10:56:35 +010035#define PRINT_HDR(hdr, name) \
36 fprintf(stderr, "header %s:\n" \
37 ".id = %i\n" \
38 ".qr = %i\n" \
39 ".opcode = %x\n" \
40 ".aa = %i\n" \
41 ".tc = %i\n" \
42 ".rd = %i\n" \
43 ".ra = %i\n" \
44 ".z = %x\n" \
45 ".rcode = %x\n" \
46 ".qdcount = %u\n" \
47 ".ancount = %u\n" \
48 ".nscount = %u\n" \
49 ".arcount = %u\n", \
50 name, hdr.id, hdr.qr, hdr.opcode, hdr.aa, hdr.tc, hdr.rd, hdr.ra, hdr.z, hdr.rcode, hdr.qdcount, \
51 hdr.ancount, hdr.nscount, hdr.arcount)
52
53static const struct osmo_mdns_rfc_header header_enc_dec_test_data[] = {
54 {
55 /* Typical use case for mslookup */
56 .id = 1337,
57 .qdcount = 1,
58 },
59 {
60 /* Fill out everything */
61 .id = 42,
62 .qr = 1,
63 .opcode = 0x02,
64 .aa = 1,
65 .tc = 1,
66 .rd = 1,
67 .ra = 1,
68 .z = 0x02,
69 .rcode = 0x03,
70 .qdcount = 1234,
71 .ancount = 1111,
72 .nscount = 2222,
73 .arcount = 3333,
74 },
75};
76
Harald Welte7a476532022-11-03 11:38:41 +010077void test_enc_dec_rfc_header(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +010078{
79 int i;
80
81 fprintf(stderr, "-- %s --\n", __func__);
82 for (i = 0; i< ARRAY_SIZE(header_enc_dec_test_data); i++) {
83 const struct osmo_mdns_rfc_header in = header_enc_dec_test_data[i];
84 struct osmo_mdns_rfc_header out = {0};
85 struct msgb *msg = msgb_alloc(4096, "dns_test");
86
87 PRINT_HDR(in, "in");
88 osmo_mdns_rfc_header_encode(msg, &in);
89 fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
90 assert(osmo_mdns_rfc_header_decode(msgb_data(msg), msgb_length(msg), &out) == 0);
91 PRINT_HDR(out, "out");
92
93 fprintf(stderr, "in (hexdump): %s\n", osmo_hexdump((unsigned char *)&in, sizeof(in)));
94 fprintf(stderr, "out (hexdump): %s\n", osmo_hexdump((unsigned char *)&out, sizeof(out)));
95 assert(memcmp(&in, &out, sizeof(in)) == 0);
96
97 fprintf(stderr, "=> OK\n\n");
98 msgb_free(msg);
99 }
100}
101
Harald Welte7a476532022-11-03 11:38:41 +0100102void test_enc_dec_rfc_header_einval(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100103{
104 struct osmo_mdns_rfc_header out = {0};
105 struct msgb *msg = msgb_alloc(4096, "dns_test");
106 fprintf(stderr, "-- %s --\n", __func__);
107
108 assert(osmo_mdns_rfc_header_decode(msgb_data(msg), 11, &out) == -EINVAL);
109 fprintf(stderr, "=> OK\n\n");
110
111 msgb_free(msg);
112}
113
114#define PRINT_QST(qst, name) \
115 fprintf(stderr, "question %s:\n" \
116 ".domain = %s\n" \
117 ".qtype = %i\n" \
118 ".qclass = %i\n", \
119 name, (qst)->domain, (qst)->qtype, (qst)->qclass)
120
121static const struct osmo_mdns_rfc_question question_enc_dec_test_data[] = {
122 {
123 .domain = "hlr.1234567.imsi",
124 .qtype = OSMO_MDNS_RFC_RECORD_TYPE_ALL,
125 .qclass = OSMO_MDNS_RFC_CLASS_IN,
126 },
127 {
128 .domain = "hlr.1234567.imsi",
129 .qtype = OSMO_MDNS_RFC_RECORD_TYPE_A,
130 .qclass = OSMO_MDNS_RFC_CLASS_ALL,
131 },
132 {
133 .domain = "hlr.1234567.imsi",
134 .qtype = OSMO_MDNS_RFC_RECORD_TYPE_AAAA,
135 .qclass = OSMO_MDNS_RFC_CLASS_ALL,
136 },
137};
138
139void test_enc_dec_rfc_question(void *ctx)
140{
141 int i;
142
143 fprintf(stderr, "-- %s --\n", __func__);
144 for (i = 0; i< ARRAY_SIZE(question_enc_dec_test_data); i++) {
145 const struct osmo_mdns_rfc_question in = question_enc_dec_test_data[i];
146 struct osmo_mdns_rfc_question *out;
147 struct msgb *msg = msgb_alloc(4096, "dns_test");
148
149 PRINT_QST(&in, "in");
Oliver Smithf80ab762022-12-23 13:35:07 +0100150 assert(osmo_mdns_rfc_question_encode(msg, &in) == 0);
Oliver Smith3a9f2672019-11-20 10:56:35 +0100151 fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
152 out = osmo_mdns_rfc_question_decode(ctx, msgb_data(msg), msgb_length(msg));
153 assert(out);
154 PRINT_QST(out, "out");
155
156 if (strcmp(in.domain, out->domain) != 0)
157 fprintf(stderr, "=> ERROR: domain does not match\n");
158 else if (in.qtype != out->qtype)
159 fprintf(stderr, "=> ERROR: qtype does not match\n");
160 else if (in.qclass != out->qclass)
161 fprintf(stderr, "=> ERROR: qclass does not match\n");
162 else
163 fprintf(stderr, "=> OK\n");
164
165 fprintf(stderr, "\n");
166 msgb_free(msg);
167 talloc_free(out);
168 }
169}
170
171void test_enc_dec_rfc_question_null(void *ctx)
172{
173 uint8_t data[5] = {0};
174
175 fprintf(stderr, "-- %s --\n", __func__);
176 assert(osmo_mdns_rfc_question_decode(ctx, data, sizeof(data)) == NULL);
177 fprintf(stderr, "=> OK\n\n");
178}
179
180#define PRINT_REC(rec, name) \
181 fprintf(stderr, "question %s:\n" \
182 ".domain = %s\n" \
183 ".type = %i\n" \
184 ".class = %i\n" \
185 ".ttl = %i\n" \
186 ".rdlength = %i\n" \
187 ".rdata = %s\n", \
188 name, (rec)->domain, (rec)->type, (rec)->class, (rec)->ttl, (rec)->rdlength, \
189 osmo_quote_str((char *)(rec)->rdata, (rec)->rdlength))
190
191static const struct osmo_mdns_rfc_record record_enc_dec_test_data[] = {
192 {
193 .domain = "hlr.1234567.imsi",
194 .type = OSMO_MDNS_RFC_RECORD_TYPE_A,
195 .class = OSMO_MDNS_RFC_CLASS_IN,
196 .ttl = 1234,
197 .rdlength = 9,
198 .rdata = (uint8_t *)"10.42.2.1",
199 },
200};
201
202void test_enc_dec_rfc_record(void *ctx)
203{
204 int i;
205
206 fprintf(stderr, "-- %s --\n", __func__);
207 for (i=0; i< ARRAY_SIZE(record_enc_dec_test_data); i++) {
208 const struct osmo_mdns_rfc_record in = record_enc_dec_test_data[i];
209 struct osmo_mdns_rfc_record *out;
210 struct msgb *msg = msgb_alloc(4096, "dns_test");
211 size_t record_len;
212
213 PRINT_REC(&in, "in");
Oliver Smithf80ab762022-12-23 13:35:07 +0100214 assert(osmo_mdns_rfc_record_encode(msg, &in) == 0);
Oliver Smith3a9f2672019-11-20 10:56:35 +0100215 fprintf(stderr, "encoded: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
216 out = osmo_mdns_rfc_record_decode(ctx, msgb_data(msg), msgb_length(msg), &record_len);
217 fprintf(stderr, "record_len: %lu\n", record_len);
218 assert(out);
219 PRINT_REC(out, "out");
220
221 if (strcmp(in.domain, out->domain) != 0)
222 fprintf(stderr, "=> ERROR: domain does not match\n");
223 else if (in.type != out->type)
224 fprintf(stderr, "=> ERROR: type does not match\n");
225 else if (in.class != out->class)
226 fprintf(stderr, "=> ERROR: class does not match\n");
227 else if (in.ttl != out->ttl)
228 fprintf(stderr, "=> ERROR: ttl does not match\n");
229 else if (in.rdlength != out->rdlength)
230 fprintf(stderr, "=> ERROR: rdlength does not match\n");
231 else if (memcmp(in.rdata, out->rdata, in.rdlength) != 0)
232 fprintf(stderr, "=> ERROR: rdata does not match\n");
233 else
234 fprintf(stderr, "=> OK\n");
235
236 fprintf(stderr, "\n");
237 msgb_free(msg);
238 talloc_free(out);
239 }
240}
241
242static uint8_t ip_v4_n[] = {23, 42, 47, 11};
243static uint8_t ip_v6_n[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
244 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00};
245
246
247enum test_records {
248 RECORD_NONE,
249 RECORD_A,
250 RECORD_AAAA,
251 RECORD_TXT_AGE,
252 RECORD_TXT_PORT_444,
253 RECORD_TXT_PORT_666,
254 RECORD_TXT_INVALID_KEY,
255 RECORD_TXT_INVALID_NO_KEY_VALUE,
256 RECORD_INVALID,
257};
258struct result_from_answer_test {
259 const char *desc;
260 const enum test_records records[5];
261 bool error;
262 const struct osmo_mslookup_result res;
263};
264
265static void test_result_from_answer(void *ctx)
266{
267 void *print_ctx = talloc_named_const(ctx, 0, __func__);
268 struct osmo_sockaddr_str test_host_v4 = {.af = AF_INET, .port=444, .ip = "23.42.47.11"};
269 struct osmo_sockaddr_str test_host_v6 = {.af = AF_INET6, .port=666,
270 .ip = "1122:3344:5566:7788:99aa:bbcc:ddee:ff00"};
271 struct osmo_mslookup_result test_result_v4 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3,
272 .host_v4 = test_host_v4};
273 struct osmo_mslookup_result test_result_v6 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3,
274 .host_v6 = test_host_v6};
275 struct osmo_mslookup_result test_result_v4_v6 = {.rc = OSMO_MSLOOKUP_RC_RESULT, .age = 3,
276 .host_v4 = test_host_v4, .host_v6 = test_host_v6};
277 struct result_from_answer_test result_from_answer_data[] = {
278 {
279 .desc = "IPv4",
280 .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444},
281 .res = test_result_v4
282 },
283 {
284 .desc = "IPv6",
285 .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_666},
286 .res = test_result_v6
287 },
288 {
289 .desc = "IPv4 + IPv6",
290 .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444, RECORD_AAAA, RECORD_TXT_PORT_666},
291 .res = test_result_v4_v6
292 },
293 {
294 .desc = "A twice",
295 .records = {RECORD_TXT_AGE, RECORD_A, RECORD_TXT_PORT_444, RECORD_A},
296 .error = true
297 },
298 {
299 .desc = "AAAA twice",
300 .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_444, RECORD_AAAA},
301 .error = true
302 },
303 {
304 .desc = "invalid TXT: no key/value pair",
305 .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_INVALID_NO_KEY_VALUE},
306 .error = true
307 },
308 {
309 .desc = "age twice",
310 .records = {RECORD_TXT_AGE, RECORD_TXT_AGE},
311 .error = true
312 },
313 {
314 .desc = "port as first record",
315 .records = {RECORD_TXT_PORT_444},
316 .error = true
317 },
318 {
319 .desc = "port without previous ip record",
320 .records = {RECORD_TXT_AGE, RECORD_TXT_PORT_444},
321 .error = true
322 },
323 {
324 .desc = "invalid TXT: invalid key",
325 .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_INVALID_KEY},
326 .error = true
327 },
328 {
329 .desc = "unexpected record type",
330 .records = {RECORD_TXT_AGE, RECORD_INVALID},
331 .error = true
332 },
333 {
334 .desc = "missing record: age",
335 .records = {RECORD_A, RECORD_TXT_PORT_444},
336 .error = true
337 },
338 {
339 .desc = "missing record: port for ipv4",
340 .records = {RECORD_TXT_AGE, RECORD_A},
341 .error = true
342 },
343 {
344 .desc = "missing record: port for ipv4 #2",
345 .records = {RECORD_TXT_AGE, RECORD_AAAA, RECORD_TXT_PORT_666, RECORD_A},
346 .error = true
347 },
348 };
349 int i = 0;
350 int j = 0;
351
352 fprintf(stderr, "-- %s --\n", __func__);
353 for (i = 0; i < ARRAY_SIZE(result_from_answer_data); i++) {
354 struct result_from_answer_test *t = &result_from_answer_data[i];
355 struct osmo_mdns_msg_answer ans = {0};
356 struct osmo_mslookup_result res = {0};
357 void *ctx_test = talloc_named_const(ctx, 0, t->desc);
358 bool is_error;
359
360 fprintf(stderr, "---\n");
361 fprintf(stderr, "test: %s\n", t->desc);
362 fprintf(stderr, "error: %s\n", t->error ? "true" : "false");
363 fprintf(stderr, "records:\n");
364 /* Build records list */
365 INIT_LLIST_HEAD(&ans.records);
366 for (j = 0; j < ARRAY_SIZE(t->records); j++) {
367 struct osmo_mdns_record *rec = NULL;
368
369 switch (t->records[j]) {
370 case RECORD_NONE:
371 break;
372 case RECORD_A:
373 fprintf(stderr, "- A 42.42.42.42\n");
374 rec = talloc_zero(ctx_test, struct osmo_mdns_record);
375 rec->type = OSMO_MDNS_RFC_RECORD_TYPE_A;
376 rec->data = ip_v4_n;
377 rec->length = sizeof(ip_v4_n);
378 break;
379 case RECORD_AAAA:
380 fprintf(stderr, "- AAAA 1122:3344:5566:7788:99aa:bbcc:ddee:ff00\n");
381 rec = talloc_zero(ctx_test, struct osmo_mdns_record);
382 rec->type = OSMO_MDNS_RFC_RECORD_TYPE_AAAA;
383 rec->data = ip_v6_n;
384 rec->length = sizeof(ip_v6_n);
385 break;
386 case RECORD_TXT_AGE:
387 fprintf(stderr, "- TXT age=3\n");
388 rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "age", "3");
389 break;
390 case RECORD_TXT_PORT_444:
391 fprintf(stderr, "- TXT port=444\n");
392 rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "port", "444");
393 break;
394 case RECORD_TXT_PORT_666:
395 fprintf(stderr, "- TXT port=666\n");
396 rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "port", "666");
397 break;
398 case RECORD_TXT_INVALID_KEY:
399 fprintf(stderr, "- TXT hello=world\n");
400 rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "hello", "world");
401 break;
402 case RECORD_TXT_INVALID_NO_KEY_VALUE:
403 fprintf(stderr, "- TXT 12345\n");
404 rec = osmo_mdns_record_txt_keyval_encode(ctx_test, "12", "45");
405 rec->data[3] = '3';
406 break;
407 case RECORD_INVALID:
408 fprintf(stderr, "- (invalid)\n");
409 rec = talloc_zero(ctx, struct osmo_mdns_record);
410 rec->type = OSMO_MDNS_RFC_RECORD_TYPE_UNKNOWN;
411 break;
412 }
413
414 if (rec)
415 llist_add_tail(&rec->list, &ans.records);
416 }
417
418 /* Verify output */
419 is_error = (osmo_mdns_result_from_answer(&res, &ans) != 0);
420 if (t->error != is_error) {
421 fprintf(stderr, "got %s\n", is_error ? "error" : "no error");
422 OSMO_ASSERT(false);
423 }
424 if (!t->error) {
425 fprintf(stderr, "exp: %s\n", osmo_mslookup_result_name_c(print_ctx, NULL, &t->res));
426 fprintf(stderr, "res: %s\n", osmo_mslookup_result_name_c(print_ctx, NULL, &res));
427 OSMO_ASSERT(t->res.rc == res.rc);
428 OSMO_ASSERT(!osmo_sockaddr_str_cmp(&t->res.host_v4, &res.host_v4));
429 OSMO_ASSERT(!osmo_sockaddr_str_cmp(&t->res.host_v6, &res.host_v6));
430 OSMO_ASSERT(t->res.age == res.age);
431 OSMO_ASSERT(t->res.last == res.last);
432 }
433
434 talloc_free(ctx_test);
435 fprintf(stderr, "=> OK\n");
436 }
437}
438
Harald Welte7a476532022-11-03 11:38:41 +0100439int main(int argc, char **argv)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100440{
441 void *ctx = talloc_named_const(NULL, 0, "main");
442 osmo_init_logging2(ctx, NULL);
443
Pau Espin Pedrold6993ea2021-02-19 13:20:18 +0100444 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Oliver Smith3a9f2672019-11-20 10:56:35 +0100445 log_set_print_level(osmo_stderr_target, 1);
446 log_set_print_category(osmo_stderr_target, 1);
447 log_set_print_category_hex(osmo_stderr_target, 0);
448 log_set_use_color(osmo_stderr_target, 0);
449
Oliver Smith3a9f2672019-11-20 10:56:35 +0100450 test_enc_dec_rfc_header();
451 test_enc_dec_rfc_header_einval();
452 test_enc_dec_rfc_question(ctx);
453 test_enc_dec_rfc_question_null(ctx);
454 test_enc_dec_rfc_record(ctx);
455
456 test_result_from_answer(ctx);
457
458 return 0;
459}