blob: 1748a9bb0b99cc8126094de84140eabb5e4ae4c3 [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
Andreas Gampe38aa0b52018-07-10 23:26:55 -070022#include <functional>
David Sehrd5f8de82018-04-27 14:12:03 -070023#include <string>
24
Andreas Gampe38aa0b52018-07-10 23:26:55 -070025#include <sys/wait.h>
26
David Sehrd5f8de82018-04-27 14:12:03 -070027#include <android-base/logging.h>
28
29#include "base/globals.h"
30#include "base/mutex.h"
31#include "base/os.h"
32#include "base/unix_file/fd_file.h"
33#include "dex/art_dex_file_loader.h"
34#include "dex/compact_dex_level.h"
35
36namespace art {
37
38using LogSeverity = android::base::LogSeverity;
39using ScopedLogSeverity = android::base::ScopedLogSeverity;
40
41// OBJ pointer helpers to avoid needing .Decode everywhere.
42#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
43#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
44#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
45#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr());
46
47class DexFile;
48
49class ScratchFile {
50 public:
51 ScratchFile();
52
53 explicit ScratchFile(const std::string& filename);
54
55 ScratchFile(const ScratchFile& other, const char* suffix);
56
Andreas Gampe44b31742018-10-01 19:30:57 -070057 ScratchFile(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070058
Andreas Gampe44b31742018-10-01 19:30:57 -070059 ScratchFile& operator=(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070060
61 explicit ScratchFile(File* file);
62
63 ~ScratchFile();
64
65 const std::string& GetFilename() const {
66 return filename_;
67 }
68
69 File* GetFile() const {
70 return file_.get();
71 }
72
73 int GetFd() const;
74
75 void Close();
76 void Unlink();
77
78 private:
79 std::string filename_;
80 std::unique_ptr<File> file_;
81};
82
83class CommonArtTestImpl {
84 public:
85 CommonArtTestImpl() = default;
86 virtual ~CommonArtTestImpl() = default;
87
Neil Fuller26a5dd62019-03-13 15:16:35 +000088 // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_RUNTIME_ROOT,
89 // and ANDROID_TZDATA_ROOT environment variables using sensible defaults if not already set.
Neil Fuller26c43772018-11-23 17:56:43 +000090 static void SetUpAndroidRootEnvVars();
David Sehrd5f8de82018-04-27 14:12:03 -070091
Neil Fuller26c43772018-11-23 17:56:43 +000092 // Set up the ANDROID_DATA environment variable, creating the directory if required.
David Sehrd5f8de82018-04-27 14:12:03 -070093 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
94 // non-derived class, be sure to also call the corresponding tear-down below.
Neil Fuller26c43772018-11-23 17:56:43 +000095 static void SetUpAndroidDataDir(std::string& android_data);
David Sehrd5f8de82018-04-27 14:12:03 -070096
Neil Fuller26c43772018-11-23 17:56:43 +000097 static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error);
David Sehrd5f8de82018-04-27 14:12:03 -070098
99 // Gets the paths of the libcore dex files.
100 static std::vector<std::string> GetLibCoreDexFileNames();
101
Vladimir Marko7a85e702018-12-03 18:47:23 +0000102 // Gets the locations of the libcore dex files.
103 static std::vector<std::string> GetLibCoreDexLocations();
104
105 static std::string GetClassPathOption(const char* option,
106 const std::vector<std::string>& class_path);
107
David Sehrd5f8de82018-04-27 14:12:03 -0700108 // Returns bin directory which contains host's prebuild tools.
109 static std::string GetAndroidHostToolsDir();
110
111 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
112 std::string GetTestDexFileName(const char* name) const;
113
114 template <typename Mutator>
115 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
116 std::vector<std::unique_ptr<const DexFile>> dex_files;
117 std::string error_msg;
118 const ArtDexFileLoader dex_file_loader;
119 CHECK(dex_file_loader.Open(input_jar.c_str(),
120 input_jar.c_str(),
121 /*verify*/ true,
122 /*verify_checksum*/ true,
123 &error_msg,
124 &dex_files)) << error_msg;
125 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
126 const std::unique_ptr<const DexFile>& dex = dex_files[0];
127 CHECK(dex->EnableWrite()) << "Failed to enable write";
128 DexFile* dex_file = const_cast<DexFile*>(dex.get());
129 mutator(dex_file);
130 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
131 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
132 return false;
133 }
134 if (output_dex->Flush() != 0) {
135 PLOG(FATAL) << "Could not flush the output file.";
136 }
137 return true;
138 }
139
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700140 struct ForkAndExecResult {
141 enum Stage {
142 kLink,
143 kFork,
144 kWaitpid,
145 kFinished,
146 };
147 Stage stage;
148 int status_code;
149
150 bool StandardSuccess() {
151 return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
152 }
153 };
154 using OutputHandlerFn = std::function<void(char*, size_t)>;
155 using PostForkFn = std::function<bool()>;
156 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
157 const PostForkFn& post_fork,
158 const OutputHandlerFn& handler);
159 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
160 const PostForkFn& post_fork,
161 std::string* output);
162
David Sehrd5f8de82018-04-27 14:12:03 -0700163 protected:
164 static bool IsHost() {
165 return !kIsTargetBuild;
166 }
167
168 // Helper - find directory with the following format:
169 // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
170 static std::string GetAndroidToolsDir(const std::string& subdir1,
171 const std::string& subdir2,
172 const std::string& subdir3);
173
174 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
175 static std::string GetCoreArtLocation();
176
177 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
178 static std::string GetCoreOatLocation();
179
180 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
181
182 void ClearDirectory(const char* dirpath, bool recursive = true);
183
184 std::string GetTestAndroidRoot();
185
David Sehr7d432422018-05-25 10:49:02 -0700186 // Open a file (allows reading of framework jars).
187 std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename);
Mathieu Chartierc2109c62019-03-20 13:34:39 -0700188
189 // Open a single dex file (aborts if there are more than one).
190 std::unique_ptr<const DexFile> OpenDexFile(const char* filename);
191
David Sehr7d432422018-05-25 10:49:02 -0700192 // Open a test file (art-gtest-*.jar).
David Sehrd5f8de82018-04-27 14:12:03 -0700193 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
194
195 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
196
197
198 std::string android_data_;
199 std::string dalvik_cache_;
200
201 virtual void SetUp();
202
203 virtual void TearDown();
204
205 // Creates the class path string for the given dex files (the list of dex file locations
206 // separated by ':').
207 std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
208 // Same as CreateClassPath but add the dex file checksum after each location. The separator
209 // is '*'.
210 std::string CreateClassPathWithChecksums(
211 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
212
213 static std::string GetCoreFileLocation(const char* suffix);
214
215 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
216};
217
218template <typename TestType>
219class CommonArtTestBase : public TestType, public CommonArtTestImpl {
220 public:
221 CommonArtTestBase() {}
222 virtual ~CommonArtTestBase() {}
223
224 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100225 void SetUp() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700226 CommonArtTestImpl::SetUp();
227 }
228
Roland Levillainf73caca2018-08-24 17:19:07 +0100229 void TearDown() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700230 CommonArtTestImpl::TearDown();
231 }
232};
233
234using CommonArtTest = CommonArtTestBase<testing::Test>;
235
236template <typename Param>
237using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
238
239#define TEST_DISABLED_FOR_TARGET() \
240 if (kIsTargetBuild) { \
241 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
242 return; \
243 }
244
245#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
246 if (!kHostStaticBuildEnabled) { \
247 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
248 return; \
249 }
250
251#define TEST_DISABLED_FOR_MEMORY_TOOL() \
Roland Levillain05e34f42018-05-24 13:19:05 +0000252 if (kRunningOnMemoryTool) { \
David Sehrd5f8de82018-04-27 14:12:03 -0700253 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
254 return; \
255 }
256
David Sehrd5f8de82018-04-27 14:12:03 -0700257#define TEST_DISABLED_FOR_HEAP_POISONING() \
258 if (kPoisonHeapReferences) { \
259 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
260 return; \
261 }
262} // namespace art
263
Roland Levillainab4326e2018-06-19 18:10:55 +0000264#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
265 if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
266 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
267 return; \
268 }
269
David Sehrd5f8de82018-04-27 14:12:03 -0700270#endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_