blob: 09264a7cb667784a2777f385f667df98f22e2af9 [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
Roland Levillainfb6a5c02019-03-29 20:20:16 +000029#include "base/file_utils.h"
David Sehrd5f8de82018-04-27 14:12:03 -070030#include "base/globals.h"
31#include "base/mutex.h"
32#include "base/os.h"
33#include "base/unix_file/fd_file.h"
34#include "dex/art_dex_file_loader.h"
35#include "dex/compact_dex_level.h"
36
37namespace art {
38
39using LogSeverity = android::base::LogSeverity;
40using ScopedLogSeverity = android::base::ScopedLogSeverity;
41
David Sehrd5f8de82018-04-27 14:12:03 -070042class DexFile;
43
44class ScratchFile {
45 public:
46 ScratchFile();
47
48 explicit ScratchFile(const std::string& filename);
49
50 ScratchFile(const ScratchFile& other, const char* suffix);
51
Andreas Gampe44b31742018-10-01 19:30:57 -070052 ScratchFile(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070053
Andreas Gampe44b31742018-10-01 19:30:57 -070054 ScratchFile& operator=(ScratchFile&& other) noexcept;
David Sehrd5f8de82018-04-27 14:12:03 -070055
56 explicit ScratchFile(File* file);
57
58 ~ScratchFile();
59
60 const std::string& GetFilename() const {
61 return filename_;
62 }
63
64 File* GetFile() const {
65 return file_.get();
66 }
67
68 int GetFd() const;
69
70 void Close();
71 void Unlink();
72
73 private:
74 std::string filename_;
75 std::unique_ptr<File> file_;
76};
77
78class CommonArtTestImpl {
79 public:
80 CommonArtTestImpl() = default;
81 virtual ~CommonArtTestImpl() = default;
82
Neil Fuller26a5dd62019-03-13 15:16:35 +000083 // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_RUNTIME_ROOT,
84 // and ANDROID_TZDATA_ROOT environment variables using sensible defaults if not already set.
Neil Fuller26c43772018-11-23 17:56:43 +000085 static void SetUpAndroidRootEnvVars();
David Sehrd5f8de82018-04-27 14:12:03 -070086
Neil Fuller26c43772018-11-23 17:56:43 +000087 // Set up the ANDROID_DATA environment variable, creating the directory if required.
David Sehrd5f8de82018-04-27 14:12:03 -070088 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
89 // non-derived class, be sure to also call the corresponding tear-down below.
Neil Fuller26c43772018-11-23 17:56:43 +000090 static void SetUpAndroidDataDir(std::string& android_data);
David Sehrd5f8de82018-04-27 14:12:03 -070091
Neil Fuller26c43772018-11-23 17:56:43 +000092 static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error);
David Sehrd5f8de82018-04-27 14:12:03 -070093
Vladimir Markob9c29f62019-03-20 14:22:51 +000094 // Get the names of the libcore modules.
95 virtual std::vector<std::string> GetLibCoreModuleNames() const;
96
97 // Gets the paths of the libcore dex files for given modules.
98 std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const;
99
David Sehrd5f8de82018-04-27 14:12:03 -0700100 // Gets the paths of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000101 std::vector<std::string> GetLibCoreDexFileNames() const;
102
103 // Gets the locations of the libcore dex files for given modules.
104 std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const;
David Sehrd5f8de82018-04-27 14:12:03 -0700105
Vladimir Marko7a85e702018-12-03 18:47:23 +0000106 // Gets the locations of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000107 std::vector<std::string> GetLibCoreDexLocations() const;
Vladimir Marko7a85e702018-12-03 18:47:23 +0000108
109 static std::string GetClassPathOption(const char* option,
110 const std::vector<std::string>& class_path);
111
David Sehrd5f8de82018-04-27 14:12:03 -0700112 // Returns bin directory which contains host's prebuild tools.
113 static std::string GetAndroidHostToolsDir();
114
115 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
116 std::string GetTestDexFileName(const char* name) const;
117
118 template <typename Mutator>
119 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
120 std::vector<std::unique_ptr<const DexFile>> dex_files;
121 std::string error_msg;
122 const ArtDexFileLoader dex_file_loader;
123 CHECK(dex_file_loader.Open(input_jar.c_str(),
124 input_jar.c_str(),
125 /*verify*/ true,
126 /*verify_checksum*/ true,
127 &error_msg,
128 &dex_files)) << error_msg;
129 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
130 const std::unique_ptr<const DexFile>& dex = dex_files[0];
131 CHECK(dex->EnableWrite()) << "Failed to enable write";
132 DexFile* dex_file = const_cast<DexFile*>(dex.get());
133 mutator(dex_file);
134 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
135 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
136 return false;
137 }
138 if (output_dex->Flush() != 0) {
139 PLOG(FATAL) << "Could not flush the output file.";
140 }
141 return true;
142 }
143
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700144 struct ForkAndExecResult {
145 enum Stage {
146 kLink,
147 kFork,
148 kWaitpid,
149 kFinished,
150 };
151 Stage stage;
152 int status_code;
153
154 bool StandardSuccess() {
155 return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
156 }
157 };
158 using OutputHandlerFn = std::function<void(char*, size_t)>;
159 using PostForkFn = std::function<bool()>;
160 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
161 const PostForkFn& post_fork,
162 const OutputHandlerFn& handler);
163 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
164 const PostForkFn& post_fork,
165 std::string* output);
166
David Sehrd5f8de82018-04-27 14:12:03 -0700167 protected:
168 static bool IsHost() {
169 return !kIsTargetBuild;
170 }
171
172 // Helper - find directory with the following format:
173 // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
174 static std::string GetAndroidToolsDir(const std::string& subdir1,
175 const std::string& subdir2,
176 const std::string& subdir3);
177
178 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
179 static std::string GetCoreArtLocation();
180
181 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
182 static std::string GetCoreOatLocation();
183
184 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
185
186 void ClearDirectory(const char* dirpath, bool recursive = true);
187
David Sehr7d432422018-05-25 10:49:02 -0700188 // Open a file (allows reading of framework jars).
189 std::vector<std::unique_ptr<const DexFile>> OpenDexFiles(const char* filename);
Mathieu Chartierc2109c62019-03-20 13:34:39 -0700190
191 // Open a single dex file (aborts if there are more than one).
192 std::unique_ptr<const DexFile> OpenDexFile(const char* filename);
193
David Sehr7d432422018-05-25 10:49:02 -0700194 // Open a test file (art-gtest-*.jar).
David Sehrd5f8de82018-04-27 14:12:03 -0700195 std::vector<std::unique_ptr<const DexFile>> OpenTestDexFiles(const char* name);
196
197 std::unique_ptr<const DexFile> OpenTestDexFile(const char* name);
198
199
200 std::string android_data_;
201 std::string dalvik_cache_;
202
203 virtual void SetUp();
204
205 virtual void TearDown();
206
207 // Creates the class path string for the given dex files (the list of dex file locations
208 // separated by ':').
209 std::string CreateClassPath(const std::vector<std::unique_ptr<const DexFile>>& dex_files);
210 // Same as CreateClassPath but add the dex file checksum after each location. The separator
211 // is '*'.
212 std::string CreateClassPathWithChecksums(
213 const std::vector<std::unique_ptr<const DexFile>>& dex_files);
214
215 static std::string GetCoreFileLocation(const char* suffix);
216
217 std::vector<std::unique_ptr<const DexFile>> loaded_dex_files_;
218};
219
220template <typename TestType>
221class CommonArtTestBase : public TestType, public CommonArtTestImpl {
222 public:
223 CommonArtTestBase() {}
224 virtual ~CommonArtTestBase() {}
225
226 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100227 void SetUp() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700228 CommonArtTestImpl::SetUp();
229 }
230
Roland Levillainf73caca2018-08-24 17:19:07 +0100231 void TearDown() override {
David Sehrd5f8de82018-04-27 14:12:03 -0700232 CommonArtTestImpl::TearDown();
233 }
234};
235
236using CommonArtTest = CommonArtTestBase<testing::Test>;
237
238template <typename Param>
239using CommonArtTestWithParam = CommonArtTestBase<testing::TestWithParam<Param>>;
240
241#define TEST_DISABLED_FOR_TARGET() \
242 if (kIsTargetBuild) { \
243 printf("WARNING: TEST DISABLED FOR TARGET\n"); \
244 return; \
245 }
246
247#define TEST_DISABLED_FOR_NON_STATIC_HOST_BUILDS() \
248 if (!kHostStaticBuildEnabled) { \
249 printf("WARNING: TEST DISABLED FOR NON-STATIC HOST BUILDS\n"); \
250 return; \
251 }
252
253#define TEST_DISABLED_FOR_MEMORY_TOOL() \
Roland Levillain05e34f42018-05-24 13:19:05 +0000254 if (kRunningOnMemoryTool) { \
David Sehrd5f8de82018-04-27 14:12:03 -0700255 printf("WARNING: TEST DISABLED FOR MEMORY TOOL\n"); \
256 return; \
257 }
258
David Sehrd5f8de82018-04-27 14:12:03 -0700259#define TEST_DISABLED_FOR_HEAP_POISONING() \
260 if (kPoisonHeapReferences) { \
261 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
262 return; \
263 }
264} // namespace art
265
Roland Levillainab4326e2018-06-19 18:10:55 +0000266#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING() \
267 if (kRunningOnMemoryTool && kPoisonHeapReferences) { \
268 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING\n"); \
269 return; \
270 }
271
David Sehrd5f8de82018-04-27 14:12:03 -0700272#endif // ART_LIBARTBASE_BASE_COMMON_ART_TEST_H_