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