]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/authkeys.c
Upgrade NTP to 4.2.8p4.
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / authkeys.c
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
56 void
57 tearDown(void)
58 {
59
60 }
61
62 static const int KEYTYPE = KEY_TYPE_MD5;
63
64 void
65 AddTrustedKey(keyid_t keyno) {
66         /*
67          * We need to add a MD5-key in addition to setting the
68          * trust, because authhavekey() requires type != 0.
69          */
70         MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
71
72         authtrust(keyno, TRUE);
73 }
74
75 void
76 AddUntrustedKey(keyid_t keyno) {
77         authtrust(keyno, FALSE);
78 }
79
80 void
81 test_AddTrustedKeys(void) {
82         const keyid_t KEYNO1 = 5;
83         const keyid_t KEYNO2 = 8;
84
85         AddTrustedKey(KEYNO1);
86         AddTrustedKey(KEYNO2);
87
88         TEST_ASSERT_TRUE(authistrusted(KEYNO1));
89         TEST_ASSERT_TRUE(authistrusted(KEYNO2));
90 }
91
92 void
93 test_AddUntrustedKey(void) {
94         const keyid_t KEYNO = 3;
95    
96         AddUntrustedKey(KEYNO);
97
98         TEST_ASSERT_FALSE(authistrusted(KEYNO));
99 }
100
101 void
102 test_HaveKeyCorrect(void) {
103         const keyid_t KEYNO = 3;
104
105         AddTrustedKey(KEYNO);
106
107         TEST_ASSERT_TRUE(auth_havekey(KEYNO));
108         TEST_ASSERT_TRUE(authhavekey(KEYNO));
109 }
110
111 void
112 test_HaveKeyIncorrect(void) {
113         const keyid_t KEYNO = 2;
114
115         TEST_ASSERT_FALSE(auth_havekey(KEYNO));
116         TEST_ASSERT_FALSE(authhavekey(KEYNO));
117 }
118
119 void
120 test_AddWithAuthUseKey(void) {
121         const keyid_t KEYNO = 5;
122         const char* KEY = "52a";
123
124         TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));     
125 }
126
127 void
128 test_EmptyKey(void) {
129         const keyid_t KEYNO = 3;
130         const char* KEY = "";
131
132
133         TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
134 }