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 "disallowed_running_bundles_plugin_test.h"
17
18 #define protected public
19 #include "disallowed_running_bundles_plugin.h"
20 #undef protected
21
22 #include "bundle_mgr_proxy.h"
23 #include "edm_constants.h"
24 #include "edm_sys_manager.h"
25 #include "if_system_ability_manager.h"
26 #include "iremote_stub.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29 #include "utils.h"
30
31 using namespace testing::ext;
32
33 namespace OHOS {
34 namespace EDM {
35 namespace TEST {
36 const std::string TEST_BUNDLE = "testBundle";
37
SetUpTestSuite(void)38 void DisallowedRunningBundlesPluginTest::SetUpTestSuite(void)
39 {
40 Utils::SetEdmInitialEnv();
41 }
42
TearDownTestSuite(void)43 void DisallowedRunningBundlesPluginTest::TearDownTestSuite(void)
44 {
45 Utils::ResetTokenTypeAndUid();
46 ASSERT_TRUE(Utils::IsOriginalUTEnv());
47 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
48 }
49
50 /**
51 * @tc.name: TestDisallowedRunningBundlesPlugin
52 * @tc.desc: Test DisallowedRunningBundlesPlugin::OnSetPolicy when data is empty.
53 * @tc.type: FUNC
54 */
55 HWTEST_F(DisallowedRunningBundlesPluginTest, TestDisallowedRunningBundlesPlugin001, TestSize.Level1)
56 {
57 DisallowedRunningBundlesPlugin plugin;
58 plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
59 std::vector<std::string> data;
60 std::vector<std::string> currentData;
61 std::vector<std::string> mergeData;
62 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
63 ASSERT_TRUE(ret == ERR_OK);
64 }
65
66 /**
67 * @tc.name: TestDisallowedRunningBundlesPlugin
68 * @tc.desc: Test DisallowedRunningBundlesPlugin::OnSetPolicy when data.size() > MAX_SIZE.
69 * @tc.type: FUNC
70 */
71 HWTEST_F(DisallowedRunningBundlesPluginTest, TestDisallowedRunningBundlesPlugin002, TestSize.Level1)
72 {
73 DisallowedRunningBundlesPlugin plugin;
74 plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
75 std::vector<std::string> data(EdmConstants::APPID_MAX_SIZE + 1, TEST_BUNDLE);
76 std::vector<std::string> currentData;
77 std::vector<std::string> mergeData;
78 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
79 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
80 }
81
82 /**
83 * @tc.name: TestDisallowedRunningBundlesPlugin
84 * @tc.desc: Test DisallowedRunningBundlesPlugin::OnSetPolicy func.
85 * @tc.type: FUNC
86 */
87 HWTEST_F(DisallowedRunningBundlesPluginTest, TestDisallowedRunningBundlesPlugin003, TestSize.Level1)
88 {
89 Utils::ResetTokenTypeAndUid();
90 DisallowedRunningBundlesPlugin plugin;
91 plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
92 std::vector<std::string> data = { TEST_BUNDLE };
93 std::vector<std::string> currentData;
94 std::vector<std::string> mergeData;
95 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
96 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
97 Utils::SetEdmInitialEnv();
98 }
99
100 /**
101 * @tc.name: TestDisallowedRunningBundlesPlugin
102 * @tc.desc: Test DisallowedRunningBundlesPlugin::OnGetPolicy function.
103 * @tc.type: FUNC
104 */
105 HWTEST_F(DisallowedRunningBundlesPluginTest, TestDisallowedRunningBundlesPlugin004, TestSize.Level1)
106 {
107 DisallowedRunningBundlesPlugin plugin;
108 plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
109 std::string policyData;
110 MessageParcel data;
111 MessageParcel reply;
112 ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
113 ASSERT_TRUE(ret == ERR_OK);
114 }
115
116 /**
117 * @tc.name: TestDisallowedRunningBundlesPlugin
118 * @tc.desc: Test DisallowedRunningBundlesPlugin::OnRemovePolicy when data is empty.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(DisallowedRunningBundlesPluginTest, TestDisallowedRunningBundlesPlugin005, TestSize.Level1)
122 {
123 DisallowedRunningBundlesPlugin plugin;
124 plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
125 std::vector<std::string> data;
126 std::vector<std::string> currentData;
127 std::vector<std::string> mergeData;
128 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
129 ASSERT_TRUE(ret == ERR_OK);
130 }
131
132 /**
133 * @tc.name: TestDisallowedRunningBundlesPlugin
134 * @tc.desc: Test DisallowedRunningBundlesPlugin::OnRemovePolicy func when it is SYSTEM_ABNORMALLY.
135 * @tc.type: FUNC
136 */
137 HWTEST_F(DisallowedRunningBundlesPluginTest, TestDisallowedRunningBundlesPlugin006, TestSize.Level1)
138 {
139 Utils::ResetTokenTypeAndUid();
140 DisallowedRunningBundlesPlugin plugin;
141 plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
142 std::vector<std::string> data = { TEST_BUNDLE };
143 std::vector<std::string> currentData;
144 std::vector<std::string> mergeData;
145 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
146 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
147 Utils::SetEdmInitialEnv();
148 }
149
150 /**
151 * @tc.name: TestDisallowedRunningBundlesPlugin
152 * @tc.desc: Test DisallowedRunningBundlesPlugin::OnAdminRemoveDone func.
153 * @tc.type: FUNC
154 */
155 HWTEST_F(DisallowedRunningBundlesPluginTest, TestDisallowedRunningBundlesPlugin007, TestSize.Level1)
156 {
157 sptr<AppExecFwk::BundleMgrProxy> bundleMgrProxy = iface_cast<AppExecFwk::BundleMgrProxy>(
158 EdmSysManager::GetRemoteObjectOfSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID));
159 sptr<AppExecFwk::IAppControlMgr> appControlProxy = bundleMgrProxy->GetAppControlProxy();
160 // set policy that "testBundle" is disallowed to run.
161 DisallowedRunningBundlesPlugin plugin;
162 plugin.maxListSize_ = EdmConstants::APPID_MAX_SIZE;
163 std::vector<std::string> data = { TEST_BUNDLE };
164 std::vector<std::string> currentData;
165 std::vector<std::string> mergeData;
166 ErrCode res = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
167 ASSERT_TRUE(res == ERR_OK);
168 ASSERT_TRUE(currentData.size() == 1);
169 ASSERT_TRUE(currentData[0] == TEST_BUNDLE);
170
171 // get current policy.
172 std::vector<std::string> result;
173 res = appControlProxy->GetAppRunningControlRule(DEFAULT_USER_ID, result);
174 ASSERT_TRUE(res == ERR_OK);
175 ASSERT_TRUE(result.size() == 1);
176 ASSERT_TRUE(result[0] == TEST_BUNDLE);
177
178 // remove policy.
179 std::string adminName = TEST_BUNDLE;
180 std::vector<std::string> appIds = { TEST_BUNDLE };
181 mergeData.clear();
182 plugin.OnBasicAdminRemove(adminName, appIds, mergeData, DEFAULT_USER_ID);
183
184 // get current policy.
185 result.clear();
186 res = appControlProxy->GetAppRunningControlRule(DEFAULT_USER_ID, result);
187 ASSERT_TRUE(res == ERR_OK);
188 ASSERT_TRUE(result.size() == 0);
189 }
190 } // namespace TEST
191 } // namespace EDM
192 } // namespace OHOS