From: Linux Karlsson Date: Tue, 8 Jun 2010 07:44:46 +0000 (+0200) Subject: Added tests for authkeys. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a001337b845ffaa8da63e6b4a80fb6cc0d1fccbf;p=thirdparty%2Fntp.git Added tests for authkeys. bk: 4c0df4eevIk056fHWLybOCMVQoZ8iA --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index f7b5966873..7113f9de5c 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -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 index 0000000000..c03adf34f6 --- /dev/null +++ b/tests/libntp/authkeys.cpp @@ -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 index 0000000000..21a622933b --- /dev/null +++ b/tests/libntp/libntptest.cpp @@ -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";