1 /* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */
6 #include "ntp_stdlib.h"
7 #include "ntp_calendar.h"
12 # include "openssl/err.h"
13 # include "openssl/rand.h"
14 # include "openssl/evp.h"
17 u_long current_time = 4;
22 void AddTrustedKey(keyid_t keyno);
23 void AddUntrustedKey(keyid_t keyno);
24 void test_AddTrustedKeys(void);
25 void test_AddUntrustedKey(void);
26 void test_HaveKeyCorrect(void);
27 void test_HaveKeyIncorrect(void);
28 void test_AddWithAuthUseKey(void);
29 void test_EmptyKey(void);
37 init_auth(); // causes segfault if called more than once
40 * init_auth() is called by tests_main.cpp earlier. It
41 * does not initialize global variables like
42 * authnumkeys, so let's reset them to zero here.
47 * Especially, empty the key cache!
64 static const int KEYTYPE = KEY_TYPE_MD5;
67 AddTrustedKey(keyid_t keyno)
70 * We need to add a MD5-key in addition to setting the
71 * trust, because authhavekey() requires type != 0.
73 MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
75 authtrust(keyno, TRUE);
81 AddUntrustedKey(keyid_t keyno)
83 authtrust(keyno, FALSE);
89 test_AddTrustedKeys(void)
91 const keyid_t KEYNO1 = 5;
92 const keyid_t KEYNO2 = 8;
94 AddTrustedKey(KEYNO1);
95 AddTrustedKey(KEYNO2);
97 TEST_ASSERT_TRUE(authistrusted(KEYNO1));
98 TEST_ASSERT_TRUE(authistrusted(KEYNO2));
104 test_AddUntrustedKey(void)
106 const keyid_t KEYNO = 3;
108 AddUntrustedKey(KEYNO);
110 TEST_ASSERT_FALSE(authistrusted(KEYNO));
116 test_HaveKeyCorrect(void)
118 const keyid_t KEYNO = 3;
120 AddTrustedKey(KEYNO);
122 TEST_ASSERT_TRUE(auth_havekey(KEYNO));
123 TEST_ASSERT_TRUE(authhavekey(KEYNO));
129 test_HaveKeyIncorrect(void)
131 const keyid_t KEYNO = 2;
133 TEST_ASSERT_FALSE(auth_havekey(KEYNO));
134 TEST_ASSERT_FALSE(authhavekey(KEYNO));
140 test_AddWithAuthUseKey(void)
142 const keyid_t KEYNO = 5;
143 const char* KEY = "52a";
145 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY));
153 const keyid_t KEYNO = 3;
154 const char* KEY = "";
157 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY));