xref: /aosp_15_r20/system/extras/simpleperf/read_apk_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
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>
20 #include "get_test_data.h"
21 #include "test_util.h"
22 
23 using namespace simpleperf;
24 
25 // @CddTest = 6.1/C-0-2
TEST(read_apk,FindElfInApkByOffset)26 TEST(read_apk, FindElfInApkByOffset) {
27   ApkInspector inspector;
28   ASSERT_TRUE(inspector.FindElfInApkByOffset("/dev/null", 0) == nullptr);
29   ASSERT_TRUE(inspector.FindElfInApkByOffset(GetTestData(APK_FILE), 0) == nullptr);
30   // Test if we can read the EmbeddedElf using an offset inside its [offset, offset+size] range
31   // in the apk file.
32   EmbeddedElf* ee = inspector.FindElfInApkByOffset(
33       GetTestData(APK_FILE), NATIVELIB_OFFSET_IN_APK + NATIVELIB_SIZE_IN_APK / 2);
34   ASSERT_TRUE(ee != nullptr);
35   ASSERT_EQ(NATIVELIB_IN_APK, ee->entry_name());
36   ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset());
37   ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size());
38 }
39 
40 // @CddTest = 6.1/C-0-2
TEST(read_apk,FindElfInApkByName)41 TEST(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 }
49 
50 // @CddTest = 6.1/C-0-2
TEST(read_apk,ParseExtractedInMemoryPath)51 TEST(read_apk, ParseExtractedInMemoryPath) {
52   std::string zip_path;
53   std::string entry_name;
54   ASSERT_TRUE(ParseExtractedInMemoryPath(
55       "[anon:dalvik-classes.dex extracted in memory from "
56       "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/"
57       "base.apk]",
58       &zip_path, &entry_name));
59   ASSERT_EQ(zip_path,
60             "/data/app/com.example.simpleperf.simpleperfexamplepurejava"
61             "-HZK6bPs3Z9SDT3a-tqmasA==/base.apk");
62   ASSERT_EQ(entry_name, "classes.dex");
63   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 "
67       "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/base.apk"
68       " (deleted)",
69       &zip_path, &entry_name));
70   ASSERT_EQ(zip_path,
71             "/data/app/com.example.simpleperf.simpleperfexamplepurejava"
72             "-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));
76 
77   // Parse multidex file.
78   ASSERT_TRUE(ParseExtractedInMemoryPath(
79       "/dev/ashmem/dalvik-classes2.dex extracted in memory from "
80       "/data/app/getxml.test.com.testgetxml-knxI11ZXLT-OVBs9X9bSkw==/base.apk!classes2.dex "
81       "(deleted)",
82       &zip_path, &entry_name));
83   ASSERT_EQ(zip_path, "/data/app/getxml.test.com.testgetxml-knxI11ZXLT-OVBs9X9bSkw==/base.apk");
84   ASSERT_EQ(entry_name, "classes2.dex");
85 }
86