| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define _GNU_SOURCE |
| 20 |
#include"config.h" |
| 21 |
#include<assert.h> |
| 22 |
#include<errno.h> |
| 23 |
#include<fcntl.h> |
| 24 |
#include<inttypes.h> |
| 25 |
#include<pthread.h> |
| 26 |
#include<stdio.h> |
| 27 |
#include<stdlib.h> |
| 28 |
|
| 29 |
#include<check.h> |
| 30 |
#include<libnet.h> |
| 31 |
#include<pcap.h> |
| 32 |
|
| 33 |
#include"arping.h" |
| 34 |
|
| 35 |
#ifndef ETH_ALEN |
| 36 |
#define ETH_ALEN 6 |
| 37 |
#endif |
| 38 |
|
| 39 |
extern libnet_t* libnet; |
| 40 |
extern int mock_libnet_lo_ok; |
| 41 |
extern int mock_libnet_null_ok; |
| 42 |
|
| 43 |
void strip_newline(char* s); |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static void |
| 50 |
xclose(int* fd) |
| 51 |
{ |
| 52 |
if (0 > close(*fd)) { |
| 53 |
fprintf(stderr, "close(%d): %s", *fd, strerror(errno)); |
| 54 |
*fd = -1; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
struct captured_output { |
| 59 |
int saved_fd; |
| 60 |
int fno; |
| 61 |
int reader_fd; |
| 62 |
char* buffer; |
| 63 |
size_t bufsize; |
| 64 |
pthread_t thread; |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static void* |
| 75 |
read_main(void* p) |
| 76 |
{ |
| 77 |
struct captured_output* out = p; |
| 78 |
char *cur = out->buffer; |
| 79 |
|
| 80 |
for (;;) { |
| 81 |
const size_t to_read = out->bufsize - (size_t)(cur - out->buffer); |
| 82 |
const ssize_t n = read(out->reader_fd, cur, to_read); |
| 83 |
if (n > 0) { |
| 84 |
cur += n; |
| 85 |
} |
| 86 |
if (n == 0) { |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
static struct captured_output* |
| 103 |
capture(int fd) |
| 104 |
{ |
| 105 |
struct captured_output* out; |
| 106 |
|
| 107 |
out = calloc(1, sizeof(struct captured_output)); |
| 108 |
fail_if(out == NULL); |
| 109 |
|
| 110 |
out->fno = fd; |
| 111 |
out->saved_fd = dup(fd); |
| 112 |
out->bufsize = 1024*100; |
| 113 |
out->buffer = calloc(1, out->bufsize); |
| 114 |
|
| 115 |
fail_if(0 > out->saved_fd); |
| 116 |
fail_if(out->buffer == NULL); |
| 117 |
|
| 118 |
|
| 119 |
int fds[2]; |
| 120 |
fail_if(0 > pipe(fds)); |
| 121 |
fail_if(0 > dup2(fds[1], fd)); |
| 122 |
out->reader_fd = fds[0]; |
| 123 |
xclose(&fds[1]); |
| 124 |
|
| 125 |
fail_if(pthread_create(&out->thread, NULL, read_main, (void*)out)); |
| 126 |
return out; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
static void |
| 135 |
stop_capture(struct captured_output* out) |
| 136 |
{ |
| 137 |
fail_if(0 > dup2(out->saved_fd, out->fno)); |
| 138 |
xclose(&out->saved_fd); |
| 139 |
fail_if(pthread_join(out->thread, NULL)); |
| 140 |
xclose(&out->reader_fd); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static void |
| 149 |
uncapture(struct captured_output* out) |
| 150 |
{ |
| 151 |
free(out->buffer); |
| 152 |
out->buffer = NULL; |
| 153 |
free(out); |
| 154 |
} |
| 155 |
|
| 156 |
static uint8_t* |
| 157 |
mkpacket(struct pcap_pkthdr* pkthdr) |
| 158 |
{ |
| 159 |
uint8_t* packet = calloc(1, 1500); |
| 160 |
fail_if(packet == NULL); |
| 161 |
|
| 162 |
struct libnet_802_3_hdr* heth; |
| 163 |
struct libnet_arp_hdr* harp; |
| 164 |
|
| 165 |
|
| 166 |
heth = (void*)packet; |
| 167 |
memcpy(heth->_802_3_dhost, "\x11\x22\x33\x44\x55\x66", 6); |
| 168 |
memcpy(heth->_802_3_shost, "\x77\x88\x99\xaa\xbb\xcc", 6); |
| 169 |
heth->_802_3_len = 0; |
| 170 |
|
| 171 |
|
| 172 |
harp = (void*)((char*)heth + LIBNET_ETH_H); |
| 173 |
harp->ar_hln = 6; |
| 174 |
harp->ar_pln = 4; |
| 175 |
harp->ar_hrd = htons(ARPHRD_ETHER); |
| 176 |
harp->ar_op = htons(ARPOP_REPLY); |
| 177 |
harp->ar_pro = htons(ETHERTYPE_IP); |
| 178 |
|
| 179 |
memcpy((char*)harp + LIBNET_ARP_H, heth->_802_3_shost, 6); |
| 180 |
memcpy((char*)harp + LIBNET_ARP_H + harp->ar_hln, &dstip, 4); |
| 181 |
|
| 182 |
memcpy((char*)harp + LIBNET_ARP_H |
| 183 |
+ harp->ar_hln |
| 184 |
+ harp->ar_pln, heth->_802_3_dhost, 6); |
| 185 |
memcpy((char*)harp + LIBNET_ARP_H |
| 186 |
+ harp->ar_hln |
| 187 |
+ harp->ar_pln |
| 188 |
+ harp->ar_hln, &srcip, 4); |
| 189 |
|
| 190 |
pkthdr->ts.tv_sec = time(NULL); |
| 191 |
pkthdr->ts.tv_usec = 0; |
| 192 |
pkthdr->len = 60; |
| 193 |
pkthdr->caplen = 60; |
| 194 |
|
| 195 |
return packet; |
| 196 |
} |
| 197 |
|
| 198 |
static void |
| 199 |
dump_packet(uint8_t* packet, uint32_t len) |
| 200 |
{ |
| 201 |
uint32_t c; |
| 202 |
for (c = 0; c < len; c++) { |
| 203 |
fprintf(stderr, "0x%.2x, ", (int)packet[c]); |
| 204 |
if (!((c+1) % 10)) { |
| 205 |
fprintf(stderr, "\n"); |
| 206 |
} |
| 207 |
} |
| 208 |
fprintf(stderr, "\n"); |
| 209 |
} |
| 210 |
|
| 211 |
START_TEST(test_mkpacket) |
| 212 |
{ |
| 213 |
uint8_t correct_packet[] = { |
| 214 |
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, |
| 215 |
0xbb, 0xcc, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, |
| 216 |
0x00, 0x02, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0x12, 0x34, |
| 217 |
0x56, 0x78, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x87, 0x65, |
| 218 |
0x43, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 219 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 220 |
}; |
| 221 |
struct pcap_pkthdr pkthdr; |
| 222 |
dstip = htonl(0x12345678); |
| 223 |
srcip = htonl(0x87654321); |
| 224 |
|
| 225 |
uint8_t* packet = mkpacket(&pkthdr); |
| 226 |
fail_if(packet == NULL); |
| 227 |
fail_unless(pkthdr.caplen == 60); |
| 228 |
if (memcmp(packet, correct_packet, pkthdr.caplen)) { |
| 229 |
dump_packet(packet, pkthdr.caplen); |
| 230 |
} |
| 231 |
fail_unless(!memcmp(packet, correct_packet, pkthdr.caplen)); |
| 232 |
free(packet); |
| 233 |
} END_TEST |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
START_TEST(pingip_uninteresting_packet) |
| 238 |
{ |
| 239 |
struct pcap_pkthdr pkthdr; |
| 240 |
uint8_t* packet; |
| 241 |
struct libnet_arp_hdr* harp; |
| 242 |
|
| 243 |
unsigned int prev_numrecvd = numrecvd; |
| 244 |
struct captured_output* sout; |
| 245 |
|
| 246 |
|
| 247 |
packet = calloc(1, 1500); |
| 248 |
sout = capture(STDOUT_FILENO); |
| 249 |
pingip_recv(NULL, &pkthdr, packet); |
| 250 |
stop_capture(sout); |
| 251 |
fail_unless(strlen(sout->buffer) == 0); |
| 252 |
fail_unless(prev_numrecvd == numrecvd); |
| 253 |
uncapture(sout); |
| 254 |
free(packet); |
| 255 |
|
| 256 |
|
| 257 |
packet = mkpacket(&pkthdr); |
| 258 |
harp = (void*)((char*)packet + LIBNET_ETH_H); |
| 259 |
harp->ar_pro = 0; |
| 260 |
sout = capture(STDOUT_FILENO); |
| 261 |
pingip_recv(NULL, &pkthdr, packet); |
| 262 |
stop_capture(sout); |
| 263 |
fail_unless(prev_numrecvd == numrecvd); |
| 264 |
fail_unless(strlen(sout->buffer) == 0); |
| 265 |
uncapture(sout); |
| 266 |
free(packet); |
| 267 |
|
| 268 |
|
| 269 |
packet = mkpacket(&pkthdr); |
| 270 |
harp = (void*)((char*)packet + LIBNET_ETH_H); |
| 271 |
harp->ar_hrd = 0; |
| 272 |
sout = capture(STDOUT_FILENO); |
| 273 |
pingip_recv(NULL, &pkthdr, packet); |
| 274 |
stop_capture(sout); |
| 275 |
fail_unless(prev_numrecvd == numrecvd); |
| 276 |
fail_unless(strlen(sout->buffer) == 0); |
| 277 |
uncapture(sout); |
| 278 |
free(packet); |
| 279 |
|
| 280 |
|
| 281 |
if (0) { |
| 282 |
uint32_t wrongip = 123; |
| 283 |
packet = mkpacket(&pkthdr); |
| 284 |
harp = (void*)((char*)packet + LIBNET_ETH_H); |
| 285 |
memcpy((char*)harp + harp->ar_hln + LIBNET_ARP_H, &wrongip, 4); |
| 286 |
sout = capture(STDOUT_FILENO); |
| 287 |
pingip_recv(NULL, &pkthdr, packet); |
| 288 |
stop_capture(sout); |
| 289 |
fail_unless(prev_numrecvd == numrecvd); |
| 290 |
fail_unless(strlen(sout->buffer) == 0); |
| 291 |
uncapture(sout); |
| 292 |
free(packet); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
packet = mkpacket(&pkthdr); |
| 297 |
pkthdr.caplen = pkthdr.len = 41; |
| 298 |
sout = capture(STDOUT_FILENO); |
| 299 |
pingip_recv(NULL, &pkthdr, packet); |
| 300 |
stop_capture(sout); |
| 301 |
fail_unless(prev_numrecvd == numrecvd); |
| 302 |
fail_unless(strlen(sout->buffer) == 0); |
| 303 |
uncapture(sout); |
| 304 |
free(packet); |
| 305 |
|
| 306 |
|
| 307 |
packet = mkpacket(&pkthdr); |
| 308 |
pkthdr.caplen = 41; |
| 309 |
sout = capture(STDOUT_FILENO); |
| 310 |
pingip_recv(NULL, &pkthdr, packet); |
| 311 |
stop_capture(sout); |
| 312 |
fail_unless(prev_numrecvd == numrecvd); |
| 313 |
fail_unless(strlen(sout->buffer) == 0); |
| 314 |
uncapture(sout); |
| 315 |
free(packet); |
| 316 |
|
| 317 |
|
| 318 |
{ |
| 319 |
uint8_t packet[] = { |
| 320 |
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, |
| 321 |
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, |
| 322 |
0x00, 0x00, |
| 323 |
0x00, 0x01, |
| 324 |
0x08, 0x00, |
| 325 |
0x04, 0x04, |
| 326 |
0x00, 0x02, |
| 327 |
|
| 328 |
0x77, 0x88, 0x99, 0xaa, |
| 329 |
0x12, 0x34, 0x56, 0x78, |
| 330 |
|
| 331 |
0x11, 0x22, 0x33, 0x44, |
| 332 |
0x87, 0x65, 0x43, 0x21, |
| 333 |
|
| 334 |
0x00, 0x00, 0x00, 0x00, |
| 335 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 336 |
0x00, 0x00, 0x00, 0x00, |
| 337 |
0x00, 0x00, 0x00, 0x00, |
| 338 |
0x00, 0x00, 0x00, 0x00, |
| 339 |
0x6f, 0xa8, 0x58, 0x63, |
| 340 |
}; |
| 341 |
pkthdr.len = 60; |
| 342 |
pkthdr.caplen = 60; |
| 343 |
sout = capture(STDOUT_FILENO); |
| 344 |
pingip_recv(NULL, &pkthdr, packet); |
| 345 |
stop_capture(sout); |
| 346 |
fail_unless(strlen(sout->buffer) == 0, sout->buffer); |
| 347 |
fail_unless(prev_numrecvd == numrecvd); |
| 348 |
uncapture(sout); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
packet = mkpacket(&pkthdr); |
| 353 |
((struct libnet_arp_hdr*)((char*)packet + LIBNET_ETH_H))->ar_pln = 6; |
| 354 |
sout = capture(STDOUT_FILENO); |
| 355 |
pingip_recv(NULL, &pkthdr, packet); |
| 356 |
stop_capture(sout); |
| 357 |
fail_unless(prev_numrecvd == numrecvd); |
| 358 |
fail_unless(strlen(sout->buffer) == 0); |
| 359 |
uncapture(sout); |
| 360 |
free(packet); |
| 361 |
} END_TEST |
| 362 |
|
| 363 |
|
| 364 |
START_TEST(pingip_interesting_packet) |
| 365 |
{ |
| 366 |
struct pcap_pkthdr pkthdr; |
| 367 |
extern uint8_t srcmac[ETH_ALEN]; |
| 368 |
memcpy(srcmac, "\x11\x22\x33\x44\x55\x66", ETH_ALEN); |
| 369 |
uint8_t packet[] = { |
| 370 |
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, |
| 371 |
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, |
| 372 |
0x00, 0x00, |
| 373 |
0x00, 0x01, |
| 374 |
0x08, 0x00, |
| 375 |
0x06, 0x04, |
| 376 |
0x00, 0x02, |
| 377 |
|
| 378 |
0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, |
| 379 |
0x12, 0x34, 0x56, 0x78, |
| 380 |
|
| 381 |
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, |
| 382 |
0x87, 0x65, 0x43, 0x21, |
| 383 |
|
| 384 |
0x00, 0x00, 0x00, 0x00, |
| 385 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 386 |
0x6f, 0xa8, 0x58, 0x63, |
| 387 |
}; |
| 388 |
numrecvd = 0; |
| 389 |
unsigned int prev_numrecvd = numrecvd; |
| 390 |
|
| 391 |
dstip = htonl(0x12345678); |
| 392 |
|
| 393 |
pkthdr.ts.tv_sec = time(NULL); |
| 394 |
pkthdr.ts.tv_usec = 0; |
| 395 |
pkthdr.len = 60; |
| 396 |
pkthdr.caplen = 60; |
| 397 |
|
| 398 |
struct captured_output *sout; |
| 399 |
|
| 400 |
|
| 401 |
const char* correct0 = |
| 402 |
"60 bytes from 77:88:99:aa:bb:cc (18.52.86.120): " |
| 403 |
"index=0 time="; |
| 404 |
sout = capture(STDOUT_FILENO); |
| 405 |
pingip_recv(NULL, &pkthdr, packet); |
| 406 |
stop_capture(sout); |
| 407 |
|
| 408 |
char* emsg = NULL; |
| 409 |
fail_unless(0 < asprintf(&emsg, "Captured: <%s> (%zd), want <%s> %zd\n", |
| 410 |
sout->buffer, strlen(sout->buffer), |
| 411 |
correct0, strlen(correct0))); |
| 412 |
fail_unless(!strncmp(sout->buffer, correct0, strlen(correct0)), emsg); |
| 413 |
uncapture(sout); |
| 414 |
free(emsg); |
| 415 |
|
| 416 |
|
| 417 |
ck_assert_int_eq(numrecvd, prev_numrecvd + 1); |
| 418 |
|
| 419 |
pingip_recv(NULL, &pkthdr, packet); |
| 420 |
|
| 421 |
ck_assert_int_eq(numrecvd,prev_numrecvd + 2); |
| 422 |
} END_TEST |
| 423 |
|
| 424 |
START_TEST(strip_newline_test) |
| 425 |
{ |
| 426 |
const char *tests[][2] = { |
| 427 |
{"", ""}, |
| 428 |
{"\n", ""}, |
| 429 |
{"\n\n\n", ""}, |
| 430 |
{"foo", "foo"}, |
| 431 |
{"foo\n", "foo"}, |
| 432 |
{"foo\n\n\n", "foo"}, |
| 433 |
{NULL, NULL}, |
| 434 |
}; |
| 435 |
int c; |
| 436 |
for (c = 0; tests[c][0]; c++){ |
| 437 |
char buf[128]; |
| 438 |
xsnprintf(buf, sizeof(buf), "%s", tests[c][0]); |
| 439 |
strip_newline(buf); |
| 440 |
fail_unless(!strcmp(buf, tests[c][1])); |
| 441 |
} |
| 442 |
} END_TEST |
| 443 |
|
| 444 |
START_TEST(get_mac_addr_success) |
| 445 |
{ |
| 446 |
const char *tests[][2] = { |
| 447 |
|
| 448 |
{"0000.0000.0000", "\x00\x00\x00\x00\x00\x00"}, |
| 449 |
{"00:00:00:00:00:00", "\x00\x00\x00\x00\x00\x00"}, |
| 450 |
{"00-00-00-00-00-00", "\x00\x00\x00\x00\x00\x00"}, |
| 451 |
|
| 452 |
|
| 453 |
{"FFFF.FFFF.FFFF", "\xFF\xFF\xFF\xFF\xFF\xFF"}, |
| 454 |
{"FF:FF:FF:FF:FF:FF", "\xFF\xFF\xFF\xFF\xFF\xFF"}, |
| 455 |
{"FF-FF-FF-FF-FF-FF", "\xFF\xFF\xFF\xFF\xFF\xFF"}, |
| 456 |
|
| 457 |
|
| 458 |
{"1122.3344.5566", "\x11\x22\x33\x44\x55\x66"}, |
| 459 |
{"11:22:33:44:55:66", "\x11\x22\x33\x44\x55\x66"}, |
| 460 |
{"11-22-33-44-55-66", "\x11\x22\x33\x44\x55\x66"}, |
| 461 |
|
| 462 |
|
| 463 |
{"1100.0000.5566", "\x11\x00\x00\x00\x55\x66"}, |
| 464 |
{"11:00:00:00:55:66", "\x11\x00\x00\x00\x55\x66"}, |
| 465 |
{"11-00-00-00-55-66", "\x11\x00\x00\x00\x55\x66"}, |
| 466 |
{NULL, NULL}, |
| 467 |
}; |
| 468 |
int c; |
| 469 |
for (c = 0; tests[c][0]; c++){ |
| 470 |
uint8_t buf[6]; |
| 471 |
fail_unless(get_mac_addr(tests[c][0], buf)); |
| 472 |
fail_unless(!memcmp(buf, tests[c][1], 6)); |
| 473 |
} |
| 474 |
} END_TEST |
| 475 |
|
| 476 |
START_TEST(get_mac_addr_fail) |
| 477 |
{ |
| 478 |
const char *tests[] = { |
| 479 |
"", |
| 480 |
"blaha", |
| 481 |
"11:22:33:44:55", |
| 482 |
"11:22:33:44:55:zz", |
| 483 |
"00:11:22:33:44:55:66", |
| 484 |
"00:11:22:33:44:55garbage", |
| 485 |
"112233:44:55:66:77:88", |
| 486 |
"123.456.789.0ab", |
| 487 |
"100:11:22:33:44:55", |
| 488 |
NULL, |
| 489 |
}; |
| 490 |
int c; |
| 491 |
for (c = 0; tests[c]; c++){ |
| 492 |
uint8_t buf[6]; |
| 493 |
fail_if(get_mac_addr(tests[c], buf)); |
| 494 |
} |
| 495 |
} END_TEST |
| 496 |
|
| 497 |
START_TEST(is_mac_addr_test) |
| 498 |
{ |
| 499 |
struct { |
| 500 |
const char* in; |
| 501 |
int expected; |
| 502 |
} tests[] = { |
| 503 |
{"00:11:22:33:44:55", 1}, |
| 504 |
{"00-11-22-33-44-55", 1}, |
| 505 |
{"1234.5678.90ab", 1}, |
| 506 |
{"00:11:22:33:44:55:66", 0}, |
| 507 |
{"00:11:22:33:44:55garbage", 0}, |
| 508 |
{"112233:44:55:66:77:88", 0}, |
| 509 |
{"123.456.789.0ab", 0}, |
| 510 |
{"100:11:22:33:44:55", 0}, |
| 511 |
{"2001:db8::1", 0}, |
| 512 |
{"1.2.3.4:80", 0}, |
| 513 |
{"", 0}, |
| 514 |
{"blaha", 0}, |
| 515 |
{NULL, 0}, |
| 516 |
}; |
| 517 |
int c; |
| 518 |
for (c = 0; tests[c].in; c++){ |
| 519 |
if (tests[c].expected) { |
| 520 |
fail_unless(is_mac_addr(tests[c].in)); |
| 521 |
} else { |
| 522 |
fail_if(is_mac_addr(tests[c].in)); |
| 523 |
} |
| 524 |
} |
| 525 |
} END_TEST |
| 526 |
|
| 527 |
START_TEST(badarg_maxcount_neg) |
| 528 |
{ |
| 529 |
char* args[] = { |
| 530 |
"arping", |
| 531 |
"-c", "-1", |
| 532 |
"-h", |
| 533 |
"dummy", |
| 534 |
NULL |
| 535 |
}; |
| 536 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 537 |
} |
| 538 |
|
| 539 |
START_TEST(badarg_maxcount_nan) |
| 540 |
{ |
| 541 |
char* args[] = { |
| 542 |
"arping", |
| 543 |
"-c", "number", |
| 544 |
"-h", |
| 545 |
"dummy", |
| 546 |
NULL |
| 547 |
}; |
| 548 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 549 |
} |
| 550 |
|
| 551 |
START_TEST(badarg_maxcount_range) |
| 552 |
{ |
| 553 |
char* args[] = { |
| 554 |
"arping", |
| 555 |
"-c", "100000000000000000000000000000", |
| 556 |
"-h", |
| 557 |
"dummy", |
| 558 |
NULL |
| 559 |
}; |
| 560 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 561 |
} |
| 562 |
|
| 563 |
START_TEST(arg_maxcount_good) |
| 564 |
{ |
| 565 |
char* args[] = { |
| 566 |
"arping", |
| 567 |
"-c", "1", |
| 568 |
"-h", |
| 569 |
"dummy", |
| 570 |
NULL |
| 571 |
}; |
| 572 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 573 |
} |
| 574 |
|
| 575 |
START_TEST(arg_maxcount_hex) |
| 576 |
{ |
| 577 |
char* args[] = { |
| 578 |
"arping", |
| 579 |
"-c", "0xa", |
| 580 |
"-h", |
| 581 |
"dummy", |
| 582 |
NULL |
| 583 |
}; |
| 584 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 585 |
} |
| 586 |
|
| 587 |
START_TEST(badarg_packetwait_neg) |
| 588 |
{ |
| 589 |
char* args[] = { |
| 590 |
"arping", |
| 591 |
"-W", "-1", |
| 592 |
"-h", |
| 593 |
"dummy", |
| 594 |
NULL |
| 595 |
}; |
| 596 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 597 |
} |
| 598 |
|
| 599 |
START_TEST(badarg_packetwait_nan) |
| 600 |
{ |
| 601 |
char* args[] = { |
| 602 |
"arping", |
| 603 |
"-W", "huteosu", |
| 604 |
"-h", |
| 605 |
"dummy", |
| 606 |
NULL |
| 607 |
}; |
| 608 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 609 |
} |
| 610 |
|
| 611 |
START_TEST(badarg_packetwait_range) |
| 612 |
{ |
| 613 |
char* args[] = { |
| 614 |
"arping", |
| 615 |
"-W", "10000000000000000000000000000000000000000", |
| 616 |
"-h", |
| 617 |
"dummy", |
| 618 |
NULL |
| 619 |
}; |
| 620 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 621 |
} |
| 622 |
|
| 623 |
START_TEST(arg_packetwait_good) |
| 624 |
{ |
| 625 |
char* args[] = { |
| 626 |
"arping", |
| 627 |
"-W", "1.2", |
| 628 |
"-h", |
| 629 |
"dummy", |
| 630 |
NULL |
| 631 |
}; |
| 632 |
exit(arping_main(sizeof(args)/sizeof(char*)-1, args)); |
| 633 |
} |
| 634 |
|
| 635 |
START_TEST(libnet_init_bad_nolo) |
| 636 |
{ |
| 637 |
|
| 638 |
|
| 639 |
mock_libnet_lo_ok = 1; |
| 640 |
do_libnet_init("bad", 0); |
| 641 |
} END_TEST |
| 642 |
|
| 643 |
START_TEST(libnet_init_null_nolo_nonull) |
| 644 |
{ |
| 645 |
mock_libnet_lo_ok = 0; |
| 646 |
mock_libnet_null_ok = 0; |
| 647 |
do_libnet_init(NULL, 0); |
| 648 |
} END_TEST |
| 649 |
|
| 650 |
START_TEST(libnet_init_good) |
| 651 |
{ |
| 652 |
mock_libnet_lo_ok = 0; |
| 653 |
do_libnet_init("good", 0); |
| 654 |
fail_if(libnet == NULL); |
| 655 |
} END_TEST |
| 656 |
|
| 657 |
START_TEST(libnet_init_null_nolo) |
| 658 |
{ |
| 659 |
mock_libnet_lo_ok = 0; |
| 660 |
mock_libnet_null_ok = 1; |
| 661 |
do_libnet_init(NULL, 0); |
| 662 |
fail_if(libnet == NULL); |
| 663 |
} |
| 664 |
END_TEST |
| 665 |
|
| 666 |
static Suite* |
| 667 |
arping_suite(void) |
| 668 |
{ |
| 669 |
Suite* s = suite_create("Arping"); |
| 670 |
|
| 671 |
|
| 672 |
TCase *tc_core; |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
#define SIGH_LIBCHECK(tn) \ |
| 677 |
tc_core = tcase_create(#tn); \ |
| 678 |
tcase_add_test(tc_core, tn); \ |
| 679 |
suite_add_tcase(s, tc_core); |
| 680 |
|
| 681 |
SIGH_LIBCHECK(libnet_init_null_nolo); |
| 682 |
SIGH_LIBCHECK(test_mkpacket); |
| 683 |
SIGH_LIBCHECK(pingip_uninteresting_packet); |
| 684 |
SIGH_LIBCHECK(pingip_interesting_packet); |
| 685 |
SIGH_LIBCHECK(strip_newline_test); |
| 686 |
SIGH_LIBCHECK(get_mac_addr_success); |
| 687 |
SIGH_LIBCHECK(get_mac_addr_fail); |
| 688 |
SIGH_LIBCHECK(is_mac_addr_test); |
| 689 |
SIGH_LIBCHECK(libnet_init_good); |
| 690 |
SIGH_LIBCHECK(arg_maxcount_good); |
| 691 |
SIGH_LIBCHECK(arg_maxcount_hex); |
| 692 |
SIGH_LIBCHECK(arg_packetwait_good); |
| 693 |
|
| 694 |
#define SIGH_LIBCHECK_EXIT(tn) \ |
| 695 |
tc_core = tcase_create(#tn); \ |
| 696 |
tcase_add_exit_test(tc_core, tn, 1); \ |
| 697 |
suite_add_tcase(s, tc_core); |
| 698 |
|
| 699 |
SIGH_LIBCHECK_EXIT(badarg_maxcount_neg); |
| 700 |
SIGH_LIBCHECK_EXIT(badarg_maxcount_nan); |
| 701 |
SIGH_LIBCHECK_EXIT(badarg_maxcount_range); |
| 702 |
SIGH_LIBCHECK_EXIT(badarg_packetwait_neg); |
| 703 |
SIGH_LIBCHECK_EXIT(badarg_packetwait_nan); |
| 704 |
SIGH_LIBCHECK_EXIT(badarg_packetwait_range); |
| 705 |
SIGH_LIBCHECK_EXIT(libnet_init_bad_nolo); |
| 706 |
SIGH_LIBCHECK_EXIT(libnet_init_null_nolo_nonull); |
| 707 |
|
| 708 |
return s; |
| 709 |
} |
| 710 |
|
| 711 |
int |
| 712 |
main() |
| 713 |
{ |
| 714 |
Suite *s = arping_suite(); |
| 715 |
SRunner *sr = srunner_create(s); |
| 716 |
srunner_run_all(sr, CK_NORMAL); |
| 717 |
const int number_failed = srunner_ntests_failed(sr); |
| 718 |
srunner_free(sr); |
| 719 |
fprintf(stderr, "Number of failed tests: %d\n", number_failed); |
| 720 |
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|