blob: 5772e4727cc6267235562f8c7679775bab8e9de7 [file] [log] [blame]
Yabin Cui323e9452015-04-20 18:07:17 -07001/*
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
Yabin Cuiea105c82020-07-22 13:01:31 -070019#include <filesystem>
20
Yabin Cui63a1c3d2017-05-19 12:57:44 -070021#include <android-base/file.h>
22
23#include "dso.h"
Yabin Cui9759e1b2015-04-28 15:54:13 -070024#include "environment.h"
Yabin Cuiea105c82020-07-22 13:01:31 -070025#include "get_test_data.h"
Yabin Cuic13ff892020-11-10 13:11:01 -080026#include "test_util.h"
27#include "thread_tree.h"
Yabin Cuiea105c82020-07-22 13:01:31 -070028
29namespace fs = std::filesystem;
Yabin Cuifaa7b922021-01-11 17:35:57 -080030using namespace simpleperf;
Yabin Cui323e9452015-04-20 18:07:17 -070031
Yabin Cui648c3e42024-04-02 13:12:45 -070032// @CddTest = 6.1/C-0-2
Yabin Cui63a1c3d2017-05-19 12:57:44 -070033TEST(environment, PrepareVdsoFile) {
34 std::string content;
35 ASSERT_TRUE(android::base::ReadFileToString("/proc/self/maps", &content));
36 if (content.find("[vdso]") == std::string::npos) {
37 // Vdso isn't used, no need to test.
38 return;
39 }
Yabin Cui47ba6562018-01-08 11:48:12 -080040 TemporaryDir tmpdir;
Yabin Cui554f3bb2021-05-05 13:34:59 -070041 auto scoped_temp_files = ScopedTempFiles::Create(tmpdir.path);
42 ASSERT_TRUE(scoped_temp_files);
Yabin Cui63a1c3d2017-05-19 12:57:44 -070043 PrepareVdsoFile();
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020044 std::unique_ptr<Dso> dso =
45 Dso::CreateDso(DSO_ELF_FILE, "[vdso]", sizeof(size_t) == sizeof(uint64_t));
Yabin Cui63a1c3d2017-05-19 12:57:44 -070046 ASSERT_TRUE(dso != nullptr);
Yabin Cui47ba6562018-01-08 11:48:12 -080047 ASSERT_NE(dso->GetDebugFilePath(), "[vdso]");
Yabin Cui63a1c3d2017-05-19 12:57:44 -070048}
Yabin Cui8faa6152018-06-07 15:52:57 -070049
Yabin Cui648c3e42024-04-02 13:12:45 -070050// @CddTest = 6.1/C-0-2
Yabin Cui8faa6152018-06-07 15:52:57 -070051TEST(environment, GetHardwareFromCpuInfo) {
ThiƩbaud Weksteen4848ee02020-10-23 16:06:59 +020052 std::string cpu_info =
53 "CPU revision : 10\n\n"
Yabin Cui8faa6152018-06-07 15:52:57 -070054 "Hardware : Symbol i.MX6 Freeport_Plat Quad/DualLite (Device Tree)\n";
55 ASSERT_EQ("Symbol i.MX6 Freeport_Plat Quad/DualLite (Device Tree)",
56 GetHardwareFromCpuInfo(cpu_info));
57}
Yabin Cui2db05b42018-07-16 14:04:49 -070058
Yabin Cui648c3e42024-04-02 13:12:45 -070059// @CddTest = 6.1/C-0-2
Yabin Cui2db05b42018-07-16 14:04:49 -070060TEST(environment, MappedFileOnlyExistInMemory) {
61 ASSERT_TRUE(MappedFileOnlyExistInMemory(""));
62 ASSERT_TRUE(MappedFileOnlyExistInMemory("[stack]"));
63 ASSERT_TRUE(MappedFileOnlyExistInMemory("[anon:.bss]"));
64 ASSERT_FALSE(MappedFileOnlyExistInMemory("[vdso]"));
65 ASSERT_TRUE(MappedFileOnlyExistInMemory("/dev/__properties__/u:object_r"));
66 ASSERT_TRUE(MappedFileOnlyExistInMemory("//anon"));
Yabin Cui739b1542018-10-17 17:19:56 -070067 ASSERT_TRUE(MappedFileOnlyExistInMemory("/memfd:/jit-cache"));
Yabin Cui2db05b42018-07-16 14:04:49 -070068 ASSERT_FALSE(MappedFileOnlyExistInMemory("./TemporaryFile-12345"));
69 ASSERT_FALSE(MappedFileOnlyExistInMemory("/system/lib64/libc.so"));
70}
Yabin Cui6e173a42018-08-13 15:58:25 -070071
Yabin Cui648c3e42024-04-02 13:12:45 -070072// @CddTest = 6.1/C-0-2
Yabin Cui6e173a42018-08-13 15:58:25 -070073TEST(environment, SetPerfEventLimits) {
74#if defined(__ANDROID__)
75 if (GetAndroidVersion() <= kAndroidVersionP) {
76 return;
77 }
78 uint64_t orig_freq = 100000;
79 size_t orig_percent = 25;
80 uint64_t orig_mlock_kb = 516;
81 bool has_freq = GetMaxSampleFrequency(&orig_freq);
82 bool has_percent = GetCpuTimeMaxPercent(&orig_percent);
83 bool has_mlock_kb = GetPerfEventMlockKb(&orig_mlock_kb);
84
85 ASSERT_TRUE(SetPerfEventLimits(orig_freq + 1, orig_percent + 1, orig_mlock_kb + 1));
86 if (has_freq) {
87 uint64_t value;
88 ASSERT_TRUE(GetMaxSampleFrequency(&value));
89 ASSERT_EQ(value, orig_freq + 1);
90 }
91 if (has_percent) {
92 size_t value;
93 ASSERT_TRUE(GetCpuTimeMaxPercent(&value));
94 ASSERT_EQ(value, orig_percent + 1);
95 }
96 if (has_mlock_kb) {
97 uint64_t value;
98 ASSERT_TRUE(GetPerfEventMlockKb(&value));
99 ASSERT_EQ(value, orig_mlock_kb + 1);
100 }
101 // Restore the environment.
102 ASSERT_TRUE(SetPerfEventLimits(orig_freq, orig_percent, orig_mlock_kb));
103#else // !defined(__ANDROID__)
104 GTEST_LOG_(INFO) << "This test tests setting properties on Android.";
105#endif
106}
Yabin Cui84c55ff2020-07-09 14:14:22 -0700107
Yabin Cui648c3e42024-04-02 13:12:45 -0700108// @CddTest = 6.1/C-0-2
Yabin Cui84c55ff2020-07-09 14:14:22 -0700109TEST(environment, GetKernelVersion) {
Yabin Cuid703bb32021-01-05 16:45:52 -0800110 ASSERT_TRUE(GetKernelVersion());
Yabin Cui84c55ff2020-07-09 14:14:22 -0700111}
Yabin Cuiea105c82020-07-22 13:01:31 -0700112
Yabin Cui648c3e42024-04-02 13:12:45 -0700113// @CddTest = 6.1/C-0-2
Yabin Cuiea105c82020-07-22 13:01:31 -0700114TEST(environment, GetModuleBuildId) {
115 BuildId build_id;
116 fs::path dir(GetTestData("sysfs/module/fake_kernel_module/notes"));
117 ASSERT_TRUE(fs::copy_file(dir / "note.gnu.build-id", dir / ".note.gnu.build-id",
118 fs::copy_options::overwrite_existing));
119 ASSERT_TRUE(GetModuleBuildId("fake_kernel_module", &build_id, GetTestData("sysfs")));
120 ASSERT_EQ(build_id, BuildId("3e0ba155286f3454"));
121}
Yabin Cuic13ff892020-11-10 13:11:01 -0800122
Yabin Cui648c3e42024-04-02 13:12:45 -0700123// @CddTest = 6.1/C-0-2
Yabin Cuic13ff892020-11-10 13:11:01 -0800124TEST(environment, GetKernelAndModuleMmaps) {
125 TEST_REQUIRE_ROOT();
126 KernelMmap kernel_mmap;
127 std::vector<KernelMmap> module_mmaps;
128 GetKernelAndModuleMmaps(&kernel_mmap, &module_mmaps);
129 // The kernel map should contain the kernel start address.
130 ASSERT_EQ(kernel_mmap.name, std::string(DEFAULT_KERNEL_MMAP_NAME) + "_stext");
131 ASSERT_GT(kernel_mmap.start_addr, 0);
132}
Yabin Cuia89a3742021-02-11 13:14:54 -0800133
Yabin Cui648c3e42024-04-02 13:12:45 -0700134// @CddTest = 6.1/C-0-2
Yabin Cuia89a3742021-02-11 13:14:54 -0800135TEST(environment, GetProcessUid) {
136 std::optional<uid_t> uid = GetProcessUid(getpid());
137 ASSERT_TRUE(uid.has_value());
138 ASSERT_EQ(uid.value(), getuid());
139}
Yabin Cuidb19d6d2021-05-12 17:36:02 -0700140
Yabin Cui648c3e42024-04-02 13:12:45 -0700141// @CddTest = 6.1/C-0-2
Yabin Cuidb19d6d2021-05-12 17:36:02 -0700142TEST(environment, GetAppType) {
143 TEST_REQUIRE_APPS();
144 ASSERT_EQ(GetAppType("com.android.simpleperf.debuggable"), "debuggable");
145 ASSERT_EQ(GetAppType("com.android.simpleperf.profileable"), "profileable");
146 ASSERT_EQ(GetAppType("com.android.simpleperf.app_not_exist"), "not_exist");
147}
Yabin Cuicfdc7832023-03-02 15:35:01 -0800148
Yabin Cui648c3e42024-04-02 13:12:45 -0700149// @CddTest = 6.1/C-0-2
Yabin Cuicfdc7832023-03-02 15:35:01 -0800150TEST(environment, GetMemorySize) {
151 auto value = GetMemorySize();
Yi Kongd5f247b2024-07-08 16:59:51 +0900152 ASSERT_GT(value, 0);
Yabin Cuicfdc7832023-03-02 15:35:01 -0800153}
Yabin Cui433d6f52024-01-09 15:29:09 -0800154
Yabin Cui648c3e42024-04-02 13:12:45 -0700155// @CddTest = 6.1/C-0-2
Yabin Cui433d6f52024-01-09 15:29:09 -0800156TEST(environment, GetARMCpuModels) {
157#if defined(__aarch64__) && defined(__ANDROID__)
158 auto models = GetARMCpuModels();
159 ASSERT_FALSE(models.empty());
160 ASSERT_FALSE(models[0].cpus.empty());
161 ASSERT_EQ(models[0].cpus[0], 0);
162#endif // defined(__aarch64__) && defined(__ANDROID__)
163}