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
TEST(read_apk,FindElfInApkByOffset)25 TEST(read_apk, FindElfInApkByOffset) {
26 ApkInspector inspector;
27 ASSERT_TRUE(inspector.FindElfInApkByOffset("/dev/null", 0) == nullptr);
28 ASSERT_TRUE(inspector.FindElfInApkByOffset(GetTestData(APK_FILE), 0) == nullptr);
29 // Test if we can read the EmbeddedElf using an offset inside its [offset, offset+size] range
30 // in the apk file.
31 EmbeddedElf* ee = inspector.FindElfInApkByOffset(
32 GetTestData(APK_FILE), NATIVELIB_OFFSET_IN_APK + NATIVELIB_SIZE_IN_APK / 2);
33 ASSERT_TRUE(ee != nullptr);
34 ASSERT_EQ(NATIVELIB_IN_APK, ee->entry_name());
35 ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset());
36 ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size());
37 }
38
TEST(read_apk,FindElfInApkByName)39 TEST(read_apk, FindElfInApkByName) {
40 ASSERT_TRUE(ApkInspector::FindElfInApkByName("/dev/null", "") == nullptr);
41 ASSERT_TRUE(ApkInspector::FindElfInApkByName(GetTestData(APK_FILE), "") == nullptr);
42 auto ee = ApkInspector::FindElfInApkByName(GetTestData(APK_FILE), NATIVELIB_IN_APK);
43 ASSERT_TRUE(ee != nullptr);
44 ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset());
45 ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size());
46 }
47
TEST(read_apk,ParseExtractedInMemoryPath)48 TEST(read_apk, ParseExtractedInMemoryPath) {
49 std::string zip_path;
50 std::string entry_name;
51 ASSERT_TRUE(ParseExtractedInMemoryPath(
52 "[anon:dalvik-classes.dex extracted in memory from "
53 "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/"
54 "base.apk]",
55 &zip_path, &entry_name));
56 ASSERT_EQ(zip_path,
57 "/data/app/com.example.simpleperf.simpleperfexamplepurejava"
58 "-HZK6bPs3Z9SDT3a-tqmasA==/base.apk");
59 ASSERT_EQ(entry_name, "classes.dex");
60 ASSERT_FALSE(
61 ParseExtractedInMemoryPath("[anon:dalvik-thread local mark stack]", &zip_path, &entry_name));
62 ASSERT_TRUE(ParseExtractedInMemoryPath(
63 "/dev/ashmem/dalvik-classes.dex extracted in memory from "
64 "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/base.apk"
65 " (deleted)",
66 &zip_path, &entry_name));
67 ASSERT_EQ(zip_path,
68 "/data/app/com.example.simpleperf.simpleperfexamplepurejava"
69 "-HZK6bPs3Z9SDT3a-tqmasA==/base.apk");
70 ASSERT_EQ(entry_name, "classes.dex");
71 ASSERT_FALSE(ParseExtractedInMemoryPath("/dev/ashmem/dalvik-thread local mark stack (deleted)",
72 &zip_path, &entry_name));
73
74 // Parse multidex file.
75 ASSERT_TRUE(ParseExtractedInMemoryPath(
76 "/dev/ashmem/dalvik-classes2.dex extracted in memory from "
77 "/data/app/getxml.test.com.testgetxml-knxI11ZXLT-OVBs9X9bSkw==/base.apk!classes2.dex "
78 "(deleted)",
79 &zip_path, &entry_name));
80 ASSERT_EQ(zip_path, "/data/app/getxml.test.com.testgetxml-knxI11ZXLT-OVBs9X9bSkw==/base.apk");
81 ASSERT_EQ(entry_name, "classes2.dex");
82 }
83