]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Renamed main class for tests.
authorLinux Karlsson <karlsson@ntp.org>
Tue, 3 Aug 2010 22:37:25 +0000 (00:37 +0200)
committerLinux Karlsson <karlsson@ntp.org>
Tue, 3 Aug 2010 22:37:25 +0000 (00:37 +0200)
Added tests for packet generation and time offset calculations.

bk: 4c589a25Mbwimz4s5CIUbrlcR-am3g

tests/libntp/Makefile.am
tests/libntp/libntptest.h
tests/sntp/Makefile.am
tests/sntp/packetHandling.cpp [new file with mode: 0644]
tests/sntp/sntptest.h
tests/tests_main.cpp [moved from tests/main.cpp with 95% similarity]
tests/tests_main.h [moved from tests/main.h with 100% similarity]

index 3126f1e71ce5fc2fe391727b539476117ca4cdd6..06a7898bc8272cd20854cc1797cd032bb5d41c4a 100644 (file)
@@ -2,7 +2,7 @@ check_PROGRAMS = tests
 LDADD = @LCRYPTO@ @GTEST_LDFLAGS@ @GTEST_LIBS@ @top_builddir@/libntp/libntp.a
 AM_CXXFLAGS = @GTEST_CXXFLAGS@
 AM_CPPFLAGS = @GTEST_CPPFLAGS@
-tests_SOURCES = ../main.cpp            \
+tests_SOURCES = ../tests_main.cpp      \
                libntptest.cpp          \
                a_md5encrypt.cpp        \
                atoint.cpp              \
