diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index c8cdbe93..4441657b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,9 +15,24 @@ # the License. # if(MINIMAL_BUILD) - set(FALCO_TESTS_SOURCES test_base.cpp engine/test_token_bucket.cpp engine/test_rulesets.cpp engine/test_falco_utils.cpp) + set( + FALCO_TESTS_SOURCES + test_base.cpp + engine/test_token_bucket.cpp + engine/test_rulesets.cpp + engine/test_falco_utils.cpp + falco/test_configuration.cpp + ) else() - set(FALCO_TESTS_SOURCES test_base.cpp engine/test_token_bucket.cpp engine/test_rulesets.cpp engine/test_falco_utils.cpp falco/test_webserver.cpp) + set( + FALCO_TESTS_SOURCES + test_base.cpp + engine/test_token_bucket.cpp + engine/test_rulesets.cpp + engine/test_falco_utils.cpp + falco/test_configuration.cpp + falco/test_webserver.cpp + ) endif() set(FALCO_TESTED_LIBRARIES falco_engine yaml-cpp) diff --git a/tests/falco/test_configuration.cpp b/tests/falco/test_configuration.cpp new file mode 100644 index 00000000..6c85718e --- /dev/null +++ b/tests/falco/test_configuration.cpp @@ -0,0 +1,106 @@ +/* +Copyright (C) 2021 The Falco Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +#include "configuration.h" +#include + +string sample_yaml = + "base_value:\n" + " id: 1\n" + " name: 'sample_name'\n" + " subvalue:\n" + " subvalue2:\n" + " boolean: true\n" + "base_value_2:\n" + " sample_list:\n" + " - elem1\n" + " - elem2\n" + " - elem3\n" +; + +TEST_CASE("configuration must load YAML data", "[configuration]") +{ + yaml_configuration conf; + + SECTION("broken YAML") + { + string sample_broken_yaml = sample_yaml + " / bad_symbol"; + REQUIRE_THROWS(conf.load_from_string(sample_broken_yaml)); + } + + SECTION("valid YAML") + { + REQUIRE_NOTHROW(conf.load_from_string(sample_yaml)); + } + + SECTION("clearing and reloading") + { + conf.load_from_string(sample_yaml); + REQUIRE(conf.is_defined("base_value") == true); + conf.clear(); + REQUIRE(conf.is_defined("base_value") == false); + conf.load_from_string(sample_yaml); + REQUIRE(conf.is_defined("base_value") == true); + } +} + +TEST_CASE("configuration must read YAML fields", "[configuration]") +{ + yaml_configuration conf; + conf.load_from_string(sample_yaml); + + SECTION("base level") + { + REQUIRE(conf.is_defined("base_value") == true); + REQUIRE(conf.is_defined("base_value_2") == true); + REQUIRE(conf.is_defined("unknown_base_value") == false); + } + + SECTION("arbitrary depth nesting") + { + REQUIRE(conf.get_scalar("base_value.id", -1) == 1); + REQUIRE(conf.get_scalar("base_value.name", "none") == "sample_name"); + REQUIRE(conf.get_scalar("base_value.subvalue.subvalue2.boolean", false) == true); + } + + SECTION("list field elements") + { + REQUIRE(conf.get_scalar("base_value_2.sample_list[0]", "none") == "elem1"); + REQUIRE(conf.get_scalar("base_value_2.sample_list[1]", "none") == "elem2"); + REQUIRE(conf.get_scalar("base_value_2.sample_list[2]", "none") == "elem3"); + } + + SECTION("sequence") + { + vector seq; + conf.get_sequence(seq, "base_value_2.sample_list"); + REQUIRE(seq.size() == 3); + REQUIRE(seq[0] == "elem1"); + REQUIRE(seq[1] == "elem2"); + REQUIRE(seq[2] == "elem3"); + } +} + +TEST_CASE("configuration must modify YAML fields", "[configuration]") +{ + string key = "base_value.subvalue.subvalue2.boolean"; + yaml_configuration conf; + conf.load_from_string(sample_yaml); + REQUIRE(conf.get_scalar(key, false) == true); + conf.set_scalar(key, false); + REQUIRE(conf.get_scalar(key, true) == false); + conf.set_scalar(key, true); + REQUIRE(conf.get_scalar(key, false) == true); +}