test-kodDatabase \
test-kodFile \
test-networking \
+ test-nameresolution \
test-utilities \
$(NULL)
g_kodDatabase.cpp \
g_kodFile.cpp \
g_networking.cpp \
+ g_nameresolution.cpp \
packetHandling.cpp \
packetProcessing.cpp \
g_utilities.cpp \
$(srcdir)/run-kodDatabase.c \
$(srcdir)/run-kodFile.c \
$(srcdir)/run-networking.c \
+ $(srcdir)/run-nameresolution.c \
$(srcdir)/run-utilities.c \
$(NULL)
$(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)
$(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 \
$(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
--- /dev/null
+#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]));
+}
--- /dev/null
+#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]));
+}
+
+*/
--- /dev/null
+/* 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());
+}