1 /*
2 * Copyright (c) 2025 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 "usb_hub_device_test.h"
17
18 #include <sys/time.h>
19 #include <sys/ioctl.h>
20 #include <iostream>
21 #include <vector>
22 #include <thread>
23 #include <chrono>
24
25 #include "delayed_sp_singleton.h"
26 #include "hilog_wrapper.h"
27 #include "if_system_ability_manager.h"
28 #include "system_ability_definition.h"
29 #include "usb_common_test.h"
30 #include "usb_srv_client.h"
31 #include "usb_errors.h"
32 #include "cJSON.h"
33 #include "common_event_manager.h"
34 #include "common_event_support.h"
35 using namespace testing::ext;
36 using namespace OHOS;
37 using namespace OHOS::USB;
38 using namespace std;
39 using namespace OHOS::HDI::Usb::V1_0;
40 using namespace OHOS::HDI::Usb::V1_2;
41 using namespace OHOS::USB::Common;
42 using namespace OHOS::EventFwk;
43 using namespace testing::ext;
44 namespace OHOS {
45 namespace USB {
46 namespace HubDevice {
47 sem_t UsbHubDeviceTest::testSem_ {};
SetUpTestCase(void)48 void UsbHubDeviceTest::SetUpTestCase(void)
49 {
50 std::cout << "please connect hub device, press enter to continue" << std::endl;
51 int32_t c;
52 while ((c = getchar()) != '\n' && c != EOF) {
53 ;
54 }
55 USB_HILOGI(MODULE_USB_SERVICE, "Start UsbHubDeviceTest");
56 }
57
TearDownTestCase(void)58 void UsbHubDeviceTest::TearDownTestCase(void)
59 {
60 USB_HILOGI(MODULE_USB_SERVICE, "End UsbHubDeviceTest");
61 }
62
SetUp(void)63 void UsbHubDeviceTest::SetUp(void)
64 {
65 sem_init(&UsbHubDeviceTest::testSem_, 1, 0);
66 }
67
TearDown(void)68 void UsbHubDeviceTest::TearDown(void)
69 {
70 sem_destroy(&UsbHubDeviceTest::testSem_);
71 }
72
73 /**
74 * @tc.name: getDevices001
75 * @tc.desc: Test system sa to getDevices(std::vector<UsbDevice> &deviceList);
76 * @tc.type: FUNC
77 */
78 HWTEST_F(UsbHubDeviceTest, getDevices001, TestSize.Level1)
79 {
80 UsbCommonTest::GrantPermissionNormalNative();
81 std::this_thread::sleep_for(std::chrono::seconds(2));
82 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : getDevices001 : getDevices");
83 vector<UsbDevice> devi;
84 auto &UsbSrvClient = UsbSrvClient::GetInstance();
85 auto ret = UsbSrvClient.GetDevices(devi);
86 ASSERT_EQ(ret, 0);
87 USB_HILOGI(MODULE_USB_SERVICE, "UsbHubDeviceTest::getDevices001 %{public}d ret=%{public}d", __LINE__, ret);
88 EXPECT_TRUE(!(devi.empty())) << "delist NULL";
89 USB_HILOGI(MODULE_USB_SERVICE, "UsbHubDeviceTest::getDevices001 %{public}d size=%{public}zu", __LINE__,
90 devi.size());
91 bool hasHub = false;
92 for (size_t i = 0; i < devi.size(); i++) {
93 if (devi[i].GetClass() == 9) {
94 hasHub = true;
95 break;
96 } else {
97 USB_HILOGI(MODULE_USB_SERVICE,
98 "UsbHubDeviceTest::getDevices001 class %{public}d ret=%{public}d", __LINE__, devi[i].GetClass());
99 }
100 }
101 ASSERT_TRUE(hasHub);
102 USB_HILOGI(MODULE_USB_SERVICE, "Case End : getDevices001 : getDevices");
103 }
104
105 class UsbSubscriberTest : public CommonEventSubscriber {
106 public:
UsbSubscriberTest(const CommonEventSubscribeInfo & sp)107 explicit UsbSubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) {}
108
OnReceiveEvent(const CommonEventData & data)109 void OnReceiveEvent(const CommonEventData &data) override
110 {
111 std::string deviceStr = data.GetData();
112 eventDatas_.push_back(data);
113 USB_HILOGI(MODULE_USB_SERVICE, "recv event ok datainfo: %{public}s", deviceStr.c_str());
114 std::cout << deviceStr << std::endl;
115 sem_post(&UsbHubDeviceTest::testSem_);
116 }
117
118 static std::vector<CommonEventData> eventDatas_;
119 };
120 std::vector<CommonEventData> UsbSubscriberTest::eventDatas_ {};
121
122 /**
123 * @tc.name: SUB_USB_Broadcast_001
124 * @tc.desc: usb device attached event
125 * @tc.type: FUNC
126 */
127 HWTEST_F(UsbHubDeviceTest, SUB_USB_Broadcast_001, TestSize.Level1)
128 {
129 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : SUB_USB_Broadcast_001");
130 UsbCommonTest::GrantPermissionSysNative();
131 std::this_thread::sleep_for(std::chrono::seconds(2));
132 MatchingSkills matchingSkills;
133 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
134 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
135 std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
136 CommonEventManager::SubscribeCommonEvent(subscriber);
137
138 std::cout << "please connect a hub to the host" << std::endl;
139
140 // block until UsbSubscriberTest post
141 sem_wait(&UsbHubDeviceTest::testSem_);
142 std::this_thread::sleep_for(std::chrono::seconds(5));
143 CommonEventManager::UnSubscribeCommonEvent(subscriber);
144 USB_HILOGI(MODULE_USB_SERVICE, "UnSubscribeCommonEvent");
145 bool hasHub = false;
146 for (size_t i = 0; i < UsbSubscriberTest::eventDatas_.size(); i++) {
147 // parse string to Json::Value
148 std::string deviceStr = UsbSubscriberTest::eventDatas_[i].GetData();
149 cJSON* deviceJson = cJSON_Parse(deviceStr.c_str());
150 if (!deviceJson) {
151 USB_HILOGI(MODULE_USB_SERVICE, "SUB_USB_Broadcast_001 error, parse json string error");
152 }
153 UsbDevice device(deviceJson);
154 if (device.GetClass() == 9) {
155 hasHub = true;
156 }
157 }
158 UsbSubscriberTest::eventDatas_.clear();
159 EXPECT_TRUE(hasHub);
160 }
161
162 /**
163 * @tc.name: SUB_USB_Broadcast_002
164 * @tc.desc: usb device attached event
165 * @tc.type: FUNC
166 */
167 HWTEST_F(UsbHubDeviceTest, SUB_USB_Broadcast_002, TestSize.Level1)
168 {
169 USB_HILOGI(MODULE_USB_SERVICE, "Case Start : SUB_USB_Broadcast_002");
170 UsbCommonTest::GrantPermissionSysNative();
171 std::this_thread::sleep_for(std::chrono::seconds(2));
172 MatchingSkills matchingSkills;
173 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
174 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
175 std::shared_ptr<UsbSubscriberTest> subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
176 CommonEventManager::SubscribeCommonEvent(subscriber);
177
178 std::cout << "please disconnect a hub to the host" << std::endl;
179
180 // block until UsbSubscriberTest post
181 sem_wait(&UsbHubDeviceTest::testSem_);
182 std::this_thread::sleep_for(std::chrono::seconds(5));
183 CommonEventManager::UnSubscribeCommonEvent(subscriber);
184 USB_HILOGI(MODULE_USB_SERVICE, "UnSubscribeCommonEvent");
185 bool hasHub = false;
186 for (size_t i = 0; i < UsbSubscriberTest::eventDatas_.size(); i++) {
187 // parse string to Json::Value
188 std::string deviceStr = UsbSubscriberTest::eventDatas_[i].GetData();
189 cJSON* deviceJson = cJSON_Parse(deviceStr.c_str());
190 if (!deviceJson) {
191 USB_HILOGI(MODULE_USB_SERVICE, "SUB_USB_Broadcast_002 error, parse json string error");
192 }
193 UsbDevice device(deviceJson);
194 if (device.GetClass() == 9) {
195 hasHub = true;
196 }
197 }
198 UsbSubscriberTest::eventDatas_.clear();
199 EXPECT_TRUE(hasHub);
200 }
201
202 } // HubDevice
203 } // USB
204 } // OHOS
205