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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright (C) 2017-2024 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.

/// @file This file contains tests which verify control command legal file entry
/// generation and callout: command_processed.
/// These tests assume the legal log library is linked in, not loaded.
/// This allows a great deal more flexibility in testing, such as overriding
/// and accessing the BackendStore::instance().
/// The load and unload callouts are exercised in ../libloadtests, which
/// actually uses the HooksManager to load and unload the library.

#include <config.h>

#include <asiolink/io_service.h>
#include <d2srv/d2_config.h>
#include <gss_tsig_api_utils.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gss_tsig_impl.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <hooks/callout_manager.h>
#include <hooks/hooks.h>
#include <testutils/gss_tsig_dns_server.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <testutils/gtest_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 isc;
using namespace isc::asiolink;
using namespace isc::data;
using namespace isc::d2;
using namespace isc::gss_tsig;
using namespace isc::gss_tsig::test;
using namespace isc::hooks;
using namespace std;

extern "C" {
extern int select_key(CalloutHandle& handle);
}

namespace isc {
namespace gss_tsig {
extern GssTsigImplPtr impl;
}
}

namespace {

const long TEST_TIMEOUT = 5 * 100; // expressed in milliseconds.

/// @brief Test fixture for exercising flex-id library callouts
/// It fetches the CalloutManager and prepares stub packets that can be used in
/// tests.
class CalloutTest : public GssApiBaseTest {
public:
    /// @brief Constructor.
    CalloutTest() : io_service_(new IOService()), test_timer_(io_service_),
            co_manager_(new CalloutManager(1)), cfg_mgr_(new D2CfgMgr()) {
        impl.reset(new GssTsigImpl());
        string config = "{\n"
            "\"ip-address\": \"127.0.0.1\",\n"
            "\"port\": 53001,\n"
            "\"dns-server-timeout\" : 1000,\n"
            "\"forward-ddns\": {\n"
            " \"ddns-domains\": [\n"
            "  {\n"
            "   \"name\": \"secure.example.org.\",\n"
            "   \"dns-servers\": [\n"
            "    {\n"
            "     \"ip-address\": \"127.0.0.1\",\n"
            "     \"port\": 5376\n"
            "    }\n"
            "   ]\n"
            "  }\n"
            " ]\n"
            "},\n"
            "\"reverse-ddns\": {\n"
            " \"ddns-domains\": [\n"
            "  {\n"
            "   \"name\": \"0.0.127.in-addr.arpa.\",\n"
            "   \"dns-servers\": [\n"
            "    {\n"
            "     \"ip-address\": \"127.0.0.1\",\n"
            "     \"port\": 5376\n"
            "    }\n"
            "   ]\n"
            "  }\n"
            " ]\n"
            "}\n"
            "}\n";
        ConstElementPtr config_set = Element::fromJSON(config);
        cfg_mgr_->simpleParseConfig(config_set);
        string hook_config = "{\n"
            "\"server-principal\": \"DNS/blu.example.nil@EXAMPLE.NIL\",\n"
            "\"client-principal\": \"administrator@EXAMPLE.NIL\",\n"
            "\"tkey-lifetime\": 7200,\n"
            "\"tkey-protocol\": \"UDP\",\n"
            "\"servers\": [\n"
            " {\n"
            "  \"domain-names\": [ ],\n"
            "  \"id\": \"foo\",\n"
            "  \"ip-address\": \"127.0.0.1\",\n"
            "  \"port\": 5376,\n"
            "  \"tkey-lifetime\": 86400,\n"
            "  \"tkey-protocol\": \"UDP\"\n"
            " }\n"
            "]\n"
            "}\n";
        setKeytab();
        setAdministratorCCache();
        ConstElementPtr hook_config_set = Element::fromJSON(hook_config);
        impl->configure(hook_config_set);
        D2CfgContextPtr d2_config(cfg_mgr_->getD2CfgContext());
        impl->setIoContext(io_service_);
        impl->finishConfigure(d2_config);
        EXPECT_NO_THROW(legacy_key_.reset(new TSIGKeyInfo("test_key",
                                                          TSIGKeyInfo::HMAC_MD5_STR,
                                                          "GWG/Xfbju4O2iXGqkSu4PQ==")));
        // Set the test timeout to break any running tasks if they hang.
        test_timer_.setup(std::bind(&CalloutTest::testTimeoutHandler, this),
                          TEST_TIMEOUT);
        impl->start();
    }

    /// @brief Destructor.
    ~CalloutTest() {
        impl.reset();
        test_timer_.cancel();
        io_service_->stop();
        io_service_->restart();
        try {
            io_service_->poll();
        } catch (...) {
        }
    }

    /// @brief Fetches the callout manager instance.
    boost::shared_ptr<CalloutManager>getCalloutManager() {
        return (co_manager_);
    }

    /// @brief Handler invoked when test timeout is hit
    ///
    /// This callback stops all running (hanging) tasks on IO service.
    void testTimeoutHandler() {
        io_service_->stop();
    }

    /// @brief The IOService which handles IO operations.
    IOServicePtr io_service_;

    /// @brief The timeout timer.
    asiolink::IntervalTimer test_timer_;

    /// @brief Callout manager accessed by this CalloutHandle.
    boost::shared_ptr<CalloutManager> co_manager_;

