Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | #include <time.h> |
| 6 | |
| 7 | #include <netinet/in.h> |
| 8 | #include <sys/socket.h> |
| 9 | |
| 10 | #include <osmocom/core/utils.h> |
| 11 | #include <osmocom/core/application.h> |
| 12 | #include <osmocom/core/logging.h> |
Pau Espin Pedrol | 042a445 | 2018-04-17 14:31:42 +0200 | [diff] [blame] | 13 | #include <osmocom/core/msgb.h> |
Neels Hofmeyr | 1d85bea | 2017-11-18 18:29:49 +0100 | [diff] [blame] | 14 | #include <osmocom/core/bits.h> |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 15 | |
| 16 | #include "../../lib/in46_addr.h" |
| 17 | #include "../../lib/syserr.h" |
| 18 | |
| 19 | static const struct in46_addr g_ia4 = { |
| 20 | .len = 4, |
| 21 | .v4.s_addr = 0x0d0c0b0a, |
| 22 | }; |
| 23 | |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 24 | static void test_in46a_to_af(void) |
| 25 | { |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 26 | printf("Testing in46a_to_af() with IPv4 addresses\n"); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 27 | |
| 28 | OSMO_ASSERT(in46a_to_af(&g_ia4) == AF_INET); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | static void test_in46a_to_sas(void) |
| 32 | { |
| 33 | struct sockaddr_storage ss; |
| 34 | struct sockaddr_in *sin = (struct sockaddr_in *) &ss; |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 35 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 36 | printf("Testing in46a_to_sas() with IPv4 addresses\n"); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 37 | |
| 38 | memset(&ss, 0, sizeof(ss)); |
| 39 | OSMO_ASSERT(in46a_to_sas(&ss, &g_ia4) == 0); |
| 40 | OSMO_ASSERT(sin->sin_family == AF_INET); |
| 41 | OSMO_ASSERT(sin->sin_addr.s_addr == g_ia4.v4.s_addr); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void test_in46a_ntop(void) |
| 45 | { |
| 46 | struct in46_addr ia; |
| 47 | char buf[256]; |
| 48 | const char *res; |
| 49 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 50 | printf("Testing in46a_ntop() with IPv4 addresses\n"); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 51 | |
| 52 | res = in46a_ntop(NULL, buf, sizeof(buf)); |
| 53 | OSMO_ASSERT(res && !strcmp(res, "UNDEFINED")); |
| 54 | printf("res = %s\n", res); |
| 55 | |
| 56 | ia.len = 0; |
| 57 | res = in46a_ntop(&ia, buf, sizeof(buf)); |
| 58 | printf("res = %s\n", res); |
| 59 | OSMO_ASSERT(res && !strcmp(res, "UNDEFINED")); |
| 60 | |
| 61 | ia.len = 4; |
| 62 | ia.v4.s_addr = htonl(0x01020304); |
| 63 | res = in46a_ntop(&ia, buf, sizeof(buf)); |
| 64 | OSMO_ASSERT(res && !strcmp(res, "1.2.3.4")); |
| 65 | printf("res = %s\n", res); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | static void test_in46p_ntoa(void) |
| 69 | { |
| 70 | const struct in46_prefix ip46 = { |
| 71 | .prefixlen = 24, |
| 72 | .addr = { |
| 73 | .len = 4, |
| 74 | .v4.s_addr = htonl(0x10203000), |
| 75 | }, |
| 76 | }; |
| 77 | printf("in46p_ntoa() returns %s\n", in46p_ntoa(&ip46)); |
| 78 | } |
| 79 | |
| 80 | static void test_in46a_equal(void) |
| 81 | { |
| 82 | struct in46_addr b; |
| 83 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 84 | printf("Testing in46a_equal() with IPv4 addresses\n"); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 85 | |
| 86 | memset(&b, 0xff, sizeof(b)); |
| 87 | b.len = g_ia4.len; |
| 88 | b.v4.s_addr = g_ia4.v4.s_addr; |
| 89 | OSMO_ASSERT(in46a_equal(&g_ia4, &b)); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 90 | } |
| 91 | |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 92 | static int log_in46a_within_mask(const struct in46_addr *addr, const struct in46_addr *net, |
| 93 | size_t prefixlen) |
| 94 | { |
| 95 | int rc; |
| 96 | |
| 97 | printf("in46a_within_mask(%s, ", in46a_ntoa(addr)); |
| 98 | printf("%s, %lu) = ", in46a_ntoa(net), prefixlen); |
| 99 | |
| 100 | rc = in46a_within_mask(addr, net, prefixlen); |
| 101 | printf("%d\n", rc); |
| 102 | |
| 103 | return rc; |
| 104 | } |
| 105 | |
| 106 | static void test_in46a_within_mask(void) |
| 107 | { |
| 108 | struct in46_addr addr, mask; |
| 109 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 110 | printf("Testing in46a_within_mask() with IPv4 addresses\n"); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 111 | |
| 112 | addr = g_ia4; |
| 113 | mask = g_ia4; |
| 114 | OSMO_ASSERT(log_in46a_within_mask(&addr, &mask, 32)); |
| 115 | |
| 116 | mask.v4.s_addr = htonl( ntohl(mask.v4.s_addr) & 0xfffffffC ); |
| 117 | OSMO_ASSERT(log_in46a_within_mask(&addr, &mask, 30)); |
| 118 | |
| 119 | mask.v4.s_addr = htonl( ntohl(mask.v4.s_addr) & 0xfff80000 ); |
| 120 | OSMO_ASSERT(log_in46a_within_mask(&addr, &mask, 13)); |
| 121 | |
| 122 | addr.v4.s_addr = htonl(ntohl(addr.v4.s_addr) + 1); |
| 123 | mask = g_ia4; |
| 124 | OSMO_ASSERT(!log_in46a_within_mask(&addr, &mask, 32)); |
| 125 | mask.v4.s_addr = htonl( ntohl(mask.v4.s_addr) & 0xfffffffC ); |
| 126 | OSMO_ASSERT(log_in46a_within_mask(&addr, &mask, 30)); |
| 127 | } |
| 128 | |
| 129 | static void test_in46a_to_eua(void) |
| 130 | { |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 131 | struct ul66_t eua; |
| 132 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 133 | printf("testing in46a_to_eua() with IPv4 addresses\n"); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 134 | |
| 135 | #if 0 /* triggers assert in current implementation */ |
| 136 | const struct in46_addr ia_invalid = { .len = 3, }; |
| 137 | OSMO_ASSERT(in46a_to_eua(&ia_invalid, &eua) < 0); |
| 138 | #endif |
| 139 | |
| 140 | /* IPv4 address */ |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 141 | OSMO_ASSERT(in46a_to_eua(&g_ia4, 1, &eua) == 0); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 142 | OSMO_ASSERT(eua.v[0] == PDP_EUA_ORG_IETF); |
| 143 | OSMO_ASSERT(eua.v[1] == PDP_EUA_TYPE_v4); |
Neels Hofmeyr | 1d85bea | 2017-11-18 18:29:49 +0100 | [diff] [blame] | 144 | OSMO_ASSERT(osmo_load32le(&eua.v[2]) == g_ia4.v4.s_addr); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void test_in46a_from_eua(void) |
| 148 | { |
| 149 | struct in46_addr ia; |
| 150 | struct ul66_t eua; |
| 151 | const uint8_t v4_unspec[] = { PDP_EUA_ORG_IETF, PDP_EUA_TYPE_v4 }; |
| 152 | const uint8_t v4_spec[] = { PDP_EUA_ORG_IETF, PDP_EUA_TYPE_v4, 1,2,3,4 }; |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 153 | memset(&eua, 0, sizeof(eua)); |
| 154 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 155 | printf("Testing in46a_from_eua() with IPv4 addresses\n"); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 156 | |
| 157 | /* default: v4 unspec */ |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 158 | OSMO_ASSERT(in46a_from_eua(&eua, &ia) == 1); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 159 | OSMO_ASSERT(ia.len == 4); |
| 160 | OSMO_ASSERT(ia.v4.s_addr == 0); |
| 161 | |
| 162 | /* invalid */ |
| 163 | eua.v[0] = 0x23; |
| 164 | eua.v[1] = PDP_EUA_TYPE_v4; |
| 165 | eua.l = 6; |
| 166 | OSMO_ASSERT(in46a_from_eua(&eua, &ia) < 0); |
| 167 | |
| 168 | /* invalid */ |
| 169 | eua.v[0] = PDP_EUA_ORG_IETF; |
| 170 | eua.v[1] = 0x23; |
| 171 | eua.l = 6; |
| 172 | OSMO_ASSERT(in46a_from_eua(&eua, &ia) < 0); |
| 173 | |
| 174 | /* unspecified V4 */ |
| 175 | memcpy(eua.v, v4_unspec, sizeof(v4_unspec)); |
| 176 | eua.l = sizeof(v4_unspec); |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 177 | OSMO_ASSERT(in46a_from_eua(&eua, &ia) == 1); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 178 | OSMO_ASSERT(ia.len == 4); |
| 179 | OSMO_ASSERT(ia.v4.s_addr == 0); |
| 180 | |
| 181 | /* specified V4 */ |
| 182 | memcpy(eua.v, v4_spec, sizeof(v4_spec)); |
| 183 | eua.l = sizeof(v4_spec); |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 184 | OSMO_ASSERT(in46a_from_eua(&eua, &ia) == 1); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 185 | OSMO_ASSERT(ia.len == 4); |
| 186 | OSMO_ASSERT(ia.v4.s_addr == htonl(0x01020304)); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Pau Espin Pedrol | 2e7b9ff | 2017-10-16 14:27:32 +0200 | [diff] [blame] | 189 | static void test_in46a_netmasklen(void) |
| 190 | { |
| 191 | struct in46_addr netmask; |
| 192 | unsigned int len; |
| 193 | |
| 194 | printf("Testing in46a_netmasklen() with IPv4 addresses\n"); |
| 195 | netmask.len = 4; |
| 196 | |
| 197 | netmask.v4.s_addr = 0xffffffff; |
| 198 | len = in46a_netmasklen(&netmask); |
| 199 | OSMO_ASSERT(len == 32); |
| 200 | |
| 201 | netmask.v4.s_addr = 0x00ffffff; |
| 202 | len = in46a_netmasklen(&netmask); |
| 203 | OSMO_ASSERT(len == 24); |
| 204 | |
| 205 | netmask.v4.s_addr = 0x00f0ffff; |
| 206 | len = in46a_netmasklen(&netmask); |
| 207 | OSMO_ASSERT(len == 20); |
| 208 | |
| 209 | netmask.v4.s_addr = 0x000000fe; |
| 210 | len = in46a_netmasklen(&netmask); |
| 211 | OSMO_ASSERT(len == 7); |
| 212 | |
| 213 | netmask.v4.s_addr = 0x00000000; |
| 214 | len = in46a_netmasklen(&netmask); |
| 215 | OSMO_ASSERT(len == 0); |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 216 | } |
Pau Espin Pedrol | 2e7b9ff | 2017-10-16 14:27:32 +0200 | [diff] [blame] | 217 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 218 | /* IPv6 specific tests */ |
| 219 | |
| 220 | static const struct in46_addr g_ia6 = { |
| 221 | .len = 16, |
| 222 | .v6.s6_addr = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, |
| 223 | }; |
| 224 | |
| 225 | static void test_in46a_to_af_v6(void) |
| 226 | { |
| 227 | struct in46_addr ia; |
| 228 | |
| 229 | printf("Testing in46a_to_af() with IPv6 addresses\n"); |
| 230 | |
| 231 | OSMO_ASSERT(in46a_to_af(&g_ia6) == AF_INET6); |
| 232 | |
| 233 | ia.len = 8; |
| 234 | OSMO_ASSERT(in46a_to_af(&ia) == AF_INET6); |
| 235 | } |
| 236 | |
| 237 | static void test_in46a_to_sas_v6(void) |
| 238 | { |
| 239 | struct sockaddr_storage ss; |
| 240 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) &ss; |
| 241 | |
| 242 | printf("Testing in46a_to_sas() with IPv6 addresses\n"); |
| 243 | |
| 244 | memset(&ss, 0, sizeof(ss)); |
| 245 | OSMO_ASSERT(in46a_to_sas(&ss, &g_ia6) == 0); |
| 246 | OSMO_ASSERT(sin6->sin6_family == AF_INET6); |
| 247 | OSMO_ASSERT(!memcmp(&sin6->sin6_addr, &g_ia6.v6, sizeof(sin6->sin6_addr))); |
| 248 | } |
| 249 | |
| 250 | static void test_in46a_ntop_v6(void) |
| 251 | { |
| 252 | char buf[256]; |
| 253 | const char *res; |
| 254 | |
| 255 | printf("Testing in46a_ntop() with IPv6 addresses\n"); |
| 256 | |
| 257 | res = in46a_ntop(&g_ia6, buf, sizeof(buf)); |
| 258 | OSMO_ASSERT(res && !strcmp(res, "102:304:506:708:90a:b0c:d0e:f10")); |
| 259 | printf("res = %s\n", res); |
| 260 | } |
| 261 | |
| 262 | static void test_in46a_equal_v6(void) |
| 263 | { |
| 264 | struct in46_addr b; |
| 265 | |
| 266 | printf("Testing in46a_equal() with IPv6 addresses\n"); |
| 267 | |
| 268 | memset(&b, 0xff, sizeof(b)); |
| 269 | b.len = g_ia6.len; |
| 270 | b.v6 = g_ia6.v6; |
| 271 | OSMO_ASSERT(in46a_equal(&g_ia6, &b)); |
| 272 | } |
| 273 | |
| 274 | static void test_in46a_to_eua_v6(void) |
| 275 | { |
| 276 | const struct in46_addr ia_v6_8 = { |
| 277 | .len = 8, |
| 278 | .v6.s6_addr = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, |
| 279 | }; |
| 280 | struct ul66_t eua; |
| 281 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 282 | printf("Testing in46a_to_eua() with IPv6 addresses\n"); |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 283 | |
| 284 | /* IPv6 address */ |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 285 | OSMO_ASSERT(in46a_to_eua(&g_ia6, 1, &eua) == 0); |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 286 | OSMO_ASSERT(eua.v[0] == PDP_EUA_ORG_IETF); |
| 287 | OSMO_ASSERT(eua.v[1] == PDP_EUA_TYPE_v6); |
| 288 | OSMO_ASSERT(!memcmp(&eua.v[2], &g_ia6.v6, 16)); |
| 289 | |
| 290 | /* IPv6 address with prefix / length 8 */ |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 291 | OSMO_ASSERT(in46a_to_eua(&ia_v6_8, 1, &eua) == 0); |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 292 | OSMO_ASSERT(eua.v[0] == PDP_EUA_ORG_IETF); |
| 293 | OSMO_ASSERT(eua.v[1] == PDP_EUA_TYPE_v6); |
| 294 | OSMO_ASSERT(!memcmp(&eua.v[2], &ia_v6_8.v6, 16)); |
| 295 | } |
| 296 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 297 | static void test_in46a_to_eua_v4v6() { |
| 298 | const struct in46_addr ia_v4v6[2] = { |
| 299 | { |
| 300 | .len = 16, |
| 301 | .v6.s6_addr = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 }, |
| 302 | }, |
| 303 | { |
| 304 | .len = 4, |
| 305 | .v4.s_addr = 0x0d0c0b0a, |
| 306 | } |
| 307 | }; |
| 308 | struct ul66_t eua; |
| 309 | printf("Testing in46a_to_eua() with IPv4v6 addresses\n"); |
| 310 | |
| 311 | /* IPv4 address */ |
| 312 | OSMO_ASSERT(in46a_to_eua(ia_v4v6, 2, &eua) == 0); |
| 313 | OSMO_ASSERT(eua.v[0] == PDP_EUA_ORG_IETF); |
| 314 | OSMO_ASSERT(eua.v[1] == PDP_EUA_TYPE_v4v6); |
| 315 | OSMO_ASSERT(osmo_load32le(&eua.v[2]) == g_ia4.v4.s_addr); |
| 316 | OSMO_ASSERT(!memcmp(&eua.v[6], &g_ia6.v6, 16)); |
| 317 | } |
| 318 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 319 | static void test_in46a_from_eua_v6(void) |
| 320 | { |
| 321 | struct in46_addr ia; |
| 322 | struct ul66_t eua; |
| 323 | const uint8_t v6_unspec[] = { PDP_EUA_ORG_IETF, PDP_EUA_TYPE_v6 }; |
| 324 | const uint8_t v6_spec[] = { PDP_EUA_ORG_IETF, PDP_EUA_TYPE_v6, |
| 325 | 1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe,0xf,0x10 }; |
| 326 | |
| 327 | memset(&eua, 0, sizeof(eua)); |
| 328 | |
| 329 | printf("Testing in46a_from_eua() with IPv6 addresses\n"); |
| 330 | |
| 331 | /* unspecified V6 */ |
| 332 | memcpy(eua.v, v6_unspec, sizeof(v6_unspec)); |
| 333 | eua.l = sizeof(v6_unspec); |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 334 | OSMO_ASSERT(in46a_from_eua(&eua, &ia) == 1); |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 335 | OSMO_ASSERT(ia.len == 16); |
| 336 | OSMO_ASSERT(IN6_IS_ADDR_UNSPECIFIED(&ia.v6)); |
| 337 | |
| 338 | /* specified V6 */ |
| 339 | memcpy(eua.v, v6_spec, sizeof(v6_spec)); |
| 340 | eua.l = sizeof(v6_spec); |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 341 | OSMO_ASSERT(in46a_from_eua(&eua, &ia) == 1); |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 342 | OSMO_ASSERT(ia.len == 16); |
| 343 | OSMO_ASSERT(!memcmp(&ia.v6, v6_spec+2, ia.len)); |
| 344 | } |
| 345 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 346 | static void test_in46a_from_eua_v4v6(void) { |
| 347 | struct in46_addr ia[2]; |
| 348 | struct ul66_t eua; |
| 349 | const uint8_t v4_unspec_v6_unspec[] = { PDP_EUA_ORG_IETF, PDP_EUA_TYPE_v4v6 }; |
| 350 | const uint8_t v4_spec_v6_unspec[] = { PDP_EUA_ORG_IETF, PDP_EUA_TYPE_v4v6, 1,2,3,4 }; |
| 351 | const uint8_t v4_unspec_v6_spec[] = { PDP_EUA_ORG_IETF, PDP_EUA_TYPE_v4v6, 1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe,0xf,0x10 }; |
| 352 | const uint8_t v4_spec_v6_spec[] = { PDP_EUA_ORG_IETF, PDP_EUA_TYPE_v4v6, 1,2,3,4, 1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe,0xf,0x10 }; |
| 353 | |
| 354 | memset(&eua, 0, sizeof(eua)); |
| 355 | |
| 356 | printf("Testing in46a_from_eua() with IPv4v6 addresses\n"); |
| 357 | |
| 358 | /* unspecified V4 & V6 */ |
| 359 | memcpy(eua.v, v4_unspec_v6_unspec, sizeof(v4_unspec_v6_unspec)); |
| 360 | eua.l = sizeof(v4_unspec_v6_unspec); |
| 361 | OSMO_ASSERT(in46a_from_eua(&eua, ia) == 2); |
| 362 | OSMO_ASSERT(ia[0].len == 4); |
| 363 | OSMO_ASSERT(ia[1].len == 16); |
| 364 | OSMO_ASSERT(ia[0].v4.s_addr == 0); |
| 365 | OSMO_ASSERT(IN6_IS_ADDR_UNSPECIFIED(&ia[1].v6)); |
| 366 | |
| 367 | /* specified V4, unspecified V6 */ |
| 368 | memcpy(eua.v, v4_spec_v6_unspec, sizeof(v4_spec_v6_unspec)); |
| 369 | eua.l = sizeof(v4_spec_v6_unspec); |
| 370 | OSMO_ASSERT(in46a_from_eua(&eua, ia) == 2); |
| 371 | OSMO_ASSERT(ia[0].len == 4); |
| 372 | OSMO_ASSERT(ia[1].len == 16); |
| 373 | OSMO_ASSERT(ia[0].v4.s_addr == htonl(0x01020304)); |
| 374 | OSMO_ASSERT(IN6_IS_ADDR_UNSPECIFIED(&ia[1].v6)); |
| 375 | |
| 376 | /* unspecified V4, specified V6 */ |
| 377 | memcpy(eua.v, v4_unspec_v6_spec, sizeof(v4_unspec_v6_spec)); |
| 378 | eua.l = sizeof(v4_unspec_v6_spec); |
| 379 | OSMO_ASSERT(in46a_from_eua(&eua, ia) == 2); |
| 380 | OSMO_ASSERT(ia[0].len == 4); |
| 381 | OSMO_ASSERT(ia[1].len == 16); |
| 382 | OSMO_ASSERT(ia[0].v4.s_addr == 0); |
| 383 | OSMO_ASSERT(!memcmp(&ia[1].v6, v4_unspec_v6_spec+2, ia[1].len)); |
| 384 | |
| 385 | /* specified V4, specified V6 */ |
| 386 | memcpy(eua.v, v4_spec_v6_spec, sizeof(v4_spec_v6_spec)); |
| 387 | eua.l = sizeof(v4_spec_v6_spec); |
| 388 | OSMO_ASSERT(in46a_from_eua(&eua, ia) == 2); |
| 389 | OSMO_ASSERT(ia[0].len == 4); |
| 390 | OSMO_ASSERT(ia[1].len == 16); |
| 391 | OSMO_ASSERT(ia[0].v4.s_addr == htonl(0x01020304)); |
| 392 | OSMO_ASSERT(!memcmp(&ia[1].v6, v4_spec_v6_spec+6, ia[1].len)); |
| 393 | } |
| 394 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 395 | static void test_in46a_netmasklen_v6(void) |
| 396 | { |
| 397 | unsigned int len; |
Pau Espin Pedrol | 2e7b9ff | 2017-10-16 14:27:32 +0200 | [diff] [blame] | 398 | printf("Testing in46a_netmasklen() with IPv6 addresses\n"); |
| 399 | const struct in46_addr netmaskA = { |
| 400 | .len = 16, |
| 401 | .v6.s6_addr = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}, |
| 402 | }; |
| 403 | len = in46a_netmasklen(&netmaskA); |
| 404 | OSMO_ASSERT(len == 128); |
| 405 | |
| 406 | const struct in46_addr netmaskB = { |
| 407 | .len = 16, |
| 408 | .v6.s6_addr = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x00,0x00}, |
| 409 | }; |
| 410 | len = in46a_netmasklen(&netmaskB); |
| 411 | OSMO_ASSERT(len == 104); |
| 412 | |
| 413 | const struct in46_addr netmaskC = { |
| 414 | .len = 16, |
| 415 | .v6.s6_addr = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0x00,0x00,0x00}, |
| 416 | }; |
| 417 | len = in46a_netmasklen(&netmaskC); |
| 418 | OSMO_ASSERT(len == 103); |
| 419 | |
| 420 | const struct in46_addr netmaskD = { |
| 421 | .len = 16, |
| 422 | .v6.s6_addr = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, |
| 423 | }; |
| 424 | len = in46a_netmasklen(&netmaskD); |
| 425 | OSMO_ASSERT(len == 0); |
| 426 | } |
| 427 | |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 428 | int main(int argc, char **argv) |
| 429 | { |
Pau Espin Pedrol | 042a445 | 2018-04-17 14:31:42 +0200 | [diff] [blame] | 430 | void *tall_ctx = talloc_named_const(NULL, 1, "Root context"); |
| 431 | msgb_talloc_ctx_init(tall_ctx, 0); |
| 432 | osmo_init_logging2(tall_ctx, &log_info); |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 433 | log_set_use_color(osmo_stderr_target, 0); |
| 434 | log_set_print_filename(osmo_stderr_target, 0); |
| 435 | |
| 436 | srand(time(NULL)); |
| 437 | |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 438 | if (argc < 2 || strcmp(argv[1], "-v6")) { |
| 439 | test_in46a_to_af(); |
| 440 | test_in46a_to_sas(); |
| 441 | test_in46a_ntop(); |
| 442 | test_in46p_ntoa(); |
| 443 | test_in46a_equal(); |
| 444 | test_in46a_within_mask(); |
| 445 | test_in46a_to_eua(); |
| 446 | test_in46a_from_eua(); |
| 447 | test_in46a_netmasklen(); |
| 448 | } else { |
| 449 | test_in46a_to_af_v6(); |
| 450 | test_in46a_to_sas_v6(); |
| 451 | test_in46a_ntop_v6(); |
| 452 | test_in46a_equal_v6(); |
| 453 | test_in46a_to_eua_v6(); |
| 454 | test_in46a_from_eua_v6(); |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 455 | test_in46a_to_eua_v4v6(); |
| 456 | test_in46a_from_eua_v4v6(); |
Pau Espin Pedrol | 9c0f4f4 | 2017-12-04 12:52:23 +0100 | [diff] [blame] | 457 | test_in46a_netmasklen_v6(); |
| 458 | } |
Neels Hofmeyr | dabb8b4 | 2017-10-29 01:53:50 +0200 | [diff] [blame] | 459 | return 0; |
Harald Welte | db924d3 | 2017-10-12 20:01:54 +0800 | [diff] [blame] | 460 | } |