1 /* 2 * Copyright (C) 2015 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 <functional> 20 #include <android-base/file.h> 21 #include <android-base/test_utils.h> 22 23 #include "environment.h" 24 25 TEST(environment, GetCpusFromString) { 26 ASSERT_EQ(GetCpusFromString(""), std::vector<int>()); 27 ASSERT_EQ(GetCpusFromString("0-2"), std::vector<int>({0, 1, 2})); 28 ASSERT_EQ(GetCpusFromString("0,2-3"), std::vector<int>({0, 2, 3})); 29 ASSERT_EQ(GetCpusFromString("1,0-3,3,4"), std::vector<int>({0, 1, 2, 3, 4})); 30 } 31 32 static bool ModulesMatch(const char* p, const char* q) { 33 if (p == nullptr && q == nullptr) { 34 return true; 35 } 36 if (p != nullptr && q != nullptr) { 37 return strcmp(p, q) == 0; 38 } 39 return false; 40 } 41 42 static bool KernelSymbolsMatch(const KernelSymbol& sym1, const KernelSymbol& sym2) { 43 return sym1.addr == sym2.addr && 44 sym1.type == sym2.type && 45 strcmp(sym1.name, sym2.name) == 0 && 46 ModulesMatch(sym1.module, sym2.module); 47 } 48 49 TEST(environment, ProcessKernelSymbols) { 50 std::string data = 51 "ffffffffa005c4e4 d __warned.41698 [libsas]\n" 52 "aaaaaaaaaaaaaaaa T _text\n" 53 "cccccccccccccccc c ccccc\n"; 54 TemporaryFile tempfile; 55 ASSERT_TRUE(android::base::WriteStringToFile(data, tempfile.path)); 56 KernelSymbol expected_symbol; 57 expected_symbol.addr = 0xffffffffa005c4e4ULL; 58 expected_symbol.type = 'd'; 59 expected_symbol.name = "__warned.41698"; 60 expected_symbol.module = "libsas"; 61 ASSERT_TRUE(ProcessKernelSymbols( 62 tempfile.path, std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol))); 63 64 expected_symbol.addr = 0xaaaaaaaaaaaaaaaaULL; 65 expected_symbol.type = 'T'; 66 expected_symbol.name = "_text"; 67 expected_symbol.module = nullptr; 68 ASSERT_TRUE(ProcessKernelSymbols( 69 tempfile.path, std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol))); 70 71 expected_symbol.name = "non_existent_symbol"; 72 ASSERT_FALSE(ProcessKernelSymbols( 73 tempfile.path, std::bind(&KernelSymbolsMatch, std::placeholders::_1, expected_symbol))); 74 } 75