• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 
20 #include "restrictions_proxy.h"
21 #include "edm_sys_manager_mock.h"
22 #include "enterprise_device_mgr_stub_mock.h"
23 #include "utils.h"
24 
25 using namespace testing::ext;
26 using namespace testing;
27 
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 const std::string ADMIN_PACKAGENAME = "com.edm.test.demo";
32 const std::string FEATURE_SNAPSHOT_SKIP = "snapshotSkip";
33 class RestrictionsProxyTest : public testing::Test {
34 protected:
35     void SetUp() override;
36 
37     void TearDown() override;
38 
39     static void TearDownTestSuite(void);
40     std::shared_ptr<RestrictionsProxy> proxy_ = nullptr;
41     std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
42     sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
43 };
44 
SetUp()45 void RestrictionsProxyTest::SetUp()
46 {
47     proxy_ = RestrictionsProxy::GetRestrictionsProxy();
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 RestrictionsProxyTest::TearDown()
55 {
56     proxy_.reset();
57     edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
58     object_ = nullptr;
59     Utils::SetEdmServiceDisable();
60 }
61 
TearDownTestSuite()62 void RestrictionsProxyTest::TearDownTestSuite()
63 {
64     ASSERT_FALSE(Utils::GetEdmServiceState());
65     std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
66 }
67 
68 /**
69  * @tc.name: TestSetDisallowedPolicySuc
70  * @tc.desc: Test SetDisallowedPolicy success func.
71  * @tc.type: FUNC
72  */
73 HWTEST_F(RestrictionsProxyTest, TestSetDisallowedPolicySuc, 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::InvokeSendRequestSetPolicy));
80     int32_t ret = proxy_->SetDisallowedPolicy(admin, true, EdmInterfaceCode::DISABLED_HDC);
81     ASSERT_TRUE(ret == ERR_OK);
82 }
83 
84 /**
85  * @tc.name: TestSetDisallowedPolicySuc_01
86  * @tc.desc: Test SetDisallowedPolicy success func.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(RestrictionsProxyTest, TestSetDisallowedPolicySuc_01, TestSize.Level1)
90 {
91     OHOS::AppExecFwk::ElementName admin;
92     admin.SetBundleName(ADMIN_PACKAGENAME);
93     MessageParcel data;
94     data.WriteParcelable(&admin);
95     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
96         .Times(1)
97         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
98     int32_t ret = proxy_->SetDisallowedPolicy(data,  EdmInterfaceCode::DISABLED_HDC);
99     ASSERT_TRUE(ret == ERR_OK);
100 }
101 
102 /**
103  * @tc.name: TestSetDisallowedPolicyFail
104  * @tc.desc: Test SetDisallowedPolicy without enable edm service func.
105  * @tc.type: FUNC
106  */
107 HWTEST_F(RestrictionsProxyTest, TestSetDisallowedPolicyFail, TestSize.Level1)
108 {
109     Utils::SetEdmServiceDisable();
110     OHOS::AppExecFwk::ElementName admin;
111     admin.SetBundleName(ADMIN_PACKAGENAME);
112     int32_t ret = proxy_->SetDisallowedPolicy(admin, true, EdmInterfaceCode::DISABLED_HDC);
113     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
114 }
115 
116 /**
117  * @tc.name: TestSetDisallowedPolicyFail_01
118  * @tc.desc: Test SetDisallowedPolicy without enable edm service func.
119  * @tc.type: FUNC
120  */
121 HWTEST_F(RestrictionsProxyTest, TestSetDisallowedPolicyFail_01, TestSize.Level1)
122 {
123     Utils::SetEdmServiceDisable();
124     OHOS::AppExecFwk::ElementName admin;
125     admin.SetBundleName(ADMIN_PACKAGENAME);
126     MessageParcel data;
127     data.WriteParcelable(&admin);
128     int32_t ret = proxy_->SetDisallowedPolicy(data, EdmInterfaceCode::DISABLED_HDC);
129     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
130 }
131 
132 /**
133  * @tc.name: TestGetDisallowedPolicySuc
134  * @tc.desc: Test GetDisallowedPolicy func.
135  * @tc.type: FUNC
136  */
137 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicySuc, TestSize.Level1)
138 {
139     AppExecFwk::ElementName admin;
140     admin.SetBundleName(ADMIN_PACKAGENAME);
141     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
142         .Times(1)
143         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
144     bool result = false;
145     int32_t ret = proxy_->GetDisallowedPolicy(&admin, EdmInterfaceCode::DISABLED_HDC, result);
146     ASSERT_TRUE(ret == ERR_OK);
147     ASSERT_TRUE(result);
148 }
149 
150 /**
151  * @tc.name: TestGetDisallowedPolicySuc_01
152  * @tc.desc: Test GetDisallowedPolicy func.
153  * @tc.type: FUNC
154  */
155 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicySuc_01, TestSize.Level1)
156 {
157     AppExecFwk::ElementName admin;
158     admin.SetBundleName(ADMIN_PACKAGENAME);
159     MessageParcel data;
160     data.WriteParcelable(&admin);
161     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
162         .Times(1)
163         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
164     bool result = false;
165     int32_t ret = proxy_->GetDisallowedPolicy(data, EdmInterfaceCode::DISABLED_HDC, result);
166     ASSERT_TRUE(ret == ERR_OK);
167     ASSERT_TRUE(result);
168 }
169 
170 /**
171  * @tc.name: TestGetDisallowedPolicyFail
172  * @tc.desc: Test GetDisallowedPolicy func.
173  * @tc.type: FUNC
174  */
175 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicyFail, TestSize.Level1)
176 {
177     Utils::SetEdmServiceDisable();
178     AppExecFwk::ElementName admin;
179     admin.SetBundleName(ADMIN_PACKAGENAME);
180     bool result = false;
181     int32_t ret = proxy_->GetDisallowedPolicy(&admin, EdmInterfaceCode::DISABLED_HDC, result);
182     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
183 }
184 
185 /**
186  * @tc.name: TestGetDisallowedPolicyFail_01
187  * @tc.desc: Test GetDisallowedPolicy func.
188  * @tc.type: FUNC
189  */
190 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicyFail_01, TestSize.Level1)
191 {
192     Utils::SetEdmServiceDisable();
193     AppExecFwk::ElementName admin;
194     admin.SetBundleName(ADMIN_PACKAGENAME);
195     MessageParcel data;
196     data.WriteParcelable(&admin);
197     bool result = false;
198     int32_t ret = proxy_->GetDisallowedPolicy(data, EdmInterfaceCode::DISABLED_HDC, result);
199     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
200 }
201 
202 /**
203  * @tc.name: TestGetDisallowedPolicyNullptr
204  * @tc.desc: Test GetDisallowedPolicy func.
205  * @tc.type: FUNC
206  */
207 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicyNullptr, TestSize.Level1)
208 {
209     Utils::SetEdmServiceDisable();
210     bool result = false;
211     int32_t ret = proxy_->GetDisallowedPolicy(nullptr, EdmInterfaceCode::DISABLED_HDC, result);
212     ASSERT_TRUE(ret == ERR_OK);
213 }
214 
215 /**
216  * @tc.name: TestSetFingerprintAuthDisabledSuc
217  * @tc.desc: Test SetFingerprintAuthDisabled success func.
218  * @tc.type: FUNC
219  */
220 HWTEST_F(RestrictionsProxyTest, TestSetFingerprintAuthDisabledSuc, TestSize.Level1)
221 {
222     OHOS::AppExecFwk::ElementName admin;
223     admin.SetBundleName(ADMIN_PACKAGENAME);
224     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
225         .Times(1)
226         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
227     int32_t ret = proxy_->SetFingerprintAuthDisabled(admin, false);
228     ASSERT_TRUE(ret == ERR_OK);
229 }
230 
231 /**
232  * @tc.name: TestSetFingerprintAuthDisabledFail
233  * @tc.desc: Test SetFingerprintAuthDisabled without enable edm service func.
234  * @tc.type: FUNC
235  */
236 HWTEST_F(RestrictionsProxyTest, TestSetFingerprintAuthDisabledFail, TestSize.Level1)
237 {
238     Utils::SetEdmServiceDisable();
239     OHOS::AppExecFwk::ElementName admin;
240     admin.SetBundleName(ADMIN_PACKAGENAME);
241     int32_t ret = proxy_->SetFingerprintAuthDisabled(admin, false);
242     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
243 }
244 
245 /**
246  * @tc.name: TestIsFingerprintAuthDisabledSuc
247  * @tc.desc: Test IsFingerprintAuthDisabled func.
248  * @tc.type: FUNC
249  */
250 HWTEST_F(RestrictionsProxyTest, TestIsFingerprintAuthDisabledSuc, TestSize.Level1)
251 {
252     AppExecFwk::ElementName admin;
253     admin.SetBundleName(ADMIN_PACKAGENAME);
254     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
255         .Times(1)
256         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
257     bool result = false;
258     int32_t ret = proxy_->IsFingerprintAuthDisabled(&admin, result);
259     ASSERT_TRUE(ret == ERR_OK);
260     ASSERT_TRUE(result);
261 }
262 
263 /**
264  * @tc.name: TestIsFingerprintAuthDisabledFail
265  * @tc.desc: Test IsFingerprintAuthDisabled func.
266  * @tc.type: FUNC
267  */
268 HWTEST_F(RestrictionsProxyTest, TestIsFingerprintAuthDisabledFail, TestSize.Level1)
269 {
270     Utils::SetEdmServiceDisable();
271     AppExecFwk::ElementName admin;
272     admin.SetBundleName(ADMIN_PACKAGENAME);
273     bool result = false;
274     int32_t ret = proxy_->IsFingerprintAuthDisabled(&admin, result);
275     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
276 }
277 
278 /**
279  * @tc.name: TestIsFingerprintAuthDisabledNullptr
280  * @tc.desc: Test IsFingerprintAuthDisabled func.
281  * @tc.type: FUNC
282  */
283 HWTEST_F(RestrictionsProxyTest, TestIsFingerprintAuthDisabledNullptr, TestSize.Level1)
284 {
285     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
286         .Times(1)
287         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
288     bool result = false;
289     int32_t ret = proxy_->IsFingerprintAuthDisabled(nullptr, result);
290     ASSERT_TRUE(ret == ERR_OK);
291     ASSERT_TRUE(result);
292 }
293 
294 /**
295  * @tc.name: TestSetDisallowedPolicyForAccountSuc
296  * @tc.desc: Test SetDisallowedPolicyForAccount success func.
297  * @tc.type: FUNC
298  */
299 HWTEST_F(RestrictionsProxyTest, TestSetDisallowedPolicyForAccountSuc, TestSize.Level1)
300 {
301     OHOS::AppExecFwk::ElementName admin;
302     admin.SetBundleName(ADMIN_PACKAGENAME);
303     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
304         .Times(1)
305         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
306     int32_t ret = proxy_->SetDisallowedPolicyForAccount(admin,
307         EdmInterfaceCode::FINGERPRINT_AUTH, false, WITHOUT_PERMISSION_TAG, 100);
308     ASSERT_TRUE(ret == ERR_OK);
309 }
310 
311 /**
312  * @tc.name: TestSetDisallowedPolicyForAccountFail
313  * @tc.desc: Test SetDisallowedPolicyForAccount without enable edm service func.
314  * @tc.type: FUNC
315  */
316 HWTEST_F(RestrictionsProxyTest, TestSetDisallowedPolicyForAccountFail, TestSize.Level1)
317 {
318     Utils::SetEdmServiceDisable();
319     OHOS::AppExecFwk::ElementName admin;
320     admin.SetBundleName(ADMIN_PACKAGENAME);
321     int32_t ret = proxy_->SetDisallowedPolicyForAccount(admin,
322         EdmInterfaceCode::FINGERPRINT_AUTH, false, WITHOUT_PERMISSION_TAG, 100);
323     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
324 }
325 
326 /**
327  * @tc.name: TestGetDisallowedPolicyForAccountSuc
328  * @tc.desc: Test GetDisallowedPolicyForAccount func.
329  * @tc.type: FUNC
330  */
331 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicyForAccountSuc, TestSize.Level1)
332 {
333     AppExecFwk::ElementName admin;
334     admin.SetBundleName(ADMIN_PACKAGENAME);
335     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
336         .Times(1)
337         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
338     bool result = true;
339     int32_t ret = proxy_->GetDisallowedPolicyForAccount(admin,
340         EdmInterfaceCode::FINGERPRINT_AUTH, result, WITHOUT_PERMISSION_TAG, 100);
341     ASSERT_TRUE(ret == ERR_OK);
342     ASSERT_TRUE(result);
343 }
344 
345 /**
346  * @tc.name: TestGetDisallowedPolicyForAccountFail
347  * @tc.desc: Test GetDisallowedPolicyForAccount func.
348  * @tc.type: FUNC
349  */
350 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicyForAccountFail, TestSize.Level1)
351 {
352     Utils::SetEdmServiceDisable();
353     AppExecFwk::ElementName admin;
354     admin.SetBundleName(ADMIN_PACKAGENAME);
355     bool result = false;
356     int32_t ret = proxy_->GetDisallowedPolicyForAccount(admin,
357         EdmInterfaceCode::FINGERPRINT_AUTH, result, WITHOUT_PERMISSION_TAG, 100);
358     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
359 }
360 
361 /**
362  * @tc.name: TestAddDisallowedListForAccountSuc
363  * @tc.desc: Test AddDisallowedListForAccount success func.
364  * @tc.type: FUNC
365  */
366 HWTEST_F(RestrictionsProxyTest, TestAddDisallowedListForAccountSuc, TestSize.Level1)
367 {
368     OHOS::AppExecFwk::ElementName admin;
369     admin.SetBundleName(ADMIN_PACKAGENAME);
370     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
371         .Times(1)
372         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
373     std::vector<std::string> bundles;
374     bundles.emplace_back("aaaa");
375     bundles.emplace_back("bbbb");
376     int32_t ret = proxy_->AddOrRemoveDisallowedListForAccount(admin, FEATURE_SNAPSHOT_SKIP, bundles, 100, true);
377     ASSERT_TRUE(ret == ERR_OK);
378 }
379 
380 /**
381  * @tc.name: TestRemoveDisallowedListForAccountSucc
382  * @tc.desc: Test RemoveDisallowedListForAccount success func.
383  * @tc.type: FUNC
384  */
385 HWTEST_F(RestrictionsProxyTest, TestRemoveDisallowedListForAccountSucc, TestSize.Level1)
386 {
387     OHOS::AppExecFwk::ElementName admin;
388     admin.SetBundleName(ADMIN_PACKAGENAME);
389     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
390         .Times(1)
391         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
392     std::vector<std::string> bundles;
393     bundles.emplace_back("aaaa");
394     bundles.emplace_back("bbbb");
395     int32_t ret = proxy_->AddOrRemoveDisallowedListForAccount(admin, FEATURE_SNAPSHOT_SKIP, bundles, 100, false);
396     ASSERT_TRUE(ret == ERR_OK);
397 }
398 
399 /**
400  * @tc.name: TestAddDisallowedListForAccountFail
401  * @tc.desc: Test AddDisallowedListForAccount without enable edm service func.
402  * @tc.type: FUNC
403  */
404 HWTEST_F(RestrictionsProxyTest, TestAddDisallowedListForAccountFail, TestSize.Level1)
405 {
406     Utils::SetEdmServiceDisable();
407     OHOS::AppExecFwk::ElementName admin;
408     admin.SetBundleName(ADMIN_PACKAGENAME);
409     std::vector<std::string> bundles;
410     bundles.emplace_back("aaaa");
411     bundles.emplace_back("bbbb");
412     int32_t ret = proxy_->AddOrRemoveDisallowedListForAccount(admin, FEATURE_SNAPSHOT_SKIP, bundles, 100, true);
413     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
414 }
415 
416 /**
417  * @tc.name: TestRemoveDisallowedListForAccountFail
418  * @tc.desc: Test RemoveDisallowedListForAccount without enable edm service func.
419  * @tc.type: FUNC
420  */
421 HWTEST_F(RestrictionsProxyTest, TestRemoveDisallowedListForAccountFail, TestSize.Level1)
422 {
423     Utils::SetEdmServiceDisable();
424     OHOS::AppExecFwk::ElementName admin;
425     admin.SetBundleName(ADMIN_PACKAGENAME);
426     std::vector<std::string> bundles;
427     bundles.emplace_back("aaaa");
428     bundles.emplace_back("bbbb");
429     int32_t ret = proxy_->AddOrRemoveDisallowedListForAccount(admin, FEATURE_SNAPSHOT_SKIP, bundles, 100, false);
430     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
431 }
432 
433 /**
434  * @tc.name: TestGetDisallowedListForAccountSuc
435  * @tc.desc: Test GetDisallowedListForAccount func.
436  * @tc.type: FUNC
437  */
438 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedListForAccountSuc, TestSize.Level1)
439 {
440     AppExecFwk::ElementName admin;
441     admin.SetBundleName(ADMIN_PACKAGENAME);
442     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
443         .Times(1)
444         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy));
445     std::vector<std::string> bundles;
446     int32_t ret = proxy_->GetDisallowedListForAccount(admin, FEATURE_SNAPSHOT_SKIP, 100, bundles);
447     ASSERT_TRUE(ret == ERR_OK);
448     ASSERT_TRUE(bundles.size() == 1);
449     ASSERT_TRUE(bundles[0] == RETURN_STRING);
450 }
451 
452 /**
453  * @tc.name: TestGetDisallowedListForAccountFail
454  * @tc.desc: Test GetDisallowedListForAccount func.
455  * @tc.type: FUNC
456  */
457 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedListForAccountFail, TestSize.Level1)
458 {
459     Utils::SetEdmServiceDisable();
460     AppExecFwk::ElementName admin;
461     admin.SetBundleName(ADMIN_PACKAGENAME);
462     std::vector<std::string> bundles;
463     int32_t ret = proxy_->GetDisallowedListForAccount(admin, FEATURE_SNAPSHOT_SKIP, 100, bundles);
464     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
465 }
466 } // namespace TEST
467 } // namespace EDM
468 } // namespace OHOS
469