]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_authkeys.cpp
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_authkeys.cpp
1 /* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */
2
3 #include "g_libntptest.h"
4
5 extern "C" {
6 #ifdef OPENSSL
7 # include "openssl/err.h"
8 # include "openssl/rand.h"
9 # include "openssl/evp.h"
10 #endif
11 #include "ntp.h"
12 #include "ntp_stdlib.h"
13 };
14
15 class authkeysTest : public libntptest {
16 protected:
17         static const int KEYTYPE = KEY_TYPE_MD5;
18
19         virtual void SetUp() {
20                 /*
21                  * init_auth() is called by tests_main.cpp earlier.  It
22                  * does not initialize global variables like
23                  * authnumkeys, so let's reset them to zero here.
24                  */
25                 authnumkeys = 0;
26
27                 /*
28                  * Especially, empty the key cache!
29                  */
30                 cache_keyid = 0;
31                 cache_type = 0;
32                 cache_flags = 0;
33                 cache_secret = NULL;
34                 cache_secretsize = 0;
35         }
36
37         void AddTrustedKey(keyid_t keyno) {
38                 /*
39                  * We need to add a MD5-key in addition to setting the
40                  * trust, because authhavekey() requires type != 0.
41                  */
42                 MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
43
44                 authtrust(keyno, TRUE);
45         }
46
47         void AddUntrustedKey(keyid_t keyno) {
48                 authtrust(keyno, FALSE);
49         }
50 };
51
52 TEST_F(authkeysTest, AddTrustedKeys) {
53         const keyid_t KEYNO1 = 5;
54         const keyid_t KEYNO2 = 8;
55
56         AddTrustedKey(KEYNO1);
57         AddTrustedKey(KEYNO2);
58
59         EXPECT_TRUE(authistrusted(KEYNO1));
60         EXPECT_TRUE(authistrusted(KEYNO2));
61 }
62
63 TEST_F(authkeysTest, AddUntrustedKey) {
64         const keyid_t KEYNO = 3;
65    
66         AddUntrustedKey(KEYNO);
67
68         EXPECT_FALSE(authistrusted(KEYNO));
69 }
70
71 TEST_F(authkeysTest, HaveKeyCorrect) {
72         const keyid_t KEYNO = 3;
73
74         AddTrustedKey(KEYNO);
75
76         EXPECT_TRUE(auth_havekey(KEYNO));
77         EXPECT_TRUE(authhavekey(KEYNO));
78 }
79
80 TEST_F(authkeysTest, HaveKeyIncorrect) {
81         const keyid_t KEYNO = 2;
82
83         EXPECT_FALSE(auth_havekey(KEYNO));
84         EXPECT_FALSE(authhavekey(KEYNO));
85 }
86
87 TEST_F(authkeysTest, AddWithAuthUseKey) {
88         const keyid_t KEYNO = 5;
89         const char* KEY = "52a";
90
91         EXPECT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));  
92 }
93
94 TEST_F(authkeysTest, EmptyKey) {
95         const keyid_t KEYNO = 3;
96         const char* KEY = "";
97
98
99         EXPECT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
100 }