d2_cfg_mgr_unittests.cc 58.3 KB
Newer Older
1
// Copyright (C) 2013-2014 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <config/module_spec.h>
#include <d2/d2_config.h>
#include <d2/d2_cfg_mgr.h>
#include <d_test_stubs.h>
19
#include <test_data_files_config.h>
20
#include <util/encode/base64.h>
21 22 23 24 25 26 27 28 29 30

#include <boost/foreach.hpp>
#include <gtest/gtest.h>

using namespace std;
using namespace isc;
using namespace isc::d2;

namespace {

31 32 33 34
std::string specfile(const std::string& name) {
    return (std::string(D2_SRC_DIR) + "/" + name);
}

35 36 37 38 39 40 41 42
/// @brief Test fixture class for testing D2CfgMgr class.
/// It maintains an member instance of D2CfgMgr and provides methods for
/// converting JSON strings to configuration element sets, checking parse
/// results, and accessing the configuration context.
class D2CfgMgrTest : public ConfigParseTest {
public:

    /// @brief Constructor
43
    D2CfgMgrTest():cfg_mgr_(new D2CfgMgr()), d2_params_() {
44 45 46 47 48 49 50 51
    }

    /// @brief Destructor
    ~D2CfgMgrTest() {
    }

    /// @brief Configuration manager instance.
    D2CfgMgrPtr cfg_mgr_;
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

