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