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 <gtest/gtest.h>
     18 
     19 #include "utils.h"
     20 
     21 static bool ModulesMatch(const char* p, const char* q) {
     22   if (p == nullptr && q == nullptr) {
     23     return true;
     24   }
     25   if (p != nullptr && q != nullptr) {
     26     return strcmp(p, q) == 0;
     27   }
     28   return false;
     29 }
     30 
     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);
     36 }
     37 
     38 TEST(utils, ProcessKernelSymbols) {
     39   std::string data =
     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(
     49       data,
     50       std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
     51 
     52   expected_symbol.addr = 0xaaaaaaaaaaaaaaaaULL;
     53   expected_symbol.type = 'T';
     54   expected_symbol.name = "_text";
     55   expected_symbol.module = nullptr;
     56   ASSERT_TRUE(ProcessKernelSymbols(
     57       data,
     58       std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
     59 
     60   expected_symbol.name = "non_existent_symbol";
     61   ASSERT_FALSE(ProcessKernelSymbols(
     62       data,
     63       std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol)));
     64 }
     65 
     66 TEST(utils, ConvertBytesToValue) {
     67   char buf[8];
     68   for (int i = 0; i < 8; ++i) {
     69     buf[i] = i;
     70   }
     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));
     75 }
     76