authkeys.c revision 1.1.1.1 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
21 // old code from google test framework, moved to SetUp() for unity
22 void setUp(void)
23 {
24 // init_lib();
25 if(counter ==0){
26 counter++;
27 init_auth(); //causes segfault if called more than once
28 }
29 /*
30 * init_auth() is called by tests_main.cpp earlier. It
31 * does not initialize global variables like
32 * authnumkeys, so let's reset them to zero here.
33 */
34 authnumkeys = 0;
35
36 /*
37 * Especially, empty the key cache!
38 */
39 cache_keyid = 0;
40 cache_type = 0;
41 cache_flags = 0;
42 cache_secret = NULL;
43 cache_secretsize = 0;
44
45 }
46
47 void tearDown(void)
48 {
49 }
50
51
52 static const int KEYTYPE = KEY_TYPE_MD5;
53
54
55
56
57 void AddTrustedKey(keyid_t keyno) {
58 /*
59 * We need to add a MD5-key in addition to setting the
60 * trust, because authhavekey() requires type != 0.
61 */
62 MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
63
64 authtrust(keyno, TRUE);
65 }
66
67 void AddUntrustedKey(keyid_t keyno) {
68 authtrust(keyno, FALSE);
69 }
70
71 void test_AddTrustedKeys() {
72 const keyid_t KEYNO1 = 5;
73 const keyid_t KEYNO2 = 8;
74
75 AddTrustedKey(KEYNO1);
76 AddTrustedKey(KEYNO2);
77
78 TEST_ASSERT_TRUE(authistrusted(KEYNO1));
79 TEST_ASSERT_TRUE(authistrusted(KEYNO2));
80 }
81
82 void test_AddUntrustedKey() {
83 const keyid_t KEYNO = 3;
84
85 AddUntrustedKey(KEYNO);
86
87 TEST_ASSERT_FALSE(authistrusted(KEYNO));
88 }
89
90 void test_HaveKeyCorrect() {
91 const keyid_t KEYNO = 3;
92
93 AddTrustedKey(KEYNO);
94
95 TEST_ASSERT_TRUE(auth_havekey(KEYNO));
96 TEST_ASSERT_TRUE(authhavekey(KEYNO));
97 }
98
99 void test_HaveKeyIncorrect() {
100 const keyid_t KEYNO = 2;
101
102 TEST_ASSERT_FALSE(auth_havekey(KEYNO));
103 TEST_ASSERT_FALSE(authhavekey(KEYNO));
104 }
105
106 void test_AddWithAuthUseKey() {
107 const keyid_t KEYNO = 5;
108 const char* KEY = "52a";
109
110 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
111 }
112
113 void test_EmptyKey() {
114 const keyid_t KEYNO = 3;
115 const char* KEY = "";
116
117
118 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
119 }
120