1 /* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */
6 #include "ntp_stdlib.h"
7 #include "ntp_calendar.h"
12 # include "openssl/err.h"
13 # include "openssl/rand.h"
14 # include "openssl/evp.h"
17 u_long current_time = 4;
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);
37 init_auth(); // causes segfault if called more than once
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.
47 * Especially, empty the key cache!
62 static const int KEYTYPE = KEY_TYPE_MD5;
65 AddTrustedKey(keyid_t keyno) {
67 * We need to add a MD5-key in addition to setting the
68 * trust, because authhavekey() requires type != 0.
70 MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
72 authtrust(keyno, TRUE);
76 AddUntrustedKey(keyid_t keyno) {
77 authtrust(keyno, FALSE);
81 test_AddTrustedKeys(void) {
82 const keyid_t KEYNO1 = 5;
83 const keyid_t KEYNO2 = 8;
85 AddTrustedKey(KEYNO1);
86 AddTrustedKey(KEYNO2);
88 TEST_ASSERT_TRUE(authistrusted(KEYNO1));
89 TEST_ASSERT_TRUE(authistrusted(KEYNO2));
93 test_AddUntrustedKey(void) {
94 const keyid_t KEYNO = 3;
96 AddUntrustedKey(KEYNO);
98 TEST_ASSERT_FALSE(authistrusted(KEYNO));
102 test_HaveKeyCorrect(void) {
103 const keyid_t KEYNO = 3;
105 AddTrustedKey(KEYNO);
107 TEST_ASSERT_TRUE(auth_havekey(KEYNO));
108 TEST_ASSERT_TRUE(authhavekey(KEYNO));
112 test_HaveKeyIncorrect(void) {
113 const keyid_t KEYNO = 2;
115 TEST_ASSERT_FALSE(auth_havekey(KEYNO));
116 TEST_ASSERT_FALSE(authhavekey(KEYNO));
120 test_AddWithAuthUseKey(void) {
121 const keyid_t KEYNO = 5;
122 const char* KEY = "52a";
124 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
128 test_EmptyKey(void) {
129 const keyid_t KEYNO = 3;
130 const char* KEY = "";
133 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));