common_runtime_test.h revision 4bf8d11df5dccc1b276cd9c40a98e8a14d79a9c8
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 "base/mutex.h" 26#include "globals.h" 27#include "os.h" 28 29namespace art { 30 31class ClassLinker; 32class CompilerCallbacks; 33class DexFile; 34class JavaVMExt; 35class Runtime; 36typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions; 37 38class ScratchFile { 39 public: 40 ScratchFile(); 41 42 ScratchFile(const ScratchFile& other, const char* suffix); 43 44 explicit ScratchFile(File* file); 45 46 ~ScratchFile(); 47 48 const std::string& GetFilename() const { 49 return filename_; 50 } 51 52 File* GetFile() const { 53 return file_.get(); 54 } 55 56 int GetFd() const; 57 58 void Unlink(); 59 60 private: 61 std::string filename_; 62 std::unique_ptr<File> file_; 63}; 64 65class CommonRuntimeTest : public testing::Test { 66 public: 67 static void SetUpAndroidRoot(); 68 69 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a 70 // non-derived class, be sure to also call the corresponding tear-down below. 71 static void SetUpAndroidData(std::string& android_data); 72 73 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error); 74 75 CommonRuntimeTest(); 76 ~CommonRuntimeTest(); 77 78 protected: 79 static bool IsHost() { 80 return !kIsTargetBuild; 81 } 82 83 const DexFile* LoadExpectSingleDexFile(const char* location); 84 85 virtual void SetUp(); 86 87 // Allow subclases such as CommonCompilerTest to add extra options. 88 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {} 89 90 void ClearDirectory(const char* dirpath); 91 92 virtual void TearDown(); 93 94 // Gets the path of the libcore dex file. 95 std::string GetLibCoreDexFileName(); 96 97 // Gets the path of the specified dex file for host or target. 98 std::string GetDexFileName(const std::string& jar_prefix); 99 100 // Gets the path of the libcore oat file. 101 std::string GetLibCoreOatFileName(); 102 103 // Gets the path of the specified oat file for host or target. 104 std::string GetOatFileName(const std::string& oat_prefix); 105 106 std::string GetTestAndroidRoot(); 107 108 std::vector<const DexFile*> OpenTestDexFiles(const char* name) 109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 110 111 const DexFile* OpenTestDexFile(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 112 113 jobject LoadDex(const char* dex_name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 114 115 std::string android_data_; 116 std::string dalvik_cache_; 117 const DexFile* java_lang_dex_file_; // owned by runtime_ 118 std::vector<const DexFile*> boot_class_path_; 119 std::unique_ptr<Runtime> runtime_; 120 // Owned by the runtime 121 ClassLinker* class_linker_; 122 123 private: 124 std::unique_ptr<CompilerCallbacks> callbacks_; 125 std::vector<const DexFile*> opened_dex_files_; 126}; 127 128// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on 129// rather than aborting, so be careful! 130class CheckJniAbortCatcher { 131 public: 132 CheckJniAbortCatcher(); 133 134 ~CheckJniAbortCatcher(); 135 136 void Check(const char* expected_text); 137 138 private: 139 static void Hook(void* data, const std::string& reason); 140 141 JavaVMExt* vm_; 142 std::string actual_; 143 144 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher); 145}; 146 147// TODO: These tests were disabled for portable when we went to having 148// MCLinker link LLVM ELF output because we no longer just have code 149// blobs in memory. We'll need to dlopen to load and relocate 150// temporary output to resurrect these tests. 151#define TEST_DISABLED_FOR_PORTABLE() \ 152 if (kUsePortableCompiler) { \ 153 printf("WARNING: TEST DISABLED FOR PORTABLE\n"); \ 154 return; \ 155 } 156 157// TODO: When heap reference poisoning works with the compiler, get rid of this. 158#define TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING() \ 159 if (kPoisonHeapReferences) { \ 160 printf("WARNING: TEST DISABLED FOR HEAP REFERENCE POISONING\n"); \ 161 return; \ 162 } 163 164} // namespace art 165 166namespace std { 167 168// TODO: isn't gtest supposed to be able to print STL types for itself? 169template <typename T> 170std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs); 171 172} // namespace std 173 174#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_ 175