1 /*
2 * Copyright (c) 2022-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 #include "thermal_mock_stub_test.h"
17
18 #include <vector>
19
20 #ifdef THERMAL_GTEST
21 #define private public
22 #define protected public
23 #endif
24
25 #include "thermal_action_callback_stub.h"
26 #include "thermal_level_callback_stub.h"
27 #include "thermal_log.h"
28 #include "thermal_mgr_errors.h"
29 #include "thermal_service.h"
30 #include "ithermal_srv.h"
31 #include "thermal_srv_proxy.h"
32 #include "thermal_srv_stub.h"
33 #include "thermal_temp_callback_stub.h"
34
35 using namespace testing::ext;
36 using namespace OHOS::PowerMgr;
37 using namespace OHOS;
38 using namespace std;
39
40 namespace {
41 MessageParcel g_data;
42 MessageParcel g_reply;
43 MessageOption g_option;
44 sptr<ThermalService> g_service = nullptr;
45 } // namespace
46
SetUpTestCase()47 void ThermalMockStubTest::SetUpTestCase()
48 {
49 g_service = ThermalService::GetInstance();
50 g_service->InitSystemTestModules();
51 g_service->OnStart();
52 }
53
54 namespace {
55 /**
56 * @tc.name: ThermalMockStubTest001
57 * @tc.desc: proxy test
58 * @tc.type: FUNC
59 * @tc.require: issueI5YZQ2
60 */
61 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest001, TestSize.Level0)
62 {
63 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest001 function start!");
64 uint32_t code = 0;
65 MessageParcel data;
66 MessageParcel reply;
67 MessageOption option;
68 sptr<ThermalActionCallbackStub> actionStub = new ThermalActionCallbackStub();
69 ASSERT_FALSE(actionStub == nullptr);
70 EXPECT_EQ(E_GET_THERMAL_SERVICE_FAILED, actionStub->OnRemoteRequest(code, data, reply, option));
71 sptr<ThermalLevelCallbackStub> levelStub = new ThermalLevelCallbackStub();
72 ASSERT_FALSE(levelStub == nullptr);
73 EXPECT_EQ(E_GET_THERMAL_SERVICE_FAILED, levelStub->OnRemoteRequest(code, data, reply, option));
74 sptr<ThermalTempCallbackStub> tempStub = new ThermalTempCallbackStub();
75 ASSERT_FALSE(tempStub == nullptr);
76 EXPECT_EQ(E_GET_THERMAL_SERVICE_FAILED, tempStub->OnRemoteRequest(code, data, reply, option));
77 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest001 function end!");
78 }
79
80 /**
81 * @tc.name: ThermalMockStubTest002
82 * @tc.desc: OnRemoteRequest all
83 * @tc.type: FUNC
84 * @tc.require: issueI6KRS8
85 */
86 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest002, TestSize.Level0)
87 {
88 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest002 function start!");
89 uint32_t begin = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_SUBSCRIBE_THERMAL_TEMP_CALLBACK);
90 uint32_t end = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_SHELL_DUMP);
91 for (uint32_t code = begin; code <= end; ++code) {
92 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
93 auto ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
94 EXPECT_TRUE(ret == ERR_INVALID_DATA || ret == E_GET_SYSTEM_ABILITY_MANAGER_FAILED_THERMAL ||
95 ret == 0) << "code: " << code << " ret:" << ret;
96 }
97 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest002 function end!");
98 }
99
100 /**
101 * @tc.name: ThermalMockStubTest003
102 * @tc.desc: OnRemoteRequest ReadInterfaceToken
103 * @tc.type: FUNC
104 */
105 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest003, TestSize.Level0)
106 {
107 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest003 function start!");
108 uint32_t code = 1;
109 int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
110 EXPECT_EQ(ret, ERR_TRANSACTION_FAILED) << "code: " << code << " ret:" << ret;
111 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest003 function end!");
112 }
113
114 /**
115 * @tc.name: ThermalMockStubTest004
116 * @tc.desc: OnRemoteRequest Invalid code
117 * @tc.type: FUNC
118 */
119 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest004, TestSize.Level0)
120 {
121 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest004 function start!");
122 uint32_t code = 9999;
123 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
124 int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
125 EXPECT_EQ(ret, IPC_STUB_UNKNOW_TRANS_ERR) << "code: " << code << " ret:" << ret;
126 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest004 function end!");
127 }
128
129 /**
130 * @tc.name: ThermalMockStubTest005
131 * @tc.desc: SubscribeThermalTempCallbackStub
132 * @tc.type: FUNC
133 */
134 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest005, TestSize.Level0)
135 {
136 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest005 function start!");
137
138 sptr<ThermalTempCallbackStub> tempStub = new ThermalTempCallbackStub();
139 ASSERT_NE(tempStub, nullptr);
140
141 std::vector<std::string> typeList;
142 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
143 ASSERT_FALSE(typeList.size() > static_cast<size_t>(VECTOR_MAX_SIZE))
144 << "The vector/array size exceeds the security limit!";
145
146 g_data.WriteInt32(typeList.size());
147 for (auto it1 = typeList.begin(); it1 != typeList.end(); ++it1) {
148 ASSERT_TRUE(g_data.WriteString16(Str8ToStr16((*it1))))
149 << "Write [" << *it1 << "] failed!";
150 }
151
152 ASSERT_TRUE(g_data.WriteRemoteObject(tempStub->AsObject())) << "Write [tempStub] failed!";
153
154 uint32_t code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_SUBSCRIBE_THERMAL_TEMP_CALLBACK);
155 int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
156 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
157
158 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
159 THERMAL_WRITE_PARCEL_NO_RET(g_data, RemoteObject, tempStub->AsObject());
160 code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_UN_SUBSCRIBE_THERMAL_TEMP_CALLBACK);
161 ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
162 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
163 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest005 function end!");
164 }
165
166 /**
167 * @tc.name: ThermalMockStubTest006
168 * @tc.desc: SubscribeThermalLevelCallbackStub
169 * @tc.type: FUNC
170 */
171 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest006, TestSize.Level0)
172 {
173 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest006 function start!");
174
175 MessageParcel data;
176 sptr<ThermalLevelCallbackStub> levelStub = new ThermalLevelCallbackStub();
177 ASSERT_NE(levelStub, nullptr);
178 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
179 THERMAL_HILOGI(LABEL_TEST, "WriteInterfaceToken1 success!");
180 ASSERT_TRUE(g_data.WriteRemoteObject(levelStub->AsObject())) << "Write [levelStub] failed!";
181 THERMAL_HILOGI(LABEL_TEST, "WriteRemoteObject1 success!");
182 uint32_t code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_SUBSCRIBE_THERMAL_LEVEL_CALLBACK);
183 int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
184 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
185
186 code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_UN_SUBSCRIBE_THERMAL_LEVEL_CALLBACK);
187 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
188 THERMAL_WRITE_PARCEL_NO_RET(g_data, RemoteObject, levelStub->AsObject());
189 ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
190 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
191
192 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest006 function end!");
193 }
194
195 /**
196 * @tc.name: ThermalMockStubTest007
197 * @tc.desc: SubscribeThermalActionCallbackStub
198 * @tc.type: FUNC
199 */
200 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest007, TestSize.Level0)
201 {
202 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest007 function start!");
203
204 sptr<ThermalActionCallbackStub> actionStub = new ThermalActionCallbackStub();
205 ASSERT_NE(actionStub, nullptr);
206 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
207 std::vector<std::string> actionList;
208 ASSERT_FALSE(actionList.size() > static_cast<size_t>(VECTOR_MAX_SIZE))
209 << "The vector/array size exceeds the security limit!";
210 g_data.WriteInt32(actionList.size());
211 for (auto it2 = actionList.begin(); it2 != actionList.end(); ++it2) {
212 ASSERT_TRUE(g_data.WriteString16(Str8ToStr16((*it2))))
213 << "Write [" << *it2 << "] failed!";
214 }
215 ASSERT_TRUE(g_data.WriteString16(Str8ToStr16("ThermalMockStubTest007"))) << "Write [desc] failed!";
216
217 ASSERT_TRUE(g_data.WriteRemoteObject(actionStub->AsObject())) << "Write [actionStub] failed!";
218 uint32_t code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_SUBSCRIBE_THERMAL_ACTION_CALLBACK);
219 int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
220 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
221
222 code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_UN_SUBSCRIBE_THERMAL_ACTION_CALLBACK);
223 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
224 THERMAL_WRITE_PARCEL_NO_RET(g_data, RemoteObject, actionStub->AsObject());
225 ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
226 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
227
228 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest007 function end!");
229 }
230
231 /**
232 * @tc.name: ThermalMockStubTest008
233 * @tc.desc: GetThermalSrvSensorInfo
234 * @tc.type: FUNC
235 */
236 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest008, TestSize.Level0)
237 {
238 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest008 function start!");
239
240 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
241 ASSERT_TRUE(g_data.WriteInt32(static_cast<int32_t>(SensorType::BATTERY))) << "Write [type] failed!";
242 uint32_t code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_GET_THERMAL_SRV_SENSOR_INFO);
243 int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
244 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
245
246 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest008 function end!");
247 }
248
249 /**
250 * @tc.name: ThermalMockStubTest009
251 * @tc.desc: SetSceneStub
252 * @tc.type: FUNC
253 */
254 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest009, TestSize.Level0)
255 {
256 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest009 function start!");
257
258 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
259 ASSERT_TRUE(g_data.WriteString16(Str8ToStr16("test"))) << "Write [scene] failed!";
260 uint32_t code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_SET_SCENE);
261 int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
262 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
263
264 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest009 function end!");
265 }
266
267 /**
268 * @tc.name: ThermalMockStubTest010
269 * @tc.desc: ShellDumpStub
270 * @tc.type: FUNC
271 */
272 HWTEST_F(ThermalMockStubTest, ThermalMockStubTest010, TestSize.Level0)
273 {
274 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest010 function start!");
275
276 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
277 const int32_t PARAM_MAX_NUM = 1024000;
278 g_data.WriteUint32(PARAM_MAX_NUM);
279 uint32_t code = static_cast<uint32_t>(PowerMgr::IThermalSrvIpcCode::COMMAND_SHELL_DUMP);
280 int32_t ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
281 EXPECT_EQ(ret, ERR_INVALID_DATA) << " ret:" << ret;
282
283 g_data.WriteInterfaceToken(ThermalSrvProxy::GetDescriptor());
284 g_data.WriteUint32(1);
285 g_data.WriteString("123");
286 ret = g_service->OnRemoteRequest(code, g_data, g_reply, g_option);
287 EXPECT_EQ(ret, ERR_OK) << " ret:" << ret;
288
289 THERMAL_HILOGI(LABEL_TEST, "ThermalMockStubTest010 function end!");
290 }
291 } // namespace
292