#include "lib/utils/TempFile.h" #include "lib/utils/TempDir.h" #include "../config.h" #include "../ConfigurationState.h" #include "../ConfigurationException.h" #include "lib/expression/ParserExceptions.h" #include "lib/config/protos/testkeys.pb.h" #include "lib/config/invalid/invalid.pb.h" #include "HTTPServer.h" #include "catch/catch.hpp" #include #include using namespace bts::config; using namespace bts::config::internal; namespace proto = google::protobuf; using bts::config::test::Foo; using bts::config::test::FooGroup; using bts::config::test::FooList; namespace bts { int config_ref_tests = 0; TEST_CASE ("Configuration Reference Tests", "[config]") { SECTION("basic_value") { initialize_for_test( "TestSite {" " value {" " name: 'ref value'" " }" "}" "Foo { \n" " value {\n" " name: @TestSite.name" " }\n" "}\n" ); const Foo& foo = get(); REQUIRE(foo.name() == "ref value"); } SECTION("scalar_to_repeat") { initialize_for_test( "TestSite {" " value {" " name: 'a@example.com' " " }" "}" "Foo { \n" " value {\n" " email: \"before@example.com\" \n" " email: @TestSite.name \n" " email: \"after@example.com\" \n" " }\n" "}\n" ); const Foo& foo = get(); REQUIRE(foo.email_size() == 3); REQUIRE(foo.email(0) == "before@example.com"); REQUIRE(foo.email(1) == "a@example.com"); REQUIRE(foo.email(2) == "after@example.com"); } SECTION("repeat_to_repeat ") { initialize_for_test( "StringTable {" " value {" " entries: 'a@example.com' " " entries: 'b@example.com' " " }" "}" "TestSite {" " value {" " name: 'c@example.com' " " }" "}" "Foo { \n" " value {\n" " email: \"before@example.com\" \n" " email: @StringTable.entries \n" " email: @TestSite.name \n" " email: \"after@example.com\" \n" " }\n" "}\n" ); const Foo& foo = get(); REQUIRE(foo.email_size() == 5); REQUIRE(foo.email(0) == "before@example.com"); REQUIRE(foo.email(1) == "a@example.com"); REQUIRE(foo.email(2) == "b@example.com"); REQUIRE(foo.email(3) == "c@example.com"); REQUIRE(foo.email(4) == "after@example.com"); } SECTION("message_value") { initialize_for_test( "Foo { \n" " rule: 'false'" " value {\n" " name: \"bad\"\n" " }\n" "}\n" "Foo { \n" " value {\n" " name: \"good\"\n" " }\n" "}\n" "FooGroup {" " value { " " group: @Foo.name " " foo: @Foo " " }" "}" ); const FooGroup& g = get(); REQUIRE(g.group() == "good"); REQUIRE(g.foo().name() == "good"); } SECTION("nested_message_value") { initialize_for_test( "NamedFoo { \n" " value {\n" " key: 'good foo' " " foo { " " name: \"good\"\n" " }" " }\n" "}\n" "FooGroup {" " value { " " group: @NamedFoo.key " " foo: @NamedFoo.foo " " }" "}" ); const FooGroup& g = get(); REQUIRE(g.group() == "good foo"); REQUIRE(g.foo().name() == "good"); } SECTION("macro_value") { initialize_for_test( "macro {" " name: 'macro1' " " expression: \"'m1'\" " "}" "Foo { \n" " value {\n" " name: $macro1" " }\n" "}\n" ); const Foo& foo = get(); REQUIRE(foo.name() == "m1"); } SECTION("enum_value") { initialize_for_test( "ConsoleColor {" " value {" " color: Blue " " }" "}" "ConsoleColors {" " value {" " color: @ConsoleColor.color" " colors: Green" " colors: @ConsoleColor.color" " colors: DarkRed" " colors: @ConsoleColor.color" " }" "}" ); const test::ConsoleColor& c1 = get(); const test::ConsoleColors& c2 = get(); REQUIRE(c2.color() == c1.color()); REQUIRE(c2.colors_size() == 4); REQUIRE(c2.colors(0) == test::ConsoleColor::Green); REQUIRE(c2.colors(1) == c1.color()); REQUIRE(c2.colors(2) == test::ConsoleColor::DarkRed); REQUIRE(c2.colors(3) == c1.color()); } SECTION("named_key") { initialize_for_test( "Foo { \n" " value {\n" " name: @FooList.item[foo2].foo.name " " }\n" "}\n" "FooList { \n" " value {\n" " item { " " key: 'foo1' " " foo { " " name: 'foo1' " " }" " }" " item { " " key: 'foo2' " " foo { " " name: 'foo2' " " }" " }" " item { " " key: 'foo3' " " foo { " " name: 'foo3' " " }" " }" " }\n" "}\n" ); REQUIRE(get().name() == "foo2"); } SECTION("named_key_rule") { initialize_for_test( "Foo { \n" " value {\n" " name: @FooList.item[foo2].foo.name " " }\n" "}\n" "FooList { \n" " value {\n" " item { " " key: 'foo1' " " foo { " " name: 'foo1' " " }" " }" " item { " " key: 'foo2' " " rule: '1=0' " " foo { " " name: 'foo2' " " }" " }" " item { " " key: 'foo3' " " foo { " " name: 'foo3' " " }" " }" " }\n" "}\n" ); //NOTE that a false rule will still create a message with defaults, so the list contains the same number of elements REQUIRE(get().name() == "Blue Foo"); REQUIRE(get().item_size() == 3); } SECTION("named_key2") { initialize_for_test( "Foo { \n" " value: @FooList.foo[foo2] " "}\n" "FooList { \n" " value {\n" " foo { " " name: 'foo1' " " id: 1 " " }" " foo { " " name: 'foo2' " " id: 2 " " }" " foo { " " name: 'foo3' " " id: 3 " " }" " }\n" "}\n" ); REQUIRE(get().name() == "foo2"); REQUIRE(get().id() == 2); } SECTION("named_key_syntax_error") { bool error = false; try { initialize_for_test( "Foo { \n" " value: @FooList[foo2] " // FooList doesn't have a key or a property named foo2 "}\n" "FooList { \n" " value {\n" " foo { " " name: 'foo1' " " id: 1 " " }" " foo { " " name: 'foo2' " " id: 2 " " }" " foo { " " name: 'foo3' " " id: 3 " " }" " }\n" "}\n" ); } catch(...) { error = true; } REQUIRE(error); } SECTION("named_struture") { initialize_for_test( "FooGroup { \n" " value {\n" " foo: @FooList.item[foo3].foo " " }\n" "}\n" "FooList { \n" " value {\n" " item { " " key: 'foo1' " " foo { " " name: 'foo1' " " id: 1 " " }" " }" " item { " " key: 'foo2' " " foo { " " name: 'foo2' " " id: 2 " " }" " }" " item { " " key: 'foo3' " " foo { " " name: 'foo3' " " id: 3 " " }" " }" " }\n" "}\n" ); REQUIRE(get().foo().name() == "foo3"); REQUIRE(get().foo().id() == 3); } SECTION("named_structure_rule") { initialize_for_test( "FooGroup { \n" " value {\n" " foo: @FooList.item[foo3].foo " " }\n" "}\n" "FooList { \n" " value {\n" " item { " " key: 'foo1' " " foo { " " name: 'foo1' " " id: 1 " " }" " }" " item { " " key: 'foo2' " " foo { " " name: 'foo2' " " id: 2 " " }" " }" " item { " " key: 'foo3' " " rule: 'false'" " foo { " " name: 'foo3' " " id: 3 " " }" " }" " }\n" "}\n" ); //NOTE that a false rule will still create a message with defaults, so the list contains the same number of elements REQUIRE(get().foo().name() == "Blue Foo"); REQUIRE(get().foo().id() == 1234); REQUIRE(get().item_size() == 3); } SECTION("named_value_rule") { initialize_for_test( "FooList { \n" " value {\n" " item { " " key: 'foo1' " " foo { " " name: 'foo1' " " id: 1 " " }" " }" " item { " " key: 'foo2' " " foo { " " name: 'foo2' " " id: 2 " " }" " }" " item { " " key: 'foo3' " " foo { " " name: 'foo3' " " id: 3 " " }" " }" " }\n" "}\n" "Foo { \n" // Adding [] to BooleanGrammer.h is very difficult because boost::spirit is crap // also it risks making list syntax [1,2,3] ambiguous so this is a workaround syntax " rule: 'FooList.item.foo1.foo.id = 2' " " value {\n" " name: 'bad' " " }\n" "}\n" "Foo { \n" " rule: 'FooList.item.foo2.foo.id = 2' " " value {\n" " name: 'good' " " }\n" "}\n" ); const Foo& foo = get(); REQUIRE(foo.name() == "good"); } SECTION("recursive_reference_good") { initialize_for_test( "TestSite { \n" " value {\n" " name: \"good\"\n" " }\n" "}\n" "Foo { \n" " value {\n" " name: @TestSite.name " " }\n" "}\n" "FooGroup {" " value { " " group: @Foo.name " " foo: @Foo " " }" "}" ); REQUIRE(get().group() == "good"); REQUIRE(get().foo().name() == "good"); } SECTION("recursive_reference_bad") { bool error = false; try { initialize_for_test( "TestSite { \n" " value {\n" " name: @Foo.name " " }\n" "}\n" "Foo { \n" " value {\n" " name: @TestSite.name " " }\n" "}\n" "FooGroup {" " value { " " group: @Foo.name " " foo: @Foo " " }" "}" ); } catch(...) { error = true; } REQUIRE(error); } SECTION("recursive_order_independent") { initialize_for_test( "FooGroup {" " value { " " group: @Foo.name " " foo: @Foo " " }" "}" "TestSite { \n" " value {\n" " name: \"good\"\n" " }\n" "}\n" "Foo { \n" " value {\n" " name: @TestSite.name " " }\n" "}\n" ); // In this test we mighe expect @Foo.name or @Foo to be evaluated first // But Foo is incomplete until @TestSite.name is evaluated. REQUIRE(get().group() == "good"); REQUIRE(get().foo().name() == "good"); } SECTION("loop_test") { bool error = false; try { initialize_for_test( "Foo { \n" " value {\n" " name: @Foo.name " " }\n" "}\n"); } catch(...) { error = true; } REQUIRE(error); } SECTION("merge_test") { // Config used to go through a merge step for each file in a directory. // This would invalidate the memory held by the deferred value queue and cause // crashes and instability. This is to verify that we don't regress the new behavior. HTTPServer server; std::string port = boost::lexical_cast(server.port()); std::string config_url = "http://localhost:" + port + "/"; std::string response = "HTTP/1.0 200 OK\r\n" "Content-Type: text/plain\r\n\r\n"; server.add_response("/spark.conf", response, "Foo {\n" " value: {\n" " name: \"included_value\"\n" " id: 4096\n" " }}\n" "TestSite {" " value { " " name: @FooGroup.group" // reference to testkeys.conf " }" "}" ); server.add_response("/testkeys.conf", response, "FooGroup {\n" " value {\n" " group: \"http_group\"\n" " foo {\n" " name: @Foo.name " // reference to spark.conf " id: 80\n" "}}}\n" ); server.start(); initialize_for_test("include: \"" + config_url + "\""); server.stop(); const FooGroup& group = get(); const test::TestSite& site = get(); REQUIRE(group.foo().name() == "included_value"); REQUIRE(site.name() == "http_group"); } SECTION("test_conversion") { initialize_for_test( "Foo {\n" " value: {\n" " name: @RuleRef.a_int \n" " id: @RuleRef.a_string " " }}\n" "Foo {\n" " rule: 'false' " " value: {\n" " name: @RuleRef.a_string \n" " id: @RuleRef.a_int " " }}\n" "RuleRef { " " value { " " a_string: '42' " " a_int: 99 " " }" "}" ); const Foo& foo = get(); REQUIRE(foo.name() == "99"); REQUIRE(foo.id() == 42); } SECTION("test_int") { initialize_for_test( "Foo {\n" " value: {\n" " id: @RuleRef.a_int " " }}\n" "RuleRef { " " value { " " a_string: 'good' " " a_int: 99 " " }" "}" ); const Foo& foo = get(); REQUIRE(foo.id() == 99); } SECTION("test_repeated_to_repeated") { initialize_for_test( "FooList {\n" " value: {\n" " foo {\n" " name: 'good1' \n" " }\n" " foo {\n" " name: 'good2' \n" " }\n" " }}\n" "RuleRef { " " value { " " foox { " " name: 'original' " " }" " foox: @FooList.foo " " }" "}" ); const test::RuleRef& result = get(); REQUIRE(result.foox_size() == 3); REQUIRE(result.foox(0).name() == "original"); REQUIRE(result.foox(1).name() == "good1"); REQUIRE(result.foox(2).name() == "good2"); } SECTION("test_repeated_after_key_rule") { initialize_for_test( "StringTable { \n" " value { \n" " entries: 'a' \n" " entries: 'b' \n" " } \n" "} \n" "FooList {\n" " value: {\n" " item {\n" " key: 'one' \n" " rule: \" 'a' in StringTable.entries\" \n" " foox { \n" " name: 'good1' \n" " }\n" " foox { \n" " name: 'good2' \n" " }\n" " }\n" " item {\n" " key: 'two' \n" " foox { \n" " name: 'bad1' \n" " }\n" " }\n" " }}\n" "RuleRef { " " value { " " foox { " " name: 'original' " " }" " foox: @FooList.item[one].foox " " }" "}" ); const test::RuleRef& result = get(); REQUIRE(result.foox_size() == 3); REQUIRE(result.foox(0).name() == "original"); REQUIRE(result.foox(1).name() == "good1"); REQUIRE(result.foox(2).name() == "good2"); } SECTION("test_repeated_after_key_rule2") { //This test fails on purpose to illustrate the frailty of the rule evaluation feature in 'keyed' list keys initialize_for_test( "TestSite {" " value {" " name: 'a' " " }" "}" "StringTable {" " value {" " entries: @TestSite.name " " entries: 'b' " " }" "}" "FooList {\n" " value: {\n" " item {\n" " key: 'one' \n" " rule: \"'a' in StringTable.entries\" \n" " foox { \n" " name: 'good1' \n" " }\n" " foox { \n" " name: 'good2' \n" " }\n" " }\n" " item {\n" " key: 'two' \n" " foox { \n" " name: 'bad1' \n" " }\n" " }\n" " }}\n" "RuleRef { " " value { " " foox { " " name: 'original' " " }" " foox: @FooList.item[one].foox " " }" "}" ); const test::RuleRef& result = get(); REQUIRE(result.foox_size() == 3); REQUIRE(result.foox(0).name() == "original"); REQUIRE(result.foox(1).name() == "good1"); REQUIRE(result.foox(2).name() == "good2"); } SECTION("test_repeated_after_key_rule3") { //This test fails on purpose to illustrate the frailty of the rule evaluation feature in 'keyed' list keys initialize_for_test( "RuleRef { " " value { " " foox { " " name: 'original' " " }" " foox: @FooList.item[one].foox " " }" "}" "TestSite {" " value {" " name: 'a' " " }" "}" "StringTable {" " value {" " entries: @TestSite.name " " entries: 'b' " " }" "}" "FooList {\n" " value: {\n" " item {\n" " key: 'one' \n" " rule: \"'a' in StringTable.entries\" \n" " foox { \n" " name: 'good1' \n" " }\n" " foox { \n" " name: 'good2' \n" " }\n" " }\n" " item {\n" " key: 'two' \n" " foox { \n" " name: 'bad1' \n" " }\n" " }\n" " }}\n" ); const test::RuleRef& result = get(); //the order matters here, but seems to be deterministic REQUIRE(result.foox_size() == 1); } // SECTION("test_repeated_after_key_rule_should_fail") { // //This test fails on purpose to illustrate the frailty of the rule evaluation feature in 'keyed' list keys // initialize_for_test( // "RuleRef { " // " value { " // " foox { " // " name: 'original' " // " }" // " foox: @FooList.item[one].foox " // " }" // "}" // "TestSite {" // " value {" // " name: 'a' " // " }" // "}" // "StringTable {" // " value {" // " entries: @TestSite.name " // " entries: 'b' " // " }" // "}" // "FooList {\n" // " value: {\n" // " item {\n" // " key: 'one' \n" // " rule: \"'a' in StringTable.entries\" \n" // " foox { \n" // " name: 'good1' \n" // " }\n" // " foox { \n" // " name: 'good2' \n" // " }\n" // " }\n" // " item {\n" // " key: 'two' \n" // " foox { \n" // " name: 'bad1' \n" // " }\n" // " }\n" // " }}\n" // ); // const test::RuleRef& result = get(); // //this should fail // REQUIRE(result.foox_size() == 3); // } SECTION("test_irrelevant_lookup") { // If a key references another key which is not present it should only be an error if the resulting key will ever be used. initialize_for_test( "FooList {\n" " value: {\n" " item {\n" " key: 'one' \n" " foox { \n" " name: 'good1' \n" " }\n" " foox { \n" " name: 'good2' \n" " }\n" " }\n" " item {\n" " key: 'two' \n" " foox { \n" " name: 'bad1' \n" " }\n" " }\n" " }}\n" "RuleRef { " " rule: 'false' " " value { " " foox { " " name: 'never used' " " }" " foox: @FooList.item[XXXXX].foox " // Invalid lookup " }" "}" "RuleRef { " " value { " " foox { " " name: 'original' " " }" " foox: @FooList.item[one].foox " " }" "}" ); const test::RuleRef& result = get(); REQUIRE(result.foox_size() == 3); } SECTION("macro_definition_order_test_hard") { // The $is_crusible test hard version // $macro1 tests Value2.x with rule about $macro2 before $macro2 is defined // Value2.x is an @reference of a 4th location initialize_for_test( "TestSite {" " value { name: 'cme_1u7_cert' }" "}" "Foo {" " rule: '$is_crucible'" " value { name: @TestSite.name }" "}" "" "Foo {" " value { name: '' }" "}" "" "macro {" " name: 'o2_cme_enabled'" " expression: \"Foo.name != ''\" " "}" "" "macro {" " name: 'is_crucible'" " expression: 'true'" "}" ); REQUIRE(get().name() == "cme_1u7_cert"); } reset(); } }