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