1 // Copyright 2015 The Kyua Authors. 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/atf_list.hpp" 30 31 #include <fstream> 32 #include <string> 33 #include <utility> 34 35 #include "engine/exceptions.hpp" 36 #include "model/metadata.hpp" 37 #include "model/test_case.hpp" 38 #include "utils/config/exceptions.hpp" 39 #include "utils/format/macros.hpp" 40 41 namespace config = utils::config; 42 namespace fs = utils::fs; 43 44 45 namespace { 46 47 48 /// Splits a property line of the form "name: word1 [... wordN]". 49 /// 50 /// \param line The line to parse. 51 /// 52 /// \return A (property_name, property_value) pair. 53 /// 54 /// \throw format_error If the value of line is invalid. 55 static std::pair< std::string, std::string > 56 split_prop_line(const std::string& line) 57 { 58 const std::string::size_type pos = line.find(": "); 59 if (pos == std::string::npos) 60 throw engine::format_error("Invalid property line; expecting line of " 61 "the form 'name: value'"); 62 return std::make_pair(line.substr(0, pos), line.substr(pos + 2)); 63 } 64 65 66 /// Parses a set of consecutive property lines. 67 /// 68 /// Processing stops when an empty line or the end of file is reached. None of 69 /// these conditions indicate errors. 70 /// 71 /// \param input The stream to read the lines from. 72 /// 73 /// \return The parsed property lines. 74 /// 75 /// throw format_error If the input stream has an invalid format. 76 static model::properties_map 77 parse_properties(std::istream& input) 78 { 79 model::properties_map properties; 80 81 std::string line; 82 while (std::getline(input, line).good() && !line.empty()) { 83 const std::pair< std::string, std::string > property = split_prop_line( 84 line); 85 if (properties.find(property.first) != properties.end()) 86 throw engine::format_error("Duplicate value for property " + 87 property.first); 88 properties.insert(property); 89 } 90 91 return properties; 92 } 93 94 95 } // anonymous namespace 96 97 98 /// Parses the metadata of an ATF test case. 99 /// 100 /// \param props The properties (name/value string pairs) as provided by the 101 /// ATF test program. 102 /// 103 /// \return A parsed metadata object. 104 /// 105 /// \throw engine::format_error If the syntax of any of the properties is 106 /// invalid. 107 model::metadata 108 engine::parse_atf_metadata(const model::properties_map& props) 109 { 110 model::metadata_builder mdbuilder; 111 112 try { 113 for (model::properties_map::const_iterator iter = props.begin(); 114 iter != props.end(); iter++) { 115 const std::string& name = (*iter).first; 116 const std::string& value = (*iter).second; 117 118 if (name == "descr") { 119 mdbuilder.set_string("description", value); 120 } else if (name == "has.cleanup") { 121 mdbuilder.set_string("has_cleanup", value); 122 } else if (name == "require.arch") { 123 mdbuilder.set_string("allowed_architectures", value); 124 } else if (name == "execenv") { 125 mdbuilder.set_string("execenv", value); 126 } else if (name == "execenv.jail.params") { 127 mdbuilder.set_string("execenv_jail_params", value); 128 } else if (name == "is.exclusive") { 129 mdbuilder.set_string("is_exclusive", value); 130 } else if (name == "require.config") { 131 mdbuilder.set_string("required_configs", value); 132 } else if (name == "require.files") { 133 mdbuilder.set_string("required_files", value); 134 } else if (name == "require.machine") { 135 mdbuilder.set_string("allowed_platforms", value); 136 } else if (name == "require.memory") { 137 mdbuilder.set_string("required_memory", value); 138 } else if (name == "require.progs") { 139 mdbuilder.set_string("required_programs", value); 140 } else if (name == "require.user") { 141 mdbuilder.set_string("required_user", value); 142 } else if (name == "timeout") { 143 mdbuilder.set_string("timeout", value); 144 } else if (name.length() > 2 && name.substr(0, 2) == "X-") { 145 mdbuilder.add_custom(name.substr(2), value); 146 } else { 147 throw engine::format_error(F("Unknown test case metadata " 148 "property '%s'") % name); 149 } 150 } 151 } catch (const config::error& e) { 152 throw engine::format_error(e.what()); 153 } 154 155 return mdbuilder.build(); 156 } 157 158 159 /// Parses the ATF list of test cases from an open stream. 160 /// 161 /// \param input The stream to read from. 162 /// 163 /// \return The collection of parsed test cases. 164 /// 165 /// \throw format_error If there is any problem in the input data. 166 model::test_cases_map 167 engine::parse_atf_list(std::istream& input) 168 { 169 std::string line; 170 171 std::getline(input, line); 172 if (line != "Content-Type: application/X-atf-tp; version=\"1\"" 173 || !input.good()) 174 throw format_error(F("Invalid header for test case list; expecting " 175 "Content-Type for application/X-atf-tp version 1, " 176 "got '%s'") % line); 177 178 std::getline(input, line); 179 if (!line.empty() || !input.good()) 180 throw format_error(F("Invalid header for test case list; expecting " 181 "a blank line, got '%s'") % line); 182 183 model::test_cases_map_builder test_cases_builder; 184 while (std::getline(input, line).good()) { 185 const std::pair< std::string, std::string > ident = split_prop_line( 186 line); 187 if (ident.first != "ident" or ident.second.empty()) 188 throw format_error("Invalid test case definition; must be " 189 "preceeded by the identifier"); 190 191 const model::properties_map props = parse_properties(input); 192 test_cases_builder.add(ident.second, parse_atf_metadata(props)); 193 } 194 const model::test_cases_map test_cases = test_cases_builder.build(); 195 if (test_cases.empty()) { 196 // The scheduler interface also checks for the presence of at least one 197 // test case. However, because the atf format itself requires one test 198 // case to be always present, we check for this condition here as well. 199 throw format_error("No test cases"); 200 } 201 return test_cases; 202 } 203