blob: 8e1501ff169ca96c58fa5f8e136dd2d2a62f9508 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "dex_file.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070018
Ian Rogers700a4022014-05-19 16:49:03 -070019#include <memory>
20
Ian Rogerse63db272014-07-15 15:36:11 -070021#include "base/stl_util.h"
22#include "base/unix_file/fd_file.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080023#include "common_runtime_test.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080024#include "dex_file-inl.h"
Orion Hodsona4c2a052016-08-17 10:51:42 +010025#include "mem_map.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "os.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_thread_state_change-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070028#include "thread-inl.h"
Alex Light9c20a142016-08-23 15:05:12 -070029#include "utils.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070030
Carl Shapiro1fb86202011-06-27 17:43:13 -070031namespace art {
32
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080033class DexFileTest : public CommonRuntimeTest {};
Brian Carlstrom9f30b382011-08-28 22:41:38 -070034
35TEST_F(DexFileTest, Open) {
Ian Rogers33e95662013-05-20 20:29:14 -070036 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -080037 std::unique_ptr<const DexFile> dex(OpenTestDexFile("Nested"));
Mathieu Chartier2cebb242015-04-21 16:50:40 -070038 ASSERT_TRUE(dex.get() != nullptr);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070039}
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070040
Alex Light9c20a142016-08-23 15:05:12 -070041static inline std::vector<uint8_t> DecodeBase64Vec(const char* src) {
42 std::vector<uint8_t> res;
43 size_t size;
44 std::unique_ptr<uint8_t[]> data(DecodeBase64(src, &size));
45 res.resize(size);
46 memcpy(res.data(), data.get(), size);
47 return res;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080048}
49
Brian Carlstrom9f30b382011-08-28 22:41:38 -070050// Although this is the same content logically as the Nested test dex,
51// the DexFileHeader test is sensitive to subtle changes in the
52// contents due to the checksum etc, so we embed the exact input here.
53//
54// class Nested {
55// class Inner {
56// }
57// }
58static const char kRawDex[] =
59 "ZGV4CjAzNQAQedgAe7gM1B/WHsWJ6L7lGAISGC7yjD2IAwAAcAAAAHhWNBIAAAAAAAAAAMQCAAAP"
60 "AAAAcAAAAAcAAACsAAAAAgAAAMgAAAABAAAA4AAAAAMAAADoAAAAAgAAAAABAABIAgAAQAEAAK4B"
61 "AAC2AQAAvQEAAM0BAADXAQAA+wEAABsCAAA+AgAAUgIAAF8CAABiAgAAZgIAAHMCAAB5AgAAgQIA"
62 "AAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAkAAAAJAAAABgAAAAAAAAAKAAAABgAAAKgBAAAAAAEA"
63 "DQAAAAAAAQAAAAAAAQAAAAAAAAAFAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAIAAAAiAEAAKsCAAAA"
64 "AAAAAQAAAAAAAAAFAAAAAAAAAAgAAACYAQAAuAIAAAAAAAACAAAAlAIAAJoCAAABAAAAowIAAAIA"
65 "AgABAAAAiAIAAAYAAABbAQAAcBACAAAADgABAAEAAQAAAI4CAAAEAAAAcBACAAAADgBAAQAAAAAA"
66 "AAAAAAAAAAAATAEAAAAAAAAAAAAAAAAAAAEAAAABAAY8aW5pdD4ABUlubmVyAA5MTmVzdGVkJElu"
67 "bmVyOwAITE5lc3RlZDsAIkxkYWx2aWsvYW5ub3RhdGlvbi9FbmNsb3NpbmdDbGFzczsAHkxkYWx2"
68 "aWsvYW5ub3RhdGlvbi9Jbm5lckNsYXNzOwAhTGRhbHZpay9hbm5vdGF0aW9uL01lbWJlckNsYXNz"
69 "ZXM7ABJMamF2YS9sYW5nL09iamVjdDsAC05lc3RlZC5qYXZhAAFWAAJWTAALYWNjZXNzRmxhZ3MA"
70 "BG5hbWUABnRoaXMkMAAFdmFsdWUAAgEABw4AAQAHDjwAAgIBDhgBAgMCCwQADBcBAgQBDhwBGAAA"
71 "AQEAAJAgAICABNQCAAABAAGAgATwAgAAEAAAAAAAAAABAAAAAAAAAAEAAAAPAAAAcAAAAAIAAAAH"
72 "AAAArAAAAAMAAAACAAAAyAAAAAQAAAABAAAA4AAAAAUAAAADAAAA6AAAAAYAAAACAAAAAAEAAAMQ"
73 "AAACAAAAQAEAAAEgAAACAAAAVAEAAAYgAAACAAAAiAEAAAEQAAABAAAAqAEAAAIgAAAPAAAArgEA"
74 "AAMgAAACAAAAiAIAAAQgAAADAAAAlAIAAAAgAAACAAAAqwIAAAAQAAABAAAAxAIAAA==";
75
Narayan Kamath52e66502016-08-01 14:20:31 +010076// kRawDex38 and 39 are dex'ed versions of the following Java source :
77//
78// public class Main {
79// public static void main(String[] foo) {
80// }
81// }
82//
83// The dex file was manually edited to change its dex version code to 38
84// or 39, respectively.
85static const char kRawDex38[] =
86 "ZGV4CjAzOAC4OovJlJ1089ikzK6asMf/f8qp3Kve5VsgAgAAcAAAAHhWNBIAAAAAAAAAAIwBAAAI"
87 "AAAAcAAAAAQAAACQAAAAAgAAAKAAAAAAAAAAAAAAAAMAAAC4AAAAAQAAANAAAAAwAQAA8AAAACIB"
88 "AAAqAQAAMgEAAEYBAABRAQAAVAE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JTWFpbi5qYXZhAAFWAAJW"
104 "TAATW0xqYXZhL2xhbmcvU3RyaW5nOwAEbWFpbgABAAcOAAMBAAcOAAAAAgAAgYAE8AEBCYgCDAAA"
105 "AAAAAAABAAAAAAAAAAEAAAAIAAAAcAAAAAIAAAAEAAAAkAAAAAMAAAACAAAAoAAAAAUAAAADAAAA"
106 "uAAAAAYAAAABAAAA0AAAAAEgAAACAAAA8AAAAAEQAAABAAAAHAEAAAIgAAAIAAAAIgEAAAMgAAAC"
107 "AAAAcwEAAAAgAAABAAAAfgEAAAAQAAABAAAAjAEAAA==";
108
ganxiaolincd16d0a2016-07-18 11:21:44 +0800109static const char kRawDexZeroLength[] =
110 "UEsDBAoAAAAAAOhxAkkAAAAAAAAAAAAAAAALABwAY2xhc3Nlcy5kZXhVVAkAA2QNoVdnDaFXdXgL"
111 "AAEE5AMBAASIEwAAUEsBAh4DCgAAAAAA6HECSQAAAAAAAAAAAAAAAAsAGAAAAAAAAAAAAKCBAAAA"
112 "AGNsYXNzZXMuZGV4VVQFAANkDaFXdXgLAAEE5AMBAASIEwAAUEsFBgAAAAABAAEAUQAAAEUAAAAA"
113 "AA==";
114
David Sehr9fddd362016-09-22 14:05:37 -0700115static const char kRawZipClassesDexPresent[] =
116 "UEsDBBQAAAAIANVRN0ms99lIMQEAACACAAALABwAY2xhc3Nlcy5kZXhVVAkAAwFj5VcUY+VXdXgL"
117 "AAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQCEwNDAQMDQ0WY"
118 "iRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGxGxAHAnEIEEcA"
119 "cS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8UFGgP6Fu4IOa"
120 "wczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYxMIX5MAhHIykL"
121 "LinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHCmg0pvBkVIGpA"
122 "Yc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACADVUTdJrPfZSDEBAAAg"
123 "AgAACwAYAAAAAAAAAAAAoIEAAAAAY2xhc3Nlcy5kZXhVVAUAAwFj5Vd1eAsAAQTkAwEABIgTAABQ"
124 "SwUGAAAAAAEAAQBRAAAAdgEAAAAA";
125
126static const char kRawZipClassesDexAbsent[] =
127 "UEsDBBQAAAAIANVRN0ms99lIMQEAACACAAAOABwAbm90Y2xhc3Nlcy5kZXhVVAkAAwFj5VcUY+VX"
128 "dXgLAAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQCEwNDAQMD"
129 "Q0WYiRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGxGxAHAnEI"
130 "EEcAcS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8UFGgP6Fu"
131 "4IOawczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYxMIX5MAhH"
132 "IykLLinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHCmg0pvBkV"
133 "IGpAYc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACADVUTdJrPfZSDEB"
134 "AAAgAgAADgAYAAAAAAAAAAAAoIEAAAAAbm90Y2xhc3Nlcy5kZXhVVAUAAwFj5Vd1eAsAAQTkAwEA"
135 "BIgTAABQSwUGAAAAAAEAAQBUAAAAeQEAAAAA";
136
137static const char kRawZipThreeDexFiles[] =
138 "UEsDBBQAAAAIAP1WN0ms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ms99lIMQEAACACAAALABwAY2xhc3Nlcy5kZXhVVAkAAwFj"
152 "5VetbOVXdXgLAAEE5AMBAASIEwAAS0mt4DIwtmDYYdV9csrcks83lpxZN2vD8f/1p1beWX3vabQC"
153 "EwNDAQMDQ0WYiRADFPQwMjBwMEDEWYB4AhADlTEsYEAAZiDeAcRApQwXgNgAyPgApJWAtBYQGwGx"
154 "GxAHAnEIEEcAcS4jRD0T1Fw2KM0ENZMVypZhRLIIqIMdag9CBMFnhtJ1jDA5RrBcMSPE7AIBkIl8"
155 "UFGgP6Fu4IOawczAZpOZl1lix8Dm45uYmWfNIOSTlViWqJ+TmJeu75+UlZpcYs3ACZLSA4kzMIYx"
156 "MIX5MAhHIykLLinKzEu3ZmDJBSoDOZiPgRlMgv3T2MDygZGRs4OJB8n9MBoWzrAwmQD1Eyy8WZHC"
157 "mg0pvBkVIGpAYc4oABEHhRuTAsRMUDwwQ9WAwoJBAaIGHE5Q9aB4BgBQSwECHgMUAAAACAD9VjdJ"
158 "rPfZSDEBAAAgAgAADAAYAAAAAAAAAAAAoIEAAAAAY2xhc3NlczIuZGV4VVQFAAOtbOVXdXgLAAEE"
159 "5AMBAASIEwAAUEsBAh4DFAAAAAgAAFc3Saz32UgxAQAAIAIAAAwAGAAAAAAAAAAAAKCBdwEAAGNs"
160 "YXNzZXMzLmRleFVUBQADr2zlV3V4CwABBOQDAQAEiBMAAFBLAQIeAxQAAAAIANVRN0ms99lIMQEA"
161 "ACACAAALABgAAAAAAAAAAACgge4CAABjbGFzc2VzLmRleFVUBQADAWPlV3V4CwABBOQDAQAEiBMA"
162 "AFBLBQYAAAAAAwADAPUAAABkBAAAAAA=";
163
Narayan Kamath52e66502016-08-01 14:20:31 +0100164static void DecodeAndWriteDexFile(const char* base64, const char* location) {
Ian Rogers33e95662013-05-20 20:29:14 -0700165 // decode base64
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700166 CHECK(base64 != nullptr);
Alex Light9c20a142016-08-23 15:05:12 -0700167 std::vector<uint8_t> dex_bytes = DecodeBase64Vec(base64);
Orion Hodsona4c2a052016-08-17 10:51:42 +0100168 CHECK_NE(dex_bytes.size(), 0u);
Ian Rogers33e95662013-05-20 20:29:14 -0700169
170 // write to provided file
Ian Rogers700a4022014-05-19 16:49:03 -0700171 std::unique_ptr<File> file(OS::CreateEmptyFile(location));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700172 CHECK(file.get() != nullptr);
Orion Hodsona4c2a052016-08-17 10:51:42 +0100173 if (!file->WriteFully(dex_bytes.data(), dex_bytes.size())) {
Ian Rogers33e95662013-05-20 20:29:14 -0700174 PLOG(FATAL) << "Failed to write base64 as dex file";
175 }
Andreas Gampe4303ba92014-11-06 01:00:46 -0800176 if (file->FlushCloseOrErase() != 0) {
177 PLOG(FATAL) << "Could not flush and close test file.";
178 }
Narayan Kamath52e66502016-08-01 14:20:31 +0100179}
180
David Sehr9fddd362016-09-22 14:05:37 -0700181static bool OpenDexFilesBase64(const char* base64,
182 const char* location,
183 std::vector<std::unique_ptr<const DexFile>>* dex_files,
184 std::string* error_msg) {
Narayan Kamath52e66502016-08-01 14:20:31 +0100185 DecodeAndWriteDexFile(base64, location);
Ian Rogers33e95662013-05-20 20:29:14 -0700186
David Sehr9fddd362016-09-22 14:05:37 -0700187 // read dex file(s)
Ian Rogers33e95662013-05-20 20:29:14 -0700188 ScopedObjectAccess soa(Thread::Current());
Aart Bik37d6a3b2016-06-21 18:30:10 -0700189 static constexpr bool kVerifyChecksum = true;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800190 std::vector<std::unique_ptr<const DexFile>> tmp;
David Sehr9fddd362016-09-22 14:05:37 -0700191 bool success = DexFile::Open(location, location, kVerifyChecksum, error_msg, &tmp);
192 if (success) {
193 for (std::unique_ptr<const DexFile>& dex_file : tmp) {
194 EXPECT_EQ(PROT_READ, dex_file->GetPermissions());
195 EXPECT_TRUE(dex_file->IsReadOnly());
196 }
197 *dex_files = std::move(tmp);
198 }
199 return success;
200}
201
202static std::unique_ptr<const DexFile> OpenDexFileBase64(const char* base64,
203 const char* location) {
204 // read dex files.
205 std::string error_msg;
206 std::vector<std::unique_ptr<const DexFile>> dex_files;
207 bool success = OpenDexFilesBase64(base64, location, &dex_files, &error_msg);
Andreas Gampe833a4852014-05-21 18:46:59 -0700208 CHECK(success) << error_msg;
David Sehr9fddd362016-09-22 14:05:37 -0700209 EXPECT_EQ(1U, dex_files.size());
210 return std::move(dex_files[0]);
Ian Rogers33e95662013-05-20 20:29:14 -0700211}
212
Orion Hodsona4c2a052016-08-17 10:51:42 +0100213static std::unique_ptr<const DexFile> OpenDexFileInMemoryBase64(const char* base64,
214 const char* location,
215 uint32_t location_checksum) {
216 CHECK(base64 != nullptr);
Alex Light9c20a142016-08-23 15:05:12 -0700217 std::vector<uint8_t> dex_bytes = DecodeBase64Vec(base64);
Orion Hodsona4c2a052016-08-17 10:51:42 +0100218 CHECK_NE(dex_bytes.size(), 0u);
219
220 std::string error_message;
221 std::unique_ptr<MemMap> region(MemMap::MapAnonymous("test-region",
222 nullptr,
223 dex_bytes.size(),
224 PROT_READ | PROT_WRITE,
225 /* low_4gb */ false,
226 /* reuse */ false,
227 &error_message));
228 memcpy(region->Begin(), dex_bytes.data(), dex_bytes.size());
229 std::unique_ptr<const DexFile> dex_file(DexFile::Open(location,
230 location_checksum,
231 std::move(region),
232 /* verify */ true,
233 /* verify_checksum */ true,
234 &error_message));
235 CHECK(dex_file != nullptr) << error_message;
236 return dex_file;
237}
238
Orion Hodsoncffc5292016-09-05 13:43:11 +0100239static void ValidateDexFileHeader(std::unique_ptr<const DexFile> dex_file) {
240 static const uint8_t kExpectedDexFileMagic[8] = {
241 /* d */ 0x64, /* e */ 0x64, /* x */ 0x78, /* \n */ 0x0d,
242 /* 0 */ 0x30, /* 3 */ 0x33, /* 5 */ 0x35, /* \0 */ 0x00
243 };
244 static const uint8_t kExpectedSha1[DexFile::kSha1DigestSize] = {
245 0x7b, 0xb8, 0x0c, 0xd4, 0x1f, 0xd6, 0x1e, 0xc5,
246 0x89, 0xe8, 0xbe, 0xe5, 0x18, 0x02, 0x12, 0x18,
247 0x2e, 0xf2, 0x8c, 0x3d,
248 };
Orion Hodsona4c2a052016-08-17 10:51:42 +0100249
Orion Hodsoncffc5292016-09-05 13:43:11 +0100250 const DexFile::Header& header = dex_file->GetHeader();
251 EXPECT_EQ(*kExpectedDexFileMagic, *header.magic_);
Orion Hodsona4c2a052016-08-17 10:51:42 +0100252 EXPECT_EQ(0x00d87910U, header.checksum_);
Orion Hodsoncffc5292016-09-05 13:43:11 +0100253 EXPECT_EQ(*kExpectedSha1, *header.signature_);
Orion Hodsona4c2a052016-08-17 10:51:42 +0100254 EXPECT_EQ(904U, header.file_size_);
255 EXPECT_EQ(112U, header.header_size_);
256 EXPECT_EQ(0U, header.link_size_);
257 EXPECT_EQ(0U, header.link_off_);
258 EXPECT_EQ(15U, header.string_ids_size_);
259 EXPECT_EQ(112U, header.string_ids_off_);
260 EXPECT_EQ(7U, header.type_ids_size_);
261 EXPECT_EQ(172U, header.type_ids_off_);
262 EXPECT_EQ(2U, header.proto_ids_size_);
263 EXPECT_EQ(200U, header.proto_ids_off_);
264 EXPECT_EQ(1U, header.field_ids_size_);
265 EXPECT_EQ(224U, header.field_ids_off_);
266 EXPECT_EQ(3U, header.method_ids_size_);
267 EXPECT_EQ(232U, header.method_ids_off_);
268 EXPECT_EQ(2U, header.class_defs_size_);
269 EXPECT_EQ(256U, header.class_defs_off_);
270 EXPECT_EQ(584U, header.data_size_);
271 EXPECT_EQ(320U, header.data_off_);
272
Orion Hodsoncffc5292016-09-05 13:43:11 +0100273 EXPECT_EQ(header.checksum_, dex_file->GetLocationChecksum());
274}
275
276TEST_F(DexFileTest, Header) {
277 ScratchFile tmp;
278 std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex, tmp.GetFilename().c_str()));
279 ValidateDexFileHeader(std::move(raw));
Orion Hodsona4c2a052016-08-17 10:51:42 +0100280}
281
282TEST_F(DexFileTest, HeaderInMemory) {
283 ScratchFile tmp;
284 std::unique_ptr<const DexFile> raw =
285 OpenDexFileInMemoryBase64(kRawDex, tmp.GetFilename().c_str(), 0x00d87910U);
Orion Hodsoncffc5292016-09-05 13:43:11 +0100286 ValidateDexFileHeader(std::move(raw));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800287}
288
Narayan Kamath52e66502016-08-01 14:20:31 +0100289TEST_F(DexFileTest, Version38Accepted) {
290 ScratchFile tmp;
291 std::unique_ptr<const DexFile> raw(OpenDexFileBase64(kRawDex38, tmp.GetFilename().c_str()));
292 ASSERT_TRUE(raw.get() != nullptr);
293
294 const DexFile::Header& header = raw->GetHeader();
295 EXPECT_EQ(38u, header.GetVersion());
296}
297
298TEST_F(DexFileTest, Version39Rejected) {
299 ScratchFile tmp;
300 const char* location = tmp.GetFilename().c_str();
301 DecodeAndWriteDexFile(kRawDex39, location);
302
303 ScopedObjectAccess soa(Thread::Current());
304 static constexpr bool kVerifyChecksum = true;
305 std::string error_msg;
306 std::vector<std::unique_ptr<const DexFile>> dex_files;
307 ASSERT_FALSE(DexFile::Open(location, location, kVerifyChecksum, &error_msg, &dex_files));
308}
309
ganxiaolincd16d0a2016-07-18 11:21:44 +0800310TEST_F(DexFileTest, ZeroLengthDexRejected) {
311 ScratchFile tmp;
312 const char* location = tmp.GetFilename().c_str();
313 DecodeAndWriteDexFile(kRawDexZeroLength, location);
314
315 ScopedObjectAccess soa(Thread::Current());
316 static constexpr bool kVerifyChecksum = true;
317 std::string error_msg;
318 std::vector<std::unique_ptr<const DexFile>> dex_files;
319 ASSERT_FALSE(DexFile::Open(location, location, kVerifyChecksum, &error_msg, &dex_files));
320}
321
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800322TEST_F(DexFileTest, GetLocationChecksum) {
Ian Rogers33e95662013-05-20 20:29:14 -0700323 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800324 std::unique_ptr<const DexFile> raw(OpenTestDexFile("Main"));
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800325 EXPECT_NE(raw->GetHeader().checksum_, raw->GetLocationChecksum());
326}
327
328TEST_F(DexFileTest, GetChecksum) {
329 uint32_t checksum;
Ian Rogers33e95662013-05-20 20:29:14 -0700330 ScopedObjectAccess soa(Thread::Current());
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700331 std::string error_msg;
Przemyslaw Szczepaniak5b8e6e32015-09-30 14:40:33 +0100332 EXPECT_TRUE(DexFile::GetChecksum(GetLibCoreDexFileNames()[0].c_str(), &checksum, &error_msg))
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700333 << error_msg;
Brian Carlstrom5b332c82012-02-01 15:02:31 -0800334 EXPECT_EQ(java_lang_dex_file_->GetLocationChecksum(), checksum);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700335}
336
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700337TEST_F(DexFileTest, ClassDefs) {
Ian Rogers33e95662013-05-20 20:29:14 -0700338 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800339 std::unique_ptr<const DexFile> raw(OpenTestDexFile("Nested"));
340 ASSERT_TRUE(raw.get() != nullptr);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700341 EXPECT_EQ(2U, raw->NumClassDefs());
342
Brian Carlstromf615a612011-07-23 12:50:34 -0700343 const DexFile::ClassDef& c0 = raw->GetClassDef(0);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700344 EXPECT_STREQ("LNested$Inner;", raw->GetClassDescriptor(c0));
345
Brian Carlstromf615a612011-07-23 12:50:34 -0700346 const DexFile::ClassDef& c1 = raw->GetClassDef(1);
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700347 EXPECT_STREQ("LNested;", raw->GetClassDescriptor(c1));
Carl Shapiro1fb86202011-06-27 17:43:13 -0700348}
349
Ian Rogersd91d6d62013-09-25 20:26:14 -0700350TEST_F(DexFileTest, GetMethodSignature) {
Ian Rogers33e95662013-05-20 20:29:14 -0700351 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800352 std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature"));
353 ASSERT_TRUE(raw.get() != nullptr);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700354 EXPECT_EQ(1U, raw->NumClassDefs());
355
356 const DexFile::ClassDef& class_def = raw->GetClassDef(0);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700357 ASSERT_STREQ("LGetMethodSignature;", raw->GetClassDescriptor(class_def));
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700358
Ian Rogers13735952014-10-08 12:43:28 -0700359 const uint8_t* class_data = raw->GetClassData(class_def);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700360 ASSERT_TRUE(class_data != nullptr);
Elliott Hughes4d6850c2012-01-18 15:55:06 -0800361 ClassDataItemIterator it(*raw, class_data);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700362
Ian Rogers0571d352011-11-03 19:51:38 -0700363 EXPECT_EQ(1u, it.NumDirectMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700364
Ian Rogers0571d352011-11-03 19:51:38 -0700365 // Check the signature for the static initializer.
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700366 {
Ian Rogers0571d352011-11-03 19:51:38 -0700367 ASSERT_EQ(1U, it.NumDirectMethods());
368 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Ian Rogers0571d352011-11-03 19:51:38 -0700369 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700370 ASSERT_STREQ("<init>", name);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700371 std::string signature(raw->GetMethodSignature(method_id).ToString());
Ian Rogers0571d352011-11-03 19:51:38 -0700372 ASSERT_EQ("()V", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700373 }
374
375 // Check both virtual methods.
Ian Rogers0571d352011-11-03 19:51:38 -0700376 ASSERT_EQ(2U, it.NumVirtualMethods());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700377 {
Ian Rogers0571d352011-11-03 19:51:38 -0700378 it.Next();
379 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700380
Ian Rogers0571d352011-11-03 19:51:38 -0700381 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700382 ASSERT_STREQ("m1", name);
383
Ian Rogersd91d6d62013-09-25 20:26:14 -0700384 std::string signature(raw->GetMethodSignature(method_id).ToString());
Ian Rogers0571d352011-11-03 19:51:38 -0700385 ASSERT_EQ("(IDJLjava/lang/Object;)Ljava/lang/Float;", signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700386 }
387
388 {
Ian Rogers0571d352011-11-03 19:51:38 -0700389 it.Next();
390 const DexFile::MethodId& method_id = raw->GetMethodId(it.GetMemberIndex());
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700391
Ian Rogers0571d352011-11-03 19:51:38 -0700392 const char* name = raw->StringDataByIdx(method_id.name_idx_);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700393 ASSERT_STREQ("m2", name);
394
Ian Rogersd91d6d62013-09-25 20:26:14 -0700395 std::string signature(raw->GetMethodSignature(method_id).ToString());
396 ASSERT_EQ("(ZSC)LGetMethodSignature;", signature);
Ian Rogers0571d352011-11-03 19:51:38 -0700397 }
398}
399
400TEST_F(DexFileTest, FindStringId) {
Ian Rogers33e95662013-05-20 20:29:14 -0700401 ScopedObjectAccess soa(Thread::Current());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800402 std::unique_ptr<const DexFile> raw(OpenTestDexFile("GetMethodSignature"));
403 ASSERT_TRUE(raw.get() != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -0700404 EXPECT_EQ(1U, raw->NumClassDefs());
405
Ian Rogersd91d6d62013-09-25 20:26:14 -0700406 const char* strings[] = { "LGetMethodSignature;", "Ljava/lang/Float;", "Ljava/lang/Object;",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700407 "D", "I", "J", nullptr };
408 for (size_t i = 0; strings[i] != nullptr; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700409 const char* str = strings[i];
410 const DexFile::StringId* str_id = raw->FindStringId(str);
411 const char* dex_str = raw->GetStringData(*str_id);
412 EXPECT_STREQ(dex_str, str);
413 }
414}
415
416TEST_F(DexFileTest, FindTypeId) {
417 for (size_t i = 0; i < java_lang_dex_file_->NumTypeIds(); i++) {
418 const char* type_str = java_lang_dex_file_->StringByTypeIdx(i);
419 const DexFile::StringId* type_str_id = java_lang_dex_file_->FindStringId(type_str);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700420 ASSERT_TRUE(type_str_id != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -0700421 uint32_t type_str_idx = java_lang_dex_file_->GetIndexForStringId(*type_str_id);
422 const DexFile::TypeId* type_id = java_lang_dex_file_->FindTypeId(type_str_idx);
Mathieu Chartier9507fa22015-10-29 15:08:57 -0700423 ASSERT_EQ(type_id, java_lang_dex_file_->FindTypeId(type_str));
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700424 ASSERT_TRUE(type_id != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -0700425 EXPECT_EQ(java_lang_dex_file_->GetIndexForTypeId(*type_id), i);
426 }
427}
428
429TEST_F(DexFileTest, FindProtoId) {
430 for (size_t i = 0; i < java_lang_dex_file_->NumProtoIds(); i++) {
431 const DexFile::ProtoId& to_find = java_lang_dex_file_->GetProtoId(i);
432 const DexFile::TypeList* to_find_tl = java_lang_dex_file_->GetProtoParameters(to_find);
433 std::vector<uint16_t> to_find_types;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700434 if (to_find_tl != nullptr) {
Ian Rogers0571d352011-11-03 19:51:38 -0700435 for (size_t j = 0; j < to_find_tl->Size(); j++) {
436 to_find_types.push_back(to_find_tl->GetTypeItem(j).type_idx_);
437 }
438 }
439 const DexFile::ProtoId* found =
440 java_lang_dex_file_->FindProtoId(to_find.return_type_idx_, to_find_types);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700441 ASSERT_TRUE(found != nullptr);
Ian Rogers0571d352011-11-03 19:51:38 -0700442 EXPECT_EQ(java_lang_dex_file_->GetIndexForProtoId(*found), i);
443 }
444}
445
446TEST_F(DexFileTest, FindMethodId) {
447 for (size_t i = 0; i < java_lang_dex_file_->NumMethodIds(); i++) {
448 const DexFile::MethodId& to_find = java_lang_dex_file_->GetMethodId(i);
449 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
450 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
451 const DexFile::ProtoId& signature = java_lang_dex_file_->GetProtoId(to_find.proto_idx_);
452 const DexFile::MethodId* found = java_lang_dex_file_->FindMethodId(klass, name, signature);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700453 ASSERT_TRUE(found != nullptr) << "Didn't find method " << i << ": "
Ian Rogers0571d352011-11-03 19:51:38 -0700454 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
455 << java_lang_dex_file_->GetStringData(name)
Ian Rogersd91d6d62013-09-25 20:26:14 -0700456 << java_lang_dex_file_->GetMethodSignature(to_find);
Ian Rogers0571d352011-11-03 19:51:38 -0700457 EXPECT_EQ(java_lang_dex_file_->GetIndexForMethodId(*found), i);
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700458 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700459}
460
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800461TEST_F(DexFileTest, FindFieldId) {
462 for (size_t i = 0; i < java_lang_dex_file_->NumFieldIds(); i++) {
463 const DexFile::FieldId& to_find = java_lang_dex_file_->GetFieldId(i);
464 const DexFile::TypeId& klass = java_lang_dex_file_->GetTypeId(to_find.class_idx_);
465 const DexFile::StringId& name = java_lang_dex_file_->GetStringId(to_find.name_idx_);
466 const DexFile::TypeId& type = java_lang_dex_file_->GetTypeId(to_find.type_idx_);
467 const DexFile::FieldId* found = java_lang_dex_file_->FindFieldId(klass, name, type);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700468 ASSERT_TRUE(found != nullptr) << "Didn't find field " << i << ": "
Ian Rogers9b1a4f42011-11-14 18:35:10 -0800469 << java_lang_dex_file_->StringByTypeIdx(to_find.type_idx_) << " "
470 << java_lang_dex_file_->StringByTypeIdx(to_find.class_idx_) << "."
471 << java_lang_dex_file_->GetStringData(name);
472 EXPECT_EQ(java_lang_dex_file_->GetIndexForFieldId(*found), i);
473 }
474}
475
Calin Juravle4e1d5792014-07-15 23:56:47 +0100476TEST_F(DexFileTest, GetMultiDexClassesDexName) {
Andreas Gampe90e34042015-04-27 20:01:52 -0700477 ASSERT_EQ("classes.dex", DexFile::GetMultiDexClassesDexName(0));
478 ASSERT_EQ("classes2.dex", DexFile::GetMultiDexClassesDexName(1));
479 ASSERT_EQ("classes3.dex", DexFile::GetMultiDexClassesDexName(2));
480 ASSERT_EQ("classes100.dex", DexFile::GetMultiDexClassesDexName(99));
481}
482
483TEST_F(DexFileTest, GetMultiDexLocation) {
Calin Juravle4e1d5792014-07-15 23:56:47 +0100484 std::string dex_location_str = "/system/app/framework.jar";
485 const char* dex_location = dex_location_str.c_str();
Andreas Gampe90e34042015-04-27 20:01:52 -0700486 ASSERT_EQ("/system/app/framework.jar", DexFile::GetMultiDexLocation(0, dex_location));
487 ASSERT_EQ("/system/app/framework.jar:classes2.dex",
488 DexFile::GetMultiDexLocation(1, dex_location));
489 ASSERT_EQ("/system/app/framework.jar:classes101.dex",
490 DexFile::GetMultiDexLocation(100, dex_location));
Calin Juravle4e1d5792014-07-15 23:56:47 +0100491}
492
493TEST_F(DexFileTest, GetDexCanonicalLocation) {
494 ScratchFile file;
Vladimir Markoaa4497d2014-09-05 14:01:17 +0100495 UniqueCPtr<const char[]> dex_location_real(realpath(file.GetFilename().c_str(), nullptr));
496 std::string dex_location(dex_location_real.get());
Calin Juravle4e1d5792014-07-15 23:56:47 +0100497
Calin Juravle61281dc2014-08-07 11:54:36 +0100498 ASSERT_EQ(dex_location, DexFile::GetDexCanonicalLocation(dex_location.c_str()));
Andreas Gampe90e34042015-04-27 20:01:52 -0700499 std::string multidex_location = DexFile::GetMultiDexLocation(1, dex_location.c_str());
Calin Juravle4e1d5792014-07-15 23:56:47 +0100500 ASSERT_EQ(multidex_location, DexFile::GetDexCanonicalLocation(multidex_location.c_str()));
501
502 std::string dex_location_sym = dex_location + "symlink";
503 ASSERT_EQ(0, symlink(dex_location.c_str(), dex_location_sym.c_str()));
504
505 ASSERT_EQ(dex_location, DexFile::GetDexCanonicalLocation(dex_location_sym.c_str()));
506
Andreas Gampe90e34042015-04-27 20:01:52 -0700507 std::string multidex_location_sym = DexFile::GetMultiDexLocation(1, dex_location_sym.c_str());
Calin Juravle4e1d5792014-07-15 23:56:47 +0100508 ASSERT_EQ(multidex_location, DexFile::GetDexCanonicalLocation(multidex_location_sym.c_str()));
509
510 ASSERT_EQ(0, unlink(dex_location_sym.c_str()));
511}
512
Richard Uhlere5fed032015-03-18 08:21:11 -0700513TEST(DexFileUtilsTest, GetBaseLocationAndMultiDexSuffix) {
514 EXPECT_EQ("/foo/bar/baz.jar", DexFile::GetBaseLocation("/foo/bar/baz.jar"));
515 EXPECT_EQ("/foo/bar/baz.jar", DexFile::GetBaseLocation("/foo/bar/baz.jar:classes2.dex"));
516 EXPECT_EQ("/foo/bar/baz.jar", DexFile::GetBaseLocation("/foo/bar/baz.jar:classes8.dex"));
517 EXPECT_EQ("", DexFile::GetMultiDexSuffix("/foo/bar/baz.jar"));
518 EXPECT_EQ(":classes2.dex", DexFile::GetMultiDexSuffix("/foo/bar/baz.jar:classes2.dex"));
519 EXPECT_EQ(":classes8.dex", DexFile::GetMultiDexSuffix("/foo/bar/baz.jar:classes8.dex"));
520}
521
David Sehr9fddd362016-09-22 14:05:37 -0700522TEST_F(DexFileTest, ZipOpenClassesPresent) {
523 ScratchFile tmp;
524 std::vector<std::unique_ptr<const DexFile>> dex_files;
525 std::string error_msg;
526 ASSERT_TRUE(OpenDexFilesBase64(kRawZipClassesDexPresent, tmp.GetFilename().c_str(), &dex_files,
527 &error_msg));
528 EXPECT_EQ(dex_files.size(), 1u);
529}
530
531TEST_F(DexFileTest, ZipOpenClassesAbsent) {
532 ScratchFile tmp;
533 std::vector<std::unique_ptr<const DexFile>> dex_files;
534 std::string error_msg;
535 ASSERT_FALSE(OpenDexFilesBase64(kRawZipClassesDexAbsent, tmp.GetFilename().c_str(), &dex_files,
536 &error_msg));
537 EXPECT_EQ(dex_files.size(), 0u);
538}
539
540TEST_F(DexFileTest, ZipOpenThreeDexFiles) {
541 ScratchFile tmp;
542 std::vector<std::unique_ptr<const DexFile>> dex_files;
543 std::string error_msg;
544 ASSERT_TRUE(OpenDexFilesBase64(kRawZipThreeDexFiles, tmp.GetFilename().c_str(), &dex_files,
545 &error_msg));
546 EXPECT_EQ(dex_files.size(), 3u);
547}
548
Carl Shapiro1fb86202011-06-27 17:43:13 -0700549} // namespace art