blob: 0f4800dfc59619c484b32002842d632b649535de [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 Fuller26c43772018-11-23 17:56:43 +000088 // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT and ANDROID_RUNTIME_ROOT
89 // environment variables using sensible defaults if not already set.
90 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
102 // Returns bin directory which contains host's prebuild tools.
103 static std::string GetAndroidHostToolsDir();
104
105 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
106 std::string GetTestDexFileName(const char* name) const;
107
108 template <typename Mutator>
109 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
110 std::vector<std::unique_ptr<const DexFile>> dex_files;
111 std::string error_msg;
112 const ArtDexFileLoader dex_file_loader;
113 CHECK(dex_file_loader.Open(input_jar.c_str(),
114 input_jar.c_str(),
115 /*verify*/ true,
116 /*verify_checksum*/ true,
117 &error_msg,
118 &dex_files)) << error_msg;
119 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
120 const std::unique_ptr<const DexFile>& dex = dex_files[0];
121 CHECK(dex->EnableWrite()) << "Failed to enable write";
122 DexFile* dex_file = const_cast<DexFile*>(dex.get());
123 mutator(dex_file);
124 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
125 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
126 return false;
127 }
128 if (output_dex->Flush() != 0) {
129 PLOG(FATAL) << "Could not flush the output file.";
130 }
131 return true;
132 }
133
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700134 struct ForkAndExecResult {
135 enum Stage {
136 kLink,
137 kFork,
138 kWaitpid,
139 kFinished,
140 };
141 Stage stage;
142 int status_code;
143
144 bool StandardSuccess() {
145 return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
146 }
147 };
148 using OutputHandlerFn = std::function<void(char*, size_t)>;
149 using PostForkFn = std::function<bool()>;
150 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
151 const PostForkFn& post_fork,
152 const OutputHandlerFn& handler);
153 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
154 const PostForkFn& post_fork,
155 std::string* output);
156
David Sehrd5f8de82018-04-27 14:12:03 -0700157 protected:
158 static bool IsHost() {
159 return !kIsTargetBuild;
160 }
161
162 // Helper - find directory with the following format:
163 // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
164 static std::string GetAndroidToolsDir(const std::string& subdir1,
165 const std::string& subdir2,
166 const std::string& subdir3);
167
168 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
169 static std::string GetCoreArtLocation();
170
171 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
172 static std::string GetCoreOatLocation();
173
174 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
175
176 void ClearDirectory(const char* dirpath, bool recursive = true);
177
178 std::string GetTestAndroidRoot();
179
David Sehr7d432422018-05-25 10:49:02 -0700180 // Open a file (allows reading of framework jars).
181 std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename);
182 // Open a test file (art-gtest-*.jar).
David Sehrd5f8de82018-04-27 14:12:03 -0700183 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
184
185 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
186
187
188 std::string android_data_;
189 std::string dalvik_cache_;
190
191 virtual void SetUp();
192
193 virtual void TearDown();
194
195 // Creates the class path string for the given dex files (the list of dex file locations
196 // separated by ':').
197 std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
198 // Same as CreateClassPath but add the dex file checksum after each location. The separator
199 // is '*'.
200 std::string CreateClassPathWithChecksums(
201 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
202
203 static std::string GetCoreFileLocation(const char* suffix);
204
205 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
206};
207
208template <typename TestType>
209class CommonArtTestBase : public TestType, public CommonArtTestImpl {
210 public:
211 CommonArtTestBase() {}
212 virtual ~CommonArtTestBase() {}
213
214 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100215 void SetUp() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700216 CommonArtTestImpl::SetUp();
217 }
218
Roland Levillainf73caca2018-08-24 17:19:07 +0100219 void TearDown() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700220 CommonArtTestImpl::TearDown();
221 }
222};
223
224using CommonArtTest = CommonArtTestBase<testing::Test>;
225
226template <typename Param>
227using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
228
229#define TEST_DISABLED_FOR_TARGET() \
230 if (kIsTargetBuild) { \
231 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
232 return; \
233 }
234
235#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
236 if (!kHostStaticBuildEnabled) { \
237 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
238 return; \
239 }
240
241#define TEST_DISABLED_FOR_MEMORY_TOOL() \
Roland Levillain05e34f42018-05-24 13:19:05 +0000242 if (kRunningOnMemoryTool) { \
David Sehrd5f8de82018-04-27 14:12:03 -0700243 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
244 return; \
245 }
246
David Sehrd5f8de82018-04-27 14:12:03 -0700247#define TEST_DISABLED_FOR_HEAP_POISONING() \
248 if (kPoisonHeapReferences) { \
249 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
250 return; \
251 }
252} // namespace art
253
Roland Levillainab4326e2018-06-19 18:10:55 +0000254#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
255 if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
256 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
257 return; \
258 }
259
David Sehrd5f8de82018-04-27 14:12:03 -0700260#endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_