| 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 | faa7b92 | 2021-01-11 17:35:57 -0800 | [diff] [blame] | 23 | using namespace simpleperf; |
| 24 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 25 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 26 | TEST(read_apk, FindElfInApkByOffset) { |
| Than McIntosh | f831e6d | 2016-02-01 19:50:20 -0500 | [diff] [blame] | 27 | ApkInspector inspector; |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 28 | ASSERT_TRUE(inspector.FindElfInApkByOffset("/dev/null", 0) == nullptr); |
| 29 | ASSERT_TRUE(inspector.FindElfInApkByOffset(GetTestData(APK_FILE), 0) == nullptr); |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 30 | // Test if we can read the EmbeddedElf using an offset inside its [offset, offset+size] range |
| 31 | // in the apk file. |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 32 | EmbeddedElf* ee = inspector.FindElfInApkByOffset( |
| 33 | GetTestData(APK_FILE), NATIVELIB_OFFSET_IN_APK + NATIVELIB_SIZE_IN_APK / 2); |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 34 | ASSERT_TRUE(ee != nullptr); |
| Yabin Cui | 8f680f6 | 2016-03-18 18:47:43 -0700 | [diff] [blame] | 35 | ASSERT_EQ(NATIVELIB_IN_APK, ee->entry_name()); |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 36 | ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset()); |
| 37 | ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size()); |
| 38 | } |
| 39 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 40 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | b1a885b | 2016-02-14 19:18:02 -0800 | [diff] [blame] | 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 | } |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 49 | |
| Yabin Cui | 648c3e4 | 2024-04-02 13:12:45 -0700 | [diff] [blame] | 50 | // @CddTest = 6.1/C-0-2 |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 51 | TEST(read_apk, ParseExtractedInMemoryPath) { |
| 52 | std::string zip_path; |
| 53 | std::string entry_name; |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 54 | ASSERT_TRUE(ParseExtractedInMemoryPath( |
| 55 | "[anon:dalvik-classes.dex extracted in memory from " |
| Joel Fernandes | 7e8b2b9 | 2018-08-24 12:20:28 -0700 | [diff] [blame] | 56 | "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/" |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 57 | "base.apk]", |
| 58 | &zip_path, &entry_name)); |
| 59 | ASSERT_EQ(zip_path, |
| 60 | "/data/app/com.example.simpleperf.simpleperfexamplepurejava" |
| Joel Fernandes | 7e8b2b9 | 2018-08-24 12:20:28 -0700 | [diff] [blame] | 61 | "-HZK6bPs3Z9SDT3a-tqmasA==/base.apk"); |
| 62 | ASSERT_EQ(entry_name, "classes.dex"); |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 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 " |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 67 | "/data/app/com.example.simpleperf.simpleperfexamplepurejava-HZK6bPs3Z9SDT3a-tqmasA==/base.apk" |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 68 | " (deleted)", |
| 69 | &zip_path, &entry_name)); |
| 70 | ASSERT_EQ(zip_path, |
| 71 | "/data/app/com.example.simpleperf.simpleperfexamplepurejava" |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 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)); |
| Yabin Cui | 16ab77d | 2018-12-13 11:34:17 -0800 | [diff] [blame] | 76 | |
| 77 | // Parse multidex file. |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 78 | ASSERT_TRUE(ParseExtractedInMemoryPath( |
| 79 | "/dev/ashmem/dalvik-classes2.dex extracted in memory from " |
| Yabin Cui | 16ab77d | 2018-12-13 11:34:17 -0800 | [diff] [blame] | 80 | "/data/app/getxml.test.com.testgetxml-knxI11ZXLT-OVBs9X9bSkw==/base.apk!classes2.dex " |
| Thiébaud Weksteen | 4848ee0 | 2020-10-23 16:06:59 +0200 | [diff] [blame] | 81 | "(deleted)", |
| 82 | &zip_path, &entry_name)); |
| Yabin Cui | 16ab77d | 2018-12-13 11:34:17 -0800 | [diff] [blame] | 83 | ASSERT_EQ(zip_path, "/data/app/getxml.test.com.testgetxml-knxI11ZXLT-OVBs9X9bSkw==/base.apk"); |
| 84 | ASSERT_EQ(entry_name, "classes2.dex"); |
| Yabin Cui | 2a53ff3 | 2018-05-21 17:37:00 -0700 | [diff] [blame] | 85 | } |