blob: 29dc39442d585d2c46d75c879c27b985eb0f5708 [file] [log] [blame]
Dan Alberte3ea0582015-03-13 23:06:01 -07001/*
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 Hughese3c5a2a2018-06-26 17:17:41 -070017#pragma once
Dan Alberte3ea0582015-03-13 23:06:01 -070018
Josh Gao8f1fa002017-04-27 19:48:44 -070019#include <regex>
Alex Valléed0ac74c2015-05-06 16:26:00 -040020#include <string>
Elliott Hughes031d9c42021-09-30 18:12:52 -070021#include <type_traits>
Alex Valléed0ac74c2015-05-06 16:26:00 -040022
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080023#include <android-base/file.h>
Elliott Hughesb6351622015-12-04 22:00:26 -080024#include <android-base/macros.h>
Alex Valléed0ac74c2015-05-06 16:26:00 -040025
Elliott Hughes1c1409f2018-05-23 09:16:46 -070026class CapturedStdFd {
Wei Wangcaee0eb2016-10-21 09:23:39 -070027 public:
Elliott Hughes1c1409f2018-05-23 09:16:46 -070028 CapturedStdFd(int std_fd);
29 ~CapturedStdFd();
Wei Wangcaee0eb2016-10-21 09:23:39 -070030
Elliott Hughes1c1409f2018-05-23 09:16:46 -070031 std::string str();
Wei Wangcaee0eb2016-10-21 09:23:39 -070032
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070033 void Start();
34 void Stop();
Elliott Hughes1c1409f2018-05-23 09:16:46 -070035 void Reset();
Wei Wangcaee0eb2016-10-21 09:23:39 -070036
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070037 private:
38 int fd() const;
39
Wei Wangcaee0eb2016-10-21 09:23:39 -070040 TemporaryFile temp_file_;
Elliott Hughes1c1409f2018-05-23 09:16:46 -070041 int std_fd_;
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070042 int old_fd_ = -1;
Wei Wangcaee0eb2016-10-21 09:23:39 -070043
Elliott Hughes1c1409f2018-05-23 09:16:46 -070044 DISALLOW_COPY_AND_ASSIGN(CapturedStdFd);
45};
46
47class CapturedStderr : public CapturedStdFd {
48 public:
49 CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
50};
51
52class CapturedStdout : public CapturedStdFd {
53 public:
54 CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
Wei Wangcaee0eb2016-10-21 09:23:39 -070055};
56
Elliott Hughes031d9c42021-09-30 18:12:52 -070057#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 } \
Josh Gao8f1fa002017-04-27 19:48:44 -070066 } while (0)
67
Elliott Hughes031d9c42021-09-30 18:12:52 -070068#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 } \
Josh Gao8f1fa002017-04-27 19:48:44 -070074 } while (0)
75
Elliott Hughes031d9c42021-09-30 18:12:52 -070076#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 } \
Josh Gao8f1fa002017-04-27 19:48:44 -070082 } while (0)
83
Elliott Hughes031d9c42021-09-30 18:12:52 -070084#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 } \
Josh Gao8f1fa002017-04-27 19:48:44 -070090 } while (0)
Florian Mayer1055a292022-04-15 14:32:18 -070091
92extern "C" void __hwasan_init() __attribute__((weak));
93static inline bool running_with_hwasan() {
94 return &__hwasan_init != 0;
95}
96
97#define SKIP_WITH_HWASAN if (running_with_hwasan()) GTEST_SKIP()