blob: c59dafcb3805da8f5f9ca13a687d380adba4a378 [file] [log] [blame]
Richard Uhler66d874d2015-01-15 09:37:19 -08001/*
2 * Copyright (C) 2014 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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "oat_file_assistant.h"
18
Richard Uhler66d874d2015-01-15 09:37:19 -080019#include <sys/param.h>
20
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070021#include <string>
22#include <vector>
23
Richard Uhler66d874d2015-01-15 09:37:19 -080024#include <gtest/gtest.h>
25
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "android-base/strings.h"
27
Mathieu Chartierc7853442015-03-27 14:35:38 -070028#include "art_field-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010029#include "class_linker-inl.h"
Jeff Hao0cb17282017-07-12 14:51:49 -070030#include "common_runtime_test.h"
Calin Juravle36eb3132017-01-13 16:32:38 -080031#include "dexopt_test.h"
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -070032#include "oat_file_manager.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080033#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070035#include "thread-current-inl.h"
Richard Uhler66d874d2015-01-15 09:37:19 -080036#include "utils.h"
37
38namespace art {
39
Calin Juravle36eb3132017-01-13 16:32:38 -080040class OatFileAssistantTest : public DexoptTest {};
Richard Uhler66d874d2015-01-15 09:37:19 -080041
Calin Juravle36eb3132017-01-13 16:32:38 -080042class OatFileAssistantNoDex2OatTest : public DexoptTest {
Richard Uhler66d874d2015-01-15 09:37:19 -080043 public:
44 virtual void SetUpRuntimeOptions(RuntimeOptions* options) {
Calin Juravle36eb3132017-01-13 16:32:38 -080045 DexoptTest::SetUpRuntimeOptions(options);
Richard Uhler66d874d2015-01-15 09:37:19 -080046 options->push_back(std::make_pair("-Xnodex2oat", nullptr));
47 }
48};
49
Calin Juravle357c66d2017-05-04 01:57:17 +000050class ScopedNonWritable {
51 public:
52 explicit ScopedNonWritable(const std::string& dex_location) {
53 is_valid_ = false;
54 size_t pos = dex_location.rfind('/');
55 if (pos != std::string::npos) {
56 is_valid_ = true;
57 dex_parent_ = dex_location.substr(0, pos);
58 if (chmod(dex_parent_.c_str(), 0555) != 0) {
59 PLOG(ERROR) << "Could not change permissions on " << dex_parent_;
60 }
61 }
62 }
63
64 bool IsSuccessful() { return is_valid_ && (access(dex_parent_.c_str(), W_OK) != 0); }
65
66 ~ScopedNonWritable() {
67 if (is_valid_) {
68 if (chmod(dex_parent_.c_str(), 0777) != 0) {
69 PLOG(ERROR) << "Could not restore permissions on " << dex_parent_;
70 }
71 }
72 }
73
74 private:
75 std::string dex_parent_;
76 bool is_valid_;
77};
78
79static bool IsExecutedAsRoot() {
80 return geteuid() == 0;
81}
Calin Juravle36eb3132017-01-13 16:32:38 -080082
Richard Uhler66d874d2015-01-15 09:37:19 -080083// Case: We have a DEX file, but no OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -070084// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -080085TEST_F(OatFileAssistantTest, DexNoOat) {
86 std::string dex_location = GetScratchDir() + "/DexNoOat.jar";
87 Copy(GetDexSrc1(), dex_location);
88
Richard Uhlerd1472a22016-04-15 15:18:56 -070089 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -080090
Richard Uhler7225a8d2016-11-22 10:12:03 +000091 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +010092 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +000093 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +010094 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +000095 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +000096 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile));
Richard Uhler7225a8d2016-11-22 10:12:03 +000097 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +000098 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -080099
100 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000101 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
102 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700103 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800104}
105
106// Case: We have no DEX file and no OAT file.
Richard Uhler9b994ea2015-06-24 08:44:19 -0700107// Expect: Status is kNoDexOptNeeded. Loading should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800108TEST_F(OatFileAssistantTest, NoDexNoOat) {
109 std::string dex_location = GetScratchDir() + "/NoDexNoOat.jar";
110
Richard Uhlerd1472a22016-04-15 15:18:56 -0700111 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800112
Andreas Gampe29d38e72016-03-23 15:31:51 +0000113 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
114 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700115 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
116
117 // Trying to make the oat file up to date should not fail or crash.
118 std::string error_msg;
Richard Uhlerd1472a22016-04-15 15:18:56 -0700119 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded, oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700120
121 // Trying to get the best oat file should fail, but not crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800122 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
123 EXPECT_EQ(nullptr, oat_file.get());
124}
125
Calin Juravle357c66d2017-05-04 01:57:17 +0000126// Case: We have a DEX file and a PIC ODEX file, but no OAT file.
127// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
128TEST_F(OatFileAssistantTest, OdexUpToDate) {
129 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
130 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
131 Copy(GetDexSrc1(), dex_location);
132 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
133
134 // For the use of oat location by making the dex parent not writable.
135 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
136
137 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
138 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
139 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
140 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
141 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
142 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
143 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
144 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
145
146 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
147 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
148 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
149 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
150}
151
152// Case: We have a DEX file and a PIC ODEX file, but no OAT file. We load the dex
153// file via a symlink.
154// Expect: The status is kNoDexOptNeeded, because PIC needs no relocation.
155TEST_F(OatFileAssistantTest, OdexUpToDateSymLink) {
156 std::string scratch_dir = GetScratchDir();
157 std::string dex_location = GetScratchDir() + "/OdexUpToDate.jar";
158 std::string odex_location = GetOdexDir() + "/OdexUpToDate.odex";
159
160 Copy(GetDexSrc1(), dex_location);
161 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
162
163 // Now replace the dex location with a symlink.
164 std::string link = scratch_dir + "/link";
165 ASSERT_EQ(0, symlink(scratch_dir.c_str(), link.c_str()));
166 dex_location = link + "/OdexUpToDate.jar";
167
168 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
169
170 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
171 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
172 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
173 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
174 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
175 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
176 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
177 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
178
179 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
180 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
181 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
182 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
183}
184
Richard Uhler66d874d2015-01-15 09:37:19 -0800185// Case: We have a DEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700186// Expect: The status is kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -0800187TEST_F(OatFileAssistantTest, OatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000188 if (IsExecutedAsRoot()) {
189 // We cannot simulate non writable locations when executed as root: b/38000545.
190 LOG(ERROR) << "Test skipped because it's running as root";
191 return;
192 }
193
Richard Uhler66d874d2015-01-15 09:37:19 -0800194 std::string dex_location = GetScratchDir() + "/OatUpToDate.jar";
195 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000196 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800197
Calin Juravle357c66d2017-05-04 01:57:17 +0000198 // For the use of oat location by making the dex parent not writable.
199 ScopedNonWritable scoped_non_writable(dex_location);
200 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
201
202 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
203
204 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
205 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
206 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
207 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
208 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
209 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
210 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
211 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
212
213 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
214 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
215 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
216 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
217}
218
219// Case: We have a DEX file and up-to-date OAT file for it. We load the dex file
220// via a symlink.
221// Expect: The status is kNoDexOptNeeded.
222TEST_F(OatFileAssistantTest, OatUpToDateSymLink) {
223 if (IsExecutedAsRoot()) {
224 // We cannot simulate non writable locations when executed as root: b/38000545.
225 LOG(ERROR) << "Test skipped because it's running as root";
226 return;
227 }
228
229 std::string real = GetScratchDir() + "/real";
230 ASSERT_EQ(0, mkdir(real.c_str(), 0700));
231 std::string link = GetScratchDir() + "/link";
232 ASSERT_EQ(0, symlink(real.c_str(), link.c_str()));
233
234 std::string dex_location = real + "/OatUpToDate.jar";
235
236 Copy(GetDexSrc1(), dex_location);
237 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
238
239 // Update the dex location to point to the symlink.
240 dex_location = link + "/OatUpToDate.jar";
241
242 // For the use of oat location by making the dex parent not writable.
243 ScopedNonWritable scoped_non_writable(dex_location);
244 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
245
Richard Uhlerd1472a22016-04-15 15:18:56 -0700246 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800247
Andreas Gampe29d38e72016-03-23 15:31:51 +0000248 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
249 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
250 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100251 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000252 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100253 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000254 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000255 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
256
Richard Uhler66d874d2015-01-15 09:37:19 -0800257 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000258 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Richard Uhler95abd042015-03-24 09:51:28 -0700259 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700260 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800261}
262
Richard Uhler2f27abd2017-01-31 14:02:34 +0000263// Case: We have a DEX file and up-to-date (ODEX) VDEX file for it, but no
264// ODEX file.
265TEST_F(OatFileAssistantTest, VdexUpToDateNoOdex) {
266 // This test case is only meaningful if vdex is enabled.
267 if (!kIsVdexEnabled) {
268 return;
269 }
Richard Uhler9a37efc2016-08-05 16:32:55 -0700270
Richard Uhler2f27abd2017-01-31 14:02:34 +0000271 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOdex.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000272 std::string odex_location = GetOdexDir() + "/VdexUpToDateNoOdex.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000273
Richard Uhler9a37efc2016-08-05 16:32:55 -0700274 Copy(GetDexSrc1(), dex_location);
275
Richard Uhler2f27abd2017-01-31 14:02:34 +0000276 // Generating and deleting the oat file should have the side effect of
277 // creating an up-to-date vdex file.
Calin Juravle357c66d2017-05-04 01:57:17 +0000278 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
279 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000280
Calin Juravle357c66d2017-05-04 01:57:17 +0000281 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000282
283 // Even though the vdex file is up to date, because we don't have the oat
284 // file, we can't know that the vdex depends on the boot image and is up to
285 // date with respect to the boot image. Instead we must assume the vdex file
286 // depends on the boot image and is out of date with respect to the boot
287 // image.
288 EXPECT_EQ(-OatFileAssistant::kDex2OatForBootImage,
289 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
290
291 // Make sure we don't crash in this case when we dump the status. We don't
292 // care what the actual dumped value is.
293 oat_file_assistant.GetStatusDump();
294}
295
296// Case: We have a DEX file and empty VDEX and ODEX files.
297TEST_F(OatFileAssistantTest, EmptyVdexOdex) {
298 std::string dex_location = GetScratchDir() + "/EmptyVdexOdex.jar";
299 std::string odex_location = GetOdexDir() + "/EmptyVdexOdex.oat";
300 std::string vdex_location = GetOdexDir() + "/EmptyVdexOdex.vdex";
301
302 Copy(GetDexSrc1(), dex_location);
Richard Uhler5cd59292017-02-01 12:54:23 +0000303 ScratchFile vdex_file(vdex_location.c_str());
304 ScratchFile odex_file(odex_location.c_str());
Richard Uhler2f27abd2017-01-31 14:02:34 +0000305
306 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
307 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
308 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
309}
310
311// Case: We have a DEX file and up-to-date (OAT) VDEX file for it, but no OAT
312// file.
313TEST_F(OatFileAssistantTest, VdexUpToDateNoOat) {
314 // This test case is only meaningful if vdex is enabled.
315 if (!kIsVdexEnabled) {
316 return;
317 }
Calin Juravle357c66d2017-05-04 01:57:17 +0000318 if (IsExecutedAsRoot()) {
319 // We cannot simulate non writable locations when executed as root: b/38000545.
320 LOG(ERROR) << "Test skipped because it's running as root";
321 return;
322 }
Richard Uhler2f27abd2017-01-31 14:02:34 +0000323
324 std::string dex_location = GetScratchDir() + "/VdexUpToDateNoOat.jar";
325 std::string oat_location;
326 std::string error_msg;
327 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
328 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
329
330 Copy(GetDexSrc1(), dex_location);
331 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
332 ASSERT_EQ(0, unlink(oat_location.c_str()));
333
Calin Juravle357c66d2017-05-04 01:57:17 +0000334 ScopedNonWritable scoped_non_writable(dex_location);
335 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
Richard Uhler9a37efc2016-08-05 16:32:55 -0700336 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
337
Richard Uhler2f27abd2017-01-31 14:02:34 +0000338 // Even though the vdex file is up to date, because we don't have the oat
339 // file, we can't know that the vdex depends on the boot image and is up to
340 // date with respect to the boot image. Instead we must assume the vdex file
341 // depends on the boot image and is out of date with respect to the boot
342 // image.
343 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler9a37efc2016-08-05 16:32:55 -0700344 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9a37efc2016-08-05 16:32:55 -0700345}
346
Andreas Gampe29d38e72016-03-23 15:31:51 +0000347// Case: We have a DEX file and speed-profile OAT file for it.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700348// Expect: The status is kNoDexOptNeeded if the profile hasn't changed, but
349// kDex2Oat if the profile has changed.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000350TEST_F(OatFileAssistantTest, ProfileOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000351 if (IsExecutedAsRoot()) {
352 // We cannot simulate non writable locations when executed as root: b/38000545.
353 LOG(ERROR) << "Test skipped because it's running as root";
354 return;
355 }
356
Andreas Gampe29d38e72016-03-23 15:31:51 +0000357 std::string dex_location = GetScratchDir() + "/ProfileOatUpToDate.jar";
358 Copy(GetDexSrc1(), dex_location);
359 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeedProfile);
360
Calin Juravle357c66d2017-05-04 01:57:17 +0000361 ScopedNonWritable scoped_non_writable(dex_location);
362 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
363
Richard Uhlerd1472a22016-04-15 15:18:56 -0700364 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000365
366 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700367 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, false));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000368 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100369 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, false));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000370 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700371 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeedProfile, true));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000372 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100373 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken, true));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000374
375 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000376 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
Andreas Gampe29d38e72016-03-23 15:31:51 +0000377 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
378 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
379}
380
Richard Uhler66d874d2015-01-15 09:37:19 -0800381// Case: We have a MultiDEX file and up-to-date OAT file for it.
Richard Uhler95abd042015-03-24 09:51:28 -0700382// Expect: The status is kNoDexOptNeeded and we load all dex files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800383TEST_F(OatFileAssistantTest, MultiDexOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000384 if (IsExecutedAsRoot()) {
385 // We cannot simulate non writable locations when executed as root: b/38000545.
386 LOG(ERROR) << "Test skipped because it's running as root";
387 return;
388 }
389
Richard Uhler66d874d2015-01-15 09:37:19 -0800390 std::string dex_location = GetScratchDir() + "/MultiDexOatUpToDate.jar";
391 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000392 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800393
Calin Juravle357c66d2017-05-04 01:57:17 +0000394 ScopedNonWritable scoped_non_writable(dex_location);
395 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
396
Richard Uhlerd1472a22016-04-15 15:18:56 -0700397 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000398 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700399 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700400 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler95abd042015-03-24 09:51:28 -0700401
402 // Verify we can load both dex files.
Richard Uhlere5fed032015-03-18 08:21:11 -0700403 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
Richard Uhler66d874d2015-01-15 09:37:19 -0800404 ASSERT_TRUE(oat_file.get() != nullptr);
405 EXPECT_TRUE(oat_file->IsExecutable());
406 std::vector<std::unique_ptr<const DexFile>> dex_files;
Richard Uhlere5fed032015-03-18 08:21:11 -0700407 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
408 EXPECT_EQ(2u, dex_files.size());
409}
410
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000411// Case: We have a MultiDEX file where the non-main multdex entry is out of date.
Richard Uhler67ff7d12015-05-14 13:21:13 -0700412// Expect: The status is kDex2OatNeeded.
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000413TEST_F(OatFileAssistantTest, MultiDexNonMainOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000414 if (IsExecutedAsRoot()) {
415 // We cannot simulate non writable locations when executed as root: b/38000545.
416 LOG(ERROR) << "Test skipped because it's running as root";
417 return;
418 }
419
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000420 std::string dex_location = GetScratchDir() + "/MultiDexNonMainOutOfDate.jar";
Richard Uhler67ff7d12015-05-14 13:21:13 -0700421
422 // Compile code for GetMultiDexSrc1.
423 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000424 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler67ff7d12015-05-14 13:21:13 -0700425
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000426 // Now overwrite the dex file with GetMultiDexSrc2 so the non-main checksum
Richard Uhler67ff7d12015-05-14 13:21:13 -0700427 // is out of date.
428 Copy(GetMultiDexSrc2(), dex_location);
429
Calin Juravle357c66d2017-05-04 01:57:17 +0000430 ScopedNonWritable scoped_non_writable(dex_location);
431 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
432
Richard Uhlerd1472a22016-04-15 15:18:56 -0700433 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000434 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700435 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed, false));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700436 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler67ff7d12015-05-14 13:21:13 -0700437}
438
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000439// Case: We have a stripped MultiDEX file where the non-main multidex entry is
440// out of date with respect to the odex file.
441TEST_F(OatFileAssistantTest, StrippedMultiDexNonMainOutOfDate) {
442 std::string dex_location = GetScratchDir() + "/StrippedMultiDexNonMainOutOfDate.jar";
443 std::string odex_location = GetOdexDir() + "/StrippedMultiDexNonMainOutOfDate.odex";
444
445 // Compile the oat from GetMultiDexSrc1.
446 Copy(GetMultiDexSrc1(), dex_location);
447 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
448
449 // Compile the odex from GetMultiDexSrc2, which has a different non-main
450 // dex checksum.
451 Copy(GetMultiDexSrc2(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100452 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kQuicken);
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000453
454 // Strip the dex file.
455 Copy(GetStrippedDexSrc1(), dex_location);
456
457 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, /*load_executable*/false);
458
459 // Because the dex file is stripped, the odex file is considered the source
460 // of truth for the dex checksums. The oat file should be considered
461 // unusable.
462 std::unique_ptr<OatFile> best_file = oat_file_assistant.GetBestOatFile();
463 ASSERT_TRUE(best_file.get() != nullptr);
464 EXPECT_EQ(best_file->GetLocation(), odex_location);
465 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
466 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
467 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
468}
469
Calin Juravle357c66d2017-05-04 01:57:17 +0000470// Case: We have a MultiDEX file and up-to-date ODEX file for it with relative
Richard Uhlere5fed032015-03-18 08:21:11 -0700471// encoded dex locations.
Richard Uhler95abd042015-03-24 09:51:28 -0700472// Expect: The oat file status is kNoDexOptNeeded.
Richard Uhlere5fed032015-03-18 08:21:11 -0700473TEST_F(OatFileAssistantTest, RelativeEncodedDexLocation) {
474 std::string dex_location = GetScratchDir() + "/RelativeEncodedDexLocation.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000475 std::string odex_location = GetOdexDir() + "/RelativeEncodedDexLocation.odex";
Richard Uhlere5fed032015-03-18 08:21:11 -0700476
477 // Create the dex file
478 Copy(GetMultiDexSrc1(), dex_location);
479
480 // Create the oat file with relative encoded dex location.
481 std::vector<std::string> args;
482 args.push_back("--dex-file=" + dex_location);
483 args.push_back("--dex-location=" + std::string("RelativeEncodedDexLocation.jar"));
Calin Juravle357c66d2017-05-04 01:57:17 +0000484 args.push_back("--oat-file=" + odex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000485 args.push_back("--compiler-filter=speed");
Richard Uhlere5fed032015-03-18 08:21:11 -0700486
487 std::string error_msg;
Igor Murashkinb1d8c312015-08-04 11:18:43 -0700488 ASSERT_TRUE(OatFileAssistant::Dex2Oat(args, &error_msg)) << error_msg;
Richard Uhlere5fed032015-03-18 08:21:11 -0700489
490 // Verify we can load both dex files.
Calin Juravle357c66d2017-05-04 01:57:17 +0000491 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
492
Richard Uhlere5fed032015-03-18 08:21:11 -0700493 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
494 ASSERT_TRUE(oat_file.get() != nullptr);
495 EXPECT_TRUE(oat_file->IsExecutable());
496 std::vector<std::unique_ptr<const DexFile>> dex_files;
497 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
Richard Uhler66d874d2015-01-15 09:37:19 -0800498 EXPECT_EQ(2u, dex_files.size());
499}
500
Richard Uhler03bc6592016-11-22 09:42:04 +0000501// Case: We have a DEX file and an OAT file out of date with respect to the
502// dex checksum.
503TEST_F(OatFileAssistantTest, OatDexOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000504 if (IsExecutedAsRoot()) {
505 // We cannot simulate non writable locations when executed as root: b/38000545.
506 LOG(ERROR) << "Test skipped because it's running as root";
507 return;
508 }
509
Richard Uhler03bc6592016-11-22 09:42:04 +0000510 std::string dex_location = GetScratchDir() + "/OatDexOutOfDate.jar";
Richard Uhler66d874d2015-01-15 09:37:19 -0800511
512 // We create a dex, generate an oat for it, then overwrite the dex with a
513 // different dex to make the oat out of date.
514 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000515 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800516 Copy(GetDexSrc2(), dex_location);
517
Calin Juravle357c66d2017-05-04 01:57:17 +0000518 ScopedNonWritable scoped_non_writable(dex_location);
519 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
520
Richard Uhlerd1472a22016-04-15 15:18:56 -0700521 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000522 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100523 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000524 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000525 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800526
527 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000528 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
529 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
530 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
531}
532
Richard Uhler2f27abd2017-01-31 14:02:34 +0000533// Case: We have a DEX file and an (ODEX) VDEX file out of date with respect
534// to the dex checksum, but no ODEX file.
535TEST_F(OatFileAssistantTest, VdexDexOutOfDate) {
536 // This test case is only meaningful if vdex is enabled.
537 if (!kIsVdexEnabled) {
538 return;
539 }
540
541 std::string dex_location = GetScratchDir() + "/VdexDexOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000542 std::string odex_location = GetOdexDir() + "/VdexDexOutOfDate.oat";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000543
544 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000545 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
546 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000547 Copy(GetDexSrc2(), dex_location);
548
Calin Juravle357c66d2017-05-04 01:57:17 +0000549 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000550
551 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
552 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
553}
554
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000555// Case: We have a MultiDEX (ODEX) VDEX file where the non-main multidex entry
556// is out of date and there is no corresponding ODEX file.
557TEST_F(OatFileAssistantTest, VdexMultiDexNonMainOutOfDate) {
Richard Uhler2f27abd2017-01-31 14:02:34 +0000558 // This test case is only meaningful if vdex is enabled.
559 if (!kIsVdexEnabled) {
560 return;
561 }
562
Richard Uhler69bcf2c2017-01-24 10:25:21 +0000563 std::string dex_location = GetScratchDir() + "/VdexMultiDexNonMainOutOfDate.jar";
Calin Juravle357c66d2017-05-04 01:57:17 +0000564 std::string odex_location = GetOdexDir() + "/VdexMultiDexNonMainOutOfDate.odex";
Richard Uhler2f27abd2017-01-31 14:02:34 +0000565
566 Copy(GetMultiDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000567 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
568 ASSERT_EQ(0, unlink(odex_location.c_str()));
Richard Uhler2f27abd2017-01-31 14:02:34 +0000569 Copy(GetMultiDexSrc2(), dex_location);
570
Calin Juravle357c66d2017-05-04 01:57:17 +0000571 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler2f27abd2017-01-31 14:02:34 +0000572
573 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
574 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
575}
576
Richard Uhler03bc6592016-11-22 09:42:04 +0000577// Case: We have a DEX file and an OAT file out of date with respect to the
578// boot image.
579TEST_F(OatFileAssistantTest, OatImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000580 if (IsExecutedAsRoot()) {
581 // We cannot simulate non writable locations when executed as root: b/38000545.
582 LOG(ERROR) << "Test skipped because it's running as root";
583 return;
584 }
585
Richard Uhler03bc6592016-11-22 09:42:04 +0000586 std::string dex_location = GetScratchDir() + "/OatImageOutOfDate.jar";
587
588 Copy(GetDexSrc1(), dex_location);
589 GenerateOatForTest(dex_location.c_str(),
590 CompilerFilter::kSpeed,
591 /*relocate*/true,
592 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000593 /*with_alternate_image*/true);
594
Calin Juravle357c66d2017-05-04 01:57:17 +0000595 ScopedNonWritable scoped_non_writable(dex_location);
596 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
597
Richard Uhler03bc6592016-11-22 09:42:04 +0000598 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler7225a8d2016-11-22 10:12:03 +0000599 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100600 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000601 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100602 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000603 EXPECT_EQ(OatFileAssistant::kDex2OatForBootImage,
Richard Uhler03bc6592016-11-22 09:42:04 +0000604 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
605
606 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
607 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
608 EXPECT_EQ(OatFileAssistant::kOatBootImageOutOfDate, oat_file_assistant.OatFileStatus());
609 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
610}
611
612// Case: We have a DEX file and a verify-at-runtime OAT file out of date with
613// respect to the boot image.
614// It shouldn't matter that the OAT file is out of date, because it is
615// verify-at-runtime.
616TEST_F(OatFileAssistantTest, OatVerifyAtRuntimeImageOutOfDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000617 if (IsExecutedAsRoot()) {
618 // We cannot simulate non writable locations when executed as root: b/38000545.
619 LOG(ERROR) << "Test skipped because it's running as root";
620 return;
621 }
622
Richard Uhler03bc6592016-11-22 09:42:04 +0000623 std::string dex_location = GetScratchDir() + "/OatVerifyAtRuntimeImageOutOfDate.jar";
624
625 Copy(GetDexSrc1(), dex_location);
626 GenerateOatForTest(dex_location.c_str(),
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100627 CompilerFilter::kExtract,
Richard Uhler03bc6592016-11-22 09:42:04 +0000628 /*relocate*/true,
629 /*pic*/false,
Richard Uhler03bc6592016-11-22 09:42:04 +0000630 /*with_alternate_image*/true);
631
Calin Juravle357c66d2017-05-04 01:57:17 +0000632 ScopedNonWritable scoped_non_writable(dex_location);
633 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
634
Richard Uhler03bc6592016-11-22 09:42:04 +0000635 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
636 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100637 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000638 EXPECT_EQ(OatFileAssistant::kDex2OatForFilter,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100639 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler03bc6592016-11-22 09:42:04 +0000640
641 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
642 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
643 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700644 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800645}
646
647// Case: We have a DEX file and an ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800648TEST_F(OatFileAssistantTest, DexOdexNoOat) {
649 std::string dex_location = GetScratchDir() + "/DexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700650 std::string odex_location = GetOdexDir() + "/DexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800651
652 // Create the dex and odex files
653 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000654 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800655
656 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700657 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800658
Andreas Gampe29d38e72016-03-23 15:31:51 +0000659 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100660 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler5923b522016-12-08 09:48:01 +0000661 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000662 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800663
664 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000665 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
666 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700667 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler5f946da2015-07-17 12:28:32 -0700668
669 // We should still be able to get the non-executable odex file to run from.
670 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
671 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhler66d874d2015-01-15 09:37:19 -0800672}
673
Richard Uhler5923b522016-12-08 09:48:01 +0000674// Case: We have a stripped DEX file and a PIC ODEX file, but no OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800675TEST_F(OatFileAssistantTest, StrippedDexOdexNoOat) {
676 std::string dex_location = GetScratchDir() + "/StrippedDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700677 std::string odex_location = GetOdexDir() + "/StrippedDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800678
679 // Create the dex and odex files
680 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000681 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800682
683 // Strip the dex file
684 Copy(GetStrippedDexSrc1(), dex_location);
685
686 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700687 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800688
Richard Uhler5923b522016-12-08 09:48:01 +0000689 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000690 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800691
692 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000693 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000694 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700695 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800696
Richard Uhler66d874d2015-01-15 09:37:19 -0800697 // Verify we can load the dex files from it.
698 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
699 ASSERT_TRUE(oat_file.get() != nullptr);
700 EXPECT_TRUE(oat_file->IsExecutable());
701 std::vector<std::unique_ptr<const DexFile>> dex_files;
702 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
703 EXPECT_EQ(1u, dex_files.size());
704}
705
Richard Uhler5923b522016-12-08 09:48:01 +0000706// Case: We have a stripped DEX file, a PIC ODEX file, and an out-of-date OAT file.
Richard Uhler66d874d2015-01-15 09:37:19 -0800707TEST_F(OatFileAssistantTest, StrippedDexOdexOat) {
708 std::string dex_location = GetScratchDir() + "/StrippedDexOdexOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700709 std::string odex_location = GetOdexDir() + "/StrippedDexOdexOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800710
711 // Create the oat file from a different dex file so it looks out of date.
712 Copy(GetDexSrc2(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000713 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800714
715 // Create the odex file
716 Copy(GetDexSrc1(), dex_location);
Richard Uhler5923b522016-12-08 09:48:01 +0000717 GeneratePicOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800718
719 // Strip the dex file.
720 Copy(GetStrippedDexSrc1(), dex_location);
721
722 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700723 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800724
Andreas Gampe29d38e72016-03-23 15:31:51 +0000725 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100726 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000727 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
728 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Nicolas Geoffray08e9eed2017-04-25 17:36:51 +0100729 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter, // Compiling from the .vdex file
Andreas Gampe29d38e72016-03-23 15:31:51 +0000730 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kEverything));
Richard Uhler66d874d2015-01-15 09:37:19 -0800731
732 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler5923b522016-12-08 09:48:01 +0000733 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
734 EXPECT_EQ(OatFileAssistant::kOatDexOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700735 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800736
737 // Verify we can load the dex files from it.
738 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
739 ASSERT_TRUE(oat_file.get() != nullptr);
740 EXPECT_TRUE(oat_file->IsExecutable());
741 std::vector<std::unique_ptr<const DexFile>> dex_files;
742 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
743 EXPECT_EQ(1u, dex_files.size());
744}
745
Richard Uhler9b994ea2015-06-24 08:44:19 -0700746// Case: We have a stripped (or resource-only) DEX file, no ODEX file and no
747// OAT file. Expect: The status is kNoDexOptNeeded.
748TEST_F(OatFileAssistantTest, ResourceOnlyDex) {
749 std::string dex_location = GetScratchDir() + "/ResourceOnlyDex.jar";
750
751 Copy(GetStrippedDexSrc1(), dex_location);
752
753 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700754 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler9b994ea2015-06-24 08:44:19 -0700755
Andreas Gampe29d38e72016-03-23 15:31:51 +0000756 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
757 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
758 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100759 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Andreas Gampe29d38e72016-03-23 15:31:51 +0000760 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100761 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700762
763 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000764 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
765 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700766 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
767
768 // Make the oat file up to date. This should have no effect.
769 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700770 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -0700771 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -0700772 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhler9b994ea2015-06-24 08:44:19 -0700773
Andreas Gampe29d38e72016-03-23 15:31:51 +0000774 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
775 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler9b994ea2015-06-24 08:44:19 -0700776
777 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000778 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
779 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700780 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
781}
782
Richard Uhler66d874d2015-01-15 09:37:19 -0800783// Case: We have a DEX file, an ODEX file and an OAT file, where the ODEX and
784// OAT files both have patch delta of 0.
Richard Uhler5923b522016-12-08 09:48:01 +0000785// Expect: It shouldn't crash.
Richard Uhler66d874d2015-01-15 09:37:19 -0800786TEST_F(OatFileAssistantTest, OdexOatOverlap) {
787 std::string dex_location = GetScratchDir() + "/OdexOatOverlap.jar";
Richard Uhler63434112015-03-16 14:32:16 -0700788 std::string odex_location = GetOdexDir() + "/OdexOatOverlap.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -0800789
Calin Juravle357c66d2017-05-04 01:57:17 +0000790 // Create the dex, the odex and the oat files.
Richard Uhler66d874d2015-01-15 09:37:19 -0800791 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000792 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Calin Juravle357c66d2017-05-04 01:57:17 +0000793 GenerateOatForTest(dex_location.c_str(),
794 CompilerFilter::kSpeed,
795 /*relocate*/false,
796 /*pic*/false,
797 /*with_alternate_image*/false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800798
799 // Verify things don't go bad.
Calin Juravle357c66d2017-05-04 01:57:17 +0000800 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800801
Calin Juravle357c66d2017-05-04 01:57:17 +0000802 // -kDex2OatForRelocation is expected rather than kDex2OatForRelocation
803 // based on the assumption that the odex location is more up-to-date than the oat
Richard Uhler70a84262016-11-08 16:51:51 +0000804 // location, even if they both need relocation.
Calin Juravle357c66d2017-05-04 01:57:17 +0000805 EXPECT_EQ(-OatFileAssistant::kDex2OatForRelocation,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000806 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -0800807
808 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +0000809 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OdexFileStatus());
810 EXPECT_EQ(OatFileAssistant::kOatRelocationOutOfDate, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -0700811 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -0800812
813 // Things aren't relocated, so it should fall back to interpreted.
814 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
815 ASSERT_TRUE(oat_file.get() != nullptr);
Richard Uhlerf16d5722015-05-11 09:32:47 -0700816
Richard Uhler66d874d2015-01-15 09:37:19 -0800817 EXPECT_FALSE(oat_file->IsExecutable());
818 std::vector<std::unique_ptr<const DexFile>> dex_files;
819 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
820 EXPECT_EQ(1u, dex_files.size());
821}
822
Andreas Gampe29d38e72016-03-23 15:31:51 +0000823// Case: We have a DEX file and a VerifyAtRuntime ODEX file, but no OAT file.
824// Expect: The status is kNoDexOptNeeded, because VerifyAtRuntime contains no code.
825TEST_F(OatFileAssistantTest, DexVerifyAtRuntimeOdexNoOat) {
826 std::string dex_location = GetScratchDir() + "/DexVerifyAtRuntimeOdexNoOat.jar";
827 std::string odex_location = GetOdexDir() + "/DexVerifyAtRuntimeOdexNoOat.odex";
David Brazdilce4b0ba2016-01-28 15:05:49 +0000828
829 // Create the dex and odex files
830 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100831 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kExtract);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000832
833 // Verify the status.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700834 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
David Brazdilce4b0ba2016-01-28 15:05:49 +0000835
Andreas Gampe29d38e72016-03-23 15:31:51 +0000836 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100837 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kExtract));
Richard Uhler7225a8d2016-11-22 10:12:03 +0000838 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Andreas Gampe29d38e72016-03-23 15:31:51 +0000839 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
David Brazdilce4b0ba2016-01-28 15:05:49 +0000840
841 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler3e580bc2016-11-08 16:23:07 +0000842 EXPECT_EQ(OatFileAssistant::kOatUpToDate, oat_file_assistant.OdexFileStatus());
Richard Uhler03bc6592016-11-22 09:42:04 +0000843 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
David Brazdilce4b0ba2016-01-28 15:05:49 +0000844 EXPECT_TRUE(oat_file_assistant.HasOriginalDexFiles());
845}
846
Richard Uhler66d874d2015-01-15 09:37:19 -0800847// Case: We have a DEX file and up-to-date OAT file for it.
848// Expect: We should load an executable dex file.
849TEST_F(OatFileAssistantTest, LoadOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000850 if (IsExecutedAsRoot()) {
851 // We cannot simulate non writable locations when executed as root: b/38000545.
852 LOG(ERROR) << "Test skipped because it's running as root";
853 return;
854 }
855
Richard Uhler66d874d2015-01-15 09:37:19 -0800856 std::string dex_location = GetScratchDir() + "/LoadOatUpToDate.jar";
857
858 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000859 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800860
Calin Juravle357c66d2017-05-04 01:57:17 +0000861 ScopedNonWritable scoped_non_writable(dex_location);
862 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
863
Richard Uhler66d874d2015-01-15 09:37:19 -0800864 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700865 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000866
867 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
868 ASSERT_TRUE(oat_file.get() != nullptr);
869 EXPECT_TRUE(oat_file->IsExecutable());
870 std::vector<std::unique_ptr<const DexFile>> dex_files;
871 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
872 EXPECT_EQ(1u, dex_files.size());
873}
874
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100875// Case: We have a DEX file and up-to-date quicken OAT file for it.
Andreas Gampe29d38e72016-03-23 15:31:51 +0000876// Expect: We should still load the oat file as executable.
877TEST_F(OatFileAssistantTest, LoadExecInterpretOnlyOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000878 if (IsExecutedAsRoot()) {
879 // We cannot simulate non writable locations when executed as root: b/38000545.
880 LOG(ERROR) << "Test skipped because it's running as root";
881 return;
882 }
883
Andreas Gampe29d38e72016-03-23 15:31:51 +0000884 std::string dex_location = GetScratchDir() + "/LoadExecInterpretOnlyOatUpToDate.jar";
885
886 Copy(GetDexSrc1(), dex_location);
Nicolas Geoffray49cda062017-04-21 13:08:25 +0100887 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
Andreas Gampe29d38e72016-03-23 15:31:51 +0000888
Calin Juravle357c66d2017-05-04 01:57:17 +0000889 ScopedNonWritable scoped_non_writable(dex_location);
890 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
891
Andreas Gampe29d38e72016-03-23 15:31:51 +0000892 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700893 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -0800894
895 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
896 ASSERT_TRUE(oat_file.get() != nullptr);
897 EXPECT_TRUE(oat_file->IsExecutable());
898 std::vector<std::unique_ptr<const DexFile>> dex_files;
899 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
900 EXPECT_EQ(1u, dex_files.size());
901}
902
903// Case: We have a DEX file and up-to-date OAT file for it.
904// Expect: Loading non-executable should load the oat non-executable.
905TEST_F(OatFileAssistantTest, LoadNoExecOatUpToDate) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000906 if (IsExecutedAsRoot()) {
907 // We cannot simulate non writable locations when executed as root: b/38000545.
908 LOG(ERROR) << "Test skipped because it's running as root";
909 return;
910 }
911
Richard Uhler66d874d2015-01-15 09:37:19 -0800912 std::string dex_location = GetScratchDir() + "/LoadNoExecOatUpToDate.jar";
913
914 Copy(GetDexSrc1(), dex_location);
Calin Juravle357c66d2017-05-04 01:57:17 +0000915
916 ScopedNonWritable scoped_non_writable(dex_location);
917 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
918
Andreas Gampe29d38e72016-03-23 15:31:51 +0000919 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -0800920
921 // Load the oat using an oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -0700922 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -0800923
924 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
925 ASSERT_TRUE(oat_file.get() != nullptr);
926 EXPECT_FALSE(oat_file->IsExecutable());
927 std::vector<std::unique_ptr<const DexFile>> dex_files;
928 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
929 EXPECT_EQ(1u, dex_files.size());
930}
931
Richard Uhler8327cf72015-10-13 16:34:59 -0700932// Case: We don't have a DEX file and can't write the oat file.
933// Expect: We should fail to generate the oat file without crashing.
934TEST_F(OatFileAssistantTest, GenNoDex) {
Calin Juravle357c66d2017-05-04 01:57:17 +0000935 if (IsExecutedAsRoot()) {
936 // We cannot simulate non writable locations when executed as root: b/38000545.
937 LOG(ERROR) << "Test skipped because it's running as root";
938 return;
939 }
Richard Uhler8327cf72015-10-13 16:34:59 -0700940
Calin Juravle357c66d2017-05-04 01:57:17 +0000941 std::string dex_location = GetScratchDir() + "/GenNoDex.jar";
942
943 ScopedNonWritable scoped_non_writable(dex_location);
944 ASSERT_TRUE(scoped_non_writable.IsSuccessful());
945
946 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler8327cf72015-10-13 16:34:59 -0700947 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -0700948 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Calin Juravle357c66d2017-05-04 01:57:17 +0000949 // We should get kUpdateSucceeded from MakeUpToDate since there's nothing
950 // that can be done in this situation.
951 ASSERT_EQ(OatFileAssistant::kUpdateSucceeded,
952 oat_file_assistant.MakeUpToDate(false, &error_msg));
953
954 // Verify it didn't create an oat in the default location (dalvik-cache).
955 OatFileAssistant ofm(dex_location.c_str(), kRuntimeISA, false);
956 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OatFileStatus());
957 // Verify it didn't create the odex file in the default location (../oat/isa/...odex)
958 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, ofm.OdexFileStatus());
Richard Uhler8327cf72015-10-13 16:34:59 -0700959}
960
Richard Uhler66d874d2015-01-15 09:37:19 -0800961// Turn an absolute path into a path relative to the current working
962// directory.
Andreas Gampeca620d72016-11-08 08:09:33 -0800963static std::string MakePathRelative(const std::string& target) {
Richard Uhler66d874d2015-01-15 09:37:19 -0800964 char buf[MAXPATHLEN];
965 std::string cwd = getcwd(buf, MAXPATHLEN);
966
967 // Split the target and cwd paths into components.
968 std::vector<std::string> target_path;
969 std::vector<std::string> cwd_path;
970 Split(target, '/', &target_path);
971 Split(cwd, '/', &cwd_path);
972
973 // Reverse the path components, so we can use pop_back().
974 std::reverse(target_path.begin(), target_path.end());
975 std::reverse(cwd_path.begin(), cwd_path.end());
976
977 // Drop the common prefix of the paths. Because we reversed the path
978 // components, this becomes the common suffix of target_path and cwd_path.
979 while (!target_path.empty() && !cwd_path.empty()
980 && target_path.back() == cwd_path.back()) {
981 target_path.pop_back();
982 cwd_path.pop_back();
983 }
984
985 // For each element of the remaining cwd_path, add '..' to the beginning
986 // of the target path. Because we reversed the path components, we add to
987 // the end of target_path.
988 for (unsigned int i = 0; i < cwd_path.size(); i++) {
989 target_path.push_back("..");
990 }
991
992 // Reverse again to get the right path order, and join to get the result.
993 std::reverse(target_path.begin(), target_path.end());
Andreas Gampe9186ced2016-12-12 14:28:21 -0800994 return android::base::Join(target_path, '/');
Richard Uhler66d874d2015-01-15 09:37:19 -0800995}
996
997// Case: Non-absolute path to Dex location.
998// Expect: Not sure, but it shouldn't crash.
999TEST_F(OatFileAssistantTest, NonAbsoluteDexLocation) {
1000 std::string abs_dex_location = GetScratchDir() + "/NonAbsoluteDexLocation.jar";
1001 Copy(GetDexSrc1(), abs_dex_location);
1002
1003 std::string dex_location = MakePathRelative(abs_dex_location);
Richard Uhlerd1472a22016-04-15 15:18:56 -07001004 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001005
1006 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler7225a8d2016-11-22 10:12:03 +00001007 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001008 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001009 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1010 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001011}
1012
1013// Case: Very short, non-existent Dex location.
Richard Uhler9b994ea2015-06-24 08:44:19 -07001014// Expect: kNoDexOptNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001015TEST_F(OatFileAssistantTest, ShortDexLocation) {
1016 std::string dex_location = "/xx";
1017
Richard Uhlerd1472a22016-04-15 15:18:56 -07001018 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001019
1020 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Andreas Gampe29d38e72016-03-23 15:31:51 +00001021 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1022 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler03bc6592016-11-22 09:42:04 +00001023 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1024 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler9b994ea2015-06-24 08:44:19 -07001025 EXPECT_FALSE(oat_file_assistant.HasOriginalDexFiles());
Richard Uhler66d874d2015-01-15 09:37:19 -08001026
Richard Uhler9b994ea2015-06-24 08:44:19 -07001027 // Trying to make it up to date should have no effect.
Richard Uhler66d874d2015-01-15 09:37:19 -08001028 std::string error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001029 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
Richard Uhler1e860612016-03-30 12:17:55 -07001030 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001031 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhler9b994ea2015-06-24 08:44:19 -07001032 EXPECT_TRUE(error_msg.empty());
Richard Uhler66d874d2015-01-15 09:37:19 -08001033}
1034
1035// Case: Non-standard extension for dex file.
Richard Uhler95abd042015-03-24 09:51:28 -07001036// Expect: The status is kDex2OatNeeded.
Richard Uhler66d874d2015-01-15 09:37:19 -08001037TEST_F(OatFileAssistantTest, LongDexExtension) {
1038 std::string dex_location = GetScratchDir() + "/LongDexExtension.jarx";
1039 Copy(GetDexSrc1(), dex_location);
1040
Richard Uhlerd1472a22016-04-15 15:18:56 -07001041 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhler66d874d2015-01-15 09:37:19 -08001042
Richard Uhler7225a8d2016-11-22 10:12:03 +00001043 EXPECT_EQ(OatFileAssistant::kDex2OatFromScratch,
Andreas Gampe29d38e72016-03-23 15:31:51 +00001044 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
Richard Uhler66d874d2015-01-15 09:37:19 -08001045
1046 EXPECT_FALSE(oat_file_assistant.IsInBootClassPath());
Richard Uhler03bc6592016-11-22 09:42:04 +00001047 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OdexFileStatus());
1048 EXPECT_EQ(OatFileAssistant::kOatCannotOpen, oat_file_assistant.OatFileStatus());
Richard Uhler66d874d2015-01-15 09:37:19 -08001049}
1050
1051// A task to generate a dex location. Used by the RaceToGenerate test.
1052class RaceGenerateTask : public Task {
1053 public:
1054 explicit RaceGenerateTask(const std::string& dex_location, const std::string& oat_location)
Jeff Haof0192c82016-03-28 20:39:50 -07001055 : dex_location_(dex_location), oat_location_(oat_location), loaded_oat_file_(nullptr)
Richard Uhler66d874d2015-01-15 09:37:19 -08001056 {}
1057
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001058 void Run(Thread* self ATTRIBUTE_UNUSED) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001059 // Load the dex files, and save a pointer to the loaded oat file, so that
1060 // we can verify only one oat file was loaded for the dex location.
Richard Uhler66d874d2015-01-15 09:37:19 -08001061 std::vector<std::unique_ptr<const DexFile>> dex_files;
1062 std::vector<std::string> error_msgs;
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001063 const OatFile* oat_file = nullptr;
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001064 dex_files = Runtime::Current()->GetOatFileManager().OpenDexFilesFromOat(
1065 dex_location_.c_str(),
Jeff Hao0cb17282017-07-12 14:51:49 -07001066 Runtime::Current()->GetSystemClassLoader(),
Mathieu Chartierfbc31082016-01-24 11:59:56 -08001067 /*dex_elements*/nullptr,
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001068 &oat_file,
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001069 &error_msgs);
Andreas Gampe9186ced2016-12-12 14:28:21 -08001070 CHECK(!dex_files.empty()) << android::base::Join(error_msgs, '\n');
Richard Uhler07b3c232015-03-31 15:57:54 -07001071 CHECK(dex_files[0]->GetOatDexFile() != nullptr) << dex_files[0]->GetLocation();
1072 loaded_oat_file_ = dex_files[0]->GetOatDexFile()->GetOatFile();
Mathieu Chartiere58991b2015-10-13 07:59:34 -07001073 CHECK_EQ(loaded_oat_file_, oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001074 }
1075
1076 const OatFile* GetLoadedOatFile() const {
1077 return loaded_oat_file_;
1078 }
1079
1080 private:
1081 std::string dex_location_;
1082 std::string oat_location_;
1083 const OatFile* loaded_oat_file_;
1084};
1085
1086// Test the case where multiple processes race to generate an oat file.
1087// This simulates multiple processes using multiple threads.
1088//
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001089// We want unique Oat files to be loaded even when there is a race to load.
1090// TODO: The test case no longer tests locking the way it was intended since we now get multiple
1091// copies of the same Oat files mapped at different locations.
Richard Uhler66d874d2015-01-15 09:37:19 -08001092TEST_F(OatFileAssistantTest, RaceToGenerate) {
1093 std::string dex_location = GetScratchDir() + "/RaceToGenerate.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001094 std::string oat_location = GetOdexDir() + "/RaceToGenerate.oat";
Richard Uhler66d874d2015-01-15 09:37:19 -08001095
Jeff Hao0cb17282017-07-12 14:51:49 -07001096 // Start the runtime to initialize the system's class loader.
1097 Thread::Current()->TransitionFromSuspendedToRunnable();
1098 runtime_->Start();
1099
Richard Uhler66d874d2015-01-15 09:37:19 -08001100 // We use the lib core dex file, because it's large, and hopefully should
1101 // take a while to generate.
Narayan Kamathd1ef4362015-11-12 11:49:06 +00001102 Copy(GetLibCoreDexFileNames()[0], dex_location);
Richard Uhler66d874d2015-01-15 09:37:19 -08001103
1104 const int kNumThreads = 32;
1105 Thread* self = Thread::Current();
1106 ThreadPool thread_pool("Oat file assistant test thread pool", kNumThreads);
1107 std::vector<std::unique_ptr<RaceGenerateTask>> tasks;
1108 for (int i = 0; i < kNumThreads; i++) {
1109 std::unique_ptr<RaceGenerateTask> task(new RaceGenerateTask(dex_location, oat_location));
1110 thread_pool.AddTask(self, task.get());
1111 tasks.push_back(std::move(task));
1112 }
1113 thread_pool.StartWorkers(self);
1114 thread_pool.Wait(self, true, false);
1115
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001116 // Verify every task got a unique oat file.
1117 std::set<const OatFile*> oat_files;
Richard Uhler66d874d2015-01-15 09:37:19 -08001118 for (auto& task : tasks) {
Mathieu Chartierf9c6fc62015-10-07 11:44:05 -07001119 const OatFile* oat_file = task->GetLoadedOatFile();
1120 EXPECT_TRUE(oat_files.find(oat_file) == oat_files.end());
1121 oat_files.insert(oat_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001122 }
1123}
1124
1125// Case: We have a DEX file and an ODEX file, no OAT file, and dex2oat is
1126// disabled.
1127// Expect: We should load the odex file non-executable.
1128TEST_F(OatFileAssistantNoDex2OatTest, LoadDexOdexNoOat) {
1129 std::string dex_location = GetScratchDir() + "/LoadDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001130 std::string odex_location = GetOdexDir() + "/LoadDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001131
1132 // Create the dex and odex files
1133 Copy(GetDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001134 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001135
1136 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001137 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001138
1139 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1140 ASSERT_TRUE(oat_file.get() != nullptr);
1141 EXPECT_FALSE(oat_file->IsExecutable());
1142 std::vector<std::unique_ptr<const DexFile>> dex_files;
1143 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1144 EXPECT_EQ(1u, dex_files.size());
1145}
1146
1147// Case: We have a MultiDEX file and an ODEX file, no OAT file, and dex2oat is
1148// disabled.
1149// Expect: We should load the odex file non-executable.
1150TEST_F(OatFileAssistantNoDex2OatTest, LoadMultiDexOdexNoOat) {
1151 std::string dex_location = GetScratchDir() + "/LoadMultiDexOdexNoOat.jar";
Richard Uhler63434112015-03-16 14:32:16 -07001152 std::string odex_location = GetOdexDir() + "/LoadMultiDexOdexNoOat.odex";
Richard Uhler66d874d2015-01-15 09:37:19 -08001153
1154 // Create the dex and odex files
1155 Copy(GetMultiDexSrc1(), dex_location);
Andreas Gampe29d38e72016-03-23 15:31:51 +00001156 GenerateOdexForTest(dex_location, odex_location, CompilerFilter::kSpeed);
Richard Uhler66d874d2015-01-15 09:37:19 -08001157
1158 // Load the oat using an executable oat file assistant.
Richard Uhlerd1472a22016-04-15 15:18:56 -07001159 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, true);
Richard Uhler66d874d2015-01-15 09:37:19 -08001160
1161 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1162 ASSERT_TRUE(oat_file.get() != nullptr);
1163 EXPECT_FALSE(oat_file->IsExecutable());
1164 std::vector<std::unique_ptr<const DexFile>> dex_files;
1165 dex_files = oat_file_assistant.LoadDexFiles(*oat_file, dex_location.c_str());
1166 EXPECT_EQ(2u, dex_files.size());
1167}
1168
Richard Uhlerf4b34872016-04-13 11:03:46 -07001169TEST_F(OatFileAssistantTest, RuntimeCompilerFilterOptionUsed) {
1170 std::string dex_location = GetScratchDir() + "/RuntimeCompilerFilterOptionUsed.jar";
1171 Copy(GetDexSrc1(), dex_location);
1172
Richard Uhlerd1472a22016-04-15 15:18:56 -07001173 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
Richard Uhlerf4b34872016-04-13 11:03:46 -07001174
1175 std::string error_msg;
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001176 Runtime::Current()->AddCompilerOption("--compiler-filter=quicken");
Richard Uhlerf4b34872016-04-13 11:03:46 -07001177 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001178 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Calin Juravle357c66d2017-05-04 01:57:17 +00001179 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001180 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Calin Juravle357c66d2017-05-04 01:57:17 +00001181 EXPECT_EQ(-OatFileAssistant::kDex2OatForFilter,
Richard Uhlerf4b34872016-04-13 11:03:46 -07001182 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1183
1184 Runtime::Current()->AddCompilerOption("--compiler-filter=speed");
1185 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001186 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
Richard Uhlerf4b34872016-04-13 11:03:46 -07001187 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
Nicolas Geoffray49cda062017-04-21 13:08:25 +01001188 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kQuicken));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001189 EXPECT_EQ(OatFileAssistant::kNoDexOptNeeded,
1190 oat_file_assistant.GetDexOptNeeded(CompilerFilter::kSpeed));
1191
1192 Runtime::Current()->AddCompilerOption("--compiler-filter=bogus");
1193 EXPECT_EQ(OatFileAssistant::kUpdateNotAttempted,
Richard Uhlerd1472a22016-04-15 15:18:56 -07001194 oat_file_assistant.MakeUpToDate(false, &error_msg));
Richard Uhlerf4b34872016-04-13 11:03:46 -07001195}
1196
Richard Uhlerb81881d2016-04-19 13:08:04 -07001197TEST(OatFileAssistantUtilsTest, DexLocationToOdexFilename) {
Richard Uhler66d874d2015-01-15 09:37:19 -08001198 std::string error_msg;
1199 std::string odex_file;
1200
Richard Uhlerb81881d2016-04-19 13:08:04 -07001201 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001202 "/foo/bar/baz.jar", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001203 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001204
Richard Uhlerb81881d2016-04-19 13:08:04 -07001205 EXPECT_TRUE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001206 "/foo/bar/baz.funnyext", kArm, &odex_file, &error_msg)) << error_msg;
Richard Uhler63434112015-03-16 14:32:16 -07001207 EXPECT_EQ("/foo/bar/oat/arm/baz.odex", odex_file);
Richard Uhler66d874d2015-01-15 09:37:19 -08001208
Richard Uhlerb81881d2016-04-19 13:08:04 -07001209 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001210 "nopath.jar", kArm, &odex_file, &error_msg));
Richard Uhlerb81881d2016-04-19 13:08:04 -07001211 EXPECT_FALSE(OatFileAssistant::DexLocationToOdexFilename(
Igor Murashkinb1d8c312015-08-04 11:18:43 -07001212 "/foo/bar/baz_noext", kArm, &odex_file, &error_msg));
Richard Uhler66d874d2015-01-15 09:37:19 -08001213}
1214
Richard Uhler23cedd22015-04-08 13:17:29 -07001215// Verify the dexopt status values from dalvik.system.DexFile
1216// match the OatFileAssistant::DexOptStatus values.
1217TEST_F(OatFileAssistantTest, DexOptStatusValues) {
Richard Uhler7225a8d2016-11-22 10:12:03 +00001218 std::pair<OatFileAssistant::DexOptNeeded, const char*> mapping[] = {
1219 {OatFileAssistant::kNoDexOptNeeded, "NO_DEXOPT_NEEDED"},
1220 {OatFileAssistant::kDex2OatFromScratch, "DEX2OAT_FROM_SCRATCH"},
1221 {OatFileAssistant::kDex2OatForBootImage, "DEX2OAT_FOR_BOOT_IMAGE"},
1222 {OatFileAssistant::kDex2OatForFilter, "DEX2OAT_FOR_FILTER"},
1223 {OatFileAssistant::kDex2OatForRelocation, "DEX2OAT_FOR_RELOCATION"},
Richard Uhler7225a8d2016-11-22 10:12:03 +00001224 };
1225
Richard Uhler23cedd22015-04-08 13:17:29 -07001226 ScopedObjectAccess soa(Thread::Current());
1227 StackHandleScope<1> hs(soa.Self());
1228 ClassLinker* linker = Runtime::Current()->GetClassLinker();
1229 Handle<mirror::Class> dexfile(
1230 hs.NewHandle(linker->FindSystemClass(soa.Self(), "Ldalvik/system/DexFile;")));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001231 ASSERT_FALSE(dexfile == nullptr);
Richard Uhler23cedd22015-04-08 13:17:29 -07001232 linker->EnsureInitialized(soa.Self(), dexfile, true, true);
1233
Richard Uhler7225a8d2016-11-22 10:12:03 +00001234 for (std::pair<OatFileAssistant::DexOptNeeded, const char*> field : mapping) {
1235 ArtField* art_field = mirror::Class::FindStaticField(
Vladimir Marko19a4d372016-12-08 14:41:46 +00001236 soa.Self(), dexfile.Get(), field.second, "I");
Richard Uhler7225a8d2016-11-22 10:12:03 +00001237 ASSERT_FALSE(art_field == nullptr);
1238 EXPECT_EQ(art_field->GetTypeAsPrimitiveType(), Primitive::kPrimInt);
1239 EXPECT_EQ(field.first, art_field->GetInt(dexfile.Get()));
1240 }
Richard Uhler23cedd22015-04-08 13:17:29 -07001241}
Richard Uhler66d874d2015-01-15 09:37:19 -08001242
Calin Juravle07c6d722017-06-07 17:06:12 +00001243// Verify that when no compiler filter is passed the default one from OatFileAssistant is used.
1244TEST_F(OatFileAssistantTest, DefaultMakeUpToDateFilter) {
1245 std::string dex_location = GetScratchDir() + "/TestDex.jar";
1246 Copy(GetDexSrc1(), dex_location);
1247
1248 OatFileAssistant oat_file_assistant(dex_location.c_str(), kRuntimeISA, false);
1249
1250 const CompilerFilter::Filter default_filter =
1251 OatFileAssistant::kDefaultCompilerFilterForDexLoading;
1252 std::string error_msg;
1253 EXPECT_EQ(OatFileAssistant::kUpdateSucceeded,
1254 oat_file_assistant.MakeUpToDate(false, &error_msg)) << error_msg;
1255 EXPECT_EQ(-OatFileAssistant::kNoDexOptNeeded,
1256 oat_file_assistant.GetDexOptNeeded(default_filter));
1257 std::unique_ptr<OatFile> oat_file = oat_file_assistant.GetBestOatFile();
1258 EXPECT_NE(nullptr, oat_file.get());
1259 EXPECT_EQ(default_filter, oat_file->GetCompilerFilter());
1260}
1261
Richard Uhler66d874d2015-01-15 09:37:19 -08001262// TODO: More Tests:
1263// * Test class linker falls back to unquickened dex for DexNoOat
1264// * Test class linker falls back to unquickened dex for MultiDexNoOat
Richard Uhler66d874d2015-01-15 09:37:19 -08001265// * Test using secondary isa
Richard Uhler66d874d2015-01-15 09:37:19 -08001266// * Test for status of oat while oat is being generated (how?)
1267// * Test case where 32 and 64 bit boot class paths differ,
1268// and we ask IsInBootClassPath for a class in exactly one of the 32 or
1269// 64 bit boot class paths.
1270// * Test unexpected scenarios (?):
1271// - Dex is stripped, don't have odex.
1272// - Oat file corrupted after status check, before reload unexecutable
1273// because it's unrelocated and no dex2oat
Richard Uhler66d874d2015-01-15 09:37:19 -08001274} // namespace art