Skip to content

Commit

Permalink
Update tests to accept unresolved hostnames as destinations.
Browse files Browse the repository at this point in the history
See #4
  • Loading branch information
brendonj committed Sep 11, 2019
1 parent d92b404 commit 3a00cbb
Show file tree
Hide file tree
Showing 32 changed files with 1,093 additions and 119 deletions.
13 changes: 13 additions & 0 deletions src/common/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,16 @@ const char *amp_inet_ntop(struct addrinfo *addr, char *buffer) {

return inet_ntop(addr->ai_family, addrptr, buffer, INET6_ADDRSTRLEN);
}



/*
* Convert an IP address family value into a short human readable string.
*/
const char *family_to_string(int family) {
switch ( family ) {
case AF_INET: return "ipv4";
case AF_INET6: return "ipv6";
default: return "unknown";
};
}
1 change: 1 addition & 0 deletions src/common/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ extern int log_level_override;

void Log(int priority, const char *fmt, ...);
const char *amp_inet_ntop(struct addrinfo *addr, char *buffer);
const char *family_to_string(int family);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ int copy_address_to_protobuf(ProtobufCBinaryData *dst,
const struct addrinfo *src) {
assert(dst);

if ( src == NULL ) {
if ( src == NULL || src->ai_addr == NULL ) {
dst->data = 0;
dst->len = 0;
return 0;
Expand Down
23 changes: 19 additions & 4 deletions src/tests/dns/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,11 @@ static void send_packet(wand_event_handler_t *ev_hdl, void *data) {
*/
info[seq].addr = dest;

if ( !dest->ai_addr ) {
Log(LOG_INFO, "No address for target %s, skipping", dest->ai_canonname);
goto next;
}

/* determine the appropriate socket to use and port field to set */
switch ( dest->ai_family ) {
case AF_INET:
Expand Down Expand Up @@ -1298,16 +1303,26 @@ void print_dns(amp_test_result_t *result) {
for ( i=0; i < msg->n_reports; i++ ) {
item = msg->reports[i];

printf("SERVER: %s", item->name);
inet_ntop(item->family, item->address.data, addrstr, INET6_ADDRSTRLEN);
printf("SERVER: %s", item->name);

if ( !item->has_address ) {
/* couldn't resolve the target, didn't test to it */
snprintf(addrstr, INET6_ADDRSTRLEN, "unresolved %s",
family_to_string(item->family));
printf(" (%s) not tested\n\n", addrstr);
continue;
}

inet_ntop(item->family, item->address.data, addrstr, INET6_ADDRSTRLEN);
printf(" (%s)", addrstr);

/* nothing further we can do if there is no rtt - no good response */
if ( !item->has_rtt ) {
printf(" (%s) no response\n\n", addrstr);
printf(" no response\n\n");
continue;
}

printf(" (%s) %dus\n", addrstr, item->rtt);
printf(" %dus\n", item->rtt);

/* if present, print instance name / nsid payload like dig does */
if ( item->instance.len > 0 ) {
Expand Down
7 changes: 5 additions & 2 deletions src/tests/dns/test/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TESTS=dns_register.test dns_encode.test dns_decode.test dns_report.test
check_PROGRAMS=dns_register.test dns_encode.test dns_decode.test dns_report.test
TESTS=dns_register.test dns_encode.test dns_decode.test dns_report.test dns_unresolved_target.test
check_PROGRAMS=dns_register.test dns_encode.test dns_decode.test dns_report.test dns_unresolved_target.test

check_LTLIBRARIES=testdns.la
testdns_la_SOURCES=../dns.c
Expand All @@ -19,5 +19,8 @@ dns_decode_test_LDADD=testdns.la
dns_report_test_SOURCES=dns_report_test.c
dns_report_test_LDADD=testdns.la

dns_unresolved_target_test_SOURCES=dns_unresolved_target_test.c
dns_unresolved_target_test_LDADD=testdns.la

AM_CFLAGS=-g -Wall -W -rdynamic -DUNIT_TEST
INCLUDES=-I../ -I../../ -I../../../common/
111 changes: 111 additions & 0 deletions src/tests/dns/test/dns_unresolved_target_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* This file is part of amplet2.
*
* Copyright (c) 2013-2019 The University of Waikato, Hamilton, New Zealand.
*
* Author: Brendon Jones
*
* All rights reserved.
*
* This code has been developed by the University of Waikato WAND
* research group. For further information please see http://www.wand.net.nz/
*
* amplet2 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*
* You must obey the GNU General Public License in all respects for all
* of the code used other than OpenSSL. If you modify file(s) with this
* exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do
* so, delete this exception statement from your version. If you delete
* this exception statement from all source files in the program, then
* also delete it here.
*
* amplet2 is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with amplet2. If not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>

#include "tests.h"
#include "dns.h"
#include "dns.pb-c.h"

#define TEST_TARGET "doesnotexist.invalid"

/*
*
*/
int main(void) {
amp_test_result_t *result;
struct addrinfo *target;
Amplet2__Dns__Report *msg;
Amplet2__Dns__Item *item;
int argc = 3;
char *argv[] = {"amp-dns", "-q", "example.com", NULL};

/*
* create a dummy addrinfo like the resolver does when it can't resolve
* the name
*/
target = calloc(1, sizeof(struct addrinfo));
target->ai_family = AF_INET;
target->ai_addrlen = 0;
target->ai_addr = NULL;
target->ai_canonname = TEST_TARGET;
target->ai_next = NULL;

/* run the test against the dummy target */
result = run_dns(argc, argv, 1, &target);

assert(result);
assert(result->data);

/* check that the results are missing/empty in the right places */
msg = amplet2__dns__report__unpack(NULL, result->len, result->data);

assert(msg);
assert(msg->header);
assert(msg->n_reports == 1);
assert(msg->reports);

item = msg->reports[0];

assert(!item->has_address);
assert(item->has_family);
assert(item->family == AF_INET);
assert(!item->has_rtt);
assert(!item->has_query_length);
assert(!item->has_response_size);
assert(!item->has_total_answer);
assert(!item->has_total_authority);
assert(!item->has_total_additional);
assert(!item->flags);
assert(!item->has_ttl);
assert(strcmp(item->name, TEST_TARGET) == 0);
assert(!item->has_instance);
assert(!item->has_rrsig);

amplet2__dns__report__free_unpacked(msg, NULL);
free(result->data);
free(result);
free(target);

return 0;
}
20 changes: 18 additions & 2 deletions src/tests/fastping/fastping.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,15 @@ static amp_test_result_t* send_icmp_stream(struct addrinfo *dest,
memset(&stop_time, 0, sizeof(struct timeval));
memset(&loss_timeout, 0, sizeof(struct timeval));

if ( dest->ai_addr == NULL ) {
if ( gettimeofday(&start_time, NULL) != 0 ) {
Log(LOG_ERR, "Could not gettimeofday(), aborting test");
exit(EXIT_FAILURE);
}

return report_result(&start_time, dest, options, NULL, NULL);
}

/* extract the socket depending on the address family */
switch ( dest->ai_family ) {
case AF_INET: sock = sockets->socket; break;
Expand Down Expand Up @@ -641,7 +650,7 @@ static amp_test_result_t* send_icmp_stream(struct addrinfo *dest,
if ( bytes > 0 ) {
/* extract the sequence number from the icmp packet */
int64_t sequence = extract_data(dest, response, pid, &from);
if ( sequence >= 0 && sequence < options->count) {
if ( sequence >= 0 && sequence < (int64_t)options->count ) {
memcpy(&(timing[sequence].time_received),
&receive_time, sizeof(struct timeval));
received++;
Expand Down Expand Up @@ -825,7 +834,14 @@ void print_fastping(amp_test_result_t *result) {

samples = item->rtt ? item->rtt->samples : 0;
percent = ((double) samples / (double) header->count) * 100;
inet_ntop(header->family, header->address.data, addrstr, INET6_ADDRSTRLEN);

if ( header->has_address ) {
inet_ntop(header->family, header->address.data, addrstr,
INET6_ADDRSTRLEN);
} else {
snprintf(addrstr, INET6_ADDRSTRLEN, "unresolved %s",
family_to_string(header->family));
}

/* print basic stats */
printf("\n");
Expand Down
7 changes: 5 additions & 2 deletions src/tests/fastping/test/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TESTS=fastping_register.test
check_PROGRAMS=fastping_register.test
TESTS=fastping_register.test fastping_unresolved_target.test
check_PROGRAMS=fastping_register.test fastping_unresolved_target.test

check_LTLIBRARIES=testfastping.la
testfastping_la_SOURCES=../fastping.c
Expand All @@ -10,5 +10,8 @@ testfastping_la_LDFLAGS=-module -avoid-version -L../../../common/ -lamp -lprotob
fastping_register_test_SOURCES=fastping_register_test.c
fastping_register_test_LDADD=testfastping.la

fastping_unresolved_target_test_SOURCES=fastping_unresolved_target_test.c
fastping_unresolved_target_test_LDADD=testfastping.la

AM_CFLAGS=-g -Wall -W -rdynamic -DUNIT_TEST
INCLUDES=-I../ -I../../../common/
102 changes: 102 additions & 0 deletions src/tests/fastping/test/fastping_unresolved_target_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* This file is part of amplet2.
*
* Copyright (c) 2013-2019 The University of Waikato, Hamilton, New Zealand.
*
* Author: Brendon Jones
*
* All rights reserved.
*
* This code has been developed by the University of Waikato WAND
* research group. For further information please see http://www.wand.net.nz/
*
* amplet2 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations including
* the two.
*
* You must obey the GNU General Public License in all respects for all
* of the code used other than OpenSSL. If you modify file(s) with this
* exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do
* so, delete this exception statement from your version. If you delete
* this exception statement from all source files in the program, then
* also delete it here.
*
* amplet2 is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with amplet2. If not, see <http://www.gnu.org/licenses/>.
*/

#include <assert.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>

#include "tests.h"
#include "fastping.h"
#include "fastping.pb-c.h"

#define TEST_TARGET "doesnotexist.invalid"

/*
*
*/
int main(void) {
amp_test_result_t *result;
struct addrinfo *target;
Amplet2__Fastping__Report *msg;
Amplet2__Fastping__Item *item;

/*
* create a dummy addrinfo like the resolver does when it can't resolve
* the name
*/
target = calloc(1, sizeof(struct addrinfo));
target->ai_family = AF_INET;
target->ai_addrlen = 0;
target->ai_addr = NULL;
target->ai_canonname = TEST_TARGET;
target->ai_next = NULL;

/* run the test against the dummy target */
result = run_fastping(0, NULL, 1, &target);

assert(result);
assert(result->data);

/* check that the results are missing/empty in the right places */
msg = amplet2__fastping__report__unpack(NULL, result->len, result->data);

assert(msg);
assert(msg->header);
assert(!msg->header->has_address);
assert(msg->header->has_family);
assert(msg->header->family == AF_INET);
assert(strcmp(msg->header->name, TEST_TARGET) == 0);
assert(msg->n_reports == 1);
assert(msg->reports);

item = msg->reports[0];

assert(!item->has_runtime);
assert(!item->rtt);
assert(!item->jitter);

amplet2__fastping__report__free_unpacked(msg, NULL);
free(result->data);
free(result);
free(target);

return 0;
}
Loading

0 comments on commit 3a00cbb

Please sign in to comment.