• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
21 #include <type_traits>
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:
CapturedStderr()49   CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
50 };
51 
52 class CapturedStdout : public CapturedStdFd {
53  public:
CapturedStdout()54   CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
55 };
56 
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     }                                                                          \
66   } while (0)
67 
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     }                                                                                      \
74   } while (0)
75 
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     }                                                                                 \
82   } while (0)
83 
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     }                                                                                             \
90   } while (0)
91 
92 extern "C" void __hwasan_init() __attribute__((weak));
running_with_hwasan()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()
98