1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <netinet/in.h>
9 #include <sys/socket.h>
11 #include <rte_string_fns.h>
13 #include <cmdline_parse.h>
14 #include <cmdline_parse_ipaddr.h>
16 #include "test_cmdline.h"
18 #define IP4(a,b,c,d) {((uint32_t)(((a) & 0xff)) | \
19 (((b) & 0xff) << 8) | \
20 (((c) & 0xff) << 16) | \
24 (((x & 0xFF) << 8) | ((x & 0xFF00) >> 8))
26 /* create IPv6 address, swapping bytes where needed */
28 # define s6_addr16 __u6_addr.__u6_addr16
30 #define IP6(a,b,c,d,e,f,g,h) .ipv6 = \
32 {U16_SWAP(a),U16_SWAP(b),U16_SWAP(c),U16_SWAP(d),\
33 U16_SWAP(e),U16_SWAP(f),U16_SWAP(g),U16_SWAP(h)}}
35 /** these are defined in netinet/in.h but not present in linux headers */
38 #define NIPQUAD_FMT "%u.%u.%u.%u"
39 #define NIPQUAD(addr) \
40 (unsigned)((unsigned char *)&addr)[0], \
41 (unsigned)((unsigned char *)&addr)[1], \
42 (unsigned)((unsigned char *)&addr)[2], \
43 (unsigned)((unsigned char *)&addr)[3]
45 #define NIP6_FMT "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x"
47 (unsigned)((addr).s6_addr[0]), \
48 (unsigned)((addr).s6_addr[1]), \
49 (unsigned)((addr).s6_addr[2]), \
50 (unsigned)((addr).s6_addr[3]), \
51 (unsigned)((addr).s6_addr[4]), \
52 (unsigned)((addr).s6_addr[5]), \
53 (unsigned)((addr).s6_addr[6]), \
54 (unsigned)((addr).s6_addr[7]), \
55 (unsigned)((addr).s6_addr[8]), \
56 (unsigned)((addr).s6_addr[9]), \
57 (unsigned)((addr).s6_addr[10]), \
58 (unsigned)((addr).s6_addr[11]), \
59 (unsigned)((addr).s6_addr[12]), \
60 (unsigned)((addr).s6_addr[13]), \
61 (unsigned)((addr).s6_addr[14]), \
62 (unsigned)((addr).s6_addr[15])
70 cmdline_ipaddr_t addr;
74 const struct ipaddr_str ipaddr_valid_strs[] = {
75 {"0.0.0.0", {AF_INET, {IP4(0,0,0,0)}, 0},
77 {"0.0.0.0/0", {AF_INET, {IP4(0,0,0,0)}, 0},
78 CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK},
79 {"0.0.0.0/24", {AF_INET, {IP4(0,0,0,0)}, 24},
80 CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK},
81 {"192.168.1.0/24", {AF_INET, {IP4(192,168,1,0)}, 24},
82 CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK},
83 {"34.56.78.90/1", {AF_INET, {IP4(34,56,78,90)}, 1},
84 CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK},
85 {"::", {AF_INET6, {IP6(0,0,0,0,0,0,0,0)}, 0},
87 {"::1", {AF_INET6, {IP6(0,0,0,0,0,0,0,1)}, 0},
89 {"::1/32", {AF_INET6, {IP6(0,0,0,0,0,0,0,1)}, 32},
90 CMDLINE_IPADDR_V6 | CMDLINE_IPADDR_NETWORK},
91 {"::/32", {AF_INET6, {IP6(0,0,0,0,0,0,0,0)}, 32},
92 CMDLINE_IPADDR_V6 | CMDLINE_IPADDR_NETWORK},
93 /* RFC5952 requests that only lowercase should be used */
94 {"1234:5678:90ab:cdef:4321:8765:BA09:FEDC", {AF_INET6,
95 {IP6(0x1234,0x5678,0x90AB,0xCDEF,0x4321,0x8765,0xBA09,0xFEDC)},
98 {"1234::1234/64", {AF_INET6,
99 {IP6(0x1234,0,0,0,0,0,0,0x1234)},
101 CMDLINE_IPADDR_V6 | CMDLINE_IPADDR_NETWORK},
102 {"1234::/64", {AF_INET6,
103 {IP6(0x1234,0,0,0,0,0,0,0)},
105 CMDLINE_IPADDR_V6 | CMDLINE_IPADDR_NETWORK},
106 {"1:1::1/32", {AF_INET6,
107 {IP6(1,1,0,0,0,0,0,1)},
109 CMDLINE_IPADDR_V6 | CMDLINE_IPADDR_NETWORK},
110 {"1:2:3:4::/64", {AF_INET6,
111 {IP6(1,2,3,4,0,0,0,0)},
113 CMDLINE_IPADDR_V6 | CMDLINE_IPADDR_NETWORK},
114 {"::ffff:192.168.1.0/64", {AF_INET6,
115 {IP6(0,0,0,0,0,0xFFFF,0xC0A8,0x100)},
117 CMDLINE_IPADDR_V6 | CMDLINE_IPADDR_NETWORK},
118 /* RFC5952 requests not using :: to skip one block of zeros*/
119 {"1::2:3:4:5:6:7", {AF_INET6,
120 {IP6(1,0,2,3,4,5,6,7)},
125 const char * ipaddr_garbage_addr4_strs[] = {
127 "192.168.1.0 garbage",
128 "192.168.1.0\0garbage",
129 "192.168.1.0#garbage",
130 "192.168.1.0\tgarbage",
131 "192.168.1.0\rgarbage",
132 "192.168.1.0\ngarbage",
134 #define IPv4_GARBAGE_ADDR IP4(192,168,1,0)
136 const char * ipaddr_garbage_addr6_strs[] = {
138 "1:2:3:4::8 garbage",
139 "1:2:3:4::8#garbage",
140 "1:2:3:4::8\0garbage",
141 "1:2:3:4::8\rgarbage",
142 "1:2:3:4::8\ngarbage",
143 "1:2:3:4::8\tgarbage",
145 #define IPv6_GARBAGE_ADDR {IP6(1,2,3,4,0,0,0,8)}
147 const char * ipaddr_garbage_network4_strs[] = {
149 "192.168.1.0/24 garbage",
150 "192.168.1.0/24\0garbage",
151 "192.168.1.0/24#garbage",
152 "192.168.1.0/24\tgarbage",
153 "192.168.1.0/24\rgarbage",
154 "192.168.1.0/24\ngarbage",
156 #define IPv4_GARBAGE_PREFIX 24
158 const char * ipaddr_garbage_network6_strs[] = {
160 "1:2:3:4::8/64 garbage",
161 "1:2:3:4::8/64#garbage",
162 "1:2:3:4::8/64\0garbage",
163 "1:2:3:4::8/64\rgarbage",
164 "1:2:3:4::8/64\ngarbage",
165 "1:2:3:4::8/64\tgarbage",
167 #define IPv6_GARBAGE_PREFIX 64
171 const char * ipaddr_invalid_strs[] = {
174 /* invalid numbers */
184 "256.123.123.123/24",
185 "255.256.123.123/24",
186 "255.255.256.123/24",
187 "255.255.255.256/24",
188 /* invalid network mask */
202 "123.123.123.123.123",
207 "123.123.123.123./24",
208 "123.123.123..123/24",
209 "123.123.123.123.123/24",
211 ".123.123.123.123/24",
212 /* invalid characters */
220 "123.123.123.123/4F",
227 "1:2:3:4:5:6:7:192.168.1.1",
228 "1234:192.168.1.1:ffff::",
229 "1:2:3:4:5:6:7:890ab",
230 "1:2:3:4:5:6:7890a:b",
231 "1:2:3:4:5:67890:a:b",
232 "1:2:3:4:56789:0:a:b",
233 "1:2:3:45678:9:0:a:b",
234 "1:2:34567:8:9:0:a:b",
235 "1:23456:7:8:9:0:a:b",
236 "12345:6:7:8:9:0:a:b",
246 "1::2:3:4:5:6:7:8/64",
248 /* invalid network mask */
249 "1:2:3:4:5:6:7:8/129",
250 "1:2:3:4:5:6:7:8/-1",
252 /* invalid characters */
258 "1234:1234:1234:1234:1234:1234:1234:1234:1234:1234:1234"
259 "random invalid text",
266 dump_addr(cmdline_ipaddr_t addr)
268 switch (addr.family) {
271 printf(NIPQUAD_FMT " prefixlen=%u\n",
272 NIPQUAD(addr.addr.ipv4.s_addr), addr.prefixlen);
277 printf(NIP6_FMT " prefixlen=%u\n",
278 NIP6(addr.addr.ipv6), addr.prefixlen);
282 printf("Can't dump: unknown address family.\n");
289 is_addr_different(cmdline_ipaddr_t addr1, cmdline_ipaddr_t addr2)
291 if (addr1.family != addr2.family)
294 if (addr1.prefixlen != addr2.prefixlen)
297 switch (addr1.family) {
300 if (memcmp(&addr1.addr.ipv4, &addr2.addr.ipv4,
301 sizeof(struct in_addr)) != 0)
307 if (memcmp(&addr1.addr.ipv6, &addr2.addr.ipv6,
308 sizeof(struct in6_addr)) != 0)
312 /* thing that should not be */
320 can_parse_addr(unsigned addr_flags, unsigned test_flags)
322 if ((test_flags & addr_flags) == addr_flags) {
323 /* if we are not trying to parse network addresses */
324 if (test_flags < CMDLINE_IPADDR_NETWORK)
326 /* if this is a network address */
327 else if (addr_flags & CMDLINE_IPADDR_NETWORK)
334 test_parse_ipaddr_valid(void)
336 cmdline_parse_token_ipaddr_t token;
337 char buf[CMDLINE_TEST_BUFSIZE];
338 cmdline_ipaddr_t result;
343 /* cover all cases in help */
344 for (flags = 0x1; flags < 0x8; flags++) {
345 token.ipaddr_data.flags = flags;
347 memset(buf, 0, sizeof(buf));
349 if (cmdline_get_help_ipaddr((cmdline_parse_token_hdr_t*)&token,
350 buf, sizeof(buf)) == -1) {
351 printf("Error: help rejected valid parameters!\n");
356 /* test valid strings */
357 for (i = 0; i < RTE_DIM(ipaddr_valid_strs); i++) {
359 /* test each valid string against different flags */
360 for (flags = 1; flags < 0x8; flags++) {
363 if (flags == CMDLINE_IPADDR_NETWORK)
366 /* clear out everything */
367 memset(buf, 0, sizeof(buf));
368 memset(&result, 0, sizeof(result));
369 memset(&token, 0, sizeof(token));
371 token.ipaddr_data.flags = flags;
373 cmdline_get_help_ipaddr((cmdline_parse_token_hdr_t*)&token,
376 ret = cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
377 ipaddr_valid_strs[i].str, (void*)&result,
380 /* if should have passed, or should have failed */
382 (can_parse_addr(ipaddr_valid_strs[i].flags, flags))) {
383 printf("Error: unexpected behavior when parsing %s as %s!\n",
384 ipaddr_valid_strs[i].str, buf);
385 printf("Parsed result: ");
387 printf("Expected result: ");
388 dump_addr(ipaddr_valid_strs[i].addr);
392 is_addr_different(result, ipaddr_valid_strs[i].addr)) {
393 printf("Error: result mismatch when parsing %s as %s!\n",
394 ipaddr_valid_strs[i].str, buf);
395 printf("Parsed result: ");
397 printf("Expected result: ");
398 dump_addr(ipaddr_valid_strs[i].addr);
404 /* test garbage ipv4 address strings */
405 for (i = 0; i < RTE_DIM(ipaddr_garbage_addr4_strs); i++) {
407 struct in_addr tmp = IPv4_GARBAGE_ADDR;
409 /* test each valid string against different flags */
410 for (flags = 1; flags < 0x8; flags++) {
413 if (flags == CMDLINE_IPADDR_NETWORK)
416 /* clear out everything */
417 memset(buf, 0, sizeof(buf));
418 memset(&result, 0, sizeof(result));
419 memset(&token, 0, sizeof(token));
421 token.ipaddr_data.flags = flags;
423 cmdline_get_help_ipaddr((cmdline_parse_token_hdr_t*)&token,
426 ret = cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
427 ipaddr_garbage_addr4_strs[i], (void*)&result,
430 /* if should have passed, or should have failed */
432 (can_parse_addr(CMDLINE_IPADDR_V4, flags))) {
433 printf("Error: unexpected behavior when parsing %s as %s!\n",
434 ipaddr_garbage_addr4_strs[i], buf);
438 memcmp(&result.addr.ipv4, &tmp, sizeof(tmp))) {
439 printf("Error: result mismatch when parsing %s as %s!\n",
440 ipaddr_garbage_addr4_strs[i], buf);
446 /* test garbage ipv6 address strings */
447 for (i = 0; i < RTE_DIM(ipaddr_garbage_addr6_strs); i++) {
449 cmdline_ipaddr_t tmp = {.addr = IPv6_GARBAGE_ADDR};
451 /* test each valid string against different flags */
452 for (flags = 1; flags < 0x8; flags++) {
455 if (flags == CMDLINE_IPADDR_NETWORK)
458 /* clear out everything */
459 memset(buf, 0, sizeof(buf));
460 memset(&result, 0, sizeof(result));
461 memset(&token, 0, sizeof(token));
463 token.ipaddr_data.flags = flags;
465 cmdline_get_help_ipaddr((cmdline_parse_token_hdr_t*)&token,
468 ret = cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
469 ipaddr_garbage_addr6_strs[i], (void*)&result,
472 /* if should have passed, or should have failed */
474 (can_parse_addr(CMDLINE_IPADDR_V6, flags))) {
475 printf("Error: unexpected behavior when parsing %s as %s!\n",
476 ipaddr_garbage_addr6_strs[i], buf);
480 memcmp(&result.addr.ipv6, &tmp.addr.ipv6, sizeof(struct in6_addr))) {
481 printf("Error: result mismatch when parsing %s as %s!\n",
482 ipaddr_garbage_addr6_strs[i], buf);
489 /* test garbage ipv4 network strings */
490 for (i = 0; i < RTE_DIM(ipaddr_garbage_network4_strs); i++) {
492 struct in_addr tmp = IPv4_GARBAGE_ADDR;
494 /* test each valid string against different flags */
495 for (flags = 1; flags < 0x8; flags++) {
498 if (flags == CMDLINE_IPADDR_NETWORK)
501 /* clear out everything */
502 memset(buf, 0, sizeof(buf));
503 memset(&result, 0, sizeof(result));
504 memset(&token, 0, sizeof(token));
506 token.ipaddr_data.flags = flags;
508 cmdline_get_help_ipaddr((cmdline_parse_token_hdr_t*)&token,
511 ret = cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
512 ipaddr_garbage_network4_strs[i], (void*)&result,
515 /* if should have passed, or should have failed */
517 (can_parse_addr(CMDLINE_IPADDR_V4 | CMDLINE_IPADDR_NETWORK, flags))) {
518 printf("Error: unexpected behavior when parsing %s as %s!\n",
519 ipaddr_garbage_network4_strs[i], buf);
523 memcmp(&result.addr.ipv4, &tmp, sizeof(tmp))) {
524 printf("Error: result mismatch when parsing %s as %s!\n",
525 ipaddr_garbage_network4_strs[i], buf);
531 /* test garbage ipv6 address strings */
532 for (i = 0; i < RTE_DIM(ipaddr_garbage_network6_strs); i++) {
534 cmdline_ipaddr_t tmp = {.addr = IPv6_GARBAGE_ADDR};
536 /* test each valid string against different flags */
537 for (flags = 1; flags < 0x8; flags++) {
540 if (flags == CMDLINE_IPADDR_NETWORK)
543 /* clear out everything */
544 memset(buf, 0, sizeof(buf));
545 memset(&result, 0, sizeof(result));
546 memset(&token, 0, sizeof(token));
548 token.ipaddr_data.flags = flags;
550 cmdline_get_help_ipaddr((cmdline_parse_token_hdr_t*)&token,
553 ret = cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
554 ipaddr_garbage_network6_strs[i], (void*)&result,
557 /* if should have passed, or should have failed */
559 (can_parse_addr(CMDLINE_IPADDR_V6 | CMDLINE_IPADDR_NETWORK, flags))) {
560 printf("Error: unexpected behavior when parsing %s as %s!\n",
561 ipaddr_garbage_network6_strs[i], buf);
565 memcmp(&result.addr.ipv6, &tmp.addr.ipv6, sizeof(struct in6_addr))) {
566 printf("Error: result mismatch when parsing %s as %s!\n",
567 ipaddr_garbage_network6_strs[i], buf);
577 test_parse_ipaddr_invalid_data(void)
579 cmdline_parse_token_ipaddr_t token;
580 char buf[CMDLINE_TEST_BUFSIZE];
581 cmdline_ipaddr_t result;
586 memset(&result, 0, sizeof(result));
588 /* test invalid strings */
589 for (i = 0; i < RTE_DIM(ipaddr_invalid_strs); i++) {
591 /* test each valid string against different flags */
592 for (flags = 1; flags < 0x8; flags++) {
595 if (flags == CMDLINE_IPADDR_NETWORK)
598 /* clear out everything */
599 memset(buf, 0, sizeof(buf));
600 memset(&token, 0, sizeof(token));
602 token.ipaddr_data.flags = flags;
604 cmdline_get_help_ipaddr((cmdline_parse_token_hdr_t*)&token,
607 ret = cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
608 ipaddr_invalid_strs[i], (void*)&result,
612 printf("Error: parsing %s as %s succeeded!\n",
613 ipaddr_invalid_strs[i], buf);
614 printf("Parsed result: ");
625 test_parse_ipaddr_invalid_param(void)
627 cmdline_parse_token_ipaddr_t token;
628 char buf[CMDLINE_TEST_BUFSIZE];
629 cmdline_ipaddr_t result;
631 snprintf(buf, sizeof(buf), "1.2.3.4");
632 token.ipaddr_data.flags = CMDLINE_IPADDR_V4;
635 if (cmdline_parse_ipaddr(NULL, buf, (void*)&result,
636 sizeof(result)) != -1) {
637 printf("Error: parser accepted invalid parameters!\n");
641 if (cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
642 NULL, (void*)&result, sizeof(result)) != -1) {
643 printf("Error: parser accepted invalid parameters!\n");
647 if (cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
648 "", (void*)&result, sizeof(result)) != -1) {
649 printf("Error: parser accepted invalid parameters!\n");
653 if (cmdline_parse_ipaddr((cmdline_parse_token_hdr_t*)&token,
654 buf, NULL, 0) == -1) {
655 printf("Error: parser rejected null result!\n");
660 if (cmdline_get_help_ipaddr(NULL, buf, 0) != -1) {
661 printf("Error: help accepted invalid parameters!\n");
665 if (cmdline_get_help_ipaddr((cmdline_parse_token_hdr_t*)&token,
667 printf("Error: help accepted invalid parameters!\n");