| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [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 | |
| Elliott Hughes | e3c5a2a | 2018-06-26 17:17:41 -0700 | [diff] [blame] | 17 | #pragma once |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 18 | |
| Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 19 | #include <regex> |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 20 | #include <string> |
| 21 | |
| Mark Salyzyn | 40a4cb4 | 2018-11-12 12:29:14 -0800 | [diff] [blame^] | 22 | #include <android-base/file.h> |
| Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 23 | #include <android-base/macros.h> |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 24 | |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 25 | class CapturedStdFd { |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 26 | public: |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 27 | CapturedStdFd(int std_fd); |
| 28 | ~CapturedStdFd(); |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 29 | |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 30 | std::string str(); |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 31 | |
| Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 32 | void Start(); |
| 33 | void Stop(); |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 34 | void Reset(); |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 35 | |
| Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 36 | private: |
| 37 | int fd() const; |
| 38 | |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 39 | TemporaryFile temp_file_; |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 40 | int std_fd_; |
| Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame] | 41 | int old_fd_ = -1; |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 42 | |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 43 | DISALLOW_COPY_AND_ASSIGN(CapturedStdFd); |
| 44 | }; |
| 45 | |
| 46 | class CapturedStderr : public CapturedStdFd { |
| 47 | public: |
| 48 | CapturedStderr() : CapturedStdFd(STDERR_FILENO) {} |
| 49 | }; |
| 50 | |
| 51 | class CapturedStdout : public CapturedStdFd { |
| 52 | public: |
| 53 | CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {} |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
| Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 56 | #define ASSERT_MATCH(str, pattern) \ |
| 57 | do { \ |
| 58 | if (!std::regex_search((str), std::regex((pattern)))) { \ |
| 59 | FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \ |
| 60 | } \ |
| 61 | } while (0) |
| 62 | |
| 63 | #define ASSERT_NOT_MATCH(str, pattern) \ |
| 64 | do { \ |
| 65 | if (std::regex_search((str), std::regex((pattern)))) { \ |
| 66 | FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \ |
| 67 | } \ |
| 68 | } while (0) |
| 69 | |
| 70 | #define EXPECT_MATCH(str, pattern) \ |
| 71 | do { \ |
| 72 | if (!std::regex_search((str), std::regex((pattern)))) { \ |
| 73 | ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \ |
| 74 | } \ |
| 75 | } while (0) |
| 76 | |
| 77 | #define EXPECT_NOT_MATCH(str, pattern) \ |
| 78 | do { \ |
| 79 | if (std::regex_search((str), std::regex((pattern)))) { \ |
| 80 | ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \ |
| 81 | } \ |
| 82 | } while (0) |