authkeys.c revision 1.1.1.4 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
17 u_long current_time = 4;
18 int counter = 0;
19
20 void setUp(void);
21 void tearDown(void);
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);
30
31
32 void
33 setUp(void)
34 {
35 if (counter == 0) {
36 counter++;
37 init_auth(); // causes segfault if called more than once
38 }
39 /*
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.
43 */
44 authnumkeys = 0;
45
46 /*
47 * Especially, empty the key cache!
48 */
49 cache_keyid = 0;
50 cache_type = 0;
51 cache_flags = 0;
52 cache_secret = NULL;
53 cache_secretsize = 0;
54
55 return;
56 }
57
58 void
59 tearDown(void)
60 {
61 return;
62 }
63
64 static const int KEYTYPE = KEY_TYPE_MD5;
65
66 void
67 AddTrustedKey(keyid_t keyno)
68 {
69 /*
70 * We need to add a MD5-key in addition to setting the
71 * trust, because authhavekey() requires type != 0.
72 */
73 MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
74
75 authtrust(keyno, TRUE);
76
77 return;
78 }
79
80 void
81 AddUntrustedKey(keyid_t keyno)
82 {
83 authtrust(keyno, FALSE);
84
85 return;
86 }
87
88 void
89 test_AddTrustedKeys(void)
90 {
91 const keyid_t KEYNO1 = 5;
92 const keyid_t KEYNO2 = 8;
93
94 AddTrustedKey(KEYNO1);
95 AddTrustedKey(KEYNO2);
96
97 TEST_ASSERT_TRUE(authistrusted(KEYNO1));
98 TEST_ASSERT_TRUE(authistrusted(KEYNO2));
99
100 return;
101 }
102
103 void
104 test_AddUntrustedKey(void)
105 {
106 const keyid_t KEYNO = 3;
107
108 AddUntrustedKey(KEYNO);
109
110 TEST_ASSERT_FALSE(authistrusted(KEYNO));
111
112 return;
113 }
114
115 void
116 test_HaveKeyCorrect(void)
117 {
118 const keyid_t KEYNO = 3;
119
120 AddTrustedKey(KEYNO);
121
122 TEST_ASSERT_TRUE(auth_havekey(KEYNO));
123 TEST_ASSERT_TRUE(authhavekey(KEYNO));
124
125 return;
126 }
127
128 void
129 test_HaveKeyIncorrect(void)
130 {
131 const keyid_t KEYNO = 2;
132
133 TEST_ASSERT_FALSE(auth_havekey(KEYNO));
134 TEST_ASSERT_FALSE(authhavekey(KEYNO));
135
136 return;
137 }
138
139 void
140 test_AddWithAuthUseKey(void)
141 {
142 const keyid_t KEYNO = 5;
143 const char* KEY = "52a";
144
145 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY));
146
147 return;
148 }
149
150 void
151 test_EmptyKey(void)
152 {
153 const keyid_t KEYNO = 3;
154 const char* KEY = "";
155
156
157 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY));
158
159 return;
160 }
161