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
20 #include "browser_proxy.h"
21 #include "bundle_mgr_host.h"
22 #include "edm_sys_manager_mock.h"
23 #include "enterprise_device_mgr_stub_mock.h"
24 #include "iservice_registry.h"
25 #include "utils.h"
26 #include "edm_log.h"
27
28 using namespace testing::ext;
29 using namespace testing;
30
31 namespace OHOS {
32 namespace EDM {
33 namespace TEST {
34 const std::string TEST_APP_ID = "test_app_id";
35 const std::string TEST_POLICIES = "test_policies";
36 class BrowserProxyTest : public testing::Test {
37 protected:
38 void SetUp() override;
39
40 void TearDown() override;
41
42 static void TearDownTestSuite(void);
43 std::shared_ptr<BrowserProxy> browserProxy_ = nullptr;
44 std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
45 sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
46 sptr<AppExecFwk::BundleMgrHost> bundleManager_ = nullptr;
47 };
48
SetUp()49 void BrowserProxyTest::SetUp()
50 {
51 browserProxy_ = BrowserProxy::GetBrowserProxy();
52 edmSysManager_ = std::make_shared<EdmSysManager>();
53 object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock();
54 edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_);
55
56 bundleManager_ = new (std::nothrow) AppExecFwk::BundleMgrHost();
57 edmSysManager_->RegisterSystemAbilityOfRemoteObject(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, bundleManager_);
58 Utils::SetEdmServiceEnable();
59 }
60
TearDown()61 void BrowserProxyTest::TearDown()
62 {
63 browserProxy_.reset();
64 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
65 edmSysManager_->UnregisterSystemAbilityOfRemoteObject(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
66 object_ = nullptr;
67 bundleManager_ = nullptr;
68 Utils::SetEdmServiceDisable();
69 }
70
TearDownTestSuite()71 void BrowserProxyTest::TearDownTestSuite()
72 {
73 ASSERT_FALSE(Utils::GetEdmServiceState());
74 std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
75 }
76
77 /**
78 * @tc.name: TestSetPoliciesSuc
79 * @tc.desc: Test SetPolicies success func.
80 * @tc.type: FUNC
81 */
82 HWTEST_F(BrowserProxyTest, TestSetPoliciesSuc, TestSize.Level1)
83 {
84 OHOS::AppExecFwk::ElementName admin;
85 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
86 .Times(1)
87 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
88 ErrCode ret = browserProxy_->SetPolicies(admin, TEST_APP_ID, TEST_POLICIES);
89 ASSERT_TRUE(ret == ERR_OK);
90 }
91
92 /**
93 * @tc.name: TestSetPoliciesFail
94 * @tc.desc: Test SetPolicies without enable edm service func.
95 * @tc.type: FUNC
96 */
97 HWTEST_F(BrowserProxyTest, TestSetPoliciesFail, TestSize.Level1)
98 {
99 Utils::SetEdmServiceDisable();
100 OHOS::AppExecFwk::ElementName admin;
101 ErrCode ret = browserProxy_->SetPolicies(admin, TEST_APP_ID, TEST_POLICIES);
102 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
103 }
104
105 /**
106 * @tc.name: TestSetPoliciesParamError
107 * @tc.desc: Test SetPolicies with empty appId.
108 * @tc.type: FUNC
109 */
110 HWTEST_F(BrowserProxyTest, TestSetPoliciesParamError, TestSize.Level1)
111 {
112 Utils::SetEdmServiceDisable();
113 OHOS::AppExecFwk::ElementName admin;
114 ErrCode ret = browserProxy_->SetPolicies(admin, "", TEST_POLICIES);
115 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
116 }
117
118 /**
119 * @tc.name: TestGetPoliciesWithThreeParamsSuc
120 * @tc.desc: Test GetPolicies with three parameters success func.
121 * @tc.type: FUNC
122 */
123 HWTEST_F(BrowserProxyTest, TestGetPoliciesWithThreeParamsSuc, TestSize.Level1)
124 {
125 OHOS::AppExecFwk::ElementName admin;
126 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
127 .Times(1)
128 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy));
129 std::string policies;
130 ErrCode ret = browserProxy_->GetPolicies(admin, TEST_APP_ID, policies);
131 ASSERT_TRUE(ret == ERR_OK);
132 ASSERT_TRUE(policies == RETURN_STRING);
133 }
134
135 /**
136 * @tc.name: TestGetPoliciesWithThreeParamsFail
137 * @tc.desc: Test GetPolicies with three parameters without enable edm service func.
138 * @tc.type: FUNC
139 */
140 HWTEST_F(BrowserProxyTest, TestGetPoliciesWithThreeParamsFail, TestSize.Level1)
141 {
142 Utils::SetEdmServiceDisable();
143 OHOS::AppExecFwk::ElementName admin;
144 std::string policies;
145 ErrCode ret = browserProxy_->GetPolicies(admin, TEST_APP_ID, policies);
146 ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
147 }
148
149 /**
150 * @tc.name: TestGetPoliciesWithTwoParamsSuc
151 * @tc.desc: Test GetPolicies with two parameters success func.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(BrowserProxyTest, TestGetPoliciesWithTwoParamSuc, TestSize.Level1)
155 {
156 OHOS::AppExecFwk::ElementName admin;
157 EXPECT_CALL(*object_, SendRequest(_, _, _, _))
158 .Times(1)
159 .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy));
160 std::string policies;
161 ErrCode ret = browserProxy_->GetPolicies(policies);
162 ASSERT_TRUE(ret == ERR_OK);
163 ASSERT_TRUE(policies == RETURN_STRING);
164 }
165
166 /**
167 * @tc.name: TestGetPoliciesWithTwoParamsFail
168 * @tc.desc: Test GetPolicies with two parameters without enable edm service func.
169 * @tc.type: FUNC
170 */
171 HWTEST_F(BrowserProxyTest, TestGetPoliciesWithTwoParamsFail, TestSize.Level1)
172 {
173 Utils::SetEdmServiceDisable();
174 OHOS::AppExecFwk::ElementName admin;
175 std::string policies;
176 ErrCode ret = browserProxy_->GetPolicies(policies);
177 ASSERT_TRUE(ret == ERR_OK);
178 ASSERT_TRUE(policies.empty());
179 }
180 } // namespace TEST
181 } // namespace EDM
182 } // namespace OHOS
183