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