dhcp_parsers_unittest.cc 24.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
//
// 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.h>
#include <config/ccsession.h>
#include <dhcp/option.h>
#include <dhcp/option_custom.h>
#include <dhcp/option_int.h>
20
#include <dhcpsrv/cfgmgr.h>
21 22
#include <dhcpsrv/subnet.h>
#include <dhcpsrv/dhcp_parsers.h>
Stephen Morris's avatar
Stephen Morris committed
23
#include <dhcpsrv/tests/test_libraries.h>
24
#include <exceptions/exceptions.h>
25
#include <hooks/hooks_manager.h>
26 27 28

#include <gtest/gtest.h>
#include <boost/foreach.hpp>
29
#include <boost/pointer_cast.hpp>
30 31 32 33 34 35 36

#include <map>
#include <string>

using namespace std;
using namespace isc;
using namespace isc::config;
37 38 39
using namespace isc::data;
using namespace isc::dhcp;
using namespace isc::hooks;
40 41 42 43 44 45 46 47 48

namespace {

/// @brief DHCP Parser test fixture class
class DhcpParserTest : public ::testing::Test {
public:
    /// @brief Constructor
    ///
    DhcpParserTest() {
49
        CfgMgr::instance().deleteActiveIfaces();
50 51 52 53 54
    }
};


/// @brief Check BooleanParser basic functionality.
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
/// Verifies that the parser:
/// 1. Does not allow empty for storage.
/// 2. Rejects a non-boolean element.
/// 3. Builds with a valid true value.
/// 4. Bbuils with a valid false value.
/// 5. Updates storage upon commit.
TEST_F(DhcpParserTest, booleanParserTest) {

    const std::string name = "boolParm";

    // Verify that parser does not allow empty for storage.
    BooleanStoragePtr bs;
    EXPECT_THROW(BooleanParser(name, bs), isc::dhcp::DhcpConfigError);

    // Construct parser for testing.
    BooleanStoragePtr storage(new BooleanStorage());
    BooleanParser parser(name, storage);

    // Verify that parser with rejects a non-boolean element.
    ElementPtr wrong_element = Element::create("I am a string");
    EXPECT_THROW(parser.build(wrong_element), isc::BadValue);

    // Verify that parser will build with a valid true value.
    bool test_value = true;
    ElementPtr element = Element::create(test_value);
    ASSERT_NO_THROW(parser.build(element));

    // Verify that commit updates storage.
84
    bool actual_value = !test_value;
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    parser.commit();
    EXPECT_NO_THROW((actual_value = storage->getParam(name)));
    EXPECT_EQ(test_value, actual_value);

    // Verify that parser will build with a valid false value.
    test_value = false;
    element->setValue(test_value);
    EXPECT_NO_THROW(parser.build(element));

    // Verify that commit updates storage.
    actual_value = ~test_value;
    parser.commit();
    EXPECT_NO_THROW((actual_value = storage->getParam(name)));
    EXPECT_EQ(test_value, actual_value);
}

/// @brief Check StringParser basic functionality
102
///
103 104
/// Verifies that the parser:
/// 1. Does not allow empty for storage.
105
/// 2. Builds with a nont string value.
106 107 108 109 110 111 112 113 114 115 116 117 118 119
/// 3. Builds with a string value.
/// 4. Updates storage upon commit.
TEST_F(DhcpParserTest, stringParserTest) {

    const std::string name = "strParm";

    // Verify that parser does not allow empty for storage.
    StringStoragePtr bs;
    EXPECT_THROW(StringParser(name, bs), isc::dhcp::DhcpConfigError);

    // Construct parser for testing.
    StringStoragePtr storage(new StringStorage());
    StringParser parser(name, storage);

120 121 122
    // Verify that parser with accepts a non-string element.
    ElementPtr element = Element::create(9999);
    EXPECT_NO_THROW(parser.build(element));
123

124 125 126 127 128 129
    // Verify that commit updates storage.
    parser.commit();
    std::string actual_value;
    EXPECT_NO_THROW((actual_value = storage->getParam(name)));
    EXPECT_EQ("9999", actual_value);

130 131
    // Verify that parser will build with a string value.
    const std::string test_value = "test value";
132
    element = Element::create(test_value);
133 134 135 136 137 138 139 140 141
    ASSERT_NO_THROW(parser.build(element));

    // Verify that commit updates storage.
    parser.commit();
    EXPECT_NO_THROW((actual_value = storage->getParam(name)));
    EXPECT_EQ(test_value, actual_value);
}

/// @brief Check Uint32Parser basic functionality
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
/// Verifies that the parser:
/// 1. Does not allow empty for storage.
/// 2. Rejects a non-integer element.
/// 3. Rejects a negative value.
/// 4. Rejects too large a value.
/// 5. Builds with value of zero.
/// 6. Builds with a value greater than zero.
/// 7. Updates storage upon commit.
TEST_F(DhcpParserTest, uint32ParserTest) {

    const std::string name = "intParm";

    // Verify that parser does not allow empty for storage.
    Uint32StoragePtr bs;
    EXPECT_THROW(Uint32Parser(name, bs), isc::dhcp::DhcpConfigError);

    // Construct parser for testing.
    Uint32StoragePtr storage(new Uint32Storage());
    Uint32Parser parser(name, storage);

    // Verify that parser with rejects a non-interger element.
    ElementPtr wrong_element = Element::create("I am a string");
    EXPECT_THROW(parser.build(wrong_element), isc::BadValue);

    // Verify that parser with rejects a negative value.
    ElementPtr int_element = Element::create(-1);
    EXPECT_THROW(parser.build(int_element), isc::BadValue);

171 172
    // Verify that parser with rejects too large a value provided we are on
    // 64-bit platform.
173 174 175 176 177
    if (sizeof(long) > sizeof(uint32_t)) {
        long max = (long)(std::numeric_limits<uint32_t>::max()) + 1;
        int_element->setValue(max);
        EXPECT_THROW(parser.build(int_element), isc::BadValue);
    }
178 179 180 181 182 183

    // Verify that parser will build with value of zero.
    int test_value = 0;
    int_element->setValue((long)test_value);
    ASSERT_NO_THROW(parser.build(int_element));

184 185 186 187 188 189
    // Verify that commit updates storage.
    parser.commit();
    uint32_t actual_value = 0;
    EXPECT_NO_THROW((actual_value = storage->getParam(name)));
    EXPECT_EQ(test_value, actual_value);

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    // Verify that parser will build with a valid positive value.
    test_value = 77;
    int_element->setValue((long)test_value);
    ASSERT_NO_THROW(parser.build(int_element));

    // Verify that commit updates storage.
    parser.commit();
    EXPECT_NO_THROW((actual_value = storage->getParam(name)));
    EXPECT_EQ(test_value, actual_value);
}

/// @brief Check InterfaceListParser  basic functionality
///
/// Verifies that the parser:
/// 1. Does not allow empty for storage.
205 206
/// 2. Does not allow name other than "interfaces"
/// 3. Parses list of interfaces and adds them to CfgMgr
207 208
/// 4. Parses wildcard interface name and sets a CfgMgr flag which indicates
/// that server will listen on all interfaces.
209 210
TEST_F(DhcpParserTest, interfaceListParserTest) {

211
    const std::string name = "interfaces";
212 213 214 215

    // Verify that parser constructor fails if parameter name isn't "interface"
    EXPECT_THROW(InterfaceListConfigParser("bogus_name"), isc::BadValue);

216 217
    boost::scoped_ptr<InterfaceListConfigParser>
        parser(new InterfaceListConfigParser(name));
218 219 220
    ElementPtr list_element = Element::createList();
    list_element->add(Element::create("eth0"));
    list_element->add(Element::create("eth1"));
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

    // Make sure there are no interfaces added yet.
    ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth0"));
    ASSERT_FALSE(CfgMgr::instance().isActiveIface("eth1"));

    // This should parse the configuration and add eth0 and eth1 to the list
    // of interfaces that server should listen on.
    parser->build(list_element);
    parser->commit();

    // Use CfgMgr instance to check if eth0 and eth1 was added, and that
    // eth2 was not added.
    CfgMgr& cfg_mgr = CfgMgr::instance();
    EXPECT_TRUE(cfg_mgr.isActiveIface("eth0"));
    EXPECT_TRUE(cfg_mgr.isActiveIface("eth1"));
    EXPECT_FALSE(cfg_mgr.isActiveIface("eth2"));

    // Add keyword all to the configuration. This should activate all
    // interfaces, including eth2, even though it has not been explicitly
    // added.
241
    list_element->add(Element::create("*"));
242 243 244 245 246 247 248 249 250

    // Reset parser's state.
    parser.reset(new InterfaceListConfigParser(name));
    parser->build(list_element);
    parser->commit();

    EXPECT_TRUE(cfg_mgr.isActiveIface("eth0"));
    EXPECT_TRUE(cfg_mgr.isActiveIface("eth1"));
    EXPECT_TRUE(cfg_mgr.isActiveIface("eth2"));
251 252
}

253
/// @brief Test Implementation of abstract OptionDataParser class. Allows
254
/// testing basic option parsing.
255 256 257
class UtestOptionDataParser : public OptionDataParser {
public:

258 259
    UtestOptionDataParser(const std::string&,
        OptionStoragePtr options, ParserContextPtr global_context)
260 261 262 263 264 265 266 267 268 269 270 271 272
        :OptionDataParser("", options, global_context) {
    }

