]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Added tests for a number of functions. Tests for inttoa() won't pass until bug 1575...
authorLinux Karlsson <karlsson@ntp.org>
Tue, 15 Jun 2010 17:36:59 +0000 (19:36 +0200)
committerLinux Karlsson <karlsson@ntp.org>
Tue, 15 Jun 2010 17:36:59 +0000 (19:36 +0200)
bk: 4c17ba3bvRPuqmX65l6C_zK1rxHnFg

12 files changed:
tests/libntp/Makefile.am
tests/libntp/atolfp.cpp [deleted file]
tests/libntp/decodenetnum.cpp
tests/libntp/humandate.cpp [new file with mode: 0644]
tests/libntp/inttoa.cpp
tests/libntp/netof.cpp [new file with mode: 0644]
tests/libntp/numtoa.cpp [new file with mode: 0644]
tests/libntp/numtohost.cpp [new file with mode: 0644]
tests/libntp/octtoint.cpp [new file with mode: 0644]
tests/libntp/sockaddrtest.h [new file with mode: 0644]
tests/libntp/socktoa.cpp [new file with mode: 0644]
tests/libntp/strtolfp.cpp [new file with mode: 0644]

index f20880edf8e4026aabbf4d7221ffdffaabbb1e2f..2902b2f99a38563300503a73ab497fc7170090bb 100644 (file)
@@ -6,16 +6,22 @@ tests_SOURCES = ../main.cpp           \
                libntptest.cpp          \
                a_md5encrypt.cpp        \
                atoint.cpp              \
-               atolfp.cpp              \
                atouint.cpp             \
                authkeys.cpp            \
                caljulian.cpp           \
                decodenetnum.cpp        \
                hextoint.cpp            \
                hextolfp.cpp            \
+               humandate.cpp           \
                inttoa.cpp              \
                lfptostr.cpp            \
-               sfptostr.cpp
+               netof.cpp               \
+               numtoa.cpp              \
+               numtohost.cpp           \
+               octtoint.cpp            \
+               sfptostr.cpp            \
+               socktoa.cpp             \
+               strtolfp.cpp
 
 INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/lib/isc/include \
        -I$(top_srcdir)/lib/isc/nothreads/include \
