xref: /aosp_15_r20/system/libbase/test_utils.cpp (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/test_utils.h"
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include <fcntl.h>
20*8f0ba417SAndroid Build Coastguard Worker #include <stdio.h>
21*8f0ba417SAndroid Build Coastguard Worker #include <stdlib.h>
22*8f0ba417SAndroid Build Coastguard Worker #include <sys/stat.h>
23*8f0ba417SAndroid Build Coastguard Worker #include <unistd.h>
24*8f0ba417SAndroid Build Coastguard Worker 
25*8f0ba417SAndroid Build Coastguard Worker #include <string>
26*8f0ba417SAndroid Build Coastguard Worker 
27*8f0ba417SAndroid Build Coastguard Worker #include <android-base/file.h>
28*8f0ba417SAndroid Build Coastguard Worker #include <android-base/logging.h>
29*8f0ba417SAndroid Build Coastguard Worker 
CapturedStdFd(int std_fd)30*8f0ba417SAndroid Build Coastguard Worker CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) {
31*8f0ba417SAndroid Build Coastguard Worker   Start();
32*8f0ba417SAndroid Build Coastguard Worker }
33*8f0ba417SAndroid Build Coastguard Worker 
~CapturedStdFd()34*8f0ba417SAndroid Build Coastguard Worker CapturedStdFd::~CapturedStdFd() {
35*8f0ba417SAndroid Build Coastguard Worker   if (old_fd_ != -1) {
36*8f0ba417SAndroid Build Coastguard Worker     Stop();
37*8f0ba417SAndroid Build Coastguard Worker   }
38*8f0ba417SAndroid Build Coastguard Worker }
39*8f0ba417SAndroid Build Coastguard Worker 
fd() const40*8f0ba417SAndroid Build Coastguard Worker int CapturedStdFd::fd() const {
41*8f0ba417SAndroid Build Coastguard Worker   return temp_file_.fd;
42*8f0ba417SAndroid Build Coastguard Worker }
43*8f0ba417SAndroid Build Coastguard Worker 
str()44*8f0ba417SAndroid Build Coastguard Worker std::string CapturedStdFd::str() {
45*8f0ba417SAndroid Build Coastguard Worker   std::string result;
46*8f0ba417SAndroid Build Coastguard Worker   CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET)));
47*8f0ba417SAndroid Build Coastguard Worker   android::base::ReadFdToString(fd(), &result);
48*8f0ba417SAndroid Build Coastguard Worker   return result;
49*8f0ba417SAndroid Build Coastguard Worker }
50*8f0ba417SAndroid Build Coastguard Worker 
Reset()51*8f0ba417SAndroid Build Coastguard Worker void CapturedStdFd::Reset() {
52*8f0ba417SAndroid Build Coastguard Worker   // Do not reset while capturing.
53*8f0ba417SAndroid Build Coastguard Worker   CHECK_EQ(-1, old_fd_);
54*8f0ba417SAndroid Build Coastguard Worker   CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET)));
55*8f0ba417SAndroid Build Coastguard Worker   CHECK_EQ(0, ftruncate(fd(), 0));
56*8f0ba417SAndroid Build Coastguard Worker }
57*8f0ba417SAndroid Build Coastguard Worker 
Start()58*8f0ba417SAndroid Build Coastguard Worker void CapturedStdFd::Start() {
59*8f0ba417SAndroid Build Coastguard Worker #if defined(_WIN32)
60*8f0ba417SAndroid Build Coastguard Worker   // On Windows, stderr is often buffered, so make sure it is unbuffered so
61*8f0ba417SAndroid Build Coastguard Worker   // that we can immediately read back what was written to stderr.
62*8f0ba417SAndroid Build Coastguard Worker   if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, nullptr, _IONBF, 0));
63*8f0ba417SAndroid Build Coastguard Worker #endif
64*8f0ba417SAndroid Build Coastguard Worker   old_fd_ = dup(std_fd_);
65*8f0ba417SAndroid Build Coastguard Worker   CHECK_NE(-1, old_fd_);
66*8f0ba417SAndroid Build Coastguard Worker   CHECK_NE(-1, dup2(fd(), std_fd_));
67*8f0ba417SAndroid Build Coastguard Worker }
68*8f0ba417SAndroid Build Coastguard Worker 
Stop()69*8f0ba417SAndroid Build Coastguard Worker void CapturedStdFd::Stop() {
70*8f0ba417SAndroid Build Coastguard Worker   CHECK_NE(-1, old_fd_);
71*8f0ba417SAndroid Build Coastguard Worker   CHECK_NE(-1, dup2(old_fd_, std_fd_));
72*8f0ba417SAndroid Build Coastguard Worker   close(old_fd_);
73*8f0ba417SAndroid Build Coastguard Worker   old_fd_ = -1;
74*8f0ba417SAndroid Build Coastguard Worker   // Note: cannot restore prior setvbuf() setting.
75*8f0ba417SAndroid Build Coastguard Worker }
76