    static OptionDataParser* factory(const std::string& param_name,
        OptionStoragePtr options, ParserContextPtr global_context) {
        return (new UtestOptionDataParser(param_name, options, global_context));
    }

protected:
    // Dummy out last two params since test derivation doesn't use them.
    virtual OptionDefinitionPtr findServerSpaceOptionDefinition (
                std::string&, uint32_t) {
        OptionDefinitionPtr def;
273
        // always return empty
274 275 276 277
        return (def);
    }
};

278
/// @brief Test Fixture class which provides basic structure for testing
279 280 281 282 283 284 285 286 287
/// configuration parsing.  This is essentially the same structure provided
/// by dhcp servers.
class ParseConfigTest : public ::testing::Test {
public:
    /// @brief Constructor
    ParseConfigTest() {
        reset_context();
    }

288 289 290 291
    ~ParseConfigTest() {
        reset_context();
    }

292
    /// @brief Parses a configuration.
293 294
    ///
    /// Parse the given configuration, populating the context storage with
295 296
    /// the parsed elements.
    ///
297 298 299
    /// @param config_set is the set of elements to parse.
    /// @return returns an ConstElementPtr containing the numeric result
    /// code and outcome comment.
300
    isc::data::ConstElementPtr parseElementSet(isc::data::ConstElementPtr
301 302 303 304 305 306 307 308 309 310 311 312 313 314
                                           config_set) {
        // Answer will hold the result.
        ConstElementPtr answer;
        if (!config_set) {
            answer = isc::config::createAnswer(1,
                                 string("Can't parse NULL config"));
            return (answer);
        }

        // option parsing must be done last, so save it if we hit if first
        ParserPtr option_parser;

        ConfigPair config_pair;
        try {
315
            // Iterate over the config elements.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
            const std::map<std::string, ConstElementPtr>& values_map =
                                                      config_set->mapValue();
            BOOST_FOREACH(config_pair, values_map) {
                // Create the parser based on element name.
                ParserPtr parser(createConfigParser(config_pair.first));
                // Options must be parsed last
                if (config_pair.first == "option-data") {
                    option_parser = parser;
                } else {
                    // Anything else  we can call build straight away.
                    parser->build(config_pair.second);
                    parser->commit();
                }
            }

            // The option values parser is the next one to be run.
332
            std::map<std::string, ConstElementPtr>::const_iterator
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
                                option_config = values_map.find("option-data");
            if (option_config != values_map.end()) {
                option_parser->build(option_config->second);
                option_parser->commit();
            }

            // Everything was fine. Configuration is successful.
            answer = isc::config::createAnswer(0, "Configuration committed.");
        } catch (const isc::Exception& ex) {
            answer = isc::config::createAnswer(1,
                        string("Configuration parsing failed: ") + ex.what());

        } catch (...) {
            answer = isc::config::createAnswer(1,
                                        string("Configuration parsing failed"));
        }

        return (answer);
    }

