]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Changes wrt to <socktoa> test
authorLokesh Walase <lokeshw24@ntp.org>
Mon, 15 Jun 2015 18:23:38 +0000 (23:53 +0530)
committerLokesh Walase <lokeshw24@ntp.org>
Mon, 15 Jun 2015 18:23:38 +0000 (23:53 +0530)
bk: 557f182aez0DXP3DTWepUBRmkjhpjQ

tests/libntp/Makefile.am
tests/libntp/g_socktoa.cpp [new file with mode: 0644]
tests/libntp/socktoa.c [new file with mode: 0644]

index 15b90653081ef2bcaff73084736e73add76c006b..d0073233d824f49e4d36b346f95e8f1d657bc7a7 100644 (file)
@@ -7,7 +7,8 @@ run_unity =     cd $(srcdir) && ruby ../../sntp/unity/auto/generate_test_runner.rb
 #removed test-libntp
 check_PROGRAMS = test-modetoa test-uglydate test-ymd2yd test-statestr test-numtoa test-numtohost \
 test-hextoint test-atoint test-atouint test-authkeys test-a_md5encrypt test-lfpfunc test-octtoint \
-test-hextolfp test-netof test-decodenetnum
+test-hextolfp test-netof test-socktoa
+#test-decodenetnum
 
 if GTEST_AVAILABLE
 check_PROGRAMS += tests
@@ -196,14 +197,23 @@ test_netof_LDADD =                   \
        $(unity_tests_LDADD)
        $(NULL)
 
-test_decodenetnum_CFLAGS =                  \
+#test_decodenetnum_CFLAGS =                  \
+#      -I$(top_srcdir)/sntp/unity      \
+#      $(NULL)
+#
+#test_decodenetnum_LDADD =                   \
+#      $(unity_tests_LDADD)
+#      $(NULL)
+
+test_socktoa_CFLAGS =                  \
        -I$(top_srcdir)/sntp/unity      \
        $(NULL)
 
-test_decodenetnum_LDADD =                   \
+test_socktoa_LDADD =                   \
        $(unity_tests_LDADD)
        $(NULL)
 
+
 test_a_md5encrypt_CFLAGS =                  \
        -I$(top_srcdir)/sntp/unity      \
        $(NULL)
@@ -304,11 +314,17 @@ test_netof_SOURCES =                      \
        run-test-netof.c                \
        $(NULL)
 
-test_decodenetnum_SOURCES =                    \
-       decodenetnum.c                  \
-       run-test-decodenetnum.c                 \
+#test_decodenetnum_SOURCES =                   \
+#      decodenetnum.c                  \
+#      run-test-decodenetnum.c                 \
+#      $(NULL)
+
+test_socktoa_SOURCES =                 \
+       socktoa.c                       \
+       run-test-socktoa.c                      \
        $(NULL)
 
+
 test_a_md5encrypt_SOURCES =                    \
        a_md5encrypt.c                          \
        run-test-a_md5encrypt.c                 \
@@ -364,8 +380,11 @@ $(srcdir)/run-test-hextolfp.c: $(srcdir)/hextolfp.c $(std_unity_list)
 $(srcdir)/run-test-netof.c: $(srcdir)/netof.c $(std_unity_list)
        $(run_unity) netof.c run-test-netof.c
 
-$(srcdir)/run-test-decodenetnum.c: $(srcdir)/decodenetnum.c $(std_unity_list)
-       $(run_unity) decodenetnum.c run-test-decodenetnum.c
+#$(srcdir)/run-test-decodenetnum.c: $(srcdir)/decodenetnum.c $(std_unity_list)
+#      $(run_unity) decodenetnum.c run-test-decodenetnum.c
+
+$(srcdir)/run-test-socktoa.c: $(srcdir)/socktoa.c $(std_unity_list)
+       $(run_unity) socktoa.c run-test-socktoa.c
 
 $(srcdir)/run-test-a_md5encrypt.c: $(srcdir)/a_md5encrypt.c $(std_unity_list)
        $(run_unity) a_md5encrypt.c run-test-a_md5encrypt.c
