1// Copyright 2010 Google Inc. 2// All rights reserved. 3// 4// Redistribution and use in source and binary forms, with or without 5// modification, are permitted provided that the following conditions are 6// met: 7// 8// * Redistributions of source code must retain the above copyright 9// notice, this list of conditions and the following disclaimer. 10// * Redistributions in binary form must reproduce the above copyright 11// notice, this list of conditions and the following disclaimer in the 12// documentation and/or other materials provided with the distribution. 13// * Neither the name of Google Inc. nor the names of its contributors 14// may be used to endorse or promote products derived from this software 15// without specific prior written permission. 16// 17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29#include "engine/config.hpp" 30 31#if defined(HAVE_CONFIG_H) 32# include "config.h" 33#endif 34 35#include <stdexcept> 36 37#include "engine/exceptions.hpp" 38#include "utils/config/exceptions.hpp" 39#include "utils/config/parser.hpp" 40#include "utils/config/tree.ipp" 41#include "utils/passwd.hpp" 42#include "utils/text/exceptions.hpp" 43#include "utils/text/operations.ipp" 44 45namespace config = utils::config; 46namespace fs = utils::fs; 47namespace passwd = utils::passwd; 48namespace text = utils::text; 49 50 51namespace { 52 53 54/// Defines the schema of a configuration tree. 55/// 56/// \param [in,out] tree The tree to populate. The tree should be empty on 57/// entry to prevent collisions with the keys defined in here. 58static void 59init_tree(config::tree& tree) 60{ 61 tree.define< config::string_node >("architecture"); 62 tree.define< config::string_node >("platform"); 63 tree.define< engine::user_node >("unprivileged_user"); 64 tree.define_dynamic("test_suites"); 65} 66 67 68/// Fills in a configuration tree with default values. 69/// 70/// \param [in,out] tree The tree to populate. init_tree() must have been 71/// called on it beforehand. 72static void 73set_defaults(config::tree& tree) 74{ 75 tree.set< config::string_node >("architecture", KYUA_ARCHITECTURE); 76 tree.set< config::string_node >("platform", KYUA_PLATFORM); 77} 78 79 80/// Configuration parser specialization for Kyua configuration files. 81class config_parser : public config::parser { 82 /// Initializes the configuration tree. 83 /// 84 /// This is a callback executed when the configuration script invokes the 85 /// syntax() method. We populate the configuration tree from here with the 86 /// schema version requested by the file. 87 /// 88 /// \param [in,out] tree The tree to populate. 89 /// \param syntax_version The version of the file format as specified in the 90 /// configuration file. 91 /// 92 /// \throw config::syntax_error If the syntax_format/syntax_version 93 /// combination is not supported. 94 void 95 setup(config::tree& tree, const int syntax_version) 96 { 97 if (syntax_version < 1 || syntax_version > 2) 98 throw config::syntax_error(F("Unsupported config version %s") % 99 syntax_version); 100 101 init_tree(tree); 102 set_defaults(tree); 103 } 104 105public: 106 /// Initializes the parser. 107 /// 108 /// \param [out] tree_ The tree in which the results of the parsing will be 109 /// stored when parse() is called. Should be empty on entry. Because 110 /// we grab a reference to this object, the tree must remain valid for 111 /// the existence of the parser object. 112 explicit config_parser(config::tree& tree_) : 113 config::parser(tree_) 114 { 115 } 116}; 117 118 119} // anonymous namespace 120 121 122/// Copies the node. 123/// 124/// \return A dynamically-allocated node. 125config::detail::base_node* 126engine::user_node::deep_copy(void) const 127{ 128 std::unique_ptr< user_node > new_node(new user_node()); 129 new_node->_value = _value; 130 return new_node.release(); 131} 132 133 134/// Pushes the node's value onto the Lua stack. 135/// 136/// \param state The Lua state onto which to push the value. 137void 138engine::user_node::push_lua(lutok::state& state) const 139{ 140 state.push_string(value().name); 141} 142 143 144/// Sets the value of the node from an entry in the Lua stack. 145/// 146/// \param state The Lua state from which to get the value. 147/// \param value_index The stack index in which the value resides. 148/// 149/// \throw value_error If the value in state(value_index) cannot be 150/// processed by this node. 151void 152engine::user_node::set_lua(lutok::state& state, const int value_index) 153{ 154 if (state.is_number(value_index)) { 155 config::typed_leaf_node< passwd::user >::set( 156 passwd::find_user_by_uid(state.to_integer(-1))); 157 } else if (state.is_string(value_index)) { 158 config::typed_leaf_node< passwd::user >::set( 159 passwd::find_user_by_name(state.to_string(-1))); 160 } else 161 throw config::value_error("Invalid user identifier"); 162} 163 164 165void 166engine::user_node::set_string(const std::string& raw_value) 167{ 168 try { 169 config::typed_leaf_node< passwd::user >::set( 170 passwd::find_user_by_name(raw_value)); 171 } catch (const std::runtime_error& e) { 172 int uid; 173 try { 174 uid = text::to_type< int >(raw_value); 175 } catch (const text::value_error& e2) { 176 throw error(F("Cannot find user with name '%s'") % raw_value); 177 } 178 179 try { 180 config::typed_leaf_node< passwd::user >::set( 181 passwd::find_user_by_uid(uid)); 182 } catch (const std::runtime_error& e2) { 183 throw error(F("Cannot find user with UID %s") % uid); 184 } 185 } 186} 187 188 189std::string 190engine::user_node::to_string(void) const 191{ 192 return config::typed_leaf_node< passwd::user >::value().name; 193} 194 195 196/// Constructs a config with the built-in settings. 197config::tree 198engine::default_config(void) 199{ 200 config::tree tree; 201 init_tree(tree); 202 set_defaults(tree); 203 return tree; 204} 205 206 207/// Constructs a config with the built-in settings. 208config::tree 209engine::empty_config(void) 210{ 211 config::tree tree; 212 init_tree(tree); 213 return tree; 214} 215 216 217/// Parses a test suite configuration file. 218/// 219/// \param file The file to parse. 220/// 221/// \return High-level representation of the configuration file. 222/// 223/// \throw load_error If there is any problem loading the file. This includes 224/// file access errors and syntax errors. 225config::tree 226engine::load_config(const utils::fs::path& file) 227{ 228 config::tree tree; 229 try { 230 config_parser(tree).parse(file); 231 } catch (const config::error& e) { 232 throw load_error(file, e.what()); 233 } 234 return tree; 235} 236