    /// @brief Create an element parser based on the element name.
    ///
355 356 357 358 359
    /// Creates a parser for the appropriate element and stores a pointer to it
    /// in the appropriate class variable.
    ///
    /// Note that the method currently it only supports option-defs, option-data
    /// and hooks-libraries.
360 361
    /// 
    /// @param config_id is the name of the configuration element. 
362 363 364
    ///
    /// @return returns a shared pointer to DhcpConfigParser.
    ///
365
    /// @throw throws NotImplemented if element name isn't supported.
366 367
    ParserPtr createConfigParser(const std::string& config_id) {
        ParserPtr parser;
368
        if (config_id.compare("option-data") == 0) {
369 370 371 372 373
            parser.reset(new OptionDataListParser(config_id, 
                                              parser_context_->options_, 
                                              parser_context_,
                                              UtestOptionDataParser::factory));

374
        } else if (config_id.compare("option-def") == 0) {
375 376 377 378 379 380 381 382
            parser.reset(new OptionDefListParser(config_id, 
                                              parser_context_->option_defs_));

        } else if (config_id.compare("hooks-libraries") == 0) {
            parser.reset(new HooksLibrariesParser(config_id));
            hooks_libraries_parser_ =
                boost::dynamic_pointer_cast<HooksLibrariesParser>(parser);

383 384 385 386 387 388 389 390 391
        } else {
            isc_throw(NotImplemented,
                "Parser error: configuration parameter not supported: "
                << config_id);
        }

        return (parser);
    }

392
    /// @brief Convenience method for parsing a configuration 
393 394
    /// 
    /// Given a configuration string, convert it into Elements
395
    /// and parse them.
396 397
    /// @param config is the configuration string to parse
    ///
398
    /// @return retuns 0 if the configuration parsed successfully,
399
    /// non-zero otherwise failure.
Stephen Morris's avatar
Stephen Morris committed
400
    int parseConfiguration(const std::string& config) {    
401 402 403 404 405 406 407
        int rcode_ = 1;
        // Turn config into elements.
        // Test json just to make sure its valid.
        ElementPtr json = Element::fromJSON(config);
        EXPECT_TRUE(json);
        if (json) {
            ConstElementPtr status = parseElementSet(json);
408 409
            ConstElementPtr comment = parseAnswer(rcode_, status);
            error_text_ = comment->stringValue();
410 411 412 413 414
        }

        return (rcode_);
    }

415
    /// @brief Find an option definition for a given space and code within
416 417 418 419 420
    /// the parser context.
    /// @param space is the space name of the desired option.
    /// @param code is the numeric "type" of the desired option.
    /// @return returns an OptionDefinitionPtr which points to the found
    /// definition or is empty.
421
    /// ASSERT_ tests don't work inside functions that return values
422 423 424
    OptionDefinitionPtr getOptionDef(std::string space, uint32_t code)
    {
        OptionDefinitionPtr def;
425
        OptionDefContainerPtr defs =
426 427 428 429 430 431 432 433 434 435 436 437 438
                            parser_context_->option_defs_->getItems(space);
        // Should always be able to get definitions list even if it is empty.
        EXPECT_TRUE(defs);
        if (defs) {
            // Attempt to find desired definiton.
            const OptionDefContainerTypeIndex& idx = defs->get<1>();
            const OptionDefContainerTypeRange& range = idx.equal_range(code);
            int cnt = std::distance(range.first, range.second);
            EXPECT_EQ(1, cnt);
            if (cnt == 1) {
                def = *(idx.begin());
            }
        }
439
        return (def);
440 441
    }

442
    /// @brief Find an option for a given space and code within the parser
443 444 445 446 447
    /// context.
    /// @param space is the space name of the desired option.
    /// @param code is the numeric "type" of the desired option.
    /// @return returns an OptionPtr which points to the found
    /// option or is empty.
448
    /// ASSERT_ tests don't work inside functions that return values
449 450 451
    OptionPtr getOptionPtr(std::string space, uint32_t code)
    {
        OptionPtr option_ptr;
452
        Subnet::OptionContainerPtr options =
453 454 455 456 457 458
                            parser_context_->options_->getItems(space);
        // Should always be able to get options list even if it is empty.
        EXPECT_TRUE(options);
        if (options) {
            // Attempt to find desired option.
            const Subnet::OptionContainerTypeIndex& idx = options->get<1>();
459
            const Subnet::OptionContainerTypeRange& range =
460 461 462 463
                                                        idx.equal_range(code);
            int cnt = std::distance(range.first, range.second);
            EXPECT_EQ(1, cnt);
            if (cnt == 1) {
464 465
                Subnet::OptionDescriptor desc = *(idx.begin());
                option_ptr = desc.option;
466 467 468 469
                EXPECT_TRUE(option_ptr);
            }
        }

470
        return (option_ptr);
471 472
    }

473
    /// @brief Wipes the contents of the context to allowing another parsing
474 475 476
    /// during a given test if needed.
    void reset_context(){
        // Note set context universe to V6 as it has to be something.
477 478 479
        CfgMgr::instance().deleteSubnets4();
        CfgMgr::instance().deleteSubnets6();
        CfgMgr::instance().deleteOptionDefs();
480
        parser_context_.reset(new ParserContext(Option::V6));
481 482 483

        // Ensure no hooks libraries are loaded.
        HooksManager::unloadLibraries();
484 485
    }

486 487 488 489 490
    /// @brief Parsers used in the parsing of the configuration
    ///
    /// Allows the tests to interrogate the state of the parsers (if required).
    boost::shared_ptr<HooksLibrariesParser> hooks_libraries_parser_;

491 492
    /// @brief Parser context - provides storage for options and definitions
    ParserContextPtr parser_context_;
493 494 495

