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;
21 // old code from google test framework, moved to SetUp() for unity
27 init_auth(); //causes segfault if called more than once
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.
37 * Especially, empty the key cache!
52 static const int KEYTYPE = KEY_TYPE_MD5;
57 void AddTrustedKey(keyid_t keyno) {
59 * We need to add a MD5-key in addition to setting the
60 * trust, because authhavekey() requires type != 0.
62 MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
64 authtrust(keyno, TRUE);
67 void AddUntrustedKey(keyid_t keyno) {
68 authtrust(keyno, FALSE);
71 void test_AddTrustedKeys() {
72 const keyid_t KEYNO1 = 5;
73 const keyid_t KEYNO2 = 8;
75 AddTrustedKey(KEYNO1);
76 AddTrustedKey(KEYNO2);
78 TEST_ASSERT_TRUE(authistrusted(KEYNO1));
79 TEST_ASSERT_TRUE(authistrusted(KEYNO2));
82 void test_AddUntrustedKey() {
83 const keyid_t KEYNO = 3;
85 AddUntrustedKey(KEYNO);
87 TEST_ASSERT_FALSE(authistrusted(KEYNO));
90 void test_HaveKeyCorrect() {
91 const keyid_t KEYNO = 3;
95 TEST_ASSERT_TRUE(auth_havekey(KEYNO));
96 TEST_ASSERT_TRUE(authhavekey(KEYNO));
99 void test_HaveKeyIncorrect() {
100 const keyid_t KEYNO = 2;
102 TEST_ASSERT_FALSE(auth_havekey(KEYNO));
103 TEST_ASSERT_FALSE(authhavekey(KEYNO));
106 void test_AddWithAuthUseKey() {
107 const keyid_t KEYNO = 5;
108 const char* KEY = "52a";
110 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
113 void test_EmptyKey() {
114 const keyid_t KEYNO = 3;
115 const char* KEY = "";
118 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));