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     OHOS::AppExecFwk::ElementName admin;
76     admin.SetBundleName(ADMIN_PACKAGENAME);
77     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
78         .Times(1)
79         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBluetoothProxySendRequestGetPolicy));
80     BluetoothInfo bluetoothInfo;
81     int32_t ret = proxy_->GetBluetoothInfo(admin, bluetoothInfo);
82     ASSERT_TRUE(ret == ERR_OK);
83     ASSERT_TRUE(bluetoothInfo.name == RETURN_STRING);
84     ASSERT_TRUE(bluetoothInfo.state == 1);
85     ASSERT_TRUE(bluetoothInfo.connectionState == 1);
86 }
87 
88 /**
89  * @tc.name: TestGetBluetoothInfoFail
90  * @tc.desc: Test GetBluetoothInfo without enable edm service func.
91  * @tc.type: FUNC
92  */
93 HWTEST_F(BluetoothManagerProxyTest, TestGetBluetoothInfoFail, TestSize.Level1)
94 {
95     Utils::SetEdmServiceDisable();
96     OHOS::AppExecFwk::ElementName admin;
97     admin.SetBundleName(ADMIN_PACKAGENAME);
98     BluetoothInfo bluetoothInfo;
99     int32_t ret = proxy_->GetBluetoothInfo(admin, bluetoothInfo);
100     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
101 }
102 
103 /**
104  * @tc.name: TestSetBluetoothDisabledSuc
105  * @tc.desc: Test SetBluetoothDisabled func.
106  * @tc.type: FUNC
107  */
108 HWTEST_F(BluetoothManagerProxyTest, TestSetBluetoothDisabledSuc, TestSize.Level1)
109 {
110     AppExecFwk::ElementName admin;
111     admin.SetBundleName(ADMIN_PACKAGENAME);
112     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
113         .Times(1)
114         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
115 
116     int32_t ret = proxy_->SetBluetoothDisabled(admin, true);
117     ASSERT_TRUE(ret == ERR_OK);
118 }
119 
120 /**
121  * @tc.name: TestSetBluetoothDisabledFail
122  * @tc.desc: Test SetBluetoothDisabled func without enable edm service.
123  * @tc.type: FUNC
124  */
125 HWTEST_F(BluetoothManagerProxyTest, TestSetBluetoothDisabledFail, TestSize.Level1)
126 {
127     Utils::SetEdmServiceDisable();
128     AppExecFwk::ElementName admin;
129     admin.SetBundleName(ADMIN_PACKAGENAME);
130 
131     int32_t ret = proxy_->SetBluetoothDisabled(admin, true);
132     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
133 }
134 
135 /**
136  * @tc.name: TestIsBluetoothDisabledSuc
137  * @tc.desc: Test IsBluetoothDisabled func.
138  * @tc.type: FUNC
139  */
140 HWTEST_F(BluetoothManagerProxyTest, TestIsBluetoothDisabledSuc, TestSize.Level1)
141 {
142     AppExecFwk::ElementName admin;
143     admin.SetBundleName(ADMIN_PACKAGENAME);
144     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
145         .Times(1)
146         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
147 
148     bool isDisable = false;
149     int32_t ret = proxy_->IsBluetoothDisabled(&admin, isDisable);
150     ASSERT_TRUE(ret == ERR_OK);
151     ASSERT_TRUE(isDisable);
152 }
153 
154 /**
155  * @tc.name: TestIsBluetoothDisabledFail
156  * @tc.desc: Test IsBluetoothDisabled func without enable edm service.
157  * @tc.type: FUNC
158  */
159 HWTEST_F(BluetoothManagerProxyTest, TestIsBluetoothDisabledFail, TestSize.Level1)
160 {
161     Utils::SetEdmServiceDisable();
162     AppExecFwk::ElementName admin;
163     admin.SetBundleName(ADMIN_PACKAGENAME);
164 
165     bool isDisable = false;
166     int32_t ret = proxy_->IsBluetoothDisabled(&admin, isDisable);
167     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
168     ASSERT_FALSE(isDisable);
169 }
170 
171 /**
172  * @tc.name: TestAddAllowedBluetoothDevicesSuc
173  * @tc.desc: Test AddAllowedBluetoothDevices func.
174  * @tc.type: FUNC
175  */
176 HWTEST_F(BluetoothManagerProxyTest, TestAddAllowedBluetoothDevicesSuc, TestSize.Level1)
177 {
178     OHOS::AppExecFwk::ElementName admin;
179     admin.SetBundleName(ADMIN_PACKAGENAME);
180     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
181         .Times(1)
182         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
183     std::vector<std::string> deviceIds = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
184     int32_t ret = proxy_->AddAllowedBluetoothDevices(admin, deviceIds);
185     ASSERT_TRUE(ret == ERR_OK);
186 }
187 
188 /**
189  * @tc.name: TestGetAllowedBluetoothDevicesFail
190  * @tc.desc: Test AddAllowedBluetoothDevices without enable edm service func.
191  * @tc.type: FUNC
192  */
193 HWTEST_F(BluetoothManagerProxyTest, TestAddAllowedBluetoothDevicesFail, TestSize.Level1)
194 {
195     Utils::SetEdmServiceDisable();
196     OHOS::AppExecFwk::ElementName admin;
197     admin.SetBundleName(ADMIN_PACKAGENAME);
198     std::vector<std::string> deviceIds;
199     int32_t ret = proxy_->AddAllowedBluetoothDevices(admin, deviceIds);
200     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
201 }
202 
203 /**
204  * @tc.name: TestGetAllowedBluetoothDevicesSuc
205  * @tc.desc: Test GetAllowedBluetoothDevices func.
206  * @tc.type: FUNC
207  */
208 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesSuc, TestSize.Level1)
209 {
210     OHOS::AppExecFwk::ElementName admin;
211     admin.SetBundleName(ADMIN_PACKAGENAME);
212     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
213         .Times(1)
214         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBluetoothProxySendRequestGetPolicy));
215     std::vector<std::string> deviceIds;
216     int32_t ret = proxy_->GetAllowedBluetoothDevices(&admin, deviceIds);
217     ASSERT_TRUE(ret == ERR_OK);
218 }
219 
220 /**
221  * @tc.name: TestGetAllowedBluetoothDevicesFail
222  * @tc.desc: Test GetAllowedBluetoothDevices without enable edm service func.
223  * @tc.type: FUNC
224  */
225 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesFail, TestSize.Level1)
226 {
227     Utils::SetEdmServiceDisable();
228     OHOS::AppExecFwk::ElementName admin;
229     admin.SetBundleName(ADMIN_PACKAGENAME);
230     std::vector<std::string> deviceIds;
231     int32_t ret = proxy_->GetAllowedBluetoothDevices(&admin, deviceIds);
232     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
233 }
234 
235 /**
236  * @tc.name: TestGetAllowedBluetoothDevicesWithoutAdminSuc
237  * @tc.desc: Test GetAllowedBluetoothDevices func.
238  * @tc.type: FUNC
239  */
240 HWTEST_F(BluetoothManagerProxyTest, TestGetAllowedBluetoothDevicesWithoutAdminSuc, TestSize.Level1)
241 {
242     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
243         .Times(1)
244         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
245     std::vector<std::string> deviceIds;
246     int32_t ret = proxy_->GetAllowedBluetoothDevices(nullptr, deviceIds);
247     ASSERT_TRUE(ret == ERR_OK);
248 }
249 
250 /**
251  * @tc.name: TestRemoveAllowedBluetoothDevicesSuc
252  * @tc.desc: Test RemoveAllowedBluetoothDevices func.
253  * @tc.type: FUNC
254  */
255 HWTEST_F(BluetoothManagerProxyTest, TestRemoveAllowedBluetoothDevicesSuc, TestSize.Level1)
256 {
257     OHOS::AppExecFwk::ElementName admin;
258     admin.SetBundleName(ADMIN_PACKAGENAME);
259     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
260         .Times(1)
261         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
262     std::vector<std::string> deviceIds = { "00:1A:2B:3C:4D:5E", "AA:BB:CC:DD:EE:FF" };
263     int32_t ret = proxy_->RemoveAllowedBluetoothDevices(admin, deviceIds);
264     ASSERT_TRUE(ret == ERR_OK);
265 }
266 
267 /**
268  * @tc.name: TestRemoveAllowedBluetoothDevicesFail
269  * @tc.desc: Test RemoveAllowedBluetoothDevices without enable edm service func.
270  * @tc.type: FUNC
271  */
272 HWTEST_F(BluetoothManagerProxyTest, TestRemoveAllowedBluetoothDevicesFail, TestSize.Level1)
273 {
274     Utils::SetEdmServiceDisable();
275     OHOS::AppExecFwk::ElementName admin;
276     admin.SetBundleName(ADMIN_PACKAGENAME);
277     std::vector<std::string> deviceIds;
278     int32_t ret = proxy_->RemoveAllowedBluetoothDevices(admin, deviceIds);
279     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
280 }
281 } // namespace TEST
282 } // namespace EDM
283 } // namespace OHOS
284