    /// @brief Error string if the parsing failed
    std::string error_text_;
496 497 498
};

/// @brief Check Basic parsing of option definitions.
499
///
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
/// Note that this tests basic operation of the OptionDefinitionListParser and
/// OptionDefinitionParser.  It uses a simple configuration consisting of one
/// one definition and verifies that it is parsed and committed to storage
/// correctly.
TEST_F(ParseConfigTest, basicOptionDefTest) {

    // Configuration string.
    std::string config =
        "{ \"option-def\": [ {"
        "      \"name\": \"foo\","
        "      \"code\": 100,"
        "      \"type\": \"ipv4-address\","
        "      \"array\": False,"
        "      \"record-types\": \"\","
        "      \"space\": \"isc\","
        "      \"encapsulate\": \"\""
        "  } ]"
        "}";

    // Verify that the configuration string parses.
    int rcode = parseConfiguration(config);
    ASSERT_TRUE(rcode == 0);

523

524
    // Verify that the option definition can be retrieved.
525
    OptionDefinitionPtr def = getOptionDef("isc", 100);
526 527 528 529 530 531 532 533 534 535 536
    ASSERT_TRUE(def);

    // Verify that the option definition is correct.
    EXPECT_EQ("foo", def->getName());
    EXPECT_EQ(100, def->getCode());
    EXPECT_FALSE(def->getArrayType());
    EXPECT_EQ(OPT_IPV4_ADDRESS_TYPE, def->getType());
    EXPECT_TRUE(def->getEncapsulatedSpace().empty());
}

/// @brief Check Basic parsing of options.
537
///
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
/// Note that this tests basic operation of the OptionDataListParser and
/// OptionDataParser.  It uses a simple configuration consisting of one
/// one definition and matching option data.  It verifies that the option
/// is parsed and committed to storage correctly.
TEST_F(ParseConfigTest, basicOptionDataTest) {

    // Configuration string.
    std::string config =
        "{ \"option-def\": [ {"
        "      \"name\": \"foo\","
        "      \"code\": 100,"
        "      \"type\": \"ipv4-address\","
        "      \"array\": False,"
        "      \"record-types\": \"\","
        "      \"space\": \"isc\","
        "      \"encapsulate\": \"\""
        " } ], "
        " \"option-data\": [ {"
        "    \"name\": \"foo\","
        "    \"space\": \"isc\","
        "    \"code\": 100,"
        "    \"data\": \"192.168.2.1\","
        "    \"csv-format\": True"
        " } ]"
        "}";

    // Verify that the configuration string parses.
    int rcode = parseConfiguration(config);
    ASSERT_TRUE(rcode == 0);

    // Verify that the option can be retrieved.
    OptionPtr opt_ptr = getOptionPtr("isc", 100);
    ASSERT_TRUE(opt_ptr);

    // Verify that the option definition is correct.
    std::string val = "type=100, len=4, data fields:\n "
                      " #0 192.168.2.1 ( ipv4-address ) \n";

    EXPECT_EQ(val, opt_ptr->toText());
}

};  // Anonymous namespace

