blob: 29dc39442d585d2c46d75c879c27b985eb0f5708 [file] [log] [blame]
Martin Stjernholmc15e7e42020-12-02 22:50:53 +00001/*
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-t13d2c5b22022-10-12 13:43:18 +080021#include <type_traits>
Martin Stjernholmc15e7e42020-12-02 22:50:53 +000022
23#include <android-base/file.h>
24#include <android-base/macros.h>
25
26class 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
47class CapturedStderr : public CapturedStdFd {
48 public:
49 CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
50};
51
52class CapturedStdout : public CapturedStdFd {
53 public:
54 CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
55};
56
android-t13d2c5b22022-10-12 13:43:18 +080057#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 Stjernholmc15e7e42020-12-02 22:50:53 +000066 } while (0)
67
android-t13d2c5b22022-10-12 13:43:18 +080068#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 Stjernholmc15e7e42020-12-02 22:50:53 +000074 } while (0)
75
android-t13d2c5b22022-10-12 13:43:18 +080076#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 Stjernholmc15e7e42020-12-02 22:50:53 +000082 } while (0)
83
android-t13d2c5b22022-10-12 13:43:18 +080084#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 Stjernholmc15e7e42020-12-02 22:50:53 +000090 } while (0)
android-t13d2c5b22022-10-12 13:43:18 +080091
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()