• 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     OHOS::AppExecFwk::ElementName admin;
169     std::vector<OHOS::AppExecFwk::ElementName> apps;
170     ErrCode ret = applicationManagerProxy_->AddAutoStartApps(admin, apps);
171     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
172 }
173 
174 /**
175  * @tc.name: TestAddAutoStartAppsSuc
176  * @tc.desc: Test AddAutoStartApps success func.
177  * @tc.type: FUNC
178  */
179 HWTEST_F(ApplicationManagerProxyTest, AddAutoStartAppsSuc, TestSize.Level1)
180 {
181     OHOS::AppExecFwk::ElementName admin;
182     OHOS::AppExecFwk::ElementName app;
183     std::vector<OHOS::AppExecFwk::ElementName> apps{app};
184     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
185         .Times(1)
186         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
187     ErrCode ret = applicationManagerProxy_->AddAutoStartApps(admin, apps);
188     ASSERT_TRUE(ret == ERR_OK);
189 }
190 
191 /**
192  * @tc.name: TestRemoveAutoStartAppsFail
193  * @tc.desc: Test RemoveAutoStartApps without enable edm service func.
194  * @tc.type: FUNC
195  */
196 HWTEST_F(ApplicationManagerProxyTest, TestRemoveAutoStartAppsFail, TestSize.Level1)
197 {
198     Utils::SetEdmServiceDisable();
199     OHOS::AppExecFwk::ElementName admin;
200     std::vector<OHOS::AppExecFwk::ElementName> apps;
201     ErrCode ret = applicationManagerProxy_->RemoveAutoStartApps(admin, apps);
202     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
203 }
204 
205 /**
206  * @tc.name: TestRemoveAutoStartAppsSuc
207  * @tc.desc: Test RemoveAutoStartApps success func.
208  * @tc.type: FUNC
209  */
210 HWTEST_F(ApplicationManagerProxyTest, TestRemoveAutoStartAppsSuc, TestSize.Level1)
211 {
212     OHOS::AppExecFwk::ElementName admin;
213     OHOS::AppExecFwk::ElementName app;
214     std::vector<OHOS::AppExecFwk::ElementName> apps{app};
215     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
216         .Times(1)
217         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy));
218     ErrCode ret = applicationManagerProxy_->RemoveAutoStartApps(admin, apps);
219     ASSERT_TRUE(ret == ERR_OK);
220 }
221 
222 /**
223  * @tc.name: TestGetAutoStartAppsSuc
224  * @tc.desc: Test GetAutoStartApps success func.
225  * @tc.type: FUNC
226  */
227 HWTEST_F(ApplicationManagerProxyTest, TestGetAutoStartAppsSuc, TestSize.Level1)
228 {
229     OHOS::AppExecFwk::ElementName admin;
230     std::vector<OHOS::AppExecFwk::ElementName> apps;
231     EXPECT_CALL(*object_, SendRequest(_, _, _, _))
232         .Times(1)
233         .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayElementSendRequestGetPolicy));
234     ErrCode ret = applicationManagerProxy_->GetAutoStartApps(admin, apps);
235     ASSERT_TRUE(ret == ERR_OK);
236     ASSERT_TRUE(apps.size() == 1);
237 }
238 
239 /**
240  * @tc.name: TestGetAutoStartAppsFail
241  * @tc.desc: Test GetAutoStartApps without enable edm service func.
242  * @tc.type: FUNC
243  */
244 HWTEST_F(ApplicationManagerProxyTest, TestGetAutoStartAppsFail, TestSize.Level1)
245 {
246     Utils::SetEdmServiceDisable();
247     OHOS::AppExecFwk::ElementName admin;
248     std::vector<OHOS::AppExecFwk::ElementName> apps;
249     ErrCode ret = applicationManagerProxy_->GetAutoStartApps(admin, apps);
250     ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE);
251 }
252 } // namespace TEST
253 } // namespace EDM
254 } // namespace OHOS
255