Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #pragma once |
| 18 | |
| 19 | #include <regex> |
| 20 | #include <string> |
android-t1 | 3d2c5b2 | 2022-10-12 13:43:18 +0800 | [diff] [blame] | 21 | #include <type_traits> |
Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 22 | |
| 23 | #include <android-base/file.h> |
| 24 | #include <android-base/macros.h> |
| 25 | |
| 26 | class CapturedStdFd { |
| 27 | public: |
| 28 | CapturedStdFd(int std_fd); |
| 29 | ~CapturedStdFd(); |
| 30 | |
| 31 | std::string str(); |
| 32 | |
| 33 | void Start(); |
| 34 | void Stop(); |
| 35 | void Reset(); |
| 36 | |
| 37 | private: |
| 38 | int fd() const; |
| 39 | |
| 40 | TemporaryFile temp_file_; |
| 41 | int std_fd_; |
| 42 | int old_fd_ = -1; |
| 43 | |
| 44 | DISALLOW_COPY_AND_ASSIGN(CapturedStdFd); |
| 45 | }; |
| 46 | |
| 47 | class CapturedStderr : public CapturedStdFd { |
| 48 | public: |
| 49 | CapturedStderr() : CapturedStdFd(STDERR_FILENO) {} |
| 50 | }; |
| 51 | |
| 52 | class CapturedStdout : public CapturedStdFd { |
| 53 | public: |
| 54 | CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {} |
| 55 | }; |
| 56 | |
android-t1 | 3d2c5b2 | 2022-10-12 13:43:18 +0800 | [diff] [blame] | 57 | #define __LIBBASE_GENERIC_REGEX_SEARCH(__s, __pattern) \ |
| 58 | (std::regex_search(__s, std::basic_regex<std::decay<decltype(__s[0])>::type>((__pattern)))) |
| 59 | |
| 60 | #define ASSERT_MATCH(__string, __pattern) \ |
| 61 | do { \ |
| 62 | auto __s = (__string); \ |
| 63 | if (!__LIBBASE_GENERIC_REGEX_SEARCH(__s, (__pattern))) { \ |
| 64 | FAIL() << "regex mismatch: expected " << (__pattern) << " in:\n" << __s; \ |
| 65 | } \ |
Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 66 | } while (0) |
| 67 | |
android-t1 | 3d2c5b2 | 2022-10-12 13:43:18 +0800 | [diff] [blame] | 68 | #define ASSERT_NOT_MATCH(__string, __pattern) \ |
| 69 | do { \ |
| 70 | auto __s = (__string); \ |
| 71 | if (__LIBBASE_GENERIC_REGEX_SEARCH(__s, (__pattern))) { \ |
| 72 | FAIL() << "regex mismatch: expected to not find " << (__pattern) << " in:\n" << __s; \ |
| 73 | } \ |
Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 74 | } while (0) |
| 75 | |
android-t1 | 3d2c5b2 | 2022-10-12 13:43:18 +0800 | [diff] [blame] | 76 | #define EXPECT_MATCH(__string, __pattern) \ |
| 77 | do { \ |
| 78 | auto __s = (__string); \ |
| 79 | if (!__LIBBASE_GENERIC_REGEX_SEARCH(__s, (__pattern))) { \ |
| 80 | ADD_FAILURE() << "regex mismatch: expected " << (__pattern) << " in:\n" << __s; \ |
| 81 | } \ |
Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 82 | } while (0) |
| 83 | |
android-t1 | 3d2c5b2 | 2022-10-12 13:43:18 +0800 | [diff] [blame] | 84 | #define EXPECT_NOT_MATCH(__string, __pattern) \ |
| 85 | do { \ |
| 86 | auto __s = (__string); \ |
| 87 | if (__LIBBASE_GENERIC_REGEX_SEARCH(__s, (__pattern))) { \ |
| 88 | ADD_FAILURE() << "regex mismatch: expected to not find " << (__pattern) << " in:\n" << __s; \ |
| 89 | } \ |
Martin Stjernholm | c15e7e4 | 2020-12-02 22:50:53 +0000 | [diff] [blame] | 90 | } while (0) |
android-t1 | 3d2c5b2 | 2022-10-12 13:43:18 +0800 | [diff] [blame] | 91 | |
| 92 | extern "C" void __hwasan_init() __attribute__((weak)); |
| 93 | static inline bool running_with_hwasan() { |
| 94 | return &__hwasan_init != 0; |
| 95 | } |
| 96 | |
| 97 | #define SKIP_WITH_HWASAN if (running_with_hwasan()) GTEST_SKIP() |