1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (C) 2021-2022 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Kea Hooks Basic
// Commercial End User License Agreement v2.0. See COPYING file in the premium/
// directory.

#include <config.h>

#include <dns/name.h>
#include <gss_tsig_context.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <managed_key.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_utils.h>
#include <testutils/test_to_element.h>
#include <util/chrono_time_utils.h>
#include <gtest/gtest.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace std::chrono;
using namespace isc;
using namespace isc::data;
using namespace isc::dns;
using namespace isc::gss_tsig;
using namespace isc::test;
using namespace isc::util;

namespace {

/// @brief Check managed GSS-TSIG key.
TEST(ManagedKeyTest, basic) {
    ManagedKeyPtr key;
    string name = "1234.sig-foo.example.com.";
    ASSERT_NO_THROW(key.reset(new ManagedKey(name)));
    ASSERT_TRUE(key);
    EXPECT_EQ(Name(name), key->getKeyName());
    EXPECT_EQ(name, key->getKeyNameStr());

    // Parent.
    EXPECT_EQ("", key->getParentID());
    EXPECT_NO_THROW(key->setParentID("foo"));
    EXPECT_EQ("foo", key->getParentID());

    // Status.
    EXPECT_EQ(ManagedKey::NOT_READY, key->getStatus());
    EXPECT_NO_THROW(key->setStatus(ManagedKey::USABLE));
    EXPECT_EQ(ManagedKey::USABLE, key->getStatus());

    // TKEY status.
    EXPECT_EQ(TKeyExchange::OTHER, key->getTKeyStatus());
    EXPECT_NO_THROW(key->setTKeyStatus(TKeyExchange::IO_STOPPED));
    EXPECT_EQ(TKeyExchange::IO_STOPPED, key->getTKeyStatus());

    // TKEY exchange.
    EXPECT_FALSE(key->getTKeyExchange());

    // Set inception and expire.
    system_clock::time_point now = system_clock::now();
    EXPECT_NO_THROW(key->setInception(now));
    std::chrono::hours day(24);
    system_clock::time_point tomorrow = now + day;
    EXPECT_NO_THROW(key->setExpire(tomorrow));

    // TKEY completion handler.

    // Success.
    EXPECT_NO_THROW((*key)(TKeyExchange::SUCCESS));
    EXPECT_EQ(TKeyExchange::SUCCESS, key->getTKeyStatus());
    EXPECT_EQ(ManagedKey::USABLE, key->getStatus());

    // Others.
    EXPECT_NO_THROW((*key)(TKeyExchange::TIMEOUT));
    EXPECT_EQ(TKeyExchange::TIMEOUT, key->getTKeyStatus());
    EXPECT_EQ(ManagedKey::IN_ERROR, key->getStatus());

    EXPECT_NO_THROW((*key)(TKeyExchange::IO_STOPPED));
    EXPECT_EQ(TKeyExchange::IO_STOPPED, key->getTKeyStatus());
    EXPECT_EQ(ManagedKey::IN_ERROR, key->getStatus());

    EXPECT_NO_THROW((*key)(TKeyExchange::INVALID_RESPONSE));
    EXPECT_EQ(TKeyExchange::INVALID_RESPONSE, key->getTKeyStatus());
    EXPECT_EQ(ManagedKey::IN_ERROR, key->getStatus());

    EXPECT_NO_THROW((*key)(TKeyExchange::UNSIGNED_RESPONSE));
    EXPECT_EQ(TKeyExchange::UNSIGNED_RESPONSE, key->getTKeyStatus());
    EXPECT_EQ(ManagedKey::IN_ERROR, key->getStatus());

    EXPECT_NO_THROW((*key)(TKeyExchange::BAD_CREDENTIALS));
    EXPECT_EQ(TKeyExchange::BAD_CREDENTIALS, key->getTKeyStatus());
    EXPECT_EQ(ManagedKey::IN_ERROR, key->getStatus());

    EXPECT_NO_THROW((*key)(TKeyExchange::OTHER));
    EXPECT_EQ(TKeyExchange::OTHER, key->getTKeyStatus());
    EXPECT_EQ(ManagedKey::IN_ERROR, key->getStatus());

    // Note that the EXPIRED status is set by users and is in fact
    // only a shortcut i.e. can be removed leaving expire > now check.
}

/// @brief Check managed GSS-TSIG key toElement method.
TEST(ManagedKeyTest, toElement) {<--- syntax error
    ManagedKeyPtr key;
    const string& name("1234.sig-foo.example.com.");
    ASSERT_NO_THROW(key.reset(new ManagedKey(name)));
    const string& id("foo");
    key->setParentID(id);
    system_clock::time_point now = system_clock::now();
    key->setInception(now);
    std::chrono::hours day(24);
    system_clock::time_point tomorrow = now + day;
    key->setExpire(tomorrow);
    ElementPtr expected = Element::createMap();
    expected->set("name", Element::create(name));
    expected->set("server-id", Element::create(id));
    expected->set("status", Element::create(string("not yet ready")));
    expected->set("tkey-exchange", Element::create(false));
    expected->set("inception-date", Element::create(clockToText(now)));
    expected->set("expire-date", Element::create(clockToText(tomorrow)));
    runToElementTest<ManagedKey>(expected, *key);
    expected->remove("tkey-exchange");

    // Usable.
    key->setStatus(ManagedKey::USABLE);
    expected->set("status", Element::create(string("usable")));
    runToElementTest<ManagedKey>(expected, *key);

    // Expired.
    key->setStatus(ManagedKey::EXPIRED);
    expected->set("status", Element::create(string("expired")));
    runToElementTest<ManagedKey>(expected, *key);

    // Error (timeout).
    key->setStatus(ManagedKey::IN_ERROR);
    key->setTKeyStatus(TKeyExchange::TIMEOUT);
    expected->set("status", Element::create(string("in error")));
    expected->set("tkey-status",
                  Element::create(string("no response, timeout")));
    runToElementTest<ManagedKey>(expected, *key);
}

/// @brief name generation.
TEST(ManagedKeyTest, genName) {
    string server = "foo.bar";
    string name = ManagedKey::genName(server);
    size_t dot = name.find_first_of(".");
    ASSERT_NE(dot, string::npos);
    EXPECT_EQ(server, name.substr(dot + 1));

    string name2 = ManagedKey::genName(server);
    EXPECT_NE(name, name2);
    dot = name2.find_first_of(".");
    ASSERT_NE(dot, string::npos);
    EXPECT_EQ(server, name2.substr(dot + 1));

    string name3 = ManagedKey::genName(server);
    EXPECT_NE(name, name3);
    EXPECT_NE(name2, name3);
}

/// @brief create Context.
TEST(ManagedKeyTest, createContext) {
    ManagedKeyPtr key;
    string name = "1234.sig-foo.example.com.";
    ASSERT_NO_THROW(key.reset(new ManagedKey(name)));
    ASSERT_TRUE(key);

    // Now create the context.
    TSIGContextPtr octx;
    ASSERT_NO_THROW(octx = key->createContext());
    ASSERT_TRUE(octx);
    EXPECT_EQ(TSIGContext::INIT, octx->getState());
    EXPECT_EQ(TSIGError::NOERROR(), octx->getError());
    GssTsigContextPtr ctx = boost::dynamic_pointer_cast<GssTsigContext>(octx);
    ASSERT_TRUE(ctx);
}

}