1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 
26 #include <algorithm>
27 #include <cctype>
28 #include <charconv>
29 #include <regex>
30 #include <string>
31 #include <string_view>
32 #include <unordered_map>
33 #include <vector>
34 
35 #include <gtest/gtest.h>
36 
37 #include "Options.h"
38 
39 namespace android {
40 namespace gtest_extras {
41 
42 // The total time each test can run before timing out and being killed.
43 constexpr uint64_t kDefaultDeadlineThresholdMs = 90000;
44 
45 // The total time each test can run before a warning is issued.
46 constexpr uint64_t kDefaultSlowThresholdMs = 2000;
47 
48 const std::unordered_map<std::string, Options::ArgInfo> Options::kArgs = {
49     {"deadline_threshold_ms", {FLAG_REQUIRES_VALUE, &Options::SetNumeric}},
50     {"slow_threshold_ms", {FLAG_REQUIRES_VALUE, &Options::SetNumeric}},
51     {"gtest_list_tests", {FLAG_NONE, &Options::SetBool}},
52     {"gtest_filter", {FLAG_ENVIRONMENT_VARIABLE | FLAG_REQUIRES_VALUE, &Options::SetString}},
53     {"gtest_flagfile", {FLAG_REQUIRES_VALUE, &Options::SetString}},
54     {
55         "gtest_repeat",
56         {FLAG_ENVIRONMENT_VARIABLE | FLAG_REQUIRES_VALUE, &Options::SetIterations},
57     },
58     {"gtest_output", {FLAG_ENVIRONMENT_VARIABLE | FLAG_REQUIRES_VALUE, &Options::SetXmlFile}},
59     {"gtest_print_time", {FLAG_ENVIRONMENT_VARIABLE | FLAG_OPTIONAL_VALUE, &Options::SetPrintTime}},
60     {
61         "gtest_also_run_disabled_tests",
62         {FLAG_ENVIRONMENT_VARIABLE | FLAG_CHILD, &Options::SetBool},
63     },
64     {"gtest_color",
65      {FLAG_ENVIRONMENT_VARIABLE | FLAG_REQUIRES_VALUE | FLAG_CHILD, &Options::SetString}},
66     {"gtest_death_test_style",
67      {FLAG_ENVIRONMENT_VARIABLE | FLAG_REQUIRES_VALUE | FLAG_CHILD, nullptr}},
68     {"gtest_break_on_failure", {FLAG_ENVIRONMENT_VARIABLE, &Options::SetBool}},
69     {"gtest_catch_exceptions", {FLAG_ENVIRONMENT_VARIABLE | FLAG_INCOMPATIBLE, nullptr}},
70     {"gtest_random_seed", {FLAG_ENVIRONMENT_VARIABLE | FLAG_INCOMPATIBLE, nullptr}},
71     {"gtest_shuffle", {FLAG_ENVIRONMENT_VARIABLE | FLAG_INCOMPATIBLE, nullptr}},
72     {"gtest_stream_result_to", {FLAG_ENVIRONMENT_VARIABLE | FLAG_INCOMPATIBLE, nullptr}},
73     {"gtest_throw_on_failure", {FLAG_ENVIRONMENT_VARIABLE, &Options::SetBool}},
74     {"gtest_shard_index",
75      {FLAG_ENVIRONMENT_VARIABLE | FLAG_REQUIRES_VALUE, &Options::SetNumericEnvOnly}},
76     {"gtest_total_shards",
77      {FLAG_ENVIRONMENT_VARIABLE | FLAG_REQUIRES_VALUE, &Options::SetNumericEnvOnly}},
78     // This does nothing, only added so that passing this option does not exit.
79     {"gtest_format", {FLAG_NONE, &Options::SetBool}},
80 };
81 
GetError(const std::string & arg,std::string_view msg,bool from_env)82 static std::string GetError(const std::string& arg, std::string_view msg, bool from_env) {
83   std::string error;
84   if (from_env) {
85     std::string variable(arg);
86     std::transform(variable.begin(), variable.end(), variable.begin(),
87                    [](char c) { return std::toupper(c); });
88     error = "env[" + variable + "] ";
89   } else if (arg[0] == '-') {
90     error = arg + " ";
91   } else {
92     error = "--" + arg + " ";
93   }
94   return error + std::string(msg);
95 }
96 
97 template <typename IntType>
GetNumeric(const std::string & arg,const std::string & value,IntType * numeric_value,bool from_env,std::string & error)98 static bool GetNumeric(const std::string& arg, const std::string& value, IntType* numeric_value,
99                        bool from_env, std::string& error) {
100   auto result = std::from_chars(value.c_str(), value.c_str() + value.size(), *numeric_value, 10);
101   if (result.ec == std::errc::result_out_of_range) {
102     error = GetError(arg, std::string("value overflows (") + value + ")", from_env);
103     return false;
104   } else if (result.ec == std::errc::invalid_argument || result.ptr == nullptr ||
105              *result.ptr != '\0') {
106     error = GetError(arg, std::string("value is not formatted as a numeric value (") + value + ")",
107                      from_env);
108     return false;
109   }
110   return true;
111 }
112 
SetPrintTime(const std::string &,const std::string & value,bool)113 bool Options::SetPrintTime(const std::string&, const std::string& value, bool) {
114   if (!value.empty() && strtol(value.c_str(), nullptr, 10) == 0) {
115     bools_.find("gtest_print_time")->second = false;
116   }
117   return true;
118 }
119 
SetNumeric(const std::string & arg,const std::string & value,bool from_env)120 bool Options::SetNumeric(const std::string& arg, const std::string& value, bool from_env) {
121   uint64_t* numeric = &numerics_.find(arg)->second;
122   if (!GetNumeric<uint64_t>(arg, value, numeric, from_env, error_)) {
123     return false;
124   }
125   if (*numeric == 0) {
126     error_ = GetError(arg, "requires a number greater than zero.", from_env);
127     return false;
128   }
129   return true;
130 }
131 
SetNumericEnvOnly(const std::string & arg,const std::string & value,bool from_env)132 bool Options::SetNumericEnvOnly(const std::string& arg, const std::string& value, bool from_env) {
133   if (!from_env) {
134     error_ = GetError(arg, "is only supported as an environment variable.", false);
135     return false;
136   }
137   uint64_t* numeric = &numerics_.find(arg)->second;
138   if (!GetNumeric<uint64_t>(arg, value, numeric, from_env, error_)) {
139     return false;
140   }
141   return true;
142 }
143 
SetBool(const std::string & arg,const std::string &,bool)144 bool Options::SetBool(const std::string& arg, const std::string&, bool) {
145   bools_.find(arg)->second = true;
146   return true;
147 }
148 
SetIterations(const std::string & arg,const std::string & value,bool from_env)149 bool Options::SetIterations(const std::string& arg, const std::string& value, bool from_env) {
150   if (!GetNumeric<int>(arg, value, &num_iterations_, from_env, error_)) {
151     return false;
152   }
153   return true;
154 }
155 
SetString(const std::string & arg,const std::string & value,bool)156 bool Options::SetString(const std::string& arg, const std::string& value, bool) {
157   strings_.find(arg)->second = value;
158   return true;
159 }
160 
SetXmlFile(const std::string & arg,const std::string & value,bool from_env)161 bool Options::SetXmlFile(const std::string& arg, const std::string& value, bool from_env) {
162   if (value.substr(0, 4) != "xml:") {
163     error_ = GetError(arg, "only supports an xml output file.", from_env);
164     return false;
165   }
166   std::string xml_file(value.substr(4));
167   if (xml_file.empty()) {
168     error_ = GetError(arg, "requires a file name after xml:", from_env);
169     return false;
170   }
171   // Need an absolute file.
172   if (xml_file[0] != '/') {
173     char* cwd = getcwd(nullptr, 0);
174     if (cwd == nullptr) {
175       error_ = GetError(arg,
176                         std::string("cannot get absolute pathname, getcwd() is failing: ") +
177                             strerror(errno) + '\n',
178                         from_env);
179       return false;
180     }
181     xml_file = std::string(cwd) + '/' + xml_file;
182     free(cwd);
183   }
184 
185   // If the output file is a directory, add the name of a file.
186   if (xml_file.back() == '/') {
187     xml_file += "test_details.xml";
188   }
189   strings_.find("xml_file")->second = xml_file;
190   return true;
191 }
192 
HandleArg(const std::string & arg,const std::string & value,const ArgInfo & info,bool from_env)193 bool Options::HandleArg(const std::string& arg, const std::string& value, const ArgInfo& info,
194                         bool from_env) {
195   if (info.flags & FLAG_INCOMPATIBLE) {
196     error_ = GetError(arg, "is not compatible with isolation runs.", from_env);
197     return false;
198   }
199 
200   if (info.flags & FLAG_TAKES_VALUE) {
201     if ((info.flags & FLAG_REQUIRES_VALUE) && value.empty()) {
202       error_ = GetError(arg, "requires an argument.", from_env);
203       return false;
204     }
205 
206     if (info.func != nullptr && !(this->*(info.func))(arg, value, from_env)) {
207       return false;
208     }
209   } else if (!value.empty()) {
210     error_ = GetError(arg, "does not take an argument.", from_env);
211     return false;
212   } else if (info.func != nullptr) {
213     return (this->*(info.func))(arg, value, from_env);
214   }
215   return true;
216 }
217 
ReadFileToString(const std::string & file,std::string * contents)218 static bool ReadFileToString(const std::string& file, std::string* contents) {
219   int fd = static_cast<int>(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
220   if (fd == -1) {
221     return false;
222   }
223   char buf[4096];
224   ssize_t bytes_read;
225   while ((bytes_read = TEMP_FAILURE_RETRY(read(fd, &buf, sizeof(buf)))) > 0) {
226     contents->append(buf, static_cast<size_t>(bytes_read));
227   }
228   close(fd);
229   return true;
230 }
231 
ProcessFlagfile(const std::string & file,std::vector<char * > * child_args)232 bool Options::ProcessFlagfile(const std::string& file, std::vector<char*>* child_args) {
233   std::string contents;
234   if (!ReadFileToString(file, &contents)) {
235     error_ = "Unable to read data from file " + file;
236     return false;
237   }
238 
239   std::regex flag_regex("^\\s*(\\S.*\\S)\\s*$");
240   std::regex empty_line_regex("^\\s*$");
241   size_t idx = 0;
242   while (idx < contents.size()) {
243     size_t newline_idx = contents.find('\n', idx);
244     if (newline_idx == std::string::npos) {
245       newline_idx = contents.size();
246     }
247     std::string line(&contents[idx], newline_idx - idx);
248     idx = newline_idx + 1;
249     std::smatch match;
250     if (std::regex_match(line, match, flag_regex)) {
251       line = match[1];
252     } else if (std::regex_match(line, match, empty_line_regex)) {
253       // Skip lines with only whitespace.
254       continue;
255     }
256     if (!ProcessSingle(line.c_str(), child_args, false)) {
257       return false;
258     }
259   }
260   return true;
261 }
262 
ProcessSingle(const char * arg,std::vector<char * > * child_args,bool allow_flagfile)263 bool Options::ProcessSingle(const char* arg, std::vector<char*>* child_args, bool allow_flagfile) {
264   if (strncmp("--", arg, 2) != 0) {
265     if (arg[0] == '-') {
266       error_ = std::string("Unknown argument: ") + arg;
267       return false;
268     } else {
269       error_ = std::string("Unexpected argument '") + arg + "'";
270       return false;
271     }
272   }
273 
274   // See if this is a name=value argument.
275   std::string name;
276   std::string value;
277   const char* equal = strchr(arg, '=');
278   if (equal != nullptr) {
279     name = std::string(&arg[2], static_cast<size_t>(equal - arg) - 2);
280     value = equal + 1;
281   } else {
282     name = &arg[2];
283   }
284   auto entry = kArgs.find(name);
285   if (entry == kArgs.end()) {
286     error_ = std::string("Unknown argument: ") + arg;
287     return false;
288   }
289 
290   if (entry->second.flags & FLAG_CHILD) {
291     child_args->push_back(strdup(arg));
292   }
293 
294   if (!HandleArg(name, value, entry->second)) {
295     return false;
296   }
297 
298   // Special case, if gtest_flagfile is set, then we need to read the
299   // file and treat each line as a flag.
300   if (name == "gtest_flagfile") {
301     if (!allow_flagfile) {
302       error_ = std::string("Argument: ") + arg + " is not allowed in flag file.";
303       return false;
304     }
305     if (!ProcessFlagfile(value, child_args)) {
306       return false;
307     }
308   }
309 
310   return true;
311 }
312 
Process(const std::vector<const char * > & args,std::vector<char * > * child_args)313 bool Options::Process(const std::vector<const char*>& args, std::vector<char*>* child_args) {
314   // Initialize the variables.
315   job_count_ = static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN));
316   num_iterations_ = ::testing::GTEST_FLAG(repeat);
317   stop_on_error_ = false;
318   numerics_.clear();
319   numerics_["deadline_threshold_ms"] = kDefaultDeadlineThresholdMs;
320   numerics_["slow_threshold_ms"] = kDefaultSlowThresholdMs;
321   numerics_["gtest_shard_index"] = 0;
322   numerics_["gtest_total_shards"] = 0;
323   strings_.clear();
324   strings_["gtest_color"] = ::testing::GTEST_FLAG(color);
325   strings_["xml_file"] = ::testing::GTEST_FLAG(output);
326   strings_["gtest_filter"] = "";
327   strings_["gtest_flagfile"] = "";
328   bools_.clear();
329   bools_["gtest_print_time"] = ::testing::GTEST_FLAG(print_time);
330   bools_["gtest_also_run_disabled_tests"] = ::testing::GTEST_FLAG(also_run_disabled_tests);
331   bools_["gtest_list_tests"] = false;
332   bools_["gtest_break_on_failure"] = false;
333   bools_["gtest_throw_on_failure"] = false;
334 
335   // This does nothing, only added so that passing this option does not exit.
336   bools_["gtest_format"] = true;
337 
338   // Loop through all of the possible environment variables.
339   for (const auto& entry : kArgs) {
340     if (entry.second.flags & FLAG_ENVIRONMENT_VARIABLE) {
341       std::string variable(entry.first);
342       std::transform(variable.begin(), variable.end(), variable.begin(),
343                      [](char c) { return std::toupper(c); });
344       char* env = getenv(variable.c_str());
345       if (env == nullptr) {
346         continue;
347       }
348       std::string value(env);
349       if (!HandleArg(entry.first, value, entry.second, true)) {
350         return false;
351       }
352     }
353   }
354 
355   child_args->push_back(strdup(args[0]));
356 
357   // Assumes the first value is not an argument, so skip it.
358   for (size_t i = 1; i < args.size(); i++) {
359     // Special handle of -j or -jXX. This flag is not allowed to be present
360     // in a --gtest_flagfile.
361     if (strncmp(args[i], "-j", 2) == 0) {
362       const char* value = &args[i][2];
363       if (*value == '\0') {
364         // Get the next argument.
365         if (i == args.size() - 1) {
366           error_ = "-j requires an argument.";
367           return false;
368         }
369         i++;
370         value = args[i];
371       }
372       if (!GetNumeric<size_t>("-j", value, &job_count_, false, error_)) {
373         return false;
374       }
375     } else {
376       if (!ProcessSingle(args[i], child_args, true)) {
377         return false;
378       }
379     }
380   }
381 
382   if (bools_["gtest_break_on_failure"] || bools_["gtest_throw_on_failure"]) {
383     stop_on_error_ = true;
384   }
385 
386   return true;
387 }
388 
389 }  // namespace gtest_extras
390 }  // namespace android
391