index 2c6618490aa336a36ba797a86dfdc61cbb1662eb..5f2920cceec39daaa0b327349c7a1291ffa1e9b9 100644 (file)
@@ -1,4 +1,4 @@
-#include "main.h"
+#include "tests_main.h"
 
 extern "C" {
 #include "ntp_stdlib.h"
index 0614ed328af2a6cd156d57b8155efa7b7c0442fa..97e6489d98ae095036822c4eb80d69830d5c7d58 100644 (file)
@@ -5,11 +5,12 @@ sntp_src = $(top_builddir)/sntp
 sntp_SOURCES_USED =    $(sntp_src)/crypto.o            \
                        $(sntp_src)/kod_management.o    \
                        $(sntp_src)/log.o               \
+                       $(sntp_src)/main.o              \
                        $(sntp_src)/networking.o        \
                        $(sntp_src)/sntp-opts.o         \
                        $(sntp_src)/utilities.o
 
-base_SOURCES =         ../main.cpp     \
+base_SOURCES =         ../tests_main.cpp       \
                        sntptest.cpp
 
 tests_SOURCES = $(base_SOURCES)                \
@@ -17,6 +18,7 @@ tests_SOURCES = $(base_SOURCES)               \
                kodDatabase.cpp         \
                kodFile.cpp             \
                networking.cpp          \
+               packetHandling.cpp      \
                utilities.cpp
 
 LDADD =        @GTEST_LDFLAGS@                 \
diff --git a/tests/sntp/packetHandling.cpp b/tests/sntp/packetHandling.cpp
new file mode 100644 (file)
index 0000000..08e318a
--- /dev/null
@@ -0,0 +1,124 @@
+#include "sntptest.h"
+
+extern "C" {
+#include "main.h"
+#include "ntp.h"
+};
+
+class mainTest : public sntptest {
+protected:
+       ::testing::AssertionResult LfpEquality(const l_fp &expected, const l_fp &actual) {
+               if (L_ISEQU(&expected, &actual)) {
+                       return ::testing::AssertionSuccess();
+               } else {
+                       return ::testing::AssertionFailure()
+                               << " expected: " << lfptoa(&expected, FRACTION_PREC)
+                               << " (" << expected.l_ui << "." << expected.l_uf << ")"
+                               << " but was: " << lfptoa(&actual, FRACTION_PREC)
+                               << " (" << actual.l_ui << "." << actual.l_uf << ")";
+               }
+       }
+};
+
+TEST_F(mainTest, GenerateUnauthenticatedPacket) {
+       pkt testpkt;
+
+       timeval xmt;
+       GETTIMEOFDAY(&xmt, NULL);
+       xmt.tv_sec += JAN_1970;
+
+       EXPECT_EQ(LEN_PKT_NOMAC,
+                         generate_pkt(&testpkt, &xmt, 0, NULL));
+
+       EXPECT_EQ(LEAP_NOTINSYNC, PKT_LEAP(testpkt.li_vn_mode));
+       EXPECT_EQ(NTP_VERSION, PKT_VERSION(testpkt.li_vn_mode));
+       EXPECT_EQ(MODE_CLIENT, PKT_MODE(testpkt.li_vn_mode));
+
+       EXPECT_EQ(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
+       EXPECT_EQ(8, testpkt.ppoll);
+
+       l_fp expected_xmt, actual_xmt;
+       TVTOTS(&xmt, &expected_xmt);
+       NTOHL_FP(&testpkt.xmt, &actual_xmt);
+       EXPECT_TRUE(LfpEquality(expected_xmt, actual_xmt));
+}
+
+TEST_F(mainTest, GenerateAuthenticatedPacket) {
+       key testkey;
+       testkey.next = NULL;
+       testkey.key_id = 30;
+       testkey.key_len = 9;
+       memcpy(testkey.key_seq, "123456789", testkey.key_len);
+       memcpy(testkey.type, "MD5", 3);
+
+       pkt testpkt;
+
+       timeval xmt;
+       GETTIMEOFDAY(&xmt, NULL);
+       xmt.tv_sec += JAN_1970;
+
+       const int EXPECTED_PKTLEN = LEN_PKT_NOMAC + MAX_MD5_LEN;
+
+       EXPECT_EQ(EXPECTED_PKTLEN,
+                         generate_pkt(&testpkt, &xmt, testkey.key_id, &testkey));
+
+       EXPECT_EQ(LEAP_NOTINSYNC, PKT_LEAP(testpkt.li_vn_mode));
+       EXPECT_EQ(NTP_VERSION, PKT_VERSION(testpkt.li_vn_mode));
+       EXPECT_EQ(MODE_CLIENT, PKT_MODE(testpkt.li_vn_mode));
+
+       EXPECT_EQ(STRATUM_UNSPEC, PKT_TO_STRATUM(testpkt.stratum));
+       EXPECT_EQ(8, testpkt.ppoll);
+
+       l_fp expected_xmt, actual_xmt;
+       TVTOTS(&xmt, &expected_xmt);
+       NTOHL_FP(&testpkt.xmt, &actual_xmt);
+       EXPECT_TRUE(LfpEquality(expected_xmt, actual_xmt));
+
+       EXPECT_EQ(testkey.key_id, ntohl(testpkt.exten[0]));
+       
+       char expected_mac[MAX_MD5_LEN];
+       ASSERT_EQ(MAX_MD5_LEN - 4, // Remove the key_id, only keep the mac.
+                         make_mac((char*)&testpkt, LEN_PKT_NOMAC, MAX_MD5_LEN, &testkey, expected_mac));
+       EXPECT_TRUE(memcmp(expected_mac, (char*)&testpkt.exten[1], MAX_MD5_LEN -4) == 0);
+}
+
+TEST_F(mainTest, OffsetCalculationPositiveOffset) {
+       pkt rpkt;
+
+       rpkt.precision = -16; // 0,000015259
+       rpkt.rootdelay = HTONS_FP(DTOUFP(0.125));
+       rpkt.rootdisp = HTONS_FP(DTOUFP(0.25)); //
+       l_fp reftime;
+       get_systime(&reftime);
+       HTONL_FP(&reftime, &rpkt.reftime);
+
+       l_fp tmp;
+
+       tmp.l_ui = 1000000000UL;
+       tmp.l_uf = 0UL;
+       HTONL_FP(&tmp, &rpkt.org);
+
+       tmp.l_ui = 1000000001UL;
+       tmp.l_uf = 2147483648UL;
+       HTONL_FP(&tmp, &rpkt.rec);
+
+       tmp.l_ui = 1000000002UL;
+       tmp.l_uf = 0UL;
+       HTONL_FP(&tmp, &rpkt.xmt);
+
+       tmp.l_ui = 1000000001UL;
+       tmp.l_uf = 0UL;
+       timeval dst;
+       TSTOTV(&tmp, &dst);
+
+       double offset, precision, root_disp;
+       offset_calculation(&rpkt, LEN_PKT_NOMAC, &dst, &offset, &precision, &root_disp);
+
+       EXPECT_DOUBLE_EQ(1.25, offset);
+       EXPECT_DOUBLE_EQ(LOGTOD(-16), precision);
+       EXPECT_DOUBLE_EQ(0.25, root_disp);
+}
+
+TEST_F(mainTest, OffsetCalculationNegativeOffset) {
+
+}
index ea4996450ab0a3a881ef50099c5cef0671890d9d..b2df70e5bd598a893b805657e045e471d8922438 100644 (file)
@@ -1,4 +1,4 @@
-#include "main.h"
+#include "tests_main.h"
 
 class sntptest : public ntptest {
 };
similarity index 95%
rename from tests/main.cpp
rename to tests/tests_main.cpp
index 98395ccb521e058a3c6eefbc1c5c62b438e00130..beb51cab6b6f0b9fd836c44926d1b8452ce59adb 100644 (file)
@@ -1,4 +1,4 @@
-#include "main.h"
+#include "tests_main.h"
 
 int main(int argc, char **argv) {
        ::testing::InitGoogleTest(&argc, argv);
similarity index 100%
rename from tests/main.h
rename to tests/tests_main.h