// Copyright 2011 Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Google Inc. nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/// Creates a configuration file for testing purposes.
///
/// To ensure that the loaded file is the one created by this function, use
/// validate_mock_config().
///
/// \param name The name of the configuration file to create.
/// \param cookie The magic value to set in the configuration file, or NULL if a
/// broken configuration file is desired.
static void
create_mock_config(const char* name, const char* cookie)
{
if (cookie != NULL) {
atf::utils::create_file(
name,
F("syntax(2)\n"
"test_suites.suite.magic_value = '%s'\n") % cookie);
} else {
atf::utils::create_file(name, "syntax(200)\n");
}
}
/// Creates an invalid system configuration.
///
/// \param cookie The magic value to set in the configuration file, or NULL if a
/// broken configuration file is desired.
static void
mock_system_config(const char* cookie)
{
fs::mkdir(fs::path("system-dir"), 0755);
utils::setenv("KYUA_CONFDIR", (fs::current_path() / "system-dir").str());
create_mock_config("system-dir/kyua.conf", cookie);
}
/// Creates an invalid user configuration.
///
/// \param cookie The magic value to set in the configuration file, or NULL if a
/// broken configuration file is desired.
static void
mock_user_config(const char* cookie)
{
fs::mkdir(fs::path("user-dir"), 0755);
fs::mkdir(fs::path("user-dir/.kyua"), 0755);
utils::setenv("HOME", (fs::current_path() / "user-dir").str());
create_mock_config("user-dir/.kyua/kyua.conf", cookie);
}
/// Ensures that a loaded configuration was created with create_mock_config().
///
/// \param user_config The configuration to validate.
/// \param cookie The magic value to expect in the configuration file.
static void
validate_mock_config(const config::tree& user_config, const char* cookie)
{
const config::properties_map& properties = user_config.all_properties(
"test_suites.suite", true);
const config::properties_map::const_iterator iter =
properties.find("magic_value");
ATF_REQUIRE(iter != properties.end());
ATF_REQUIRE_EQ(cookie, (*iter).second);
}
/// Ensures that two configuration trees are equal.
///
/// \param exp_tree The expected configuration tree.
/// \param actual_tree The configuration tree being validated against exp_tree.
static void
require_eq(const config::tree& exp_tree, const config::tree& actual_tree)
{
ATF_REQUIRE(exp_tree.all_properties() == actual_tree.all_properties());
}
ATF_TEST_CASE_WITHOUT_HEAD(load_config__user__ok);
ATF_TEST_CASE_BODY(load_config__user__ok)
{
mock_system_config(NULL);
mock_user_config("I am the user config");
ATF_TEST_CASE_WITHOUT_HEAD(load_config__system__ok);
ATF_TEST_CASE_BODY(load_config__system__ok)
{
mock_system_config("I am the system config");
utils::setenv("HOME", "/the/user/does/not/exist");