1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <fstream>
17 #include <ios>
18 #include "parameters.h"
19 #include "hitrace_option/hitrace_option.h"
20 #include <gtest/gtest.h>
21
22 using namespace testing::ext;
23 using namespace OHOS::HiviewDFX::Hitrace;
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 namespace HitraceTest {
28
29 #ifdef LOG_DOMAIN
30 #undef LOG_DOMAIN
31 #define LOG_DOMAIN 0xD002D33
32 #endif
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #define LOG_TAG "HitraceOptionTest"
36 #endif
37
38 const std::string TELEMETRY_APP_PARAM = "debug.hitrace.telemetry.app";
39 const std::string SET_EVENT_PID = "/sys/kernel/tracing/set_event_pid";
40 const std::string DEBUG_SET_EVENT_PID = "/sys/kernel/debug/tracing/set_event_pid";
41
42 bool WriteStrToFile(const std::string& filename, const std::string& str);
43
44 class HitraceOptionTest : public testing::Test {
45 public:
46 static void SetUpTestCase();
47 static void TearDownTestCase();
48 void SetUp();
49 void TearDown();
50 };
51
SetUpTestCase()52 void HitraceOptionTest::SetUpTestCase() { }
53
TearDownTestCase()54 void HitraceOptionTest::TearDownTestCase() { }
55
SetUp()56 void HitraceOptionTest::SetUp()
57 {
58 WriteStrToFile(SET_EVENT_PID, "");
59 WriteStrToFile(DEBUG_SET_EVENT_PID, "");
60 }
61
TearDown()62 void HitraceOptionTest::TearDown()
63 {
64 WriteStrToFile(SET_EVENT_PID, "");
65 WriteStrToFile(DEBUG_SET_EVENT_PID, "");
66 }
67
WriteStrToFile(const std::string & filename,const std::string & str)68 bool WriteStrToFile(const std::string& filename, const std::string& str)
69 {
70 std::ofstream out;
71 out.open(filename, std::ios::out);
72 if (out.fail()) {
73 return false;
74 }
75 out << str;
76 if (out.bad()) {
77 out.close();
78 return false;
79 }
80 out.flush();
81 out.close();
82 return true;
83 }
84
ReadFile(const std::string & filename)85 std::string ReadFile(const std::string& filename)
86 {
87 std::ifstream fileIn(filename.c_str());
88 if (!fileIn.is_open()) {
89 return "";
90 }
91 std::string str((std::istreambuf_iterator<char>(fileIn)), std::istreambuf_iterator<char>());
92 fileIn.close();
93 return str;
94 }
95
ContainsPid(const std::string & filename,pid_t pid)96 bool ContainsPid(const std::string& filename, pid_t pid)
97 {
98 std::string pidStr = std::to_string(pid);
99 std::ifstream file(filename);
100 if (!file.is_open()) {
101 return false;
102 }
103
104 std::string line;
105 bool find = false;
106 while (std::getline(file, line)) {
107 if (line == pidStr) {
108 find = true;
109 break;
110 }
111 }
112
113 file.close();
114 return find;
115 }
116
117 HWTEST_F(HitraceOptionTest, SetTelemetryAppNameTest_001, TestSize.Level1)
118 {
119 ASSERT_TRUE(OHOS::system::SetParameter(TELEMETRY_APP_PARAM, "a"));
120 ASSERT_EQ(OHOS::system::GetParameter(TELEMETRY_APP_PARAM, ""), "a");
121
122 EXPECT_EQ(SetFilterAppName("com.test.app"), HITRACE_NO_ERROR);
123 EXPECT_EQ(OHOS::system::GetParameter(TELEMETRY_APP_PARAM, ""), "com.test.app");
124 }
125
126 HWTEST_F(HitraceOptionTest, AddFilterPid_001, TestSize.Level1)
127 {
128 WriteStrToFile(SET_EVENT_PID, "");
129 ASSERT_EQ(ReadFile(SET_EVENT_PID), "");
130 ASSERT_EQ(ReadFile(DEBUG_SET_EVENT_PID), "");
131
132 EXPECT_EQ(AddFilterPid(1), HITRACE_NO_ERROR);
133 EXPECT_TRUE(ContainsPid(SET_EVENT_PID, 1));
134 EXPECT_TRUE(ContainsPid(DEBUG_SET_EVENT_PID, 1));
135
136 pid_t pid = getpid();
137 EXPECT_EQ(AddFilterPid(pid), HITRACE_NO_ERROR);
138 EXPECT_TRUE(ContainsPid(SET_EVENT_PID, 1));
139 EXPECT_TRUE(ContainsPid(DEBUG_SET_EVENT_PID, 1));
140 EXPECT_TRUE(ContainsPid(SET_EVENT_PID, pid));
141 EXPECT_TRUE(ContainsPid(DEBUG_SET_EVENT_PID, pid));
142 }
143
144 HWTEST_F(HitraceOptionTest, ClearFilterPid_001, TestSize.Level1)
145 {
146 WriteStrToFile(SET_EVENT_PID, "1");
147 ASSERT_EQ(ReadFile(SET_EVENT_PID), "1\n");
148 ASSERT_EQ(ReadFile(DEBUG_SET_EVENT_PID), "1\n");
149
150 EXPECT_EQ(ClearFilterPid(), HITRACE_NO_ERROR);
151 EXPECT_EQ(ReadFile(SET_EVENT_PID), "");
152 EXPECT_EQ(ReadFile(DEBUG_SET_EVENT_PID), "");
153 }
154
155 HWTEST_F(HitraceOptionTest, FilterAppTrace_001, TestSize.Level1)
156 {
157 ASSERT_TRUE(OHOS::system::SetParameter(TELEMETRY_APP_PARAM, ""));
158 ASSERT_EQ(OHOS::system::GetParameter(TELEMETRY_APP_PARAM, "null"), "");
159 WriteStrToFile(SET_EVENT_PID, "");
160 ASSERT_EQ(ReadFile(SET_EVENT_PID), "");
161 ASSERT_EQ(ReadFile(DEBUG_SET_EVENT_PID), "");
162
163 FilterAppTrace("com.test.app", 1);
164 EXPECT_EQ(ReadFile(SET_EVENT_PID), "");
165
166 ASSERT_TRUE(OHOS::system::SetParameter(TELEMETRY_APP_PARAM, "com.test.app"));
167 ASSERT_EQ(OHOS::system::GetParameter(TELEMETRY_APP_PARAM, ""), "com.test.app");
168 FilterAppTrace("com.test.app", 1);
169 EXPECT_TRUE(ContainsPid(SET_EVENT_PID, 1));
170 EXPECT_TRUE(ContainsPid(DEBUG_SET_EVENT_PID, 1));
171 }
172
173 } // namespace HitraceTest
174 } // namespace HiviewDFX
175 } // namespace OHOS
176