| Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 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> |
| Yabin Cui | 569f64a | 2016-02-05 17:32:08 -0800 | [diff] [blame] | 20 | #include "get_test_data.h" |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 21 | #include "test_util.h" |
| Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 22 | |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 23 | TEST(read_apk, FindElfInApkByOffset) { |
| Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 24 | ApkInspector inspector; |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 25 | ASSERT_TRUE(inspector.FindElfInApkByOffset("/dev/null", 0) == nullptr); |
| 26 | ASSERT_TRUE(inspector.FindElfInApkByOffset(GetTestData(APK_FILE), 0) == nullptr); |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 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); |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 31 | ASSERT_TRUE(ee != nullptr); |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 32 | ASSERT_EQ(NATIVELIB_IN_APK, ee->entry_name()); |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 33 | ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset()); |
| 34 | ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size()); |
| 35 | } |
| 36 | |
| 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 | } |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 45 | |
| 46 | TEST(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 | } |