blob: a1f7cbb107d7d6f0bb1f2b1acc1ac1c5909e4692 [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
David Sehrd5f8de82018-04-27 14:12:03 -070041class DexFile;
42
43class ScratchFile {
44 public:
45 ScratchFile();
46
47 explicit ScratchFile(const std::string& filename);
48
49 ScratchFile(const ScratchFile& other, const char* suffix);
50
Andreas Gampe44b31742018-10-01 19:30:57 -070051 ScratchFile(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070052
Andreas Gampe44b31742018-10-01 19:30:57 -070053 ScratchFile& operator=(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070054
55 explicit ScratchFile(File* file);
56
57 ~ScratchFile();
58
59 const std::string& GetFilename() const {
60 return filename_;
61 }
62
63 File* GetFile() const {
64 return file_.get();
65 }
66
67 int GetFd() const;
68
69 void Close();
70 void Unlink();
71
72 private:
73 std::string filename_;
74 std::unique_ptr<File> file_;
75};
76
77class CommonArtTestImpl {
78 public:
79 CommonArtTestImpl() = default;
80 virtual ~CommonArtTestImpl() = default;
81
Neil Fuller26a5dd62019-03-13 15:16:35 +000082 // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_RUNTIME_ROOT,
83 // and ANDROID_TZDATA_ROOT environment variables using sensible defaults if not already set.
Neil Fuller26c43772018-11-23 17:56:43 +000084 static void SetUpAndroidRootEnvVars();
David Sehrd5f8de82018-04-27 14:12:03 -070085
Neil Fuller26c43772018-11-23 17:56:43 +000086 // Set up the ANDROID_DATA environment variable, creating the directory if required.
David Sehrd5f8de82018-04-27 14:12:03 -070087 // 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.
Neil Fuller26c43772018-11-23 17:56:43 +000089 static void SetUpAndroidDataDir(std::string& android_data);
David Sehrd5f8de82018-04-27 14:12:03 -070090
Neil Fuller26c43772018-11-23 17:56:43 +000091 static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error);
David Sehrd5f8de82018-04-27 14:12:03 -070092
Vladimir Markob9c29f62019-03-20 14:22:51 +000093 // Get the names of the libcore modules.
94 virtual std::vector<std::string> GetLibCoreModuleNames() const;
95
96 // Gets the paths of the libcore dex files for given modules.
97 std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const;
98
David Sehrd5f8de82018-04-27 14:12:03 -070099 // Gets the paths of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000100 std::vector<std::string> GetLibCoreDexFileNames() const;
101
102 // Gets the locations of the libcore dex files for given modules.
103 std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const;
David Sehrd5f8de82018-04-27 14:12:03 -0700104
Vladimir Marko7a85e702018-12-03 18:47:23 +0000105 // Gets the locations of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000106 std::vector<std::string> GetLibCoreDexLocations() const;
Vladimir Marko7a85e702018-12-03 18:47:23 +0000107
108 static std::string GetClassPathOption(const char* option,
109 const std::vector<std::string>& class_path);
110
David Sehrd5f8de82018-04-27 14:12:03 -0700111 // Returns bin directory which contains host's prebuild tools.
112 static std::string GetAndroidHostToolsDir();
113
114 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
115 std::string GetTestDexFileName(const char* name) const;
116
117 template <typename Mutator>
118 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
119 std::vector<std::unique_ptr<const DexFile>> dex_files;
120 std::string error_msg;
121 const ArtDexFileLoader dex_file_loader;
122 CHECK(dex_file_loader.Open(input_jar.c_str(),
123 input_jar.c_str(),
124 /*verify*/ true,
125 /*verify_checksum*/ true,
126 &error_msg,
127 &dex_files)) << error_msg;
128 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
129 const std::unique_ptr<const DexFile>& dex = dex_files[0];
130 CHECK(dex->EnableWrite()) << "Failed to enable write";
131 DexFile* dex_file = const_cast<DexFile*>(dex.get());
132 mutator(dex_file);
133 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
134 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
135 return false;
136 }
137 if (output_dex->Flush() != 0) {
138 PLOG(FATAL) << "Could not flush the output file.";
139 }
140 return true;
141 }
142
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700143 struct ForkAndExecResult {
144 enum Stage {
145 kLink,
146 kFork,
147 kWaitpid,
148 kFinished,
149 };
150 Stage stage;
151 int status_code;
152
153 bool StandardSuccess() {
154 return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
155 }
156 };
157 using OutputHandlerFn = std::function<void(char*, size_t)>;
158 using PostForkFn = std::function<bool()>;
159 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
160 const PostForkFn& post_fork,
161 const OutputHandlerFn& handler);
162 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
163 const PostForkFn& post_fork,
164 std::string* output);
165
David Sehrd5f8de82018-04-27 14:12:03 -0700166 protected:
167 static bool IsHost() {
168 return !kIsTargetBuild;
169 }
170
171 // Helper - find directory with the following format:
172 // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
173 static std::string GetAndroidToolsDir(const std::string& subdir1,
174 const std::string& subdir2,
175 const std::string& subdir3);
176
177 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
178 static std::string GetCoreArtLocation();
179
180 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
181 static std::string GetCoreOatLocation();
182
183 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
184
185 void ClearDirectory(const char* dirpath, bool recursive = true);
186
187 std::string GetTestAndroidRoot();
188
David Sehr7d432422018-05-25 10:49:02 -0700189 // Open a file (allows reading of framework jars).
190 std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename);
Mathieu Chartierc2109c62019-03-20 13:34:39 -0700191
192 // Open a single dex file (aborts if there are more than one).
193 std::unique_ptr<const DexFile> OpenDexFile(const char* filename);
194
David Sehr7d432422018-05-25 10:49:02 -0700195 // Open a test file (art-gtest-*.jar).
David Sehrd5f8de82018-04-27 14:12:03 -0700196 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
197
198 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
199
200
201 std::string android_data_;
202 std::string dalvik_cache_;
203
204 virtual void SetUp();
205
206 virtual void TearDown();
207
208 // Creates the class path string for the given dex files (the list of dex file locations
209 // separated by ':').
210 std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
211 // Same as CreateClassPath but add the dex file checksum after each location. The separator
212 // is '*'.
213 std::string CreateClassPathWithChecksums(
214 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
215
216 static std::string GetCoreFileLocation(const char* suffix);
217
218 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
219};
220
221template <typename TestType>
222class CommonArtTestBase : public TestType, public CommonArtTestImpl {
223 public:
224 CommonArtTestBase() {}
225 virtual ~CommonArtTestBase() {}
226
227 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100228 void SetUp() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700229 CommonArtTestImpl::SetUp();
230 }
231
Roland Levillainf73caca2018-08-24 17:19:07 +0100232 void TearDown() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700233 CommonArtTestImpl::TearDown();
234 }
235};
236
237using CommonArtTest = CommonArtTestBase<testing::Test>;
238
239template <typename Param>
240using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
241
242#define TEST_DISABLED_FOR_TARGET() \
243 if (kIsTargetBuild) { \
244 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
245 return; \
246 }
247
248#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
249 if (!kHostStaticBuildEnabled) { \
250 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
251 return; \
252 }
253
254#define TEST_DISABLED_FOR_MEMORY_TOOL() \
Roland Levillain05e34f42018-05-24 13:19:05 +0000255 if (kRunningOnMemoryTool) { \
David Sehrd5f8de82018-04-27 14:12:03 -0700256 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
257 return; \
258 }
259
David Sehrd5f8de82018-04-27 14:12:03 -0700260#define TEST_DISABLED_FOR_HEAP_POISONING() \
261 if (kPoisonHeapReferences) { \
262 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
263 return; \
264 }
265} // namespace art
266
Roland Levillainab4326e2018-06-19 18:10:55 +0000267#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
268 if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
269 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
270 return; \
271 }
272
David Sehrd5f8de82018-04-27 14:12:03 -0700273#endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_