blob: b54711322b45e699e5e7462fc48f58c981977c31 [file] [log] [blame]
Richard Uhlere5fed032015-03-18 08:21:11 -07001/*
2 * Copyright (C) 2015 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#include "oat_file.h"
18
19#include <string>
20
21#include <gtest/gtest.h>
22
Andreas Gampe7848da42015-04-09 11:15:04 -070023#include "common_runtime_test.h"
David Srbeckyec2cdf42017-12-08 16:21:25 +000024#include "dexopt_test.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
David Srbeckyec2cdf42017-12-08 16:21:25 +000026#include "vdex_file.h"
Andreas Gampe7848da42015-04-09 11:15:04 -070027
Richard Uhlere5fed032015-03-18 08:21:11 -070028namespace art {
29
David Srbeckyec2cdf42017-12-08 16:21:25 +000030class OatFileTest : public DexoptTest {
Andreas Gampe7848da42015-04-09 11:15:04 -070031};
32
33TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation) {
Richard Uhlere5fed032015-03-18 08:21:11 -070034 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
35 OatFile::ResolveRelativeEncodedDexLocation(
36 nullptr, "/data/app/foo/base.apk"));
37
38 EXPECT_EQ(std::string("/system/framework/base.apk"),
39 OatFile::ResolveRelativeEncodedDexLocation(
40 "/data/app/foo/base.apk", "/system/framework/base.apk"));
41
42 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
43 OatFile::ResolveRelativeEncodedDexLocation(
44 "/data/app/foo/base.apk", "base.apk"));
45
46 EXPECT_EQ(std::string("/data/app/foo/base.apk"),
47 OatFile::ResolveRelativeEncodedDexLocation(
48 "/data/app/foo/base.apk", "foo/base.apk"));
49
Calin Juravlea308a322017-07-18 16:51:51 -070050 EXPECT_EQ(std::string("/data/app/foo/base.apk!classes2.dex"),
Richard Uhlere5fed032015-03-18 08:21:11 -070051 OatFile::ResolveRelativeEncodedDexLocation(
Calin Juravlea308a322017-07-18 16:51:51 -070052 "/data/app/foo/base.apk", "base.apk!classes2.dex"));
Richard Uhlere5fed032015-03-18 08:21:11 -070053
Calin Juravlea308a322017-07-18 16:51:51 -070054 EXPECT_EQ(std::string("/data/app/foo/base.apk!classes11.dex"),
Richard Uhlere5fed032015-03-18 08:21:11 -070055 OatFile::ResolveRelativeEncodedDexLocation(
Calin Juravlea308a322017-07-18 16:51:51 -070056 "/data/app/foo/base.apk", "base.apk!classes11.dex"));
Richard Uhlere5fed032015-03-18 08:21:11 -070057
58 EXPECT_EQ(std::string("base.apk"),
59 OatFile::ResolveRelativeEncodedDexLocation(
60 "/data/app/foo/sludge.apk", "base.apk"));
61
62 EXPECT_EQ(std::string("o/base.apk"),
63 OatFile::ResolveRelativeEncodedDexLocation(
64 "/data/app/foo/base.apk", "o/base.apk"));
65}
66
David Srbeckyec2cdf42017-12-08 16:21:25 +000067TEST_F(OatFileTest, LoadOat) {
68 std::string dex_location = GetScratchDir() + "/LoadOat.jar";
69
70 Copy(GetDexSrc1(), dex_location);
71 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
72
73 std::string oat_location;
74 std::string error_msg;
75 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
76 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +010077 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +010078 oat_location.c_str(),
David Srbeckyec2cdf42017-12-08 16:21:25 +000079 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +010080 /*executable=*/ false,
81 /*low_4gb=*/ false,
David Srbeckyec2cdf42017-12-08 16:21:25 +000082 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +010083 /*reservation=*/ nullptr,
David Srbeckyec2cdf42017-12-08 16:21:25 +000084 &error_msg));
85 ASSERT_TRUE(odex_file.get() != nullptr);
86
87 // Check that the vdex file was loaded in the reserved space of odex file.
88 EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin());
89}
90
Andreas Gampefc604a72018-02-08 15:43:37 -080091TEST_F(OatFileTest, ChangingMultiDexUncompressed) {
92 std::string dex_location = GetScratchDir() + "/MultiDexUncompressed.jar";
93
94 Copy(GetTestDexFileName("MultiDexUncompressed"), dex_location);
95 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
96
97 std::string oat_location;
98 std::string error_msg;
99 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
100 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
101
102 // Ensure we can load that file. Just a precondition.
103 {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100104 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100105 oat_location.c_str(),
Andreas Gampefc604a72018-02-08 15:43:37 -0800106 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100107 /*executable=*/ false,
108 /*low_4gb=*/ false,
Andreas Gampefc604a72018-02-08 15:43:37 -0800109 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100110 /*reservation=*/ nullptr,
Andreas Gampefc604a72018-02-08 15:43:37 -0800111 &error_msg));
112 ASSERT_TRUE(odex_file != nullptr);
113 ASSERT_EQ(2u, odex_file->GetOatDexFiles().size());
114 }
115
116 // Now replace the source.
117 Copy(GetTestDexFileName("MainUncompressed"), dex_location);
118
119 // And try to load again.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100120 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100121 oat_location,
122 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100123 /*executable=*/ false,
124 /*low_4gb=*/ false,
Andreas Gampefc604a72018-02-08 15:43:37 -0800125 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100126 /*reservation=*/ nullptr,
Andreas Gampefc604a72018-02-08 15:43:37 -0800127 &error_msg));
128 EXPECT_TRUE(odex_file == nullptr);
129 EXPECT_NE(std::string::npos, error_msg.find("expected 2 uncompressed dex files, but found 1"))
130 << error_msg;
131}
132
Richard Uhlere5fed032015-03-18 08:21:11 -0700133} // namespace art