Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 17 | #ifndef __TEST_UTILS_H |
| 18 | #define __TEST_UTILS_H |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 19 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 20 | #include <inttypes.h> |
| 21 | #include <sys/mman.h> |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <sys/wait.h> |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <atomic> |
| 27 | #include <string> |
| 28 | #include <regex> |
| 29 | |
Elliott Hughes | 939a7e0 | 2015-12-04 15:27:46 -0800 | [diff] [blame] | 30 | #include <android-base/file.h> |
| 31 | #include <android-base/stringprintf.h> |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 32 | |
| 33 | #include "private/ScopeGuard.h" |
| 34 | |
Josh Gao | 2f06e10 | 2017-01-10 13:00:37 -0800 | [diff] [blame] | 35 | #if defined(__BIONIC__) |
| 36 | #define KNOWN_FAILURE_ON_BIONIC(x) xfail_ ## x |
| 37 | #else |
| 38 | #define KNOWN_FAILURE_ON_BIONIC(x) x |
| 39 | #endif |
| 40 | |
Elliott Hughes | ffbb0f8 | 2016-10-10 18:34:27 -0700 | [diff] [blame] | 41 | #if defined(__linux__) |
| 42 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 43 | struct map_record { |
| 44 | uintptr_t addr_start; |
| 45 | uintptr_t addr_end; |
| 46 | |
| 47 | int perms; |
| 48 | |
| 49 | size_t offset; |
| 50 | |
| 51 | dev_t device; |
| 52 | ino_t inode; |
| 53 | |
| 54 | std::string pathname; |
| 55 | }; |
| 56 | |
| 57 | class Maps { |
| 58 | public: |
| 59 | static bool parse_maps(std::vector<map_record>* maps) { |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 60 | FILE* fp = fopen("/proc/self/maps", "re"); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 61 | if (fp == nullptr) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | auto fp_guard = make_scope_guard([&]() { |
| 66 | fclose(fp); |
| 67 | }); |
| 68 | |
| 69 | char line[BUFSIZ]; |
| 70 | while (fgets(line, sizeof(line), fp) != nullptr) { |
| 71 | map_record record; |
Dmitriy Ivanov | 1dce3ed | 2015-04-06 19:05:58 -0700 | [diff] [blame] | 72 | uint32_t dev_major, dev_minor; |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 73 | int path_offset; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 74 | char prot[5]; // sizeof("rwxp") |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 75 | if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR " %4s %" SCNxPTR " %x:%x %lu %n", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 76 | &record.addr_start, &record.addr_end, prot, &record.offset, |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 77 | &dev_major, &dev_minor, &record.inode, &path_offset) == 7) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 78 | record.perms = 0; |
| 79 | if (prot[0] == 'r') { |
| 80 | record.perms |= PROT_READ; |
| 81 | } |
| 82 | if (prot[1] == 'w') { |
| 83 | record.perms |= PROT_WRITE; |
| 84 | } |
| 85 | if (prot[2] == 'x') { |
| 86 | record.perms |= PROT_EXEC; |
| 87 | } |
| 88 | |
| 89 | // TODO: parse shared/private? |
| 90 | |
| 91 | record.device = makedev(dev_major, dev_minor); |
Elliott Hughes | 15dfd63 | 2015-09-22 16:40:14 -0700 | [diff] [blame] | 92 | record.pathname = line + path_offset; |
| 93 | if (!record.pathname.empty() && record.pathname.back() == '\n') { |
| 94 | record.pathname.pop_back(); |
| 95 | } |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 96 | maps->push_back(record); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | }; |
| 103 | |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 104 | extern "C" pid_t gettid(); |
| 105 | |
Elliott Hughes | ffbb0f8 | 2016-10-10 18:34:27 -0700 | [diff] [blame] | 106 | #endif |
| 107 | |
Yabin Cui | 76144aa | 2015-11-19 13:52:16 -0800 | [diff] [blame] | 108 | static inline void WaitUntilThreadSleep(std::atomic<pid_t>& tid) { |
| 109 | while (tid == 0) { |
| 110 | usleep(1000); |
| 111 | } |
| 112 | std::string filename = android::base::StringPrintf("/proc/%d/stat", tid.load()); |
| 113 | std::regex regex {R"(\s+S\s+)"}; |
| 114 | |
| 115 | while (true) { |
| 116 | std::string content; |
| 117 | ASSERT_TRUE(android::base::ReadFileToString(filename, &content)); |
| 118 | if (std::regex_search(content, regex)) { |
| 119 | break; |
| 120 | } |
| 121 | usleep(1000); |
| 122 | } |
| 123 | } |
| 124 | |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 125 | static inline void AssertChildExited(int pid, int expected_exit_status) { |
| 126 | int status; |
| 127 | ASSERT_EQ(pid, waitpid(pid, &status, 0)); |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame^] | 128 | if (expected_exit_status >= 0) { |
| 129 | ASSERT_TRUE(WIFEXITED(status)); |
| 130 | ASSERT_EQ(expected_exit_status, WEXITSTATUS(status)); |
| 131 | } else { |
| 132 | ASSERT_TRUE(WIFSIGNALED(status)); |
| 133 | ASSERT_EQ(-expected_exit_status, WTERMSIG(status)); |
| 134 | } |
Elliott Hughes | 33697a0 | 2016-01-26 13:04:57 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Dimitry Ivanov | 2ba1cf3 | 2016-05-17 13:29:37 -0700 | [diff] [blame] | 137 | // The absolute path to the executable |
| 138 | const std::string& get_executable_path(); |
Dimitry Ivanov | d17a377 | 2016-03-01 13:11:28 -0800 | [diff] [blame] | 139 | |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 140 | // Get realpath |
| 141 | bool get_realpath(const std::string& path, std::string* realpath); |
Dimitry Ivanov | d0b5c3a | 2016-11-25 12:23:11 -0800 | [diff] [blame] | 142 | // Get dirname |
| 143 | std::string get_dirname(const char* path); |
Dimitry Ivanov | a36e59b | 2016-09-01 11:37:39 -0700 | [diff] [blame] | 144 | |
Dimitry Ivanov | 5543746 | 2016-07-20 15:33:07 -0700 | [diff] [blame] | 145 | // Access to argc/argv/envp |
| 146 | int get_argc(); |
| 147 | char** get_argv(); |
| 148 | char** get_envp(); |
| 149 | |
Evgenii Stepanov | 68ecec1 | 2017-01-31 13:19:30 -0800 | [diff] [blame^] | 150 | // ExecTestHelper is only used in bionic and glibc tests. |
| 151 | #ifndef __APPLE__ |
| 152 | class ExecTestHelper { |
| 153 | public: |
| 154 | char** GetArgs() { |
| 155 | return const_cast<char**>(args_.data()); |
| 156 | } |
| 157 | char** GetEnv() { |
| 158 | return const_cast<char**>(env_.data()); |
| 159 | } |
| 160 | |
| 161 | void SetArgs(const std::vector<const char*> args) { |
| 162 | args_ = args; |
| 163 | } |
| 164 | void SetEnv(const std::vector<const char*> env) { |
| 165 | env_ = env; |
| 166 | } |
| 167 | |
| 168 | void Run(const std::function<void()>& child_fn, int expected_exit_status, |
| 169 | const char* expected_output) { |
| 170 | int fds[2]; |
| 171 | ASSERT_NE(pipe(fds), -1); |
| 172 | |
| 173 | pid_t pid = fork(); |
| 174 | ASSERT_NE(pid, -1); |
| 175 | |
| 176 | if (pid == 0) { |
| 177 | // Child. |
| 178 | close(fds[0]); |
| 179 | dup2(fds[1], STDOUT_FILENO); |
| 180 | dup2(fds[1], STDERR_FILENO); |
| 181 | if (fds[1] != STDOUT_FILENO && fds[1] != STDERR_FILENO) close(fds[1]); |
| 182 | child_fn(); |
| 183 | FAIL(); |
| 184 | } |
| 185 | |
| 186 | // Parent. |
| 187 | close(fds[1]); |
| 188 | std::string output; |
| 189 | char buf[BUFSIZ]; |
| 190 | ssize_t bytes_read; |
| 191 | while ((bytes_read = TEMP_FAILURE_RETRY(read(fds[0], buf, sizeof(buf)))) > 0) { |
| 192 | output.append(buf, bytes_read); |
| 193 | } |
| 194 | close(fds[0]); |
| 195 | |
| 196 | AssertChildExited(pid, expected_exit_status); |
| 197 | if (expected_output != nullptr) { |
| 198 | ASSERT_EQ(expected_output, output); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private: |
| 203 | std::vector<const char*> args_; |
| 204 | std::vector<const char*> env_; |
| 205 | }; |
| 206 | #endif |
| 207 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 208 | #endif |