Home | History | Annotate | Line # | Download | only in libntp
decodenetnum.c revision 1.3
      1 /*	$NetBSD: decodenetnum.c,v 1.3 2022/10/09 21:41:04 christos Exp $	*/
      2 
      3 #include "config.h"
      4 #include "ntp_stdlib.h"
      5 #include "sockaddrtest.h"
      6 
      7 #include "unity.h"
      8 
      9 void setUp(void);
     10 extern void test_IPv4AddressOnly(void);
     11 extern void test_IPv4AddressWithPort(void);
     12 extern void test_IPv6AddressOnly(void);
     13 extern void test_IPv6AddressWithPort(void);
     14 extern void test_IPv6AddressWithScope(void);
     15 extern void test_IPv6AddressWithPortAndScope(void);
     16 extern void test_IllegalAddress(void);
     17 extern void test_IllegalCharInPort(void);
     18 extern void test_NameBufOverflow(void);
     19 
     20 /*
     21  * NOTE: The IPv6 specific tests are reduced to stubs when IPv6 is
     22  * disabled.
     23  *
     24  * ISC_PLATFORM_HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6
     25  * ISC_PLATFORM_WANTIPV6 can be changed with build --disable-ipv6.
     26  *
     27  * If we want IPv6 but don't have it, the tests should fail, I think.
     28  */
     29 void
     30 setUp(void)
     31 {
     32 	init_lib();
     33 }
     34 
     35 
     36 void
     37 test_IPv4AddressOnly(void)
     38 {
     39 	const char *str = "192.0.2.1";
     40 	sockaddr_u actual;
     41 
     42 	sockaddr_u expected;
     43 	memset(&expected, 0, sizeof(expected));
     44 	expected.sa4.sin_family = AF_INET;
     45 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
     46 	SET_PORT(&expected, NTP_PORT);
     47 
     48 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
     49 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
     50 }
     51 
     52 void
     53 test_IPv4AddressWithPort(void)
     54 {
     55 	const char *str = "192.0.2.2:2000";
     56 	sockaddr_u actual;
     57 
     58 	sockaddr_u expected;
     59 	memset(&expected, 0, sizeof(expected));
     60 	expected.sa4.sin_family = AF_INET;
     61 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
     62 	SET_PORT(&expected, 2000);
     63 
     64 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
     65 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
     66 }
     67 
     68 
     69 void
     70 test_IPv6AddressOnly(void)
     71 {
     72 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
     73 
     74 	const struct in6_addr address = {
     75 		0x20, 0x01, 0x0d, 0xb8,
     76 		0x85, 0xa3, 0x08, 0xd3,
     77 		0x13, 0x19, 0x8a, 0x2e,
     78 		0x03, 0x70, 0x73, 0x34
     79 	};
     80 
     81 	const char *str1 = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
     82 	const char *str2 = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]";
     83 	sockaddr_u actual;
     84 
     85 	sockaddr_u expected;
     86 	memset(&expected, 0, sizeof(expected));
     87 	expected.sa6.sin6_family = AF_INET6;
     88 	expected.sa6.sin6_addr = address;
     89 	SET_PORT(&expected, NTP_PORT);
     90 
     91 	TEST_ASSERT_TRUE(decodenetnum(str1, &actual));
     92 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
     93 
     94 	TEST_ASSERT_TRUE(decodenetnum(str2, &actual));
     95 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
     96 
     97 #else
     98 
     99 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
    100 
    101 #endif
    102 }
    103 
    104 
    105 void
    106 test_IPv6AddressWithPort(void)
    107 {
    108 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
    109 
    110 	const struct in6_addr address = {
    111 		0x20, 0x01, 0x0d, 0xb8,
    112 		0x85, 0xa3, 0x08, 0xd3,
    113 		0x13, 0x19, 0x8a, 0x2e,
    114 		0x03, 0x70, 0x73, 0x34
    115 	};
    116 
    117 	const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
    118 	sockaddr_u actual;
    119 
    120 	sockaddr_u expected;
    121 	memset(&expected, 0, sizeof(expected));
    122 	expected.sa6.sin6_family = AF_INET6;
    123 	expected.sa6.sin6_addr = address;
    124 	SET_PORT(&expected, 3000);
    125 
    126 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
    127 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
    128 
    129 #else
    130 
    131 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
    132 
    133 #endif
    134 }
    135 
    136 void test_IPv6AddressWithScope(void)
    137 {
    138 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
    139 
    140 	const struct in6_addr address = {
    141 		0x20, 0x01, 0x0d, 0xb8,
    142 		0x85, 0xa3, 0x08, 0xd3,
    143 		0x13, 0x19, 0x8a, 0x2e,
    144 		0x03, 0x70, 0x73, 0x34
    145 	};
    146 
    147 	const char *str1 = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334%42";
    148 	const char *str2 = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334%42]";
    149 	sockaddr_u actual;
    150 
    151 	sockaddr_u expected;
    152 	memset(&expected, 0, sizeof(expected));
    153 	expected.sa6.sin6_family = AF_INET6;
    154 	expected.sa6.sin6_addr = address;
    155 	expected.sa6.sin6_scope_id = 42;
    156 	SET_PORT(&expected, NTP_PORT);
    157 
    158 	TEST_ASSERT_TRUE(decodenetnum(str1, &actual));
    159 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
    160 
    161 	TEST_ASSERT_TRUE(decodenetnum(str2, &actual));
    162 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
    163 
    164 #else
    165 
    166 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
    167 
    168 #endif
    169 }
    170 
    171 void test_IPv6AddressWithPortAndScope(void)
    172 {
    173 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
    174 
    175 	const struct in6_addr address = {
    176 		0x20, 0x01, 0x0d, 0xb8,
    177 		0x85, 0xa3, 0x08, 0xd3,
    178 		0x13, 0x19, 0x8a, 0x2e,
    179 		0x03, 0x70, 0x73, 0x34
    180 	};
    181 
    182 	const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334%42]:3000";
    183 	sockaddr_u actual;
    184 
    185 	sockaddr_u expected;
    186 	memset(&expected, 0, sizeof(expected));
    187 	expected.sa6.sin6_family = AF_INET6;
    188 	expected.sa6.sin6_addr = address;
    189 	expected.sa6.sin6_scope_id = 42;
    190 	SET_PORT(&expected, 3000);
    191 
    192 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
    193 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
    194 
    195 #else
    196 
    197 	TEST_IGNORE_MESSAGE("IPV6 disabled in build");
    198 
    199 #endif
    200 }
    201 
    202 void
    203 test_IllegalAddress(void)
    204 {
    205 	const char *str = "192.0.2.270:2000";
    206 	sockaddr_u actual;
    207 
    208 	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
    209 }
    210 
    211 
    212 void
    213 test_IllegalCharInPort(void)
    214 {
    215 	/* An illegal port does not make the decodenetnum fail, but instead
    216 	 * makes it use the standard port.
    217 	 */
    218 	const char *str = "192.0.2.1:a700";
    219 	sockaddr_u actual;
    220 
    221 	sockaddr_u expected;
    222 	memset(&expected, 0, sizeof(expected));
    223 	expected.sa4.sin_family = AF_INET;
    224 	expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
    225 	SET_PORT(&expected, NTP_PORT);
    226 
    227 	TEST_ASSERT_TRUE(decodenetnum(str, &actual));
    228 	TEST_ASSERT_TRUE(IsEqual(expected, actual));
    229 }
    230 
    231 void
    232 test_NameBufOverflow(void)
    233 {
    234 	const char *str =
    235 	    "loremipsumloremipsumloremipsumloremipsumloremipsum"
    236 	    "loremipsumloremipsumloremipsumloremipsum";
    237 
    238 	sockaddr_u actual;
    239 	TEST_ASSERT_FALSE(decodenetnum(str, &actual));
    240 }
    241