blob: 2abe68e0268778c95d735ed3db7c89071680865e [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
Elliott Hughes1c1409f2018-05-23 09:16:46 -070065 std::string str();
Wei Wangcaee0eb2016-10-21 09:23:39 -070066
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070067 void Start();
68 void Stop();
Elliott Hughes1c1409f2018-05-23 09:16:46 -070069 void Reset();
Wei Wangcaee0eb2016-10-21 09:23:39 -070070
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070071 private:
72 int fd() const;
73
Wei Wangcaee0eb2016-10-21 09:23:39 -070074 TemporaryFile temp_file_;
Elliott Hughes1c1409f2018-05-23 09:16:46 -070075 int std_fd_;
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070076 int old_fd_ = -1;
Wei Wangcaee0eb2016-10-21 09:23:39 -070077
Elliott Hughes1c1409f2018-05-23 09:16:46 -070078 DISALLOW_COPY_AND_ASSIGN(CapturedStdFd);
79};
80
81class CapturedStderr : public CapturedStdFd {
82 public:
83 CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
84};
85
86class CapturedStdout : public CapturedStdFd {
87 public:
88 CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
Wei Wangcaee0eb2016-10-21 09:23:39 -070089};
90
Josh Gao8f1fa002017-04-27 19:48:44 -070091#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)