authkeys.c revision 294905
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#include <limits.h> 17 18u_long current_time = 4; 19int counter = 0; 20 21void setUp(void); 22void tearDown(void); 23void AddTrustedKey(keyid_t keyno); 24void AddUntrustedKey(keyid_t keyno); 25void test_AddTrustedKeys(void); 26void test_AddUntrustedKey(void); 27void test_HaveKeyCorrect(void); 28void test_HaveKeyIncorrect(void); 29void test_AddWithAuthUseKey(void); 30void test_EmptyKey(void); 31void test_auth_log2(void); 32 33 34void 35setUp(void) 36{ 37 if (counter == 0) { 38 counter++; 39 init_auth(); // causes segfault if called more than once 40 } 41 /* 42 * init_auth() is called by tests_main.cpp earlier. It 43 * does not initialize global variables like 44 * authnumkeys, so let's reset them to zero here. 45 */ 46 authnumkeys = 0; 47 48 /* 49 * Especially, empty the key cache! 50 */ 51 cache_keyid = 0; 52 cache_type = 0; 53 cache_flags = 0; 54 cache_secret = NULL; 55 cache_secretsize = 0; 56 57 return; 58} 59 60void 61tearDown(void) 62{ 63 return; 64} 65 66static const int KEYTYPE = KEY_TYPE_MD5; 67 68void 69AddTrustedKey(keyid_t keyno) 70{ 71 /* 72 * We need to add a MD5-key in addition to setting the 73 * trust, because authhavekey() requires type != 0. 74 */ 75 MD5auth_setkey(keyno, KEYTYPE, NULL, 0, NULL); 76 77 authtrust(keyno, TRUE); 78 79 return; 80} 81 82void 83AddUntrustedKey(keyid_t keyno) 84{ 85 authtrust(keyno, FALSE); 86 87 return; 88} 89 90void 91test_AddTrustedKeys(void) 92{ 93 const keyid_t KEYNO1 = 5; 94 const keyid_t KEYNO2 = 8; 95 96 AddTrustedKey(KEYNO1); 97 AddTrustedKey(KEYNO2); 98 99 TEST_ASSERT_TRUE(authistrusted(KEYNO1)); 100 TEST_ASSERT_TRUE(authistrusted(KEYNO2)); 101 102 return; 103} 104 105void 106test_AddUntrustedKey(void) 107{ 108 const keyid_t KEYNO = 3; 109 110 AddUntrustedKey(KEYNO); 111 112 TEST_ASSERT_FALSE(authistrusted(KEYNO)); 113 114 return; 115} 116 117void 118test_HaveKeyCorrect(void) 119{ 120 const keyid_t KEYNO = 3; 121 122 AddTrustedKey(KEYNO); 123 124 TEST_ASSERT_TRUE(auth_havekey(KEYNO)); 125 TEST_ASSERT_TRUE(authhavekey(KEYNO)); 126 127 return; 128} 129 130void 131test_HaveKeyIncorrect(void) 132{ 133 const keyid_t KEYNO = 2; 134 135 TEST_ASSERT_FALSE(auth_havekey(KEYNO)); 136 TEST_ASSERT_FALSE(authhavekey(KEYNO)); 137 138 return; 139} 140 141void 142test_AddWithAuthUseKey(void) 143{ 144 const keyid_t KEYNO = 5; 145 const char* KEY = "52a"; 146 147 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY)); 148 149 return; 150} 151 152void 153test_EmptyKey(void) 154{ 155 const keyid_t KEYNO = 3; 156 const char* KEY = ""; 157 158 159 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (const u_char*)KEY)); 160 161 return; 162} 163 164/* test the implementation of 'auth_log2' -- use a local copy of the code */ 165 166static u_short 167auth_log2( 168 size_t x) 169{ 170 int s; 171 int r = 0; 172 size_t m = ~(size_t)0; 173 174 for (s = sizeof(size_t) / 2 * CHAR_BIT; s != 0; s >>= 1) { 175 m <<= s; 176 if (x & m) 177 r += s; 178 else 179 x <<= s; 180 } 181 return (u_short)r; 182} 183 184void 185test_auth_log2(void) 186{ 187 int l2; 188 size_t tv; 189 190 TEST_ASSERT_EQUAL_INT(0, auth_log2(0)); 191 TEST_ASSERT_EQUAL_INT(0, auth_log2(1)); 192 for (l2 = 1; l2 < sizeof(size_t)*CHAR_BIT; ++l2) { 193 tv = (size_t)1 << l2; 194 TEST_ASSERT_EQUAL_INT(l2, auth_log2( tv )); 195 TEST_ASSERT_EQUAL_INT(l2, auth_log2( tv + 1 )); 196 TEST_ASSERT_EQUAL_INT(l2, auth_log2(2*tv - 1)); 197 } 198} 199