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 <csignal>
17 #include <iostream>
18 #include <strings.h>
19 #include <vector>
20 #include <map>
21 #include <semaphore.h>
22 #include "cJSON.h"
23 #include "usb_dfx_test.h"
24 
25 #include "ashmem.h"
26 #include "securec.h"
27 #include "delayed_sp_singleton.h"
28 #include "hilog_wrapper.h"
29 #include "if_system_ability_manager.h"
30 #include "ipc_skeleton.h"
31 #include "iservice_registry.h"
32 #include "string_ex.h"
33 #include "system_ability_definition.h"
34 #include "usb_common_test.h"
35 #include "usb_errors.h"
36 #include "usb_srv_client.h"
37 #include "usb_srv_support.h"
38 #include "common_event_manager.h"
39 #include "common_event_support.h"
40 
41 using namespace OHOS::EventFwk;
42 using namespace testing::ext;
43 using namespace OHOS::USB;
44 using namespace OHOS;
45 using namespace OHOS::HDI::Usb::V1_0;
46 using namespace OHOS::USB::Common;
47 
48 
49 namespace OHOS {
50 namespace USB {
51 namespace USBDFX {
52 constexpr int32_t USB_BUS_NUM_INVALID = -1;
53 constexpr int32_t USB_DEV_ADDR_INVALID = -1;
54 constexpr uint32_t USB_ENDPOINT_DIR_OUT = 0;
55 constexpr uint32_t USB_ENDPOINT_DIR_IN = 0x80;
56 constexpr uint32_t ASHMEM_MAX_SIZE = 1024;
57 constexpr uint32_t MEM_DATA = 1024 * 1024;
58 sem_t UsbDfxTest::testSem_ {};
59 
InitAshmemOne(sptr<Ashmem> & asmptr,int32_t asmSize,uint8_t rflg)60 static int32_t InitAshmemOne(sptr<Ashmem> &asmptr, int32_t asmSize, uint8_t rflg)
61 {
62     asmptr = Ashmem::CreateAshmem("ttashmem000", asmSize);
63     if (asmptr == nullptr) {
64         USB_HILOGE(MODULE_USB_SERVICE, "InitAshmemOne CreateAshmem failed");
65         return UEC_SERVICE_NO_MEMORY;
66     }
67 
68     asmptr->MapReadAndWriteAshmem();
69 
70     if (rflg == 0) {
71         uint8_t tdata[ASHMEM_MAX_SIZE];
72         int32_t offset = 0;
73         int32_t tlen = 0;
74         int32_t retSafe = memset_s(tdata, sizeof(tdata), 'Y', ASHMEM_MAX_SIZE);
75         if (retSafe != EOK) {
76             USB_HILOGE(MODULE_USB_SERVICE, "InitAshmemOne memset_s failed");
77             return UEC_SERVICE_NO_MEMORY;
78         }
79         while (offset < asmSize) {
80             tlen = (asmSize - offset) < ASHMEM_MAX_SIZE ? (asmSize - offset) : ASHMEM_MAX_SIZE;
81             asmptr->WriteToAshmem(tdata, tlen, offset);
82             offset += tlen;
83         }
84     }
85 
86     return 0;
87 }
88 
SetUpTestCase(void)89 void UsbDfxTest::SetUpTestCase(void)
90 {
91     UsbCommonTest::GrantPermissionSysNative();
92     USB_HILOGI(MODULE_USB_SERVICE, "Start UsbDfxTest");
93 }
94 
TearDownTestCase(void)95 void UsbDfxTest::TearDownTestCase(void)
96 {
97     USB_HILOGI(MODULE_USB_SERVICE, "End UsbDfxTest");
98 }
99 
SetUp(void)100 void UsbDfxTest::SetUp(void) {}
101 
TearDown(void)102 void UsbDfxTest::TearDown(void) {}
103 
104 class UsbSubscriberTest : public CommonEventSubscriber {
105 public:
UsbSubscriberTest(const CommonEventSubscribeInfo & sp)106     explicit UsbSubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) {}
107 
OnReceiveEvent(const CommonEventData & data)108     void OnReceiveEvent(const CommonEventData &data) override
109     {
110         USB_HILOGI(MODULE_USB_SERVICE, "recv event ok");
111         auto &want = data.GetWant();
112         if (want.GetAction() == CommonEventSupport::COMMON_EVENT_USB_STATE) {
113             eventDatas[CommonEventSupport::COMMON_EVENT_USB_STATE] = 1;
114         } else if (want.GetAction() == CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED) {
115             eventDatas[CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED] = 1;
116         }
117         sem_post(&UsbDfxTest::testSem_);
118     }
119 
PrintPromptMsg()120     bool PrintPromptMsg()
121     {
122         auto usbStates = eventDatas.find(CommonEventSupport::COMMON_EVENT_USB_STATE);
123         auto attachStates = eventDatas.find(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
124         if (usbStates != eventDatas.end() && attachStates != eventDatas.end()) {
125             std::cout<< "test ok" << std::endl;
126             return true;
127         } else if (usbStates == eventDatas.end()) {
128             std::cout<< "please connect or disconnect the gadget to some host" << std::endl;
129         } else if (attachStates == eventDatas.end()) {
130             std::cout << "please connect or disconnect a device to the host" << std::endl;
131         }
132         return false;
133     }
GetMapSize()134     int32_t GetMapSize()
135     {
136         return eventDatas.size();
137     }
138 
139 private:
140     std::map<std::string, int32_t> eventDatas;
141 };
142 
143 /**
144  * @tc.name: ReportSysEvent001
145  * @tc.desc: Trigger the dot event PLUG_IN_OUT_HOST_MODE, PLUG_IN_OUT_DEVICE_MODE,
146  * @tc.type: FUNC
147  */
148 HWTEST_F(UsbDfxTest, ReportSysEvent001, TestSize.Level1)
149 {
150     MatchingSkills matchingSkills;
151     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_STATE);
152     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
153     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
154     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
155     std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
156     CommonEventManager::SubscribeCommonEvent(subscriber);
157 
158     while (!subscriber->PrintPromptMsg())
159     {
160         sem_wait(&UsbDfxTest::testSem_);
161     }
162 
163     ASSERT_NE(subscriber->GetMapSize(), 0);
164 }
165 
166 /**
167  * @tc.name: ReportSysEvent002
168  * @tc.desc: Trigger the dot event FUNCTION_CHANGED, PORT_ROLE_CHANGED
169  * @tc.type: FUNC
170  */
171 HWTEST_F(UsbDfxTest, GetCurrentFunctions002, TestSize.Level1)
172 {
173     std::cout << "please connect device, press enter to continue" << std::endl;
174     int32_t c;
175     while ((c = getchar()) != '\n' && c != EOF) {
176         std::cout << "please connect device, press enter to continue" << std::endl;
177     }
178     int32_t ret = 0;
179     USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ReportSysEvent002");
180     auto &UsbSrvClient = UsbSrvClient::GetInstance();
181     int32_t funcs = static_cast<int32_t>(UsbSrvSupport::FUNCTION_NONE);
182     UsbSrvClient.GetCurrentFunctions(funcs);
183     USB_HILOGI(MODULE_USB_SERVICE, "UsbDfxTest::ret=%{public}d", ret);
184     int32_t isok = UsbSrvClient.SetCurrentFunctions(funcs);
185     USB_HILOGI(MODULE_USB_SERVICE, "UsbDfxTest::SetCurrentFunctions=%{public}d", isok);
186 
187     UsbSrvClient.SetPortRole(
188         UsbSrvSupport::PORT_MODE_DEVICE, UsbSrvSupport::POWER_ROLE_SOURCE, UsbSrvSupport::DATA_ROLE_HOST);
189     USB_HILOGI(MODULE_USB_SERVICE, "UsbDfxTest::status=%{public}d", ret);
190     UsbCommonTest::SwitchErrCode(ret);
191     ASSERT_EQ(0, ret);
192 
193     USB_HILOGI(MODULE_USB_SERVICE, "Case End : ReportSysEvent002");
194 }
195 
196 /**
197  * @tc.name: ReportSysEvent003
198  * @tc.desc: Trigger the dot event USB_TRANSFOR_FAULT
199  * @tc.type: FUNC
200  */
201 HWTEST_F(UsbDfxTest, GetCurrentFunctions003, TestSize.Level1)
202 {
203     int32_t ret = 0;
204     USB_HILOGI(MODULE_USB_SERVICE, "Case Start : ReportSysEvent003.");
205     UsbCommonTest::GrantSysNoPermissionNative();
206     auto &UsbSrvClient = UsbSrvClient::GetInstance();
207     int32_t funcs = static_cast<int32_t>(UsbSrvSupport::FUNCTION_NONE);
208     UsbSrvClient.GetCurrentFunctions(funcs);
209     UsbSrvClient.SetCurrentFunctions(funcs);
210 
211     std::vector<UsbPort> usbports;
212     UsbSrvClient.GetPorts(usbports);
213 
214     UsbSrvClient.SetPortRole(
215         UsbSrvSupport::PORT_MODE_DEVICE, UsbSrvSupport::POWER_ROLE_SOURCE, UsbSrvSupport::DATA_ROLE_HOST);
216 
217     UsbCommonTest::GrantPermissionSysNative();
218     USB_HILOGI(MODULE_USB_SERVICE, "UsbDfxTest::ret=%{public}d", ret);
219     ASSERT_EQ(ret, 0);
220     UsbDevice device;
221     device.SetBusNum(USB_BUS_NUM_INVALID);
222     device.SetDevAddr(USB_DEV_ADDR_INVALID);
223     USBDevicePipe pipe;
224     UsbSrvClient.OpenDevice(device, pipe);
225     vector<uint8_t> buffData;
226     USBEndpoint pointIn(USB_ENDPOINT_DIR_IN, 0, 0, 0);
227     UsbSrvClient.BulkTransfer(pipe, pointIn, buffData, 100);
228     USBEndpoint pointOut(USB_ENDPOINT_DIR_OUT, 0, 0, 0);
229     UsbSrvClient.BulkTransfer(pipe, pointOut, buffData, 100);
230 
231     struct UsbCtrlTransfer ctrldata = {0b10000000, 8, 0, 0, 500};
232     UsbSrvClient.ControlTransfer(pipe, ctrldata, buffData);
233 
234     UsbSrvClient.GetRawDescriptors(pipe, buffData);
235     UsbSrvClient.Close(pipe);
236 
237     sptr<Ashmem> ashmem;
238     uint8_t rflg = 0;
239     InitAshmemOne(ashmem, MEM_DATA, rflg);
240     UsbSrvClient.BulkRead(pipe, pointIn, ashmem);
241     ret = UsbSrvClient.BulkWrite(pipe, pointOut, ashmem);
242 
243     UsbCommonTest::GrantPermissionSysNative();
244     ASSERT_NE(ret, 0);
245     USB_HILOGI(MODULE_USB_SERVICE, "Case End : ReportSysEvent003.");
246 }
247 
248 /**
249  * @tc.name: HiTrace001
250  * @tc.desc: Trigger hitrace
251  * @tc.type: FUNC
252  */
253 HWTEST_F(UsbDfxTest, HiTrace001, TestSize.Level1)
254 {
255     USB_HILOGI(MODULE_USB_SERVICE, "Case Start : HiTrace001.");
256     auto &UsbSrvClient = UsbSrvClient::GetInstance();
257     int32_t funcs = static_cast<int32_t>(UsbSrvSupport::FUNCTION_NONE);
258     UsbSrvClient.GetCurrentFunctions(funcs);
259 
260     UsbSrvClient.SetPortRole(
261         UsbSrvSupport::PORT_MODE_DEVICE, UsbSrvSupport::POWER_ROLE_SOURCE, UsbSrvSupport::DATA_ROLE_HOST);
262     UsbDevice device;
263     device.SetBusNum(USB_BUS_NUM_INVALID);
264     device.SetDevAddr(USB_DEV_ADDR_INVALID);
265     USBDevicePipe pipe;
266     UsbSrvClient.OpenDevice(device, pipe);
267     UsbInterface interface;
268     UsbSrvClient.ClaimInterface(pipe, interface, true);
269     UsbSrvClient.ReleaseInterface(pipe, interface);
270 
271     vector<uint8_t> buffData;
272     USBEndpoint pointIn(USB_ENDPOINT_DIR_IN, 0, 0, 0);
273     UsbSrvClient.BulkTransfer(pipe, pointIn, buffData, 100);
274     USBEndpoint pointOut(USB_ENDPOINT_DIR_OUT, 0, 0, 0);
275     UsbSrvClient.BulkTransfer(pipe, pointOut, buffData, 100);
276 
277     struct UsbCtrlTransfer ctrldata = {0b10000000, 8, 0, 0, 500};
278     UsbSrvClient.ControlTransfer(pipe, ctrldata, buffData);
279 
280     USBConfig config;
281     UsbSrvClient.SetConfiguration(pipe, config);
282 
283     UsbSrvClient.SetInterface(pipe, interface);
284 
285     sptr<Ashmem> ashmem;
286     uint8_t rflg = 0;
287     InitAshmemOne(ashmem, MEM_DATA, rflg);
288     UsbSrvClient.BulkRead(pipe, pointIn, ashmem);
289     UsbSrvClient.BulkWrite(pipe, pointOut, ashmem);
290 
291     ASSERT_EQ(true, true);
292     UsbCommonTest::GrantPermissionSysNative();
293     USB_HILOGI(MODULE_USB_SERVICE, "Case End : HiTrace001.");
294 }
295 
296 } // Core
297 } // USB
298 } // OHOS
299