Home | History | Annotate | Line # | Download | only in libntp
authkeys.c revision 1.1.1.5
      1 /*	$NetBSD: authkeys.c,v 1.1.1.5 2016/01/08 21:21:33 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 	return;
     58 }
     59 
     60 void
     61 tearDown(void)
     62 {
     63 	return;
     64 }
     65 
     66 static const int KEYTYPE = KEY_TYPE_MD5;
     67 
     68 void
     69 AddTrustedKey(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);
     76 
     77 	authtrust(keyno, TRUE);
     78 
     79 	return;
     80 }
     81 
     82 void
     83 AddUntrustedKey(keyid_t keyno)
     84 {
     85 	authtrust(keyno, FALSE);
     86 
     87 	return;
     88 }
     89 
     90 void
     91 test_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 
    105 void
    106 test_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 
    117 void
    118 test_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 
    130 void
    131 test_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 
    141 void
    142 test_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 
    152 void
    153 test_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