]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Added tests for modetoa() and tsftomsu(), and added more tests for authkeys.
authorLinux Karlsson <karlsson@ntp.org>
Mon, 21 Jun 2010 20:49:41 +0000 (22:49 +0200)
committerLinux Karlsson <karlsson@ntp.org>
Mon, 21 Jun 2010 20:49:41 +0000 (22:49 +0200)
bk: 4c1fd065VsaxF2-R_ivlaEGdf12vfg

tests/libntp/Makefile.am
tests/libntp/authkeys.cpp
tests/libntp/modetoa.cpp [new file with mode: 0644]
tests/libntp/tsftomsu.cpp [new file with mode: 0644]

index 0f7d58b1b99cfd5424e0f0557964190e8c3f5331..560bef55d35854bb52571d6881853a4c0bd6aec9 100644 (file)
@@ -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
index f18bd20aaf10f945f9a9433621f0edd3e0babeac..f7063880f3853f1e3b0d574b7146513fb4b561f1 100644 (file)
@@ -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 (file)
index 0000000..cb7404d
--- /dev/null
@@ -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 (file)
index 0000000..e14f176
--- /dev/null
@@ -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));
+}