From: Linux Karlsson Date: Mon, 21 Jun 2010 20:49:41 +0000 (+0200) Subject: Added tests for modetoa() and tsftomsu(), and added more tests for authkeys. X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c24e50371bbd810a72bf442ee93bc32b27f2ff82;p=thirdparty%2Fntp.git Added tests for modetoa() and tsftomsu(), and added more tests for authkeys. bk: 4c1fd065VsaxF2-R_ivlaEGdf12vfg --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index 0f7d58b1b9..560bef55d3 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -15,6 +15,7 @@ tests_SOURCES = ../main.cpp \ humandate.cpp \ inttoa.cpp \ lfptostr.cpp \ + modetoa.cpp \ netof.cpp \ numtoa.cpp \ numtohost.cpp \ @@ -25,6 +26,7 @@ tests_SOURCES = ../main.cpp \ socktoa.cpp \ ssl_init.cpp \ strtolfp.cpp \ + tsftomsu.cpp \ uglydate.cpp \ uinttoa.cpp \ ymd2yd.cpp diff --git a/tests/libntp/authkeys.cpp b/tests/libntp/authkeys.cpp index f18bd20aaf..f7063880f3 100644 --- a/tests/libntp/authkeys.cpp +++ b/tests/libntp/authkeys.cpp @@ -1,3 +1,5 @@ +/* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */ + #include "libntptest.h" extern "C" { @@ -7,6 +9,8 @@ extern "C" { class authkeysTest : public libntptest { protected: + static const int KEYTYPE = KEY_TYPE_MD5; + virtual void SetUp() { init_auth(); @@ -22,7 +26,7 @@ protected: * We need to add a MD5-key in addition to setting the * trust, because authhavekey() requires type != 0. */ - MD5auth_setkey(keyno, KEY_TYPE_MD5, NULL, 0); + MD5auth_setkey(keyno, KEYTYPE, NULL, 0); authtrust(keyno, 1); } @@ -51,18 +55,30 @@ TEST_F(authkeysTest, AddUntrustedKey) { EXPECT_FALSE(authistrusted(KEYNO)); } -/* +/* // Would require definition of struct savekey. TEST_F(authkeysTest, FindKey) { const keyid_t KEYNO1 = 2; const keyid_t KEYNO2 = 66; - authtrust(KEYNO1, 1); - authtrust(KEYNO2, 1); + AddTrustedKey(KEYNO1); + AddTrustedKey(KEYNO2); savekey* key1 = auth_findkey(KEYNO1); EXPECT_TRUE(key1 != NULL); EXPECT_EQ(KEYNO1, key1->keyid); } + +TEST_F(authkeysTest, FindKeyIncorrect) { + const keyid_t KEYNO1 = 4; + const keyid_t KEYNO2 = 10; + const keyid_t KEYNOTADDED = 14; + + AddTrustedKey(KEYNO1); + AddTrustedKey(KEYNO2); + + savekey* key = auth_findkey(KEYNOTADDED); + EXPECT_TRUE(key == NULL); +} */ TEST_F(authkeysTest, HaveKeyCorrect) { @@ -81,20 +97,17 @@ TEST_F(authkeysTest, HaveKeyIncorrect) { EXPECT_FALSE(authhavekey(KEYNO)); } -/*TEST_F(authkeysTest, DeleteKeys) { - const keyid_t KEYNO1 = 2; - const keyid_t KEYNO2 = 5; - const keyid_t KEYNO3 = 66; +TEST_F(authkeysTest, AddWithAuthUseKey) { + const keyid_t KEYNO = 5; + const char* KEY = "52a"; - AddTrustedKey(KEYNO1); - AddTrustedKey(KEYNO2); - AddTrustedKey(KEYNO3); + EXPECT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY)); +} - EXPECT_EQ(3, authnumkeys); +TEST_F(authkeysTest, EmptyKey) { + const keyid_t KEYNO = 3; + const char* KEY = ""; - auth_delkeys(); - EXPECT_FALSE(auth_havekey(KEYNO1)); - EXPECT_FALSE(auth_havekey(KEYNO2)); - EXPECT_FALSE(auth_havekey(KEYNO3)); -}*/ + EXPECT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY)); +} diff --git a/tests/libntp/modetoa.cpp b/tests/libntp/modetoa.cpp new file mode 100644 index 0000000000..cb7404d039 --- /dev/null +++ b/tests/libntp/modetoa.cpp @@ -0,0 +1,16 @@ +#include "libntptest.h" + +class modetoaTest : public libntptest { +}; + +TEST_F(modetoaTest, KnownMode) { + const int MODE = 3; // Should be "client" + + EXPECT_STREQ("client", modetoa(MODE)); +} + +TEST_F(modetoaTest, UnknownMode) { + const int MODE = 100; + + EXPECT_STREQ("mode#100", modetoa(MODE)); +} diff --git a/tests/libntp/tsftomsu.cpp b/tests/libntp/tsftomsu.cpp new file mode 100644 index 0000000000..e14f1767f5 --- /dev/null +++ b/tests/libntp/tsftomsu.cpp @@ -0,0 +1,29 @@ +#include "libntptest.h" + +class tsftomsuTest : public libntptest { +protected: + static const u_long HALF = 2147483648UL; + static const u_long HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005 + static const u_long HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005 + static const u_long QUARTER = 1073741824L; +}; + +TEST_F(tsftomsuTest, Half) { + EXPECT_EQ(500, tsftomsu(HALF, 0)); +} + +TEST_F(tsftomsuTest, Quarter) { + EXPECT_EQ(250, tsftomsu(QUARTER, 1)); +} + +TEST_F(tsftomsuTest, HalfPromilleRoundUp) { + EXPECT_EQ(1, tsftomsu(HALF_PROMILLE_UP, 1)); +} + +TEST_F(tsftomsuTest, HalfPromilleRoundDown) { + EXPECT_EQ(0, tsftomsu(HALF_PROMILLE_DOWN, 1)); +} + +TEST_F(tsftomsuTest, HalfPromilleTruncate) { + EXPECT_EQ(0, tsftomsu(HALF_PROMILLE_UP, 0)); +}