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