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"
18 u_long current_time = 4;
23 void AddTrustedKey(keyid_t keyno);
24 void AddUntrustedKey(keyid_t keyno);
25 void test_AddTrustedKeys(void);
26 void test_AddUntrustedKey(void);
27 void test_HaveKeyCorrect(void);
28 void test_HaveKeyIncorrect(void);
29 void test_AddWithAuthUseKey(void);
30 void test_EmptyKey(void);
31 void test_auth_log2(void);
39 init_auth(); // causes segfault if called more than once
42 * init_auth() is called by tests_main.cpp earlier. It
43 * does not initialize global variables like
44 * authnumkeys, so let's reset them to zero here.
49 * Especially, empty the key cache!
66 static const int KEYTYPE = KEY_TYPE_MD5;
69 AddTrustedKey(keyid_t keyno)
72 * We need to add a MD5-key in addition to setting the
73 * trust, because authhavekey() requires type != 0.
75 MD5auth_setkey(keyno, KEYTYPE, NULL, 0, NULL);
77 authtrust(keyno, TRUE);
83 AddUntrustedKey(keyid_t keyno)
85 authtrust(keyno, FALSE);
91 test_AddTrustedKeys(void)
93 const keyid_t KEYNO1 = 5;
94 const keyid_t KEYNO2 = 8;
96 AddTrustedKey(KEYNO1);
97 AddTrustedKey(KEYNO2);
99 TEST_ASSERT_TRUE(authistrusted(KEYNO1));
100 TEST_ASSERT_TRUE(authistrusted(KEYNO2));
106 test_AddUntrustedKey(void)
108 const keyid_t KEYNO = 3;
110 AddUntrustedKey(KEYNO);
112 TEST_ASSERT_FALSE(authistrusted(KEYNO));
118 test_HaveKeyCorrect(void)
120 const keyid_t KEYNO = 3;
122 AddTrustedKey(KEYNO);
124 TEST_ASSERT_TRUE(auth_havekey(KEYNO));
125 TEST_ASSERT_TRUE(authhavekey(KEYNO));
131 test_HaveKeyIncorrect(void)
133 const keyid_t KEYNO = 2;
135 TEST_ASSERT_FALSE(auth_havekey(KEYNO));
136 TEST_ASSERT_FALSE(authhavekey(KEYNO));
142 test_AddWithAuthUseKey(void)
144 const keyid_t KEYNO = 5;
145 const char* KEY = "52a";
147 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY));
155 const keyid_t KEYNO = 3;
156 const char* KEY = "";
159 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY));
164 /* test the implementation of 'auth_log2' -- use a local copy of the code */
172 size_t m = ~(size_t)0;
174 for (s = sizeof(size_t) / 2 * CHAR_BIT; s != 0; s >>= 1) {
190 TEST_ASSERT_EQUAL_INT(0, auth_log2(0));
191 TEST_ASSERT_EQUAL_INT(0, auth_log2(1));
192 for (l2 = 1; l2 < sizeof(size_t)*CHAR_BIT; ++l2) {
193 tv = (size_t)1 << l2;
194 TEST_ASSERT_EQUAL_INT(l2, auth_log2( tv ));
195 TEST_ASSERT_EQUAL_INT(l2, auth_log2( tv + 1 ));
196 TEST_ASSERT_EQUAL_INT(l2, auth_log2(2*tv - 1));