blob: a4764c275dd005108195e04d6915b5dcab8232c9 [file] [log] [blame]
David Sehrd5f8de82018-04-27 14:12:03 -07001/*
2 * Copyright (C) 2018 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_LIBARTBASE_BASE_COMMON_ART_TEST_H_
18#define ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_
19
20#include <gtest/gtest.h>
21
22#include <string>
23
24#include <android-base/logging.h>
25
26#include "base/globals.h"
27#include "base/mutex.h"
28#include "base/os.h"
29#include "base/unix_file/fd_file.h"
30#include "dex/art_dex_file_loader.h"
31#include "dex/compact_dex_level.h"
32
33namespace art {
34
35using LogSeverity = android::base::LogSeverity;
36using ScopedLogSeverity = android::base::ScopedLogSeverity;
37
38// OBJ pointer helpers to avoid needing .Decode everywhere.
39#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
40#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
41#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
42#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
43
44class DexFile;
45
46class ScratchFile {
47 public:
48 ScratchFile();
49
50 explicit ScratchFile(const std::string& filename);
51
52 ScratchFile(const ScratchFile& other, const char* suffix);
53
54 ScratchFile(ScratchFile&& other);
55
56 ScratchFile& operator=(ScratchFile&& other);
57
58 explicit ScratchFile(File* file);
59
60 ~ScratchFile();
61
62 const std::string& GetFilename() const {
63 return filename_;
64 }
65
66 File* GetFile() const {
67 return file_.get();
68 }
69
70 int GetFd() const;
71
72 void Close();
73 void Unlink();
74
75 private:
76 std::string filename_;
77 std::unique_ptr<File> file_;
78};
79
80class CommonArtTestImpl {
81 public:
82 CommonArtTestImpl() = default;
83 virtual ~CommonArtTestImpl() = default;
84
85 static void SetUpAndroidRoot();
86
87 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
88 // non-derived class, be sure to also call the corresponding tear-down below.
89 static void SetUpAndroidData(std::string& android_data);
90
91 static void TearDownAndroidData(const std::string& android_data, bool fail_on_error);
92
93 // Gets the paths of the libcore dex files.
94 static std::vector<std::string> GetLibCoreDexFileNames();
95
96 // Returns bin directory which contains host's prebuild tools.
97 static std::string GetAndroidHostToolsDir();
98
99 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
100 std::string GetTestDexFileName(const char* name) const;
101
102 template <typename Mutator>
103 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
104 std::vector<std::unique_ptr<const DexFile>> dex_files;
105 std::string error_msg;
106 const ArtDexFileLoader dex_file_loader;
107 CHECK(dex_file_loader.Open(input_jar.c_str(),
108 input_jar.c_str(),
109 /*verify*/ true,
110 /*verify_checksum*/ true,
111 &error_msg,
112 &dex_files)) << error_msg;
113 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
114 const std::unique_ptr<const DexFile>& dex = dex_files[0];
115 CHECK(dex->EnableWrite()) << "Failed to enable write";
116 DexFile* dex_file = const_cast<DexFile*>(dex.get());
117 mutator(dex_file);
118 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
119 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
120 return false;
121 }
122 if (output_dex->Flush() != 0) {
123 PLOG(FATAL) << "Could not flush the output file.";
124 }
125 return true;
126 }
127
128 protected:
129 static bool IsHost() {
130 return !kIsTargetBuild;
131 }
132
133 // Helper - find directory with the following format:
134 // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
135 static std::string GetAndroidToolsDir(const std::string& subdir1,
136 const std::string& subdir2,
137 const std::string& subdir3);
138
139 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
140 static std::string GetCoreArtLocation();
141
142 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
143 static std::string GetCoreOatLocation();
144
145 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
146
147 void ClearDirectory(const char* dirpath, bool recursive = true);
148
149 std::string GetTestAndroidRoot();
150
151 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
152
153 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
154
155
156 std::string android_data_;
157 std::string dalvik_cache_;
158
159 virtual void SetUp();
160
161 virtual void TearDown();
162
163 // Creates the class path string for the given dex files (the list of dex file locations
164 // separated by ':').
165 std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
166 // Same as CreateClassPath but add the dex file checksum after each location. The separator
167 // is '*'.
168 std::string CreateClassPathWithChecksums(
169 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
170
171 static std::string GetCoreFileLocation(const char* suffix);
172
173 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
174};
175
176template <typename TestType>
177class CommonArtTestBase : public TestType, public CommonArtTestImpl {
178 public:
179 CommonArtTestBase() {}
180 virtual ~CommonArtTestBase() {}
181
182 protected:
183 virtual void SetUp() OVERRIDE {
184 CommonArtTestImpl::SetUp();
185 }
186
187 virtual void TearDown() OVERRIDE {
188 CommonArtTestImpl::TearDown();
189 }
190};
191
192using CommonArtTest = CommonArtTestBase<testing::Test>;
193
194template <typename Param>
195using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
196
197#define TEST_DISABLED_FOR_TARGET() \
198 if (kIsTargetBuild) { \
199 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
200 return; \
201 }
202
203#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
204 if (!kHostStaticBuildEnabled) { \
205 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
206 return; \
207 }
208
209#define TEST_DISABLED_FOR_MEMORY_TOOL() \
210 if (RUNNING_ON_MEMORY_TOOL > 0) { \
211 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
212 return; \
213 }
214
215#define TEST_DISABLED_FOR_MEMORY_TOOL_VALGRIND() \
216 if (RUNNING_ON_MEMORY_TOOL > 0 && kMemoryToolIsValgrind) { \
217 printf("WARNING: TEST DISABLED FOR MEMORY TOOL VALGRIND\n"); \
218 return; \
219 }
220
221#define TEST_DISABLED_FOR_MEMORY_TOOL_ASAN() \
222 if (RUNNING_ON_MEMORY_TOOL > 0 && !kMemoryToolIsValgrind) { \
223 printf("WARNING: TEST DISABLED FOR MEMORY TOOL ASAN\n"); \
224 return; \
225 }
226
227#define TEST_DISABLED_FOR_HEAP_POISONING() \
228 if (kPoisonHeapReferences) { \
229 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
230 return; \
231 }
232} // namespace art
233
234#endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_