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 == "require.config") { 125 mdbuilder.set_string("required_configs", value); 126 } else if (name == "require.files") { 127 mdbuilder.set_string("required_files", value); 128 } else if (name == "require.machine") { 129 mdbuilder.set_string("allowed_platforms", value); 130 } else if (name == "require.memory") { 131 mdbuilder.set_string("required_memory", value); 132 } else if (name == "require.progs") { 133 mdbuilder.set_string("required_programs", value); 134 } else if (name == "require.user") { 135 mdbuilder.set_string("required_user", value); 136 } else if (name == "timeout") { 137 mdbuilder.set_string("timeout", value); 138 } else if (name.length() > 2 && name.substr(0, 2) == "X-") { 139 mdbuilder.add_custom(name.substr(2), value); 140 } else { 141 throw engine::format_error(F("Unknown test case metadata " 142 "property '%s'") % name); 143 } 144 } 145 } catch (const config::error& e) { 146 throw engine::format_error(e.what()); 147 } 148 149 return mdbuilder.build(); 150 } 151 152 153 /// Parses the ATF list of test cases from an open stream. 154 /// 155 /// \param input The stream to read from. 156 /// 157 /// \return The collection of parsed test cases. 158 /// 159 /// \throw format_error If there is any problem in the input data. 160 model::test_cases_map 161 engine::parse_atf_list(std::istream& input) 162 { 163 std::string line; 164 165 std::getline(input, line); 166 if (line != "Content-Type: application/X-atf-tp; version=\"1\"" 167 || !input.good()) 168 throw format_error(F("Invalid header for test case list; expecting " 169 "Content-Type for application/X-atf-tp version 1, " 170 "got '%s'") % line); 171 172 std::getline(input, line); 173 if (!line.empty() || !input.good()) 174 throw format_error(F("Invalid header for test case list; expecting " 175 "a blank line, got '%s'") % line); 176 177 model::test_cases_map_builder test_cases_builder; 178 while (std::getline(input, line).good()) { 179 const std::pair< std::string, std::string > ident = split_prop_line( 180 line); 181 if (ident.first != "ident" or ident.second.empty()) 182 throw format_error("Invalid test case definition; must be " 183 "preceeded by the identifier"); 184 185 const model::properties_map props = parse_properties(input); 186 test_cases_builder.add(ident.second, parse_atf_metadata(props)); 187 } 188 const model::test_cases_map test_cases = test_cases_builder.build(); 189 if (test_cases.empty()) { 190 // The scheduler interface also checks for the presence of at least one 191 // test case. However, because the atf format itself requires one test 192 // case to be always present, we check for this condition here as well. 193 throw format_error("No test cases"); 194 } 195 return test_cases; 196 } 197