blob: b20f2784629a4f00560aabeb5795db09eeb4bee2 [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
Mark Salyzyn40a4cb42018-11-12 12:29:14 -080022#include <android-base/file.h>
Elliott Hughesb6351622015-12-04 22:00:26 -080023#include <android-base/macros.h>
Alex Valléed0ac74c2015-05-06 16:26:00 -040024
Elliott Hughes1c1409f2018-05-23 09:16:46 -070025class CapturedStdFd {
Wei Wangcaee0eb2016-10-21 09:23:39 -070026 public:
Elliott Hughes1c1409f2018-05-23 09:16:46 -070027 CapturedStdFd(int std_fd);
28 ~CapturedStdFd();
Wei Wangcaee0eb2016-10-21 09:23:39 -070029
Elliott Hughes1c1409f2018-05-23 09:16:46 -070030 std::string str();
Wei Wangcaee0eb2016-10-21 09:23:39 -070031
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070032 void Start();
33 void Stop();
Elliott Hughes1c1409f2018-05-23 09:16:46 -070034 void Reset();
Wei Wangcaee0eb2016-10-21 09:23:39 -070035
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070036 private:
37 int fd() const;
38
Wei Wangcaee0eb2016-10-21 09:23:39 -070039 TemporaryFile temp_file_;
Elliott Hughes1c1409f2018-05-23 09:16:46 -070040 int std_fd_;
Christopher Ferrisa6f0b672018-08-30 13:31:45 -070041 int old_fd_ = -1;
Wei Wangcaee0eb2016-10-21 09:23:39 -070042
Elliott Hughes1c1409f2018-05-23 09:16:46 -070043 DISALLOW_COPY_AND_ASSIGN(CapturedStdFd);
44};
45
46class CapturedStderr : public CapturedStdFd {
47 public:
48 CapturedStderr() : CapturedStdFd(STDERR_FILENO) {}
49};
50
51class CapturedStdout : public CapturedStdFd {
52 public:
53 CapturedStdout() : CapturedStdFd(STDOUT_FILENO) {}
Wei Wangcaee0eb2016-10-21 09:23:39 -070054};
55
Josh Gao8f1fa002017-04-27 19:48:44 -070056#define ASSERT_MATCH(str, pattern) \
57 do { \
58 if (!std::regex_search((str), std::regex((pattern)))) { \
59 FAIL() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
60 } \
61 } while (0)
62
63#define ASSERT_NOT_MATCH(str, pattern) \
64 do { \
65 if (std::regex_search((str), std::regex((pattern)))) { \
66 FAIL() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
67 } \
68 } while (0)
69
70#define EXPECT_MATCH(str, pattern) \
71 do { \
72 if (!std::regex_search((str), std::regex((pattern)))) { \
73 ADD_FAILURE() << "regex mismatch: expected " << (pattern) << " in:\n" << (str); \
74 } \
75 } while (0)
76
77#define EXPECT_NOT_MATCH(str, pattern) \
78 do { \
79 if (std::regex_search((str), std::regex((pattern)))) { \
80 ADD_FAILURE() << "regex mismatch: expected to not find " << (pattern) << " in:\n" << (str); \
81 } \
82 } while (0)