authkeys.c revision 289999
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
17u_long current_time = 4;
18int counter = 0;
19
20void setUp(void);
21void tearDown(void);
22void AddTrustedKey(keyid_t keyno);
23void AddUntrustedKey(keyid_t keyno);
24void test_AddTrustedKeys(void);
25void test_AddUntrustedKey(void);
26void test_HaveKeyCorrect(void);
27void test_HaveKeyIncorrect(void);
28void test_AddWithAuthUseKey(void);
29void test_EmptyKey(void);
30
31
32void
33setUp(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
56void
57tearDown(void)
58{
59
60}
61
62static const int KEYTYPE = KEY_TYPE_MD5;
63
64void
65AddTrustedKey(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
75void
76AddUntrustedKey(keyid_t keyno) {
77	authtrust(keyno, FALSE);
78}
79
80void
81test_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
92void
93test_AddUntrustedKey(void) {
94	const keyid_t KEYNO = 3;
95
96	AddUntrustedKey(KEYNO);
97
98	TEST_ASSERT_FALSE(authistrusted(KEYNO));
99}
100
101void
102test_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
111void
112test_HaveKeyIncorrect(void) {
113	const keyid_t KEYNO = 2;
114
115	TEST_ASSERT_FALSE(auth_havekey(KEYNO));
116	TEST_ASSERT_FALSE(authhavekey(KEYNO));
117}
118
119void
120test_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
127void
128test_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}
135