blob: 7a122ba6d93dcf6979d878682cc191ef945a5ea3 [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
David Brazdil3e8aae02019-03-26 18:48:02 +000033TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_NullAbsLocation) {
34 std::string dex_location;
35 std::string dex_file_name;
36 OatFile::ResolveRelativeEncodedDexLocation(nullptr,
37 "/data/app/foo/base.apk",
38 &dex_location,
39 &dex_file_name);
40 ASSERT_EQ("/data/app/foo/base.apk", dex_file_name);
41 ASSERT_EQ("/data/app/foo/base.apk", dex_location);
42}
Richard Uhlere5fed032015-03-18 08:21:11 -070043
David Brazdil3e8aae02019-03-26 18:48:02 +000044TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_NullAbsLocation_Multidex) {
45 std::string dex_location;
46 std::string dex_file_name;
47 OatFile::ResolveRelativeEncodedDexLocation(nullptr,
48 "/data/app/foo/base.apk!classes2.dex",
49 &dex_location,
50 &dex_file_name);
51 ASSERT_EQ("/data/app/foo/base.apk!classes2.dex", dex_file_name);
52 ASSERT_EQ("/data/app/foo/base.apk!classes2.dex", dex_location);
53}
Richard Uhlere5fed032015-03-18 08:21:11 -070054
David Brazdil3e8aae02019-03-26 18:48:02 +000055TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelLocationAbsolute) {
56 std::string dex_location;
57 std::string dex_file_name;
58 OatFile::ResolveRelativeEncodedDexLocation("base.apk",
59 "/system/framework/base.apk",
60 &dex_location,
61 &dex_file_name);
62 ASSERT_EQ(kIsTargetBuild ? "/system/framework/base.apk" : "base.apk", dex_file_name);
63 ASSERT_EQ("/system/framework/base.apk", dex_location);
64}
Richard Uhlere5fed032015-03-18 08:21:11 -070065
David Brazdil3e8aae02019-03-26 18:48:02 +000066TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_BothAbsoluteLocations) {
67 std::string dex_location;
68 std::string dex_file_name;
69 OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/base.apk",
70 "/system/framework/base.apk",
71 &dex_location,
72 &dex_file_name);
73 ASSERT_EQ(kIsTargetBuild ? "/system/framework/base.apk" : "/data/app/foo/base.apk",
74 dex_file_name);
75 ASSERT_EQ("/system/framework/base.apk", dex_location);
76}
Richard Uhlere5fed032015-03-18 08:21:11 -070077
David Brazdil3e8aae02019-03-26 18:48:02 +000078TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelSuffixOfAbsLocation1) {
79 std::string dex_location;
80 std::string dex_file_name;
81 OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/base.apk",
82 "base.apk",
83 &dex_location,
84 &dex_file_name);
85 ASSERT_EQ("/data/app/foo/base.apk", dex_file_name);
86 ASSERT_EQ("/data/app/foo/base.apk", dex_location);
87}
Richard Uhlere5fed032015-03-18 08:21:11 -070088
David Brazdil3e8aae02019-03-26 18:48:02 +000089TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelSuffixOfAbsLocation2) {
90 std::string dex_location;
91 std::string dex_file_name;
92 OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/base.apk",
93 "foo/base.apk",
94 &dex_location,
95 &dex_file_name);
96 ASSERT_EQ("/data/app/foo/base.apk", dex_file_name);
97 ASSERT_EQ("/data/app/foo/base.apk", dex_location);
98}
99
100TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelSuffixOfAbsLocation_Multidex) {
101 std::string dex_location;
102 std::string dex_file_name;
103 OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/base.apk",
104 "base.apk!classes11.dex",
105 &dex_location,
106 &dex_file_name);
107 ASSERT_EQ("/data/app/foo/base.apk!classes11.dex", dex_file_name);
108 ASSERT_EQ("/data/app/foo/base.apk!classes11.dex", dex_location);
109}
110
111TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelNotSuffixOfAbsLocation1) {
112 std::string dex_location;
113 std::string dex_file_name;
114 OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/sludge.apk",
115 "base.apk!classes2.dex",
116 &dex_location,
117 &dex_file_name);
118 ASSERT_EQ(kIsTargetBuild ? "base.apk!classes2.dex" : "/data/app/foo/sludge.apk!classes2.dex",
119 dex_file_name);
120 ASSERT_EQ("base.apk!classes2.dex", dex_location);
121}
122
123TEST_F(OatFileTest, ResolveRelativeEncodedDexLocation_RelNotSuffixOfAbsLocation2) {
124 std::string dex_location;
125 std::string dex_file_name;
126 OatFile::ResolveRelativeEncodedDexLocation("/data/app/foo/sludge.apk",
127 "o/base.apk",
128 &dex_location,
129 &dex_file_name);
130 ASSERT_EQ(kIsTargetBuild ? "o/base.apk" : "/data/app/foo/sludge.apk", dex_file_name);
131 ASSERT_EQ("o/base.apk", dex_location);
Richard Uhlere5fed032015-03-18 08:21:11 -0700132}
133
David Srbeckyec2cdf42017-12-08 16:21:25 +0000134TEST_F(OatFileTest, LoadOat) {
135 std::string dex_location = GetScratchDir() + "/LoadOat.jar";
136
137 Copy(GetDexSrc1(), dex_location);
138 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kSpeed);
139
140 std::string oat_location;
141 std::string error_msg;
142 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
143 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100144 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100145 oat_location.c_str(),
David Srbeckyec2cdf42017-12-08 16:21:25 +0000146 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100147 /*executable=*/ false,
148 /*low_4gb=*/ false,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000149 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100150 /*reservation=*/ nullptr,
David Srbeckyec2cdf42017-12-08 16:21:25 +0000151 &error_msg));
152 ASSERT_TRUE(odex_file.get() != nullptr);
153
154 // Check that the vdex file was loaded in the reserved space of odex file.
155 EXPECT_EQ(odex_file->GetVdexFile()->Begin(), odex_file->VdexBegin());
156}
157
Andreas Gampefc604a72018-02-08 15:43:37 -0800158TEST_F(OatFileTest, ChangingMultiDexUncompressed) {
159 std::string dex_location = GetScratchDir() + "/MultiDexUncompressed.jar";
160
161 Copy(GetTestDexFileName("MultiDexUncompressed"), dex_location);
162 GenerateOatForTest(dex_location.c_str(), CompilerFilter::kQuicken);
163
164 std::string oat_location;
165 std::string error_msg;
166 ASSERT_TRUE(OatFileAssistant::DexLocationToOatFilename(
167 dex_location, kRuntimeISA, &oat_location, &error_msg)) << error_msg;
168
169 // Ensure we can load that file. Just a precondition.
170 {
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100171 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Nicolas Geoffray30025092018-04-19 14:43:29 +0100172 oat_location.c_str(),
Andreas Gampefc604a72018-02-08 15:43:37 -0800173 oat_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100174 /*executable=*/ false,
175 /*low_4gb=*/ false,
Andreas Gampefc604a72018-02-08 15:43:37 -0800176 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100177 /*reservation=*/ nullptr,
Andreas Gampefc604a72018-02-08 15:43:37 -0800178 &error_msg));
179 ASSERT_TRUE(odex_file != nullptr);
180 ASSERT_EQ(2u, odex_file->GetOatDexFiles().size());
181 }
182
183 // Now replace the source.
184 Copy(GetTestDexFileName("MainUncompressed"), dex_location);
185
186 // And try to load again.
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100187 std::unique_ptr<OatFile> odex_file(OatFile::Open(/*zip_fd=*/ -1,
Vladimir Markoc09cd052018-08-23 16:36:36 +0100188 oat_location,
189 oat_location,
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100190 /*executable=*/ false,
191 /*low_4gb=*/ false,
Andreas Gampefc604a72018-02-08 15:43:37 -0800192 dex_location.c_str(),
Vladimir Markof4efa9e2018-10-17 14:12:45 +0100193 /*reservation=*/ nullptr,
Andreas Gampefc604a72018-02-08 15:43:37 -0800194 &error_msg));
195 EXPECT_TRUE(odex_file == nullptr);
196 EXPECT_NE(std::string::npos, error_msg.find("expected 2 uncompressed dex files, but found 1"))
197 << error_msg;
198}
199
Richard Uhlere5fed032015-03-18 08:21:11 -0700200} // namespace art