| 1 |
// Comprehensive edge case testing |
| 2 |
// Tests unusual scenarios and boundary conditions |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
#include <stdlib.h> |
| 7 |
|
| 8 |
#ifndef _WIN32 |
| 9 |
#include <unistd.h> |
| 10 |
#include <sys/socket.h> |
| 11 |
#include <netdb.h> |
| 12 |
#include <arpa/inet.h> |
| 13 |
#else |
| 14 |
#include <winsock2.h> |
| 15 |
#include <ws2tcpip.h> |
| 16 |
#endif |
| 17 |
|
| 18 |
#define MAX_IP_RESULTS 10 |
| 19 |
|
| 20 |
static int |
| 21 |
resolve_host(const char *hostname, char *ipaddr_list, int max_results, |
| 22 |
int req_ai_family) |
| 23 |
{ |
| 24 |
struct addrinfo hints; |
| 25 |
struct addrinfo *result; |
| 26 |
char ipaddr[INET6_ADDRSTRLEN]; |
| 27 |
int error; |
| 28 |
|
| 29 |
// Truncate ipaddr_list to an empty string. |
| 30 |
ipaddr_list[0] = '\0'; |
| 31 |
|
| 32 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 33 |
|
| 34 |
hints.ai_family = req_ai_family; |
| 35 |
hints.ai_socktype = SOCK_STREAM; |
| 36 |
|
| 37 |
error = getaddrinfo(hostname, NULL, &hints, &result); |
| 38 |
if (error) return error; |
| 39 |
int i = 0; |
| 40 |
size_t offset = 0; |
| 41 |
for(struct addrinfo *ai = result; ai != NULL && i < max_results; ai = ai->ai_next, i++) { |
| 42 |
getnameinfo(ai->ai_addr, ai->ai_addrlen, ipaddr, sizeof(ipaddr), NULL, 0, |
| 43 |
NI_NUMERICHOST); |
| 44 |
if (offset > 0) { |
| 45 |
ipaddr_list[offset++] = ';'; |
| 46 |
} |
| 47 |
size_t len = strlen(ipaddr); |
| 48 |
strcpy(ipaddr_list + offset, ipaddr); |
| 49 |
offset += len; |
| 50 |
} |
| 51 |
freeaddrinfo(result); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
void test_case(const char *name, const char *hostname, int max_results, int family) { |
| 56 |
char ipaddr_list[INET6_ADDRSTRLEN * MAX_IP_RESULTS + MAX_IP_RESULTS]; |
| 57 |
memset(ipaddr_list, 0xFF, sizeof(ipaddr_list)); // Fill with 0xFF |
| 58 |
|
| 59 |
printf("\n--- %s ---\n", name); |
| 60 |
printf("Hostname: '%s', max_results: %d\n", hostname, max_results); |
| 61 |
|
| 62 |
int result = resolve_host(hostname, ipaddr_list, max_results, family); |
| 63 |
|
| 64 |
if (result != 0) { |
| 65 |
printf("Failed: %s\n", gai_strerror(result)); |
| 66 |
// Verify buffer was initialized to empty string |
| 67 |
if (ipaddr_list[0] != '\0') { |
| 68 |
printf("✗ ERROR: Buffer not initialized to empty string on failure\n"); |
| 69 |
} else { |
| 70 |
printf("✓ Buffer properly initialized to empty string on failure\n"); |
| 71 |
} |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
size_t len = strlen(ipaddr_list); |
| 76 |
printf("Success: '%s' (length: %zu)\n", ipaddr_list, len); |
| 77 |
|
| 78 |
// Count IPs |
| 79 |
int ip_count = (len > 0) ? 1 : 0; |
| 80 |
int semicolon_count = 0; |
| 81 |
for (size_t i = 0; i < len; i++) { |
| 82 |
if (ipaddr_list[i] == ';') { |
| 83 |
semicolon_count++; |
| 84 |
ip_count++; |
| 85 |
} |
| 86 |
} |
| 87 |
printf("IPs: %d, Semicolons: %d\n", ip_count, semicolon_count); |
| 88 |
|
| 89 |
// Validation checks |
| 90 |
int errors = 0; |
| 91 |
|
| 92 |
// Check null termination |
| 93 |
if (ipaddr_list[len] != '\0') { |
| 94 |
printf("✗ ERROR: String not null-terminated\n"); |
| 95 |
errors++; |
| 96 |
} |
| 97 |
|
| 98 |
// Check no writes beyond null terminator |
| 99 |
int found_non_ff = 0; |
| 100 |
for (size_t i = len + 1; i < sizeof(ipaddr_list); i++) { |
| 101 |
if ((unsigned char)ipaddr_list[i] != 0xFF) { |
| 102 |
found_non_ff = 1; |
| 103 |
break; |
| 104 |
} |
| 105 |
} |
| 106 |
if (found_non_ff) { |
| 107 |
printf("✗ ERROR: Wrote beyond null terminator\n"); |
| 108 |
errors++; |
| 109 |
} |
| 110 |
|
| 111 |
// Check semicolon placement |
| 112 |
if (len > 0) { |
| 113 |
if (ipaddr_list[0] == ';') { |
| 114 |
printf("✗ ERROR: Starts with semicolon\n"); |
| 115 |
errors++; |
| 116 |
} |
| 117 |
} |
| 118 |
if (len > 0 && ipaddr_list[len-1] == ';') { |
| 119 |
printf("✗ ERROR: Ends with semicolon\n"); |
| 120 |
errors++; |
| 121 |
} |
| 122 |
|
| 123 |
// Check for double semicolons |
| 124 |
if (len > 1) { |
| 125 |
for (size_t i = 0; i < len - 1; i++) { |
| 126 |
if (ipaddr_list[i] == ';' && ipaddr_list[i+1] == ';') { |
| 127 |
printf("✗ ERROR: Contains ';;'\n"); |
| 128 |
errors++; |
| 129 |
break; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// Check IP count matches semicolons |
| 135 |
if (ip_count != semicolon_count + 1 && len > 0) { |
| 136 |
printf("✗ ERROR: IP count mismatch (expected %d, got %d)\n", |
| 137 |
semicolon_count + 1, ip_count); |
| 138 |
errors++; |
| 139 |
} |
| 140 |
|
| 141 |
if (errors == 0) { |
| 142 |
printf("✓ All validations passed\n"); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
int main() { |
| 147 |
printf("=== Comprehensive Edge Case Testing ===\n"); |
| 148 |
|
| 149 |
// Edge case: max_results boundaries |
| 150 |
test_case("Max results = 0", "localhost", 0, AF_INET); |
| 151 |
test_case("Max results = 1", "localhost", 1, AF_INET); |
| 152 |
test_case("Max results = MAX_IP_RESULTS", "google.com", MAX_IP_RESULTS, AF_UNSPEC); |
| 153 |
|
| 154 |
// Edge case: Different IP types |
| 155 |
test_case("IPv4 only", "google.com", 10, AF_INET); |
| 156 |
test_case("IPv6 only", "google.com", 10, AF_INET6); |
| 157 |
test_case("Both IPv4 and IPv6", "google.com", 10, AF_UNSPEC); |
| 158 |
|
| 159 |
// Edge case: Numeric IPs (should not fail) |
| 160 |
test_case("Numeric IPv4", "8.8.8.8", 10, AF_INET); |
| 161 |
test_case("Numeric IPv6 full", "2001:4860:4860::8888", 10, AF_INET6); |
| 162 |
test_case("Numeric IPv6 compressed", "::1", 10, AF_INET6); |
| 163 |
test_case("Numeric IPv4 mapped IPv6", "::ffff:8.8.8.8", 10, AF_INET6); |
| 164 |
|
| 165 |
// Edge case: Special hostnames |
| 166 |
test_case("Localhost", "localhost", 10, AF_INET); |
| 167 |
test_case("Localhost IPv6", "localhost", 10, AF_INET6); |
| 168 |
|
| 169 |
// Edge case: Non-existent hostnames |
| 170 |
test_case("Invalid hostname", "this-definitely-does-not-exist.invalid", 10, AF_INET); |
| 171 |
|
| 172 |
printf("\n=== All Edge Case Tests Complete ===\n"); |
| 173 |
|
| 174 |
return 0; |
| 175 |
} |