authkeys.c revision 293894
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 return; 56} 57 58void 59tearDown(void) 60{ 61 return; 62} 63 64static const int KEYTYPE = KEY_TYPE_MD5; 65 66void 67AddTrustedKey(keyid_t keyno) 68{ 69 /* 70 * We need to add a MD5-key in addition to setting the 71 * trust, because authhavekey() requires type != 0. 72 */ 73 MD5auth_setkey(keyno, KEYTYPE, NULL, 0); 74 75 authtrust(keyno, TRUE); 76 77 return; 78} 79 80void 81AddUntrustedKey(keyid_t keyno) 82{ 83 authtrust(keyno, FALSE); 84 85 return; 86} 87 88void 89test_AddTrustedKeys(void) 90{ 91 const keyid_t KEYNO1 = 5; 92 const keyid_t KEYNO2 = 8; 93 94 AddTrustedKey(KEYNO1); 95 AddTrustedKey(KEYNO2); 96 97 TEST_ASSERT_TRUE(authistrusted(KEYNO1)); 98 TEST_ASSERT_TRUE(authistrusted(KEYNO2)); 99 100 return; 101} 102 103void 104test_AddUntrustedKey(void) 105{ 106 const keyid_t KEYNO = 3; 107 108 AddUntrustedKey(KEYNO); 109 110 TEST_ASSERT_FALSE(authistrusted(KEYNO)); 111 112 return; 113} 114 115void 116test_HaveKeyCorrect(void) 117{ 118 const keyid_t KEYNO = 3; 119 120 AddTrustedKey(KEYNO); 121 122 TEST_ASSERT_TRUE(auth_havekey(KEYNO)); 123 TEST_ASSERT_TRUE(authhavekey(KEYNO)); 124 125 return; 126} 127 128void 129test_HaveKeyIncorrect(void) 130{ 131 const keyid_t KEYNO = 2; 132 133 TEST_ASSERT_FALSE(auth_havekey(KEYNO)); 134 TEST_ASSERT_FALSE(authhavekey(KEYNO)); 135 136 return; 137} 138 139void 140test_AddWithAuthUseKey(void) 141{ 142 const keyid_t KEYNO = 5; 143 const char* KEY = "52a"; 144 145 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY)); 146 147 return; 148} 149 150void 151test_EmptyKey(void) 152{ 153 const keyid_t KEYNO = 3; 154 const char* KEY = ""; 155 156 157 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY)); 158 159 return; 160} 161