From: Lokesh Walase Date: Mon, 22 Jun 2015 07:36:53 +0000 (+0530) Subject: Nameresolution test X-Git-Tag: NTP_4_2_8P3_RC3~22^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=672b0f5cca6ab5d621f5fc46a9474679f7c3b4bc;p=thirdparty%2Fntp.git Nameresolution test bk: 5587bb15CtHthYoCk6qeTuPrPNIB9w --- diff --git a/sntp/tests/Makefile.am b/sntp/tests/Makefile.am index 523d32341..9ccb1ca8e 100644 --- a/sntp/tests/Makefile.am +++ b/sntp/tests/Makefile.am @@ -16,6 +16,7 @@ check_PROGRAMS = \ test-kodDatabase \ test-kodFile \ test-networking \ + test-nameresolution \ test-utilities \ $(NULL) @@ -40,6 +41,7 @@ tests_SOURCES = \ g_kodDatabase.cpp \ g_kodFile.cpp \ g_networking.cpp \ + g_nameresolution.cpp \ packetHandling.cpp \ packetProcessing.cpp \ g_utilities.cpp \ @@ -116,6 +118,7 @@ BUILT_SOURCES += \ $(srcdir)/run-kodDatabase.c \ $(srcdir)/run-kodFile.c \ $(srcdir)/run-networking.c \ + $(srcdir)/run-nameresolution.c \ $(srcdir)/run-utilities.c \ $(NULL) @@ -151,6 +154,14 @@ test_networking_LDADD = \ $(unity_tests_LDADD) \ $(NULL) +test_nameresolution_CFLAGS = \ + -I$(top_srcdir)/unity \ + $(NULL) + +test_nameresolution_LDADD = \ + $(unity_tests_LDADD) \ + $(NULL) + test_utilities_CFLAGS = \ -I$(top_srcdir)/unity \ $(NULL) @@ -173,6 +184,12 @@ test_networking_SOURCES = \ $(top_builddir)/version.c \ $(NULL) +test_nameresolution_SOURCES = \ + nameresolution.c \ + run-nameresolution.c \ + $(top_builddir)/version.c \ + $(NULL) + test_kodDatabase_SOURCES = \ kodDatabase.c \ run-kodDatabase.c \ @@ -216,6 +233,9 @@ $(srcdir)/run-kodDatabase.c: $(srcdir)/kodDatabase.c $(std_unity_list) $(srcdir)/run-networking.c: $(srcdir)/networking.c $(std_unity_list) $(run_unity) networking.c run-networking.c +$(srcdir)/run-nameresolution.c: $(srcdir)/nameresolution.c $(std_unity_list) + $(run_unity) nameresolution.c run-nameresolution.c + $(srcdir)/run-utilities.c: $(srcdir)/utilities.c $(std_unity_list) $(run_unity) utilities.c run-utilities.c diff --git a/sntp/tests/g_nameresolution.cpp b/sntp/tests/g_nameresolution.cpp new file mode 100644 index 000000000..9b48189c8 --- /dev/null +++ b/sntp/tests/g_nameresolution.cpp @@ -0,0 +1,171 @@ +#include "g_sntptest.h" + +extern "C" { +#include "networking.h" +}; + +class networkingTest : public sntptest { +protected: + ::testing::AssertionResult CompareAddrinfo(const char* host, + int family, int flags, + const addrinfo& actual) { + if (family != actual.ai_family) + return ::testing::AssertionFailure() + << "Family mismatch, expected: " << family + << " but was: " << actual.ai_family; + sockaddr_u* sock = new sockaddr_u; + void* expectedaddr = NULL, *actualaddr = NULL; + int size = 0, addrsize = 0; + if (family == AF_INET) { + expectedaddr = &sock->sa4.sin_addr; + actualaddr = &((sockaddr_u*)actual.ai_addr)->sa4.sin_addr; + size = sizeof(sock->sa4); + addrsize = sizeof(sock->sa4.sin_addr); + } else { + expectedaddr = &sock->sa6.sin6_addr; + actualaddr = &((sockaddr_u*)actual.ai_addr)->sa6.sin6_addr; + size = sizeof(sock->sa6); + addrsize = sizeof(sock->sa6.sin6_addr); + } + sock->sa.sa_family = family; + + if (inet_pton(family, host, expectedaddr) != 1) + return ::testing::AssertionFailure() + << "inet_pton failed!"; + + if (flags != actual.ai_flags) + return ::testing::AssertionFailure() + << "Flags mismatch, expected: " << flags + << " but was: " << actual.ai_flags; + + if (size != actual.ai_addrlen) + return ::testing::AssertionFailure() + << "Address length mismatch, expected: " << size + << " but was: " << actual.ai_addrlen; + + if (memcmp(expectedaddr, actualaddr, addrsize) != 0) + return ::testing::AssertionFailure() + << "Address mismatch"; + return ::testing::AssertionSuccess(); + } +}; + +TEST_F(networkingTest, ResolveSingleAddress) { + const char* HOSTS[1] = {"192.0.2.1"}; + const int HOSTCOUNT = COUNTOF(HOSTS); + + addrinfo** actual = NULL; + + ASSERT_EQ(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC)); + + ASSERT_TRUE(actual != NULL); + EXPECT_TRUE(CompareAddrinfo(HOSTS[0], AF_INET, 0, **actual)); +} + +TEST_F(networkingTest, ResolveMultipleAddresses) { + const char* HOSTS[3] = {"192.0.2.1", "192.0.2.5", "192.0.2.10"}; + const int HOSTCOUNT = COUNTOF(HOSTS); + + addrinfo** actual = NULL; + + ASSERT_EQ(3, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC)); + + ASSERT_TRUE(actual != NULL); + for (int i=0; isa4.sin_addr; + actualaddr = &((sockaddr_u*)actual.ai_addr)->sa4.sin_addr; + size = sizeof(sock->sa4); + addrsize = sizeof(sock->sa4.sin_addr); + } else { + expectedaddr = &sock->sa6.sin6_addr; + actualaddr = &((sockaddr_u*)actual.ai_addr)->sa6.sin6_addr; + size = sizeof(sock->sa6); + addrsize = sizeof(sock->sa6.sin6_addr); + } + sock->sa.sa_family = family; + + if (inet_pton(family, host, expectedaddr) != 1) + return FALSE; + if (flags != actual.ai_flags) + return FALSE; + if (size != actual.ai_addrlen) + return FALSE; + if (memcmp(expectedaddr, actualaddr, addrsize) != 0) + return FALSE; + + return TRUE; +} + + +void test_ResolveSingleAddress(void) { + const char* HOSTS[1] = {"192.0.2.1"}; + const int HOSTCOUNT = COUNTOF(HOSTS); + + struct addrinfo** actual = NULL; + + TEST_ASSERT_EQUAL(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC)); + + //TEST_ASSERT_TRUE(actual != NULL); + //TEST_ASSERT_TRUE(CompareAddrinfo(HOSTS[0], AF_INET, 0, **actual)); +} + +/* +TEST_F(networkingTest, ResolveMultipleAddresses) { + const char* HOSTS[3] = {"192.0.2.1", "192.0.2.5", "192.0.2.10"}; + const int HOSTCOUNT = COUNTOF(HOSTS); + + addrinfo** actual = NULL; + + ASSERT_EQ(3, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC)); + + ASSERT_TRUE(actual != NULL); + for (int i=0; i +#include + +//=======External Functions This Runner Calls===== +extern void setUp(void); +extern void tearDown(void); +extern void test_ResolveSingleAddress(void); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + +char *progname; + + +//=======MAIN===== +int main(int argc, char *argv[]) +{ + progname = argv[0]; + Unity.TestFile = "nameresolution.c"; + UnityBegin("nameresolution.c"); + RUN_TEST(test_ResolveSingleAddress, 41); + + return (UnityEnd()); +}