| 1 |
#include "config.h" |
| 2 |
#include "torture.h" |
| 3 |
#include "libssh/options.h" |
| 4 |
#include "libssh/session.h" |
| 5 |
#include "match.c" |
| 6 |
#ifdef HAVE_IFADDRS_H |
| 7 |
#include <ifaddrs.h> |
| 8 |
#endif |
| 9 |
#include <net/if.h> |
| 10 |
#include <stdbool.h> |
| 11 |
|
| 12 |
/* This list contains common local subnet addresses and more generic ones */ |
| 13 |
#define IPV4_LIST \ |
| 14 |
"158.46.192.0/18,213.86.215.224/27,61.67.54.0/23,164.155.128.0/21," \ |
| 15 |
"171.10.0.0/16,205.59.221.0/24,122.105.209.48/28,10.0.1.0/24," \ |
| 16 |
"130.192.28.0/22,172.16.16.0/16,192.168.0.0/24,169.254.0.0/16" |
| 17 |
|
| 18 |
#define IPV6_LIST "fe80::/64" |
| 19 |
|
| 20 |
static int |
| 21 |
setup(void **state) |
| 22 |
{ |
| 23 |
ssh_session session = NULL; |
| 24 |
char *wd = NULL; |
| 25 |
int verbosity; |
| 26 |
|
| 27 |
session = ssh_new(); |
| 28 |
|
| 29 |
verbosity = torture_libssh_verbosity(); |
| 30 |
ssh_options_set(session, SSH_OPTIONS_LOG_VERBOSITY, &verbosity); |
| 31 |
wd = torture_get_current_working_dir(); |
| 32 |
ssh_options_set(session, SSH_OPTIONS_SSH_DIR, wd); |
| 33 |
free(wd); |
| 34 |
|
| 35 |
*state = session; |
| 36 |
|
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
static int |
| 41 |
teardown(void **state) |
| 42 |
{ |
| 43 |
ssh_free(*state); |
| 44 |
|
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @brief helper function loading configuration from either file or string |
| 50 |
*/ |
| 51 |
static void |
| 52 |
_parse_config(ssh_session session, |
| 53 |
const char *file, |
| 54 |
const char *string, |
| 55 |
int expected) |
| 56 |
{ |
| 57 |
/* |
| 58 |
* Initialisation of ret is not needed, but the compiler is not able to |
| 59 |
* understand fail() so it will complain about uninitialised use of ret |
| 60 |
* below in assert_ssh_return_code_equal() |
| 61 |
*/ |
| 62 |
int ret = -1; |
| 63 |
|
| 64 |
/* |
| 65 |
* make sure either config file or config string is given, |
| 66 |
* not both |
| 67 |
*/ |
| 68 |
assert_int_not_equal(file == NULL, string == NULL); |
| 69 |
|
| 70 |
if (file != NULL) { |
| 71 |
ret = ssh_config_parse_file(session, file); |
| 72 |
} else if (string != NULL) { |
| 73 |
ret = ssh_config_parse_string(session, string); |
| 74 |
} else { |
| 75 |
/* should not happen */ |
| 76 |
fail(); |
| 77 |
} |
| 78 |
|
| 79 |
/* make sure parsing went as expected */ |
| 80 |
assert_ssh_return_code_equal(session, ret, expected); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @brief converts subnet mask to prefix length (IPv4) |
| 85 |
*/ |
| 86 |
static int |
| 87 |
subnet_mask_to_prefix_length_4(struct in_addr subnet_mask) |
| 88 |
{ |
| 89 |
uint32_t mask; |
| 90 |
int prefix_length = 0; |
| 91 |
|
| 92 |
mask = ntohl(subnet_mask.s_addr); |
| 93 |
|
| 94 |
/* Count the number of consecutive 1 bits */ |
| 95 |
while (mask & 0x80000000) { |
| 96 |
prefix_length++; |
| 97 |
mask <<= 1; |
| 98 |
} |
| 99 |
return prefix_length; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @brief converts subnet mask to prefix length (IPv6) |
| 104 |
*/ |
| 105 |
static int |
| 106 |
subnet_mask_to_prefix_length_6(struct in6_addr subnet_mask) |
| 107 |
{ |
| 108 |
uint8_t *mask = NULL, chunk; |
| 109 |
int i, j, prefix_length = 0; |
| 110 |
|
| 111 |
mask = subnet_mask.s6_addr; |
| 112 |
|
| 113 |
/* Count the number of consecutive 1 bits in each byte chunk */ |
| 114 |
for (i = 0; i < 16; i++) { |
| 115 |
chunk = mask[i]; |
| 116 |
while (chunk) { |
| 117 |
for (j = 0; j < 8; j++) { |
| 118 |
if (chunk & 0x80) { |
| 119 |
prefix_length++; |
| 120 |
chunk <<= 1; |
| 121 |
} else { |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
return prefix_length; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @brief helper function returning the IPv4 and IPv6 network ID |
| 132 |
* (in CIDR format) corresponding to any of the running local interfaces. |
| 133 |
* The network interface corresponding to IPv4 and IPv6 network ID may be |
| 134 |
* different. |
| 135 |
* |
| 136 |
* @note If no non-loopback network interfaces are found for IPv4 or |
| 137 |
* IPv6, the function will fall back to using the loopback addresses. |
| 138 |
*/ |
| 139 |
static int |
| 140 |
get_network_id(char *net_id_4, char *net_id_6) |
| 141 |
{ |
| 142 |
struct ifaddrs *ifa = NULL, *ifaddrs = NULL; |
| 143 |
struct in_addr addr, network_id_4, subnet_mask_4; |
| 144 |
struct in6_addr addr6, network_id_6, subnet_mask_6; |
| 145 |
struct sockaddr_in netmask; |
| 146 |
struct sockaddr_in6 netmask6; |
| 147 |
char address[NI_MAXHOST], *a = NULL; |
| 148 |
char *network_id_str = NULL, network_id_str6[INET6_ADDRSTRLEN], |
| 149 |
lo_net_id_4[NI_MAXHOST], lo_net_id_6[NI_MAXHOST]; |
| 150 |
int i, prefix_length, rc; |
| 151 |
int found_4 = 0, found_lo_4 = 0, found_6 = 0, found_lo_6 = 0; |
| 152 |
socklen_t sa_len; |
| 153 |
|
| 154 |
ZERO_STRUCT(addr); |
| 155 |
ZERO_STRUCT(network_id_4); |
| 156 |
ZERO_STRUCT(subnet_mask_4); |
| 157 |
|
| 158 |
ZERO_STRUCT(addr6); |
| 159 |
ZERO_STRUCT(network_id_6); |
| 160 |
ZERO_STRUCT(subnet_mask_6); |
| 161 |
|
| 162 |
if (getifaddrs(&ifaddrs) != 0) { |
| 163 |
goto out; |
| 164 |
} |
| 165 |
|
| 166 |
for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { |
| 167 |
if (found_4 && found_6) { |
| 168 |
break; |
| 169 |
} |
| 170 |
|
| 171 |
if (ifa->ifa_addr == NULL || (ifa->ifa_flags & IFF_UP) == 0) { |
| 172 |
continue; |
| 173 |
} |
| 174 |
|
| 175 |
switch (ifa->ifa_addr->sa_family) { |
| 176 |
case AF_INET: |
| 177 |
if (found_4) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
sa_len = sizeof(struct sockaddr_in); |
| 181 |
break; |
| 182 |
case AF_INET6: |
| 183 |
if (found_6) { |
| 184 |
continue; |
| 185 |
} |
| 186 |
sa_len = sizeof(struct sockaddr_in6); |
| 187 |
break; |
| 188 |
default: |
| 189 |
continue; |
| 190 |
} |
| 191 |
|
| 192 |
rc = getnameinfo(ifa->ifa_addr, |
| 193 |
sa_len, |
| 194 |
address, |
| 195 |
sizeof(address), |
| 196 |
NULL, |
| 197 |
0, |
| 198 |
NI_NUMERICHOST); |
| 199 |
if (rc != 0) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
if (ifa->ifa_addr->sa_family == AF_INET) { |
| 204 |
|
| 205 |
/* Extract subnet mask */ |
| 206 |
memcpy(&netmask, ifa->ifa_netmask, sizeof(struct sockaddr_in)); |
| 207 |
subnet_mask_4 = netmask.sin_addr; |
| 208 |
|
| 209 |
rc = inet_pton(AF_INET, address, &addr); |
| 210 |
if (rc == 0) { |
| 211 |
continue; |
| 212 |
} |
| 213 |
|
| 214 |
/* Calculate the network ID */ |
| 215 |
network_id_4.s_addr = addr.s_addr & subnet_mask_4.s_addr; |
| 216 |
|
| 217 |
/* Convert network ID to string and compute prefix length */ |
| 218 |
network_id_str = inet_ntoa(network_id_4); |
| 219 |
if (network_id_str == NULL) { |
| 220 |
continue; |
| 221 |
} |
| 222 |
prefix_length = subnet_mask_to_prefix_length_4(subnet_mask_4); |
| 223 |
if (prefix_length > 32) { |
| 224 |
continue; |
| 225 |
} |
| 226 |
|
| 227 |
if (strcmp(ifa->ifa_name, "lo") == 0) { |
| 228 |
/* Store it temporarily in case needed for fallback */ |
| 229 |
snprintf(lo_net_id_4, |
| 230 |
NI_MAXHOST, |
| 231 |
"%s/%u", |
| 232 |
network_id_str, |
| 233 |
prefix_length); |
| 234 |
found_lo_4 = 1; |
| 235 |
} else { |
| 236 |
snprintf(net_id_4, |
| 237 |
NI_MAXHOST, |
| 238 |
"%s/%u", |
| 239 |
network_id_str, |
| 240 |
prefix_length); |
| 241 |
found_4 = 1; |
| 242 |
} |
| 243 |
} else if (ifa->ifa_addr->sa_family == AF_INET6) { |
| 244 |
|
| 245 |
/* Remove interface in case of IPv6 address: addr%interface */ |
| 246 |
a = strchr(address, '%'); |
| 247 |
if (a != NULL) { |
| 248 |
*a = '\0'; |
| 249 |
} |
| 250 |
|
| 251 |
/* Extract subnet mask */ |
| 252 |
memcpy(&netmask6, ifa->ifa_netmask, sizeof(struct sockaddr_in6)); |
| 253 |
subnet_mask_6 = netmask6.sin6_addr; |
| 254 |
|
| 255 |
rc = inet_pton(AF_INET6, address, &addr6); |
| 256 |
if (rc == 0) { |
| 257 |
continue; |
| 258 |
} |
| 259 |
|
| 260 |
/* Calculate the network ID */ |
| 261 |
for (i = 0; i < 16; i++) { |
| 262 |
network_id_6.s6_addr[i] = |
| 263 |
addr6.s6_addr[i] & subnet_mask_6.s6_addr[i]; |
| 264 |
} |
| 265 |
|
| 266 |
/* Convert network ID to string and compute prefix length */ |
| 267 |
if (inet_ntop(AF_INET6, |
| 268 |
&network_id_6, |
| 269 |
network_id_str6, |
| 270 |
INET6_ADDRSTRLEN) == NULL) { |
| 271 |
continue; |
| 272 |
} |
| 273 |
prefix_length = subnet_mask_to_prefix_length_6(subnet_mask_6); |
| 274 |
if (prefix_length > 128) { |
| 275 |
continue; |
| 276 |
} |
| 277 |
|
| 278 |
if (strcmp(ifa->ifa_name, "lo") == 0) { |
| 279 |
/* Store it temporarily in case needed for fallback */ |
| 280 |
snprintf(lo_net_id_6, |
| 281 |
NI_MAXHOST, |
| 282 |
"%s/%u", |
| 283 |
network_id_str6, |
| 284 |
prefix_length); |
| 285 |
found_lo_6 = 1; |
| 286 |
} else { |
| 287 |
snprintf(net_id_6, |
| 288 |
NI_MAXHOST, |
| 289 |
"%s/%u", |
| 290 |
network_id_str6, |
| 291 |
prefix_length); |
| 292 |
found_6 = 1; |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/* |
| 298 |
* Fallback to the loopback network ID (127.0.0.0/8) if no other |
| 299 |
* IPv4 network ID has been found. |
| 300 |
*/ |
| 301 |
if (!found_4 && found_lo_4) { |
| 302 |
snprintf(net_id_4, NI_MAXHOST, "%s", lo_net_id_4); |
| 303 |
found_4 = 1; |
| 304 |
} |
| 305 |
|
| 306 |
/* |
| 307 |
* Fallback to the loopback network ID (::1/128) if no other |
| 308 |
* IPv6 network ID has been found. |
| 309 |
*/ |
| 310 |
if (!found_6 && found_lo_6) { |
| 311 |
snprintf(net_id_6, NI_MAXHOST, "%s", lo_net_id_6); |
| 312 |
found_6 = 1; |
| 313 |
} |
| 314 |
|
| 315 |
freeifaddrs(ifaddrs); |
| 316 |
|
| 317 |
out: |
| 318 |
/* if both net_id_4 and net_id_6 are not set then we should fail */ |
| 319 |
return (found_4 && found_6) ? 0 : -1; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* @brief Verify the match between a IPv4/IPv6 address and a IPv4/IPv6 subnet |
| 324 |
*/ |
| 325 |
static void |
| 326 |
assert_true_match_cidr(const char *try, |
| 327 |
const char *match, |
| 328 |
unsigned int mask_len, |
| 329 |
int af, |
| 330 |
int rv) |
| 331 |
{ |
| 332 |
struct in_addr try_addr, match_addr; |
| 333 |
struct in6_addr try_addr6, match_addr6; |
| 334 |
int r1, r2; |
| 335 |
|
| 336 |
switch (af) { |
| 337 |
case AF_INET: |
| 338 |
ZERO_STRUCT(try_addr); |
| 339 |
ZERO_STRUCT(match_addr); |
| 340 |
|
| 341 |
r1 = inet_pton(AF_INET, try, &try_addr); |
| 342 |
r2 = inet_pton(AF_INET, match, &match_addr); |
| 343 |
if (r1 == 0 || r2 == 0) { |
| 344 |
fail(); |
| 345 |
} |
| 346 |
assert_int_equal(cidr_match_4(&try_addr, &match_addr, mask_len), rv); |
| 347 |
break; |
| 348 |
case AF_INET6: |
| 349 |
ZERO_STRUCT(try_addr6); |
| 350 |
ZERO_STRUCT(match_addr6); |
| 351 |
|
| 352 |
r1 = inet_pton(AF_INET6, try, &try_addr6); |
| 353 |
r2 = inet_pton(AF_INET6, match, &match_addr6); |
| 354 |
if (r1 == 0 || r2 == 0) { |
| 355 |
fail(); |
| 356 |
} |
| 357 |
assert_int_equal(cidr_match_6(&try_addr6, &match_addr6, mask_len), rv); |
| 358 |
break; |
| 359 |
default: |
| 360 |
fail(); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* @brief Verify the configuration parser accepts Match localnetwork keyword |
| 366 |
*/ |
| 367 |
static void |
| 368 |
torture_config_match_localnetwork(void **state, bool use_file) |
| 369 |
{ |
| 370 |
ssh_session session = *state; |
| 371 |
const char *config = NULL; |
| 372 |
char config_string[2048]; |
| 373 |
char network_id_4[NI_MAXHOST], network_id_6[NI_MAXHOST]; |
| 374 |
const char *file = NULL, *string = NULL; |
| 375 |
|
| 376 |
if (use_file == true) { |
| 377 |
file = "libssh_testconfig_localnetwork.tmp"; |
| 378 |
} |
| 379 |
|
| 380 |
if (get_network_id(network_id_4, network_id_6) == -1) { |
| 381 |
fail(); |
| 382 |
} |
| 383 |
|
| 384 |
/* IPv4 test */ |
| 385 |
snprintf(config_string, |
| 386 |
sizeof(config_string), |
| 387 |
"Match localnetwork %s\n" |
| 388 |
"\tHostName expected.com\n", |
| 389 |
network_id_4); |
| 390 |
config = config_string; |
| 391 |
|
| 392 |
if (use_file == true) { |
| 393 |
torture_write_file(file, config); |
| 394 |
} else { |
| 395 |
string = config; |
| 396 |
} |
| 397 |
torture_reset_config(session); |
| 398 |
_parse_config(session, file, string, SSH_OK); |
| 399 |
assert_string_equal(session->opts.host, "expected.com"); |
| 400 |
|
| 401 |
/* IPv6 test */ |
| 402 |
snprintf(config_string, |
| 403 |
sizeof(config_string), |
| 404 |
"Match localnetwork %s\n" |
| 405 |
"\tHostName expected.com\n", |
| 406 |
network_id_6); |
| 407 |
config = config_string; |
| 408 |
|
| 409 |
if (use_file == true) { |
| 410 |
torture_write_file(file, config); |
| 411 |
} else { |
| 412 |
string = config; |
| 413 |
} |
| 414 |
torture_reset_config(session); |
| 415 |
_parse_config(session, file, string, SSH_OK); |
| 416 |
assert_string_equal(session->opts.host, "expected.com"); |
| 417 |
|
| 418 |
/* Test negate condition */ |
| 419 |
snprintf(config_string, |
| 420 |
sizeof(config_string), |
| 421 |
"Match Host station !localnetwork %s\n" |
| 422 |
"\tHostName expected.com\n" |
| 423 |
"Host station\n" |
| 424 |
"\tHostName negate.com\n", |
| 425 |
network_id_4); |
| 426 |
config = config_string; |
| 427 |
|
| 428 |
if (use_file == true) { |
| 429 |
torture_write_file(file, config); |
| 430 |
} else { |
| 431 |
string = config; |
| 432 |
} |
| 433 |
torture_reset_config(session); |
| 434 |
ssh_options_set(session, SSH_OPTIONS_HOST, "station"); |
| 435 |
_parse_config(session, file, string, SSH_OK); |
| 436 |
assert_string_equal(session->opts.host, "negate.com"); |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* @brief Verify the configuration parser accepts Match localnetwork keyword |
| 441 |
* through configuration file. |
| 442 |
*/ |
| 443 |
static void |
| 444 |
torture_config_match_localnetwork_file(void **state) |
| 445 |
{ |
| 446 |
torture_config_match_localnetwork(state, true); |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* @brief Verify the configuration parser accepts Match localnetwork keyword |
| 451 |
* through configuration string. |
| 452 |
*/ |
| 453 |
static void |
| 454 |
torture_config_match_localnetwork_string(void **state) |
| 455 |
{ |
| 456 |
torture_config_match_localnetwork(state, false); |
| 457 |
} |
| 458 |
|
| 459 |
/** |
| 460 |
* @brief Verify the cidr matching function works correctly |
| 461 |
* with IPv4 addresses |
| 462 |
*/ |
| 463 |
static void |
| 464 |
torture_match_cidr_address_list_ipv4(void **state) |
| 465 |
{ |
| 466 |
int rc; |
| 467 |
(void)state; |
| 468 |
|
| 469 |
/* Test some valid IPv4 addresses */ |
| 470 |
rc = match_cidr_address_list("192.158.50.5", "192.158.50.0/28", AF_INET); |
| 471 |
assert_int_equal(rc, 1); |
| 472 |
rc = match_cidr_address_list("10.2.200.200", "10.2.128.0/17", AF_INET); |
| 473 |
assert_int_equal(rc, 1); |
| 474 |
rc = match_cidr_address_list("192.168.175.40", "192.168.175.0/26", AF_INET); |
| 475 |
assert_int_equal(rc, 1); |
| 476 |
rc = match_cidr_address_list("172.31.140.100", "172.31.128.0/19", AF_INET); |
| 477 |
assert_int_equal(rc, 1); |
| 478 |
rc = match_cidr_address_list("10.3.9.50", "10.3.8.0/23", AF_INET); |
| 479 |
assert_int_equal(rc, 1); |
| 480 |
|
| 481 |
/* Test positive match with unknown host address family */ |
| 482 |
rc = match_cidr_address_list("158.15.96.13", "158.12.30.0/12", -1); |
| 483 |
assert_int_equal(rc, 1); |
| 484 |
|
| 485 |
/* Test some valid IPv4 addresses against IPV4_LIST */ |
| 486 |
rc = match_cidr_address_list("164.155.128.15", IPV4_LIST, AF_INET); |
| 487 |
assert_int_equal(rc, 1); |
| 488 |
rc = match_cidr_address_list("158.46.223.71", IPV4_LIST, AF_INET); |
| 489 |
assert_int_equal(rc, 1); |
| 490 |
rc = match_cidr_address_list("205.59.221.160", IPV4_LIST, AF_INET); |
| 491 |
assert_int_equal(rc, 1); |
| 492 |
rc = match_cidr_address_list("10.0.1.254", IPV4_LIST, AF_INET); |
| 493 |
assert_int_equal(rc, 1); |
| 494 |
rc = match_cidr_address_list("172.16.58.1", IPV4_LIST, AF_INET); |
| 495 |
assert_int_equal(rc, 1); |
| 496 |
rc = match_cidr_address_list("169.254.20.28", IPV4_LIST, AF_INET); |
| 497 |
assert_int_equal(rc, 1); |
| 498 |
|
| 499 |
rc = match_cidr_address_list("255.255.255.255", "0.0.0.0/0", AF_INET); |
| 500 |
assert_int_equal(rc, 1); |
| 501 |
|
| 502 |
/* Test some not matching IPv4 addresses */ |
| 503 |
rc = match_cidr_address_list("172.21.0.200", "172.20.240.0/20", AF_INET); |
| 504 |
assert_int_equal(rc, 0); |
| 505 |
rc = match_cidr_address_list("10.10.14.100", "10.10.10.0/22", AF_INET); |
| 506 |
assert_int_equal(rc, 0); |
| 507 |
rc = match_cidr_address_list("192.168.150.8", "192.168.150.0/29", AF_INET); |
| 508 |
assert_int_equal(rc, 0); |
| 509 |
rc = match_cidr_address_list("10.238.16.50", "10.255.0.0/12", AF_INET); |
| 510 |
assert_int_equal(rc, 0); |
| 511 |
rc = match_cidr_address_list("172.31.160.100", "172.31.128.0/19", AF_INET); |
| 512 |
assert_int_equal(rc, 0); |
| 513 |
rc = match_cidr_address_list("192.168.4.98", IPV4_LIST, AF_INET); |
| 514 |
assert_int_equal(rc, 0); |
| 515 |
rc = match_cidr_address_list("0.0.0.0", IPV4_LIST, AF_INET); |
| 516 |
assert_int_equal(rc, 0); |
| 517 |
|
| 518 |
/* Test negative match with unknown host address family */ |
| 519 |
rc = match_cidr_address_list("122.105.210.57", IPV4_LIST, -1); |
| 520 |
assert_int_equal(rc, 0); |
| 521 |
|
| 522 |
/* Test some invalid input */ |
| 523 |
rc = match_cidr_address_list("192.168.1.x", "192.168.1.0/24", AF_INET); |
| 524 |
assert_int_equal(rc, -1); |
| 525 |
rc = match_cidr_address_list("0.168.f2.b8", "172.0.0.0/24", AF_INET); |
| 526 |
assert_int_equal(rc, -1); |
| 527 |
rc = match_cidr_address_list("10.0.1.2/22", "10.0.1.0/22", AF_INET); |
| 528 |
assert_int_equal(rc, -1); |
| 529 |
rc = match_cidr_address_list("10.0.1.2/", "10.0.1.0/22", AF_INET); |
| 530 |
assert_int_equal(rc, -1); |
| 531 |
rc = match_cidr_address_list("172.16.16.5/abc1", "172.16.16.0/24", AF_INET); |
| 532 |
assert_int_equal(rc, -1); |
| 533 |
rc = match_cidr_address_list("172.16.18.251", "172.16.16.0", AF_INET); |
| 534 |
assert_int_equal(rc, -1); |
| 535 |
|
| 536 |
/* Test invalid input with unknown host address family */ |
| 537 |
rc = match_cidr_address_list("172.67.3.x", IPV4_LIST, -1); |
| 538 |
assert_int_equal(rc, -1); |
| 539 |
|
| 540 |
/* Test invalid CIDR list */ |
| 541 |
rc = match_cidr_address_list(NULL, "192.168.1.0/33", AF_INET); |
| 542 |
assert_int_equal(rc, -1); |
| 543 |
rc = match_cidr_address_list(NULL, "", -1); |
| 544 |
assert_int_equal(rc, -1); |
| 545 |
rc = match_cidr_address_list(NULL, ",", -1); |
| 546 |
assert_int_equal(rc, -1); |
| 547 |
rc = match_cidr_address_list(NULL, ",192.168.1.0/24", -1); |
| 548 |
assert_int_equal(rc, -1); |
| 549 |
rc = match_cidr_address_list(NULL, "10.0.0.0/24 , 192.168.1.0/24", -1); |
| 550 |
assert_int_equal(rc, -1); |
| 551 |
rc = match_cidr_address_list( |
| 552 |
NULL, |
| 553 |
"ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255/128junkdata", |
| 554 |
-1); |
| 555 |
assert_int_equal(rc, -1); |
| 556 |
} |
| 557 |
|
| 558 |
/** |
| 559 |
* @brief Verify the cidr matching function works correctly |
| 560 |
* with IPv6 addresses |
| 561 |
*/ |
| 562 |
static void |
| 563 |
torture_match_cidr_address_list_ipv6(void **state) |
| 564 |
{ |
| 565 |
/* Test link-local addresses against fe80::/64 */ |
| 566 |
int i, rc, valid_addr_len, invalid_addr_len; |
| 567 |
const char *valid_addr[] = {"fe80::aadf:b119:507a:986a%abcdef", |
| 568 |
"fe80::0000:b418:efd4:5160:0a25%abcdef", |
| 569 |
"fe80::c7f5:7f94:4bd9:c35c%abcdef", |
| 570 |
"fe80::321f:46c2:0cea:ec54%abcdef", |
| 571 |
"fe80::906d:b670:86a2:fd68%abc", |
| 572 |
"fe80::b1c2:0000:0039:b598%", |
| 573 |
"fe80::07e8:39e6:cb49:9cd4", |
| 574 |
"fe80::1%abcdef", |
| 575 |
"fe80:0:0:0:202:b3ff:fe1e:8329%abcdef", |
| 576 |
"fe80:0000:0000:0000:0202:b3ff:fe1e:8329"}; |
| 577 |
|
| 578 |
const char *invalid_addr[] = {"fe80::8d1d:4d88:68a8:44f8:f3e7%abcdef", |
| 579 |
"2001:0db8:85a3::8a2e:0370:7334%abcdef", |
| 580 |
"fd00::adf8:7c21:147c:6c97", |
| 581 |
"::1%lo", |
| 582 |
"fe80::1:4d88:68a8:1200:f3e7%abcdef"}; |
| 583 |
|
| 584 |
(void)state; |
| 585 |
|
| 586 |
/* Test valid link-local addresses */ |
| 587 |
valid_addr_len = sizeof(valid_addr) / sizeof(valid_addr[0]); |
| 588 |
for (i = 0; i < valid_addr_len; i++) { |
| 589 |
rc = match_cidr_address_list(valid_addr[i], IPV6_LIST, AF_INET6); |
| 590 |
assert_int_equal(rc, 1); |
| 591 |
} |
| 592 |
rc = match_cidr_address_list("fe80:0000:0000:0000:0202:b3ff:fe1e:8329", |
| 593 |
"fe80:0000:0000:0000:0202:b3ff:fe1e:8328/127", |
| 594 |
AF_INET6); |
| 595 |
assert_int_equal(rc, 1); |
| 596 |
|
| 597 |
/* Test positive match with unknown host address family */ |
| 598 |
rc = match_cidr_address_list("fe80::aadf:b119:507a:986a%abcdef", |
| 599 |
IPV6_LIST, |
| 600 |
-1); |
| 601 |
assert_int_equal(rc, 1); |
| 602 |
|
| 603 |
/* Test some invalid input */ |
| 604 |
invalid_addr_len = sizeof(invalid_addr) / sizeof(invalid_addr[0]); |
| 605 |
for (i = 0; i < invalid_addr_len; i++) { |
| 606 |
rc = match_cidr_address_list(invalid_addr[i], IPV6_LIST, AF_INET6); |
| 607 |
assert_int_equal(rc, 0); |
| 608 |
} |
| 609 |
|
| 610 |
/* Test negative match with unknown host address family */ |
| 611 |
rc = match_cidr_address_list("fe80::8d1d:4d88:68a8:44f8:f3e7%abcdef", |
| 612 |
IPV6_LIST, |
| 613 |
-1); |
| 614 |
assert_int_equal(rc, 0); |
| 615 |
|
| 616 |
/* Test errors */ |
| 617 |
rc = match_cidr_address_list("fe80::be50:09ca::2be3", IPV6_LIST, AF_INET6); |
| 618 |
assert_int_equal(rc, -1); |
| 619 |
rc = match_cidr_address_list("fe80:x:202:b3ff:fe1e:8329", |
| 620 |
IPV6_LIST, |
| 621 |
AF_INET6); |
| 622 |
assert_int_equal(rc, -1); |
| 623 |
rc = match_cidr_address_list("fe80::202:ghfc:zzzz:1a49", |
| 624 |
IPV6_LIST, |
| 625 |
AF_INET6); |
| 626 |
assert_int_equal(rc, -1); |
| 627 |
rc = match_cidr_address_list("fe80:0000:0000:0000:0202:b3ff:fe1e:8329", |
| 628 |
"fe80:0000:0000:0000:0202:b3ff:fe1e:8329/131", |
| 629 |
AF_INET6); |
| 630 |
assert_int_equal(rc, -1); |
| 631 |
rc = match_cidr_address_list("fe80:0000:0000:0000:0202:b3ff:fe1e:8329", |
| 632 |
"fe80:0000:0000:0000:0202:b3ff:fe1e:8329//127", |
| 633 |
AF_INET6); |
| 634 |
assert_int_equal(rc, -1); |
| 635 |
|
| 636 |
/* Test invalid input with unknown host address family */ |
| 637 |
rc = match_cidr_address_list("fe80::ba67:1002:gffx:zz32", IPV6_LIST, -1); |
| 638 |
assert_int_equal(rc, -1); |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* @brief Verify the cidr_match_4 function works correctly |
| 643 |
*/ |
| 644 |
static void |
| 645 |
torture_match_cidr_v4(void **state) |
| 646 |
{ |
| 647 |
int af = AF_INET; |
| 648 |
(void)state; |
| 649 |
|
| 650 |
/* Test some matching input */ |
| 651 |
assert_true_match_cidr("192.168.1.20", "192.168.1.0", 24, af, 1); |
| 652 |
assert_true_match_cidr("172.31.5.128", "172.31.0.0", 16, af, 1); |
| 653 |
assert_true_match_cidr("10.0.0.158", "10.0.0.128", 25, af, 1); |
| 654 |
assert_true_match_cidr("192.168.255.250", "192.168.255.248", 29, af, 1); |
| 655 |
assert_true_match_cidr("122.105.209.57", "122.105.209.48", 28, af, 1); |
| 656 |
assert_true_match_cidr("192.168.100.150", "192.168.64.0", 18, af, 1); |
| 657 |
|
| 658 |
/* Test some not matching input */ |
| 659 |
assert_true_match_cidr("172.16.56.30", "172.16.48.0", 21, af, 0); |
| 660 |
assert_true_match_cidr("10.18.5.5", "10.10.4.0", 23, af, 0); |
| 661 |
assert_true_match_cidr("172.16.32.50", "172.16.0.0", 19, af, 0); |
| 662 |
assert_true_match_cidr("203.0.120.10", "203.0.112.0", 21, af, 0); |
| 663 |
assert_true_match_cidr("172.31.112.150", "172.31.96.0", 20, af, 0); |
| 664 |
assert_true_match_cidr("198.52.20.200", "198.48.0.0", 14, af, 0); |
| 665 |
} |
| 666 |
|
| 667 |
/** |
| 668 |
* @brief Verify the cidr_match_6 function works correctly |
| 669 |
*/ |
| 670 |
static void |
| 671 |
torture_match_cidr_v6(void **state) |
| 672 |
{ |
| 673 |
int af = AF_INET6; |
| 674 |
(void)state; |
| 675 |
|
| 676 |
/* Test some matching input */ |
| 677 |
assert_true_match_cidr("2001:0db8:85a3:0000:0000:8a2e:0370:7334", |
| 678 |
"2001:0db8:85a3:0000::", |
| 679 |
64, |
| 680 |
af, |
| 681 |
1); |
| 682 |
assert_true_match_cidr("2001:0db8:0000:0042:0000:8a2e:0370:7334", |
| 683 |
"2001:0db8:0000::", |
| 684 |
48, |
| 685 |
af, |
| 686 |
1); |
| 687 |
assert_true_match_cidr("fe80::8a2e:0370:7334", "fe80::", 64, af, 1); |
| 688 |
assert_true_match_cidr("fd00::8a2e:0370:7334", "fd00::", 56, af, 1); |
| 689 |
assert_true_match_cidr("fe80:0000:0000:0000:0000:0000:fe1e:32ff", |
| 690 |
"fe80::", |
| 691 |
96, |
| 692 |
af, |
| 693 |
1); |
| 694 |
assert_true_match_cidr("2001:0db8:1a2b:3c4d:5e6f:7a8b::18", |
| 695 |
"2001:0db8:1a2b:3c4d:5e6f:7a8b::", |
| 696 |
120, |
| 697 |
af, |
| 698 |
1); |
| 699 |
|
| 700 |
/* Test some not matching input */ |
| 701 |
assert_true_match_cidr("2001:0db8:1234:5678:9abc:def0:1234:5678", |
| 702 |
"2001:0db8:1234:5678::", |
| 703 |
96, |
| 704 |
af, |
| 705 |
0); |
| 706 |
assert_true_match_cidr("2001:3858:accd::", |
| 707 |
"2001:3858:abcd:eaa1::", |
| 708 |
48, |
| 709 |
af, |
| 710 |
0); |
| 711 |
assert_true_match_cidr("2001:0db8:1234:5678::ff4c", |
| 712 |
"2001:0db8:1234:5600::", |
| 713 |
110, |
| 714 |
af, |
| 715 |
0); |
| 716 |
assert_true_match_cidr("fe80::0001:af12:a1b2:c3d4:e5f7", |
| 717 |
"fe80::", |
| 718 |
64, |
| 719 |
af, |
| 720 |
0); |
| 721 |
assert_true_match_cidr("2001:0db8:84ff:ffff:ffff:ffff:ffff:fffa", |
| 722 |
"2001:0db8:8500::", |
| 723 |
80, |
| 724 |
af, |
| 725 |
0); |
| 726 |
assert_true_match_cidr("::3", "::", 127, af, 0); |
| 727 |
} |
| 728 |
|
| 729 |
int |
| 730 |
torture_run_tests(void) |
| 731 |
{ |
| 732 |
int rc; |
| 733 |
struct CMUnitTest tests[] = { |
| 734 |
cmocka_unit_test_setup_teardown( |
| 735 |
torture_config_match_localnetwork_string, |
| 736 |
setup, |
| 737 |
teardown), |
| 738 |
cmocka_unit_test_setup_teardown(torture_config_match_localnetwork_file, |
| 739 |
setup, |
| 740 |
teardown), |
| 741 |
cmocka_unit_test(torture_match_cidr_address_list_ipv4), |
| 742 |
cmocka_unit_test(torture_match_cidr_address_list_ipv6), |
| 743 |
cmocka_unit_test(torture_match_cidr_v4), |
| 744 |
cmocka_unit_test(torture_match_cidr_v6), |
| 745 |
}; |
| 746 |
|
| 747 |
ssh_init(); |
| 748 |
torture_filter_tests(tests); |
| 749 |
rc = cmocka_run_group_tests(tests, setup, teardown); |
| 750 |
ssh_finalize(); |
| 751 |
return rc; |
| 752 |
} |