Stephen Morris's avatar
Stephen Morris committed
581 582
/// These tests check basic operation of the HooksLibrariesParser.

583 584
// hooks-libraries that do not contain anything.
TEST_F(ParseConfigTest, noHooksLibrariesTest) {
Stephen Morris's avatar
Stephen Morris committed
585

586 587
    // Configuration with hooks-libraries not present.
    string config = "{ \"hooks-libraries\": [] }";
Stephen Morris's avatar
Stephen Morris committed
588 589 590

    // Verify that the configuration string parses.
    int rcode = parseConfiguration(config);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    ASSERT_TRUE(rcode == 0) << error_text_;

    // Check that the parser recorded no change to the current state
    // (as the test starts with no hooks libraries loaded).
    std::vector<std::string> libraries;
    bool changed;
    hooks_libraries_parser_->getLibraries(libraries, changed);
    EXPECT_TRUE(libraries.empty());
    EXPECT_FALSE(changed);

    // Load a single library and repeat the parse.
    vector<string> basic_library;
    basic_library.push_back(string(BASIC_CALLOUT_LIBRARY));
    HooksManager::loadLibraries(basic_library);

    rcode = parseConfiguration(config);
    ASSERT_TRUE(rcode == 0) << error_text_;

    // This time the change should have been recorded.
    hooks_libraries_parser_->getLibraries(libraries, changed);
    EXPECT_TRUE(libraries.empty());
    EXPECT_TRUE(changed);

    // But repeating it again and we are back to no change.
    rcode = parseConfiguration(config);
    ASSERT_TRUE(rcode == 0) << error_text_;
    hooks_libraries_parser_->getLibraries(libraries, changed);
    EXPECT_TRUE(libraries.empty());
    EXPECT_FALSE(changed);
Stephen Morris's avatar
Stephen Morris committed
620 621 622 623

}


