summaryrefslogtreecommitdiff
path: root/tests/libntp/authkeys.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/libntp/authkeys.c')
-rw-r--r--tests/libntp/authkeys.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/tests/libntp/authkeys.c b/tests/libntp/authkeys.c
new file mode 100644
index 0000000000000..b949628da3bd0
--- /dev/null
+++ b/tests/libntp/authkeys.c
@@ -0,0 +1,119 @@
+/* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */
+
+#include "config.h"
+
+#include "ntp.h"
+#include "ntp_stdlib.h"
+#include "ntp_calendar.h"
+
+#include "unity.h"
+
+#ifdef OPENSSL
+# include "openssl/err.h"
+# include "openssl/rand.h"
+# include "openssl/evp.h"
+#endif
+
+u_long current_time = 4;
+int counter = 0;
+
+
+// old code from google test framework, moved to SetUp() for unity
+void setUp(void)
+{
+// init_lib();
+ if(counter ==0){
+ counter++;
+ init_auth(); //causes segfault if called more than once
+ }
+/*
+ * init_auth() is called by tests_main.cpp earlier. It
+ * does not initialize global variables like
+ * authnumkeys, so let's reset them to zero here.
+ */
+ authnumkeys = 0;
+
+ /*
+ * Especially, empty the key cache!
+ */
+ cache_keyid = 0;
+ cache_type = 0;
+ cache_flags = 0;
+ cache_secret = NULL;
+ cache_secretsize = 0;
+
+}
+
+void tearDown(void)
+{
+}
+
+
+static const int KEYTYPE = KEY_TYPE_MD5;
+
+
+
+
+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, KEYTYPE, NULL, 0);
+
+ authtrust(keyno, TRUE);
+}
+
+void AddUntrustedKey(keyid_t keyno) {
+ authtrust(keyno, FALSE);
+}
+
+void test_AddTrustedKeys() {
+ const keyid_t KEYNO1 = 5;
+ const keyid_t KEYNO2 = 8;
+
+ AddTrustedKey(KEYNO1);
+ AddTrustedKey(KEYNO2);
+
+ TEST_ASSERT_TRUE(authistrusted(KEYNO1));
+ TEST_ASSERT_TRUE(authistrusted(KEYNO2));
+}
+
+void test_AddUntrustedKey() {
+ const keyid_t KEYNO = 3;
+
+ AddUntrustedKey(KEYNO);
+
+ TEST_ASSERT_FALSE(authistrusted(KEYNO));
+}
+
+void test_HaveKeyCorrect() {
+ const keyid_t KEYNO = 3;
+
+ AddTrustedKey(KEYNO);
+
+ TEST_ASSERT_TRUE(auth_havekey(KEYNO));
+ TEST_ASSERT_TRUE(authhavekey(KEYNO));
+}
+
+void test_HaveKeyIncorrect() {
+ const keyid_t KEYNO = 2;
+
+ TEST_ASSERT_FALSE(auth_havekey(KEYNO));
+ TEST_ASSERT_FALSE(authhavekey(KEYNO));
+}
+
+void test_AddWithAuthUseKey() {
+ const keyid_t KEYNO = 5;
+ const char* KEY = "52a";
+
+ TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
+}
+
+void test_EmptyKey() {
+ const keyid_t KEYNO = 3;
+ const char* KEY = "";
+
+
+ TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
+}