1 /*
2 * Copyright (c) 2023 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 #define private public
17 #define protected public
18
19 #include <gtest/gtest.h>
20 #include <gtest/hwext/gtest-multithread.h>
21 #include "socperf_ipc_interface_code.h"
22 #include "socperf_server.h"
23 #include "socperf_stub.h"
24 #include "socperf.h"
25
26 using namespace testing::ext;
27 using namespace testing::mt;
28
29 namespace OHOS {
30 namespace SOCPERF {
31 class SocPerfServerTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 private:
38 std::shared_ptr<SocPerfServer> socPerfServer_;
39 SocPerf socPerf_;
40 };
41
SetUpTestCase(void)42 void SocPerfServerTest::SetUpTestCase(void)
43 {
44 }
45
TearDownTestCase(void)46 void SocPerfServerTest::TearDownTestCase(void)
47 {
48 }
49
SetUp(void)50 void SocPerfServerTest::SetUp(void)
51 {
52 socPerfServer_ = DelayedSingleton<SocPerfServer>::GetInstance();
53 socPerfServer_->OnStart();
54
55 socPerf_.Init();
56 }
57
TearDown(void)58 void SocPerfServerTest::TearDown(void)
59 {
60 socPerfServer_ = nullptr;
61 }
62
63 /*
64 * @tc.name: SocPerfServerTest_SocPerfAPI_001
65 * @tc.desc: test socperf api
66 * @tc.type FUNC
67 * @tc.require: issueI78T3V
68 */
69 HWTEST_F(SocPerfServerTest, SocPerfServerTest_SocPerfAPI_001, Function | MediumTest | Level0)
70 {
71 std::string msg = "testBoost";
72 socPerf_.PerfRequest(10000, msg);
73 socPerf_.PerfRequestEx(10000, true, msg);
74 socPerf_.PerfRequestEx(10000, false, msg);
75 socPerf_.PerfRequestEx(10028, true, msg);
76 socPerf_.PerfRequestEx(10028, false, msg);
77 socPerf_.LimitRequest(ActionType::ACTION_TYPE_POWER, {1001}, {999000}, msg);
78 socPerf_.LimitRequest(ActionType::ACTION_TYPE_THERMAL, {1001}, {999000}, msg);
79 socPerf_.LimitRequest(ActionType::ACTION_TYPE_POWER, {1001}, {1325000}, msg);
80 socPerf_.LimitRequest(ActionType::ACTION_TYPE_THERMAL, {1001}, {1325000}, msg);
81 socPerf_.PowerLimitBoost(true, msg);
82 socPerf_.ThermalLimitBoost(true, msg);
83 EXPECT_EQ(msg, "testBoost");
84 std::string id = "1000";
85 std::string name = "lit_cpu_freq";
86 std::string pair = "1001";
87 std::string mode = "1";
88 std::string persisMode = "1";
89 std::string configFile = "";
90 bool ret = socPerf_.CheckResourceTag(id.c_str(), name.c_str(), pair.c_str(), mode.c_str(), persisMode.c_str(),
91 configFile.c_str());
92 EXPECT_TRUE(ret);
93 ret = socPerf_.CheckResourceTag(nullptr, name.c_str(), pair.c_str(), mode.c_str(), persisMode.c_str(),
94 configFile.c_str());
95 EXPECT_FALSE(ret);
96 }
97
98 /*
99 * @tc.name: SocPerfServerTest_SocPerfServerAPI_000
100 * @tc.desc: test socperf server api
101 * @tc.type FUNC
102 * @tc.require: issueI78T3V
103 */
104 HWTEST_F(SocPerfServerTest, SocPerfServerTest_SocPerfServerAPI_000, Function | MediumTest | Level0)
105 {
106 std::string msg = "testBoost";
107 socPerfServer_->PerfRequest(10000, msg);
108 socPerfServer_->PerfRequestEx(10000, true, msg);
109 socPerfServer_->PerfRequestEx(10000, false, msg);
110 socPerfServer_->LimitRequest(ActionType::ACTION_TYPE_POWER, {1001}, {1364000}, msg);
111 socPerfServer_->LimitRequest(ActionType::ACTION_TYPE_POWER, {11001}, {2}, msg);
112 socPerfServer_->LimitRequest(ActionType::ACTION_TYPE_MAX, {11001}, {2}, msg);
113 socPerfServer_->PowerLimitBoost(true, msg);
114 socPerfServer_->LimitRequest(ActionType::ACTION_TYPE_THERMAL, {1001}, {1364000}, msg);
115 socPerfServer_->ThermalLimitBoost(true, msg);
116 socPerfServer_->PowerLimitBoost(false, msg);
117 socPerfServer_->ThermalLimitBoost(false, msg);
118 bool allowDump = socPerfServer_->AllowDump();
119 EXPECT_TRUE(allowDump);
120 int32_t fd = -1;
121 std::vector<std::u16string> args = {to_utf16("1"), to_utf16("2"), to_utf16("3"), to_utf16("-a"), to_utf16("-h")};
122 socPerfServer_->Dump(fd, args);
123 socPerfServer_->OnStop();
124 EXPECT_EQ(msg, "testBoost");
125 }
126
127 /*
128 * @tc.name: SocPerfServerTest_SocperfThreadWrapp_001
129 * @tc.desc: test log switch func
130 * @tc.type FUNC
131 * @tc.require: issueI78T3V
132 */
133 HWTEST_F(SocPerfServerTest, SocPerfServerTest_SocperfThreadWrapp_001, Function | MediumTest | Level0)
134 {
135 std::string msg = "";
136 #ifdef SOCPERF_ADAPTOR_FFRT
137 auto socPerfThreadWrap = std::make_shared<SocPerfThreadWrap>();
138 #else
139 auto runner = AppExecFwk::EventRunner::Create("socperf#");
140 auto socPerfThreadWrap = std::make_shared<SocPerfThreadWrap>(runner);
141 #endif
142 socPerfThreadWrap->PostDelayTask(1000, nullptr);
143 socPerfThreadWrap->InitResNodeInfo(nullptr);
144 socPerfThreadWrap->InitPerfFunc(nullptr, nullptr);
145 socPerfThreadWrap->InitPerfFunc(nullptr, msg.c_str());
146 socPerfThreadWrap->InitPerfFunc(msg.c_str(), nullptr);
147 socPerfThreadWrap->InitPerfFunc(msg.c_str(), msg.c_str());
148 socPerfThreadWrap->InitGovResNodeInfo(nullptr);
149 socPerfThreadWrap->DoFreqActionPack(nullptr);
150 socPerfThreadWrap->UpdateLimitStatus(0, nullptr, 0);
151 socPerfThreadWrap->DoFreqAction(0, nullptr);
152 socPerfThreadWrap->DoFreqAction(1000, nullptr);
153 EXPECT_NE(msg.c_str(), "-1");
154 bool ret = false;
155 int inValidResId = 9999;
156 ret = socPerfThreadWrap->IsResId(inValidResId);
157 EXPECT_FALSE(ret);
158 ret = socPerfThreadWrap->IsValidResId(inValidResId);
159 EXPECT_FALSE(ret);
160 ret = socPerfThreadWrap->IsGovResId(inValidResId);
161 EXPECT_FALSE(ret);
162 int32_t fd = socPerfThreadWrap->GetFdForFilePath("");
163 EXPECT_TRUE(fd < 0);
164 int32_t level = 10;
165 int64_t value = 0;
166 ret = socPerfThreadWrap->GetResValueByLevel(inValidResId, level, value);
167 EXPECT_FALSE(ret);
168 }
169
170 class SocperfStubTest : public SocPerfStub {
171 public:
SocperfStubTest()172 SocperfStubTest() {}
PerfRequest(int32_t cmdId,const std::string & msg)173 void PerfRequest(int32_t cmdId, const std::string& msg) override {}
PerfRequestEx(int32_t cmdId,bool onOffTag,const std::string & msg)174 void PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg) override {}
PowerLimitBoost(bool onOffTag,const std::string & msg)175 void PowerLimitBoost(bool onOffTag, const std::string& msg) override {}
ThermalLimitBoost(bool onOffTag,const std::string & msg)176 void ThermalLimitBoost(bool onOffTag, const std::string& msg) override {}
LimitRequest(int32_t clientId,const std::vector<int32_t> & tags,const std::vector<int64_t> & configs,const std::string & msg)177 void LimitRequest(int32_t clientId,
178 const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg) override {}
179 };
180
181 /*
182 * @tc.name: SocPerfStubTest_SocPerfServerAPI_001
183 * @tc.desc: test socperf stub api
184 * @tc.type FUNC
185 * @tc.require: issueI78T3V
186 */
187 HWTEST_F(SocPerfServerTest, SocPerfStubTest_SocPerfServerAPI_001, Function | MediumTest | Level0)
188 {
189 SocperfStubTest socPerfStub;
190 MessageParcel data;
191 data.WriteInterfaceToken(SocPerfStub::GetDescriptor());
192 data.WriteInt32(10000);
193 data.WriteString("");
194 MessageParcel reply;
195 MessageOption option;
196 uint32_t requestIpcId = static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_PERF_REQUEST);
197 socPerfStub.OnRemoteRequest(requestIpcId, data, reply, option);
198 }
199
200 /*
201 * @tc.name: SocPerfStubTest_SocPerfServerAPI_002
202 * @tc.desc: test socperf stub api
203 * @tc.type FUNC
204 * @tc.require: issueI78T3V
205 */
206 HWTEST_F(SocPerfServerTest, SocPerfStubTest_SocPerfServerAPI_002, Function | MediumTest | Level0)
207 {
208 SocperfStubTest socPerfStub;
209 MessageParcel data;
210 data.WriteInterfaceToken(SocPerfStub::GetDescriptor());
211 data.WriteInt32(10000);
212 data.WriteString("");
213 MessageParcel reply;
214 MessageOption option;
215 uint32_t requestExIpcId = static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_PERF_REQUEST_EX);
216 socPerfStub.OnRemoteRequest(requestExIpcId, data, reply, option);
217 }
218
219 /*
220 * @tc.name: SocPerfStubTest_SocPerfServerAPI_003
221 * @tc.desc: test socperf stub api
222 * @tc.type FUNC
223 * @tc.require: issueI78T3V
224 */
225 HWTEST_F(SocPerfServerTest, SocPerfStubTest_SocPerfServerAPI_003, Function | MediumTest | Level0)
226 {
227 SocperfStubTest socPerfStub;
228 MessageParcel data;
229 data.WriteInterfaceToken(SocPerfStub::GetDescriptor());
230 data.WriteInt32(1);
231 std::vector<int32_t> tags = {1001};
232 data.WriteInt32Vector(tags);
233 std::vector<int64_t> configs = {1416000};
234 data.WriteInt64Vector(configs);
235 data.WriteString("");
236 MessageParcel reply;
237 MessageOption option;
238 uint32_t powerLimitId = static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_LIMIT_REQUEST);
239 socPerfStub.OnRemoteRequest(powerLimitId, data, reply, option);
240 }
241
242 /*
243 * @tc.name: SocPerfStubTest_SocPerfServerAPI_004
244 * @tc.desc: test socperf stub api
245 * @tc.type FUNC
246 * @tc.require: issueI78T3V
247 */
248 HWTEST_F(SocPerfServerTest, SocPerfStubTest_SocPerfServerAPI_004, Function | MediumTest | Level0)
249 {
250 SocperfStubTest socPerfStub;
251 MessageParcel data;
252 data.WriteInterfaceToken(SocPerfStub::GetDescriptor());
253 data.WriteBool(true);
254 MessageParcel reply;
255 MessageOption option;
256 uint32_t powerLimitIpcId = static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_POWER_LIMIT_BOOST_FREQ);
257 socPerfStub.OnRemoteRequest(powerLimitIpcId, data, reply, option);
258 }
259
260 /*
261 * @tc.name: SocPerfStubTest_SocPerfServerAPI_005
262 * @tc.desc: test socperf stub api
263 * @tc.type FUNC
264 * @tc.require: issueI78T3V
265 */
266 HWTEST_F(SocPerfServerTest, SocPerfStubTest_SocPerfServerAPI_005, Function | MediumTest | Level0)
267 {
268 SocperfStubTest socPerfStub;
269 MessageParcel data;
270 data.WriteInterfaceToken(SocPerfStub::GetDescriptor());
271 data.WriteBool(true);
272 MessageParcel reply;
273 MessageOption option;
274 uint32_t thermalLimitIpcId = static_cast<uint32_t>(SocPerfInterfaceCode::TRANS_IPC_ID_THERMAL_LIMIT_BOOST_FREQ);
275 socPerfStub.OnRemoteRequest(thermalLimitIpcId, data, reply, option);
276 }
277
278 /*
279 * @tc.name: SocPerfStubTest_SocPerfServerAPI_006
280 * @tc.desc: test socperf stub api
281 * @tc.type FUNC
282 * @tc.require: issueI78T3V
283 */
284 HWTEST_F(SocPerfServerTest, SocPerfStubTest_SocPerfServerAPI_006, Function | MediumTest | Level0)
285 {
286 SocperfStubTest socPerfStub;
287 MessageParcel data;
288 data.WriteInterfaceToken(SocPerfStub::GetDescriptor());
289 data.WriteBool(true);
290 MessageParcel reply;
291 MessageOption option;
292 uint32_t ipcId = 0x0004;
293 socPerfStub.OnRemoteRequest(ipcId, data, reply, option);
294 }
295
296 } // namespace SOCPERF
297 } // namespace OHOS