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