1 /* This file contains test for both libntp/authkeys.c and libntp/authusekey.c */
3 #include "g_libntptest.h"
7 # include "openssl/err.h"
8 # include "openssl/rand.h"
9 # include "openssl/evp.h"
12 #include "ntp_stdlib.h"
15 class authkeysTest : public libntptest {
17 static const int KEYTYPE = KEY_TYPE_MD5;
19 virtual void SetUp() {
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.
28 * Especially, empty the key cache!
37 void AddTrustedKey(keyid_t keyno) {
39 * We need to add a MD5-key in addition to setting the
40 * trust, because authhavekey() requires type != 0.
42 MD5auth_setkey(keyno, KEYTYPE, NULL, 0);
44 authtrust(keyno, TRUE);
47 void AddUntrustedKey(keyid_t keyno) {
48 authtrust(keyno, FALSE);
52 TEST_F(authkeysTest, AddTrustedKeys) {
53 const keyid_t KEYNO1 = 5;
54 const keyid_t KEYNO2 = 8;
56 AddTrustedKey(KEYNO1);
57 AddTrustedKey(KEYNO2);
59 EXPECT_TRUE(authistrusted(KEYNO1));
60 EXPECT_TRUE(authistrusted(KEYNO2));
63 TEST_F(authkeysTest, AddUntrustedKey) {
64 const keyid_t KEYNO = 3;
66 AddUntrustedKey(KEYNO);
68 EXPECT_FALSE(authistrusted(KEYNO));
71 TEST_F(authkeysTest, HaveKeyCorrect) {
72 const keyid_t KEYNO = 3;
76 EXPECT_TRUE(auth_havekey(KEYNO));
77 EXPECT_TRUE(authhavekey(KEYNO));
80 TEST_F(authkeysTest, HaveKeyIncorrect) {
81 const keyid_t KEYNO = 2;
83 EXPECT_FALSE(auth_havekey(KEYNO));
84 EXPECT_FALSE(authhavekey(KEYNO));
87 TEST_F(authkeysTest, AddWithAuthUseKey) {
88 const keyid_t KEYNO = 5;
89 const char* KEY = "52a";
91 EXPECT_TRUE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));
94 TEST_F(authkeysTest, EmptyKey) {
95 const keyid_t KEYNO = 3;
99 EXPECT_FALSE(authusekey(KEYNO, KEYTYPE, (u_char*)KEY));