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