David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_ |
| 18 | #define ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_ |
| 19 | |
| 20 | #include <gtest/gtest.h> |
| 21 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 22 | #include <functional> |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 25 | #include <sys/wait.h> |
| 26 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 27 | #include <android-base/logging.h> |
| 28 | |
Roland Levillain | fb6a5c0 | 2019-03-29 20:20:16 +0000 | [diff] [blame^] | 29 | #include "base/file_utils.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 30 | #include "base/globals.h" |
| 31 | #include "base/mutex.h" |
| 32 | #include "base/os.h" |
| 33 | #include "base/unix_file/fd_file.h" |
| 34 | #include "dex/art_dex_file_loader.h" |
| 35 | #include "dex/compact_dex_level.h" |
| 36 | |
| 37 | namespace art { |
| 38 | |
| 39 | using LogSeverity = android::base::LogSeverity; |
| 40 | using ScopedLogSeverity = android::base::ScopedLogSeverity; |
| 41 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 42 | class DexFile; |
| 43 | |
| 44 | class ScratchFile { |
| 45 | public: |
| 46 | ScratchFile(); |
| 47 | |
| 48 | explicit ScratchFile(const std::string& filename); |
| 49 | |
| 50 | ScratchFile(const ScratchFile& other, const char* suffix); |
| 51 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 52 | ScratchFile(ScratchFile&& other) noexcept; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 53 | |
Andreas Gampe | 44b3174 | 2018-10-01 19:30:57 -0700 | [diff] [blame] | 54 | ScratchFile& operator=(ScratchFile&& other) noexcept; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 55 | |
| 56 | explicit ScratchFile(File* file); |
| 57 | |
| 58 | ~ScratchFile(); |
| 59 | |
| 60 | const std::string& GetFilename() const { |
| 61 | return filename_; |
| 62 | } |
| 63 | |
| 64 | File* GetFile() const { |
| 65 | return file_.get(); |
| 66 | } |
| 67 | |
| 68 | int GetFd() const; |
| 69 | |
| 70 | void Close(); |
| 71 | void Unlink(); |
| 72 | |
| 73 | private: |
| 74 | std::string filename_; |
| 75 | std::unique_ptr<File> file_; |
| 76 | }; |
| 77 | |
| 78 | class CommonArtTestImpl { |
| 79 | public: |
| 80 | CommonArtTestImpl() = default; |
| 81 | virtual ~CommonArtTestImpl() = default; |
| 82 | |
Neil Fuller | 26a5dd6 | 2019-03-13 15:16:35 +0000 | [diff] [blame] | 83 | // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_RUNTIME_ROOT, |
| 84 | // and ANDROID_TZDATA_ROOT environment variables using sensible defaults if not already set. |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 85 | static void SetUpAndroidRootEnvVars(); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 86 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 87 | // Set up the ANDROID_DATA environment variable, creating the directory if required. |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 88 | // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a |
| 89 | // non-derived class, be sure to also call the corresponding tear-down below. |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 90 | static void SetUpAndroidDataDir(std::string& android_data); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 91 | |
Neil Fuller | 26c4377 | 2018-11-23 17:56:43 +0000 | [diff] [blame] | 92 | static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 93 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 94 | // Get the names of the libcore modules. |
| 95 | virtual std::vector<std::string> GetLibCoreModuleNames() const; |
| 96 | |
| 97 | // Gets the paths of the libcore dex files for given modules. |
| 98 | std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const; |
| 99 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 100 | // Gets the paths of the libcore dex files. |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 101 | std::vector<std::string> GetLibCoreDexFileNames() const; |
| 102 | |
| 103 | // Gets the locations of the libcore dex files for given modules. |
| 104 | std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const; |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 105 | |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 106 | // Gets the locations of the libcore dex files. |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 107 | std::vector<std::string> GetLibCoreDexLocations() const; |
Vladimir Marko | 7a85e70 | 2018-12-03 18:47:23 +0000 | [diff] [blame] | 108 | |
| 109 | static std::string GetClassPathOption(const char* option, |
| 110 | const std::vector<std::string>& class_path); |
| 111 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 112 | // Returns bin directory which contains host's prebuild tools. |
| 113 | static std::string GetAndroidHostToolsDir(); |
| 114 | |
| 115 | // Retuerns the filename for a test dex (i.e. XandY or ManyMethods). |
| 116 | std::string GetTestDexFileName(const char* name) const; |
| 117 | |
| 118 | template <typename Mutator> |
| 119 | bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) { |
| 120 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 121 | std::string error_msg; |
| 122 | const ArtDexFileLoader dex_file_loader; |
| 123 | CHECK(dex_file_loader.Open(input_jar.c_str(), |
| 124 | input_jar.c_str(), |
| 125 | /*verify*/ true, |
| 126 | /*verify_checksum*/ true, |
| 127 | &error_msg, |
| 128 | &dex_files)) << error_msg; |
| 129 | EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported"; |
| 130 | const std::unique_ptr<const DexFile>& dex = dex_files[0]; |
| 131 | CHECK(dex->EnableWrite()) << "Failed to enable write"; |
| 132 | DexFile* dex_file = const_cast<DexFile*>(dex.get()); |
| 133 | mutator(dex_file); |
| 134 | const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum(); |
| 135 | if (!output_dex->WriteFully(dex->Begin(), dex->Size())) { |
| 136 | return false; |
| 137 | } |
| 138 | if (output_dex->Flush() != 0) { |
| 139 | PLOG(FATAL) << "Could not flush the output file."; |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 144 | struct ForkAndExecResult { |
| 145 | enum Stage { |
| 146 | kLink, |
| 147 | kFork, |
| 148 | kWaitpid, |
| 149 | kFinished, |
| 150 | }; |
| 151 | Stage stage; |
| 152 | int status_code; |
| 153 | |
| 154 | bool StandardSuccess() { |
| 155 | return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0; |
| 156 | } |
| 157 | }; |
| 158 | using OutputHandlerFn = std::function<void(char*, size_t)>; |
| 159 | using PostForkFn = std::function<bool()>; |
| 160 | static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv, |
| 161 | const PostForkFn& post_fork, |
| 162 | const OutputHandlerFn& handler); |
| 163 | static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv, |
| 164 | const PostForkFn& post_fork, |
| 165 | std::string* output); |
| 166 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 167 | protected: |
| 168 | static bool IsHost() { |
| 169 | return !kIsTargetBuild; |
| 170 | } |
| 171 | |
| 172 | // Helper - find directory with the following format: |
| 173 | // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/ |
| 174 | static std::string GetAndroidToolsDir(const std::string& subdir1, |
| 175 | const std::string& subdir2, |
| 176 | const std::string& subdir3); |
| 177 | |
| 178 | // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art |
| 179 | static std::string GetCoreArtLocation(); |
| 180 | |
| 181 | // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat |
| 182 | static std::string GetCoreOatLocation(); |
| 183 | |
| 184 | std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location); |
| 185 | |
| 186 | void ClearDirectory(const char* dirpath, bool recursive = true); |
| 187 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 188 | // Open a file (allows reading of framework jars). |
| 189 | std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename); |
Mathieu Chartier | c2109c6 | 2019-03-20 13:34:39 -0700 | [diff] [blame] | 190 | |
| 191 | // Open a single dex file (aborts if there are more than one). |
| 192 | std::unique_ptr<const DexFile> OpenDexFile(const char* filename); |
| 193 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 194 | // Open a test file (art-gtest-*.jar). |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 195 | std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name); |
| 196 | |
| 197 | std::unique_ptr<const DexFile> OpenTestDexFile(const char* name); |
| 198 | |
| 199 | |
| 200 | std::string android_data_; |
| 201 | std::string dalvik_cache_; |
| 202 | |
| 203 | virtual void SetUp(); |
| 204 | |
| 205 | virtual void TearDown(); |
| 206 | |
| 207 | // Creates the class path string for the given dex files (the list of dex file locations |
| 208 | // separated by ':'). |
| 209 | std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files); |
| 210 | // Same as CreateClassPath but add the dex file checksum after each location. The separator |
| 211 | // is '*'. |
| 212 | std::string CreateClassPathWithChecksums( |
| 213 | const std::vector<std::unique_ptr<const DexFile>>& dex_files); |
| 214 | |
| 215 | static std::string GetCoreFileLocation(const char* suffix); |
| 216 | |
| 217 | std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_; |
| 218 | }; |
| 219 | |
| 220 | template <typename TestType> |
| 221 | class CommonArtTestBase : public TestType, public CommonArtTestImpl { |
| 222 | public: |
| 223 | CommonArtTestBase() {} |
| 224 | virtual ~CommonArtTestBase() {} |
| 225 | |
| 226 | protected: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 227 | void SetUp() override { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 228 | CommonArtTestImpl::SetUp(); |
| 229 | } |
| 230 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 231 | void TearDown() override { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 232 | CommonArtTestImpl::TearDown(); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | using CommonArtTest = CommonArtTestBase<testing::Test>; |
| 237 | |
| 238 | template <typename Param> |
| 239 | using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>; |
| 240 | |
| 241 | #define TEST_DISABLED_FOR_TARGET() \ |
| 242 | if (kIsTargetBuild) { \ |
| 243 | printf("WARNING: TEST DISABLED FOR TARGET\n"); \ |
| 244 | return; \ |
| 245 | } |
| 246 | |
| 247 | #define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \ |
| 248 | if (!kHostStaticBuildEnabled) { \ |
| 249 | printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \ |
| 250 | return; \ |
| 251 | } |
| 252 | |
| 253 | #define TEST_DISABLED_FOR_MEMORY_TOOL() \ |
Roland Levillain | 05e34f4 | 2018-05-24 13:19:05 +0000 | [diff] [blame] | 254 | if (kRunningOnMemoryTool) { \ |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 255 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \ |
| 256 | return; \ |
| 257 | } |
| 258 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 259 | #define TEST_DISABLED_FOR_HEAP_POISONING() \ |
| 260 | if (kPoisonHeapReferences) { \ |
| 261 | printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \ |
| 262 | return; \ |
| 263 | } |
| 264 | } // namespace art |
| 265 | |
Roland Levillain | ab4326e | 2018-06-19 18:10:55 +0000 | [diff] [blame] | 266 | #define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \ |
| 267 | if (kRunningOnMemoryTool && kPoisonHeapReferences) { \ |
| 268 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \ |
| 269 | return; \ |
| 270 | } |
| 271 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 272 | #endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_ |