    /// @brief Build JSON configuration string for a D2Params element
    ///
    /// Constructs a JSON string for "params" element using replacable
    /// parameters.
    ///
    /// @param ip_address string to insert as ip_address value
    /// @param port integer to insert as port value
    /// @param dns_server_timeout integer to insert as dns_server_timeout value
    /// @param ncr_protocol string to insert as ncr_protocol value
    /// @param ncr_format string to insert as ncr_format value
    ///
    /// @return std::string containing the JSON configuration text
    std::string makeParamsConfigString(const std::string& ip_address,
                                       const int port,
                                       const int dns_server_timeout,
                                       const std::string& ncr_protocol,
                                       const std::string& ncr_format) {
        std::ostringstream config;
        config <<
            "{"
            " \"ip_address\": \"" << ip_address << "\" , "
            " \"port\": " << port << " , "
            " \"dns_server_timeout\": " << dns_server_timeout << " , "
            " \"ncr_protocol\": \"" << ncr_protocol << "\" , "
            " \"ncr_format\": \"" << ncr_format << "\", "
            "\"tsig_keys\": [], "
            "\"forward_ddns\" : {}, "
            "\"reverse_ddns\" : {} "
            "}";

        return (config.str());
    }

86 87 88 89 90 91
    /// @brief Enumeration to select between expected configuration outcomes
    enum RunConfigMode {
       SHOULD_PASS,
       SHOULD_FAIL
    };

92 93 94 95 96 97 98 99 100 101
    /// @brief Parses a configuration string and tests against a given outcome
    ///
    /// Convenience method which accepts JSON text and an expected pass or fail
    /// outcome. It converts the text into an ElementPtr and passes that to
    /// configuration manager's parseConfig method.  It then tests the
    /// parse result against the expected outcome  If they do not match it
    /// the method asserts a failure.   If they do match, it refreshes the
    /// the D2Params pointer with the newly parsed instance.
    ///
    /// @param config_str the JSON configuration text to parse
102 103 104 105
    /// @param mode indicator if the parsing should fail or not.  It defaults
    /// defaults to SHOULD_PASS.
    ///
    void runConfig(std::string config_str, RunConfigMode mode=SHOULD_PASS) {
106 107 108 109 110
        // We assume the config string is valid JSON.
        ASSERT_TRUE(fromJSON(config_str));

        // Parse the configuration and verify we got the expected outcome.
        answer_ = cfg_mgr_->parseConfig(config_set_);
111
        ASSERT_TRUE(checkAnswer(mode == SHOULD_FAIL));
112 113 114 115 116 117 118 119 120 121

        // Verify that the D2 context can be retrieved and is not null.
        D2CfgContextPtr context;
        ASSERT_NO_THROW(context = cfg_mgr_->getD2CfgContext());

        // Verify that the global scalars have the proper values.
        d2_params_ = context->getD2Params();
        ASSERT_TRUE(d2_params_);
    }

122
    /// @brief Pointer the D2Params most recently parsed.
123
    D2ParamsPtr d2_params_;
124 125 126 127 128
};

/// @brief Tests that the spec file is valid.
/// Verifies that the BIND10 DHCP-DDNS configuration specification file
//  is valid.
129
TEST(D2SpecTest, basicSpec) {
130 131
    ASSERT_NO_THROW(isc::config::
                    moduleSpecFromFile(specfile("dhcp-ddns.spec")));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

/// @brief Convenience function which compares the contents of the given
/// DnsServerInfo against the given set of values.
///
/// It is structured in such a way that each value is checked, and output
/// is generate for all that do not match.
///
/// @param server is a pointer to the server to check against.
/// @param hostname is the value to compare against server's hostname_.
/// @param ip_address is the string value to compare against server's
/// ip_address_.
/// @param port is the value to compare against server's port.
///
/// @return returns true if there is a match across the board, otherwise it
/// returns false.
bool checkServer(DnsServerInfoPtr server, const char* hostname,
                 const char *ip_address, uint32_t port)
{
    // Return value, assume its a match.
    bool result = true;

154
    if (!server) {
155 156 157 158 159 160
        EXPECT_TRUE(server);
        return false;
    }

    // Check hostname.
    if (server->getHostname() != hostname) {
161
        EXPECT_EQ(hostname, server->getHostname());
162 163 164 165 166
        result = false;
    }

    // Check IP address.
    if (server->getIpAddress().toText() != ip_address) {
167
        EXPECT_EQ(ip_address, server->getIpAddress().toText());
168 169 170 171 172
        result = false;
    }

    // Check port.
    if (server->getPort() !=  port) {
173
        EXPECT_EQ (port, server->getPort());
174 175 176 177 178 179
        result = false;
    }

    return (result);
}

180
/// @brief Convenience function which compares the contents of the given
181 182
/// TSIGKeyInfo against the given set of values, and that the TSIGKey
/// member points to a key.
183
///
184
/// @param key is a pointer to the TSIGKeyInfo instance to verify
185 186 187 188 189 190
/// @param name is the value to compare against key's name_.
/// @param algorithm is the string value to compare against key's algorithm.
/// @param secret is the value to compare against key's secret.
///
/// @return returns true if there is a match across the board, otherwise it
/// returns false.
191
bool checkKey(TSIGKeyInfoPtr key, const std::string& name,
192
                 const std::string& algorithm, const std::string& secret) {
193
    // Return value, assume its a match.
194 195 196 197 198
    return (((key) &&
        (key->getName() == name) &&
        (key->getAlgorithm() == algorithm)  &&
        (key->getSecret() ==  secret)  &&
        (key->getTSIGKey())));
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
}

/// @brief Test fixture class for testing DnsServerInfo parsing.
class TSIGKeyInfoTest : public ConfigParseTest {
public:

    /// @brief Constructor
    TSIGKeyInfoTest() {
        reset();
    }

    /// @brief Destructor
    ~TSIGKeyInfoTest() {
    }

    /// @brief Wipe out the current storage and parser and replace
    /// them with new ones.
    void reset() {
        keys_.reset(new TSIGKeyInfoMap());
        parser_.reset(new TSIGKeyInfoParser("test", keys_));
    }

    /// @brief Storage for "committing" keys.
    TSIGKeyInfoMapPtr keys_;

    /// @brief Pointer to the current parser instance.
    isc::dhcp::ParserPtr parser_;
};

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
/// @brief Test fixture class for testing DnsServerInfo parsing.
class DnsServerInfoTest : public ConfigParseTest {
public:

    /// @brief Constructor
    DnsServerInfoTest() {
        reset();
    }

    /// @brief Destructor
    ~DnsServerInfoTest() {
    }

    /// @brief Wipe out the current storage and parser and replace
    /// them with new ones.
    void reset() {
        servers_.reset(new DnsServerInfoStorage());
        parser_.reset(new DnsServerInfoParser("test", servers_));
    }

    /// @brief Storage for "committing" servers.
    DnsServerInfoStoragePtr servers_;

    /// @brief Pointer to the current parser instance.
    isc::dhcp::ParserPtr parser_;
};

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

/// @brief Test fixture class for testing DDnsDomain parsing.
class DdnsDomainTest : public ConfigParseTest {
public:

    /// @brief Constructor
    DdnsDomainTest() {
        reset();
    }

    /// @brief Destructor
    ~DdnsDomainTest() {
    }

    /// @brief Wipe out the current storage and parser and replace
    /// them with new ones.
    void reset() {
        keys_.reset(new TSIGKeyInfoMap());
        domains_.reset(new DdnsDomainMap());
        parser_.reset(new DdnsDomainParser("test", domains_, keys_));
    }

    /// @brief Add TSIGKeyInfos to the key map
    ///
    /// @param name the name of the key
    /// @param algorithm the algorithm of the key
    /// @param secret the secret value of the key
    void addKey(const std::string& name, const std::string& algorithm,
                const std::string& secret) {
        TSIGKeyInfoPtr key_info(new TSIGKeyInfo(name, algorithm, secret));
285
        (*keys_)[name]=key_info;
286 287 288 289 290 291 292 293 294 295 296 297
    }

    /// @brief Storage for "committing" domains.
    DdnsDomainMapPtr domains_;

    /// @brief Storage for TSIGKeys
    TSIGKeyInfoMapPtr keys_;

    /// @brief Pointer to the current parser instance.
    isc::dhcp::ParserPtr parser_;
};

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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/// @brief Tests a basic valid configuration for D2Param.
TEST_F(D2CfgMgrTest, validParamsEntry) {
    // Verify that ip_address can be valid v4 address.
    std::string config = makeParamsConfigString ("192.0.0.1", 777, 333,
                                           "UDP", "JSON");
    runConfig(config);

    EXPECT_EQ(isc::asiolink::IOAddress("192.0.0.1"),
              d2_params_->getIpAddress());
    EXPECT_EQ(777, d2_params_->getPort());
    EXPECT_EQ(333, d2_params_->getDnsServerTimeout());
    EXPECT_EQ(dhcp_ddns::NCR_UDP, d2_params_->getNcrProtocol());
    EXPECT_EQ(dhcp_ddns::FMT_JSON, d2_params_->getNcrFormat());

    // Verify that ip_address can be valid v6 address.
    config = makeParamsConfigString ("3001::5", 777, 333, "UDP", "JSON");
    runConfig(config);

    // Verify that the global scalars have the proper values.
    EXPECT_EQ(isc::asiolink::IOAddress("3001::5"),
              d2_params_->getIpAddress());
}

/// @brief Tests default values for D2Params.
/// It verifies that D2Params is populated with default value for optional
/// parameter if not supplied in the configuration.
/// Currently they are all optional.
TEST_F(D2CfgMgrTest, defaultValues) {

    // Check that omitting ip_address gets you its default
    std::string config =
            "{"
            " \"port\": 777 , "
            " \"dns_server_timeout\": 333 , "
            " \"ncr_protocol\": \"UDP\" , "
            " \"ncr_format\": \"JSON\", "
            "\"tsig_keys\": [], "
            "\"forward_ddns\" : {}, "
            "\"reverse_ddns\" : {} "
            "}";

    runConfig(config);
    EXPECT_EQ(isc::asiolink::IOAddress(D2Params::DFT_IP_ADDRESS),
              d2_params_->getIpAddress());

    // Check that omitting port gets you its default
    config =
            "{"
            " \"ip_address\": \"192.0.0.1\" , "
            " \"dns_server_timeout\": 333 , "
            " \"ncr_protocol\": \"UDP\" , "
            " \"ncr_format\": \"JSON\", "
            "\"tsig_keys\": [], "
            "\"forward_ddns\" : {}, "
            "\"reverse_ddns\" : {} "
            "}";

    runConfig(config);
    EXPECT_EQ(D2Params::DFT_PORT, d2_params_->getPort());

    // Check that omitting timeout gets you its default
    config =
            "{"
            " \"ip_address\": \"192.0.0.1\" , "
            " \"port\": 777 , "
            " \"ncr_protocol\": \"UDP\" , "
            " \"ncr_format\": \"JSON\", "
            "\"tsig_keys\": [], "
            "\"forward_ddns\" : {}, "
            "\"reverse_ddns\" : {} "
            "}";

    runConfig(config);
    EXPECT_EQ(D2Params::DFT_DNS_SERVER_TIMEOUT,
              d2_params_->getDnsServerTimeout());

    // Check that protocol timeout gets you its default
    config =
            "{"
            " \"ip_address\": \"192.0.0.1\" , "
            " \"port\": 777 , "
            " \"dns_server_timeout\": 333 , "
            " \"ncr_format\": \"JSON\", "
            "\"tsig_keys\": [], "
            "\"forward_ddns\" : {}, "
            "\"reverse_ddns\" : {} "
            "}";

    runConfig(config);
    EXPECT_EQ(dhcp_ddns::stringToNcrProtocol(D2Params::DFT_NCR_PROTOCOL),
              d2_params_->getNcrProtocol());

    // Check that format timeout gets you its default
    config =
            "{"
            " \"ip_address\": \"192.0.0.1\" , "
            " \"port\": 777 , "
            " \"dns_server_timeout\": 333 , "
            " \"ncr_protocol\": \"UDP\", "
            "\"tsig_keys\": [], "
            "\"forward_ddns\" : {}, "
            "\"reverse_ddns\" : {} "
            "}";

    runConfig(config);
    EXPECT_EQ(dhcp_ddns::stringToNcrFormat(D2Params::DFT_NCR_FORMAT),
              d2_params_->getNcrFormat());
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
/// @brief Tests the unsupported scalar parameters and objects are detected.
TEST_F(D2CfgMgrTest, unsupportedTopLevelItems) {
    // Check that an unsupported top level parameter fails.
    std::string config =
            "{"
            " \"ip_address\": \"127.0.0.1\", "
            " \"port\": 777 , "
            " \"dns_server_timeout\": 333 , "
            " \"ncr_protocol\": \"UDP\" , "
            " \"ncr_format\": \"JSON\", "
            "\"tsig_keys\": [], "
            "\"forward_ddns\" : {}, "
            "\"reverse_ddns\" : {}, "
            "\"bogus_param\" : true "
            "}";

    runConfig(config, SHOULD_FAIL);

    // Check that unsupported top level objects fails.  For
    // D2 these fail as they are not in the parse order.
    config =
            "{"
            " \"ip_address\": \"127.0.0.1\", "
            " \"port\": 777 , "
            " \"dns_server_timeout\": 333 , "
            " \"ncr_protocol\": \"UDP\" , "
            " \"ncr_format\": \"JSON\", "
            "\"tsig_keys\": [], "
            "\"bogus_object_one\" : {}, "
            "\"forward_ddns\" : {}, "
            "\"reverse_ddns\" : {}, "
            "\"bogus_object_two\" : {} "
            "}";

    runConfig(config, SHOULD_FAIL);
}


445 446 447 448 449 450 451 452 453 454 455 456
/// @brief Tests the enforcement of data validation when parsing D2Params.
/// It verifies that:
/// -# ip_address cannot be "0.0.0.0"
/// -# ip_address cannot be "::"
/// -# port cannot be 0
/// -# dns_server_timeout cannat be 0
/// -# ncr_protocol must be valid
/// -# ncr_format must be valid
TEST_F(D2CfgMgrTest, invalidEntry) {
    // Cannot use IPv4 ANY address
    std::string config = makeParamsConfigString ("0.0.0.0", 777, 333,
                                           "UDP", "JSON");
457
    runConfig(config, SHOULD_FAIL);
458 459 460

    // Cannot use IPv6 ANY address
    config = makeParamsConfigString ("::", 777, 333, "UDP", "JSON");
461
    runConfig(config, SHOULD_FAIL);
462 463 464

    // Cannot use port  0
    config = makeParamsConfigString ("127.0.0.1", 0, 333, "UDP", "JSON");
465
    runConfig(config, SHOULD_FAIL);
466 467 468

    // Cannot use dns server timeout of 0
    config = makeParamsConfigString ("127.0.0.1", 777, 0, "UDP", "JSON");
469
    runConfig(config, SHOULD_FAIL);
470 471 472

    // Invalid protocol
    config = makeParamsConfigString ("127.0.0.1", 777, 333, "BOGUS", "JSON");
473
    runConfig(config, SHOULD_FAIL);
474

475 476 477 478
    // Unsupported protocol
    config = makeParamsConfigString ("127.0.0.1", 777, 333, "TCP", "JSON");
    runConfig(config, SHOULD_FAIL);

479 480
    // Invalid format
    config = makeParamsConfigString ("127.0.0.1", 777, 333, "UDP", "BOGUS");
481
    runConfig(config, SHOULD_FAIL);
482 483
}

484 485 486 487
/// @brief Tests the enforcement of data validation when parsing TSIGKeyInfos.
/// It verifies that:
/// 1. Name cannot be blank.
/// 2. Algorithm cannot be blank.
488
/// 3. Secret cannot be blank.
489
TEST_F(TSIGKeyInfoTest, invalidEntry) {
490 491 492
    // Config with a blank name entry.
    std::string config = "{"
                         " \"name\": \"\" , "
493
                         " \"algorithm\": \"HMAC-MD5\" , "
494
                         "   \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
495
                         "}";
496
    ASSERT_TRUE(fromJSON(config));
497

498 499
    // Verify that build fails on blank name.
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
500 501 502 503 504

    // Config with a blank algorithm entry.
    config = "{"
                         " \"name\": \"d2_key_one\" , "
                         " \"algorithm\": \"\" , "
505
                         "   \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
506 507
                         "}";

508
    ASSERT_TRUE(fromJSON(config));
509

510 511
    // Verify that build fails on blank algorithm.
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
512

513 514 515 516
    // Config with an invalid algorithm entry.
    config = "{"
                         " \"name\": \"d2_key_one\" , "
                         " \"algorithm\": \"bogus\" , "
517
                         "   \"secret\": \"LSWXnfkKZjdPJI5QxlpnfQ==\" "
518 519
                         "}";

520
    ASSERT_TRUE(fromJSON(config));
521

522 523
    // Verify that build fails on blank algorithm.
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
524 525 526 527

    // Config with a blank secret entry.
    config = "{"
                         " \"name\": \"d2_key_one\" , "
528
                         " \"algorithm\": \"HMAC-MD5\" , "
529 530 531
                         " \"secret\": \"\" "
                         "}";

532
    ASSERT_TRUE(fromJSON(config));
533

534 535
    // Verify that build fails blank secret
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
536 537 538 539

    // Config with an invalid secret entry.
    config = "{"
                         " \"name\": \"d2_key_one\" , "
540
                         " \"algorithm\": \"HMAC-MD5\" , "
541 542 543 544 545 546 547
                         " \"secret\": \"bogus\" "
                         "}";

    ASSERT_TRUE(fromJSON(config));

    // Verify that build fails an invalid secret
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
548 549 550 551
}

/// @brief Verifies that TSIGKeyInfo parsing creates a proper TSIGKeyInfo
/// when given a valid combination of entries.
552
TEST_F(TSIGKeyInfoTest, validEntry) {
553 554 555
    // Valid entries for TSIG key, all items are required.
    std::string config = "{"
                         " \"name\": \"d2_key_one\" , "
556
                         " \"algorithm\": \"HMAC-MD5\" , "
557
                         " \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
558
                         "}";
559
    ASSERT_TRUE(fromJSON(config));
560 561

    // Verify that it builds and commits without throwing.
562 563
    //ASSERT_NO_THROW(parser_->build(config_set_));
    (parser_->build(config_set_));
564 565 566 567
    ASSERT_NO_THROW(parser_->commit());

    // Verify the correct number of keys are present
    int count =  keys_->size();
568
    EXPECT_EQ(1, count);
569 570 571 572 573 574 575

    // Find the key and retrieve it.
    TSIGKeyInfoMap::iterator gotit = keys_->find("d2_key_one");
    ASSERT_TRUE(gotit != keys_->end());
    TSIGKeyInfoPtr& key = gotit->second;

    // Verify the key contents.
576
    EXPECT_TRUE(checkKey(key, "d2_key_one", "HMAC-MD5",
577
                         "dGhpcyBrZXkgd2lsbCBtYXRjaA=="));
578 579 580 581 582 583 584 585 586
}

/// @brief Verifies that attempting to parse an invalid list of TSIGKeyInfo
/// entries is detected.
TEST_F(TSIGKeyInfoTest, invalidTSIGKeyList) {
    // Construct a list of keys with an invalid key entry.
    std::string config = "["

                         " { \"name\": \"key1\" , "
587
                         "   \"algorithm\": \"HMAC-MD5\" ,"
588
                         "   \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
589
                         " },"
590
                         // this entry has an invalid algorithm
591 592
                         " { \"name\": \"key2\" , "
                         "   \"algorithm\": \"\" ,"
593
                         "   \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
594 595
                         " },"
                         " { \"name\": \"key3\" , "
596
                         "   \"algorithm\": \"HMAC-MD5\" ,"
597
                         "   \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
598 599 600
                         " }"
                         " ]";

601
    ASSERT_TRUE(fromJSON(config));
602 603 604 605 606 607

    // Create the list parser.
    isc::dhcp::ParserPtr parser;
    ASSERT_NO_THROW(parser.reset(new TSIGKeyInfoListParser("test", keys_)));

    // Verify that the list builds without errors.
608
    EXPECT_THROW(parser->build(config_set_), D2CfgError);
609 610 611 612 613 614 615 616 617
}

/// @brief Verifies that attempting to parse an invalid list of TSIGKeyInfo
/// entries is detected.
TEST_F(TSIGKeyInfoTest, duplicateTSIGKey) {
    // Construct a list of keys with an invalid key entry.
    std::string config = "["

                         " { \"name\": \"key1\" , "
618
                         "   \"algorithm\": \"HMAC-MD5\" ,"
619
                         "   \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
620 621
                         " },"
                         " { \"name\": \"key2\" , "
622
                         "   \"algorithm\": \"HMAC-MD5\" ,"
623
                         "   \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
624 625
                         " },"
                         " { \"name\": \"key1\" , "
626
                         "   \"algorithm\": \"HMAC-MD5\" ,"
627
                         "   \"secret\": \"GWG/Xfbju4O2iXGqkSu4PQ==\" "
628 629 630
                         " }"
                         " ]";

631
    ASSERT_TRUE(fromJSON(config));
632 633 634 635 636 637

    // Create the list parser.
    isc::dhcp::ParserPtr parser;
    ASSERT_NO_THROW(parser.reset(new TSIGKeyInfoListParser("test", keys_)));

    // Verify that the list builds without errors.
638
    EXPECT_THROW(parser->build(config_set_), D2CfgError);
639 640 641
}

/// @brief Verifies a valid list of TSIG Keys parses correctly.
642
/// Also verifies that all of the supported algorithm names work.
643
TEST_F(TSIGKeyInfoTest, validTSIGKeyList) {
644
    // Construct a valid list of keys.
645 646 647
    std::string config = "["

                         " { \"name\": \"key1\" , "
648
                         "   \"algorithm\": \"HMAC-MD5\" ,"
649
                         "  \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
650 651
                         " },"
                         " { \"name\": \"key2\" , "
652
                         "   \"algorithm\": \"HMAC-SHA1\" ,"
653
                         "  \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
654 655
                         " },"
                         " { \"name\": \"key3\" , "
656
                         "   \"algorithm\": \"HMAC-SHA256\" ,"
657
                         "  \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
658 659
                         " },"
                         " { \"name\": \"key4\" , "
660
                         "   \"algorithm\": \"HMAC-SHA224\" ,"
661
                         "  \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
662 663
                         " },"
                         " { \"name\": \"key5\" , "
664
                         "   \"algorithm\": \"HMAC-SHA384\" ,"
665
                         "  \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
666 667
                         " },"
                         " { \"name\": \"key6\" , "
668
                         "   \"algorithm\": \"HMAC-SHA512\" ,"
669
                         "   \"secret\": \"dGhpcyBrZXkgd2lsbCBtYXRjaA==\" "
670 671 672
                         " }"
                         " ]";

673
    ASSERT_TRUE(fromJSON(config));
674 675 676 677 678 679 680 681

    // Verify that the list builds and commits without errors.
    // Create the list parser.
    isc::dhcp::ParserPtr parser;
    ASSERT_NO_THROW(parser.reset(new TSIGKeyInfoListParser("test", keys_)));
    ASSERT_NO_THROW(parser->build(config_set_));
    ASSERT_NO_THROW(parser->commit());

682
    std::string ref_secret = "dGhpcyBrZXkgd2lsbCBtYXRjaA==";
683 684
    // Verify the correct number of keys are present
    int count =  keys_->size();
685
    ASSERT_EQ(6, count);
686 687 688 689 690 691 692

    // Find the 1st key and retrieve it.
    TSIGKeyInfoMap::iterator gotit = keys_->find("key1");
    ASSERT_TRUE(gotit != keys_->end());
    TSIGKeyInfoPtr& key = gotit->second;

    // Verify the key contents.
693
    EXPECT_TRUE(checkKey(key, "key1", TSIGKeyInfo::HMAC_MD5_STR, ref_secret));
694 695 696 697 698 699 700

    // Find the 2nd key and retrieve it.
    gotit = keys_->find("key2");
    ASSERT_TRUE(gotit != keys_->end());
    key = gotit->second;

    // Verify the key contents.
701
    EXPECT_TRUE(checkKey(key, "key2", TSIGKeyInfo::HMAC_SHA1_STR, ref_secret));
702 703 704 705 706 707 708

    // Find the 3rd key and retrieve it.
    gotit = keys_->find("key3");
    ASSERT_TRUE(gotit != keys_->end());
    key = gotit->second;

    // Verify the key contents.
709 710
    EXPECT_TRUE(checkKey(key, "key3", TSIGKeyInfo::HMAC_SHA256_STR,
                         ref_secret));
711 712 713 714 715 716 717

    // Find the 4th key and retrieve it.
    gotit = keys_->find("key4");
    ASSERT_TRUE(gotit != keys_->end());
    key = gotit->second;

    // Verify the key contents.
718 719
    EXPECT_TRUE(checkKey(key, "key4", TSIGKeyInfo::HMAC_SHA224_STR,
                         ref_secret));
720 721 722 723 724 725 726

    // Find the 5th key and retrieve it.
    gotit = keys_->find("key5");
    ASSERT_TRUE(gotit != keys_->end());
    key = gotit->second;

    // Verify the key contents.
727 728
    EXPECT_TRUE(checkKey(key, "key5", TSIGKeyInfo::HMAC_SHA384_STR,
                         ref_secret));
729 730 731 732 733 734 735

    // Find the 6th key and retrieve it.
    gotit = keys_->find("key6");
    ASSERT_TRUE(gotit != keys_->end());
    key = gotit->second;

    // Verify the key contents.
736 737
    EXPECT_TRUE(checkKey(key, "key6", TSIGKeyInfo::HMAC_SHA512_STR,
                         ref_secret));
738 739
}

740 741 742 743 744
/// @brief Tests the enforcement of data validation when parsing DnsServerInfos.
/// It verifies that:
/// 1. Specifying both a hostname and an ip address is not allowed.
/// 2. Specifying both blank a hostname and blank ip address is not allowed.
/// 3. Specifying a negative port number is not allowed.
745
TEST_F(DnsServerInfoTest, invalidEntry) {
746
    // Create a config in which both host and ip address are supplied.
747
    // Verify that build fails.
748 749
    std::string config = "{ \"hostname\": \"pegasus.tmark\", "
                         "  \"ip_address\": \"127.0.0.1\" } ";
750
    ASSERT_TRUE(fromJSON(config));
751
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
752 753

    // Neither host nor ip address supplied
754
    // Verify that builds fails.
755 756
    config = "{ \"hostname\": \"\", "
             "  \"ip_address\": \"\" } ";
757
    ASSERT_TRUE(fromJSON(config));
758
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
759 760 761 762 763

    // Create a config with a negative port number.
    // Verify that build fails.
    config = "{ \"ip_address\": \"192.168.5.6\" ,"
             "  \"port\": -100 }";
764
    ASSERT_TRUE(fromJSON(config));
765 766 767
    EXPECT_THROW (parser_->build(config_set_), isc::BadValue);
}

768

769 770 771 772 773 774
/// @brief Verifies that DnsServerInfo parsing creates a proper DnsServerInfo
/// when given a valid combination of entries.
/// It verifies that:
/// 1. A DnsServerInfo entry is correctly made, when given only a hostname.
/// 2. A DnsServerInfo entry is correctly made, when given ip address and port.
/// 3. A DnsServerInfo entry is correctly made, when given only an ip address.
775
TEST_F(DnsServerInfoTest, validEntry) {
776 777 778 779
    /// @todo When resolvable hostname is supported you'll need this test.
    /// // Valid entries for dynamic host
    /// std::string config = "{ \"hostname\": \"pegasus.tmark\" }";
    /// ASSERT_TRUE(fromJSON(config));
780

781 782 783
    /// // Verify that it builds and commits without throwing.
    /// ASSERT_NO_THROW(parser_->build(config_set_));
    /// ASSERT_NO_THROW(parser_->commit());
784

785 786 787
    /// //Verify the correct number of servers are present
    /// int count =  servers_->size();
    /// EXPECT_EQ(1, count);
788

789 790 791 792 793
    /// Verify the server exists and has the correct values.
    /// DnsServerInfoPtr server = (*servers_)[0];
    /// EXPECT_TRUE(checkServer(server, "pegasus.tmark",
    ///                         DnsServerInfo::EMPTY_IP_STR,
    ///                         DnsServerInfo::STANDARD_DNS_PORT));
794

795 796
    /// // Start over for a new test.
    /// reset();
797 798

    // Valid entries for static ip
799 800
    std::string config = " { \"ip_address\": \"127.0.0.1\" , "
                         "  \"port\": 100 }";
801
    ASSERT_TRUE(fromJSON(config));
802 803 804 805 806 807

    // Verify that it builds and commits without throwing.
    ASSERT_NO_THROW(parser_->build(config_set_));
    ASSERT_NO_THROW(parser_->commit());

    // Verify the correct number of servers are present
808
    int count =  servers_->size();
809
    EXPECT_EQ(1, count);
810 811

    // Verify the server exists and has the correct values.
812
    DnsServerInfoPtr server = (*servers_)[0];
813 814 815 816 817 818 819
    EXPECT_TRUE(checkServer(server, "", "127.0.0.1", 100));

    // Start over for a new test.
    reset();

    // Valid entries for static ip, no port
    config = " { \"ip_address\": \"192.168.2.5\" }";
820
    ASSERT_TRUE(fromJSON(config));
821 822 823 824 825 826 827

    // Verify that it builds and commits without throwing.
    ASSERT_NO_THROW(parser_->build(config_set_));
    ASSERT_NO_THROW(parser_->commit());

    // Verify the correct number of servers are present
    count =  servers_->size();
828
    EXPECT_EQ(1, count);
829 830 831 832

    // Verify the server exists and has the correct values.
    server = (*servers_)[0];
    EXPECT_TRUE(checkServer(server, "", "192.168.2.5",
833
                            DnsServerInfo::STANDARD_DNS_PORT));
834 835 836 837 838 839
}

/// @brief Verifies that attempting to parse an invalid list of DnsServerInfo
/// entries is detected.
TEST_F(ConfigParseTest, invalidServerList) {
    // Construct a list of servers with an invalid server entry.
840 841 842
    std::string config = "[ { \"ip_address\": \"127.0.0.1\" }, "
                        "{ \"ip_address\": \"\" }, "
                        "{ \"ip_address\": \"127.0.0.2\" } ]";
843
    ASSERT_TRUE(fromJSON(config));
844 845 846 847 848 849

    // Create the server storage and list parser.
    DnsServerInfoStoragePtr servers(new DnsServerInfoStorage());
    isc::dhcp::ParserPtr parser;
    ASSERT_NO_THROW(parser.reset(new DnsServerInfoListParser("test", servers)));

850 851
    // Verify that build fails.
    EXPECT_THROW(parser->build(config_set_), D2CfgError);
852 853 854 855 856 857
}

/// @brief Verifies that a list of DnsServerInfo entries parses correctly given
/// a valid configuration.
TEST_F(ConfigParseTest, validServerList) {
    // Create a valid list of servers.
858 859 860
    std::string config = "[ { \"ip_address\": \"127.0.0.1\" }, "
                        "{ \"ip_address\": \"127.0.0.2\" }, "
                        "{ \"ip_address\": \"127.0.0.3\" } ]";
861
    ASSERT_TRUE(fromJSON(config));
862 863 864 865 866 867 868 869 870 871 872 873

    // Create the server storage and list parser.
    DnsServerInfoStoragePtr servers(new DnsServerInfoStorage());
    isc::dhcp::ParserPtr parser;
    ASSERT_NO_THROW(parser.reset(new DnsServerInfoListParser("test", servers)));

    // Verfiy that the list builds and commits without error.
    ASSERT_NO_THROW(parser->build(config_set_));
    ASSERT_NO_THROW(parser->commit());

    // Verify that the server storage contains the correct number of servers.
    int count =  servers->size();
874
    EXPECT_EQ(3, count);
875 876 877

    // Verify the first server exists and has the correct values.
    DnsServerInfoPtr server = (*servers)[0];
878
    EXPECT_TRUE(checkServer(server, "", "127.0.0.1",
879
                            DnsServerInfo::STANDARD_DNS_PORT));
880 881 882

    // Verify the second server exists and has the correct values.
    server = (*servers)[1];
883
    EXPECT_TRUE(checkServer(server, "", "127.0.0.2",
884
                            DnsServerInfo::STANDARD_DNS_PORT));
885 886 887

    // Verify the third server exists and has the correct values.
    server = (*servers)[2];
888
    EXPECT_TRUE(checkServer(server, "", "127.0.0.3",
889
                            DnsServerInfo::STANDARD_DNS_PORT));
890 891 892 893 894 895 896 897
}

/// @brief Tests the enforcement of data validation when parsing DdnsDomains.
/// It verifies that:
/// 1. Domain storage cannot be null when constructing a DdnsDomainParser.
/// 2. The name entry is not optional.
/// 3. The server list man not be empty.
/// 4. That a mal-formed server entry is detected.
898 899
/// 5. That an undefined key name is detected.
TEST_F(DdnsDomainTest, invalidDdnsDomainEntry) {
900
    // Verify that attempting to construct the parser with null storage fails.
901 902 903
    DdnsDomainMapPtr domains;
    ASSERT_THROW(isc::dhcp::ParserPtr(
                 new DdnsDomainParser("test", domains, keys_)), D2CfgError);
904 905 906 907 908 909 910 911 912 913

    // Create a domain configuration without a name
    std::string config = "{  \"key_name\": \"d2_key.tmark.org\" , "
                         "  \"dns_servers\" : [ "
                         "  {  \"ip_address\": \"127.0.0.1\" , "
                         "    \"port\": 100 },"
                         "  { \"ip_address\": \"127.0.0.2\" , "
                         "    \"port\": 200 },"
                         "  {  \"ip_address\": \"127.0.0.3\" , "
                         "    \"port\": 300 } ] } ";
914
    ASSERT_TRUE(fromJSON(config));
915

916 917
    // Verify that the domain configuration builds fails.
    EXPECT_THROW(parser_->build(config_set_), isc::dhcp::DhcpConfigError);
918 919 920 921 922 923

    // Create a domain configuration with an empty server list.
    config = "{ \"name\": \"tmark.org\" , "
             "  \"key_name\": \"d2_key.tmark.org\" , "
             "  \"dns_servers\" : [ "
             "   ] } ";
924
    ASSERT_TRUE(fromJSON(config));
925 926

    // Verify that the domain configuration build fails.
927
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
928 929 930 931 932 933 934

    // Create a domain configuration with a mal-formed server entry.
    config = "{ \"name\": \"tmark.org\" , "
             "  \"key_name\": \"d2_key.tmark.org\" , "
             "  \"dns_servers\" : [ "
             "  {  \"ip_address\": \"127.0.0.3\" , "
             "    \"port\": -1 } ] } ";
935
    ASSERT_TRUE(fromJSON(config));
936 937

    // Verify that the domain configuration build fails.
938
    EXPECT_THROW(parser_->build(config_set_), isc::BadValue);
939

940 941 942 943 944 945
    // Create a domain configuration without an defined key name
    config = "{ \"name\": \"tmark.org\" , "
             "  \"key_name\": \"d2_key.tmark.org\" , "
             "  \"dns_servers\" : [ "
             "  {  \"ip_address\": \"127.0.0.3\" , "
             "    \"port\": 300 } ] } ";
946
    ASSERT_TRUE(fromJSON(config));
947

948 949
    // Verify that the domain configuration build fails.
    EXPECT_THROW(parser_->build(config_set_), D2CfgError);
950
}
951 952 953 954 955 956

/// @brief Verifies the basics of parsing DdnsDomains.
/// It verifies that:
/// 1. Valid construction of DdnsDomainParser functions.
/// 2. Given a valid, configuration entry, DdnsDomainParser parses
/// correctly.
957 958
/// (It indirectly verifies the operation of DdnsDomainMap).
TEST_F(DdnsDomainTest, ddnsDomainParsing) {
959 960 961 962 963 964 965 966 967 968 969 970
    // Create a valid domain configuration entry containing three valid
    // servers.
    std::string config =
                        "{ \"name\": \"tmark.org\" , "
                        "  \"key_name\": \"d2_key.tmark.org\" , "
                        "  \"dns_servers\" : [ "
                        "  {  \"ip_address\": \"127.0.0.1\" , "
                        "    \"port\": 100 },"
                        "  { \"ip_address\": \"127.0.0.2\" , "
                        "    \"port\": 200 },"
                        "  {  \"ip_address\": \"127.0.0.3\" , "
                        "    \"port\": 300 } ] } ";
971
    ASSERT_TRUE(fromJSON(config));
972

973
    // Add a TSIG key to the test key map, so key validation will pass.
974
    addKey("d2_key.tmark.org", "HMAC-MD5", "GWG/Xfbju4O2iXGqkSu4PQ==");
975 976

    // Verify that the domain configuration builds and commits without error.
977 978
    ASSERT_NO_THROW(parser_->build(config_set_));
    ASSERT_NO_THROW(parser_->commit());
979 980

    // Verify that the domain storage contains the correct number of domains.
981
    int count =  domains_->size();
982
    EXPECT_EQ(1, count);
983 984 985

    // Verify that the expected domain exists and can be retrieved from
    // the storage.
986 987
    DdnsDomainMap::iterator gotit = domains_->find("tmark.org");
    ASSERT_TRUE(gotit != domains_->end());
988 989 990
    DdnsDomainPtr& domain = gotit->second;

    // Verify the name and key_name values.
991 992
    EXPECT_EQ("tmark.org", domain->getName());
    EXPECT_EQ("d2_key.tmark.org", domain->getKeyName());
993 994
    ASSERT_TRUE(domain->getTSIGKeyInfo());
    ASSERT_TRUE(domain->getTSIGKeyInfo()->getTSIGKey());
995 996 997 998 999 1000

    // Verify that the server list exists and contains the correct number of
    // servers.
    const DnsServerInfoStoragePtr& servers = domain->getServers();
    EXPECT_TRUE(servers);
    count =  servers->size();
1001
    EXPECT_EQ(3, count);
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

    // Fetch each server and verify its contents.
    DnsServerInfoPtr server = (*servers)[0];
    EXPECT_TRUE(server);

    EXPECT_TRUE(checkServer(server, "", "127.0.0.1", 100));

    server = (*servers)[1];
    EXPECT_TRUE(server);

    EXPECT_TRUE(checkServer(server, "", "127.0.0.2", 200));

    server = (*servers)[2];
    EXPECT_TRUE(server);

    EXPECT_TRUE(checkServer(server, "", "127.0.0.3", 300));
}

/// @brief Tests the fundamentals of parsing DdnsDomain lists.
/// This test verifies that given a valid domain list configuration
/// it will accurately parse and populate each domain in the list.
1023
TEST_F(DdnsDomainTest, DdnsDomainListParsing) {
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
    // Create a valid domain list configuration, with two domains
    // that have three servers each.
    std::string config =
                        "[ "
                        "{ \"name\": \"tmark.org\" , "
                        "  \"key_name\": \"d2_key.tmark.org\" , "
                        "  \"dns_servers\" : [ "
                        "  { \"ip_address\": \"127.0.0.1\" , "
                        "    \"port\": 100 },"
                        "  { \"ip_address\": \"127.0.0.2\" , "
                        "    \"port\": 200 },"
                        "  { \"ip_address\": \"127.0.0.3\" , "
                        "    \"port\": 300 } ] } "
                        ", "
                        "{ \"name\": \"billcat.net\" , "
                        "  \"key_name\": \"d2_key.billcat.net\" , "
                        "  \"dns_servers\" : [ "
                        "  { \"ip_address\": \"127.0.0.4\" , "
                        "    \"port\": 400 },"
                        "  { \"ip_address\": \"127.0.0.5\" , "
                        "    \"port\": 500 },"
                        "  { \"ip_address\": \"127.0.0.6\" , "
                        "    \"port\": 600 } ] } "
                        "] ";

1049
    ASSERT_TRUE(fromJSON(config));
1050

1051
    // Add keys to key map so key validation passes.
1052 1053
    addKey("d2_key.tmark.org", "HMAC-MD5", "GWG/Xfbju4O2iXGqkSu4PQ==");
    addKey("d2_key.billcat.net", "HMAC-MD5", "GWG/Xfbju4O2iXGqkSu4PQ==");
1054 1055

    // Create the list parser
1056
    isc::dhcp::ParserPtr list_parser;
1057 1058
    ASSERT_NO_THROW(list_parser.reset(
                    new DdnsDomainListParser("test", domains_, keys_)));
1059 1060

    // Verify that the domain configuration builds and commits without error.
1061 1062
    ASSERT_NO_THROW(list_parser->build(config_set_));
    ASSERT_NO_THROW(list_parser->commit());
1063 1064

    // Verify that the domain storage contains the correct number of domains.
1065
    int count =  domains_->size();
1066
    EXPECT_EQ(2, count);
1067 1068

    // Verify that the first domain exists and can be retrieved.
1069 1070
    DdnsDomainMap::iterator gotit = domains_->find("tmark.org");
    ASSERT_TRUE(gotit != domains_->end());
1071 1072 1073
    DdnsDomainPtr& domain = gotit->second;

    // Verify the name and key_name values of the first domain.
1074 1075
    EXPECT_EQ("tmark.org", domain->getName());
    EXPECT_EQ("d2_key.tmark.org", domain->getKeyName());
1076 1077
    ASSERT_TRUE(domain->getTSIGKeyInfo());
    ASSERT_TRUE(domain->getTSIGKeyInfo()->getTSIGKey());
1078 1079 1080 1081 1082

    // Verify the each of the first domain's servers
    DnsServerInfoStoragePtr servers = domain->getServers();
    EXPECT_TRUE(servers);
    count =  servers->size();
1083
    EXPECT_EQ(3, count);
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

    DnsServerInfoPtr server = (*servers)[0];
    EXPECT_TRUE(server);
    EXPECT_TRUE(checkServer(server, "", "127.0.0.1", 100));

    server = (*servers)[1];
    EXPECT_TRUE(server);
    EXPECT_TRUE(checkServer(server, "", "127.0.0.2", 200));

    server = (*servers)[2];
    EXPECT_TRUE(server);
    EXPECT_TRUE(checkServer(server, "", "127.0.0.3", 300));

    // Verify second domain
1098 1099
    gotit = domains_->find("billcat.net");
    ASSERT_TRUE(gotit != domains_->end());
1100 1101 1102
    domain = gotit->second;

    // Verify the name and key_name values of the second domain.
1103 1104
    EXPECT_EQ("billcat.net", domain->getName());
    EXPECT_EQ("d2_key.billcat.net", domain->getKeyName());
1105 1106
    ASSERT_TRUE(domain->getTSIGKeyInfo());
    ASSERT_TRUE(domain->getTSIGKeyInfo()->getTSIGKey());