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 "usb_device_id.h"
24 #include "usb_manager_proxy.h"
25 #include "utils.h"
26
27 using namespace testing::ext;
28 using namespace testing;
29
30 namespace OHOS {
31 namespace EDM {
32 namespace TEST {
33 const std::string ADMIN_PACKAGENAME = "com.edm.test.demo";
34 class UsbManagerProxyTest : public testing::Test {
35 protected:
36 void SetUp() override;
37
38 void TearDown() override;
39
40 static void TearDownTestSuite(void);
41 std::shared_ptr<UsbManagerProxy> proxy_ = nullptr;
42 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
43 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
44 };
45
SetUp()46 void UsbManagerProxyTest::SetUp()
47 {
48 proxy_ = UsbManagerProxy::GetUsbManagerProxy();
49 edmSysManager_ = std::make_shared<EdmSysManager>();
50 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
51 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
52 Utils::SetEdmServiceEnable();
53 }
54
TearDown()55 void UsbManagerProxyTest::TearDown()
56 {
57 proxy_.reset();
58 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
59 object_ = nullptr;
60 Utils::SetEdmServiceDisable();
61 }
62
TearDownTestSuite()63 void UsbManagerProxyTest::TearDownTestSuite()
64 {
65 ASSERT_FALSE(Utils::GetEdmServiceState());
66 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
67 }
68
69 /**
70 * @tc.name: TestSetUsbReadOnlySuc
71 * @tc.desc: Test SetUsbReadOnly success func.
72 * @tc.type: FUNC
73 */
74 HWTEST_F(UsbManagerProxyTest, TestSetUsbReadOnlySuc, TestSize.Level1)
75 {
76 OHOS::AppExecFwk::ElementName admin;
77 admin.SetBundleName(ADMIN_PACKAGENAME);
78 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
79 .Times(1)
80 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
81 int32_t ret = proxy_->SetUsbReadOnly(admin, true);
82 ASSERT_TRUE(ret == ERR_OK);
83 }
84
85 /**
86 * @tc.name: TestSetUsbReadOnlyFail
87 * @tc.desc: Test SetUsbReadOnly without enable edm service func.
88 * @tc.type: FUNC
89 */
90 HWTEST_F(UsbManagerProxyTest, TestSetUsbReadOnlyFail, TestSize.Level1)
91 {
92 Utils::SetEdmServiceDisable();
93 OHOS::AppExecFwk::ElementName admin;
94 admin.SetBundleName(ADMIN_PACKAGENAME);
95 int32_t ret = proxy_->SetUsbReadOnly(admin, true);
96 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
97 }
98
99 /**
100 * @tc.name: TestDisableUsbSuc
101 * @tc.desc: Test DisableUsb success func.
102 * @tc.type: FUNC
103 */
104 HWTEST_F(UsbManagerProxyTest, TestDisableUsbSuc, TestSize.Level1)
105 {
106 OHOS::AppExecFwk::ElementName admin;
107 admin.SetBundleName(ADMIN_PACKAGENAME);
108 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
109 .Times(1)
110 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
111 int32_t ret = proxy_->DisableUsb(admin, true);
112 ASSERT_TRUE(ret == ERR_OK);
113 }
114
115 /**
116 * @tc.name: TestDisableUsbFail
117 * @tc.desc: Test DisableUsb without enable edm service func.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(UsbManagerProxyTest, TestDisableUsbFail, TestSize.Level1)
121 {
122 Utils::SetEdmServiceDisable();
123 OHOS::AppExecFwk::ElementName admin;
124 admin.SetBundleName(ADMIN_PACKAGENAME);
125 int32_t ret = proxy_->DisableUsb(admin, true);
126 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
127 }
128
129 /**
130 * @tc.name: TestIsUsbDisabledSuc
131 * @tc.desc: Test IsUsbDisabled func.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(UsbManagerProxyTest, TestIsUsbDisabledSuc, TestSize.Level1)
135 {
136 OHOS::AppExecFwk::ElementName admin;
137 admin.SetBundleName(ADMIN_PACKAGENAME);
138 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
139 .Times(1)
140 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
141
142 bool isDisable = false;
143 int32_t ret = proxy_->IsUsbDisabled(&admin, isDisable);
144 ASSERT_TRUE(ret == ERR_OK);
145 ASSERT_TRUE(isDisable);
146 }
147
148 /**
149 * @tc.name: TestIsUsbDisabledFail
150 * @tc.desc: Test IsUsbDisabled func without enable edm service.
151 * @tc.type: FUNC
152 */
153 HWTEST_F(UsbManagerProxyTest, TestIsUsbDisabledFail, TestSize.Level1)
154 {
155 Utils::SetEdmServiceDisable();
156 OHOS::AppExecFwk::ElementName admin;
157 admin.SetBundleName(ADMIN_PACKAGENAME);
158
159 bool isDisable = false;
160 int32_t ret = proxy_->IsUsbDisabled(&admin, isDisable);
161 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
162 ASSERT_FALSE(isDisable);
163 }
164
165 /**
166 * @tc.name: TestAddAllowedUsbDevicesSuc
167 * @tc.desc: Test AddAllowedUsbDevices success func.
168 * @tc.type: FUNC
169 */
170 HWTEST_F(UsbManagerProxyTest, TestAddAllowedUsbDevicesSuc, TestSize.Level1)
171 {
172 OHOS::AppExecFwk::ElementName admin;
173 admin.SetBundleName(ADMIN_PACKAGENAME);
174 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
175 .Times(1)
176 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
177 std::vector<UsbDeviceId> usbDeviceIds;
178 UsbDeviceId id1;
179 id1.SetVendorId(1);
180 id1.SetProductId(9);
181 usbDeviceIds.push_back(id1);
182 int32_t ret = proxy_->AddAllowedUsbDevices(admin, usbDeviceIds);
183 ASSERT_TRUE(ret == ERR_OK);
184 }
185
186 /**
187 * @tc.name: TestAddAllowedUsbDevicesFail
188 * @tc.desc: Test AddAllowedUsbDevices without enable edm service func.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(UsbManagerProxyTest, TestAddAllowedUsbDevicesFail, TestSize.Level1)
192 {
193 Utils::SetEdmServiceDisable();
194 OHOS::AppExecFwk::ElementName admin;
195 admin.SetBundleName(ADMIN_PACKAGENAME);
196 std::vector<UsbDeviceId> usbDeviceIds;
197 UsbDeviceId id1;
198 id1.SetVendorId(1);
199 id1.SetProductId(9);
200 usbDeviceIds.push_back(id1);
201 int32_t ret = proxy_->AddAllowedUsbDevices(admin, usbDeviceIds);
202 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
203 }
204
205 /**
206 * @tc.name: TestRemoveAllowedUsbDevicesSuc
207 * @tc.desc: Test RemoveAllowedUsbDevices success func.
208 * @tc.type: FUNC
209 */
210 HWTEST_F(UsbManagerProxyTest, TestRemoveAllowedUsbDevicesSuc, TestSize.Level1)
211 {
212 OHOS::AppExecFwk::ElementName admin;
213 admin.SetBundleName(ADMIN_PACKAGENAME);
214 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
215 .Times(1)
216 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
217 std::vector<UsbDeviceId> usbDeviceIds;
218 UsbDeviceId id1;
219 id1.SetVendorId(1);
220 id1.SetProductId(9);
221 usbDeviceIds.push_back(id1);
222 int32_t ret = proxy_->RemoveAllowedUsbDevices(admin, usbDeviceIds);
223 ASSERT_TRUE(ret == ERR_OK);
224 }
225
226 /**
227 * @tc.name: TestRemoveAllowedUsbDevicesFail
228 * @tc.desc: Test RemoveAllowedUsbDevices without enable edm service func.
229 * @tc.type: FUNC
230 */
231 HWTEST_F(UsbManagerProxyTest, TestRemoveAllowedUsbDevicesFail, TestSize.Level1)
232 {
233 Utils::SetEdmServiceDisable();
234 OHOS::AppExecFwk::ElementName admin;
235 admin.SetBundleName(ADMIN_PACKAGENAME);
236 std::vector<UsbDeviceId> usbDeviceIds;
237 UsbDeviceId id1;
238 id1.SetVendorId(1);
239 id1.SetProductId(9);
240 usbDeviceIds.push_back(id1);
241 int32_t ret = proxy_->RemoveAllowedUsbDevices(admin, usbDeviceIds);
242 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
243 }
244
245 /**
246 * @tc.name: TestGetAllowedUsbDevicesSuc
247 * @tc.desc: Test GetAllowedUsbDevices func.
248 * @tc.type: FUNC
249 */
250 HWTEST_F(UsbManagerProxyTest, TestGetAllowedUsbDevicesSuc, TestSize.Level1)
251 {
252 OHOS::AppExecFwk::ElementName admin;
253 admin.SetBundleName(ADMIN_PACKAGENAME);
254 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
255 .Times(1)
256 .WillOnce(Invoke(object_.GetRefPtr(),
257 &EnterpriseDeviceMgrStubMock::InvokeAllowedUsbDevicesSendRequestGetPolicy));
258
259 std::vector<UsbDeviceId> usbDeviceIds;
260 int32_t ret = proxy_->GetAllowedUsbDevices(admin, usbDeviceIds);
261 ASSERT_TRUE(ret == ERR_OK);
262 ASSERT_TRUE(usbDeviceIds.size() == 1);
263 }
264
265 /**
266 * @tc.name: TestGetAllowedUsbDevicesFail
267 * @tc.desc: Test GetAllowedUsbDevices func without enable edm service.
268 * @tc.type: FUNC
269 */
270 HWTEST_F(UsbManagerProxyTest, TestGetAllowedUsbDevicesFail, TestSize.Level1)
271 {
272 Utils::SetEdmServiceDisable();
273 OHOS::AppExecFwk::ElementName admin;
274 admin.SetBundleName(ADMIN_PACKAGENAME);
275 std::vector<UsbDeviceId> usbDeviceIds;
276 int32_t ret = proxy_->GetAllowedUsbDevices(admin, usbDeviceIds);
277 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
278 ASSERT_TRUE(usbDeviceIds.empty());
279 }
280
281 /**
282 * @tc.name: TestSetUsbStorageDeviceAccessPolicySuc
283 * @tc.desc: Test SetUsbStorageDeviceAccessPolicy success func.
284 * @tc.type: FUNC
285 */
286 HWTEST_F(UsbManagerProxyTest, TestSetUsbStorageDeviceAccessPolicySuc, TestSize.Level1)
287 {
288 OHOS::AppExecFwk::ElementName admin;
289 admin.SetBundleName(ADMIN_PACKAGENAME);
290 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
291 .Times(1)
292 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
293 int32_t ret = proxy_->SetUsbStorageDeviceAccessPolicy(admin, 2);
294 ASSERT_TRUE(ret == ERR_OK);
295 }
296
297 /**
298 * @tc.name: TestSetUsbStorageDeviceAccessPolicyFail
299 * @tc.desc: Test SetUsbStorageDeviceAccessPolicy without enable edm service func.
300 * @tc.type: FUNC
301 */
302 HWTEST_F(UsbManagerProxyTest, TestSetUsbStorageDeviceAccessPolicyFail, TestSize.Level1)
303 {
304 Utils::SetEdmServiceDisable();
305 OHOS::AppExecFwk::ElementName admin;
306 admin.SetBundleName(ADMIN_PACKAGENAME);
307 int32_t ret = proxy_->SetUsbStorageDeviceAccessPolicy(admin, 2);
308 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
309 }
310
311 /**
312 * @tc.name: TestGetUsbStorageDeviceAccessPolicySuc
313 * @tc.desc: Test GetUsbStorageDeviceAccessPolicy func.
314 * @tc.type: FUNC
315 */
316 HWTEST_F(UsbManagerProxyTest, TestGetUsbStorageDeviceAccessPolicySuc, TestSize.Level1)
317 {
318 OHOS::AppExecFwk::ElementName admin;
319 admin.SetBundleName(ADMIN_PACKAGENAME);
320 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
321 .Times(1)
322 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeIntSendRequestGetPolicy));
323
324 int32_t policy = 0;
325 int32_t ret = proxy_->GetUsbStorageDeviceAccessPolicy(admin, policy);
326 ASSERT_TRUE(ret == ERR_OK);
327 ASSERT_TRUE(policy == 0);
328 }
329
330 /**
331 * @tc.name: TestGetUsbStorageDeviceAccessPolicyFail
332 * @tc.desc: Test GetUsbStorageDeviceAccessPolicy func without enable edm service.
333 * @tc.type: FUNC
334 */
335 HWTEST_F(UsbManagerProxyTest, TestGetUsbStorageDeviceAccessPolicyFail, TestSize.Level1)
336 {
337 Utils::SetEdmServiceDisable();
338 OHOS::AppExecFwk::ElementName admin;
339 admin.SetBundleName(ADMIN_PACKAGENAME);
340 int32_t policy = 0;
341 int32_t ret = proxy_->GetUsbStorageDeviceAccessPolicy(admin, policy);
342 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
343 }
344 } // namespace TEST
345 } // namespace EDM
346 } // namespace OHOS
347