• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "application_manager_proxy.h"
22 #include "edm_sys_manager_mock.h"
23 #include "enterprise_device_mgr_stub_mock.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 ApplicationManagerProxyTest : public testing::Test {
34 protected:
35     void SetUp() override;
36 
37     void TearDown() override;
38 
39     static void TearDownTestSuite(void);
40     std::shared_ptr<ApplicationManagerProxy> applicationManagerProxy_ = nullptr;
41     std::shared_ptr<EdmSysManager> edmSysManager_ = nullptr;
42     sptr<EnterpriseDeviceMgrStubMock> object_ = nullptr;
43 };
44 
SetUp()45 void ApplicationManagerProxyTest::SetUp()
46 {
47     applicationManagerProxy_ = ApplicationManagerProxy::GetApplicationManagerProxy();
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 ApplicationManagerProxyTest::TearDown()
55 {
56     applicationManagerProxy_.reset();
57     edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID);
58     object_ = nullptr;
59     Utils::SetEdmServiceDisable();
60 }
61 
TearDownTestSuite()62 void ApplicationManagerProxyTest::TearDownTestSuite()
63 {
64     ASSERT_FALSE(Utils::GetEdmServiceState());
65     std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl;
66 }
67 
68 /**
69  * @tc.name: TestAddDisallowedRunningBundlesSuc
70  * @tc.desc: Test AddDisallowedRunningBundles success func.
71  * @tc.type: FUNC
72  */
73 HWTEST_F(ApplicationManagerProxyTest, AddDisallowedRunningBundlesSuc, TestSize.Level1)
74 {
75     OHOS::AppExecFwk::ElementName admin;
76     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
77     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
78         .Times(1)
79         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
80     ErrCode ret = applicationManagerProxy_->AddDisallowedRunningBundles(admin, bundles, DEFAULT_USER_ID);
81     ASSERT_TRUE(ret == ERR_OK);
82 }
83 
84 /**
85  * @tc.name: TestAddDisallowedRunningBundlesFail
86  * @tc.desc: Test AddDisallowedRunningBundles without enable edm service func.
87  * @tc.type: FUNC
88  */
89 HWTEST_F(ApplicationManagerProxyTest, AddDisallowedRunningBundles, TestSize.Level1)
90 {
91     Utils::SetEdmServiceDisable();
92     OHOS::AppExecFwk::ElementName admin;
93     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
94     ErrCode ret = applicationManagerProxy_->AddDisallowedRunningBundles(admin, bundles, DEFAULT_USER_ID);
95     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
96 }
97 
98 /**
99  * @tc.name: TestRemoveDisallowedRunningBundlesSuc
100  * @tc.desc: Test RemoveDisallowedRunningBundles success func.
101  * @tc.type: FUNC
102  */
103 HWTEST_F(ApplicationManagerProxyTest, RemoveDisallowedRunningBundlesSuc, TestSize.Level1)
104 {
105     OHOS::AppExecFwk::ElementName admin;
106     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
107     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
108         .Times(1)
109         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
110     ErrCode ret = applicationManagerProxy_->RemoveDisallowedRunningBundles(admin, bundles, DEFAULT_USER_ID);
111     ASSERT_TRUE(ret == ERR_OK);
112 }
113 
114 /**
115  * @tc.name: TestRemoveDisallowedRunningBundlesFail
116  * @tc.desc: Test RemoveDisallowedRunningBundles without enable edm service func.
117  * @tc.type: FUNC
118  */
119 HWTEST_F(ApplicationManagerProxyTest, RemoveDisallowedRunningBundlesFail, TestSize.Level1)
120 {
121     Utils::SetEdmServiceDisable();
122     OHOS::AppExecFwk::ElementName admin;
123     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
124     ErrCode ret = applicationManagerProxy_->RemoveDisallowedRunningBundles(admin, bundles, DEFAULT_USER_ID);
125     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
126 }
127 
128 /**
129  * @tc.name: TestGetDisallowedRunningBundlesSuc
130  * @tc.desc: Test GetDisallowedRunningBundles success func.
131  * @tc.type: FUNC
132  */
133 HWTEST_F(ApplicationManagerProxyTest, GetDisallowedRunningBundlesSuc, TestSize.Level1)
134 {
135     OHOS::AppExecFwk::ElementName admin;
136     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
137     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
138         .Times(1)
139         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy));
140     ErrCode ret = applicationManagerProxy_->GetDisallowedRunningBundles(admin, DEFAULT_USER_ID, bundles);
141     ASSERT_TRUE(ret == ERR_OK);
142     ASSERT_TRUE(bundles.size() == 1);
143     ASSERT_TRUE(bundles[0] == RETURN_STRING);
144 }
145 
146 /**
147  * @tc.name: TestGetDisallowedRunningBundlesFail
148  * @tc.desc: Test GetDisallowedRunningBundles without enable edm service func.
149  * @tc.type: FUNC
150  */
151 HWTEST_F(ApplicationManagerProxyTest, GetDisallowedRunningBundlesFail, TestSize.Level1)
152 {
153     Utils::SetEdmServiceDisable();
154     OHOS::AppExecFwk::ElementName admin;
155     std::vector<std::string> bundles = {ADMIN_PACKAGENAME};
156     ErrCode ret = applicationManagerProxy_->GetDisallowedRunningBundles(admin, DEFAULT_USER_ID, bundles);
157     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
158 }
159 
160 /**
161  * @tc.name: TestAddAutoStartAppsFail
162  * @tc.desc: Test AddAutoStartApps without enable edm service func.
163  * @tc.type: FUNC
164  */
165 HWTEST_F(ApplicationManagerProxyTest, TestAddAutoStartAppsFail, TestSize.Level1)
166 {
167     Utils::SetEdmServiceDisable();
168     MessageParcel data;
169     OHOS::AppExecFwk::ElementName admin;
170     std::vector<std::string> apps;
171     data.WriteParcelable(&admin);
172     data.WriteStringVector(apps);
173 
174     ErrCode ret = applicationManagerProxy_->AddOrRemoveAutoStartApps(data, true);
175     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
176 }
177 
178 /**
179  * @tc.name: TestAddAutoStartAppsSuc
180  * @tc.desc: Test AddAutoStartApps success func.
181  * @tc.type: FUNC
182  */
183 HWTEST_F(ApplicationManagerProxyTest, AddAutoStartAppsSuc, TestSize.Level1)
184 {
185     MessageParcel data;
186     OHOS::AppExecFwk::ElementName admin;
187     std::vector<std::string> apps;
188     data.WriteParcelable(&admin);
189     data.WriteStringVector(apps);
190 
191     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
192         .Times(1)
193         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
194     ErrCode ret = applicationManagerProxy_->AddOrRemoveAutoStartApps(data, true);
195     ASSERT_TRUE(ret == ERR_OK);
196 }
197 
198 /**
199  * @tc.name: TestRemoveAutoStartAppsFail
200  * @tc.desc: Test RemoveAutoStartApps without enable edm service func.
201  * @tc.type: FUNC
202  */
203 HWTEST_F(ApplicationManagerProxyTest, TestRemoveAutoStartAppsFail, TestSize.Level1)
204 {
205     Utils::SetEdmServiceDisable();
206     MessageParcel data;
207     OHOS::AppExecFwk::ElementName admin;
208     std::vector<std::string> apps;
209     data.WriteParcelable(&admin);
210     data.WriteStringVector(apps);
211 
212     ErrCode ret = applicationManagerProxy_->AddOrRemoveAutoStartApps(data, false);
213     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
214 }
215 
216 /**
217  * @tc.name: TestRemoveAutoStartAppsSuc
218  * @tc.desc: Test RemoveAutoStartApps success func.
219  * @tc.type: FUNC
220  */
221 HWTEST_F(ApplicationManagerProxyTest, TestRemoveAutoStartAppsSuc, TestSize.Level1)
222 {
223     MessageParcel data;
224     OHOS::AppExecFwk::ElementName admin;
225     std::vector<std::string> apps;
226     data.WriteParcelable(&admin);
227     data.WriteStringVector(apps);
228 
229     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
230         .Times(1)
231         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
232     ErrCode ret = applicationManagerProxy_->AddOrRemoveAutoStartApps(data, false);
233     ASSERT_TRUE(ret == ERR_OK);
234 }
235 
236 /**
237  * @tc.name: TestGetAutoStartAppsSuc
238  * @tc.desc: Test GetAutoStartApps success func.
239  * @tc.type: FUNC
240  */
241 HWTEST_F(ApplicationManagerProxyTest, TestGetAutoStartAppsSuc, TestSize.Level1)
242 {
243     MessageParcel data;
244     std::vector<OHOS::AppExecFwk::ElementName> apps;
245     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
246         .Times(1)
247         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayElementSendRequestGetPolicy));
248     ErrCode ret = applicationManagerProxy_->GetAutoStartApps(data, apps);
249     ASSERT_TRUE(ret == ERR_OK);
250     ASSERT_TRUE(apps.size() == 1);
251 }
252 
253 /**
254  * @tc.name: TestGetAutoStartAppsFail
255  * @tc.desc: Test GetAutoStartApps without enable edm service func.
256  * @tc.type: FUNC
257  */
258 HWTEST_F(ApplicationManagerProxyTest, TestGetAutoStartAppsFail, TestSize.Level1)
259 {
260     Utils::SetEdmServiceDisable();
261     MessageParcel data;
262     std::vector<OHOS::AppExecFwk::ElementName> apps;
263     ErrCode ret = applicationManagerProxy_->GetAutoStartApps(data, apps);
264     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
265 }
266 
267 /**
268  * @tc.name: TestAddKeepAliveAppsFail
269  * @tc.desc: Test AddKeepAliveApps without enable edm service func.
270  * @tc.type: FUNC
271  */
272 HWTEST_F(ApplicationManagerProxyTest, TestAddKeepAliveAppsFail, TestSize.Level1)
273 {
274     Utils::SetEdmServiceDisable();
275     OHOS::AppExecFwk::ElementName admin;
276     std::vector<std::string> keepAliveApps;
277     std::string retMessage;
278     ErrCode ret = applicationManagerProxy_->AddKeepAliveApps(admin, keepAliveApps, DEFAULT_USER_ID, retMessage);
279     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
280 }
281 
282 /**
283  * @tc.name: TestAddKeepAliveAppsSuc
284  * @tc.desc: Test AddKeepAliveApps success func.
285  * @tc.type: FUNC
286  */
287 HWTEST_F(ApplicationManagerProxyTest, TestAddKeepAliveAppsSuc, TestSize.Level1)
288 {
289     OHOS::AppExecFwk::ElementName admin;
290     std::vector<std::string> keepAliveApps;
291     std::string retMessage;
292     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
293         .Times(1)
294         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
295     ErrCode ret = applicationManagerProxy_->AddKeepAliveApps(admin, keepAliveApps, DEFAULT_USER_ID, retMessage);
296     ASSERT_TRUE(ret == ERR_OK);
297 }
298 
299 /**
300  * @tc.name: TestRemoveKeepAliveAppsFail
301  * @tc.desc: Test RemoveKeepAliveApps without enable edm service func.
302  * @tc.type: FUNC
303  */
304 HWTEST_F(ApplicationManagerProxyTest, TestRemoveKeepAliveAppsFail, TestSize.Level1)
305 {
306     Utils::SetEdmServiceDisable();
307     OHOS::AppExecFwk::ElementName admin;
308     std::vector<std::string> keepAliveApps;
309     ErrCode ret = applicationManagerProxy_->RemoveKeepAliveApps(admin, keepAliveApps, DEFAULT_USER_ID);
310     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
311 }
312 
313 /**
314  * @tc.name: TestRemoveKeepAliveAppsSuc
315  * @tc.desc: Test RemoveKeepAliveApps success func.
316  * @tc.type: FUNC
317  */
318 HWTEST_F(ApplicationManagerProxyTest, TestRemoveKeepAliveAppsSuc, TestSize.Level1)
319 {
320     OHOS::AppExecFwk::ElementName admin;
321     std::vector<std::string> keepAliveApps;
322     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
323         .Times(1)
324         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
325     ErrCode ret = applicationManagerProxy_->RemoveKeepAliveApps(admin, keepAliveApps, DEFAULT_USER_ID);
326     ASSERT_TRUE(ret == ERR_OK);
327 }
328 
329 /**
330  * @tc.name: TestGetKeepAliveAppsFail
331  * @tc.desc: Test GetKeepAliveApps without enable edm service func.
332  * @tc.type: FUNC
333  */
334 HWTEST_F(ApplicationManagerProxyTest, TestGetKeepAliveAppsFail, TestSize.Level1)
335 {
336     Utils::SetEdmServiceDisable();
337     OHOS::AppExecFwk::ElementName admin;
338     std::vector<std::string> keepAliveApps;
339     ErrCode ret = applicationManagerProxy_->GetKeepAliveApps(admin, keepAliveApps, DEFAULT_USER_ID);
340     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
341 }
342 
343 /**
344  * @tc.name: TestGetKeepAliveAppsSuc
345  * @tc.desc: Test GetKeepAliveApps success func.
346  * @tc.type: FUNC
347  */
348 HWTEST_F(ApplicationManagerProxyTest, TestGetKeepAliveAppsSuc, TestSize.Level1)
349 {
350     OHOS::AppExecFwk::ElementName admin;
351     std::vector<std::string> keepAliveApps;
352     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
353         .Times(1)
354         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy));
355     ErrCode ret = applicationManagerProxy_->GetKeepAliveApps(admin, keepAliveApps, DEFAULT_USER_ID);
356     ASSERT_TRUE(ret == ERR_OK);
357     ASSERT_TRUE(keepAliveApps.size() == 1);
358 }
359 } // namespace TEST
360 } // namespace EDM
361 } // namespace OHOS
362