]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Added tests for authkeys.
authorLinux Karlsson <karlsson@ntp.org>
Tue, 8 Jun 2010 07:44:46 +0000 (09:44 +0200)
committerLinux Karlsson <karlsson@ntp.org>
Tue, 8 Jun 2010 07:44:46 +0000 (09:44 +0200)
bk: 4c0df4eevIk056fHWLybOCMVQoZ8iA

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

index f7b59668730f4c3a97c34a2f8b22d1ccbd6434c7..7113f9de5c157fff79bb764c3aaf3947366dabd7 100644 (file)
@@ -1,6 +1,8 @@
 check_PROGRAMS = tests
-LDADD = @top_builddir@/libntp/libntp.a -lgtest_main -lpthread
-tests_SOURCES = hextoint.cpp
+LDADD = @top_builddir@/libntp/libntp.a @LCRYPTO@ -lgtest_main -lpthread
+tests_SOURCES = libntptest.cpp         \
+               hextoint.cpp            \
+               authkeys.cpp
 
 INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/lib/isc/include \
        -I$(top_srcdir)/lib/isc/nothreads/include \
diff --git a/tests/libntp/authkeys.cpp b/tests/libntp/authkeys.cpp
new file mode 100644 (file)
index 0000000..c03adf3
--- /dev/null
@@ -0,0 +1,73 @@
+#include "libntptest.h"
+
+extern "C" {
+#include "ntp.h"
+#include "ntp_stdlib.h"
+};
+
+class authkeysTest : public libntptest {
+protected:
+       virtual void SetUp() {
+               init_auth();
+       }
+
+       void AddTrustedKey(keyid_t keyno) {
+               /*
+                * 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);
+
+               authtrust(keyno, 1);
+       }
+
+       void AddUntrustedKey(keyid_t keyno) {
+               authtrust(keyno, 0);
+       }
+};
+
+TEST_F(authkeysTest, AddTrustedKey) {
+       const keyid_t KEYNO = 5;
+
+       AddTrustedKey(KEYNO);
+
+       EXPECT_TRUE(authistrusted(KEYNO));
+}
+
+TEST_F(authkeysTest, AddUntrustedKey) {
+       const keyid_t KEYNO = 3;
+   
+       AddUntrustedKey(KEYNO);
+
+       EXPECT_FALSE(authistrusted(KEYNO));
+}
+
+/*
+TEST_F(authkeysTest, FindKey) {
+       const keyid_t KEYNO1 = 2;
+       const keyid_t KEYNO2 = 66;
+
+       authtrust(KEYNO1, 1);
+       authtrust(KEYNO2, 1);
+
+       savekey* key1 = auth_findkey(KEYNO1);
+       EXPECT_TRUE(key1 != NULL);
+       EXPECT_EQ(KEYNO1, key1->keyid);
+}
+*/
+
+TEST_F(authkeysTest, HaveKeyCorrect) {
+       const keyid_t KEYNO = 3;
+
+       AddTrustedKey(KEYNO);
+
+       EXPECT_TRUE(auth_havekey(KEYNO));
+       EXPECT_TRUE(authhavekey(KEYNO));
+}
+
+TEST_F(authkeysTest, HaveKeyIncorrect) {
+       const keyid_t KEYNO = 2;
+
+       EXPECT_FALSE(auth_havekey(KEYNO));
+       EXPECT_FALSE(authhavekey(KEYNO));
+}
diff --git a/tests/libntp/libntptest.cpp b/tests/libntp/libntptest.cpp
new file mode 100644 (file)
index 0000000..21a6229
--- /dev/null
@@ -0,0 +1,9 @@
+#include "libntptest.h"
+
+/* This file contains various constants that libntp needs to be set
+ *  and that is normally defined in ntpd/ntpq/...
+ */
+
+u_long current_time = 4; // needed by authkeys. Used only in to calculate lifetime.
+volatile int debug = 0;
+const char *progname = "libntptest";