| 1 |
// Test program for resolve_host function |
| 2 |
// Compile: gcc -g -Wall -o test_resolve_host test_resolve_host.c -DTEST_RESOLVE_HOST |
| 3 |
// Run: ./test_resolve_host |
| 4 |
|
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
#include <stdlib.h> |
| 8 |
|
| 9 |
#ifndef _WIN32 |
| 10 |
#include <unistd.h> |
| 11 |
#include <sys/socket.h> |
| 12 |
#include <netdb.h> |
| 13 |
#include <arpa/inet.h> |
| 14 |
#else |
| 15 |
#include <winsock2.h> |
| 16 |
#include <ws2tcpip.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#define MAX_IP_RESULTS 10 |
| 20 |
|
| 21 |
// Copy the resolve_host function from pacparser.c |
| 22 |
static int |
| 23 |
resolve_host(const char *hostname, char *ipaddr_list, int max_results, |
| 24 |
int req_ai_family) |
| 25 |
{ |
| 26 |
struct addrinfo hints; |
| 27 |
struct addrinfo *result; |
| 28 |
char ipaddr[INET6_ADDRSTRLEN]; |
| 29 |
int error; |
| 30 |
|
| 31 |
// Truncate ipaddr_list to an empty string. |
| 32 |
ipaddr_list[0] = '\0'; |
| 33 |
|
| 34 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 35 |
|
| 36 |
hints.ai_family = req_ai_family; |
| 37 |
hints.ai_socktype = SOCK_STREAM; |
| 38 |
|
| 39 |
error = getaddrinfo(hostname, NULL, &hints, &result); |
| 40 |
if (error) return error; |
| 41 |
int i = 0; |
| 42 |
size_t offset = 0; |
| 43 |
for(struct addrinfo *ai = result; ai != NULL && i < max_results; ai = ai->ai_next, i++) { |
| 44 |
getnameinfo(ai->ai_addr, ai->ai_addrlen, ipaddr, sizeof(ipaddr), NULL, 0, |
| 45 |
NI_NUMERICHOST); |
| 46 |
if (offset > 0) { |
| 47 |
ipaddr_list[offset++] = ';'; |
| 48 |
} |
| 49 |
size_t len = strlen(ipaddr); |
| 50 |
strcpy(ipaddr_list + offset, ipaddr); |
| 51 |
offset += len; |
| 52 |
} |
| 53 |
freeaddrinfo(result); |
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
// Test harness |
| 58 |
typedef struct { |
| 59 |
const char *name; |
| 60 |
const char *hostname; |
| 61 |
int max_results; |
| 62 |
int ai_family; |
| 63 |
int expect_success; |
| 64 |
const char *description; |
| 65 |
} test_case_t; |
| 66 |
|
| 67 |
void run_test(test_case_t *tc) { |
| 68 |
char ipaddr_list[INET6_ADDRSTRLEN * MAX_IP_RESULTS + MAX_IP_RESULTS]; |
| 69 |
|
| 70 |
// Fill with sentinel pattern to detect buffer issues |
| 71 |
memset(ipaddr_list, 0xAA, sizeof(ipaddr_list)); |
| 72 |
|
| 73 |
printf("\n--- Test: %s ---\n", tc->name); |
| 74 |
printf("Description: %s\n", tc->description); |
| 75 |
printf("Hostname: %s, max_results: %d, family: %s\n", |
| 76 |
tc->hostname, tc->max_results, |
| 77 |
tc->ai_family == AF_INET ? "AF_INET" : |
| 78 |
tc->ai_family == AF_INET6 ? "AF_INET6" : "AF_UNSPEC"); |
| 79 |
|
| 80 |
int result = resolve_host(tc->hostname, ipaddr_list, tc->max_results, tc->ai_family); |
| 81 |
|
| 82 |
if (result != 0) { |
| 83 |
printf("Result: FAILED (error code: %d, %s)\n", result, gai_strerror(result)); |
| 84 |
if (!tc->expect_success) { |
| 85 |
printf("✓ Expected failure\n"); |
| 86 |
} else { |
| 87 |
printf("✗ Unexpected failure\n"); |
| 88 |
} |
| 89 |
} else { |
| 90 |
printf("Result: SUCCESS\n"); |
| 91 |
printf("IP addresses: '%s'\n", ipaddr_list); |
| 92 |
|
| 93 |
// Verify results |
| 94 |
if (!tc->expect_success) { |
| 95 |
printf("✗ Expected failure but got success\n"); |
| 96 |
} else { |
| 97 |
printf("✓ Expected success\n"); |
| 98 |
|
| 99 |
// Check for buffer overruns (sentinel bytes after the null terminator) |
| 100 |
size_t len = strlen(ipaddr_list); |
| 101 |
int overrun = 0; |
| 102 |
for (size_t i = len + 1; i < sizeof(ipaddr_list); i++) { |
| 103 |
if (ipaddr_list[i] != (char)0xAA) { |
| 104 |
overrun = 1; |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
if (overrun) { |
| 109 |
printf("✗ Buffer overrun detected!\n"); |
| 110 |
} else { |
| 111 |
printf("✓ No buffer overrun\n"); |
| 112 |
} |
| 113 |
|
| 114 |
// Count semicolons |
| 115 |
int semicolons = 0; |
| 116 |
for (size_t i = 0; i < len; i++) { |
| 117 |
if (ipaddr_list[i] == ';') semicolons++; |
| 118 |
} |
| 119 |
printf(" Number of IPs: %d (semicolons: %d)\n", semicolons + 1, semicolons); |
| 120 |
|
| 121 |
// Verify no double semicolons |
| 122 |
if (strstr(ipaddr_list, ";;")) { |
| 123 |
printf("✗ Double semicolons found!\n"); |
| 124 |
} else { |
| 125 |
printf("✓ No double semicolons\n"); |
| 126 |
} |
| 127 |
|
| 128 |
// Verify doesn't start or end with semicolon |
| 129 |
if (len > 0 && (ipaddr_list[0] == ';' || ipaddr_list[len-1] == ';')) { |
| 130 |
printf("✗ Starts or ends with semicolon!\n"); |
| 131 |
} else { |
| 132 |
printf("✓ Doesn't start/end with semicolon\n"); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
int main() { |
| 139 |
printf("=== resolve_host() Test Suite ===\n"); |
| 140 |
|
| 141 |
test_case_t tests[] = { |
| 142 |
// Basic tests |
| 143 |
{ |
| 144 |
.name = "IPv4 localhost", |
| 145 |
.hostname = "localhost", |
| 146 |
.max_results = 10, |
| 147 |
.ai_family = AF_INET, |
| 148 |
.expect_success = 1, |
| 149 |
.description = "Resolve localhost to IPv4 (should get 127.0.0.1)" |
| 150 |
}, |
| 151 |
{ |
| 152 |
.name = "IPv6 localhost", |
| 153 |
.hostname = "localhost", |
| 154 |
.max_results = 10, |
| 155 |
.ai_family = AF_INET6, |
| 156 |
.expect_success = 1, |
| 157 |
.description = "Resolve localhost to IPv6 (should get ::1)" |
| 158 |
}, |
| 159 |
{ |
| 160 |
.name = "Any family localhost", |
| 161 |
.hostname = "localhost", |
| 162 |
.max_results = 10, |
| 163 |
.ai_family = AF_UNSPEC, |
| 164 |
.expect_success = 1, |
| 165 |
.description = "Resolve localhost to any family (may get both IPv4 and IPv6)" |
| 166 |
}, |
| 167 |
|
| 168 |
// Edge cases |
| 169 |
{ |
| 170 |
.name = "Max results = 1", |
| 171 |
.hostname = "localhost", |
| 172 |
.max_results = 1, |
| 173 |
.ai_family = AF_UNSPEC, |
| 174 |
.expect_success = 1, |
| 175 |
.description = "Limit to 1 result (should not have semicolons)" |
| 176 |
}, |
| 177 |
{ |
| 178 |
.name = "Max results = 2", |
| 179 |
.hostname = "localhost", |
| 180 |
.max_results = 2, |
| 181 |
.ai_family = AF_UNSPEC, |
| 182 |
.expect_success = 1, |
| 183 |
.description = "Limit to 2 results" |
| 184 |
}, |
| 185 |
{ |
| 186 |
.name = "Invalid hostname", |
| 187 |
.hostname = "this.hostname.definitely.does.not.exist.invalid", |
| 188 |
.max_results = 10, |
| 189 |
.ai_family = AF_INET, |
| 190 |
.expect_success = 0, |
| 191 |
.description = "Should fail with invalid hostname" |
| 192 |
}, |
| 193 |
{ |
| 194 |
.name = "Empty hostname", |
| 195 |
.hostname = "", |
| 196 |
.max_results = 10, |
| 197 |
.ai_family = AF_INET, |
| 198 |
.expect_success = 0, |
| 199 |
.description = "Should fail with empty hostname" |
| 200 |
}, |
| 201 |
|
| 202 |
// Real-world tests (may fail if no network) |
| 203 |
{ |
| 204 |
.name = "google.com IPv4", |
| 205 |
.hostname = "google.com", |
| 206 |
.max_results = 10, |
| 207 |
.ai_family = AF_INET, |
| 208 |
.expect_success = 1, |
| 209 |
.description = "Resolve google.com to IPv4 (may return multiple IPs)" |
| 210 |
}, |
| 211 |
{ |
| 212 |
.name = "google.com IPv6", |
| 213 |
.hostname = "google.com", |
| 214 |
.max_results = 10, |
| 215 |
.ai_family = AF_INET6, |
| 216 |
.expect_success = 1, |
| 217 |
.description = "Resolve google.com to IPv6 (may fail if no IPv6)" |
| 218 |
}, |
| 219 |
|
| 220 |
// Numeric IP tests |
| 221 |
{ |
| 222 |
.name = "Numeric IPv4", |
| 223 |
.hostname = "127.0.0.1", |
| 224 |
.max_results = 10, |
| 225 |
.ai_family = AF_INET, |
| 226 |
.expect_success = 1, |
| 227 |
.description = "Numeric IPv4 address (should return itself)" |
| 228 |
}, |
| 229 |
{ |
| 230 |
.name = "Numeric IPv6", |
| 231 |
.hostname = "::1", |
| 232 |
.max_results = 10, |
| 233 |
.ai_family = AF_INET6, |
| 234 |
.expect_success = 1, |
| 235 |
.description = "Numeric IPv6 address (should return itself)" |
| 236 |
}, |
| 237 |
}; |
| 238 |
|
| 239 |
int num_tests = sizeof(tests) / sizeof(tests[0]); |
| 240 |
|
| 241 |
for (int i = 0; i < num_tests; i++) { |
| 242 |
run_test(&tests[i]); |
| 243 |
} |
| 244 |
|
| 245 |
printf("\n=== Test Suite Complete ===\n"); |
| 246 |
printf("Total tests run: %d\n", num_tests); |
| 247 |
printf("\nNote: Some tests (google.com, IPv6) may fail depending on network availability.\n"); |
| 248 |
printf("This is expected and not a bug in resolve_host().\n"); |
| 249 |
|
| 250 |
return 0; |
| 251 |
} |