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 class RestrictionsProxyTest : public testing::Test {
33 protected:
34 void SetUp() override;
35
36 void TearDown() override;
37
38 static void TearDownTestSuite(void);
39 std::shared_ptr<RestrictionsProxy> proxy_ = nullptr;
40 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
41 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
42 };
43
SetUp()44 void RestrictionsProxyTest::SetUp()
45 {
46 proxy_ = RestrictionsProxy::GetRestrictionsProxy();
47 edmSysManager_ = std::make_shared<EdmSysManager>();
48 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
49 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
50 Utils::SetEdmServiceEnable();
51 }
52
TearDown()53 void RestrictionsProxyTest::TearDown()
54 {
55 proxy_.reset();
56 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
57 object_ = nullptr;
58 Utils::SetEdmServiceDisable();
59 }
60
TearDownTestSuite()61 void RestrictionsProxyTest::TearDownTestSuite()
62 {
63 ASSERT_FALSE(Utils::GetEdmServiceState());
64 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
65 }
66
67 /**
68 * @tc.name: TestSetDisallowedPolicySuc
69 * @tc.desc: Test SetDisallowedPolicy success func.
70 * @tc.type: FUNC
71 */
72 HWTEST_F(RestrictionsProxyTest, TestSetDisallowedPolicySuc, TestSize.Level1)
73 {
74 OHOS::AppExecFwk::ElementName admin;
75 admin.SetBundleName(ADMIN_PACKAGENAME);
76 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
77 .Times(1)
78 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
79 int32_t ret = proxy_->SetDisallowedPolicy(admin, true, EdmInterfaceCode::DISABLED_HDC);
80 ASSERT_TRUE(ret == ERR_OK);
81 }
82
83 /**
84 * @tc.name: TestSetDisallowedPolicyFail
85 * @tc.desc: Test SetDisallowedPolicy without enable edm service func.
86 * @tc.type: FUNC
87 */
88 HWTEST_F(RestrictionsProxyTest, TestSetDisallowedPolicyFail, TestSize.Level1)
89 {
90 Utils::SetEdmServiceDisable();
91 OHOS::AppExecFwk::ElementName admin;
92 admin.SetBundleName(ADMIN_PACKAGENAME);
93 int32_t ret = proxy_->SetDisallowedPolicy(admin, true, EdmInterfaceCode::DISABLED_HDC);
94 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
95 }
96
97 /**
98 * @tc.name: TestGetDisallowedPolicySuc
99 * @tc.desc: Test GetDisallowedPolicy func.
100 * @tc.type: FUNC
101 */
102 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicySuc, TestSize.Level1)
103 {
104 AppExecFwk::ElementName admin;
105 admin.SetBundleName(ADMIN_PACKAGENAME);
106 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
107 .Times(1)
108 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy));
109 bool result = false;
110 int32_t ret = proxy_->GetDisallowedPolicy(&admin, EdmInterfaceCode::DISABLED_HDC, result);
111 ASSERT_TRUE(ret == ERR_OK);
112 ASSERT_TRUE(result);
113 }
114
115 /**
116 * @tc.name: TestGetDisallowedPolicyFail
117 * @tc.desc: Test GetDisallowedPolicy func.
118 * @tc.type: FUNC
119 */
120 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicyFail, TestSize.Level1)
121 {
122 Utils::SetEdmServiceDisable();
123 AppExecFwk::ElementName admin;
124 admin.SetBundleName(ADMIN_PACKAGENAME);
125 bool result = false;
126 int32_t ret = proxy_->GetDisallowedPolicy(&admin, EdmInterfaceCode::DISABLED_HDC, result);
127 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
128 }
129
130 /**
131 * @tc.name: TestGetDisallowedPolicyNullptr
132 * @tc.desc: Test GetDisallowedPolicy func.
133 * @tc.type: FUNC
134 */
135 HWTEST_F(RestrictionsProxyTest, TestGetDisallowedPolicyNullptr, TestSize.Level1)
136 {
137 Utils::SetEdmServiceDisable();
138 bool result = false;
139 int32_t ret = proxy_->GetDisallowedPolicy(nullptr, EdmInterfaceCode::DISABLED_HDC, result);
140 ASSERT_TRUE(ret == ERR_OK);
141 }
142 } // namespace TEST
143 } // namespace EDM
144 } // namespace OHOS
145