    /// @brief Config manager used to store DNS servers.
    boost::shared_ptr<D2CfgMgr> cfg_mgr_;

    /// @brief The TSIG Key (non GSS-TSIG key).
    TSIGKeyInfoPtr legacy_key_;

    /// @brief Dummy DNS server.
    boost::shared_ptr<DummyDNSServer> dns_server_;
};

TEST_F(CalloutTest, select_keyNoServerArgument) {<--- syntax error
    CalloutHandle handle(getCalloutManager());
    ASSERT_THROW_MSG(select_key(handle), NoSuchArgument,
                     "unable to find argument with name current_server");
}

TEST_F(CalloutTest, select_keyNoTSIGKeyArgument) {
    CalloutHandle handle(getCalloutManager());
    handle.setArgument("current_server", DnsServerInfoPtr());
    ASSERT_THROW_MSG(select_key(handle), NoSuchArgument,
                     "unable to find argument with name tsig_key");
}

TEST_F(CalloutTest, select_keyNoGSSWithNoFallback) {
    CalloutHandle handle(getCalloutManager());
    DnsServerPtr server = impl->getServer("foo");
    ASSERT_TRUE(server);
    ASSERT_TRUE(server->getServerInfos().size());
    DnsServerInfoPtr info = server->getServerInfos()[0];
    handle.setArgument("current_server", info);
    handle.setArgument("tsig_key", legacy_key_->getTSIGKey());
    int ret = select_key(handle);
    ASSERT_EQ(ret, 0);
    // If no GSS-TSIG TKEY is available, the non GSS-TSIG key is not used and
    // the server is skipped by updating the callout status.
    ASSERT_EQ(handle.getStatus(), CalloutHandle::NEXT_STEP_SKIP);
    D2TsigKeyPtr key;
    handle.getArgument("tsig_key", key);
    ASSERT_EQ(key, legacy_key_->getTSIGKey());
}

TEST_F(CalloutTest, select_keyNoGSSWithFallback) {
    CalloutHandle handle(getCalloutManager());
    DnsServerPtr server = impl->getServer("foo");
    ASSERT_TRUE(server);
    ASSERT_TRUE(server->getServerInfos().size());
    server->setFallback(true);
    DnsServerInfoPtr info = server->getServerInfos()[0];
    handle.setArgument("current_server", info);
    handle.setArgument("tsig_key", legacy_key_->getTSIGKey());
    int ret = select_key(handle);
    ASSERT_EQ(ret, 0);
    // If no GSS-TSIG TKEY is available, the non GSS-TSIG key is used.
    ASSERT_EQ(handle.getStatus(), CalloutHandle::NEXT_STEP_CONTINUE);
    D2TsigKeyPtr key;
    handle.getArgument("tsig_key", key);
    ASSERT_EQ(key, legacy_key_->getTSIGKey());
}

TEST_F(CalloutTest, select_keyGSSWithNoFallback) {
    CalloutHandle handle(getCalloutManager());
    dns_server_.reset(new DummyDNSServer(io_service_, true, true, false, true, false));
    dns_server_->start();
    io_service_->run();
    DnsServerPtr server = impl->getServer("foo");
    ASSERT_TRUE(server);
    ASSERT_TRUE(server->getServerInfos().size());
    DnsServerInfoPtr info = server->getServerInfos()[0];
    handle.setArgument("current_server", info);
    handle.setArgument("tsig_key", legacy_key_->getTSIGKey());
    int ret = select_key(handle);
    ASSERT_EQ(ret, 0);
    // If GSS-TSIG TKEY is available it is used instead on the non GSS-TSIG key.
    ASSERT_EQ(handle.getStatus(), CalloutHandle::NEXT_STEP_CONTINUE);
    D2TsigKeyPtr key;
    handle.getArgument("tsig_key", key);
    ASSERT_TRUE(key);
    ASSERT_NE(key, legacy_key_->getTSIGKey());
    ManagedKeyPtr mkey = boost::dynamic_pointer_cast<ManagedKey>(key);
    ASSERT_TRUE(mkey);
}

TEST_F(CalloutTest, select_keyGSSWithFallback) {
    CalloutHandle handle(getCalloutManager());
    dns_server_.reset(new DummyDNSServer(io_service_, true, true, false, true, false));
    dns_server_->start();
    io_service_->run();
    DnsServerPtr server = impl->getServer("foo");
    ASSERT_TRUE(server);
    ASSERT_TRUE(server->getServerInfos().size());
    server->setFallback(true);
    DnsServerInfoPtr info = server->getServerInfos()[0];
    handle.setArgument("current_server", info);
    handle.setArgument("tsig_key", legacy_key_->getTSIGKey());
    int ret = select_key(handle);
    ASSERT_EQ(ret, 0);
    // If GSS-TSIG TKEY is available it is used instead on the non GSS-TSIG key.
    ASSERT_EQ(handle.getStatus(), CalloutHandle::NEXT_STEP_CONTINUE);
    D2TsigKeyPtr key;
    handle.getArgument("tsig_key", key);
    ASSERT_TRUE(key);
    ASSERT_NE(key, legacy_key_->getTSIGKey());
    ManagedKeyPtr mkey = boost::dynamic_pointer_cast<ManagedKey>(key);
    ASSERT_TRUE(mkey);
}

}