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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Copyright (C) 2021-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 exercise the load and unload
/// functions in the high availability hook library. In order to test
/// the load function, one must be able to pass it hook library
/// parameters. The the only way to populate these parameters is by
/// actually loading the library via HooksManager::loadLibraries().

#include <config.h>

#include <asiolink/io_service.h>
#include <cc/command_interpreter.h>
#include <d2srv/d2_cfg_mgr.h>
#include <exceptions/exceptions.h>
#include <hooks/hooks.h>
#include <hooks/hooks_manager.h>
#include <process/daemon.h>
#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.
#include <errno.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

using namespace std;
using namespace isc;
using namespace isc::asiolink;
using namespace isc::config;
using namespace isc::d2;
using namespace isc::data;
using namespace isc::hooks;
using namespace isc::process;

namespace {

/// @brief Structure that holds registered hook indexes.
struct TestHooks {
    /// @brief Index of d2_srv_configured callout.
    int hook_index_d2_srv_configured_;

    /// @brief Constructor
    ///
    /// The constructor registers hook points for callout tests.
    TestHooks() {
        hook_index_d2_srv_configured_ =
            HooksManager::registerHook("d2_srv_configured");
    }
};

TestHooks testHooks;

/// @brief Test fixture for testing loading and unloading the GSS-TSIG library.
class LibLoadTest : public ::testing::Test {
public:
    /// @brief Constructor.
    LibLoadTest() {
        reset();
    }

    /// @brief Destructor.
    /// Removes files that may be left over from previous tests.
    virtual ~LibLoadTest() {
        reset();
    }

    /// @brief Removes files that may be left over from previous tests.
    virtual void reset() {
        HooksManager::unloadLibraries();
    }

    /// @brief Adds library/parameters to list of libraries to be loaded.
    void addLib(const std::string& lib, ConstElementPtr params) {
        libraries_.push_back(make_pair(lib, params));
    }

    /// @brief Load all specified libraries.
    ///
    /// The libraries are stored in libraries_
    bool loadLibs() {
        return (HooksManager::loadLibraries(libraries_));
    }

    /// @brief Unloads all libraries.
    void unloadLibs() {
        EXPECT_NO_THROW(HooksManager::unloadLibraries());
    }

    /// @brief Return basic, valid GSS-TSIG configuration in JSON format.
    ElementPtr createValidJsonConfiguration() const {
        std::string config_text = "{ }";
        return (Element::fromJSON(config_text));
    }

