blob: 363d8daaf80e5b922ed574befeb3ca3b104a01fe [file] [log] [blame]
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -08001/*
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
Ian Rogerse63db272014-07-15 15:36:11 -070020#include <gtest/gtest.h>
21#include <jni.h>
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022
Ian Rogerse63db272014-07-15 15:36:11 -070023#include <string>
24
25#include "base/mutex.h"
26#include "globals.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080027#include "os.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028
29namespace art {
30
Ian Rogerse63db272014-07-15 15:36:11 -070031class ClassLinker;
32class CompilerCallbacks;
33class DexFile;
34class JavaVMExt;
35class Runtime;
36typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
37
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080038class ScratchFile {
39 public:
Ian Rogerse63db272014-07-15 15:36:11 -070040 ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080041
Ian Rogerse63db272014-07-15 15:36:11 -070042 ScratchFile(const ScratchFile& other, const char* suffix);
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000043
Ian Rogerse63db272014-07-15 15:36:11 -070044 explicit ScratchFile(File* file);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070045
Ian Rogerse63db272014-07-15 15:36:11 -070046 ~ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080047
48 const std::string& GetFilename() const {
49 return filename_;
50 }
51
52 File* GetFile() const {
53 return file_.get();
54 }
55
Ian Rogerse63db272014-07-15 15:36:11 -070056 int GetFd() const;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080057
Ian Rogerse63db272014-07-15 15:36:11 -070058 void Unlink();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070059
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080060 private:
61 std::string filename_;
Ian Rogers700a4022014-05-19 16:49:03 -070062 std::unique_ptr<File> file_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080063};
64
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080065class CommonRuntimeTest : public testing::Test {
66 public:
Ian Rogerse63db272014-07-15 15:36:11 -070067 static void SetEnvironmentVariables(std::string& android_data);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080068
Ian Rogerse63db272014-07-15 15:36:11 -070069 CommonRuntimeTest();
70 ~CommonRuntimeTest();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080071
72 protected:
73 static bool IsHost() {
74 return !kIsTargetBuild;
75 }
76
Ian Rogerse63db272014-07-15 15:36:11 -070077 const DexFile* LoadExpectSingleDexFile(const char* location);
Andreas Gampe833a4852014-05-21 18:46:59 -070078
Ian Rogerse63db272014-07-15 15:36:11 -070079 virtual void SetUp();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080080
81 // Allow subclases such as CommonCompilerTest to add extra options.
Ian Rogerse63db272014-07-15 15:36:11 -070082 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {}
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080083
Alex Lighta59dd802014-07-02 16:28:08 -070084 void ClearDirectory(const char* dirpath);
85
Ian Rogerse63db272014-07-15 15:36:11 -070086 virtual void TearDown();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080087
Jeff Haof0a3f092014-07-24 16:26:09 -070088 // Gets the path of the libcore dex file.
Ian Rogerse63db272014-07-15 15:36:11 -070089 std::string GetLibCoreDexFileName();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080090
Jeff Haof0a3f092014-07-24 16:26:09 -070091 // Gets the path of the specified dex file for host or target.
Ian Rogerse63db272014-07-15 15:36:11 -070092 std::string GetDexFileName(const std::string& jar_prefix);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080093
Jeff Haof0a3f092014-07-24 16:26:09 -070094 // Gets the path of the libcore oat file.
95 std::string GetLibCoreOatFileName();
96
97 // Gets the path of the specified oat file for host or target.
98 std::string GetOatFileName(const std::string& oat_prefix);
99
Ian Rogerse63db272014-07-15 15:36:11 -0700100 std::string GetTestAndroidRoot();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800101
Andreas Gampe833a4852014-05-21 18:46:59 -0700102 std::vector<const DexFile*> OpenTestDexFiles(const char* name)
Ian Rogerse63db272014-07-15 15:36:11 -0700103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700104
Ian Rogerse63db272014-07-15 15:36:11 -0700105 const DexFile* OpenTestDexFile(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800106
Ian Rogerse63db272014-07-15 15:36:11 -0700107 jobject LoadDex(const char* dex_name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800108
109 std::string android_data_;
110 std::string dalvik_cache_;
111 const DexFile* java_lang_dex_file_; // owned by runtime_
112 std::vector<const DexFile*> boot_class_path_;
Ian Rogers700a4022014-05-19 16:49:03 -0700113 std::unique_ptr<Runtime> runtime_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800114 // Owned by the runtime
115 ClassLinker* class_linker_;
116
117 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700118 std::unique_ptr<CompilerCallbacks> callbacks_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800119 std::vector<const DexFile*> opened_dex_files_;
120};
121
122// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
123// rather than aborting, so be careful!
124class CheckJniAbortCatcher {
125 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700126 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800127
Ian Rogerse63db272014-07-15 15:36:11 -0700128 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800129
Ian Rogerse63db272014-07-15 15:36:11 -0700130 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800131
132 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700133 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800134
135 JavaVMExt* vm_;
136 std::string actual_;
137
138 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
139};
140
141// TODO: These tests were disabled for portable when we went to having
142// MCLinker link LLVM ELF output because we no longer just have code
143// blobs in memory. We'll need to dlopen to load and relocate
144// temporary output to resurrect these tests.
145#define TEST_DISABLED_FOR_PORTABLE() \
146 if (kUsePortableCompiler) { \
147 printf("WARNING: TEST DISABLED FOR PORTABLE\n"); \
148 return; \
149 }
150
Hiroshi Yamauchi05b15d62014-03-19 12:57:56 -0700151// TODO: When heap reference poisoning works with the compiler, get rid of this.
152#define TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING() \
153 if (kPoisonHeapReferences) { \
154 printf("WARNING: TEST DISABLED FOR HEAP REFERENCE POISONING\n"); \
155 return; \
156 }
157
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800158} // namespace art
159
160namespace std {
161
162// TODO: isn't gtest supposed to be able to print STL types for itself?
163template <typename T>
Ian Rogerse63db272014-07-15 15:36:11 -0700164std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800165
166} // namespace std
167
168#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_