1 /* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */
2 
3 #include "config.h"
4 
5 #include "ntp.h"
6 #include "ntp_stdlib.h"
7 #include "ntp_calendar.h"
8 
9 #include "unity.h"
10 
11 #ifdef OPENSSL
12 # include "openssl/err.h"
13 # include "openssl/rand.h"
14 # include "openssl/evp.h"
15 #endif
16 #include <limits.h>
17 
18 u_long current_time = 4;
19 int counter = 0;
20 
21 void setUp(void);
22 void tearDown(void);
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);
32 
33 
34 void
setUp(void)35 setUp(void)
36 {
37 	if (counter == 0) {
38 		counter++;
39 		init_auth(); // causes segfault if called more than once
40 	}
41 	/*
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.
45 	 */
46 	authnumkeys = 0;
47 
48 	/*
49 	 * Especially, empty the key cache!
50 	 */
51 	cache_keyid = 0;
52 	cache_type = 0;
53 	cache_flags = 0;
54 	cache_secret = NULL;
55 	cache_secretsize = 0;
56 
57 	return;
58 }
59 
60 void
tearDown(void)61 tearDown(void)
62 {
63 	return;
64 }
65 
66 static const int KEYTYPE = KEY_TYPE_MD5;
67 
68 void
AddTrustedKey(keyid_t keyno)69 AddTrustedKey(keyid_t keyno)
70 {
71 	/*
72 	 * We need to add a MD5-key in addition to setting the
73 	 * trust, because authhavekey() requires type != 0.
74 	 */
75 	MD5auth_setkey(keyno, KEYTYPE, NULL, 0, NULL);
76 
77 	authtrust(keyno, TRUE);
78 
79 	return;
80 }
81 
82 void
AddUntrustedKey(keyid_t keyno)83 AddUntrustedKey(keyid_t keyno)
84 {
85 	authtrust(keyno, FALSE);
86 
87 	return;
88 }
89 
90 void
test_AddTrustedKeys(void)91 test_AddTrustedKeys(void)
92 {
93 	const keyid_t KEYNO1 = 5;
94 	const keyid_t KEYNO2 = 8;
95 
96 	AddTrustedKey(KEYNO1);
97 	AddTrustedKey(KEYNO2);
98 
99 	TEST_ASSERT_TRUE(authistrusted(KEYNO1));
100 	TEST_ASSERT_TRUE(authistrusted(KEYNO2));
101 
102 	return;
103 }
104 
105 void
test_AddUntrustedKey(void)106 test_AddUntrustedKey(void)
107 {
108 	const keyid_t KEYNO = 3;
109 
110 	AddUntrustedKey(KEYNO);
111 
112 	TEST_ASSERT_FALSE(authistrusted(KEYNO));
113 
114 	return;
115 }
116 
117 void
test_HaveKeyCorrect(void)118 test_HaveKeyCorrect(void)
119 {
120 	const keyid_t KEYNO = 3;
121 
122 	AddTrustedKey(KEYNO);
123 
124 	TEST_ASSERT_TRUE(auth_havekey(KEYNO));
125 	TEST_ASSERT_TRUE(authhavekey(KEYNO));
126 
127 	return;
128 }
129 
130 void
test_HaveKeyIncorrect(void)131 test_HaveKeyIncorrect(void)
132 {
133 	const keyid_t KEYNO = 2;
134 
135 	TEST_ASSERT_FALSE(auth_havekey(KEYNO));
136 	TEST_ASSERT_FALSE(authhavekey(KEYNO));
137 
138 	return;
139 }
140 
141 void
test_AddWithAuthUseKey(void)142 test_AddWithAuthUseKey(void)
143 {
144 	const keyid_t KEYNO = 5;
145 	const char* KEY = "52a";
146 
147 	TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY));
148 
149 	return;
150 }
151 
152 void
test_EmptyKey(void)153 test_EmptyKey(void)
154 {
155 	const keyid_t KEYNO = 3;
156 	const char* KEY = "";
157 
158 
159 	TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY));
160 
161 	return;
162 }
163 
164 /* test the implementation of 'auth_log2' -- use a local copy of the code */
165 
166 static u_short
auth_log2(size_t x)167 auth_log2(
168 	size_t x)
169 {
170 	int	s;
171 	int	r = 0;
172 	size_t  m = ~(size_t)0;
173 
174 	for (s = sizeof(size_t) / 2 * CHAR_BIT; s != 0; s >>= 1) {
175 		m <<= s;
176 		if (x & m)
177 			r += s;
178 		else
179 			x <<= s;
180 	}
181 	return (u_short)r;
182 }
183 
184 void
test_auth_log2(void)185 test_auth_log2(void)
186 {
187 	int	l2;
188 	size_t	tv;
189 
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));
197 	}
198 }
199