2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
21 static bool ModulesMatch(const char* p, const char* q) {
22 if (p == nullptr && q == nullptr) {
25 if (p != nullptr && q != nullptr) {
26 return strcmp(p, q) == 0;
31 static bool KernelSymbolsMatch(const KernelSymbol& sym1,
32 const KernelSymbol& sym2) {
33 return sym1.addr == sym2.addr && sym1.type == sym2.type &&
34 strcmp(sym1.name, sym2.name) == 0 &&
35 ModulesMatch(sym1.module, sym2.module);
38 TEST(utils, ProcessKernelSymbols) {
40 "ffffffffa005c4e4 d __warned.41698 [libsas]\n"
41 "aaaaaaaaaaaaaaaa T _text\n"
42 "cccccccccccccccc c ccccc\n";
43 KernelSymbol expected_symbol;
44 expected_symbol.addr = 0xffffffffa005c4e4ULL;
45 expected_symbol.type = 'd';
46 expected_symbol.name = "__warned.41698";
47 expected_symbol.module = "libsas";
48 ASSERT_TRUE(ProcessKernelSymbols(
50 std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
52 expected_symbol.addr = 0xaaaaaaaaaaaaaaaaULL;
53 expected_symbol.type = 'T';
54 expected_symbol.name = "_text";
55 expected_symbol.module = nullptr;
56 ASSERT_TRUE(ProcessKernelSymbols(
58 std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
60 expected_symbol.name = "non_existent_symbol";
61 ASSERT_FALSE(ProcessKernelSymbols(
63 std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
66 TEST(utils, ConvertBytesToValue) {
68 for (int i = 0; i < 8; ++i) {
71 ASSERT_EQ(0x1ULL, ConvertBytesToValue(buf + 1, 1));
72 ASSERT_EQ(0x201ULL, ConvertBytesToValue(buf + 1, 2));
73 ASSERT_EQ(0x05040302ULL, ConvertBytesToValue(buf + 2, 4));
74 ASSERT_EQ(0x0706050403020100ULL, ConvertBytesToValue(buf, 8));