blob: 9efea84743e758dacb80ea9f447bb639dd2cda84 [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
Andreas Gampee21dc3d2014-12-08 16:59:43 -080058 void Close();
Ian Rogerse63db272014-07-15 15:36:11 -070059 void Unlink();
Brian Carlstrom0e12bdc2014-05-14 17:44:28 -070060
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080061 private:
62 std::string filename_;
Ian Rogers700a4022014-05-19 16:49:03 -070063 std::unique_ptr<File> file_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080064};
65
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080066class CommonRuntimeTest : public testing::Test {
67 public:
Andreas Gampe7747c8d2014-08-06 14:53:03 -070068 static void SetUpAndroidRoot();
69
70 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
71 // non-derived class, be sure to also call the corresponding tear-down below.
72 static void SetUpAndroidData(std::string& android_data);
73
74 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080075
Ian Rogerse63db272014-07-15 15:36:11 -070076 CommonRuntimeTest();
77 ~CommonRuntimeTest();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080078
Igor Murashkinaaebaa02015-01-26 10:55:53 -080079 // Gets the path of the libcore dex file.
80 static std::string GetLibCoreDexFileName();
81
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080082 protected:
83 static bool IsHost() {
84 return !kIsTargetBuild;
85 }
86
Igor Murashkin37743352014-11-13 14:38:00 -080087 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
88 static std::string GetCoreArtLocation();
89
90 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
91 static std::string GetCoreOatLocation();
92
Richard Uhlerfbef44d2014-12-23 09:48:51 -080093 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
Andreas Gampe833a4852014-05-21 18:46:59 -070094
Ian Rogerse63db272014-07-15 15:36:11 -070095 virtual void SetUp();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080096
97 // Allow subclases such as CommonCompilerTest to add extra options.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070098 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080099
Alex Lighta59dd802014-07-02 16:28:08 -0700100 void ClearDirectory(const char* dirpath);
101
Ian Rogerse63db272014-07-15 15:36:11 -0700102 virtual void TearDown();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800103
Jeff Haof0a3f092014-07-24 16:26:09 -0700104 // Gets the path of the specified dex file for host or target.
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800105 static std::string GetDexFileName(const std::string& jar_prefix);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800106
Ian Rogerse63db272014-07-15 15:36:11 -0700107 std::string GetTestAndroidRoot();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800108
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800109 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name)
Ian Rogerse63db272014-07-15 15:36:11 -0700110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Andreas Gampe833a4852014-05-21 18:46:59 -0700111
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800112 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name)
113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800114
Ian Rogerse63db272014-07-15 15:36:11 -0700115 jobject LoadDex(const char* dex_name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800116
117 std::string android_data_;
118 std::string dalvik_cache_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800119
Ian Rogers700a4022014-05-19 16:49:03 -0700120 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800121
122 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
123 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800124 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800125 const DexFile* java_lang_dex_file_;
126 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800127
128 private:
Igor Murashkin37743352014-11-13 14:38:00 -0800129 static std::string GetCoreFileLocation(const char* suffix);
130
Ian Rogerse63db272014-07-15 15:36:11 -0700131 std::unique_ptr<CompilerCallbacks> callbacks_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800132 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800133};
134
135// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
136// rather than aborting, so be careful!
137class CheckJniAbortCatcher {
138 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700139 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800140
Ian Rogerse63db272014-07-15 15:36:11 -0700141 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800142
Ian Rogerse63db272014-07-15 15:36:11 -0700143 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800144
145 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700146 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800147
Ian Rogers68d8b422014-07-17 11:09:10 -0700148 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800149 std::string actual_;
150
151 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
152};
153
Hiroshi Yamauchi05b15d62014-03-19 12:57:56 -0700154// TODO: When heap reference poisoning works with the compiler, get rid of this.
155#define TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING() \
156 if (kPoisonHeapReferences) { \
157 printf("WARNING: TEST DISABLED FOR HEAP REFERENCE POISONING\n"); \
158 return; \
159 }
160
Nicolas Geoffray54accbc2014-08-13 03:40:45 +0100161#define TEST_DISABLED_FOR_MIPS() \
162 if (kRuntimeISA == kMips || kRuntimeISA == kMips64) { \
163 printf("WARNING: TEST DISABLED FOR MIPS\n"); \
164 return; \
165 }
166
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800167} // namespace art
168
169namespace std {
170
171// TODO: isn't gtest supposed to be able to print STL types for itself?
172template <typename T>
Ian Rogerse63db272014-07-15 15:36:11 -0700173std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800174
175} // namespace std
176
177#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_