diff --git a/tests/libntp/g_socktoa.cpp b/tests/libntp/g_socktoa.cpp
new file mode 100644 (file)
index 0000000..6c70b6d
--- /dev/null
@@ -0,0 +1,100 @@
+#include "sockaddrtest.h"
+
+class socktoaTest : public sockaddrtest {
+};
+
+TEST_F(socktoaTest, IPv4AddressWithPort) {
+       sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
+
+       EXPECT_STREQ("192.0.2.10", socktoa(&input));
+       EXPECT_STREQ("192.0.2.10:123", sockporttoa(&input));
+}
+
+TEST_F(socktoaTest, IPv6AddressWithPort) {
+       const struct in6_addr address = {
+               0x20, 0x01, 0x0d, 0xb8,
+               0x85, 0xa3, 0x08, 0xd3, 
+               0x13, 0x19, 0x8a, 0x2e,
+               0x03, 0x70, 0x73, 0x34
+       };
+
+       const char* expected =
+               "2001:db8:85a3:8d3:1319:8a2e:370:7334";
+       const char* expected_port = 
+               "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
+
+       sockaddr_u input;
+       memset(&input, 0, sizeof(input));
+       AF(&input) = AF_INET6;
+       SET_ADDR6N(&input, address);
+       SET_PORT(&input, 123);
+
+       EXPECT_STREQ(expected, socktoa(&input));
+       EXPECT_STREQ(expected_port, sockporttoa(&input));
+}
+
+#ifdef ISC_PLATFORM_HAVESCOPEID
+TEST_F(socktoaTest, ScopedIPv6AddressWithPort) {
+       const struct in6_addr address = {
+               0xfe, 0x80, 0x00, 0x00,
+               0x00, 0x00, 0x00, 0x00,
+               0x02, 0x12, 0x3f, 0xff, 
+               0xfe, 0x29, 0xff, 0xfa
+       };
+
+       const char* expected =
+               "fe80::212:3fff:fe29:fffa%5";
+       const char* expected_port = 
+               "[fe80::212:3fff:fe29:fffa%5]:123";
+
+       sockaddr_u input;
+       memset(&input, 0, sizeof(input));
+       AF(&input) = AF_INET6;
+       SET_ADDR6N(&input, address);
+       SET_PORT(&input, 123);
+       SCOPE_VAR(&input) = 5;
+
+       EXPECT_STREQ(expected, socktoa(&input));
+       EXPECT_STREQ(expected_port, sockporttoa(&input));
+}
+#endif /* ISC_PLATFORM_HAVESCOPEID */
+
+TEST_F(socktoaTest, HashEqual) {
+       sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
+       sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
+
+       ASSERT_TRUE(IsEqual(input1, input2));
+       EXPECT_EQ(sock_hash(&input1), sock_hash(&input2));
+}
+
+TEST_F(socktoaTest, HashNotEqual) {
+       /* These two addresses should not generate the same hash. */
+       sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
+       sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
+
+       ASSERT_FALSE(IsEqual(input1, input2));
+       EXPECT_NE(sock_hash(&input1), sock_hash(&input2));
+}
+
+TEST_F(socktoaTest, IgnoreIPv6Fields) {
+       const struct in6_addr address = {
+               0x20, 0x01, 0x0d, 0xb8,
+        0x85, 0xa3, 0x08, 0xd3, 
+        0x13, 0x19, 0x8a, 0x2e,
+        0x03, 0x70, 0x73, 0x34
+       };
+
+       sockaddr_u input1, input2;
+
+       input1.sa6.sin6_family = AF_INET6;
+       input1.sa6.sin6_addr = address;
+       input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
+       SET_PORT(&input1, NTP_PORT);
+
+       input2.sa6.sin6_family = AF_INET6;
+       input2.sa6.sin6_addr = address;
+       input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
+       SET_PORT(&input2, NTP_PORT);
+
+       EXPECT_EQ(sock_hash(&input1), sock_hash(&input2));
+}
diff --git a/tests/libntp/socktoa.c b/tests/libntp/socktoa.c
new file mode 100644 (file)
index 0000000..dca973e
--- /dev/null
@@ -0,0 +1,103 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+#include "ntp_calendar.h"
+#include "unity.h"
+
+#include "c_sockaddrtest.h"
+
+
+void test_IPv4AddressWithPort(void) {
+       sockaddr_u input = CreateSockaddr4("192.0.2.10", 123);
+
+       TEST_ASSERT_EQUAL_STRING("192.0.2.10", socktoa(&input));
+       TEST_ASSERT_EQUAL_STRING("192.0.2.10:123", sockporttoa(&input));
+}
+
+void test_IPv6AddressWithPort(void) {
+       const struct in6_addr address = {
+               0x20, 0x01, 0x0d, 0xb8,
+               0x85, 0xa3, 0x08, 0xd3, 
+               0x13, 0x19, 0x8a, 0x2e,
+               0x03, 0x70, 0x73, 0x34
+       };
+
+       const char* expected =
+               "2001:db8:85a3:8d3:1319:8a2e:370:7334";
+       const char* expected_port = 
+               "[2001:db8:85a3:8d3:1319:8a2e:370:7334]:123";
+
+       sockaddr_u input;
+       memset(&input, 0, sizeof(input));
+       AF(&input) = AF_INET6;
+       SET_ADDR6N(&input, address);
+       SET_PORT(&input, 123);
+
+       TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
+       TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
+}
+
+#ifdef ISC_PLATFORM_HAVESCOPEID
+void test_ScopedIPv6AddressWithPort(void) {
+       const struct in6_addr address = {
+               0xfe, 0x80, 0x00, 0x00,
+               0x00, 0x00, 0x00, 0x00,
+               0x02, 0x12, 0x3f, 0xff, 
+               0xfe, 0x29, 0xff, 0xfa
+       };
+
+       const char* expected =
+               "fe80::212:3fff:fe29:fffa%5";
+       const char* expected_port = 
+               "[fe80::212:3fff:fe29:fffa%5]:123";
+
+       sockaddr_u input;
+       memset(&input, 0, sizeof(input));
+       AF(&input) = AF_INET6;
+       SET_ADDR6N(&input, address);
+       SET_PORT(&input, 123);
+       SCOPE_VAR(&input) = 5;
+
+       TEST_ASSERT_EQUAL_STRING(expected, socktoa(&input));
+       TEST_ASSERT_EQUAL_STRING(expected_port, sockporttoa(&input));
+}
+#endif /* ISC_PLATFORM_HAVESCOPEID */
+
+void test_HashEqual(void) {
+       sockaddr_u input1 = CreateSockaddr4("192.00.2.2", 123);
+       sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
+
+       TEST_ASSERT_TRUE(IsEqual(input1, input2));
+       TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
+}
+
+void test_HashNotEqual(void) {
+       /* These two addresses should not generate the same hash. */
+       sockaddr_u input1 = CreateSockaddr4("192.0.2.1", 123);
+       sockaddr_u input2 = CreateSockaddr4("192.0.2.2", 123);
+
+       TEST_ASSERT_FALSE(IsEqual(input1, input2));
+       //TODO : EXPECT_NE(sock_hash(&input1), sock_hash(&input2));
+}
+
+void test_IgnoreIPv6Fields(void) {
+       const struct in6_addr address = {
+               0x20, 0x01, 0x0d, 0xb8,
+        0x85, 0xa3, 0x08, 0xd3, 
+        0x13, 0x19, 0x8a, 0x2e,
+        0x03, 0x70, 0x73, 0x34
+       };
+
+       sockaddr_u input1, input2;
+
+       input1.sa6.sin6_family = AF_INET6;
+       input1.sa6.sin6_addr = address;
+       input1.sa6.sin6_flowinfo = 30L; // This value differs from input2.
+       SET_PORT(&input1, NTP_PORT);
+
+       input2.sa6.sin6_family = AF_INET6;
+       input2.sa6.sin6_addr = address;
+       input2.sa6.sin6_flowinfo = 10L; // This value differs from input1.
+       SET_PORT(&input2, NTP_PORT);
+
+       TEST_ASSERT_EQUAL(sock_hash(&input1), sock_hash(&input2));
+}