| 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 | |
| Elliott Hughes | b635162 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 22 | #include <android-base/macros.h> |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 23 | |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 24 | class TemporaryFile { |
| 25 | public: |
| 26 | TemporaryFile(); |
| Yabin Cui | a11c57e | 2017-12-06 14:20:07 -0800 | [diff] [blame] | 27 | explicit TemporaryFile(const std::string& tmp_dir); |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 28 | ~TemporaryFile(); |
| 29 | |
| Tianjie Xu | 1852b80 | 2017-09-11 12:01:09 -0700 | [diff] [blame] | 30 | // Release the ownership of fd, caller is reponsible for closing the |
| 31 | // fd or stream properly. |
| 32 | int release(); |
| Yabin Cui | b7abae1 | 2018-03-08 17:03:04 -0800 | [diff] [blame] | 33 | // Don't remove the temporary file in the destructor. |
| 34 | void DoNotRemove() { remove_file_ = false; } |
| Tianjie Xu | 1852b80 | 2017-09-11 12:01:09 -0700 | [diff] [blame] | 35 | |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 36 | int fd; |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 37 | char path[1024]; |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 38 | |
| 39 | private: |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 40 | void init(const std::string& tmp_dir); |
| 41 | |
| Yabin Cui | b7abae1 | 2018-03-08 17:03:04 -0800 | [diff] [blame] | 42 | bool remove_file_ = true; |
| 43 | |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 44 | DISALLOW_COPY_AND_ASSIGN(TemporaryFile); |
| Dan Albert | e3ea058 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 47 | class TemporaryDir { |
| 48 | public: |
| 49 | TemporaryDir(); |
| 50 | ~TemporaryDir(); |
| 51 | |
| 52 | char path[1024]; |
| 53 | |
| 54 | private: |
| 55 | bool init(const std::string& tmp_dir); |
| 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(TemporaryDir); |
| 58 | }; |
| Alex Vallée | d0ac74c | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 59 | |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 60 | class CapturedStdFd { |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 61 | public: |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 62 | CapturedStdFd(int std_fd); |
| 63 | ~CapturedStdFd(); |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 64 | |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 65 | std::string str(); |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 66 | |
| Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 67 | void Start(); |
| 68 | void Stop(); |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 69 | void Reset(); |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 70 | |
| Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 71 | private: |
| 72 | int fd() const; |
| 73 | |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 74 | TemporaryFile temp_file_; |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 75 | int std_fd_; |
| Christopher Ferris | a6f0b67 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 76 | int old_fd_ = -1; |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 77 | |
| Elliott Hughes | 1c1409f | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 78 | DISALLOW_COPY_AND_ASSIGN(CapturedStdFd); |
| 79 | }; |
| 80 | |
| 81 | class CapturedStderr : public CapturedStdFd { |
| 82 | public: |
| 83 | CapturedStderr() : CapturedStdFd(STDERR_FILENO) {} |
| 84 | }; |
| 85 | |
| 86 | class CapturedStdout : public CapturedStdFd { |
| 87 | public: |
| 88 | CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {} |
| Wei Wang | caee0eb | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
| Josh Gao | 8f1fa00 | 2017-04-27 19:48:44 -0700 | [diff] [blame] | 91 | #define ASSERT_MATCH(str, pattern) \ |
| 92 | do { \ |
| 93 | if (!std::regex_search((str), std::regex((pattern)))) { \ |
| 94 | FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \ |
| 95 | } \ |
| 96 | } while (0) |
| 97 | |
| 98 | #define ASSERT_NOT_MATCH(str, pattern) \ |
| 99 | do { \ |
| 100 | if (std::regex_search((str), std::regex((pattern)))) { \ |
| 101 | FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \ |
| 102 | } \ |
| 103 | } while (0) |
| 104 | |
| 105 | #define EXPECT_MATCH(str, pattern) \ |
| 106 | do { \ |
| 107 | if (!std::regex_search((str), std::regex((pattern)))) { \ |
| 108 | ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \ |
| 109 | } \ |
| 110 | } while (0) |
| 111 | |
| 112 | #define EXPECT_NOT_MATCH(str, pattern) \ |
| 113 | do { \ |
| 114 | if (std::regex_search((str), std::regex((pattern)))) { \ |
| 115 | ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \ |
| 116 | } \ |
| 117 | } while (0) |