From: Linux Karlsson Date: Tue, 3 Aug 2010 22:37:25 +0000 (+0200) Subject: Renamed main class for tests. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=89b2189dc2c1f4bdb613e99d966aa61ad090bc64;p=thirdparty%2Fntp.git Renamed main class for tests. Added tests for packet generation and time offset calculations. bk: 4c589a25Mbwimz4s5CIUbrlcR-am3g --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index 3126f1e71c..06a7898bc8 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -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 \ diff --git a/tests/libntp/libntptest.h b/tests/libntp/libntptest.h index 2c6618490a..5f2920ccee 100644 --- a/tests/libntp/libntptest.h +++ b/tests/libntp/libntptest.h @@ -1,4 +1,4 @@ -#include "main.h" +#include "tests_main.h" extern "C" { #include "ntp_stdlib.h" diff --git a/tests/sntp/Makefile.am b/tests/sntp/Makefile.am index 0614ed328a..97e6489d98 100644 --- a/tests/sntp/Makefile.am +++ b/tests/sntp/Makefile.am @@ -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 index 0000000000..08e318a165 --- /dev/null +++ b/tests/sntp/packetHandling.cpp @@ -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) { + +} diff --git a/tests/sntp/sntptest.h b/tests/sntp/sntptest.h index ea4996450a..b2df70e5bd 100644 --- a/tests/sntp/sntptest.h +++ b/tests/sntp/sntptest.h @@ -1,4 +1,4 @@ -#include "main.h" +#include "tests_main.h" class sntptest : public ntptest { }; diff --git a/tests/main.cpp b/tests/tests_main.cpp similarity index 95% rename from tests/main.cpp rename to tests/tests_main.cpp index 98395ccb52..beb51cab6b 100644 --- a/tests/main.cpp +++ b/tests/tests_main.cpp @@ -1,4 +1,4 @@ -#include "main.h" +#include "tests_main.h" int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/tests/main.h b/tests/tests_main.h similarity index 100% rename from tests/main.h rename to tests/tests_main.h