1 /* 2 * Copyright (C) 2011 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_RUNTIME_COMMON_RUNTIME_TEST_H_ 18 #define ART_RUNTIME_COMMON_RUNTIME_TEST_H_ 19 20 #include <gtest/gtest.h> 21 #include <jni.h> 22 23 #include <string> 24 25 #include "arch/instruction_set.h" 26 #include "base/mutex.h" 27 #include "globals.h" 28 #include "os.h" 29 30 namespace art { 31 32 class ClassLinker; 33 class CompilerCallbacks; 34 class DexFile; 35 class JavaVMExt; 36 class Runtime; 37 typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions; 38 39 class ScratchFile { 40 public: 41 ScratchFile(); 42 43 ScratchFile(const ScratchFile& other, const char* suffix); 44 45 explicit ScratchFile(File* file); 46 47 ~ScratchFile(); 48 GetFilename()49 const std::string& GetFilename() const { 50 return filename_; 51 } 52 GetFile()53 File* GetFile() const { 54 return file_.get(); 55 } 56 57 int GetFd() const; 58 59 void Close(); 60 void Unlink(); 61 62 private: 63 std::string filename_; 64 std::unique_ptr<File> file_; 65 }; 66 67 class CommonRuntimeTestImpl { 68 public: 69 CommonRuntimeTestImpl(); 70 virtual ~CommonRuntimeTestImpl(); 71 static void SetUpAndroidRoot(); 72 73 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a 74 // non-derived class, be sure to also call the corresponding tear-down below. 75 static void SetUpAndroidData(std::string& android_data); 76 77 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error); 78 79 // Gets the paths of the libcore dex files. 80 static std::vector<std::string> GetLibCoreDexFileNames(); 81 82 // Returns bin directory which contains host's prebuild tools. 83 static std::string GetAndroidHostToolsDir(); 84 85 // Returns bin directory wahich contains target's prebuild tools. 86 static std::string GetAndroidTargetToolsDir(InstructionSet isa); 87 88 protected: 89 // Allow subclases such as CommonCompilerTest to add extra options. SetUpRuntimeOptions(RuntimeOptions * options ATTRIBUTE_UNUSED)90 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {} 91 92 // Called before the runtime is created. PreRuntimeCreate()93 virtual void PreRuntimeCreate() {} 94 95 // Called after the runtime is created. PostRuntimeCreate()96 virtual void PostRuntimeCreate() {} 97 IsHost()98 static bool IsHost() { 99 return !kIsTargetBuild; 100 } 101 102 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art 103 static std::string GetCoreArtLocation(); 104 105 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat 106 static std::string GetCoreOatLocation(); 107 108 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location); 109 110 void ClearDirectory(const char* dirpath); 111 112 std::string GetTestAndroidRoot(); 113 114 std::string GetTestDexFileName(const char* name); 115 116 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name) 117 SHARED_REQUIRES(Locks::mutator_lock_); 118 119 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name) 120 SHARED_REQUIRES(Locks::mutator_lock_); 121 122 jobject LoadDex(const char* dex_name) SHARED_REQUIRES(Locks::mutator_lock_); 123 124 std::string android_data_; 125 std::string dalvik_cache_; 126 127 std::unique_ptr<Runtime> runtime_; 128 129 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all 130 // owned by the runtime. 131 ClassLinker* class_linker_; 132 const DexFile* java_lang_dex_file_; 133 std::vector<const DexFile*> boot_class_path_; 134 135 // Get the dex files from a PathClassLoader. This in order of the dex elements and their dex 136 // arrays. 137 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader); 138 139 // Get the first dex file from a PathClassLoader. Will abort if it is null. 140 const DexFile* GetFirstDexFile(jobject jclass_loader); 141 142 std::unique_ptr<CompilerCallbacks> callbacks_; 143 144 void SetUp(); 145 146 void TearDown(); 147 148 void FinalizeSetup(); 149 150 private: 151 static std::string GetCoreFileLocation(const char* suffix); 152 153 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_; 154 }; 155 156 template <typename TestType> 157 class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl { 158 public: CommonRuntimeTestBase()159 CommonRuntimeTestBase() {} ~CommonRuntimeTestBase()160 virtual ~CommonRuntimeTestBase() {} 161 162 protected: SetUp()163 virtual void SetUp() { 164 CommonRuntimeTestImpl::SetUp(); 165 } 166 TearDown()167 virtual void TearDown() { 168 CommonRuntimeTestImpl::TearDown(); 169 } 170 171 // Called to finish up runtime creation and filling test fields. By default runs root 172 // initializers, initialize well-known classes, and creates the heap thread pool. FinalizeSetup()173 virtual void FinalizeSetup() { 174 CommonRuntimeTestImpl::FinalizeSetup(); 175 } 176 }; 177 178 using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>; 179 180 template <typename Param> 181 using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>; 182 183 // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on 184 // rather than aborting, so be careful! 185 class CheckJniAbortCatcher { 186 public: 187 CheckJniAbortCatcher(); 188 189 ~CheckJniAbortCatcher(); 190 191 void Check(const char* expected_text); 192 193 private: 194 static void Hook(void* data, const std::string& reason); 195 196 JavaVMExt* const vm_; 197 std::string actual_; 198 199 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher); 200 }; 201 202 #define TEST_DISABLED_FOR_MIPS() \ 203 if (kRuntimeISA == kMips) { \ 204 printf("WARNING: TEST DISABLED FOR MIPS\n"); \ 205 return; \ 206 } 207 208 } // namespace art 209 210 namespace std { 211 212 // TODO: isn't gtest supposed to be able to print STL types for itself? 213 template <typename T> 214 std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs); 215 216 } // namespace std 217 218 #endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_ 219