diff --git a/tests/libntp/atolfp.cpp b/tests/libntp/atolfp.cpp
deleted file mode 100644 (file)
index f1aec66..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-#include "lfptest.h"
-
-class atolfpTest : public lfptest {
-};
-
-TEST_F(atolfpTest, PositiveInteger) {
-       const char *str = "500";
-       l_fp expected = {500,0};
-       l_fp actual;
-
-       ASSERT_TRUE(atolfp(str, &actual));
-       EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(atolfpTest, NegativeInteger) {
-       const char *str = "-300";
-       l_fp expected = {-300,0};
-       l_fp actual;
-
-       ASSERT_TRUE(atolfp(str, &actual));
-       EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(atolfpTest, PositiveFraction) {
-       const char *str = "+500.5";
-       l_fp expected = {500, -2147483648};
-       l_fp actual;
-
-       ASSERT_TRUE(atolfp(str, &actual));
-       EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(atolfpTest, NegativeFraction) {
-       const char *str = "-300.75";
-       l_fp expected = {-301, 1073741824}; // Fraction is 2^32 * 1/4
-       l_fp actual;
-
-       ASSERT_TRUE(atolfp(str, &actual));
-       EXPECT_TRUE(IsEqual(expected, actual));
-}
-
-TEST_F(atolfpTest, InvalidChars) {
-       const char *str = "500.4a2";
-       l_fp actual;
-
-       ASSERT_FALSE(atolfp(str, &actual));
-}
index 9ac494cff6e42b4e20fae1aa3bc8888287940297..c6766de288ee2cb5f1173742d7d29d58d1c5cb89 100644 (file)
@@ -1,41 +1,6 @@
-#include "libntptest.h"
+#include "sockaddrtest.h"
 
-extern "C" {
-#include "ntp.h"
-};
-
-class decodenetnumTest : public libntptest {
-protected:
-       ::testing::AssertionResult IsEqual(const sockaddr_u &expected, const sockaddr_u &actual) {
-               if (expected.sa.sa_family != actual.sa.sa_family) {
-                       return ::testing::AssertionFailure()
-                               << "Expected sa_family: " << expected.sa.sa_family
-                               << " but got: " << actual.sa.sa_family;
-               }
-
-               if (actual.sa.sa_family == AF_INET) { // IPv4
-                       if (expected.sa4.sin_port == actual.sa4.sin_port &&
-                               memcmp(&expected.sa4.sin_addr, &actual.sa4.sin_addr,
-                                          sizeof(in_addr)) == 0) {
-                               return ::testing::AssertionSuccess();
-                       } else {
-                               return ::testing::AssertionFailure()
-                                       << "IPv4 comparision failed";
-                       }
-               } else if (actual.sa.sa_family == AF_INET6) { //IPv6
-                       if (expected.sa6.sin6_port == actual.sa6.sin6_port &&
-                               memcmp(&expected.sa6.sin6_addr, &actual.sa6.sin6_addr,
-                                          sizeof(in6_addr)) == 0) {
-                               return ::testing::AssertionSuccess();
-                       } else {
-                               return ::testing::AssertionFailure()
-                                       << "IPv6 comparision failed";
-                       }
-               } else { // Unknown family
-                       return ::testing::AssertionFailure()
-                               << "Unknown sa_family: " << actual.sa.sa_family;
-               }
-       }
+class decodenetnumTest : public sockaddrtest {
 };
 
 TEST_F(decodenetnumTest, IPv4AddressOnly) {
diff --git a/tests/libntp/humandate.cpp b/tests/libntp/humandate.cpp
new file mode 100644 (file)
index 0000000..4fb8609
--- /dev/null
@@ -0,0 +1,40 @@
+#include "libntptest.h"
+
+#include <sstream>
+#include <string>
+
+class humandateTest : public libntptest {
+};
+
+TEST_F(humandateTest, RegularTime) {
+       time_t sample = 1276601278;
+       std::ostringstream expected;
+
+       tm* time;
+       time = localtime(&sample);
+       ASSERT_TRUE(time != NULL);
+
+       expected << time->tm_hour << ":"
+                        << time->tm_min << ":"
+                        << time->tm_sec;
+
+       EXPECT_STREQ(expected.str().c_str(), humantime(sample));
+}
+
+TEST_F(humandateTest, CurrentTime) {
+       time_t sample;
+       std::ostringstream expected;
+
+       time(&sample);
+
+       tm* time;
+       time = localtime(&sample);
+       ASSERT_TRUE(time != NULL);
+
+       expected << std::setfill('0')
+                        << std::setw(2) << time->tm_hour << ":"
+                        << std::setw(2) << time->tm_min << ":"
+                        << std::setw(2) << time->tm_sec;
+
+       EXPECT_STREQ(expected.str().c_str(), humantime(sample));
+}
index 05ea88cadbffc3e2369372c3a9783787e336a4b7..01ab3227f69de0680433a0ed532b417895669a9f 100644 (file)
@@ -11,8 +11,9 @@ TEST_F(inttoaTest, NegativeNumber) {
        EXPECT_STREQ("-3", inttoa(-3));
 }
 
+/* Bug 1575 start */
 TEST_F(inttoaTest, BigPositiveNumber) {
-       EXPECT_STREQ("2147483647", inttoa(2147483647));
+       EXPECT_STREQ("2147483647", inttoa(2147483647L));
 }
 
 TEST_F(inttoaTest, BigNegativeNumber) {
@@ -22,3 +23,4 @@ TEST_F(inttoaTest, BigNegativeNumber) {
 TEST_F(inttoaTest, MediumNumber) {
        EXPECT_STREQ("20000001", inttoa(20000001));
 }
+/* Bug 1575 end */
diff --git a/tests/libntp/netof.cpp b/tests/libntp/netof.cpp
new file mode 100644 (file)
index 0000000..67b9df4
--- /dev/null
@@ -0,0 +1,69 @@
+#include "sockaddrtest.h"
+
+class netofTest : public sockaddrtest  {
+};
+
+TEST_F(netofTest, ClassBAddress) {
+       sockaddr_u input = CreateSockaddr4("172.16.2.1", NTP_PORT);
+       sockaddr_u expected = CreateSockaddr4("172.16.0.0", NTP_PORT);
+
+       sockaddr_u* actual = netof(&input);
+
+       ASSERT_TRUE(actual != NULL);
+       EXPECT_TRUE(IsEqual(expected, *actual));
+}
+
+TEST_F(netofTest, ClassCAddress) {
+       sockaddr_u input = CreateSockaddr4("192.0.2.255", NTP_PORT);
+       sockaddr_u expected = CreateSockaddr4("192.0.2.0", NTP_PORT);
+
+       sockaddr_u* actual = netof(&input);
+
+       ASSERT_TRUE(actual != NULL);
+       EXPECT_TRUE(IsEqual(expected, *actual));
+}
+
+TEST_F(netofTest, ClassAAddress) {
+       /* Class A addresses are assumed to be classless,
+        * thus the same address should be returned.
+        */
+       sockaddr_u input = CreateSockaddr4("10.20.30.40", NTP_PORT);
+       sockaddr_u expected = CreateSockaddr4("10.20.30.40", NTP_PORT);
+
+       sockaddr_u* actual = netof(&input);
+
+       ASSERT_TRUE(actual != NULL);
+       EXPECT_TRUE(IsEqual(expected, *actual));
+}
+
+TEST_F(netofTest, IPv6Address) {
+       /* IPv6 addresses are assumed to have 64-bit host- and 64-bit network parts. */
+       const struct in6_addr input_address = {
+               0x20, 0x01, 0x0d, 0xb8,
+        0x85, 0xa3, 0x08, 0xd3, 
+        0x13, 0x19, 0x8a, 0x2e,
+        0x03, 0x70, 0x73, 0x34
+       }; // 2001:0db8:85a3:08d3:1319:8a2e:0370:7334
+
+       const struct in6_addr expected_address = {
+               0x20, 0x01, 0x0d, 0xb8,
+        0x85, 0xa3, 0x08, 0xd3, 
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00
+       }; // 2001:0db8:85a3:08d3:0000:0000:0000:0000
+       
+       sockaddr_u input;
+       input.sa6.sin6_family = AF_INET6;
+       input.sa6.sin6_addr = input_address;
+       SET_PORT(&input, 3000);
+
+       sockaddr_u expected;
+       expected.sa6.sin6_family = AF_INET6;
+       expected.sa6.sin6_addr = expected_address;
+       SET_PORT(&expected, 3000);
+
+       sockaddr_u* actual = netof(&input);
+
+       ASSERT_TRUE(actual != NULL);
+       EXPECT_TRUE(IsEqual(expected, *actual));
+}
diff --git a/tests/libntp/numtoa.cpp b/tests/libntp/numtoa.cpp
new file mode 100644 (file)
index 0000000..06a5df4
--- /dev/null
@@ -0,0 +1,17 @@
+#include "libntptest.h"
+
+class numtoaTest : public libntptest {
+};
+
+TEST_F(numtoaTest, Address) {
+       u_int32 input = htonl(3221225472+512+1); // 192.0.2.1
+
+       EXPECT_STREQ("192.0.2.1", numtoa(input));
+}
+
+TEST_F(numtoaTest, Netmask) {
+       // 255.255.255.0
+       u_int32 input = htonl(255*256*256*256 + 255*256*256 + 255*256);
+
+       EXPECT_STREQ("255.255.255.0", numtoa(input));
+}
diff --git a/tests/libntp/numtohost.cpp b/tests/libntp/numtohost.cpp
new file mode 100644 (file)
index 0000000..ffc574e
--- /dev/null
@@ -0,0 +1,15 @@
+#include "libntptest.h"
+
+class numtohostTest : public libntptest {
+};
+
+TEST_F(numtohostTest, LoopbackNetNonResolve) {
+       /* A loopback address in 127.0.0.0/8 is chosen, and
+        * numtohost() should not try to resolve it unless
+        * it is 127.0.0.1
+        */
+
+       u_int32 input = 127*256*256*256 + 1*256 + 1; // 127.0.1.1
+       
+       EXPECT_STREQ("127.0.1.1", numtohost(htonl(input)));
+}
diff --git a/tests/libntp/octtoint.cpp b/tests/libntp/octtoint.cpp
new file mode 100644 (file)
index 0000000..f88ffff
--- /dev/null
@@ -0,0 +1,57 @@
+#include "libntptest.h"
+
+class octtointTest : public libntptest {
+};
+
+TEST_F(octtointTest, SingleDigit) {
+       const char* str = "5";
+       u_long actual;
+
+       ASSERT_TRUE(octtoint(str, &actual));
+       EXPECT_EQ(5, actual);
+}
+
+TEST_F(octtointTest, MultipleDigits) {
+       const char* str = "271";
+       u_long actual;
+
+       ASSERT_TRUE(octtoint(str, &actual));
+       EXPECT_EQ(185, actual);
+}
+
+TEST_F(octtointTest, Zero) {
+       const char* str = "0";
+       u_long actual;
+
+       ASSERT_TRUE(octtoint(str, &actual));
+       EXPECT_EQ(0, actual);
+}
+
+TEST_F(octtointTest, MaximumUnsigned32bit) {
+       const char* str = "37777777777";
+       u_long actual;
+
+       ASSERT_TRUE(octtoint(str, &actual));
+       EXPECT_EQ(4294967295, actual);
+}
+
+TEST_F(octtointTest, Overflow) {
+       const char* str = "40000000000";
+       u_long actual;
+
+       ASSERT_FALSE(octtoint(str, &actual));
+}
+
+TEST_F(octtointTest, IllegalCharacter) {
+       const char* str = "5ac2";
+       u_long actual;
+
+       ASSERT_FALSE(octtoint(str, &actual));
+}
+
+TEST_F(octtointTest, IllegalDigit) {
+       const char* str = "5283";
+       u_long actual;
+
+       ASSERT_FALSE(octtoint(str, &actual));
+}
diff --git a/tests/libntp/sockaddrtest.h b/tests/libntp/sockaddrtest.h
new file mode 100644 (file)
index 0000000..4baac4d
--- /dev/null
@@ -0,0 +1,58 @@
+#ifndef TESTS_SOCKADDRTEST_H
+#define TESTS_SOCKADDRTEST_H
+
+#include "libntptest.h"
+
+extern "C" {
+#include "ntp.h"
+};
+
+class sockaddrtest : public libntptest {
+protected:
+       ::testing::AssertionResult IsEqual(const sockaddr_u &expected, const sockaddr_u &actual) {
+               if (expected.sa.sa_family != actual.sa.sa_family) {
+                       return ::testing::AssertionFailure()
+                               << "Expected sa_family: " << expected.sa.sa_family
+                               << " but got: " << actual.sa.sa_family;
+               }
+
+               if (actual.sa.sa_family == AF_INET) { // IPv4
+                       if (expected.sa4.sin_port == actual.sa4.sin_port &&
+                               memcmp(&expected.sa4.sin_addr, &actual.sa4.sin_addr,
+                                          sizeof(in_addr)) == 0) {
+                               return ::testing::AssertionSuccess();
+                       } else {
+                               return ::testing::AssertionFailure()
+                                       << "IPv4 comparision failed, expected: "
+                                       << expected.sa4.sin_addr.s_addr
+                                       << "(" << socktoa(&expected) << ")"
+                                       << " but was: "
+                                       << actual.sa4.sin_addr.s_addr
+                                       << "(" << socktoa(&actual) << ")";
+                       }
+               } else if (actual.sa.sa_family == AF_INET6) { //IPv6
+                       if (expected.sa6.sin6_port == actual.sa6.sin6_port &&
+                               memcmp(&expected.sa6.sin6_addr, &actual.sa6.sin6_addr,
+                                          sizeof(in6_addr)) == 0) {
+                               return ::testing::AssertionSuccess();
+                       } else {
+                               return ::testing::AssertionFailure()
+                                       << "IPv6 comparision failed";
+                       }
+               } else { // Unknown family
+                       return ::testing::AssertionFailure()
+                               << "Unknown sa_family: " << actual.sa.sa_family;
+               }
+       }
+
+       sockaddr_u CreateSockaddr4(const char* address, unsigned int port) {
+               sockaddr_u s;
+               s.sa4.sin_family = AF_INET;
+               s.sa4.sin_addr.s_addr = inet_addr(address);
+               SET_PORT(&s, port);
+
+               return s;
+       }
+};
+
+#endif // TESTS_SOCKADDRTEST_H
diff --git a/tests/libntp/socktoa.cpp b/tests/libntp/socktoa.cpp
new file mode 100644 (file)
index 0000000..2b6827e
--- /dev/null
@@ -0,0 +1,73 @@
+#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;
+       input.sa6.sin6_family = AF_INET6;
+       input.sa6.sin6_addr = address;
+       SET_PORT(&input, 123);
+
+       EXPECT_STREQ(expected, socktoa(&input));
+       EXPECT_STREQ(expected_port, sockporttoa(&input));
+}
+
+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/strtolfp.cpp b/tests/libntp/strtolfp.cpp
new file mode 100644 (file)
index 0000000..4a758c9
--- /dev/null
@@ -0,0 +1,102 @@
+#include "lfptest.h"
+
+/* This class tests both atolfp and mstolfp */
+
+class strtolfpTest : public lfptest {
+protected:
+       static const int32 HALF = -2147483648L;
+       static const int32 QUARTER = 1073741824L;
+       static const int32 QUARTER_PROMILLE_APPRX = 1073742L;
+};
+
+TEST_F(strtolfpTest, PositiveInteger) {
+       const char *str = "500";
+       const char *str_ms = "500000";
+
+       l_fp expected = {500,0};
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, NegativeInteger) {
+       const char *str = "-300";
+       const char *str_ms = "-300000";
+
+       l_fp expected = {-300,0};
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, PositiveFraction) {
+       const char *str = "+500.5";
+       const char *str_ms = "500500.0";
+
+       l_fp expected = {500, HALF};
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, NegativeFraction) {
+       const char *str = "-300.75";
+       const char *str_ms = "-300750";
+
+       l_fp expected = {-301, QUARTER}; // Fraction is 2^32 * 1/4
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, PositiveMsFraction) {
+       const char *str = "300.00025";
+       const char *str_ms = "300000.25";
+
+       l_fp expected = {300, QUARTER_PROMILLE_APPRX};
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, NegativeMsFraction) {
+       const char *str = "-199.99975";
+       const char *str_ms = "-199999.75";
+
+       l_fp expected = {-200, QUARTER_PROMILLE_APPRX};
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, InvalidChars) {
+       const char *str = "500.4a2";
+       l_fp actual, actual_ms;
+
+       ASSERT_FALSE(atolfp(str, &actual));
+       ASSERT_FALSE(mstolfp(str, &actual_ms));
+}