From: Lokesh Walase Date: Mon, 15 Jun 2015 06:29:18 +0000 (+0530) Subject: Changes for test X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc433f054f9e05cd89a7c62539d89a727038422a;p=thirdparty%2Fntp.git Changes for test bk: 557e70beTu0SBnChNuWnF2q3JPVGnw --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index c7ffbf2d7..15b906530 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -7,7 +7,7 @@ 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-hextolfp test-netof test-decodenetnum if GTEST_AVAILABLE check_PROGRAMS += tests @@ -56,7 +56,7 @@ tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp \ caltontp.cpp \ calyearstart.cpp \ clocktime.cpp \ - decodenetnum.cpp \ + g_decodenetnum.cpp \ g_hextoint.cpp \ g_hextolfp.cpp \ humandate.cpp \ @@ -196,6 +196,13 @@ test_netof_LDADD = \ $(unity_tests_LDADD) $(NULL) +test_decodenetnum_CFLAGS = \ + -I$(top_srcdir)/sntp/unity \ + $(NULL) + +test_decodenetnum_LDADD = \ + $(unity_tests_LDADD) + $(NULL) test_a_md5encrypt_CFLAGS = \ -I$(top_srcdir)/sntp/unity \ @@ -297,7 +304,10 @@ test_netof_SOURCES = \ run-test-netof.c \ $(NULL) - +test_decodenetnum_SOURCES = \ + decodenetnum.c \ + run-test-decodenetnum.c \ + $(NULL) test_a_md5encrypt_SOURCES = \ a_md5encrypt.c \ @@ -354,6 +364,9 @@ $(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-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/decodenetnum.c b/tests/libntp/decodenetnum.c new file mode 100644 index 000000000..8d29eb64c --- /dev/null +++ b/tests/libntp/decodenetnum.c @@ -0,0 +1,96 @@ +#include "config.h" +#include "ntp_stdlib.h" +#include "ntp_calendar.h" +#include "unity.h" + +#include "c_sockaddrtest.h" + + +void test_IPv4AddressOnly(void) { + const char *str = "192.0.2.1"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); + SET_PORT(&expected, NTP_PORT); + + TEST_ASSERT_TRUE(decodenetnum(str, &actual)); + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} + +void test_IPv4AddressWithPort(void) { + const char *str = "192.0.2.2:2000"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2"); + SET_PORT(&expected, 2000); + + TEST_ASSERT_TRUE(decodenetnum(str, &actual)); + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} + +void test_IPv6AddressOnly(void) { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + + const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa6.sin6_family = AF_INET6; + expected.sa6.sin6_addr = address; + SET_PORT(&expected, NTP_PORT); + + TEST_ASSERT_TRUE(decodenetnum(str, &actual)); + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} + +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 *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa6.sin6_family = AF_INET6; + expected.sa6.sin6_addr = address; + SET_PORT(&expected, 3000); + + TEST_ASSERT_TRUE(decodenetnum(str, &actual)); + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} + +void test_IllegalAddress(void) { + const char *str = "192.0.2.270:2000"; + sockaddr_u actual; + + TEST_ASSERT_FALSE(decodenetnum(str, &actual)); +} + +void test_IllegalCharInPort(void) { + /* An illegal port does not make the decodenetnum fail, but instead + * makes it use the standard port. + */ + const char *str = "192.0.2.1:a700"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); + SET_PORT(&expected, NTP_PORT); + + TEST_ASSERT_TRUE(decodenetnum(str, &actual)); + TEST_ASSERT_TRUE(IsEqual(expected, actual)); +} diff --git a/tests/libntp/g_decodenetnum.cpp b/tests/libntp/g_decodenetnum.cpp new file mode 100644 index 000000000..c6766de28 --- /dev/null +++ b/tests/libntp/g_decodenetnum.cpp @@ -0,0 +1,93 @@ +#include "sockaddrtest.h" + +class decodenetnumTest : public sockaddrtest { +}; + +TEST_F(decodenetnumTest, IPv4AddressOnly) { + const char *str = "192.0.2.1"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); + SET_PORT(&expected, NTP_PORT); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(decodenetnumTest, IPv4AddressWithPort) { + const char *str = "192.0.2.2:2000"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2"); + SET_PORT(&expected, 2000); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(decodenetnumTest, IPv6AddressOnly) { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + + const char *str = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa6.sin6_family = AF_INET6; + expected.sa6.sin6_addr = address; + SET_PORT(&expected, NTP_PORT); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(decodenetnumTest, IPv6AddressWithPort) { + const struct in6_addr address = { + 0x20, 0x01, 0x0d, 0xb8, + 0x85, 0xa3, 0x08, 0xd3, + 0x13, 0x19, 0x8a, 0x2e, + 0x03, 0x70, 0x73, 0x34 + }; + + const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa6.sin6_family = AF_INET6; + expected.sa6.sin6_addr = address; + SET_PORT(&expected, 3000); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +} + +TEST_F(decodenetnumTest, IllegalAddress) { + const char *str = "192.0.2.270:2000"; + sockaddr_u actual; + + ASSERT_FALSE(decodenetnum(str, &actual)); +} + +TEST_F(decodenetnumTest, IllegalCharInPort) { + /* An illegal port does not make the decodenetnum fail, but instead + * makes it use the standard port. + */ + const char *str = "192.0.2.1:a700"; + sockaddr_u actual; + + sockaddr_u expected; + expected.sa4.sin_family = AF_INET; + expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1"); + SET_PORT(&expected, NTP_PORT); + + ASSERT_TRUE(decodenetnum(str, &actual)); + EXPECT_TRUE(IsEqual(expected, actual)); +}