blob: c0be177c392cce30d2a900bb75f141c0eb72c02d [file] [log] [blame]
Than McIntoshf831e6d2016-02-01 19:50:20 -05001/*
2 * Copyright (C) 2016 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 "read_apk.h"
18
19#include <gtest/gtest.h>
Yabin Cui569f64a2016-02-05 17:32:08 -080020#include "get_test_data.h"
Yabin Cuib1a885b2016-02-14 19:18:02 -080021#include "test_util.h"
Than McIntoshf831e6d2016-02-01 19:50:20 -050022
Yabin Cuifaa7b922021-01-11 17:35:57 -080023using namespace simpleperf;
24
Yabin Cui648c3e42024-04-02 13:12:45 -070025// @CddTest = 6.1/C-0-2
Yabin Cuib1a885b2016-02-14 19:18:02 -080026TEST(read_apk, FindElfInApkByOffset) {
Than McIntoshf831e6d2016-02-01 19:50:20 -050027 ApkInspector inspector;
Yabin Cuib1a885b2016-02-14 19:18:02 -080028 ASSERT_TRUE(inspector.FindElfInApkByOffset("/dev/null", 0) == nullptr);
29 ASSERT_TRUE(inspector.FindElfInApkByOffset(GetTestData(APK_FILE), 0) == nullptr);
Yabin Cui8f680f62016-03-18 18:47:43 -070030 // Test if we can read the EmbeddedElf using an offset inside its [offset, offset+size] range
31 // in the apk file.
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020032 EmbeddedElf* ee = inspector.FindElfInApkByOffset(
33 GetTestData(APK_FILE), NATIVELIB_OFFSET_IN_APK + NATIVELIB_SIZE_IN_APK / 2);
Yabin Cuib1a885b2016-02-14 19:18:02 -080034 ASSERT_TRUE(ee != nullptr);
Yabin Cui8f680f62016-03-18 18:47:43 -070035 ASSERT_EQ(NATIVELIB_IN_APK, ee->entry_name());
Yabin Cuib1a885b2016-02-14 19:18:02 -080036 ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset());
37 ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size());
38}
39
Yabin Cui648c3e42024-04-02 13:12:45 -070040// @CddTest = 6.1/C-0-2
Yabin Cuib1a885b2016-02-14 19:18:02 -080041TEST(read_apk, FindElfInApkByName) {
42 ASSERT_TRUE(ApkInspector::FindElfInApkByName("/dev/null", "") == nullptr);
43 ASSERT_TRUE(ApkInspector::FindElfInApkByName(GetTestData(APK_FILE), "") == nullptr);
44 auto ee = ApkInspector::FindElfInApkByName(GetTestData(APK_FILE), NATIVELIB_IN_APK);
45 ASSERT_TRUE(ee != nullptr);
46 ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset());
47 ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size());
48}
Yabin Cui2a53ff32018-05-21 17:37:00 -070049
Yabin Cui648c3e42024-04-02 13:12:45 -070050// @CddTest = 6.1/C-0-2
Yabin Cui2a53ff32018-05-21 17:37:00 -070051TEST(read_apk, ParseExtractedInMemoryPath) {
52 std::string zip_path;
53 std::string entry_name;
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020054 ASSERT_TRUE(ParseExtractedInMemoryPath(
55 "[anon:dalvik-classes.dex extracted in memory from "
Joel Fernandes7e8b2b92018-08-24 12:20:28 -070056 "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/"
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020057 "base.apk]",
58 &zip_path, &entry_name));
59 ASSERT_EQ(zip_path,
60 "/data/app/com.example.simpleperf.simpleperfexamplepurejava"
Joel Fernandes7e8b2b92018-08-24 12:20:28 -070061 "-HZK6bPs3Z9SDT3a-tqmasA==/base.apk");
62 ASSERT_EQ(entry_name, "classes.dex");
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020063 ASSERT_FALSE(
64 ParseExtractedInMemoryPath("[anon:dalvik-thread local mark stack]", &zip_path, &entry_name));
65 ASSERT_TRUE(ParseExtractedInMemoryPath(
66 "/dev/ashmem/dalvik-classes.dex extracted in memory from "
Yabin Cui2a53ff32018-05-21 17:37:00 -070067 "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/base.apk"
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020068 " (deleted)",
69 &zip_path, &entry_name));
70 ASSERT_EQ(zip_path,
71 "/data/app/com.example.simpleperf.simpleperfexamplepurejava"
Yabin Cui2a53ff32018-05-21 17:37:00 -070072 "-HZK6bPs3Z9SDT3a-tqmasA==/base.apk");
73 ASSERT_EQ(entry_name, "classes.dex");
74 ASSERT_FALSE(ParseExtractedInMemoryPath("/dev/ashmem/dalvik-thread local mark stack (deleted)",
75 &zip_path, &entry_name));
Yabin Cui16ab77d2018-12-13 11:34:17 -080076
77 // Parse multidex file.
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020078 ASSERT_TRUE(ParseExtractedInMemoryPath(
79 "/dev/ashmem/dalvik-classes2.dex extracted in memory from "
Yabin Cui16ab77d2018-12-13 11:34:17 -080080 "/data/app/getxml.test.com.testgetxml-knxI11ZXLT-OVBs9X9bSkw==/base.apk!classes2.dex "
Thiébaud Weksteen4848ee02020-10-23 16:06:59 +020081 "(deleted)",
82 &zip_path, &entry_name));
Yabin Cui16ab77d2018-12-13 11:34:17 -080083 ASSERT_EQ(zip_path, "/data/app/getxml.test.com.testgetxml-knxI11ZXLT-OVBs9X9bSkw==/base.apk");
84 ASSERT_EQ(entry_name, "classes2.dex");
Yabin Cui2a53ff32018-05-21 17:37:00 -070085}