blob: 9e2ea97f2358628eeaec50c1727009f19d32a84f [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>
21
Elliott Hughesb6351622015-12-04 22:00:26 -080022#include <android-base/macros.h>
Alex Valléed0ac74c2015-05-06 16:26:00 -040023
Dan Alberte3ea0582015-03-13 23:06:01 -070024class TemporaryFile {
25 public:
26 TemporaryFile();
Yabin Cuia11c57e2017-12-06 14:20:07 -080027 explicit TemporaryFile(const std::string& tmp_dir);
Dan Alberte3ea0582015-03-13 23:06:01 -070028 ~TemporaryFile();
29
Tianjie Xu1852b802017-09-11 12:01:09 -070030 // Release the ownership of fd, caller is reponsible for closing the
31 // fd or stream properly.
32 int release();
Yabin Cuib7abae12018-03-08 17:03:04 -080033 // Don't remove the temporary file in the destructor.
34 void DoNotRemove() { remove_file_ = false; }
Tianjie Xu1852b802017-09-11 12:01:09 -070035
Dan Alberte3ea0582015-03-13 23:06:01 -070036 int fd;
Alex Valléed0ac74c2015-05-06 16:26:00 -040037 char path[1024];
Dan Alberte3ea0582015-03-13 23:06:01 -070038
39 private:
Alex Valléed0ac74c2015-05-06 16:26:00 -040040 void init(const std::string& tmp_dir);
41
Yabin Cuib7abae12018-03-08 17:03:04 -080042 bool remove_file_ = true;
43
Alex Valléed0ac74c2015-05-06 16:26:00 -040044 DISALLOW_COPY_AND_ASSIGN(TemporaryFile);
Dan Alberte3ea0582015-03-13 23:06:01 -070045};
46
Alex Valléed0ac74c2015-05-06 16:26:00 -040047class 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éed0ac74c2015-05-06 16:26:00 -040059
Elliott Hughes1c1409f2018-05-23 09:16:46 -070060class CapturedStdFd {
Wei Wangcaee0eb2016-10-21 09:23:39 -070061 public:
Elliott Hughes1c1409f2018-05-23 09:16:46 -070062 CapturedStdFd(int std_fd);
63 ~CapturedStdFd();
Wei Wangcaee0eb2016-10-21 09:23:39 -070064
65 int fd() const;
Elliott Hughes1c1409f2018-05-23 09:16:46 -070066 std::string str();
Wei Wangcaee0eb2016-10-21 09:23:39 -070067
68 private:
Elliott Hughes1c1409f2018-05-23 09:16:46 -070069 void Init();
70 void Reset();
Wei Wangcaee0eb2016-10-21 09:23:39 -070071
72 TemporaryFile temp_file_;
Elliott Hughes1c1409f2018-05-23 09:16:46 -070073 int std_fd_;
74 int old_fd_;
Wei Wangcaee0eb2016-10-21 09:23:39 -070075
Elliott Hughes1c1409f2018-05-23 09:16:46 -070076 DISALLOW_COPY_AND_ASSIGN(CapturedStdFd);
77};
78
79class CapturedStderr : public CapturedStdFd {
80 public:
81 CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
82};
83
84class CapturedStdout : public CapturedStdFd {
85 public:
86 CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
Wei Wangcaee0eb2016-10-21 09:23:39 -070087};
88
Josh Gao8f1fa002017-04-27 19:48:44 -070089#define ASSERT_MATCH(str, pattern) \
90 do { \
91 if (!std::regex_search((str), std::regex((pattern)))) { \
92 FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
93 } \
94 } while (0)
95
96#define ASSERT_NOT_MATCH(str, pattern) \
97 do { \
98 if (std::regex_search((str), std::regex((pattern)))) { \
99 FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
100 } \
101 } while (0)
102
103#define EXPECT_MATCH(str, pattern) \
104 do { \
105 if (!std::regex_search((str), std::regex((pattern)))) { \
106 ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
107 } \
108 } while (0)
109
110#define EXPECT_NOT_MATCH(str, pattern) \
111 do { \
112 if (std::regex_search((str), std::regex((pattern)))) { \
113 ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
114 } \
115 } while (0)