]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Nameresolution test
authorLokesh Walase <lokeshw24@ntp.org>
Mon, 22 Jun 2015 07:36:53 +0000 (13:06 +0530)
committerLokesh Walase <lokeshw24@ntp.org>
Mon, 22 Jun 2015 07:36:53 +0000 (13:06 +0530)
bk: 5587bb15CtHthYoCk6qeTuPrPNIB9w

sntp/tests/Makefile.am
sntp/tests/g_nameresolution.cpp [new file with mode: 0644]
sntp/tests/nameresolution.c [new file with mode: 0644]
sntp/tests/run-nameresolution.c [new file with mode: 0644]

index 523d32341052dc43e6f490e203e8889d706fd7f0..9ccb1ca8e7488be07fd207d2abd56bea6ebb8b7c 100644 (file)
@@ -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 (file)
index 0000000..9b48189
--- /dev/null
@@ -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; i<HOSTCOUNT; i++) {
+               EXPECT_TRUE(CompareAddrinfo(HOSTS[i], AF_INET, 0, *actual[i]))
+                       << "Failed for host number " << i;
+       }
+}
+
+TEST_F(networkingTest, ResolveIPv6Address) {
+       const char* HOSTS[1] = {"2001:0DB8:AC10:FE01:0:0:0:0"};
+       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_INET6, 0, **actual));
+}
+
+TEST_F(networkingTest, ResolveIPv6InvalidComparision) {
+       const char* HOSTS[1] = {"2001:0DB8:AC10:FE01::"};
+       const char* INVALID = "2001:0db8:ac10:fe01:0:1:0:0";
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
+       ASSERT_TRUE(actual != NULL);
+
+       EXPECT_FALSE(CompareAddrinfo(INVALID, AF_INET6, 0, **actual));
+}
+
+TEST_F(networkingTest, ResolveMixedAddressTypes) {
+       const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
+                                                       "192.0.2.30", "2001:ab0:1000::"};
+       const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(4, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
+       ASSERT_TRUE(actual != NULL);
+
+       for (int i=0; i<HOSTCOUNT; i++) {
+               EXPECT_TRUE(CompareAddrinfo(HOSTS[i], FAMILIES[i], 0, *actual[i]))
+                       << "Failed for host number " << i;
+       }
+}
+
+TEST_F(networkingTest, ResolveInvalidAddress) {
+       const char* HOSTS[1] = {"192.258.2.1"};
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(0, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
+}
+
+TEST_F(networkingTest, ResolveMixedAddressValidity) {
+       const char* HOSTS[3] = {"2001:52ij:130:1::", "192.0.2.13", "192.0.257.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[1], AF_INET, 0, **actual));
+}
+
+TEST_F(networkingTest, ResolveIgnoringIPv6) {
+       const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
+                                                       "192.0.2.30", "2001:ab0:1000::"};
+       const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(2, resolve_hosts(HOSTS, HOSTCOUNT, &actual, AF_INET));
+       ASSERT_TRUE(actual != NULL);
+
+       EXPECT_TRUE(CompareAddrinfo(HOSTS[1], FAMILIES[1], 0, *actual[0]));
+       EXPECT_TRUE(CompareAddrinfo(HOSTS[2], FAMILIES[2], 0, *actual[1]));
+}
+
+TEST_F(networkingTest, ResolveIgnoringIPv4) {
+       const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
+                                                       "192.0.2.30", "2001:ab0:1000::"};
+       const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(2, resolve_hosts(HOSTS, HOSTCOUNT, &actual, AF_INET6));
+       ASSERT_TRUE(actual != NULL);
+
+       EXPECT_TRUE(CompareAddrinfo(HOSTS[0], FAMILIES[0], 0, *actual[0]));
+       EXPECT_TRUE(CompareAddrinfo(HOSTS[3], FAMILIES[3], 0, *actual[1]));
+}
diff --git a/sntp/tests/nameresolution.c b/sntp/tests/nameresolution.c
new file mode 100644 (file)
index 0000000..8f1f6d9
--- /dev/null
@@ -0,0 +1,162 @@
+#include "config.h"
+#include "unity.h"
+#include "ntp_stdlib.h"
+
+#include "sntptest.h"
+#include "networking.h"
+
+int CompareAddrinfo(const char* host, int family, int flags, const struct addrinfo actual) {
+               if (family != actual.ai_family)
+                       return FALSE ;
+
+               sockaddr_u* sock = (sockaddr_u*)malloc(sizeof(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 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<HOSTCOUNT; i++) {
+               EXPECT_TRUE(CompareAddrinfo(HOSTS[i], AF_INET, 0, *actual[i]))
+                       << "Failed for host number " << i;
+       }
+}
+
+TEST_F(networkingTest, ResolveIPv6Address) {
+       const char* HOSTS[1] = {"2001:0DB8:AC10:FE01:0:0:0:0"};
+       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_INET6, 0, **actual));
+}
+
+TEST_F(networkingTest, ResolveIPv6InvalidComparision) {
+       const char* HOSTS[1] = {"2001:0DB8:AC10:FE01::"};
+       const char* INVALID = "2001:0db8:ac10:fe01:0:1:0:0";
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(1, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
+       ASSERT_TRUE(actual != NULL);
+
+       EXPECT_FALSE(CompareAddrinfo(INVALID, AF_INET6, 0, **actual));
+}
+
+TEST_F(networkingTest, ResolveMixedAddressTypes) {
+       const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
+                                                       "192.0.2.30", "2001:ab0:1000::"};
+       const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(4, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
+       ASSERT_TRUE(actual != NULL);
+
+       for (int i=0; i<HOSTCOUNT; i++) {
+               EXPECT_TRUE(CompareAddrinfo(HOSTS[i], FAMILIES[i], 0, *actual[i]))
+                       << "Failed for host number " << i;
+       }
+}
+
+TEST_F(networkingTest, ResolveInvalidAddress) {
+       const char* HOSTS[1] = {"192.258.2.1"};
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(0, resolve_hosts(HOSTS, HOSTCOUNT, &actual, PF_UNSPEC));
+}
+
+TEST_F(networkingTest, ResolveMixedAddressValidity) {
+       const char* HOSTS[3] = {"2001:52ij:130:1::", "192.0.2.13", "192.0.257.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[1], AF_INET, 0, **actual));
+}
+
+TEST_F(networkingTest, ResolveIgnoringIPv6) {
+       const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
+                                                       "192.0.2.30", "2001:ab0:1000::"};
+       const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(2, resolve_hosts(HOSTS, HOSTCOUNT, &actual, AF_INET));
+       ASSERT_TRUE(actual != NULL);
+
+       EXPECT_TRUE(CompareAddrinfo(HOSTS[1], FAMILIES[1], 0, *actual[0]));
+       EXPECT_TRUE(CompareAddrinfo(HOSTS[2], FAMILIES[2], 0, *actual[1]));
+}
+
+TEST_F(networkingTest, ResolveIgnoringIPv4) {
+       const char* HOSTS[4] = {"2001:0db8:ac10:fe01::", "192.0.2.10",
+                                                       "192.0.2.30", "2001:ab0:1000::"};
+       const int FAMILIES[4] = {AF_INET6, AF_INET, AF_INET, AF_INET6};
+       const int HOSTCOUNT = COUNTOF(HOSTS);
+
+       addrinfo** actual = NULL;
+
+       ASSERT_EQ(2, resolve_hosts(HOSTS, HOSTCOUNT, &actual, AF_INET6));
+       ASSERT_TRUE(actual != NULL);
+
+       EXPECT_TRUE(CompareAddrinfo(HOSTS[0], FAMILIES[0], 0, *actual[0]));
+       EXPECT_TRUE(CompareAddrinfo(HOSTS[3], FAMILIES[3], 0, *actual[1]));
+}
+
+*/
diff --git a/sntp/tests/run-nameresolution.c b/sntp/tests/run-nameresolution.c
new file mode 100644 (file)
index 0000000..154e653
--- /dev/null
@@ -0,0 +1,51 @@
+/* AUTOGENERATED FILE. DO NOT EDIT. */
+
+//=======Test Runner Used To Run Each Test Below=====
+#define RUN_TEST(TestFunc, TestLineNum) \
+{ \
+  Unity.CurrentTestName = #TestFunc; \
+  Unity.CurrentTestLineNumber = TestLineNum; \
+  Unity.NumberOfTests++; \
+  if (TEST_PROTECT()) \
+  { \
+      setUp(); \
+      TestFunc(); \
+  } \
+  if (TEST_PROTECT() && !TEST_IS_IGNORED) \
+  { \
+    tearDown(); \
+  } \
+  UnityConcludeTest(); \
+}
+
+//=======Automagically Detected Files To Include=====
+#include "unity.h"
+#include <setjmp.h>
+#include <stdio.h>
+
+//=======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());
+}