blob: ef1c1aef464cf0a1824d1c092cf6be1fbfafbe23 [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
Victor Chang64611242019-07-05 16:32:41 +010083 // Set up ANDROID_BUILD_TOP, ANDROID_HOST_OUT, ANDROID_ROOT, ANDROID_I18N_ROOT,
Martin Stjernholme58624f2019-09-20 15:53:40 +010084 // ANDROID_ART_ROOT, and ANDROID_TZDATA_ROOT environment variables using sensible defaults
Victor Chang64611242019-07-05 16:32:41 +010085 // if not already set.
Neil Fuller26c43772018-11-23 17:56:43 +000086 static void SetUpAndroidRootEnvVars();
David Sehrd5f8de82018-04-27 14:12:03 -070087
Neil Fuller26c43772018-11-23 17:56:43 +000088 // Set up the ANDROID_DATA environment variable, creating the directory if required.
David Sehrd5f8de82018-04-27 14:12:03 -070089 // Note: setting up ANDROID_DATA may create a temporary directory. If this is used in a
90 // non-derived class, be sure to also call the corresponding tear-down below.
Neil Fuller26c43772018-11-23 17:56:43 +000091 static void SetUpAndroidDataDir(std::string& android_data);
David Sehrd5f8de82018-04-27 14:12:03 -070092
Neil Fuller26c43772018-11-23 17:56:43 +000093 static void TearDownAndroidDataDir(const std::string& android_data, bool fail_on_error);
David Sehrd5f8de82018-04-27 14:12:03 -070094
Vladimir Markob9c29f62019-03-20 14:22:51 +000095 // Get the names of the libcore modules.
96 virtual std::vector<std::string> GetLibCoreModuleNames() const;
97
98 // Gets the paths of the libcore dex files for given modules.
99 std::vector<std::string> GetLibCoreDexFileNames(const std::vector<std::string>& modules) const;
100
David Sehrd5f8de82018-04-27 14:12:03 -0700101 // Gets the paths of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000102 std::vector<std::string> GetLibCoreDexFileNames() const;
103
104 // Gets the locations of the libcore dex files for given modules.
105 std::vector<std::string> GetLibCoreDexLocations(const std::vector<std::string>& modules) const;
David Sehrd5f8de82018-04-27 14:12:03 -0700106
Vladimir Marko7a85e702018-12-03 18:47:23 +0000107 // Gets the locations of the libcore dex files.
Vladimir Markob9c29f62019-03-20 14:22:51 +0000108 std::vector<std::string> GetLibCoreDexLocations() const;
Vladimir Marko7a85e702018-12-03 18:47:23 +0000109
110 static std::string GetClassPathOption(const char* option,
111 const std::vector<std::string>& class_path);
112
David Sehrd5f8de82018-04-27 14:12:03 -0700113 // Returns bin directory which contains host's prebuild tools.
114 static std::string GetAndroidHostToolsDir();
115
116 // Retuerns the filename for a test dex (i.e. XandY or ManyMethods).
117 std::string GetTestDexFileName(const char* name) const;
118
119 template <typename Mutator>
120 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
121 std::vector<std::unique_ptr<const DexFile>> dex_files;
122 std::string error_msg;
123 const ArtDexFileLoader dex_file_loader;
124 CHECK(dex_file_loader.Open(input_jar.c_str(),
125 input_jar.c_str(),
126 /*verify*/ true,
127 /*verify_checksum*/ true,
128 &error_msg,
129 &dex_files)) << error_msg;
130 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
131 const std::unique_ptr<const DexFile>& dex = dex_files[0];
132 CHECK(dex->EnableWrite()) << "Failed to enable write";
133 DexFile* dex_file = const_cast<DexFile*>(dex.get());
134 mutator(dex_file);
135 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
136 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
137 return false;
138 }
139 if (output_dex->Flush() != 0) {
140 PLOG(FATAL) << "Could not flush the output file.";
141 }
142 return true;
143 }
144
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700145 struct ForkAndExecResult {
146 enum Stage {
147 kLink,
148 kFork,
149 kWaitpid,
150 kFinished,
151 };
152 Stage stage;
153 int status_code;
154
155 bool StandardSuccess() {
156 return stage == kFinished && WIFEXITED(status_code) && WEXITSTATUS(status_code) == 0;
157 }
158 };
159 using OutputHandlerFn = std::function<void(char*, size_t)>;
160 using PostForkFn = std::function<bool()>;
161 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
162 const PostForkFn& post_fork,
163 const OutputHandlerFn& handler);
164 static ForkAndExecResult ForkAndExec(const std::vector<std::string>& argv,
165 const PostForkFn& post_fork,
166 std::string* output);
167
David Sehrd5f8de82018-04-27 14:12:03 -0700168 protected:
169 static bool IsHost() {
170 return !kIsTargetBuild;
171 }
172
173 // Helper - find directory with the following format:
174 // ${ANDROID_BUILD_TOP}/${subdir1}/${subdir2}-${version}/${subdir3}/bin/
175 static std::string GetAndroidToolsDir(const std::string& subdir1,
176 const std::string& subdir2,
177 const std::string& subdir3);
178
179 // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
180 static std::string GetCoreArtLocation();
181
182 // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
183 static std::string GetCoreOatLocation();
184
185 std::unique_ptr<const DexFile> LoadExpectSingleDexFile(const char* location);
186
187 void ClearDirectory(const char* dirpath, bool recursive = true);
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_