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 "allowed_usb_devices_plugin.h"
18 #include "edm_ipc_interface_code.h"
19 #include "iplugin_manager.h"
20 #include "plugin_singleton.h"
21 #include "utils.h"
22
23 using namespace testing::ext;
24 using namespace testing;
25
26 namespace OHOS {
27 namespace EDM {
28 namespace TEST {
29 const int32_t DEVICE_VID_1 = 222;
30 const int32_t DEVICE_PID_1 = 333;
31 const int32_t DEVICE_VID_2 = 444;
32 const int32_t DEVICE_PID_2 = 555;
33
34 class AllowedUsbDevicesPluginTest : public testing::Test {
35 protected:
36 static void SetUpTestSuite(void);
37
38 static void TearDownTestSuite(void);
39 };
40
SetUpTestSuite(void)41 void AllowedUsbDevicesPluginTest::SetUpTestSuite(void)
42 {
43 Utils::SetEdmInitialEnv();
44 }
45
TearDownTestSuite(void)46 void AllowedUsbDevicesPluginTest::TearDownTestSuite(void)
47 {
48 Utils::ResetTokenTypeAndUid();
49 ASSERT_TRUE(Utils::IsOriginalUTEnv());
50 std::cout << "now ut process is original ut env : " << Utils::IsOriginalUTEnv() << std::endl;
51 }
52
53 /**
54 * @tc.name: TestOnSetPolicyEmpty
55 * @tc.desc: Test AllowUsbDevicesPlugin::OnSetPolicy function when policy is empty.
56 * @tc.type: FUNC
57 */
58 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnSetPolicyEmpty, TestSize.Level1)
59 {
60 AllowUsbDevicesPlugin plugin;
61 std::vector<UsbDeviceId> policyData;
62 std::vector<UsbDeviceId> currentData;
63 std::vector<UsbDeviceId> mergeData;
64 ErrCode ret = plugin.OnSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
65 ASSERT_TRUE(ret == ERR_OK);
66 }
67
68 /**
69 * @tc.name: TestOnSetPolicyWithDataAndCurrentData
70 * @tc.desc: Test AllowUsbDevicesPlugin::OnSetPolicy function when policy has value and current data is empty.
71 * @tc.type: FUNC
72 */
73 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnSetPolicyWithDataAndCurrentData, TestSize.Level1)
74 {
75 AllowUsbDevicesPlugin plugin;
76 std::vector<UsbDeviceId> policyData;
77 UsbDeviceId id1;
78 id1.SetVendorId(DEVICE_VID_1);
79 id1.SetProductId(DEVICE_PID_1);
80 policyData.emplace_back(id1);
81 std::vector<UsbDeviceId> currentData;
82 UsbDeviceId id2;
83 id2.SetVendorId(DEVICE_VID_2);
84 id2.SetProductId(DEVICE_PID_2);
85 policyData.emplace_back(id2);
86 std::vector<UsbDeviceId> mergeData;
87 ErrCode ret = plugin.OnSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
88 ASSERT_TRUE(ret == ERR_OK);
89 }
90
91 /**
92 * @tc.name: TestOnSetPolicyWithDataWithoutCurrentData
93 * @tc.desc: Test AllowUsbDevicesPlugin::OnSetPolicy function when policy has value and current data.
94 * @tc.type: FUNC
95 */
96 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnSetPolicyWithDataWithoutCurrentData, TestSize.Level1)
97 {
98 AllowUsbDevicesPlugin plugin;
99 std::vector<UsbDeviceId> policyData;
100 UsbDeviceId id;
101 id.SetVendorId(DEVICE_VID_1);
102 id.SetProductId(DEVICE_PID_1);
103 policyData.emplace_back(id);
104 std::vector<UsbDeviceId> currentData;
105 std::vector<UsbDeviceId> mergeData;
106 ErrCode ret = plugin.OnSetPolicy(policyData, currentData, mergeData, DEFAULT_USER_ID);
107 ASSERT_TRUE(ret == ERR_OK);
108 }
109
110 /**
111 * @tc.name: TestOnAdminRemovePolicyEmpty
112 * @tc.desc: Test AllowUsbDevicesPlugin::OnAdminRemove function when policy is true.
113 * @tc.type: FUNC
114 */
115 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnAdminRemovePolicyEmpty, TestSize.Level1)
116 {
117 AllowUsbDevicesPlugin plugin;
118 std::string adminName{"testAdminName"};
119 std::vector<UsbDeviceId> policyData;
120 std::vector<UsbDeviceId> mergeData;
121 ErrCode ret = plugin.OnAdminRemove(adminName, policyData, mergeData, DEFAULT_USER_ID);
122 ASSERT_TRUE(ret == ERR_OK);
123 }
124
125 /**
126 * @tc.name: TestOnAdminRemoveFalse
127 * @tc.desc: Test AllowUsbDevicesPlugin::OnAdminRemove function when policy is disabled.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnAdminRemoveHasPolicy, TestSize.Level1)
131 {
132 AllowUsbDevicesPlugin plugin;
133 std::string adminName{"testAdminName"};
134 std::vector<UsbDeviceId> policyData;
135 UsbDeviceId id;
136 id.SetVendorId(DEVICE_VID_1);
137 id.SetProductId(DEVICE_PID_1);
138 policyData.emplace_back(id);
139 std::vector<UsbDeviceId> mergeData;
140 ErrCode ret = plugin.OnAdminRemove(adminName, policyData, mergeData, DEFAULT_USER_ID);
141 ASSERT_TRUE(ret == ERR_OK);
142 }
143 } // namespace TEST
144 } // namespace EDM
145 } // namespace OHOS