Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 1 | /* |
| 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 Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 17 | #include "android-base/test_utils.h" |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 18 | |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 25 | #if defined(_WIN32) |
| 26 | #include <windows.h> |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 27 | #include <direct.h> |
Elliott Hughes | 38e2b63 | 2016-02-17 11:53:54 -0800 | [diff] [blame] | 28 | #define OS_PATH_SEPARATOR '\\' |
| 29 | #else |
| 30 | #define OS_PATH_SEPARATOR '/' |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 31 | #endif |
| 32 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 33 | #include <string> |
| 34 | |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 35 | #include <android-base/file.h> |
| 36 | #include <android-base/logging.h> |
| 37 | |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 38 | #ifdef _WIN32 |
| 39 | int mkstemp(char* template_name) { |
| 40 | if (_mktemp(template_name) == nullptr) { |
| 41 | return -1; |
| 42 | } |
| 43 | // Use open() to match the close() that TemporaryFile's destructor does. |
Spencer Low | 2fbeb0c | 2015-09-01 14:57:58 -0700 | [diff] [blame] | 44 | // Use O_BINARY to match base file APIs. |
| 45 | return open(template_name, O_CREAT | O_EXCL | O_RDWR | O_BINARY, |
| 46 | S_IRUSR | S_IWUSR); |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | char* mkdtemp(char* template_name) { |
| 50 | if (_mktemp(template_name) == nullptr) { |
| 51 | return nullptr; |
| 52 | } |
| 53 | if (_mkdir(template_name) == -1) { |
| 54 | return nullptr; |
| 55 | } |
| 56 | return template_name; |
| 57 | } |
| 58 | #endif |
| 59 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 60 | static std::string GetSystemTempDir() { |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 61 | #if defined(__ANDROID__) |
Yabin Cui | 57e9cea | 2016-12-14 17:45:49 -0800 | [diff] [blame] | 62 | const char* tmpdir = "/data/local/tmp"; |
| 63 | if (access(tmpdir, R_OK | W_OK | X_OK) == 0) { |
| 64 | return tmpdir; |
| 65 | } |
| 66 | // Tests running in app context can't access /data/local/tmp, |
| 67 | // so try current directory if /data/local/tmp is not accessible. |
| 68 | return "."; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 69 | #elif defined(_WIN32) |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 70 | char tmp_dir[MAX_PATH]; |
| 71 | DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir); |
| 72 | CHECK_NE(result, 0ul) << "GetTempPathA failed, error: " << GetLastError(); |
| 73 | CHECK_LT(result, sizeof(tmp_dir)) << "path truncated to: " << result; |
| 74 | |
| 75 | // GetTempPath() returns a path with a trailing slash, but init() |
| 76 | // does not expect that, so remove it. |
| 77 | CHECK_EQ(tmp_dir[result - 1], '\\'); |
| 78 | tmp_dir[result - 1] = '\0'; |
| 79 | return tmp_dir; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 80 | #else |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 81 | return "/tmp"; |
Dan Albert | 0c4b3a3 | 2015-04-29 11:32:23 -0700 | [diff] [blame] | 82 | #endif |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 85 | TemporaryFile::TemporaryFile() { |
| 86 | init(GetSystemTempDir()); |
| 87 | } |
| 88 | |
Yabin Cui | 464ea61 | 2017-12-06 14:20:07 -0800 | [diff] [blame] | 89 | TemporaryFile::TemporaryFile(const std::string& tmp_dir) { |
| 90 | init(tmp_dir); |
| 91 | } |
| 92 | |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 93 | TemporaryFile::~TemporaryFile() { |
Tianjie Xu | f9bc1b0 | 2017-09-11 12:01:09 -0700 | [diff] [blame] | 94 | if (fd != -1) { |
| 95 | close(fd); |
| 96 | } |
Yabin Cui | ef58cef | 2018-03-08 17:03:04 -0800 | [diff] [blame] | 97 | if (remove_file_) { |
| 98 | unlink(path); |
| 99 | } |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Tianjie Xu | f9bc1b0 | 2017-09-11 12:01:09 -0700 | [diff] [blame] | 102 | int TemporaryFile::release() { |
| 103 | int result = fd; |
| 104 | fd = -1; |
| 105 | return result; |
| 106 | } |
| 107 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 108 | void TemporaryFile::init(const std::string& tmp_dir) { |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 109 | snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), |
| 110 | OS_PATH_SEPARATOR); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 111 | fd = mkstemp(path); |
Dan Albert | 58310b4 | 2015-03-13 23:06:01 -0700 | [diff] [blame] | 112 | } |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 113 | |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 114 | TemporaryDir::TemporaryDir() { |
| 115 | init(GetSystemTempDir()); |
| 116 | } |
| 117 | |
| 118 | TemporaryDir::~TemporaryDir() { |
| 119 | rmdir(path); |
| 120 | } |
| 121 | |
| 122 | bool TemporaryDir::init(const std::string& tmp_dir) { |
Spencer Low | 40d0c7a | 2015-07-31 20:21:35 -0700 | [diff] [blame] | 123 | snprintf(path, sizeof(path), "%s%cTemporaryDir-XXXXXX", tmp_dir.c_str(), |
| 124 | OS_PATH_SEPARATOR); |
Alex Vallée | 47d67c9 | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 125 | return (mkdtemp(path) != nullptr); |
| 126 | } |
Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 127 | |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 128 | CapturedStdFd::CapturedStdFd(int std_fd) : std_fd_(std_fd), old_fd_(-1) { |
Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 129 | Start(); |
Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 132 | CapturedStdFd::~CapturedStdFd() { |
Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 133 | if (old_fd_ != -1) { |
| 134 | Stop(); |
| 135 | } |
Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 138 | int CapturedStdFd::fd() const { |
Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 139 | return temp_file_.fd; |
| 140 | } |
| 141 | |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 142 | std::string CapturedStdFd::str() { |
| 143 | std::string result; |
| 144 | CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET))); |
| 145 | android::base::ReadFdToString(fd(), &result); |
| 146 | return result; |
| 147 | } |
| 148 | |
Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 149 | void CapturedStdFd::Reset() { |
| 150 | // Do not reset while capturing. |
| 151 | CHECK_EQ(-1, old_fd_); |
| 152 | CHECK_EQ(0, TEMP_FAILURE_RETRY(lseek(fd(), 0, SEEK_SET))); |
| 153 | CHECK_EQ(0, ftruncate(fd(), 0)); |
| 154 | } |
| 155 | |
| 156 | void CapturedStdFd::Start() { |
Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 157 | #if defined(_WIN32) |
| 158 | // On Windows, stderr is often buffered, so make sure it is unbuffered so |
| 159 | // that we can immediately read back what was written to stderr. |
Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 160 | if (std_fd_ == STDERR_FILENO) CHECK_EQ(0, setvbuf(stderr, nullptr, _IONBF, 0)); |
Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 161 | #endif |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 162 | old_fd_ = dup(std_fd_); |
| 163 | CHECK_NE(-1, old_fd_); |
| 164 | CHECK_NE(-1, dup2(fd(), std_fd_)); |
Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 167 | void CapturedStdFd::Stop() { |
| 168 | CHECK_NE(-1, old_fd_); |
Elliott Hughes | 1be0d14 | 2018-05-23 09:16:46 -0700 | [diff] [blame] | 169 | CHECK_NE(-1, dup2(old_fd_, std_fd_)); |
Christopher Ferris | eea85c9 | 2018-08-30 13:31:45 -0700 | [diff] [blame^] | 170 | close(old_fd_); |
| 171 | old_fd_ = -1; |
Wei Wang | 8c17630 | 2016-10-21 09:23:39 -0700 | [diff] [blame] | 172 | // Note: cannot restore prior setvbuf() setting. |
| 173 | } |