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 <gtest/gtest.h>
18 
19 #include "get_test_data.h"
20 #include "utils.h"
21 
ModulesMatch(const char * p,const char * q)22 static bool ModulesMatch(const char* p, const char* q) {
23   if (p == nullptr && q == nullptr) {
24     return true;
25   }
26   if (p != nullptr && q != nullptr) {
27     return strcmp(p, q) == 0;
28   }
29   return false;
30 }
31 
KernelSymbolsMatch(const KernelSymbol & sym1,const KernelSymbol & sym2)32 static bool KernelSymbolsMatch(const KernelSymbol& sym1,
33                                const KernelSymbol& sym2) {
34   return sym1.addr == sym2.addr && sym1.type == sym2.type &&
35          strcmp(sym1.name, sym2.name) == 0 &&
36          ModulesMatch(sym1.module, sym2.module);
37 }
38 
TEST(utils,ProcessKernelSymbols)39 TEST(utils, ProcessKernelSymbols) {
40   std::string data =
41       "ffffffffa005c4e4 d __warned.41698   [libsas]\n"
42       "aaaaaaaaaaaaaaaa T _text\n"
43       "cccccccccccccccc c ccccc\n";
44   KernelSymbol expected_symbol;
45   expected_symbol.addr = 0xffffffffa005c4e4ULL;
46   expected_symbol.type = 'd';
47   expected_symbol.name = "__warned.41698";
48   expected_symbol.module = "libsas";
49   ASSERT_TRUE(ProcessKernelSymbols(
50       data,
51       std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
52 
53   expected_symbol.addr = 0xaaaaaaaaaaaaaaaaULL;
54   expected_symbol.type = 'T';
55   expected_symbol.name = "_text";
56   expected_symbol.module = nullptr;
57   ASSERT_TRUE(ProcessKernelSymbols(
58       data,
59       std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
60 
61   expected_symbol.name = "non_existent_symbol";
62   ASSERT_FALSE(ProcessKernelSymbols(
63       data,
64       std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
65 }
66 
TEST(utils,ConvertBytesToValue)67 TEST(utils, ConvertBytesToValue) {
68   char buf[8];
69   for (int i = 0; i < 8; ++i) {
70     buf[i] = i;
71   }
72   ASSERT_EQ(0x1ULL, ConvertBytesToValue(buf + 1, 1));
73   ASSERT_EQ(0x201ULL, ConvertBytesToValue(buf + 1, 2));
74   ASSERT_EQ(0x05040302ULL, ConvertBytesToValue(buf + 2, 4));
75   ASSERT_EQ(0x0706050403020100ULL, ConvertBytesToValue(buf, 8));
76 }
77 
TEST(utils,ArchiveHelper)78 TEST(utils, ArchiveHelper) {
79   std::unique_ptr<ArchiveHelper> ahelper = ArchiveHelper::CreateInstance(GetTestData(APK_FILE));
80   ASSERT_TRUE(ahelper);
81   bool found = false;
82   ZipEntry lib_entry;
83   ASSERT_TRUE(ahelper->IterateEntries([&](ZipEntry& entry, const std::string& name) {
84     if (name == NATIVELIB_IN_APK) {
85       found = true;
86       lib_entry = entry;
87       return false;
88     }
89     return true;
90   }));
91   ASSERT_TRUE(found);
92   ZipEntry entry;
93   ASSERT_TRUE(ahelper->FindEntry(NATIVELIB_IN_APK, &entry));
94   ASSERT_EQ(entry.offset, lib_entry.offset);
95   std::vector<uint8_t> data;
96   ASSERT_TRUE(ahelper->GetEntryData(entry, &data));
97 
98   // Check reading wrong file formats.
99   ASSERT_FALSE(ArchiveHelper::CreateInstance(GetTestData(ELF_FILE)));
100   ASSERT_FALSE(ArchiveHelper::CreateInstance("/dev/zero"));
101 }
102