Wed, 31 Dec 2014 07:53:36 +0100
Correct small whitespace inconsistency, lost while renaming variables.
michael@0 | 1 | // Copyright 2008, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | // |
michael@0 | 30 | // Author: wan@google.com (Zhanyong Wan) |
michael@0 | 31 | |
michael@0 | 32 | #include "gtest/internal/gtest-port.h" |
michael@0 | 33 | |
michael@0 | 34 | #include <limits.h> |
michael@0 | 35 | #include <stdlib.h> |
michael@0 | 36 | #include <stdio.h> |
michael@0 | 37 | #include <string.h> |
michael@0 | 38 | |
michael@0 | 39 | #if GTEST_OS_WINDOWS_MOBILE |
michael@0 | 40 | # include <windows.h> // For TerminateProcess() |
michael@0 | 41 | #elif GTEST_OS_WINDOWS |
michael@0 | 42 | # include <io.h> |
michael@0 | 43 | # include <sys/stat.h> |
michael@0 | 44 | #else |
michael@0 | 45 | # include <unistd.h> |
michael@0 | 46 | #endif // GTEST_OS_WINDOWS_MOBILE |
michael@0 | 47 | |
michael@0 | 48 | #if GTEST_OS_MAC |
michael@0 | 49 | # include <mach/mach_init.h> |
michael@0 | 50 | # include <mach/task.h> |
michael@0 | 51 | # include <mach/vm_map.h> |
michael@0 | 52 | #endif // GTEST_OS_MAC |
michael@0 | 53 | |
michael@0 | 54 | #if GTEST_OS_QNX |
michael@0 | 55 | # include <devctl.h> |
michael@0 | 56 | # include <sys/procfs.h> |
michael@0 | 57 | #endif // GTEST_OS_QNX |
michael@0 | 58 | |
michael@0 | 59 | #include "gtest/gtest-spi.h" |
michael@0 | 60 | #include "gtest/gtest-message.h" |
michael@0 | 61 | #include "gtest/internal/gtest-internal.h" |
michael@0 | 62 | #include "gtest/internal/gtest-string.h" |
michael@0 | 63 | |
michael@0 | 64 | // Indicates that this translation unit is part of Google Test's |
michael@0 | 65 | // implementation. It must come before gtest-internal-inl.h is |
michael@0 | 66 | // included, or there will be a compiler error. This trick is to |
michael@0 | 67 | // prevent a user from accidentally including gtest-internal-inl.h in |
michael@0 | 68 | // his code. |
michael@0 | 69 | #define GTEST_IMPLEMENTATION_ 1 |
michael@0 | 70 | #include "src/gtest-internal-inl.h" |
michael@0 | 71 | #undef GTEST_IMPLEMENTATION_ |
michael@0 | 72 | |
michael@0 | 73 | namespace testing { |
michael@0 | 74 | namespace internal { |
michael@0 | 75 | |
michael@0 | 76 | #if defined(_MSC_VER) || defined(__BORLANDC__) |
michael@0 | 77 | // MSVC and C++Builder do not provide a definition of STDERR_FILENO. |
michael@0 | 78 | const int kStdOutFileno = 1; |
michael@0 | 79 | const int kStdErrFileno = 2; |
michael@0 | 80 | #else |
michael@0 | 81 | const int kStdOutFileno = STDOUT_FILENO; |
michael@0 | 82 | const int kStdErrFileno = STDERR_FILENO; |
michael@0 | 83 | #endif // _MSC_VER |
michael@0 | 84 | |
michael@0 | 85 | #if GTEST_OS_MAC |
michael@0 | 86 | |
michael@0 | 87 | // Returns the number of threads running in the process, or 0 to indicate that |
michael@0 | 88 | // we cannot detect it. |
michael@0 | 89 | size_t GetThreadCount() { |
michael@0 | 90 | const task_t task = mach_task_self(); |
michael@0 | 91 | mach_msg_type_number_t thread_count; |
michael@0 | 92 | thread_act_array_t thread_list; |
michael@0 | 93 | const kern_return_t status = task_threads(task, &thread_list, &thread_count); |
michael@0 | 94 | if (status == KERN_SUCCESS) { |
michael@0 | 95 | // task_threads allocates resources in thread_list and we need to free them |
michael@0 | 96 | // to avoid leaks. |
michael@0 | 97 | vm_deallocate(task, |
michael@0 | 98 | reinterpret_cast<vm_address_t>(thread_list), |
michael@0 | 99 | sizeof(thread_t) * thread_count); |
michael@0 | 100 | return static_cast<size_t>(thread_count); |
michael@0 | 101 | } else { |
michael@0 | 102 | return 0; |
michael@0 | 103 | } |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | #elif GTEST_OS_QNX |
michael@0 | 107 | |
michael@0 | 108 | // Returns the number of threads running in the process, or 0 to indicate that |
michael@0 | 109 | // we cannot detect it. |
michael@0 | 110 | size_t GetThreadCount() { |
michael@0 | 111 | const int fd = open("/proc/self/as", O_RDONLY); |
michael@0 | 112 | if (fd < 0) { |
michael@0 | 113 | return 0; |
michael@0 | 114 | } |
michael@0 | 115 | procfs_info process_info; |
michael@0 | 116 | const int status = |
michael@0 | 117 | devctl(fd, DCMD_PROC_INFO, &process_info, sizeof(process_info), NULL); |
michael@0 | 118 | close(fd); |
michael@0 | 119 | if (status == EOK) { |
michael@0 | 120 | return static_cast<size_t>(process_info.num_threads); |
michael@0 | 121 | } else { |
michael@0 | 122 | return 0; |
michael@0 | 123 | } |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | #else |
michael@0 | 127 | |
michael@0 | 128 | size_t GetThreadCount() { |
michael@0 | 129 | // There's no portable way to detect the number of threads, so we just |
michael@0 | 130 | // return 0 to indicate that we cannot detect it. |
michael@0 | 131 | return 0; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | #endif // GTEST_OS_MAC |
michael@0 | 135 | |
michael@0 | 136 | #if GTEST_USES_POSIX_RE |
michael@0 | 137 | |
michael@0 | 138 | // Implements RE. Currently only needed for death tests. |
michael@0 | 139 | |
michael@0 | 140 | RE::~RE() { |
michael@0 | 141 | if (is_valid_) { |
michael@0 | 142 | // regfree'ing an invalid regex might crash because the content |
michael@0 | 143 | // of the regex is undefined. Since the regex's are essentially |
michael@0 | 144 | // the same, one cannot be valid (or invalid) without the other |
michael@0 | 145 | // being so too. |
michael@0 | 146 | regfree(&partial_regex_); |
michael@0 | 147 | regfree(&full_regex_); |
michael@0 | 148 | } |
michael@0 | 149 | free(const_cast<char*>(pattern_)); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | // Returns true iff regular expression re matches the entire str. |
michael@0 | 153 | bool RE::FullMatch(const char* str, const RE& re) { |
michael@0 | 154 | if (!re.is_valid_) return false; |
michael@0 | 155 | |
michael@0 | 156 | regmatch_t match; |
michael@0 | 157 | return regexec(&re.full_regex_, str, 1, &match, 0) == 0; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | // Returns true iff regular expression re matches a substring of str |
michael@0 | 161 | // (including str itself). |
michael@0 | 162 | bool RE::PartialMatch(const char* str, const RE& re) { |
michael@0 | 163 | if (!re.is_valid_) return false; |
michael@0 | 164 | |
michael@0 | 165 | regmatch_t match; |
michael@0 | 166 | return regexec(&re.partial_regex_, str, 1, &match, 0) == 0; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | // Initializes an RE from its string representation. |
michael@0 | 170 | void RE::Init(const char* regex) { |
michael@0 | 171 | pattern_ = posix::StrDup(regex); |
michael@0 | 172 | |
michael@0 | 173 | // Reserves enough bytes to hold the regular expression used for a |
michael@0 | 174 | // full match. |
michael@0 | 175 | const size_t full_regex_len = strlen(regex) + 10; |
michael@0 | 176 | char* const full_pattern = new char[full_regex_len]; |
michael@0 | 177 | |
michael@0 | 178 | snprintf(full_pattern, full_regex_len, "^(%s)$", regex); |
michael@0 | 179 | is_valid_ = regcomp(&full_regex_, full_pattern, REG_EXTENDED) == 0; |
michael@0 | 180 | // We want to call regcomp(&partial_regex_, ...) even if the |
michael@0 | 181 | // previous expression returns false. Otherwise partial_regex_ may |
michael@0 | 182 | // not be properly initialized can may cause trouble when it's |
michael@0 | 183 | // freed. |
michael@0 | 184 | // |
michael@0 | 185 | // Some implementation of POSIX regex (e.g. on at least some |
michael@0 | 186 | // versions of Cygwin) doesn't accept the empty string as a valid |
michael@0 | 187 | // regex. We change it to an equivalent form "()" to be safe. |
michael@0 | 188 | if (is_valid_) { |
michael@0 | 189 | const char* const partial_regex = (*regex == '\0') ? "()" : regex; |
michael@0 | 190 | is_valid_ = regcomp(&partial_regex_, partial_regex, REG_EXTENDED) == 0; |
michael@0 | 191 | } |
michael@0 | 192 | EXPECT_TRUE(is_valid_) |
michael@0 | 193 | << "Regular expression \"" << regex |
michael@0 | 194 | << "\" is not a valid POSIX Extended regular expression."; |
michael@0 | 195 | |
michael@0 | 196 | delete[] full_pattern; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | #elif GTEST_USES_SIMPLE_RE |
michael@0 | 200 | |
michael@0 | 201 | // Returns true iff ch appears anywhere in str (excluding the |
michael@0 | 202 | // terminating '\0' character). |
michael@0 | 203 | bool IsInSet(char ch, const char* str) { |
michael@0 | 204 | return ch != '\0' && strchr(str, ch) != NULL; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | // Returns true iff ch belongs to the given classification. Unlike |
michael@0 | 208 | // similar functions in <ctype.h>, these aren't affected by the |
michael@0 | 209 | // current locale. |
michael@0 | 210 | bool IsAsciiDigit(char ch) { return '0' <= ch && ch <= '9'; } |
michael@0 | 211 | bool IsAsciiPunct(char ch) { |
michael@0 | 212 | return IsInSet(ch, "^-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~"); |
michael@0 | 213 | } |
michael@0 | 214 | bool IsRepeat(char ch) { return IsInSet(ch, "?*+"); } |
michael@0 | 215 | bool IsAsciiWhiteSpace(char ch) { return IsInSet(ch, " \f\n\r\t\v"); } |
michael@0 | 216 | bool IsAsciiWordChar(char ch) { |
michael@0 | 217 | return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || |
michael@0 | 218 | ('0' <= ch && ch <= '9') || ch == '_'; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | // Returns true iff "\\c" is a supported escape sequence. |
michael@0 | 222 | bool IsValidEscape(char c) { |
michael@0 | 223 | return (IsAsciiPunct(c) || IsInSet(c, "dDfnrsStvwW")); |
michael@0 | 224 | } |
michael@0 | 225 | |
michael@0 | 226 | // Returns true iff the given atom (specified by escaped and pattern) |
michael@0 | 227 | // matches ch. The result is undefined if the atom is invalid. |
michael@0 | 228 | bool AtomMatchesChar(bool escaped, char pattern_char, char ch) { |
michael@0 | 229 | if (escaped) { // "\\p" where p is pattern_char. |
michael@0 | 230 | switch (pattern_char) { |
michael@0 | 231 | case 'd': return IsAsciiDigit(ch); |
michael@0 | 232 | case 'D': return !IsAsciiDigit(ch); |
michael@0 | 233 | case 'f': return ch == '\f'; |
michael@0 | 234 | case 'n': return ch == '\n'; |
michael@0 | 235 | case 'r': return ch == '\r'; |
michael@0 | 236 | case 's': return IsAsciiWhiteSpace(ch); |
michael@0 | 237 | case 'S': return !IsAsciiWhiteSpace(ch); |
michael@0 | 238 | case 't': return ch == '\t'; |
michael@0 | 239 | case 'v': return ch == '\v'; |
michael@0 | 240 | case 'w': return IsAsciiWordChar(ch); |
michael@0 | 241 | case 'W': return !IsAsciiWordChar(ch); |
michael@0 | 242 | } |
michael@0 | 243 | return IsAsciiPunct(pattern_char) && pattern_char == ch; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | return (pattern_char == '.' && ch != '\n') || pattern_char == ch; |
michael@0 | 247 | } |
michael@0 | 248 | |
michael@0 | 249 | // Helper function used by ValidateRegex() to format error messages. |
michael@0 | 250 | String FormatRegexSyntaxError(const char* regex, int index) { |
michael@0 | 251 | return (Message() << "Syntax error at index " << index |
michael@0 | 252 | << " in simple regular expression \"" << regex << "\": ").GetString(); |
michael@0 | 253 | } |
michael@0 | 254 | |
michael@0 | 255 | // Generates non-fatal failures and returns false if regex is invalid; |
michael@0 | 256 | // otherwise returns true. |
michael@0 | 257 | bool ValidateRegex(const char* regex) { |
michael@0 | 258 | if (regex == NULL) { |
michael@0 | 259 | // TODO(wan@google.com): fix the source file location in the |
michael@0 | 260 | // assertion failures to match where the regex is used in user |
michael@0 | 261 | // code. |
michael@0 | 262 | ADD_FAILURE() << "NULL is not a valid simple regular expression."; |
michael@0 | 263 | return false; |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | bool is_valid = true; |
michael@0 | 267 | |
michael@0 | 268 | // True iff ?, *, or + can follow the previous atom. |
michael@0 | 269 | bool prev_repeatable = false; |
michael@0 | 270 | for (int i = 0; regex[i]; i++) { |
michael@0 | 271 | if (regex[i] == '\\') { // An escape sequence |
michael@0 | 272 | i++; |
michael@0 | 273 | if (regex[i] == '\0') { |
michael@0 | 274 | ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1) |
michael@0 | 275 | << "'\\' cannot appear at the end."; |
michael@0 | 276 | return false; |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | if (!IsValidEscape(regex[i])) { |
michael@0 | 280 | ADD_FAILURE() << FormatRegexSyntaxError(regex, i - 1) |
michael@0 | 281 | << "invalid escape sequence \"\\" << regex[i] << "\"."; |
michael@0 | 282 | is_valid = false; |
michael@0 | 283 | } |
michael@0 | 284 | prev_repeatable = true; |
michael@0 | 285 | } else { // Not an escape sequence. |
michael@0 | 286 | const char ch = regex[i]; |
michael@0 | 287 | |
michael@0 | 288 | if (ch == '^' && i > 0) { |
michael@0 | 289 | ADD_FAILURE() << FormatRegexSyntaxError(regex, i) |
michael@0 | 290 | << "'^' can only appear at the beginning."; |
michael@0 | 291 | is_valid = false; |
michael@0 | 292 | } else if (ch == '$' && regex[i + 1] != '\0') { |
michael@0 | 293 | ADD_FAILURE() << FormatRegexSyntaxError(regex, i) |
michael@0 | 294 | << "'$' can only appear at the end."; |
michael@0 | 295 | is_valid = false; |
michael@0 | 296 | } else if (IsInSet(ch, "()[]{}|")) { |
michael@0 | 297 | ADD_FAILURE() << FormatRegexSyntaxError(regex, i) |
michael@0 | 298 | << "'" << ch << "' is unsupported."; |
michael@0 | 299 | is_valid = false; |
michael@0 | 300 | } else if (IsRepeat(ch) && !prev_repeatable) { |
michael@0 | 301 | ADD_FAILURE() << FormatRegexSyntaxError(regex, i) |
michael@0 | 302 | << "'" << ch << "' can only follow a repeatable token."; |
michael@0 | 303 | is_valid = false; |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | prev_repeatable = !IsInSet(ch, "^$?*+"); |
michael@0 | 307 | } |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | return is_valid; |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | // Matches a repeated regex atom followed by a valid simple regular |
michael@0 | 314 | // expression. The regex atom is defined as c if escaped is false, |
michael@0 | 315 | // or \c otherwise. repeat is the repetition meta character (?, *, |
michael@0 | 316 | // or +). The behavior is undefined if str contains too many |
michael@0 | 317 | // characters to be indexable by size_t, in which case the test will |
michael@0 | 318 | // probably time out anyway. We are fine with this limitation as |
michael@0 | 319 | // std::string has it too. |
michael@0 | 320 | bool MatchRepetitionAndRegexAtHead( |
michael@0 | 321 | bool escaped, char c, char repeat, const char* regex, |
michael@0 | 322 | const char* str) { |
michael@0 | 323 | const size_t min_count = (repeat == '+') ? 1 : 0; |
michael@0 | 324 | const size_t max_count = (repeat == '?') ? 1 : |
michael@0 | 325 | static_cast<size_t>(-1) - 1; |
michael@0 | 326 | // We cannot call numeric_limits::max() as it conflicts with the |
michael@0 | 327 | // max() macro on Windows. |
michael@0 | 328 | |
michael@0 | 329 | for (size_t i = 0; i <= max_count; ++i) { |
michael@0 | 330 | // We know that the atom matches each of the first i characters in str. |
michael@0 | 331 | if (i >= min_count && MatchRegexAtHead(regex, str + i)) { |
michael@0 | 332 | // We have enough matches at the head, and the tail matches too. |
michael@0 | 333 | // Since we only care about *whether* the pattern matches str |
michael@0 | 334 | // (as opposed to *how* it matches), there is no need to find a |
michael@0 | 335 | // greedy match. |
michael@0 | 336 | return true; |
michael@0 | 337 | } |
michael@0 | 338 | if (str[i] == '\0' || !AtomMatchesChar(escaped, c, str[i])) |
michael@0 | 339 | return false; |
michael@0 | 340 | } |
michael@0 | 341 | return false; |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | // Returns true iff regex matches a prefix of str. regex must be a |
michael@0 | 345 | // valid simple regular expression and not start with "^", or the |
michael@0 | 346 | // result is undefined. |
michael@0 | 347 | bool MatchRegexAtHead(const char* regex, const char* str) { |
michael@0 | 348 | if (*regex == '\0') // An empty regex matches a prefix of anything. |
michael@0 | 349 | return true; |
michael@0 | 350 | |
michael@0 | 351 | // "$" only matches the end of a string. Note that regex being |
michael@0 | 352 | // valid guarantees that there's nothing after "$" in it. |
michael@0 | 353 | if (*regex == '$') |
michael@0 | 354 | return *str == '\0'; |
michael@0 | 355 | |
michael@0 | 356 | // Is the first thing in regex an escape sequence? |
michael@0 | 357 | const bool escaped = *regex == '\\'; |
michael@0 | 358 | if (escaped) |
michael@0 | 359 | ++regex; |
michael@0 | 360 | if (IsRepeat(regex[1])) { |
michael@0 | 361 | // MatchRepetitionAndRegexAtHead() calls MatchRegexAtHead(), so |
michael@0 | 362 | // here's an indirect recursion. It terminates as the regex gets |
michael@0 | 363 | // shorter in each recursion. |
michael@0 | 364 | return MatchRepetitionAndRegexAtHead( |
michael@0 | 365 | escaped, regex[0], regex[1], regex + 2, str); |
michael@0 | 366 | } else { |
michael@0 | 367 | // regex isn't empty, isn't "$", and doesn't start with a |
michael@0 | 368 | // repetition. We match the first atom of regex with the first |
michael@0 | 369 | // character of str and recurse. |
michael@0 | 370 | return (*str != '\0') && AtomMatchesChar(escaped, *regex, *str) && |
michael@0 | 371 | MatchRegexAtHead(regex + 1, str + 1); |
michael@0 | 372 | } |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | // Returns true iff regex matches any substring of str. regex must be |
michael@0 | 376 | // a valid simple regular expression, or the result is undefined. |
michael@0 | 377 | // |
michael@0 | 378 | // The algorithm is recursive, but the recursion depth doesn't exceed |
michael@0 | 379 | // the regex length, so we won't need to worry about running out of |
michael@0 | 380 | // stack space normally. In rare cases the time complexity can be |
michael@0 | 381 | // exponential with respect to the regex length + the string length, |
michael@0 | 382 | // but usually it's must faster (often close to linear). |
michael@0 | 383 | bool MatchRegexAnywhere(const char* regex, const char* str) { |
michael@0 | 384 | if (regex == NULL || str == NULL) |
michael@0 | 385 | return false; |
michael@0 | 386 | |
michael@0 | 387 | if (*regex == '^') |
michael@0 | 388 | return MatchRegexAtHead(regex + 1, str); |
michael@0 | 389 | |
michael@0 | 390 | // A successful match can be anywhere in str. |
michael@0 | 391 | do { |
michael@0 | 392 | if (MatchRegexAtHead(regex, str)) |
michael@0 | 393 | return true; |
michael@0 | 394 | } while (*str++ != '\0'); |
michael@0 | 395 | return false; |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | // Implements the RE class. |
michael@0 | 399 | |
michael@0 | 400 | RE::~RE() { |
michael@0 | 401 | free(const_cast<char*>(pattern_)); |
michael@0 | 402 | free(const_cast<char*>(full_pattern_)); |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | // Returns true iff regular expression re matches the entire str. |
michael@0 | 406 | bool RE::FullMatch(const char* str, const RE& re) { |
michael@0 | 407 | return re.is_valid_ && MatchRegexAnywhere(re.full_pattern_, str); |
michael@0 | 408 | } |
michael@0 | 409 | |
michael@0 | 410 | // Returns true iff regular expression re matches a substring of str |
michael@0 | 411 | // (including str itself). |
michael@0 | 412 | bool RE::PartialMatch(const char* str, const RE& re) { |
michael@0 | 413 | return re.is_valid_ && MatchRegexAnywhere(re.pattern_, str); |
michael@0 | 414 | } |
michael@0 | 415 | |
michael@0 | 416 | // Initializes an RE from its string representation. |
michael@0 | 417 | void RE::Init(const char* regex) { |
michael@0 | 418 | pattern_ = full_pattern_ = NULL; |
michael@0 | 419 | if (regex != NULL) { |
michael@0 | 420 | pattern_ = posix::StrDup(regex); |
michael@0 | 421 | } |
michael@0 | 422 | |
michael@0 | 423 | is_valid_ = ValidateRegex(regex); |
michael@0 | 424 | if (!is_valid_) { |
michael@0 | 425 | // No need to calculate the full pattern when the regex is invalid. |
michael@0 | 426 | return; |
michael@0 | 427 | } |
michael@0 | 428 | |
michael@0 | 429 | const size_t len = strlen(regex); |
michael@0 | 430 | // Reserves enough bytes to hold the regular expression used for a |
michael@0 | 431 | // full match: we need space to prepend a '^', append a '$', and |
michael@0 | 432 | // terminate the string with '\0'. |
michael@0 | 433 | char* buffer = static_cast<char*>(malloc(len + 3)); |
michael@0 | 434 | full_pattern_ = buffer; |
michael@0 | 435 | |
michael@0 | 436 | if (*regex != '^') |
michael@0 | 437 | *buffer++ = '^'; // Makes sure full_pattern_ starts with '^'. |
michael@0 | 438 | |
michael@0 | 439 | // We don't use snprintf or strncpy, as they trigger a warning when |
michael@0 | 440 | // compiled with VC++ 8.0. |
michael@0 | 441 | memcpy(buffer, regex, len); |
michael@0 | 442 | buffer += len; |
michael@0 | 443 | |
michael@0 | 444 | if (len == 0 || regex[len - 1] != '$') |
michael@0 | 445 | *buffer++ = '$'; // Makes sure full_pattern_ ends with '$'. |
michael@0 | 446 | |
michael@0 | 447 | *buffer = '\0'; |
michael@0 | 448 | } |
michael@0 | 449 | |
michael@0 | 450 | #endif // GTEST_USES_POSIX_RE |
michael@0 | 451 | |
michael@0 | 452 | const char kUnknownFile[] = "unknown file"; |
michael@0 | 453 | |
michael@0 | 454 | // Formats a source file path and a line number as they would appear |
michael@0 | 455 | // in an error message from the compiler used to compile this code. |
michael@0 | 456 | GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) { |
michael@0 | 457 | const char* const file_name = file == NULL ? kUnknownFile : file; |
michael@0 | 458 | |
michael@0 | 459 | if (line < 0) { |
michael@0 | 460 | return String::Format("%s:", file_name).c_str(); |
michael@0 | 461 | } |
michael@0 | 462 | #ifdef _MSC_VER |
michael@0 | 463 | return String::Format("%s(%d):", file_name, line).c_str(); |
michael@0 | 464 | #else |
michael@0 | 465 | return String::Format("%s:%d:", file_name, line).c_str(); |
michael@0 | 466 | #endif // _MSC_VER |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | // Formats a file location for compiler-independent XML output. |
michael@0 | 470 | // Although this function is not platform dependent, we put it next to |
michael@0 | 471 | // FormatFileLocation in order to contrast the two functions. |
michael@0 | 472 | // Note that FormatCompilerIndependentFileLocation() does NOT append colon |
michael@0 | 473 | // to the file location it produces, unlike FormatFileLocation(). |
michael@0 | 474 | GTEST_API_ ::std::string FormatCompilerIndependentFileLocation( |
michael@0 | 475 | const char* file, int line) { |
michael@0 | 476 | const char* const file_name = file == NULL ? kUnknownFile : file; |
michael@0 | 477 | |
michael@0 | 478 | if (line < 0) |
michael@0 | 479 | return file_name; |
michael@0 | 480 | else |
michael@0 | 481 | return String::Format("%s:%d", file_name, line).c_str(); |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | |
michael@0 | 485 | GTestLog::GTestLog(GTestLogSeverity severity, const char* file, int line) |
michael@0 | 486 | : severity_(severity) { |
michael@0 | 487 | const char* const marker = |
michael@0 | 488 | severity == GTEST_INFO ? "[ INFO ]" : |
michael@0 | 489 | severity == GTEST_WARNING ? "[WARNING]" : |
michael@0 | 490 | severity == GTEST_ERROR ? "[ ERROR ]" : "[ FATAL ]"; |
michael@0 | 491 | GetStream() << ::std::endl << marker << " " |
michael@0 | 492 | << FormatFileLocation(file, line).c_str() << ": "; |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | // Flushes the buffers and, if severity is GTEST_FATAL, aborts the program. |
michael@0 | 496 | GTestLog::~GTestLog() { |
michael@0 | 497 | GetStream() << ::std::endl; |
michael@0 | 498 | if (severity_ == GTEST_FATAL) { |
michael@0 | 499 | fflush(stderr); |
michael@0 | 500 | posix::Abort(); |
michael@0 | 501 | } |
michael@0 | 502 | } |
michael@0 | 503 | // Disable Microsoft deprecation warnings for POSIX functions called from |
michael@0 | 504 | // this class (creat, dup, dup2, and close) |
michael@0 | 505 | #ifdef _MSC_VER |
michael@0 | 506 | # pragma warning(push) |
michael@0 | 507 | # pragma warning(disable: 4996) |
michael@0 | 508 | #endif // _MSC_VER |
michael@0 | 509 | |
michael@0 | 510 | #if GTEST_HAS_STREAM_REDIRECTION |
michael@0 | 511 | |
michael@0 | 512 | // Object that captures an output stream (stdout/stderr). |
michael@0 | 513 | class CapturedStream { |
michael@0 | 514 | public: |
michael@0 | 515 | // The ctor redirects the stream to a temporary file. |
michael@0 | 516 | CapturedStream(int fd) : fd_(fd), uncaptured_fd_(dup(fd)) { |
michael@0 | 517 | # if GTEST_OS_WINDOWS |
michael@0 | 518 | char temp_dir_path[MAX_PATH + 1] = { '\0' }; // NOLINT |
michael@0 | 519 | char temp_file_path[MAX_PATH + 1] = { '\0' }; // NOLINT |
michael@0 | 520 | |
michael@0 | 521 | ::GetTempPathA(sizeof(temp_dir_path), temp_dir_path); |
michael@0 | 522 | const UINT success = ::GetTempFileNameA(temp_dir_path, |
michael@0 | 523 | "gtest_redir", |
michael@0 | 524 | 0, // Generate unique file name. |
michael@0 | 525 | temp_file_path); |
michael@0 | 526 | GTEST_CHECK_(success != 0) |
michael@0 | 527 | << "Unable to create a temporary file in " << temp_dir_path; |
michael@0 | 528 | const int captured_fd = creat(temp_file_path, _S_IREAD | _S_IWRITE); |
michael@0 | 529 | GTEST_CHECK_(captured_fd != -1) << "Unable to open temporary file " |
michael@0 | 530 | << temp_file_path; |
michael@0 | 531 | filename_ = temp_file_path; |
michael@0 | 532 | # else |
michael@0 | 533 | // There's no guarantee that a test has write access to the current |
michael@0 | 534 | // directory, so we create the temporary file in the /tmp directory instead. |
michael@0 | 535 | // We use /tmp on most systems, and /mnt/sdcard on Android. That's because |
michael@0 | 536 | // Android doesn't have /tmp. |
michael@0 | 537 | # if GTEST_OS_LINUX_ANDROID |
michael@0 | 538 | char name_template[] = "/mnt/sdcard/gtest_captured_stream.XXXXXX"; |
michael@0 | 539 | # else |
michael@0 | 540 | char name_template[] = "/tmp/captured_stream.XXXXXX"; |
michael@0 | 541 | # endif // GTEST_OS_LINUX_ANDROID |
michael@0 | 542 | const int captured_fd = mkstemp(name_template); |
michael@0 | 543 | filename_ = name_template; |
michael@0 | 544 | # endif // GTEST_OS_WINDOWS |
michael@0 | 545 | fflush(NULL); |
michael@0 | 546 | dup2(captured_fd, fd_); |
michael@0 | 547 | close(captured_fd); |
michael@0 | 548 | } |
michael@0 | 549 | |
michael@0 | 550 | ~CapturedStream() { |
michael@0 | 551 | remove(filename_.c_str()); |
michael@0 | 552 | } |
michael@0 | 553 | |
michael@0 | 554 | String GetCapturedString() { |
michael@0 | 555 | if (uncaptured_fd_ != -1) { |
michael@0 | 556 | // Restores the original stream. |
michael@0 | 557 | fflush(NULL); |
michael@0 | 558 | dup2(uncaptured_fd_, fd_); |
michael@0 | 559 | close(uncaptured_fd_); |
michael@0 | 560 | uncaptured_fd_ = -1; |
michael@0 | 561 | } |
michael@0 | 562 | |
michael@0 | 563 | FILE* const file = posix::FOpen(filename_.c_str(), "r"); |
michael@0 | 564 | const String content = ReadEntireFile(file); |
michael@0 | 565 | posix::FClose(file); |
michael@0 | 566 | return content; |
michael@0 | 567 | } |
michael@0 | 568 | |
michael@0 | 569 | private: |
michael@0 | 570 | // Reads the entire content of a file as a String. |
michael@0 | 571 | static String ReadEntireFile(FILE* file); |
michael@0 | 572 | |
michael@0 | 573 | // Returns the size (in bytes) of a file. |
michael@0 | 574 | static size_t GetFileSize(FILE* file); |
michael@0 | 575 | |
michael@0 | 576 | const int fd_; // A stream to capture. |
michael@0 | 577 | int uncaptured_fd_; |
michael@0 | 578 | // Name of the temporary file holding the stderr output. |
michael@0 | 579 | ::std::string filename_; |
michael@0 | 580 | |
michael@0 | 581 | GTEST_DISALLOW_COPY_AND_ASSIGN_(CapturedStream); |
michael@0 | 582 | }; |
michael@0 | 583 | |
michael@0 | 584 | // Returns the size (in bytes) of a file. |
michael@0 | 585 | size_t CapturedStream::GetFileSize(FILE* file) { |
michael@0 | 586 | fseek(file, 0, SEEK_END); |
michael@0 | 587 | return static_cast<size_t>(ftell(file)); |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | // Reads the entire content of a file as a string. |
michael@0 | 591 | String CapturedStream::ReadEntireFile(FILE* file) { |
michael@0 | 592 | const size_t file_size = GetFileSize(file); |
michael@0 | 593 | char* const buffer = new char[file_size]; |
michael@0 | 594 | |
michael@0 | 595 | size_t bytes_last_read = 0; // # of bytes read in the last fread() |
michael@0 | 596 | size_t bytes_read = 0; // # of bytes read so far |
michael@0 | 597 | |
michael@0 | 598 | fseek(file, 0, SEEK_SET); |
michael@0 | 599 | |
michael@0 | 600 | // Keeps reading the file until we cannot read further or the |
michael@0 | 601 | // pre-determined file size is reached. |
michael@0 | 602 | do { |
michael@0 | 603 | bytes_last_read = fread(buffer+bytes_read, 1, file_size-bytes_read, file); |
michael@0 | 604 | bytes_read += bytes_last_read; |
michael@0 | 605 | } while (bytes_last_read > 0 && bytes_read < file_size); |
michael@0 | 606 | |
michael@0 | 607 | const String content(buffer, bytes_read); |
michael@0 | 608 | delete[] buffer; |
michael@0 | 609 | |
michael@0 | 610 | return content; |
michael@0 | 611 | } |
michael@0 | 612 | |
michael@0 | 613 | # ifdef _MSC_VER |
michael@0 | 614 | # pragma warning(pop) |
michael@0 | 615 | # endif // _MSC_VER |
michael@0 | 616 | |
michael@0 | 617 | static CapturedStream* g_captured_stderr = NULL; |
michael@0 | 618 | static CapturedStream* g_captured_stdout = NULL; |
michael@0 | 619 | |
michael@0 | 620 | // Starts capturing an output stream (stdout/stderr). |
michael@0 | 621 | void CaptureStream(int fd, const char* stream_name, CapturedStream** stream) { |
michael@0 | 622 | if (*stream != NULL) { |
michael@0 | 623 | GTEST_LOG_(FATAL) << "Only one " << stream_name |
michael@0 | 624 | << " capturer can exist at a time."; |
michael@0 | 625 | } |
michael@0 | 626 | *stream = new CapturedStream(fd); |
michael@0 | 627 | } |
michael@0 | 628 | |
michael@0 | 629 | // Stops capturing the output stream and returns the captured string. |
michael@0 | 630 | String GetCapturedStream(CapturedStream** captured_stream) { |
michael@0 | 631 | const String content = (*captured_stream)->GetCapturedString(); |
michael@0 | 632 | |
michael@0 | 633 | delete *captured_stream; |
michael@0 | 634 | *captured_stream = NULL; |
michael@0 | 635 | |
michael@0 | 636 | return content; |
michael@0 | 637 | } |
michael@0 | 638 | |
michael@0 | 639 | // Starts capturing stdout. |
michael@0 | 640 | void CaptureStdout() { |
michael@0 | 641 | CaptureStream(kStdOutFileno, "stdout", &g_captured_stdout); |
michael@0 | 642 | } |
michael@0 | 643 | |
michael@0 | 644 | // Starts capturing stderr. |
michael@0 | 645 | void CaptureStderr() { |
michael@0 | 646 | CaptureStream(kStdErrFileno, "stderr", &g_captured_stderr); |
michael@0 | 647 | } |
michael@0 | 648 | |
michael@0 | 649 | // Stops capturing stdout and returns the captured string. |
michael@0 | 650 | String GetCapturedStdout() { return GetCapturedStream(&g_captured_stdout); } |
michael@0 | 651 | |
michael@0 | 652 | // Stops capturing stderr and returns the captured string. |
michael@0 | 653 | String GetCapturedStderr() { return GetCapturedStream(&g_captured_stderr); } |
michael@0 | 654 | |
michael@0 | 655 | #endif // GTEST_HAS_STREAM_REDIRECTION |
michael@0 | 656 | |
michael@0 | 657 | #if GTEST_HAS_DEATH_TEST |
michael@0 | 658 | |
michael@0 | 659 | // A copy of all command line arguments. Set by InitGoogleTest(). |
michael@0 | 660 | ::std::vector<testing::internal::string> g_argvs; |
michael@0 | 661 | |
michael@0 | 662 | static const ::std::vector<testing::internal::string>* g_injected_test_argvs = |
michael@0 | 663 | NULL; // Owned. |
michael@0 | 664 | |
michael@0 | 665 | void SetInjectableArgvs(const ::std::vector<testing::internal::string>* argvs) { |
michael@0 | 666 | if (g_injected_test_argvs != argvs) |
michael@0 | 667 | delete g_injected_test_argvs; |
michael@0 | 668 | g_injected_test_argvs = argvs; |
michael@0 | 669 | } |
michael@0 | 670 | |
michael@0 | 671 | const ::std::vector<testing::internal::string>& GetInjectableArgvs() { |
michael@0 | 672 | if (g_injected_test_argvs != NULL) { |
michael@0 | 673 | return *g_injected_test_argvs; |
michael@0 | 674 | } |
michael@0 | 675 | return g_argvs; |
michael@0 | 676 | } |
michael@0 | 677 | #endif // GTEST_HAS_DEATH_TEST |
michael@0 | 678 | |
michael@0 | 679 | #if GTEST_OS_WINDOWS_MOBILE |
michael@0 | 680 | namespace posix { |
michael@0 | 681 | void Abort() { |
michael@0 | 682 | DebugBreak(); |
michael@0 | 683 | TerminateProcess(GetCurrentProcess(), 1); |
michael@0 | 684 | } |
michael@0 | 685 | } // namespace posix |
michael@0 | 686 | #endif // GTEST_OS_WINDOWS_MOBILE |
michael@0 | 687 | |
michael@0 | 688 | // Returns the name of the environment variable corresponding to the |
michael@0 | 689 | // given flag. For example, FlagToEnvVar("foo") will return |
michael@0 | 690 | // "GTEST_FOO" in the open-source version. |
michael@0 | 691 | static String FlagToEnvVar(const char* flag) { |
michael@0 | 692 | const String full_flag = |
michael@0 | 693 | (Message() << GTEST_FLAG_PREFIX_ << flag).GetString(); |
michael@0 | 694 | |
michael@0 | 695 | Message env_var; |
michael@0 | 696 | for (size_t i = 0; i != full_flag.length(); i++) { |
michael@0 | 697 | env_var << ToUpper(full_flag.c_str()[i]); |
michael@0 | 698 | } |
michael@0 | 699 | |
michael@0 | 700 | return env_var.GetString(); |
michael@0 | 701 | } |
michael@0 | 702 | |
michael@0 | 703 | // Parses 'str' for a 32-bit signed integer. If successful, writes |
michael@0 | 704 | // the result to *value and returns true; otherwise leaves *value |
michael@0 | 705 | // unchanged and returns false. |
michael@0 | 706 | bool ParseInt32(const Message& src_text, const char* str, Int32* value) { |
michael@0 | 707 | // Parses the environment variable as a decimal integer. |
michael@0 | 708 | char* end = NULL; |
michael@0 | 709 | const long long_value = strtol(str, &end, 10); // NOLINT |
michael@0 | 710 | |
michael@0 | 711 | // Has strtol() consumed all characters in the string? |
michael@0 | 712 | if (*end != '\0') { |
michael@0 | 713 | // No - an invalid character was encountered. |
michael@0 | 714 | Message msg; |
michael@0 | 715 | msg << "WARNING: " << src_text |
michael@0 | 716 | << " is expected to be a 32-bit integer, but actually" |
michael@0 | 717 | << " has value \"" << str << "\".\n"; |
michael@0 | 718 | printf("%s", msg.GetString().c_str()); |
michael@0 | 719 | fflush(stdout); |
michael@0 | 720 | return false; |
michael@0 | 721 | } |
michael@0 | 722 | |
michael@0 | 723 | // Is the parsed value in the range of an Int32? |
michael@0 | 724 | const Int32 result = static_cast<Int32>(long_value); |
michael@0 | 725 | if (long_value == LONG_MAX || long_value == LONG_MIN || |
michael@0 | 726 | // The parsed value overflows as a long. (strtol() returns |
michael@0 | 727 | // LONG_MAX or LONG_MIN when the input overflows.) |
michael@0 | 728 | result != long_value |
michael@0 | 729 | // The parsed value overflows as an Int32. |
michael@0 | 730 | ) { |
michael@0 | 731 | Message msg; |
michael@0 | 732 | msg << "WARNING: " << src_text |
michael@0 | 733 | << " is expected to be a 32-bit integer, but actually" |
michael@0 | 734 | << " has value " << str << ", which overflows.\n"; |
michael@0 | 735 | printf("%s", msg.GetString().c_str()); |
michael@0 | 736 | fflush(stdout); |
michael@0 | 737 | return false; |
michael@0 | 738 | } |
michael@0 | 739 | |
michael@0 | 740 | *value = result; |
michael@0 | 741 | return true; |
michael@0 | 742 | } |
michael@0 | 743 | |
michael@0 | 744 | // Reads and returns the Boolean environment variable corresponding to |
michael@0 | 745 | // the given flag; if it's not set, returns default_value. |
michael@0 | 746 | // |
michael@0 | 747 | // The value is considered true iff it's not "0". |
michael@0 | 748 | bool BoolFromGTestEnv(const char* flag, bool default_value) { |
michael@0 | 749 | const String env_var = FlagToEnvVar(flag); |
michael@0 | 750 | const char* const string_value = posix::GetEnv(env_var.c_str()); |
michael@0 | 751 | return string_value == NULL ? |
michael@0 | 752 | default_value : strcmp(string_value, "0") != 0; |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | // Reads and returns a 32-bit integer stored in the environment |
michael@0 | 756 | // variable corresponding to the given flag; if it isn't set or |
michael@0 | 757 | // doesn't represent a valid 32-bit integer, returns default_value. |
michael@0 | 758 | Int32 Int32FromGTestEnv(const char* flag, Int32 default_value) { |
michael@0 | 759 | const String env_var = FlagToEnvVar(flag); |
michael@0 | 760 | const char* const string_value = posix::GetEnv(env_var.c_str()); |
michael@0 | 761 | if (string_value == NULL) { |
michael@0 | 762 | // The environment variable is not set. |
michael@0 | 763 | return default_value; |
michael@0 | 764 | } |
michael@0 | 765 | |
michael@0 | 766 | Int32 result = default_value; |
michael@0 | 767 | if (!ParseInt32(Message() << "Environment variable " << env_var, |
michael@0 | 768 | string_value, &result)) { |
michael@0 | 769 | printf("The default value %s is used.\n", |
michael@0 | 770 | (Message() << default_value).GetString().c_str()); |
michael@0 | 771 | fflush(stdout); |
michael@0 | 772 | return default_value; |
michael@0 | 773 | } |
michael@0 | 774 | |
michael@0 | 775 | return result; |
michael@0 | 776 | } |
michael@0 | 777 | |
michael@0 | 778 | // Reads and returns the string environment variable corresponding to |
michael@0 | 779 | // the given flag; if it's not set, returns default_value. |
michael@0 | 780 | const char* StringFromGTestEnv(const char* flag, const char* default_value) { |
michael@0 | 781 | const String env_var = FlagToEnvVar(flag); |
michael@0 | 782 | const char* const value = posix::GetEnv(env_var.c_str()); |
michael@0 | 783 | return value == NULL ? default_value : value; |
michael@0 | 784 | } |
michael@0 | 785 | |
michael@0 | 786 | } // namespace internal |
michael@0 | 787 | } // namespace testing |