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     ErrCode ret = plugin.OnSetPolicy(policyData, currentData, DEFAULT_USER_ID);
64     ASSERT_TRUE(ret == ERR_OK);
65 }
66 
67 /**
68  * @tc.name: TestOnSetPolicyWithDataAndCurrentData
69  * @tc.desc: Test AllowUsbDevicesPlugin::OnSetPolicy function when policy has value and current data is empty.
70  * @tc.type: FUNC
71  */
72 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnSetPolicyWithDataAndCurrentData, TestSize.Level1)
73 {
74     AllowUsbDevicesPlugin plugin;
75     std::vector<UsbDeviceId> policyData;
76     UsbDeviceId id1;
77     id1.SetVendorId(DEVICE_VID_1);
78     id1.SetProductId(DEVICE_PID_1);
79     policyData.emplace_back(id1);
80     std::vector<UsbDeviceId> currentData;
81     UsbDeviceId id2;
82     id2.SetVendorId(DEVICE_VID_2);
83     id2.SetProductId(DEVICE_PID_2);
84     policyData.emplace_back(id2);
85     ErrCode ret = plugin.OnSetPolicy(policyData, currentData, DEFAULT_USER_ID);
86     ASSERT_TRUE(ret == ERR_OK);
87 }
88 
89 /**
90  * @tc.name: TestOnSetPolicyWithDataWithoutCurrentData
91  * @tc.desc: Test AllowUsbDevicesPlugin::OnSetPolicy function when policy has value and current data.
92  * @tc.type: FUNC
93  */
94 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnSetPolicyWithDataWithoutCurrentData, TestSize.Level1)
95 {
96     AllowUsbDevicesPlugin plugin;
97     std::vector<UsbDeviceId> policyData;
98     UsbDeviceId id;
99     id.SetVendorId(DEVICE_VID_1);
100     id.SetProductId(DEVICE_PID_1);
101     policyData.emplace_back(id);
102     std::vector<UsbDeviceId> currentData;
103     ErrCode ret = plugin.OnSetPolicy(policyData, currentData, DEFAULT_USER_ID);
104     ASSERT_TRUE(ret == ERR_OK);
105 }
106 
107 /**
108  * @tc.name: TestOnGetPolicy
109  * @tc.desc: Test AllowUsbDevicesPlugin::OnGetPolicy function.
110  * @tc.type: FUNC
111  */
112 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnGetPolicy, TestSize.Level1)
113 {
114     std::shared_ptr<IPlugin> plugin = AllowUsbDevicesPlugin::GetPlugin();
115     std::string policyData{""};
116     MessageParcel data;
117     MessageParcel reply;
118     ErrCode ret = plugin->OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
119     int32_t flag = ERR_INVALID_VALUE;
120     ASSERT_TRUE(reply.ReadInt32(flag) && (flag == ERR_OK));
121     ASSERT_TRUE(ret == ERR_OK);
122 }
123 
124 /**
125  * @tc.name: TestOnAdminRemovePolicyEmpty
126  * @tc.desc: Test AllowUsbDevicesPlugin::OnAdminRemove function when policy is true.
127  * @tc.type: FUNC
128  */
129 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnAdminRemovePolicyEmpty, TestSize.Level1)
130 {
131     AllowUsbDevicesPlugin plugin;
132     std::string adminName{"testAdminName"};
133     std::vector<UsbDeviceId> policyData;
134     ErrCode ret = plugin.OnAdminRemove(adminName, policyData, DEFAULT_USER_ID);
135     ASSERT_TRUE(ret == ERR_OK);
136 }
137 
138 /**
139  * @tc.name: TestOnAdminRemoveFalse
140  * @tc.desc: Test AllowUsbDevicesPlugin::OnAdminRemove function when policy is disabled.
141  * @tc.type: FUNC
142  */
143 HWTEST_F(AllowedUsbDevicesPluginTest, TestOnAdminRemoveHasPolicy, TestSize.Level1)
144 {
145     AllowUsbDevicesPlugin plugin;
146     std::string adminName{"testAdminName"};
147     std::vector<UsbDeviceId> policyData;
148     UsbDeviceId id;
149     id.SetVendorId(DEVICE_VID_1);
150     id.SetProductId(DEVICE_PID_1);
151     policyData.emplace_back(id);
152     ErrCode ret = plugin.OnAdminRemove(adminName, policyData, DEFAULT_USER_ID);
153     ASSERT_TRUE(ret == ERR_OK);
154 }
155 } // namespace TEST
156 } // namespace EDM
157 } // namespace OHOS