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 <base/file.h>
21
22#include "environment.h"
23
24TEST(environment, GetOnlineCpusFromString) {
25  ASSERT_EQ(GetOnlineCpusFromString(""), std::vector<int>());
26  ASSERT_EQ(GetOnlineCpusFromString("0-2"), std::vector<int>({0, 1, 2}));
27  ASSERT_EQ(GetOnlineCpusFromString("0,2-3"), std::vector<int>({0, 2, 3}));
28}
29
30static bool FindKernelSymbol(const KernelSymbol& sym1, const KernelSymbol& sym2) {
31  return sym1.addr == sym2.addr && sym1.type == sym2.type && strcmp(sym1.name, sym2.name) == 0 &&
32         ((sym1.module == nullptr && sym2.module == nullptr) ||
33          (strcmp(sym1.module, sym2.module) == 0));
34}
35
36TEST(environment, ProcessKernelSymbols) {
37  std::string data =
38      "ffffffffa005c4e4 d __warned.41698   [libsas]\n"
39      "aaaaaaaaaaaaaaaa T _text\n"
40      "cccccccccccccccc c ccccc\n";
41  const char* tempfile = "tempfile_process_kernel_symbols";
42  ASSERT_TRUE(android::base::WriteStringToFile(data, tempfile));
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      tempfile, std::bind(&FindKernelSymbol, std::placeholders::_1, expected_symbol)));
50
51  expected_symbol.addr = 0xaaaaaaaaaaaaaaaaULL;
52  expected_symbol.type = 'T';
53  expected_symbol.name = "_text";
54  expected_symbol.module = nullptr;
55  ASSERT_TRUE(ProcessKernelSymbols(
56      tempfile, std::bind(&FindKernelSymbol, std::placeholders::_1, expected_symbol)));
57
58  expected_symbol.name = "non_existent_symbol";
59  ASSERT_FALSE(ProcessKernelSymbols(
60      tempfile, std::bind(&FindKernelSymbol, std::placeholders::_1, expected_symbol)));
61  ASSERT_EQ(0, unlink(tempfile));
62}
63