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