blob: eec13ca362cb7f6bcc4bf53a615cc08ce5abf972 [file] [log] [blame]
Holger Hans Peter Freytherb79a1482014-01-02 13:55:00 +01001/* tests for utilities of libmsomcore */
2/*
3 * (C) 2014 Holger Hans Peter Freyther
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +020023#include <osmocom/gsm/ipa.h>
24
25#include <osmocom/core/logging.h>
Holger Hans Peter Freytherb79a1482014-01-02 13:55:00 +010026#include <osmocom/core/utils.h>
Neels Hofmeyr59f4caf2018-07-19 22:13:19 +020027#include <osmocom/core/socket.h>
Holger Hans Peter Freytherb79a1482014-01-02 13:55:00 +010028
29#include <stdio.h>
Harald Welte504caac2017-10-27 17:19:59 +020030#include <ctype.h>
Harald Welte15a5f8d2018-06-06 16:58:17 +020031#include <time.h>
Neels Hofmeyr59f4caf2018-07-19 22:13:19 +020032#include <netinet/in.h>
33#include <arpa/inet.h>
Holger Hans Peter Freytherb79a1482014-01-02 13:55:00 +010034
35static void hexdump_test(void)
36{
37 uint8_t data[4098];
38 int i;
39
40 for (i = 0; i < ARRAY_SIZE(data); ++i)
41 data[i] = i & 0xff;
42
43 printf("Plain dump\n");
44 printf("%s\n", osmo_hexdump(data, 4));
45
46 printf("Corner case\n");
47 printf("%s\n", osmo_hexdump(data, ARRAY_SIZE(data)));
48 printf("%s\n", osmo_hexdump_nospc(data, ARRAY_SIZE(data)));
49}
50
Neels Hofmeyr7adb5672017-02-14 15:48:19 +010051static void hexparse_test(void)
52{
53 int i;
54 int rc;
55 uint8_t data[256];
56
57 printf("\nHexparse 0..255 in lower case\n");
58 memset(data, 0, sizeof(data));
59 rc = osmo_hexparse(
60 "000102030405060708090a0b0c0d0e0f"
61 "101112131415161718191a1b1c1d1e1f"
62 "202122232425262728292a2b2c2d2e2f"
63 "303132333435363738393a3b3c3d3e3f"
64 "404142434445464748494a4b4c4d4e4f"
65 "505152535455565758595a5b5c5d5e5f"
66 "606162636465666768696a6b6c6d6e6f"
67 "707172737475767778797a7b7c7d7e7f"
68 "808182838485868788898a8b8c8d8e8f"
69 "909192939495969798999a9b9c9d9e9f"
70 "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
71 "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
72 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
73 "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
74 "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
75 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"
76 , data, sizeof(data));
77 printf("rc = %d\n", rc);
78 printf("--> %s\n\n", osmo_hexdump(data, sizeof(data)));
79 for (i = 0; i < sizeof(data); i++)
80 OSMO_ASSERT(data[i] == i);
81
82 printf("Hexparse 0..255 in upper case\n");
83 memset(data, 0, sizeof(data));
84 rc = osmo_hexparse(
85 "000102030405060708090A0B0C0D0E0F"
86 "101112131415161718191A1B1C1D1E1F"
87 "202122232425262728292A2B2C2D2E2F"
88 "303132333435363738393A3B3C3D3E3F"
89 "404142434445464748494A4B4C4D4E4F"
90 "505152535455565758595A5B5C5D5E5F"
91 "606162636465666768696A6B6C6D6E6F"
92 "707172737475767778797A7B7C7D7E7F"
93 "808182838485868788898A8B8C8D8E8F"
94 "909192939495969798999A9B9C9D9E9F"
95 "A0A1A2A3A4A5A6A7A8A9AAABACADAEAF"
96 "B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF"
97 "C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF"
98 "D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF"
99 "E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
100 "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"
101 , data, sizeof(data));
102 printf("rc = %d\n", rc);
103 printf("--> %s\n\n", osmo_hexdump(data, sizeof(data)));
104 for (i = 0; i < sizeof(data); i++)
105 OSMO_ASSERT(data[i] == i);
106
107 printf("Hexparse 0..255 in mixed case\n");
108 memset(data, 0, sizeof(data));
109 rc = osmo_hexparse(
110 "000102030405060708090A0B0C0D0E0F"
111 "101112131415161718191A1B1C1D1E1F"
112 "202122232425262728292A2B2C2D2E2F"
113 "303132333435363738393a3b3c3d3e3f"
114 "404142434445464748494A4B4C4D4E4F"
115 "505152535455565758595a5b5c5d5e5f"
116 "606162636465666768696A6B6C6D6E6F"
117 "707172737475767778797A7B7C7D7E7F"
118 "808182838485868788898A8B8C8D8E8F"
119 "909192939495969798999A9B9C9D9E9F"
120 "A0A1A2A3a4a5a6a7a8a9AAABACADAEAF"
121 "B0B1B2B3b4b5b6b7b8b9BABBBCBDBEBF"
122 "C0C1C2C3c4c5c6c7c8c9CACBCCCDCECF"
123 "D0D1D2D3d4d5d6d7d8d9DADBDCDDDEDF"
124 "E0E1E2E3e4e5e6e7e8e9EAEBECEDEEEF"
125 "F0F1F2F3f4f5f6f7f8f9FAFBFCFDFEFF"
126 , data, sizeof(data));
127 printf("rc = %d\n", rc);
128 printf("--> %s\n\n", osmo_hexdump(data, sizeof(data)));
129 for (i = 0; i < sizeof(data); i++)
130 OSMO_ASSERT(data[i] == i);
131
Neels Hofmeyr437ed4a2017-02-14 15:54:31 +0100132 printf("Hexparse 0..255 with whitespace\n");
133 memset(data, 0, sizeof(data));
134 rc = osmo_hexparse(
135 "00 01\t02\r030405060708090A0B0C0D0 E 0 F\n"
136 "10 11\t12\r131415161718191A1B1C1D1 E 1 F\n"
137 "20 21\t22\r232425262728292A2B2C2D2 E 2 F\n"
138 "30 31\t32\r333435363738393a3b3c3d3 e 3 f\n"
139 "40 41\t42\r434445464748494A4B4C4D4 E 4 F\n"
140 "50 51\t52\r535455565758595a5b5c5d5 e 5 f\n"
141 "60 61\t62\r636465666768696A6B6C6D6 E 6 F\n"
142 "70 71\t72\r737475767778797A7B7C7D7 E 7 F\n"
143 "80 81\t82\r838485868788898A8B8C8D8 E 8 F\n"
144 "90 91\t92\r939495969798999A9B9C9D9 E 9 F\n"
145 "A0 A1\tA2\rA3a4a5a6a7a8a9AAABACADA E A F\n"
146 "B0 B1\tB2\rB3b4b5b6b7b8b9BABBBCBDB E B F\n"
147 "C0 C1\tC2\rC3c4c5c6c7c8c9CACBCCCDC E C F \n"
148 "D0 D1\tD2\rD3d4d5d6d7d8d9DADBDCDDD E D F\t\n"
149 "E0 E1\tE2\rE3e4e5e6e7e8e9EAEBECEDE E E F \t\n"
150 "F0 F1\tF2\rF3f4f5f6f7f8f9FAFBFCFDF E F F \t\r\n"
151 , data, sizeof(data));
152 printf("rc = %d\n", rc);
153 printf("--> %s\n\n", osmo_hexdump(data, sizeof(data)));
154 for (i = 0; i < sizeof(data); i++)
155 OSMO_ASSERT(data[i] == i);
156
Neels Hofmeyr7adb5672017-02-14 15:48:19 +0100157 printf("Hexparse with buffer too short\n");
158 memset(data, 0, sizeof(data));
159 rc = osmo_hexparse("000102030405060708090a0b0c0d0e0f", data, 15);
160 printf("rc = %d\n", rc);
161
162 printf("Hexparse with uneven amount of digits\n");
163 memset(data, 0, sizeof(data));
164 rc = osmo_hexparse("000102030405060708090a0b0c0d0e0", data, 16);
165 printf("rc = %d\n", rc);
166
167 printf("Hexparse with invalid char\n");
168 memset(data, 0, sizeof(data));
169 rc = osmo_hexparse("0001020304050X0708090a0b0c0d0e0f", data, 16);
170 printf("rc = %d\n", rc);
171}
172
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200173static void test_idtag_parsing(void)
174{
175 struct tlv_parsed tvp;
176 int rc;
177
Harald Welte48fd0192018-07-31 20:19:49 +0200178 /* IPA CCM IDENTITY REQUEST message: 8bit length followed by respective value */
179 static uint8_t id_get_data[] = {
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200180 0x01, 0x08,
181 0x01, 0x07,
182 0x01, 0x02,
183 0x01, 0x03,
184 0x01, 0x04,
185 0x01, 0x05,
186 0x01, 0x01,
187 0x01, 0x00,
188 0x11, 0x23, 0x4e, 0x6a, 0x28, 0xd2, 0xa2, 0x53, 0x3a, 0x2a, 0x82, 0xa7, 0x7a, 0xef, 0x29, 0xd4, 0x44, 0x30,
189 0x11, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
190 };
191
Harald Welte48fd0192018-07-31 20:19:49 +0200192 rc = ipa_ccm_idtag_parse_off(&tvp, id_get_data, sizeof(id_get_data), 1);
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200193 OSMO_ASSERT(rc == 0);
194
195 OSMO_ASSERT(TLVP_PRESENT(&tvp, 8));
196 OSMO_ASSERT(TLVP_LEN(&tvp, 8) == 0);
197
198 OSMO_ASSERT(TLVP_PRESENT(&tvp, 7));
199 OSMO_ASSERT(TLVP_LEN(&tvp, 7) == 0);
200
201 OSMO_ASSERT(TLVP_PRESENT(&tvp, 2));
202 OSMO_ASSERT(TLVP_LEN(&tvp, 2) == 0);
203
204 OSMO_ASSERT(TLVP_PRESENT(&tvp, 3));
205 OSMO_ASSERT(TLVP_LEN(&tvp, 3) == 0);
206
207 OSMO_ASSERT(TLVP_PRESENT(&tvp, 4));
208 OSMO_ASSERT(TLVP_LEN(&tvp, 4) == 0);
209
210 OSMO_ASSERT(TLVP_PRESENT(&tvp, 5));
211 OSMO_ASSERT(TLVP_LEN(&tvp, 5) == 0);
212
213 OSMO_ASSERT(TLVP_PRESENT(&tvp, 1));
214 OSMO_ASSERT(TLVP_LEN(&tvp, 1) == 0);
215
216 OSMO_ASSERT(TLVP_PRESENT(&tvp, 0));
217 OSMO_ASSERT(TLVP_LEN(&tvp, 0) == 0);
218
219 OSMO_ASSERT(TLVP_PRESENT(&tvp, 0x23));
220 OSMO_ASSERT(TLVP_LEN(&tvp, 0x23) == 16);
221
222 OSMO_ASSERT(TLVP_PRESENT(&tvp, 0x24));
223 OSMO_ASSERT(TLVP_LEN(&tvp, 0x24) == 16);
224
225 OSMO_ASSERT(!TLVP_PRESENT(&tvp, 0x25));
226}
227
Neels Hofmeyr4335bad2017-10-07 04:39:14 +0200228static struct {
229 const char *str;
230 int min_digits;
231 int max_digits;
232 bool require_even;
233 bool expect_ok;
234} test_hexstrs[] = {
235 { NULL, 0, 10, false, true },
236 { NULL, 1, 10, false, false },
237 { "", 0, 10, false, true },
238 { "", 1, 10, false, false },
239 { " ", 0, 10, false, false },
240 { "1", 0, 10, false, true },
241 { "1", 1, 10, false, true },
242 { "1", 1, 10, true, false },
243 { "1", 2, 10, false, false },
244 { "123", 1, 10, false, true },
245 { "123", 1, 10, true, false },
246 { "123", 4, 10, false, false },
247 { "1234", 4, 10, true, true },
248 { "12345", 4, 10, true, false },
249 { "123456", 4, 10, true, true },
250 { "1234567", 4, 10, true, false },
251 { "12345678", 4, 10, true, true },
252 { "123456789", 4, 10, true, false },
253 { "123456789a", 4, 10, true, true },
254 { "123456789ab", 4, 10, true, false },
255 { "123456789abc", 4, 10, true, false },
256 { "123456789ab", 4, 10, false, false },
257 { "123456789abc", 4, 10, false, false },
258 { "0123456789abcdefABCDEF", 0, 100, false, true },
259 { "0123456789 abcdef ABCDEF", 0, 100, false, false },
260 { "foobar", 0, 100, false, false },
261 { "BeadedBeeAced1EbbedDefacedFacade", 32, 32, true, true },
262 { "C01ffedC1cadaeAc1d1f1edAcac1aB0a", 32, 32, false, true },
263 { "DeafBeddedBabeAcceededFadedDecaff", 32, 32, false, false },
264};
265
266bool test_is_hexstr()
267{
268 int i;
269 bool pass = true;
270 bool ok = true;
271 printf("\n----- %s\n", __func__);
272
273 for (i = 0; i < ARRAY_SIZE(test_hexstrs); i++) {
274 ok = osmo_is_hexstr(test_hexstrs[i].str,
275 test_hexstrs[i].min_digits,
276 test_hexstrs[i].max_digits,
277 test_hexstrs[i].require_even);
278 pass = pass && (ok == test_hexstrs[i].expect_ok);
279 printf("%2d: %s str='%s' min=%d max=%d even=%d expect=%s\n",
280 i, test_hexstrs[i].expect_ok == ok ? "pass" : "FAIL",
281 test_hexstrs[i].str,
282 test_hexstrs[i].min_digits,
283 test_hexstrs[i].max_digits,
284 test_hexstrs[i].require_even,
285 test_hexstrs[i].expect_ok ? "valid" : "invalid");
286 }
287 return pass;
288}
289
Harald Welte504caac2017-10-27 17:19:59 +0200290struct bcdcheck {
291 uint8_t bcd;
292 char ch;
293};
294
295static const struct bcdcheck bcdchecks[] = {
296 { 0, '0' },
297 { 1, '1' },
298 { 2, '2' },
299 { 3, '3' },
300 { 4, '4' },
301 { 5, '5' },
302 { 6, '6' },
303 { 7, '7' },
304 { 8, '8' },
305 { 9, '9' },
306 { 0xA, 'A' },
307 { 0xB, 'B' },
308 { 0xC, 'C' },
309 { 0xD, 'D' },
310 { 0xE, 'E' },
311 { 0xF, 'F' },
312};
313
314static void bcd_test(void)
315{
316 int i;
317
318 printf("\nTesting BCD conversion\n");
319 for (i = 0; i < ARRAY_SIZE(bcdchecks); i++) {
320 const struct bcdcheck *check = &bcdchecks[i];
321 char ch = osmo_bcd2char(check->bcd);
322 printf("\tval=0x%x, expected=%c, found=%c\n", check->bcd, check->ch, ch);
323 OSMO_ASSERT(osmo_bcd2char(check->bcd) == check->ch);
324 /* test char -> bcd back-coversion */
325 OSMO_ASSERT(osmo_char2bcd(ch) == check->bcd);
326 /* test for lowercase hex char */
327 OSMO_ASSERT(osmo_char2bcd(tolower(ch)) == check->bcd);
328 }
329}
330
Neels Hofmeyr9910bbc2017-12-16 00:54:52 +0100331static void str_escape_test(void)
332{
333 int i;
334 int j;
335 uint8_t in_buf[32];
336 char out_buf[11];
337 const char *printable = "printable";
338 const char *res;
339
340 printf("\nTesting string escaping\n");
341 printf("- all chars from 0 to 255 in batches of 16:\n");
Pau Espin Pedrol6de34ee2018-02-01 12:49:39 +0100342 in_buf[16] = '\0';
Neels Hofmeyr9910bbc2017-12-16 00:54:52 +0100343 for (j = 0; j < 16; j++) {
344 for (i = 0; i < 16; i++)
345 in_buf[i] = (j << 4) | i;
346 printf("\"%s\"\n", osmo_escape_str((const char*)in_buf, 16));
347 }
348
349 printf("- nul terminated:\n");
350 printf("\"%s\"\n", osmo_escape_str("termi\nated", -1));
351
352 printf("- passthru:\n");
353 res = osmo_escape_str(printable, -1);
354 if (res != printable)
355 printf("NOT passed through! \"%s\"\n", res);
356 else
357 printf("passed through unchanged \"%s\"\n", res);
358
359 printf("- zero length:\n");
360 printf("\"%s\"\n", osmo_escape_str("omitted", 0));
361
362 printf("- truncation when too long:\n");
363 memset(in_buf, 'x', sizeof(in_buf));
364 in_buf[0] = '\a';
365 in_buf[7] = 'E';
366 memset(out_buf, 0x7f, sizeof(out_buf));
367 printf("\"%s\"\n", osmo_escape_str_buf((const char *)in_buf, sizeof(in_buf), out_buf, 10));
368 OSMO_ASSERT(out_buf[10] == 0x7f);
369
370 printf("- passthrough without truncation when no escaping needed:\n");
371 memset(in_buf, 'x', sizeof(in_buf));
372 in_buf[19] = 'E';
373 in_buf[20] = '\0';
374 memset(out_buf, 0x7f, sizeof(out_buf));
375 printf("\"%s\"\n", osmo_escape_str_buf((const char *)in_buf, -1, out_buf, 10));
376 OSMO_ASSERT(out_buf[0] == 0x7f);
377}
378
Neels Hofmeyr04eb56f2018-04-09 00:41:28 +0200379static void str_quote_test(void)
380{
381 int i;
382 int j;
383 uint8_t in_buf[32];
384 char out_buf[11];
385 const char *printable = "printable";
386 const char *res;
387
388 printf("\nTesting string quoting\n");
389 printf("- all chars from 0 to 255 in batches of 16:\n");
390 in_buf[16] = '\0';
391 for (j = 0; j < 16; j++) {
392 for (i = 0; i < 16; i++)
393 in_buf[i] = (j << 4) | i;
394 printf("'%s'\n", osmo_quote_str((const char*)in_buf, 16));
395 }
396
397 printf("- nul terminated:\n");
398 printf("'%s'\n", osmo_quote_str("termi\nated", -1));
399
400 printf("- never passthru:\n");
401 res = osmo_quote_str(printable, -1);
402 if (res != printable)
403 printf("NOT passed through. '%s'\n", res);
404 else
405 printf("passed through unchanged '%s'\n", res);
406
407 printf("- zero length:\n");
408 printf("'%s'\n", osmo_quote_str("omitted", 0));
409
410 printf("- truncation when too long:\n");
411 memset(in_buf, 'x', sizeof(in_buf));
412 in_buf[0] = '\a';
413 in_buf[5] = 'E';
414 memset(out_buf, 0x7f, sizeof(out_buf));
415 printf("'%s'\n", osmo_quote_str_buf((const char *)in_buf, sizeof(in_buf), out_buf, 10));
416 OSMO_ASSERT(out_buf[10] == 0x7f);
417
418 printf("- always truncation, even when no escaping needed:\n");
419 memset(in_buf, 'x', sizeof(in_buf));
420 in_buf[6] = 'E'; /* dst has 10, less 2 quotes and nul, leaves 7, i.e. in[6] is last */
421 in_buf[20] = '\0';
422 memset(out_buf, 0x7f, sizeof(out_buf));
423 printf("'%s'\n", osmo_quote_str_buf((const char *)in_buf, -1, out_buf, 10));
424 OSMO_ASSERT(out_buf[0] == '"');
425
426 printf("- try to feed too little buf for quoting:\n");
427 printf("'%s'\n", osmo_quote_str_buf("", -1, out_buf, 2));
428
429 printf("- NULL string becomes a \"NULL\" literal:\n");
430 printf("'%s'\n", osmo_quote_str_buf(NULL, -1, out_buf, 10));
431}
432
Harald Welte15a5f8d2018-06-06 16:58:17 +0200433static void isqrt_test(void)
434{
435 int i;
436
437 printf("\nTesting integer square-root\n");
438 srand(time(NULL));
439 for (i = 0; i < 1024; i++) {
440 uint16_t x;
441 uint32_t r = rand();
442 if (RAND_MAX < UINT16_MAX)
443 x = r * (UINT16_MAX/RAND_MAX);
444 else
445 x = r;
Neels Hofmeyr6979c542018-07-19 22:05:21 +0200446 uint32_t sq = (uint32_t)x*x;
Harald Welte15a5f8d2018-06-06 16:58:17 +0200447 uint32_t y = osmo_isqrt32(sq);
448 if (y != x)
449 printf("ERROR: x=%u, sq=%u, osmo_isqrt(%u) = %u\n", x, sq, sq, y);
450 }
451}
452
Neels Hofmeyr59f4caf2018-07-19 22:13:19 +0200453
454struct osmo_sockaddr_to_str_and_uint_test_case {
455 uint16_t port;
456 bool omit_port;
457 const char *addr;
458 unsigned int addr_len;
459 bool omit_addr;
460 unsigned int expect_rc;
461 const char *expect_returned_addr;
462};
463
464struct osmo_sockaddr_to_str_and_uint_test_case osmo_sockaddr_to_str_and_uint_test_data[] = {
465 {
466 .port = 0,
467 .addr = "0.0.0.0",
468 .addr_len = 20,
469 .expect_rc = 7,
470 },
471 {
472 .port = 65535,
473 .addr = "255.255.255.255",
474 .addr_len = 20,
475 .expect_rc = 15,
476 },
477 {
478 .port = 1234,
479 .addr = "234.23.42.123",
480 .addr_len = 20,
481 .expect_rc = 13,
482 },
483 {
484 .port = 1234,
485 .addr = "234.23.42.123",
486 .addr_len = 10,
487 .expect_rc = 13,
488 .expect_returned_addr = "234.23.42",
489 },
490 {
491 .port = 1234,
492 .omit_port = true,
493 .addr = "234.23.42.123",
494 .addr_len = 20,
495 .expect_rc = 13,
496 },
497 {
498 .port = 1234,
499 .addr = "234.23.42.123",
500 .omit_addr = true,
501 .expect_rc = 0,
502 .expect_returned_addr = "",
503 },
504 {
505 .port = 1234,
506 .addr = "234.23.42.123",
507 .addr_len = 0,
508 .expect_rc = 13,
509 .expect_returned_addr = "",
510 },
511 {
512 .port = 1234,
513 .addr = "234.23.42.123",
514 .omit_port = true,
515 .omit_addr = true,
516 .expect_rc = 0,
517 .expect_returned_addr = "",
518 },
519};
520
521static void osmo_sockaddr_to_str_and_uint_test(void)
522{
523 int i;
524 printf("\n%s\n", __func__);
525
526 for (i = 0; i < ARRAY_SIZE(osmo_sockaddr_to_str_and_uint_test_data); i++) {
527 struct osmo_sockaddr_to_str_and_uint_test_case *t =
528 &osmo_sockaddr_to_str_and_uint_test_data[i];
529
530 struct sockaddr_in sin = {
531 .sin_family = AF_INET,
532 .sin_port = htons(t->port),
533 };
534 inet_aton(t->addr, &sin.sin_addr);
535
536 char addr[20] = {};
537 uint16_t port = 0;
538 unsigned int rc;
539
540 rc = osmo_sockaddr_to_str_and_uint(
541 t->omit_addr? NULL : addr, t->addr_len,
542 t->omit_port? NULL : &port,
543 (const struct sockaddr*)&sin);
544
545 printf("[%d] %s:%u%s%s addr_len=%u --> %s:%u rc=%u\n",
546 i,
547 t->addr ? : "-",
548 t->port,
549 t->omit_addr ? " (omit addr)" : "",
550 t->omit_port ? " (omit port)" : "",
551 t->addr_len,
552 addr, port, rc);
553 if (rc != t->expect_rc)
554 printf("ERROR: Expected rc = %u\n", t->expect_rc);
555 if (!t->expect_returned_addr)
556 t->expect_returned_addr = t->addr;
557 if (strcmp(t->expect_returned_addr, addr))
558 printf("ERROR: Expected addr = '%s'\n", t->expect_returned_addr);
559 if (!t->omit_port && port != t->port)
560 printf("ERROR: Expected port = %u\n", t->port);
561 }
562}
563
Holger Hans Peter Freytherb79a1482014-01-02 13:55:00 +0100564int main(int argc, char **argv)
565{
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200566 static const struct log_info log_info = {};
567 log_init(&log_info, NULL);
568
Holger Hans Peter Freytherb79a1482014-01-02 13:55:00 +0100569 hexdump_test();
Neels Hofmeyr7adb5672017-02-14 15:48:19 +0100570 hexparse_test();
Holger Hans Peter Freytherf558ed42015-06-02 15:52:06 +0200571 test_idtag_parsing();
Neels Hofmeyr4335bad2017-10-07 04:39:14 +0200572 test_is_hexstr();
Harald Welte504caac2017-10-27 17:19:59 +0200573 bcd_test();
Neels Hofmeyr9910bbc2017-12-16 00:54:52 +0100574 str_escape_test();
Neels Hofmeyr04eb56f2018-04-09 00:41:28 +0200575 str_quote_test();
Harald Welte15a5f8d2018-06-06 16:58:17 +0200576 isqrt_test();
Neels Hofmeyr59f4caf2018-07-19 22:13:19 +0200577 osmo_sockaddr_to_str_and_uint_test();
Holger Hans Peter Freytherb79a1482014-01-02 13:55:00 +0100578 return 0;
579}