common_runtime_test.h revision a59dd80f9f48cb750d329d4d4af2d99d72b484d1
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 SetEnvironmentVariables(std::string& android_data);
68
69  CommonRuntimeTest();
70  ~CommonRuntimeTest();
71
72 protected:
73  static bool IsHost() {
74    return !kIsTargetBuild;
75  }
76
77  const DexFile* LoadExpectSingleDexFile(const char* location);
78
79  virtual void SetUp();
80
81  // Allow subclases such as CommonCompilerTest to add extra options.
82  virtual void SetUpRuntimeOptions(RuntimeOptions* options) {}
83
84  void ClearDirectory(const char* dirpath);
85
86  virtual void TearDown();
87
88  std::string GetLibCoreDexFileName();
89
90  std::string GetDexFileName(const std::string& jar_prefix);
91
92  std::string GetTestAndroidRoot();
93
94  std::vector<const DexFile*> OpenTestDexFiles(const char* name)
95      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96
97  const DexFile* OpenTestDexFile(const char* name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98
99  jobject LoadDex(const char* dex_name) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
100
101  std::string android_data_;
102  std::string dalvik_cache_;
103  const DexFile* java_lang_dex_file_;  // owned by runtime_
104  std::vector<const DexFile*> boot_class_path_;
105  std::unique_ptr<Runtime> runtime_;
106  // Owned by the runtime
107  ClassLinker* class_linker_;
108
109 private:
110  std::unique_ptr<CompilerCallbacks> callbacks_;
111  std::vector<const DexFile*> opened_dex_files_;
112};
113
114// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
115// rather than aborting, so be careful!
116class CheckJniAbortCatcher {
117 public:
118  CheckJniAbortCatcher();
119
120  ~CheckJniAbortCatcher();
121
122  void Check(const char* expected_text);
123
124 private:
125  static void Hook(void* data, const std::string& reason);
126
127  JavaVMExt* vm_;
128  std::string actual_;
129
130  DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
131};
132
133// TODO: These tests were disabled for portable when we went to having
134// MCLinker link LLVM ELF output because we no longer just have code
135// blobs in memory. We'll need to dlopen to load and relocate
136// temporary output to resurrect these tests.
137#define TEST_DISABLED_FOR_PORTABLE() \
138  if (kUsePortableCompiler) { \
139    printf("WARNING: TEST DISABLED FOR PORTABLE\n"); \
140    return; \
141  }
142
143// TODO: When heap reference poisoning works with the compiler, get rid of this.
144#define TEST_DISABLED_FOR_HEAP_REFERENCE_POISONING() \
145  if (kPoisonHeapReferences) { \
146    printf("WARNING: TEST DISABLED FOR HEAP REFERENCE POISONING\n"); \
147    return; \
148  }
149
150}  // namespace art
151
152namespace std {
153
154// TODO: isn't gtest supposed to be able to print STL types for itself?
155template <typename T>
156std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs);
157
158}  // namespace std
159
160#endif  // ART_RUNTIME_COMMON_RUNTIME_TEST_H_
161