summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/include/android-base/test_utils.h17
-rw-r--r--base/logging_test.cpp36
-rw-r--r--base/test_utils.cpp29
3 files changed, 46 insertions, 36 deletions
diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h
index 4ea3c8e48..c0bf0c1e1 100644
--- a/base/include/android-base/test_utils.h
+++ b/base/include/android-base/test_utils.h
@@ -48,4 +48,21 @@ class TemporaryDir {
48 DISALLOW_COPY_AND_ASSIGN(TemporaryDir); 48 DISALLOW_COPY_AND_ASSIGN(TemporaryDir);
49}; 49};
50 50
51class CapturedStderr {
52 public:
53 CapturedStderr();
54 ~CapturedStderr();
55
56 int fd() const;
57
58 private:
59 void init();
60 void reset();
61
62 TemporaryFile temp_file_;
63 int old_stderr_;
64
65 DISALLOW_COPY_AND_ASSIGN(CapturedStderr);
66};
67
51#endif // ANDROID_BASE_TEST_UTILS_H 68#endif // ANDROID_BASE_TEST_UTILS_H
diff --git a/base/logging_test.cpp b/base/logging_test.cpp
index 1ee181a42..2d9c2baec 100644
--- a/base/logging_test.cpp
+++ b/base/logging_test.cpp
@@ -37,42 +37,6 @@
37#define HOST_TEST(suite, name) TEST(suite, name) 37#define HOST_TEST(suite, name) TEST(suite, name)
38#endif 38#endif
39 39
40class CapturedStderr {
41 public:
42 CapturedStderr() : old_stderr_(-1) {
43 init();
44 }
45
46 ~CapturedStderr() {
47 reset();
48 }
49
50 int fd() const {
51 return temp_file_.fd;
52 }
53
54 private:
55 void init() {
56#if defined(_WIN32)
57 // On Windows, stderr is often buffered, so make sure it is unbuffered so
58 // that we can immediately read back what was written to stderr.
59 ASSERT_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
60#endif
61 old_stderr_ = dup(STDERR_FILENO);
62 ASSERT_NE(-1, old_stderr_);
63 ASSERT_NE(-1, dup2(fd(), STDERR_FILENO));
64 }
65
66 void reset() {
67 ASSERT_NE(-1, dup2(old_stderr_, STDERR_FILENO));
68 ASSERT_EQ(0, close(old_stderr_));
69 // Note: cannot restore prior setvbuf() setting.
70 }
71
72 TemporaryFile temp_file_;
73 int old_stderr_;
74};
75
76#if defined(_WIN32) 40#if defined(_WIN32)
77static void ExitSignalAbortHandler(int) { 41static void ExitSignalAbortHandler(int) {
78 _exit(3); 42 _exit(3);
diff --git a/base/test_utils.cpp b/base/test_utils.cpp
index 635af6c6e..3b3d698cc 100644
--- a/base/test_utils.cpp
+++ b/base/test_utils.cpp
@@ -102,3 +102,32 @@ bool TemporaryDir::init(const std::string& tmp_dir) {
102 OS_PATH_SEPARATOR); 102 OS_PATH_SEPARATOR);
103 return (mkdtemp(path) != nullptr); 103 return (mkdtemp(path) != nullptr);
104} 104}
105
106CapturedStderr::CapturedStderr() : old_stderr_(-1) {
107 init();
108}
109
110CapturedStderr::~CapturedStderr() {
111 reset();
112}
113
114int CapturedStderr::fd() const {
115 return temp_file_.fd;
116}
117
118void CapturedStderr::init() {
119#if defined(_WIN32)
120 // On Windows, stderr is often buffered, so make sure it is unbuffered so
121 // that we can immediately read back what was written to stderr.
122 CHECK_EQ(0, setvbuf(stderr, NULL, _IONBF, 0));
123#endif
124 old_stderr_ = dup(STDERR_FILENO);
125 CHECK_NE(-1, old_stderr_);
126 CHECK_NE(-1, dup2(fd(), STDERR_FILENO));
127}
128
129void CapturedStderr::reset() {
130 CHECK_NE(-1, dup2(old_stderr_, STDERR_FILENO));
131 CHECK_EQ(0, close(old_stderr_));
132 // Note: cannot restore prior setvbuf() setting.
133}