Home | History | Annotate | Download | only in simpleperf
      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 
     24 TEST(read_apk, IsValidApkPath) {
     25   ASSERT_FALSE(IsValidApkPath("/dev/zero"));
     26   ASSERT_FALSE(IsValidApkPath(GetTestData(ELF_FILE)));
     27   ASSERT_TRUE(IsValidApkPath(GetTestData(APK_FILE)));
     28 }
     29 
     30 TEST(read_apk, FindElfInApkByOffset) {
     31   ApkInspector inspector;
     32   ASSERT_TRUE(inspector.FindElfInApkByOffset("/dev/null", 0) == nullptr);
     33   ASSERT_TRUE(inspector.FindElfInApkByOffset(GetTestData(APK_FILE), 0) == nullptr);
     34   // Test if we can read the EmbeddedElf using an offset inside its [offset, offset+size] range
     35   // in the apk file.
     36   EmbeddedElf* ee = inspector.FindElfInApkByOffset(GetTestData(APK_FILE),
     37                                                    NATIVELIB_OFFSET_IN_APK + NATIVELIB_SIZE_IN_APK / 2);
     38   ASSERT_TRUE(ee != nullptr);
     39   ASSERT_EQ(NATIVELIB_IN_APK, ee->entry_name());
     40   ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset());
     41   ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size());
     42 }
     43 
     44 TEST(read_apk, FindElfInApkByName) {
     45   ASSERT_TRUE(ApkInspector::FindElfInApkByName("/dev/null", "") == nullptr);
     46   ASSERT_TRUE(ApkInspector::FindElfInApkByName(GetTestData(APK_FILE), "") == nullptr);
     47   auto ee = ApkInspector::FindElfInApkByName(GetTestData(APK_FILE), NATIVELIB_IN_APK);
     48   ASSERT_TRUE(ee != nullptr);
     49   ASSERT_EQ(NATIVELIB_OFFSET_IN_APK, ee->entry_offset());
     50   ASSERT_EQ(NATIVELIB_SIZE_IN_APK, ee->entry_size());
     51 }
     52 
     53 TEST(read_apk, GetBuildIdFromApkFile) {
     54   BuildId build_id;
     55   ASSERT_EQ(ElfStatus::NO_ERROR, GetBuildIdFromApkFile(GetTestData(APK_FILE), NATIVELIB_IN_APK, &build_id));
     56   ASSERT_EQ(build_id, native_lib_build_id);
     57 }
     58 
     59 TEST(read_apk, ParseSymbolsFromApkFile) {
     60   std::map<std::string, ElfFileSymbol> symbols;
     61   ASSERT_EQ(ElfStatus::NO_SYMBOL_TABLE,
     62             ParseSymbolsFromApkFile(GetTestData(APK_FILE), NATIVELIB_IN_APK, native_lib_build_id,
     63                                     std::bind(ParseSymbol, std::placeholders::_1, &symbols)));
     64   CheckElfFileSymbols(symbols);
     65 }
     66