1 /*
2 * Copyright (c) 2024 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 "disallowed_usb_devices_plugin.h"
18 #include "edm_ipc_interface_code.h"
19 #include "utils.h"
20
21 using namespace testing::ext;
22 using namespace testing;
23
24 namespace OHOS {
25 namespace EDM {
26 namespace TEST {
27 const int32_t BASE_CLASS = 3;
28 const int32_t SUB_CLASS = 1;
29 const int32_t PROTOCOL = 2;
30 const std::string TEST_POLICY_DATA = "[{\"baseClass\":3,\"subClass\":1,\"protocol\":2,\"isDeviceType\":false}]";
31 const std::string TEST_CURRENT_DATA = "[{\"baseClass\":8,\"subClass\":3,\"protocol\":2,\"isDeviceType\":false}]";
32
33 class DisallowedUsbDevicesPluginTest : public testing::Test {
34 protected:
35 static void SetUpTestSuite(void);
36
37 static void TearDownTestSuite(void);
38 };
39
SetUpTestSuite(void)40 void DisallowedUsbDevicesPluginTest::SetUpTestSuite(void)
41 {
42 Utils::SetEdmInitialEnv();
43 }
44
TearDownTestSuite(void)45 void DisallowedUsbDevicesPluginTest::TearDownTestSuite(void)
46 {
47 Utils::ResetTokenTypeAndUid();
48 ASSERT_TRUE(Utils::IsOriginalUTEnv());
49 std::cout << "now ut process is original ut env : " << Utils::IsOriginalUTEnv() << std::endl;
50 }
51
52 /**
53 * @tc.name: TestOnHandlePolicyDataSetAllEmpty
54 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when data and current data is empty
55 * @tc.type: FUNC
56 */
57 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicyDataSetAllEmpty, TestSize.Level1)
58 {
59 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
60 std::uint32_t funcCode =
61 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
62 MessageParcel data;
63 data.WriteUint32(0);
64 MessageParcel reply;
65 HandlePolicyData handlePolicyData{"", false};
66 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
67 ASSERT_TRUE(ret == ERR_OK);
68 ASSERT_TRUE(handlePolicyData.policyData == "");
69 ASSERT_FALSE(handlePolicyData.isChanged);
70 }
71
72 /**
73 * @tc.name: TestOnHandlePolicySetCurrentDataEmpty
74 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when current data is empty
75 * @tc.type: FUNC
76 */
77 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicySetCurrentDataEmpty, TestSize.Level1)
78 {
79 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
80 std::uint32_t funcCode =
81 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
82 MessageParcel data;
83 std::vector<USB::UsbDeviceType> usbDeviceTypes;
84 USB::UsbDeviceType type;
85 type.baseClass = BASE_CLASS;
86 type.subClass = SUB_CLASS;
87 type.protocol = PROTOCOL;
88 type.isDeviceType = false;
89 usbDeviceTypes.emplace_back(type);
90 data.WriteUint32(usbDeviceTypes.size());
__anon266fb5fa0102(const auto usbDeviceType) 91 std::for_each(usbDeviceTypes.begin(), usbDeviceTypes.end(), [&](const auto usbDeviceType) {
92 usbDeviceType.Marshalling(data);
93 });
94 MessageParcel reply;
95 HandlePolicyData handlePolicyData{"", false};
96 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
97 ASSERT_TRUE(ret == ERR_OK);
98 std::string result;
99 std::copy_if(handlePolicyData.policyData.begin(), handlePolicyData.policyData.end(),
__anon266fb5fa0202(char c) 100 std::back_inserter(result), [](char c) {
101 return !std::isspace(static_cast<unsigned char>(c));
102 });
103 ASSERT_TRUE(result == TEST_POLICY_DATA);
104 ASSERT_TRUE(handlePolicyData.isChanged);
105 }
106
107 /**
108 * @tc.name: TestOnHandlePolicySetDataEmpty
109 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when data is empty
110 * @tc.type: FUNC
111 */
112 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicySetDataEmpty, TestSize.Level1)
113 {
114 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
115 std::uint32_t funcCode =
116 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
117 MessageParcel data;
118 data.WriteUint32(0);
119 MessageParcel reply;
120 HandlePolicyData handlePolicyData{TEST_POLICY_DATA, false};
121 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
122 ASSERT_TRUE(ret == ERR_OK);
123 std::string result;
124 std::copy_if(handlePolicyData.policyData.begin(), handlePolicyData.policyData.end(),
__anon266fb5fa0302(char c) 125 std::back_inserter(result), [](char c) {
126 return !std::isspace(static_cast<unsigned char>(c));
127 });
128 ASSERT_TRUE(result == TEST_POLICY_DATA);
129 }
130
131 /**
132 * @tc.name: TestOnHandlePolicySetDataSame
133 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when data is same as current data
134 * @tc.type: FUNC
135 */
136 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicySetDataSame, TestSize.Level1)
137 {
138 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
139 std::uint32_t funcCode =
140 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
141 MessageParcel data;
142 std::vector<USB::UsbDeviceType> usbDeviceTypes;
143 USB::UsbDeviceType type;
144 type.baseClass = BASE_CLASS;
145 type.subClass = SUB_CLASS;
146 type.protocol = PROTOCOL;
147 type.isDeviceType = false;
148 usbDeviceTypes.emplace_back(type);
149 data.WriteUint32(usbDeviceTypes.size());
__anon266fb5fa0402(const auto usbDeviceType) 150 std::for_each(usbDeviceTypes.begin(), usbDeviceTypes.end(), [&](const auto usbDeviceType) {
151 usbDeviceType.Marshalling(data);
152 });
153 MessageParcel reply;
154 HandlePolicyData handlePolicyData{TEST_POLICY_DATA, false};
155 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
156 ASSERT_TRUE(ret == ERR_OK);
157 std::string result;
158 std::copy_if(handlePolicyData.policyData.begin(), handlePolicyData.policyData.end(),
__anon266fb5fa0502(char c) 159 std::back_inserter(result), [](char c) {
160 return !std::isspace(static_cast<unsigned char>(c));
161 });
162 ASSERT_TRUE(result == TEST_POLICY_DATA);
163 }
164
165 /**
166 * @tc.name: TestOnHandlePolicySetDataDiff
167 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when data is different from current data
168 * @tc.type: FUNC
169 */
170 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicySetDataDiff, TestSize.Level1)
171 {
172 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
173 std::uint32_t funcCode =
174 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
175 MessageParcel data;
176 std::vector<USB::UsbDeviceType> usbDeviceTypes;
177 USB::UsbDeviceType type;
178 type.baseClass = BASE_CLASS;
179 type.subClass = SUB_CLASS;
180 type.protocol = PROTOCOL;
181 type.isDeviceType = false;
182 usbDeviceTypes.emplace_back(type);
183 data.WriteUint32(usbDeviceTypes.size());
__anon266fb5fa0602(const auto usbDeviceType) 184 std::for_each(usbDeviceTypes.begin(), usbDeviceTypes.end(), [&](const auto usbDeviceType) {
185 usbDeviceType.Marshalling(data);
186 });
187 MessageParcel reply;
188 HandlePolicyData handlePolicyData{TEST_CURRENT_DATA, false};
189 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
190 ASSERT_TRUE(ret == ERR_OK);
191 ASSERT_TRUE(handlePolicyData.isChanged);
192 }
193
194 /**
195 * @tc.name: TestOnHandlePolicyDataRemoveAllEmpty
196 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when data and current data is empty
197 * @tc.type: FUNC
198 */
199 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicyDataRemoveAllEmpty, TestSize.Level1)
200 {
201 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
202 std::uint32_t funcCode =
203 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
204 MessageParcel data;
205 data.WriteUint32(0);
206 MessageParcel reply;
207 HandlePolicyData handlePolicyData{"", false};
208 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
209 ASSERT_TRUE(ret == ERR_OK);
210 ASSERT_TRUE(handlePolicyData.policyData == "");
211 ASSERT_FALSE(handlePolicyData.isChanged);
212 }
213
214 /**
215 * @tc.name: TestOnHandlePolicyRemoveCurrentDataEmpty
216 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when current data is empty
217 * @tc.type: FUNC
218 */
219 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicyRemoveCurrentDataEmpty, TestSize.Level1)
220 {
221 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
222 std::uint32_t funcCode =
223 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
224 MessageParcel data;
225 std::vector<USB::UsbDeviceType> usbDeviceTypes;
226 USB::UsbDeviceType type;
227 type.baseClass = BASE_CLASS;
228 type.subClass = SUB_CLASS;
229 type.protocol = PROTOCOL;
230 type.isDeviceType = false;
231 usbDeviceTypes.emplace_back(type);
232 data.WriteUint32(usbDeviceTypes.size());
__anon266fb5fa0702(const auto usbDeviceType) 233 std::for_each(usbDeviceTypes.begin(), usbDeviceTypes.end(), [&](const auto usbDeviceType) {
234 usbDeviceType.Marshalling(data);
235 });
236 MessageParcel reply;
237 HandlePolicyData handlePolicyData{"", false};
238 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
239 ASSERT_TRUE(ret == ERR_OK);
240 ASSERT_TRUE(handlePolicyData.policyData == "");
241 ASSERT_FALSE(handlePolicyData.isChanged);
242 }
243
244 /**
245 * @tc.name: TestOnHandlePolicyRemoveDataEmpty
246 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when data is empty
247 * @tc.type: FUNC
248 */
249 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicyRemoveDataEmpty, TestSize.Level1)
250 {
251 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
252 std::uint32_t funcCode =
253 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
254 MessageParcel data;
255 data.WriteUint32(0);
256 MessageParcel reply;
257 HandlePolicyData handlePolicyData{TEST_POLICY_DATA, false};
258 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
259 ASSERT_TRUE(ret == ERR_OK);
260 std::string result;
261 std::copy_if(handlePolicyData.policyData.begin(), handlePolicyData.policyData.end(),
__anon266fb5fa0802(char c) 262 std::back_inserter(result), [](char c) {
263 return !std::isspace(static_cast<unsigned char>(c));
264 });
265 ASSERT_TRUE(result == TEST_POLICY_DATA);
266 }
267
268 /**
269 * @tc.name: TestOnHandlePolicyRemoveDataSame
270 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when data is same as current data
271 * @tc.type: FUNC
272 */
273 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicyRemoveDataSame, TestSize.Level1)
274 {
275 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
276 std::uint32_t funcCode =
277 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
278 MessageParcel data;
279 std::vector<USB::UsbDeviceType> usbDeviceTypes;
280 USB::UsbDeviceType type;
281 type.baseClass = BASE_CLASS;
282 type.subClass = SUB_CLASS;
283 type.protocol = PROTOCOL;
284 type.isDeviceType = false;
285 usbDeviceTypes.emplace_back(type);
286 data.WriteUint32(usbDeviceTypes.size());
__anon266fb5fa0902(const auto usbDeviceType) 287 std::for_each(usbDeviceTypes.begin(), usbDeviceTypes.end(), [&](const auto usbDeviceType) {
288 usbDeviceType.Marshalling(data);
289 });
290 MessageParcel reply;
291 HandlePolicyData handlePolicyData{TEST_POLICY_DATA, false};
292 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
293 ASSERT_TRUE(ret == ERR_OK);
294 ASSERT_TRUE(handlePolicyData.policyData == "");
295 ASSERT_TRUE(handlePolicyData.isChanged);
296 }
297
298 /**
299 * @tc.name: TestOnHandlePolicyRemoveDataDiff
300 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnHandlePolicy when data is different from current data
301 * @tc.type: FUNC
302 */
303 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnHandlePolicyRemoveDataDiff, TestSize.Level1)
304 {
305 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
306 std::uint32_t funcCode =
307 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOWED_USB_DEVICES);
308 MessageParcel data;
309 std::vector<USB::UsbDeviceType> usbDeviceTypes;
310 USB::UsbDeviceType type;
311 type.baseClass = BASE_CLASS;
312 type.subClass = SUB_CLASS;
313 type.protocol = PROTOCOL;
314 type.isDeviceType = false;
315 usbDeviceTypes.emplace_back(type);
316 data.WriteUint32(usbDeviceTypes.size());
__anon266fb5fa0a02(const auto usbDeviceType) 317 std::for_each(usbDeviceTypes.begin(), usbDeviceTypes.end(), [&](const auto usbDeviceType) {
318 usbDeviceType.Marshalling(data);
319 });
320 MessageParcel reply;
321 HandlePolicyData handlePolicyData{TEST_CURRENT_DATA, false};
322 ErrCode ret = plugin->OnHandlePolicy(funcCode, data, reply, handlePolicyData, DEFAULT_USER_ID);
323 ASSERT_TRUE(ret == ERR_OK);
324 std::string result;
325 std::copy_if(handlePolicyData.policyData.begin(), handlePolicyData.policyData.end(),
__anon266fb5fa0b02(char c) 326 std::back_inserter(result), [](char c) {
327 return !std::isspace(static_cast<unsigned char>(c));
328 });
329 ASSERT_TRUE(result == TEST_CURRENT_DATA);
330 }
331
332 /**
333 * @tc.name: TestOnGetPolicyEmpty
334 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnGetPolicy function when data is empty.
335 * @tc.type: FUNC
336 */
337 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnGetPolicyEmpty, TestSize.Level1)
338 {
339 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
340 std::string policyData{""};
341 MessageParcel data;
342 MessageParcel reply;
343 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
344 int32_t flag = ERR_INVALID_VALUE;
345 ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
346 ASSERT_TRUE(ret == ERR_OK);
347 }
348
349 /**
350 * @tc.name: TestOnGetPolicySuc
351 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnGetPolicy function when data is not empty.
352 * @tc.type: FUNC
353 */
354 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnGetPolicySuc, TestSize.Level1)
355 {
356 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
357 std::string policyData{TEST_POLICY_DATA};
358 MessageParcel data;
359 MessageParcel reply;
360 ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
361 int32_t flag = ERR_INVALID_VALUE;
362 ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
363 ASSERT_TRUE(ret == ERR_OK);
364 }
365
366 /**
367 * @tc.name: TestOnAdminRemovePolicyEmpty
368 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnAdminRemove function when policy is true.
369 * @tc.type: FUNC
370 */
371 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnAdminRemovePolicyEmpty, TestSize.Level1)
372 {
373 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
374 std::string adminName{"testAdminName"};
375 std::string policyData{""};
376 ErrCode ret = plugin->OnAdminRemove(adminName, policyData, DEFAULT_USER_ID);
377 ASSERT_TRUE(ret == ERR_OK);
378 }
379
380 /**
381 * @tc.name: TestOnAdminRemoveFalse
382 * @tc.desc: Test DisallowedUsbDevicesPluginTest::OnAdminRemove function when policy is disabled.
383 * @tc.type: FUNC
384 */
385 HWTEST_F(DisallowedUsbDevicesPluginTest, TestOnAdminRemoveHasPolicy, TestSize.Level1)
386 {
387 std::shared_ptr<DisallowedUsbDevicesPlugin> plugin = std::make_shared<DisallowedUsbDevicesPlugin>();
388 std::string adminName{"testAdminName"};
389 std::string policyData{TEST_POLICY_DATA};
390 ErrCode ret = plugin->OnAdminRemove(adminName, policyData, DEFAULT_USER_ID);
391 ASSERT_TRUE(ret == ERR_OK);
392 }
393 } // namespace TEST
394 } // namespace EDM
395 } // namespace OHOS