    /// @brief Libraries.
    HookLibsCollection libraries_;
};

// Simple test that checks the library can be loaded in a D2 server.
TEST_F(LibLoadTest, validLoad) {<--- syntax error
    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    // Add library with valid configuration.
    addLib(LIBDHCP_GSS_TSIG_SO, createValidJsonConfiguration());

    // Library should load without issue.
    EXPECT_TRUE(loadLibs());
}

// Simple test that checks the library can be loaded and unloaded several times
// in a D2 server.
TEST_F(LibLoadTest, validLoads) {
    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    // Add library with valid configuration.
    addLib(LIBDHCP_GSS_TSIG_SO, createValidJsonConfiguration());

    EXPECT_TRUE(loadLibs());
    unloadLibs();

    EXPECT_TRUE(loadLibs());
    unloadLibs();
}

// Verifies that an unknown parameter in an otherwise valid configuration
// is detected.
TEST_F(LibLoadTest, unknownParameterLoad) {
    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    /// Add unknown element "foo" to valid config.
    ElementPtr config = createValidJsonConfiguration();
    config->set("foo", Element::create("bar"));

    // Add library with invalid configuration
    addLib(LIBDHCP_GSS_TSIG_SO, config);

    // The load should fail.
    EXPECT_FALSE(loadLibs());
}

// Verifies that a bad type parameter in an otherwise valid configuration
// is detected.
TEST_F(LibLoadTest, badTypeParameterLoad) {
    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    /// Add servers with bad type to valid config.
    ElementPtr config = createValidJsonConfiguration();
    config->set("servers", Element::createMap());

    // Add library with invalid configuration
    addLib(LIBDHCP_GSS_TSIG_SO, config);

    // The load should fail.
    EXPECT_FALSE(loadLibs());
}

// Verifies that a bad parameter in an otherwise valid configuration
// is detected.
TEST_F(LibLoadTest, badParameterLoad) {
    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    /// Add tkey-protocol with bad value to valid config.
    ElementPtr config = createValidJsonConfiguration();
    config->set("tkey-protocol", Element::create("FOO"));

    // Add library with invalid configuration
    addLib(LIBDHCP_GSS_TSIG_SO, config);

    // The load should fail.
    EXPECT_FALSE(loadLibs());
}

// Verifies that invalid d2_srv_configured callout d2_config parameter fail.
TEST_F(LibLoadTest, badD2ConfigCallout) {
    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    // Add library with valid configuration.
    addLib(LIBDHCP_GSS_TSIG_SO, createValidJsonConfiguration());

    // Load the library.
    ASSERT_TRUE(loadLibs());

    // Get and setup the callout handle.
    EXPECT_TRUE(HooksManager::calloutsPresent(testHooks.hook_index_d2_srv_configured_));
    CalloutHandlePtr handle = HooksManager::createCalloutHandle();
    handle->setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
    D2CfgContextPtr d2_config;
    handle->setArgument("server_config", d2_config);

    // Execute the callout (the exception is not propagated).
    EXPECT_NO_THROW(HooksManager::callCallouts(testHooks.hook_index_d2_srv_configured_,
                                               *handle));

    // Expect an exception logged with error message:
    // gss_tsig d2_srv_configured: server_config is null
}

// Verifies that valid and empty config works with d2_srv_configured callout.
TEST_F(LibLoadTest, emptyCallout) {
    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    // Add library with valid configuration.
    addLib(LIBDHCP_GSS_TSIG_SO, createValidJsonConfiguration());

    // Load the library.
    ASSERT_TRUE(loadLibs());

    // Get and setup the callout handle.
    EXPECT_TRUE(HooksManager::calloutsPresent(testHooks.hook_index_d2_srv_configured_));
    CalloutHandlePtr handle = HooksManager::createCalloutHandle();
    handle->setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
    D2CfgContextPtr d2_config(new D2CfgContext());
    handle->setArgument("server_config", d2_config);

    // Execute the callout.
    EXPECT_NO_THROW(HooksManager::callCallouts(testHooks.hook_index_d2_srv_configured_,
                                               *handle));

    // The configuration was accepted.
    EXPECT_EQ(CalloutHandle::NEXT_STEP_CONTINUE, handle->getStatus());
}

// Verifies that mismatch between hook config and empty d2 config
// is detected.
TEST_F(LibLoadTest, badEmptyCallout) {
    // Create a hook config.
    string config = "{\n"
        "\"server-principal\": \"DNS/server.example.org@REALM\",\n"
        "\"servers\": [ {\n"
        "  \"id\": \"foo\",\n"
        "  \"ip-address\": \"192.0.2.1\"\n"
        " } ] }\n";
    ElementPtr json;
    ASSERT_NO_THROW(json = Element::fromJSON(config));

    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    // Add library with configuration.
    addLib(LIBDHCP_GSS_TSIG_SO, json);

    // Load the library.
    ASSERT_TRUE(loadLibs());

    // Get and setup the callout handle.
    EXPECT_TRUE(HooksManager::calloutsPresent(testHooks.hook_index_d2_srv_configured_));
    CalloutHandlePtr handle = HooksManager::createCalloutHandle();
    handle->setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
    D2CfgContextPtr d2_config(new D2CfgContext());
    handle->setArgument("server_config", d2_config);

    // Execute the callout.
    EXPECT_NO_THROW(HooksManager::callCallouts(testHooks.hook_index_d2_srv_configured_,
                                               *handle));

    // The configuration was rejected.
    EXPECT_EQ(CalloutHandle::NEXT_STEP_DROP, handle->getStatus());
    string expected = "gss_tsig config mismatch: server info can't be found";
    string error;
    handle->getArgument("error", error);
    EXPECT_EQ(expected, error);
}

// Verifies that mismatch between hook config and d2 config
// is detected.
TEST_F(LibLoadTest, badCallout) {
    // D2 config with not matching server.
    string d2_config = "{\n"
        "\"forward-ddns\": {\n"
        " \"ddns-domains\": [ {\n"
        "  \"name\": \"foo.example.com.\",\n"
        "  \"dns-servers\": [ {\n"
        "   \"ip-address\": \"192.1.2.3\"\n"
        "   } ] } ] } }\n";
    ConstElementPtr d2_json;
    ASSERT_NO_THROW(d2_json = Element::fromJSON(d2_config));
    D2CfgMgr mgr;
    ConstElementPtr answer = mgr.simpleParseConfig(d2_json);
    int rcode = -1;
    ConstElementPtr comment = parseAnswer(rcode, answer);
    if (rcode != CONTROL_RESULT_SUCCESS) {
        FAIL() << "d2 config parse failed: " << comment->str();
    }
    D2CfgContextPtr d2_ctx = mgr.getD2CfgContext();
    ASSERT_TRUE(d2_ctx);

    // Create a hook config.
    string config = "{\n"
        "\"server-principal\": \"DNS/server.example.org@REALM\",\n"
        "\"servers\": [ {\n"
        "  \"id\": \"foo\",\n"
        "  \"ip-address\": \"192.0.2.1\"\n"
        " } ] }\n";
    ElementPtr json;
    ASSERT_NO_THROW(json = Element::fromJSON(config));

    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    // Add library with configuration.
    addLib(LIBDHCP_GSS_TSIG_SO, json);

    // Load the library.
    ASSERT_TRUE(loadLibs());

    // Get and setup the callout handle.
    EXPECT_TRUE(HooksManager::calloutsPresent(testHooks.hook_index_d2_srv_configured_));
    CalloutHandlePtr handle = HooksManager::createCalloutHandle();
    handle->setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
    handle->setArgument("server_config", d2_ctx);

    // Execute the callout.
    EXPECT_NO_THROW(HooksManager::callCallouts(testHooks.hook_index_d2_srv_configured_,
                                               *handle));

    // The configuration was rejected.
    EXPECT_EQ(CalloutHandle::NEXT_STEP_DROP, handle->getStatus());
    string expected = "gss_tsig config mismatch: server info can't be found";
    string error;
    handle->getArgument("error", error);
    EXPECT_EQ(expected, error);
}

// Verifies that hook config and d2 config work.
TEST_F(LibLoadTest, callout) {
    // D2 config with matching server.
    string d2_config = "{\n"
        "\"forward-ddns\": {\n"
        " \"ddns-domains\": [ {\n"
        "  \"name\": \"foo.example.com.\",\n"
        "  \"dns-servers\": [ {\n"
        "   \"ip-address\": \"192.0.2.1\"\n"
        "   } ] } ] } }\n";
    ConstElementPtr d2_json;
    ASSERT_NO_THROW(d2_json = Element::fromJSON(d2_config));
    D2CfgMgr mgr;
    ConstElementPtr answer = mgr.simpleParseConfig(d2_json);
    int rcode = -1;
    ConstElementPtr comment = parseAnswer(rcode, answer);
    if (rcode != CONTROL_RESULT_SUCCESS) {
        FAIL() << "d2 config parse failed: " << comment->str();
    }
    D2CfgContextPtr d2_ctx = mgr.getD2CfgContext();
    ASSERT_TRUE(d2_ctx);

    // Create a hook config.
    string config = "{\n"
        "\"server-principal\": \"DNS/server.example.org@REALM\",\n"
        "\"servers\": [ {\n"
        "  \"id\": \"foo\",\n"
        "  \"ip-address\": \"192.0.2.1\"\n"
        " } ] }\n";
    ElementPtr json;
    ASSERT_NO_THROW(json = Element::fromJSON(config));

    // Set proc name.
    Daemon::setProcName("kea-dhcp-ddns");

    // Add library with configuration.
    addLib(LIBDHCP_GSS_TSIG_SO, json);

    // Load the library.
    ASSERT_TRUE(loadLibs());

    // Get and setup the callout handle.
    EXPECT_TRUE(HooksManager::calloutsPresent(testHooks.hook_index_d2_srv_configured_));
    CalloutHandlePtr handle = HooksManager::createCalloutHandle();
    handle->setStatus(CalloutHandle::NEXT_STEP_CONTINUE);
    handle->setArgument("server_config", d2_ctx);

    // Execute the callout.
    EXPECT_NO_THROW(HooksManager::callCallouts(testHooks.hook_index_d2_srv_configured_,
                                               *handle));

    // The configuration was accepted.
    EXPECT_EQ(CalloutHandle::NEXT_STEP_CONTINUE, handle->getStatus());
}

} // end of anonymous namespace