blob: f3184578b642075fa23f6908d9f8101c4ffa9e6f [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
David Srbecky3e52aa42015-04-12 07:45:18 +010025#include "arch/instruction_set.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "base/mutex.h"
27#include "globals.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028#include "os.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080029
30namespace art {
31
Ian Rogerse63db272014-07-15 15:36:11 -070032class ClassLinker;
33class CompilerCallbacks;
34class DexFile;
35class JavaVMExt;
36class Runtime;
37typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions;
38
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080039class ScratchFile {
40 public:
Ian Rogerse63db272014-07-15 15:36:11 -070041 ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080042
Ian Rogerse63db272014-07-15 15:36:11 -070043 ScratchFile(const ScratchFile& other, const char* suffix);
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000044
Ian Rogerse63db272014-07-15 15:36:11 -070045 explicit ScratchFile(File* file);
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070046
Ian Rogerse63db272014-07-15 15:36:11 -070047 ~ScratchFile();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080048
49 const std::string& GetFilename() const {
50 return filename_;
51 }
52
53 File* GetFile() const {
54 return file_.get();
55 }
56
Ian Rogerse63db272014-07-15 15:36:11 -070057 int GetFd() const;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080058
Andreas Gampee21dc3d2014-12-08 16:59:43 -080059 void Close();
Ian Rogerse63db272014-07-15 15:36:11 -070060 void Unlink();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070061
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080062 private:
63 std::string filename_;
Ian Rogers700a4022014-05-19 16:49:03 -070064 std::unique_ptr<File> file_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080065};
66
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080067class CommonRuntimeTest : public testing::Test {
68 public:
Andreas Gampe7747c8d2014-08-06 14:53:03 -070069 static void SetUpAndroidRoot();
70
71 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
72 // non-derived class, be sure to also call the corresponding tear-down below.
73 static void SetUpAndroidData(std::string& android_data);
74
75 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080076
Ian Rogerse63db272014-07-15 15:36:11 -070077 CommonRuntimeTest();
78 ~CommonRuntimeTest();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080079
Igor Murashkinaaebaa02015-01-26 10:55:53 -080080 // Gets the path of the libcore dex file.
81 static std::string GetLibCoreDexFileName();
82
David Srbecky3e52aa42015-04-12 07:45:18 +010083 // Returns bin directory which contains host's prebuild tools.
84 static std::string GetAndroidHostToolsDir();
85
86 // Returns bin directory which contains target's prebuild tools.
87 static std::string GetAndroidTargetToolsDir(InstructionSet isa);
88
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080089 protected:
90 static bool IsHost() {
91 return !kIsTargetBuild;
92 }
93
Igor Murashkin37743352014-11-13 14:38:00 -080094 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
95 static std::string GetCoreArtLocation();
96
97 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
98 static std::string GetCoreOatLocation();
99
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800100 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
Andreas Gampe833a4852014-05-21 18:46:59 -0700101
Ian Rogerse63db272014-07-15 15:36:11 -0700102 virtual void SetUp();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800103
104 // Allow subclases such as CommonCompilerTest to add extra options.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700105 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800106
Alex Lighta59dd802014-07-02 16:28:08 -0700107 void ClearDirectory(const char* dirpath);
108
Ian Rogerse63db272014-07-15 15:36:11 -0700109 virtual void TearDown();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800110
Richard Uhler66d874d2015-01-15 09:37:19 -0800111 // Called before the runtime is created.
112 virtual void PreRuntimeCreate() {}
113
114 // Called after the runtime is created.
115 virtual void PostRuntimeCreate() {}
116
Andreas Gampea00f0122015-12-16 16:54:35 -0800117 // Called to finish up runtime creation and filling test fields. By default runs root
118 // initializers, initialize well-known classes, and creates the heap thread pool.
119 virtual void FinalizeSetup();
120
Jeff Haof0a3f092014-07-24 16:26:09 -0700121 // Gets the path of the specified dex file for host or target.
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800122 static std::string GetDexFileName(const std::string& jar_prefix);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800123
Ian Rogerse63db272014-07-15 15:36:11 -0700124 std::string GetTestAndroidRoot();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800125
Richard Uhler66d874d2015-01-15 09:37:19 -0800126 std::string GetTestDexFileName(const char* name);
127
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800128 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name)
Mathieu Chartier90443472015-07-16 20:32:27 -0700129 SHARED_REQUIRES(Locks::mutator_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700130
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800131 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name)
Mathieu Chartier90443472015-07-16 20:32:27 -0700132 SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800133
Mathieu Chartier90443472015-07-16 20:32:27 -0700134 jobject LoadDex(const char* dex_name) SHARED_REQUIRES(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800135
136 std::string android_data_;
137 std::string dalvik_cache_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800138
Ian Rogers700a4022014-05-19 16:49:03 -0700139 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800140
141 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
142 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800143 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800144 const DexFile* java_lang_dex_file_;
145 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800146
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700147 // Get the dex files from a PathClassLoader. This in order of the dex elements and their dex
148 // arrays.
149 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
150
151 // Get the first dex file from a PathClassLoader. Will abort if it is null.
152 const DexFile* GetFirstDexFile(jobject jclass_loader);
153
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700154 std::unique_ptr<CompilerCallbacks> callbacks_;
155
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800156 private:
Igor Murashkin37743352014-11-13 14:38:00 -0800157 static std::string GetCoreFileLocation(const char* suffix);
158
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800159 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800160};
161
162// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
163// rather than aborting, so be careful!
164class CheckJniAbortCatcher {
165 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700166 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800167
Ian Rogerse63db272014-07-15 15:36:11 -0700168 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800169
Ian Rogerse63db272014-07-15 15:36:11 -0700170 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800171
172 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700173 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800174
Ian Rogers68d8b422014-07-17 11:09:10 -0700175 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800176 std::string actual_;
177
178 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
179};
180
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100181#define TEST_DISABLED_FOR_MIPS() \
Douglas Leungd90957f2015-04-30 19:22:49 -0700182 if (kRuntimeISA == kMips) { \
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100183 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
184 return; \
185 }
186
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800187} // namespace art
188
189namespace std {
190
191// TODO: isn't gtest supposed to be able to print STL types for itself?
192template <typename T>
Ian Rogerse63db272014-07-15 15:36:11 -0700193std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800194
195} // namespace std
196
197#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_