1 /* $NetBSD: authkeys.c,v 1.1.1.3 2015/10/23 17:47:45 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 19 u_long current_time = 4; 20 int counter = 0; 21 22 void setUp(void); 23 void tearDown(void); 24 void AddTrustedKey(keyid_t keyno); 25 void AddUntrustedKey(keyid_t keyno); 26 void test_AddTrustedKeys(void); 27 void test_AddUntrustedKey(void); 28 void test_HaveKeyCorrect(void); 29 void test_HaveKeyIncorrect(void); 30 void test_AddWithAuthUseKey(void); 31 void test_EmptyKey(void); 32 33 34 void 35 setUp(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 58 void 59 tearDown(void) 60 { 61 62 } 63 64 static const int KEYTYPE = KEY_TYPE_MD5; 65 66 void 67 AddTrustedKey(keyid_t keyno) { 68 /* 69 * We need to add a MD5-key in addition to setting the 70 * trust, because authhavekey() requires type != 0. 71 */ 72 MD5auth_setkey(keyno, KEYTYPE, NULL, 0); 73 74 authtrust(keyno, TRUE); 75 } 76 77 void 78 AddUntrustedKey(keyid_t keyno) { 79 authtrust(keyno, FALSE); 80 } 81 82 void 83 test_AddTrustedKeys(void) { 84 const keyid_t KEYNO1 = 5; 85 const keyid_t KEYNO2 = 8; 86 87 AddTrustedKey(KEYNO1); 88 AddTrustedKey(KEYNO2); 89 90 TEST_ASSERT_TRUE(authistrusted(KEYNO1)); 91 TEST_ASSERT_TRUE(authistrusted(KEYNO2)); 92 } 93 94 void 95 test_AddUntrustedKey(void) { 96 const keyid_t KEYNO = 3; 97 98 AddUntrustedKey(KEYNO); 99 100 TEST_ASSERT_FALSE(authistrusted(KEYNO)); 101 } 102 103 void 104 test_HaveKeyCorrect(void) { 105 const keyid_t KEYNO = 3; 106 107 AddTrustedKey(KEYNO); 108 109 TEST_ASSERT_TRUE(auth_havekey(KEYNO)); 110 TEST_ASSERT_TRUE(authhavekey(KEYNO)); 111 } 112 113 void 114 test_HaveKeyIncorrect(void) { 115 const keyid_t KEYNO = 2; 116 117 TEST_ASSERT_FALSE(auth_havekey(KEYNO)); 118 TEST_ASSERT_FALSE(authhavekey(KEYNO)); 119 } 120 121 void 122 test_AddWithAuthUseKey(void) { 123 const keyid_t KEYNO = 5; 124 const char* KEY = "52a"; 125 126 TEST_ASSERT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY)); 127 } 128 129 void 130 test_EmptyKey(void) { 131 const keyid_t KEYNO = 3; 132 const char* KEY = ""; 133 134 135 TEST_ASSERT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY)); 136 } 137