624
TEST_F(ParseConfigTest, validHooksLibrariesTest) {
Stephen Morris's avatar
Stephen Morris committed
625

626
    // Configuration string.  This contains a set of valid libraries.
Stephen Morris's avatar
Stephen Morris committed
627 628 629 630 631
    const std::string quote("\"");
    const std::string comma(", ");

    const std::string config =
        std::string("{ ") +
632
            std::string("\"hooks-libraries\": [") +
Stephen Morris's avatar
Stephen Morris committed
633 634 635 636 637 638 639
                quote + std::string(BASIC_CALLOUT_LIBRARY) + quote + comma +
                quote + std::string(FULL_CALLOUT_LIBRARY)  + quote +
            std::string("]") +
        std::string("}");

    // Verify that the configuration string parses.
    int rcode = parseConfiguration(config);
640
    ASSERT_TRUE(rcode == 0) << error_text_;
Stephen Morris's avatar
Stephen Morris committed
641

642 643 644 645 646 647 648
    // Check that the parser holds two libraries and the configuration is
    // recorded as having changed.
    std::vector<std::string> libraries;
    bool changed;
    hooks_libraries_parser_->getLibraries(libraries, changed);
    EXPECT_EQ(2, libraries.size());
    EXPECT_TRUE(changed);
Stephen Morris's avatar
Stephen Morris committed
649 650 651 652 653 654

    // The expected libraries should be the list of libraries specified
    // in the given order.
    std::vector<std::string> expected;
    expected.push_back(BASIC_CALLOUT_LIBRARY);
    expected.push_back(FULL_CALLOUT_LIBRARY);
655 656 657 658 659
    EXPECT_TRUE(expected == libraries);

    // Parse the string again.
    rcode = parseConfiguration(config);
    ASSERT_TRUE(rcode == 0) << error_text_;
Stephen Morris's avatar
Stephen Morris committed
660

661 662 663 664
    // The list has not changed, and this is what we should see.
    hooks_libraries_parser_->getLibraries(libraries, changed);
    EXPECT_EQ(2, libraries.size());
    EXPECT_FALSE(changed);
Stephen Morris's avatar
Stephen Morris committed
665 666
}

667
// Check with a set of libraries, some of which are invalid.
Stephen Morris's avatar
Stephen Morris committed
668 669 670 671 672 673 674 675 676 677 678
TEST_F(ParseConfigTest, invalidHooksLibrariesTest) {

    // @todo Initialize global library context to null

    // Configuration string.  This contains an invalid library which should
    // trigger an error in the "build" stage.
    const std::string quote("\"");
    const std::string comma(", ");

    const std::string config =
        std::string("{ ") +
679
            std::string("\"hooks-libraries\": [") +
Stephen Morris's avatar
Stephen Morris committed
680 681 682 683 684 685
                quote + std::string(BASIC_CALLOUT_LIBRARY) + quote + comma +
                quote + std::string(NOT_PRESENT_LIBRARY) + quote + comma +
                quote + std::string(FULL_CALLOUT_LIBRARY)  + quote +
            std::string("]") +
        std::string("}");

686 687
    // Verify that the configuration fails to parse. (Syntactically it's OK,
    // but the library is invalid).
Stephen Morris's avatar
Stephen Morris committed
688
    int rcode = parseConfiguration(config);
689
    ASSERT_FALSE(rcode == 0) << error_text_;
Stephen Morris's avatar
Stephen Morris committed
690

691 692 693
    // Check that the message contains the library in error.
    EXPECT_FALSE(error_text_.find(NOT_PRESENT_LIBRARY) == string::npos) <<
        "Error text returned from parse failure is " << error_text_;
Stephen Morris's avatar
Stephen Morris committed
694
}