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 #include <gtest/gtest.h>
17 #include <string>
18 #include <system_ability_definition.h>
19 #include <vector>
20
21 #include "edm_sys_manager_mock.h"
22 #include "enterprise_device_mgr_stub_mock.h"
23 #include "bluetooth_manager_proxy.h"
24 #include "utils.h"
25
26 using namespace testing::ext;
27 using namespace testing;
28
29 namespace OHOS {
30 namespace EDM {
31 namespace TEST {
32 const std::string ADMIN_PACKAGENAME = "com.edm.test.demo";
33 class BluetoothManagerProxyTest : public testing::Test {
34 protected:
35 void SetUp() override;
36
37 void TearDown() override;
38
39 static void TearDownTestSuite(void);
40 std::shared_ptr<BluetoothManagerProxy> proxy_ = nullptr;
41 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
42 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
43 };
44
SetUp()45 void BluetoothManagerProxyTest::SetUp()
46 {
47 proxy_ = BluetoothManagerProxy::GetBluetoothManagerProxy();
48 edmSysManager_ = std::make_shared<EdmSysManager>();
49 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
50 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
51 Utils::SetEdmServiceEnable();
52 }
53
TearDown()54 void BluetoothManagerProxyTest::TearDown()
55 {
56 proxy_.reset();
57 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
58 object_ = nullptr;
59 Utils::SetEdmServiceDisable();
60 }
61
TearDownTestSuite()62 void BluetoothManagerProxyTest::TearDownTestSuite()
63 {
64 ASSERT_FALSE(Utils::GetEdmServiceState());
65 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
66 }
67
68 /**
69 * @tc.name: TestGetBluetoothInfoSuc
70 * @tc.desc: Test GetBluetoothInfo success func.
71 * @tc.type: FUNC
72 */
73 HWTEST_F(BluetoothManagerProxyTest, TestGetBluetoothInfoSuc, TestSize.Level1)
74 {
75 MessageParcel data;
76 OHOS::AppExecFwk::ElementName admin;
77 admin.SetBundleName(ADMIN_PACKAGENAME);
78 data.WriteParcelable(&admin);
79 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
80 .Times(1)
81 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBluetoothProxySendRequestGetPolicy));
82
83 BluetoothInfo bluetoothInfo;
84 int32_t ret = proxy_->GetBluetoothInfo(data, bluetoothInfo);
85 ASSERT_TRUE(ret == ERR_OK);
86 ASSERT_TRUE(bluetoothInfo.name == RETURN_STRING);
87 ASSERT_TRUE(bluetoothInfo.state == 1);
88 ASSERT_TRUE(bluetoothInfo.connectionState == 1);
89 }
90
91 /**
92 * @tc.name: TestGetBluetoothInfoFail
93 * @tc.desc: Test GetBluetoothInfo without enable edm service func.
94 * @tc.type: FUNC
95 */
96 HWTEST_F(BluetoothManagerProxyTest, TestGetBluetoothInfoFail, TestSize.Level1)
97 {
98 Utils::SetEdmServiceDisable();
99 MessageParcel data;
100 OHOS::AppExecFwk::ElementName admin;
101 admin.SetBundleName(ADMIN_PACKAGENAME);
102 data.WriteParcelable(&admin);
103
104 BluetoothInfo bluetoothInfo;
105 int32_t ret = proxy_->GetBluetoothInfo(data, bluetoothInfo);
106 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
107 }
108
109 /**
110 * @tc.name: TestSetBluetoothDisabledSuc
111 * @tc.desc: Test SetBluetoothDisabled func.
112 * @tc.type: FUNC
113 */
114 HWTEST_F(BluetoothManagerProxyTest, TestSetBluetoothDisabledSuc, TestSize.Level1)
115 {
116 MessageParcel data;
117 AppExecFwk::ElementName admin;
118 admin.SetBundleName(ADMIN_PACKAGENAME);
119 data.WriteParcelable(&admin);
120 data.WriteBool(true);
121 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
122 .Times(1)
123 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
124
125 int32_t ret = proxy_->SetBluetoothDisabled(data);
126 ASSERT_TRUE(ret == ERR_OK);
127 }
128
129 /**
130 * @tc.name: TestSetBluetoothDisabledFail
131 * @tc.desc: Test SetBluetoothDisabled func without enable edm service.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(BluetoothManagerProxyTest, TestSetBluetoothDisabledFail, TestSize.Level1)
135 {
136 Utils::SetEdmServiceDisable();
137 MessageParcel data;
138 AppExecFwk::ElementName admin;
139 admin.SetBundleName(ADMIN_PACKAGENAME);
140 data.WriteParcelable(&admin);
141 data.WriteBool(true);
142
143 int32_t ret = proxy_->SetBluetoothDisabled(data);
144 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
145 }
146
147 /**
148 * @tc.name: TestIsBluetoothDisabledSuc
149 * @tc.desc: Test IsBluetoothDisabled func.
150 * @tc.type: FUNC
151 */
152 HWTEST_F(BluetoothManagerProxyTest, TestIsBluetoothDisabledSuc, TestSize.Level1)
153 {
154 MessageParcel data;
155 AppExecFwk::ElementName admin;
156 admin.SetBundleName(ADMIN_PACKAGENAME);
157 data.WriteParcelable(&admin);
158 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
159 .Times(1)
160 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
161
162 bool isDisable = false;
163 int32_t ret = proxy_->IsBluetoothDisabled(data, isDisable);
164 ASSERT_TRUE(ret == ERR_OK);
165 ASSERT_TRUE(isDisable);
166 }
167
168 /**
169 * @tc.name: TestIsBluetoothDisabledFail
170 * @tc.desc: Test IsBluetoothDisabled func without enable edm service.
171 * @tc.type: FUNC
172 */
173 HWTEST_F(BluetoothManagerProxyTest, TestIsBluetoothDisabledFail, TestSize.Level1)
174 {
175 Utils::SetEdmServiceDisable();
176 MessageParcel data;
177 AppExecFwk::ElementName admin;
178 admin.SetBundleName(ADMIN_PACKAGENAME);
179 data.WriteParcelable(&admin);
180
181 bool isDisable = false;
182 int32_t ret = proxy_->IsBluetoothDisabled(data, isDisable);
183 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
184 ASSERT_FALSE(isDisable);
185 }
186
187 /**
188 * @tc.name: TestAddAllowedBluetoothDevicesSuc
189 * @tc.desc: Test AddAllowedBluetoothDevices func.
190 * @tc.type: FUNC
191 */
192 HWTEST_F(BluetoothManagerProxyTest, TestAddAllowedBluetoothDevicesSuc, TestSize.Level1)
193 {
194 MessageParcel data;
195 OHOS::AppExecFwk::ElementName admin;
196 admin.SetBundleName(ADMIN_PACKAGENAME);
197 data.WriteParcelable(&admin);
198 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
199 .Times(1)
200 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
201 std::vector<std::string> deviceIds = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
202 data.WriteStringVector(deviceIds);
203 int32_t ret = proxy_->AddOrRemoveAllowedBluetoothDevices(data, true);
204 ASSERT_TRUE(ret == ERR_OK);
205 }
206
207 /**
208 * @tc.name: TestAddAllowedBluetoothDevicesFail
209 * @tc.desc: Test AddAllowedBluetoothDevices func.
210 * @tc.type: FUNC
211 */
212 HWTEST_F(BluetoothManagerProxyTest, TestAddAllowedBluetoothDevicesFail, TestSize.Level1)
213 {
214 Utils::SetEdmServiceDisable();
215 OHOS::AppExecFwk::ElementName admin;
216 admin.SetBundleName(ADMIN_PACKAGENAME);
217 std::vector<std::string> deviceIds;
218 MessageParcel data;
219 data.WriteParcelable(&admin);
220 data.WriteStringVector(deviceIds);
221 int32_t ret = proxy_->AddOrRemoveAllowedBluetoothDevices(data, true);
222 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
223 }
224
225 /**
226 * @tc.name: TestGetAllowedBluetoothDevicesSuc
227 * @tc.desc: Test GetAllowedBluetoothDevices func.
228 * @tc.type: FUNC
229 */
230 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesSuc, TestSize.Level1)
231 {
232 OHOS::AppExecFwk::ElementName admin;
233 admin.SetBundleName(ADMIN_PACKAGENAME);
234 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
235 .Times(1)
236 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy));
237 std::vector<std::string> deviceIds;
238 int32_t ret = proxy_->GetAllowedBluetoothDevices(&admin, deviceIds);
239 ASSERT_TRUE(ret == ERR_OK);
240 ASSERT_TRUE(deviceIds.size() == 1);
241 ASSERT_EQ(deviceIds[0], RETURN_STRING);
242 }
243
244 /**
245 * @tc.name: TestGetAllowedBluetoothDevicesFail
246 * @tc.desc: Test GetAllowedBluetoothDevices without enable edm service func.
247 * @tc.type: FUNC
248 */
249 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesFail, TestSize.Level1)
250 {
251 Utils::SetEdmServiceDisable();
252 OHOS::AppExecFwk::ElementName admin;
253 admin.SetBundleName(ADMIN_PACKAGENAME);
254 std::vector<std::string> deviceIds;
255 int32_t ret = proxy_->GetAllowedBluetoothDevices(&admin, deviceIds);
256 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
257 }
258
259 /**
260 * @tc.name: TestGetAllowedBluetoothDevicesWithoutAdminSuc
261 * @tc.desc: Test GetAllowedBluetoothDevices func.
262 * @tc.type: FUNC
263 */
264 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesWithoutAdminSuc, TestSize.Level1)
265 {
266 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
267 .Times(1)
268 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy));
269 std::vector<std::string> deviceIds;
270 int32_t ret = proxy_->GetAllowedBluetoothDevices(nullptr, deviceIds);
271 ASSERT_TRUE(ret == ERR_OK);
272 ASSERT_TRUE(deviceIds.size() == 1);
273 ASSERT_EQ(deviceIds[0], RETURN_STRING);
274 }
275
276 /**
277 * @tc.name: TestGetAllowedBluetoothDevicesSuc
278 * @tc.desc: Test GetAllowedBluetoothDevices func.
279 * @tc.type: FUNC
280 */
281 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesSuc001, TestSize.Level1)
282 {
283 MessageParcel data;
284 OHOS::AppExecFwk::ElementName admin;
285 admin.SetBundleName(ADMIN_PACKAGENAME);
286 data.WriteParcelable(&admin);
287 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
288 .Times(1)
289 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy));
290
291 std::vector<std::string> deviceIds;
292 int32_t ret = proxy_->GetAllowedBluetoothDevices(data, deviceIds);
293 ASSERT_TRUE(ret == ERR_OK);
294 ASSERT_TRUE(deviceIds.size() == 1);
295 ASSERT_EQ(deviceIds[0], RETURN_STRING);
296 }
297
298 /**
299 * @tc.name: TestGetAllowedBluetoothDevicesWithoutAdminSuc
300 * @tc.desc: Test GetAllowedBluetoothDevices func.
301 * @tc.type: FUNC
302 */
303 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesWithoutAdminSuc001, TestSize.Level1)
304 {
305 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
306 .Times(1)
307 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy));
308 std::vector<std::string> deviceIds;
309 MessageParcel data;
310 data.WriteParcelable(nullptr);
311 int32_t ret = proxy_->GetAllowedBluetoothDevices(data, deviceIds);
312 ASSERT_TRUE(ret == ERR_OK);
313 ASSERT_TRUE(deviceIds.size() == 1);
314 ASSERT_EQ(deviceIds[0], RETURN_STRING);
315 }
316
317 /**
318 * @tc.name: TestGetAllowedBluetoothDevicesFail
319 * @tc.desc: Test GetAllowedBluetoothDevices without enable edm service func.
320 * @tc.type: FUNC
321 */
322 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesFail001, TestSize.Level1)
323 {
324 Utils::SetEdmServiceDisable();
325 MessageParcel data;
326 OHOS::AppExecFwk::ElementName admin;
327 admin.SetBundleName(ADMIN_PACKAGENAME);
328 data.WriteParcelable(&admin);
329
330 std::vector<std::string> deviceIds;
331 int32_t ret = proxy_->GetAllowedBluetoothDevices(data, deviceIds);
332 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
333 }
334
335 /**
336 * @tc.name: TestGetAllowedBluetoothDevicesFail
337 * @tc.desc: Test GetAllowedBluetoothDevicesFail func if does not have Admin parameter without enable edm service.
338 * @tc.type: FUNC
339 */
340 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesFail002, TestSize.Level1)
341 {
342 Utils::SetEdmServiceDisable();
343 MessageParcel data;
344 const std::u16string descriptor = u"ohos.edm.testdemo";
345 data.WriteInterfaceToken(descriptor);
346 data.WriteInt32(WITHOUT_USERID);
347 data.WriteString(WITHOUT_PERMISSION_TAG);
348 data.WriteInt32(WITHOUT_ADMIN);
349 std::vector<std::string> deviceIds;
350 ErrCode ret = proxy_->GetAllowedBluetoothDevices(data, deviceIds);
351 ASSERT_TRUE(ret == ERR_OK);
352 }
353
354 /**
355 * @tc.name: TestRemoveAllowedBluetoothDevicesSuc
356 * @tc.desc: Test RemoveAllowedBluetoothDevices func.
357 * @tc.type: FUNC
358 */
359 HWTEST_F(BluetoothManagerProxyTest, TestRemoveAllowedBluetoothDevicesSuc, TestSize.Level1)
360 {
361 MessageParcel data;
362 OHOS::AppExecFwk::ElementName admin;
363 admin.SetBundleName(ADMIN_PACKAGENAME);
364 data.WriteParcelable(&admin);
365 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
366 .Times(1)
367 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
368 std::vector<std::string> deviceIds = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
369 data.WriteStringVector(deviceIds);
370
371 int32_t ret = proxy_->AddOrRemoveAllowedBluetoothDevices(data, false);
372 ASSERT_TRUE(ret == ERR_OK);
373 }
374
375 /**
376 * @tc.name: TestRemoveAllowedBluetoothDevicesFail
377 * @tc.desc: Test RemoveAllowedBluetoothDevices without enable edm service func.
378 * @tc.type: FUNC
379 */
380 HWTEST_F(BluetoothManagerProxyTest, TestRemoveAllowedBluetoothDevicesFail, TestSize.Level1)
381 {
382 Utils::SetEdmServiceDisable();
383 MessageParcel data;
384 OHOS::AppExecFwk::ElementName admin;
385 admin.SetBundleName(ADMIN_PACKAGENAME);
386 data.WriteParcelable(&admin);
387 std::vector<std::string> deviceIds;
388 data.WriteStringVector(deviceIds);
389
390 int32_t ret = proxy_->AddOrRemoveAllowedBluetoothDevices(data, false);
391 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
392 }
393 } // namespace TEST
394 } // namespace EDM
395 } // namespace OHOS
396