blob: d5e44e1b1bcf4fbf8a1b806754bd8669731c044b [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 Cuib1a885b2016-02-14 19:18:02 -080023TEST(read_apk, FindElfInApkByOffset) {
Than McIntoshf831e6d2016-02-01 19:50:20 -050024 ApkInspector inspector;
Yabin Cuib1a885b2016-02-14 19:18:02 -080025 ASSERT_TRUE(inspector.FindElfInApkByOffset("/dev/null", 0) == nullptr);
26 ASSERT_TRUE(inspector.FindElfInApkByOffset(GetTestData(APK_FILE), 0) == nullptr);
Yabin Cui8f680f62016-03-18 18:47:43 -070027 // 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);
Yabin Cuib1a885b2016-02-14 19:18:02 -080031 ASSERT_TRUE(ee != nullptr);
Yabin Cui8f680f62016-03-18 18:47:43 -070032 ASSERT_EQ(NATIVELIB_IN_APK, ee->entry_name());
Yabin Cuib1a885b2016-02-14 19:18:02 -080033 ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset());
34 ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size());
35}
36
37TEST(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}
Yabin Cui2a53ff32018-05-21 17:37:00 -070045
46TEST(read_apk, ParseExtractedInMemoryPath) {
47 std::string zip_path;
48 std::string entry_name;
49 ASSERT_TRUE(ParseExtractedInMemoryPath("/dev/ashmem/dalvik-classes.dex extracted in memory from "
50 "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/base.apk"
51 " (deleted)", &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("/dev/ashmem/dalvik-thread local mark stack (deleted)",
56 &zip_path, &entry_name));
57}