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 | |
| 22 | #include <string> |
| 23 | |
| 24 | #include <android-base/logging.h> |
| 25 | |
| 26 | #include "base/globals.h" |
| 27 | #include "base/mutex.h" |
| 28 | #include "base/os.h" |
| 29 | #include "base/unix_file/fd_file.h" |
| 30 | #include "dex/art_dex_file_loader.h" |
| 31 | #include "dex/compact_dex_level.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | using LogSeverity = android::base::LogSeverity; |
| 36 | using ScopedLogSeverity = android::base::ScopedLogSeverity; |
| 37 | |
| 38 | // OBJ pointer helpers to avoid needing .Decode everywhere. |
| 39 | #define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 40 | #define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 41 | #define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 42 | #define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 43 | |
| 44 | class DexFile; |
| 45 | |
| 46 | class ScratchFile { |
| 47 | public: |
| 48 | ScratchFile(); |
| 49 | |
| 50 | explicit ScratchFile(const std::string& filename); |
| 51 | |
| 52 | ScratchFile(const ScratchFile& other, const char* suffix); |
| 53 | |
| 54 | ScratchFile(ScratchFile&& other); |
| 55 | |
| 56 | ScratchFile& operator=(ScratchFile&& other); |
| 57 | |
| 58 | explicit ScratchFile(File* file); |
| 59 | |
| 60 | ~ScratchFile(); |
| 61 | |
| 62 | const std::string& GetFilename() const { |
| 63 | return filename_; |
| 64 | } |
| 65 | |
| 66 | File* GetFile() const { |
| 67 | return file_.get(); |
| 68 | } |
| 69 | |
| 70 | int GetFd() const; |
| 71 | |
| 72 | void Close(); |
| 73 | void Unlink(); |
| 74 | |
| 75 | private: |
| 76 | std::string filename_; |
| 77 | std::unique_ptr<File> file_; |
| 78 | }; |
| 79 | |
| 80 | class CommonArtTestImpl { |
| 81 | public: |
| 82 | CommonArtTestImpl() = default; |
| 83 | virtual ~CommonArtTestImpl() = default; |
| 84 | |
| 85 | static void SetUpAndroidRoot(); |
| 86 | |
| 87 | // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a |
| 88 | // non-derived class, be sure to also call the corresponding tear-down below. |
| 89 | static void SetUpAndroidData(std::string& android_data); |
| 90 | |
| 91 | static void TearDownAndroidData(const std::string& android_data, bool fail_on_error); |
| 92 | |
| 93 | // Gets the paths of the libcore dex files. |
| 94 | static std::vector<std::string> GetLibCoreDexFileNames(); |
| 95 | |
| 96 | // Returns bin directory which contains host's prebuild tools. |
| 97 | static std::string GetAndroidHostToolsDir(); |
| 98 | |
| 99 | // Retuerns the filename for a test dex (i.e. XandY or ManyMethods). |
| 100 | std::string GetTestDexFileName(const char* name) const; |
| 101 | |
| 102 | template <typename Mutator> |
| 103 | bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) { |
| 104 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 105 | std::string error_msg; |
| 106 | const ArtDexFileLoader dex_file_loader; |
| 107 | CHECK(dex_file_loader.Open(input_jar.c_str(), |
| 108 | input_jar.c_str(), |
| 109 | /*verify*/ true, |
| 110 | /*verify_checksum*/ true, |
| 111 | &error_msg, |
| 112 | &dex_files)) << error_msg; |
| 113 | EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported"; |
| 114 | const std::unique_ptr<const DexFile>& dex = dex_files[0]; |
| 115 | CHECK(dex->EnableWrite()) << "Failed to enable write"; |
| 116 | DexFile* dex_file = const_cast<DexFile*>(dex.get()); |
| 117 | mutator(dex_file); |
| 118 | const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum(); |
| 119 | if (!output_dex->WriteFully(dex->Begin(), dex->Size())) { |
| 120 | return false; |
| 121 | } |
| 122 | if (output_dex->Flush() != 0) { |
| 123 | PLOG(FATAL) << "Could not flush the output file."; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | protected: |
| 129 | static bool IsHost() { |
| 130 | return !kIsTargetBuild; |
| 131 | } |
| 132 | |
| 133 | // Helper - find directory with the following format: |
| 134 | // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/ |
| 135 | static std::string GetAndroidToolsDir(const std::string& subdir1, |
| 136 | const std::string& subdir2, |
| 137 | const std::string& subdir3); |
| 138 | |
| 139 | // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art |
| 140 | static std::string GetCoreArtLocation(); |
| 141 | |
| 142 | // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat |
| 143 | static std::string GetCoreOatLocation(); |
| 144 | |
| 145 | std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location); |
| 146 | |
| 147 | void ClearDirectory(const char* dirpath, bool recursive = true); |
| 148 | |
| 149 | std::string GetTestAndroidRoot(); |
| 150 | |
David Sehr | 7d43242 | 2018-05-25 10:49:02 -0700 | [diff] [blame] | 151 | // Open a file (allows reading of framework jars). |
| 152 | std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename); |
| 153 | // Open a test file (art-gtest-*.jar). |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 154 | std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name); |
| 155 | |
| 156 | std::unique_ptr<const DexFile> OpenTestDexFile(const char* name); |
| 157 | |
| 158 | |
| 159 | std::string android_data_; |
| 160 | std::string dalvik_cache_; |
| 161 | |
| 162 | virtual void SetUp(); |
| 163 | |
| 164 | virtual void TearDown(); |
| 165 | |
| 166 | // Creates the class path string for the given dex files (the list of dex file locations |
| 167 | // separated by ':'). |
| 168 | std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files); |
| 169 | // Same as CreateClassPath but add the dex file checksum after each location. The separator |
| 170 | // is '*'. |
| 171 | std::string CreateClassPathWithChecksums( |
| 172 | const std::vector<std::unique_ptr<const DexFile>>& dex_files); |
| 173 | |
| 174 | static std::string GetCoreFileLocation(const char* suffix); |
| 175 | |
| 176 | std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_; |
| 177 | }; |
| 178 | |
| 179 | template <typename TestType> |
| 180 | class CommonArtTestBase : public TestType, public CommonArtTestImpl { |
| 181 | public: |
| 182 | CommonArtTestBase() {} |
| 183 | virtual ~CommonArtTestBase() {} |
| 184 | |
| 185 | protected: |
| 186 | virtual void SetUp() OVERRIDE { |
| 187 | CommonArtTestImpl::SetUp(); |
| 188 | } |
| 189 | |
| 190 | virtual void TearDown() OVERRIDE { |
| 191 | CommonArtTestImpl::TearDown(); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | using CommonArtTest = CommonArtTestBase<testing::Test>; |
| 196 | |
| 197 | template <typename Param> |
| 198 | using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>; |
| 199 | |
| 200 | #define TEST_DISABLED_FOR_TARGET() \ |
| 201 | if (kIsTargetBuild) { \ |
| 202 | printf("WARNING: TEST DISABLED FOR TARGET\n"); \ |
| 203 | return; \ |
| 204 | } |
| 205 | |
| 206 | #define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \ |
| 207 | if (!kHostStaticBuildEnabled) { \ |
| 208 | printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \ |
| 209 | return; \ |
| 210 | } |
| 211 | |
| 212 | #define TEST_DISABLED_FOR_MEMORY_TOOL() \ |
Andreas Gampe | 8b362a8 | 2018-05-22 20:54:14 +0000 | [diff] [blame] | 213 | if (RUNNING_ON_MEMORY_TOOL > 0) { \ |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 214 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \ |
| 215 | return; \ |
| 216 | } |
| 217 | |
Andreas Gampe | 8b362a8 | 2018-05-22 20:54:14 +0000 | [diff] [blame] | 218 | #define TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND() \ |
| 219 | if (RUNNING_ON_MEMORY_TOOL > 0 && kMemoryToolIsValgrind) { \ |
| 220 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL VALGRIND\n"); \ |
| 221 | return; \ |
| 222 | } |
| 223 | |
| 224 | #define TEST_DISABLED_FOR_MEMORY_TOOL_ASAN() \ |
| 225 | if (RUNNING_ON_MEMORY_TOOL > 0 && !kMemoryToolIsValgrind) { \ |
| 226 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL ASAN\n"); \ |
| 227 | return; \ |
| 228 | } |
| 229 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 230 | #define TEST_DISABLED_FOR_HEAP_POISONING() \ |
| 231 | if (kPoisonHeapReferences) { \ |
| 232 | printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \ |
| 233 | return; \ |
| 234 | } |
| 235 | } // namespace art |
| 236 | |
| 237 | #endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_ |