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 <android-base/file.h>
20 
21 #include "dso.h"
22 #include "environment.h"
23 
TEST(environment,GetCpusFromString)24 TEST(environment, GetCpusFromString) {
25   ASSERT_EQ(GetCpusFromString(""), std::vector<int>());
26   ASSERT_EQ(GetCpusFromString("0-2"), std::vector<int>({0, 1, 2}));
27   ASSERT_EQ(GetCpusFromString("0,2-3"), std::vector<int>({0, 2, 3}));
28   ASSERT_EQ(GetCpusFromString("1,0-3,3,4"), std::vector<int>({0, 1, 2, 3, 4}));
29 }
30 
TEST(environment,PrepareVdsoFile)31 TEST(environment, PrepareVdsoFile) {
32   std::string content;
33   ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &content));
34   if (content.find("[vdso]") == std::string::npos) {
35     // Vdso isn't used, no need to test.
36     return;
37   }
38   TemporaryDir tmpdir;
39   ScopedTempFiles scoped_temp_files(tmpdir.path);
40   PrepareVdsoFile();
41   std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_ELF_FILE, "[vdso]",
42                                             sizeof(size_t) == sizeof(uint64_t));
43   ASSERT_TRUE(dso != nullptr);
44   ASSERT_NE(dso->GetDebugFilePath(), "[vdso]");
45 }
46 
TEST(environment,GetHardwareFromCpuInfo)47 TEST(environment, GetHardwareFromCpuInfo) {
48   std::string cpu_info = "CPU revision : 10\n\n"
49       "Hardware : Symbol i.MX6 Freeport_Plat Quad/DualLite (Device Tree)\n";
50   ASSERT_EQ("Symbol i.MX6 Freeport_Plat Quad/DualLite (Device Tree)",
51             GetHardwareFromCpuInfo(cpu_info));
52 }
53 
TEST(environment,MappedFileOnlyExistInMemory)54 TEST(environment, MappedFileOnlyExistInMemory) {
55   ASSERT_TRUE(MappedFileOnlyExistInMemory(""));
56   ASSERT_TRUE(MappedFileOnlyExistInMemory("[stack]"));
57   ASSERT_TRUE(MappedFileOnlyExistInMemory("[anon:.bss]"));
58   ASSERT_FALSE(MappedFileOnlyExistInMemory("[vdso]"));
59   ASSERT_TRUE(MappedFileOnlyExistInMemory("/dev/__properties__/u:object_r"));
60   ASSERT_TRUE(MappedFileOnlyExistInMemory("//anon"));
61   ASSERT_TRUE(MappedFileOnlyExistInMemory("/memfd:/jit-cache"));
62   ASSERT_FALSE(MappedFileOnlyExistInMemory("./TemporaryFile-12345"));
63   ASSERT_FALSE(MappedFileOnlyExistInMemory("/system/lib64/libc.so"));
64 }
65 
TEST(environment,SetPerfEventLimits)66 TEST(environment, SetPerfEventLimits) {
67 #if defined(__ANDROID__)
68   if (GetAndroidVersion() <= kAndroidVersionP) {
69     return;
70   }
71   uint64_t orig_freq = 100000;
72   size_t orig_percent = 25;
73   uint64_t orig_mlock_kb = 516;
74   bool has_freq = GetMaxSampleFrequency(&orig_freq);
75   bool has_percent = GetCpuTimeMaxPercent(&orig_percent);
76   bool has_mlock_kb = GetPerfEventMlockKb(&orig_mlock_kb);
77 
78   ASSERT_TRUE(SetPerfEventLimits(orig_freq + 1, orig_percent + 1, orig_mlock_kb + 1));
79   if (has_freq) {
80     uint64_t value;
81     ASSERT_TRUE(GetMaxSampleFrequency(&value));
82     ASSERT_EQ(value, orig_freq + 1);
83   }
84   if (has_percent) {
85     size_t value;
86     ASSERT_TRUE(GetCpuTimeMaxPercent(&value));
87     ASSERT_EQ(value, orig_percent + 1);
88   }
89   if (has_mlock_kb) {
90     uint64_t value;
91     ASSERT_TRUE(GetPerfEventMlockKb(&value));
92     ASSERT_EQ(value, orig_mlock_kb + 1);
93   }
94   // Restore the environment.
95   ASSERT_TRUE(SetPerfEventLimits(orig_freq, orig_percent, orig_mlock_kb));
96 #else  // !defined(__ANDROID__)
97   GTEST_LOG_(INFO) << "This test tests setting properties on Android.";
98 #endif
99 }
100