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 <gtest/gtest.h>
17 #include <iostream>
18 #include "UsbSubTest.h"
19 #include "hdf_log.h"
20 #include "usbd_type.h"
21 #include "usbd_wrapper.h"
22 #include "v2_0/iusbd_subscriber.h"
23 #include "v2_0/iusb_host_interface.h"
24 #include "v2_0/iusb_port_interface.h"
25 #include "v2_0/usb_types.h"
26
27 using OHOS::HDI::Usb::V2_0::UsbDev;
28
29 namespace {
30 const uint8_t INDEX_0 = 0;
31 const uint8_t INDEX_1 = 1;
32 const uint8_t INDEX_INVALID = 255;
33 const uint8_t BUS_NUM_INVALID = 255;
34 const uint8_t DEV_ADDR_INVALID = 255;
35 const uint8_t INTERFACEID_OK = 1;
36 const uint8_t INTERFACEID_INVALID = 255;
37 const int SLEEP_TIME = 3;
38 class UsbdInterfaceTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42
43 static UsbDev dev_;
44 static OHOS::sptr<OHOS::USB::UsbSubTest> subscriber_;
45 };
46 UsbDev UsbdInterfaceTest::dev_ = {0, 0};
47 OHOS::sptr<OHOS::USB::UsbSubTest> UsbdInterfaceTest::subscriber_ = nullptr;
48
49 using namespace testing::ext;
50 using namespace OHOS;
51 using namespace OHOS::USB;
52 using namespace std;
53 using namespace OHOS::HDI::Usb::V2_0;
54
55 sptr<IUsbHostInterface> g_usbHostInterface = nullptr;
56 sptr<IUsbPortInterface> g_usbPortInterface = nullptr;
57
SwitchErrCode(int32_t ret)58 int32_t SwitchErrCode(int32_t ret)
59 {
60 return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
61 }
62
SetUpTestCase(void)63 void UsbdInterfaceTest::SetUpTestCase(void)
64 {
65 g_usbHostInterface = IUsbHostInterface::Get(true);
66 g_usbPortInterface = IUsbPortInterface::Get();
67 if (g_usbHostInterface == nullptr || g_usbPortInterface == nullptr) {
68 HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
69 exit(0);
70 }
71 auto ret = g_usbPortInterface->SetPortRole(1, 1, 1);
72 sleep(SLEEP_TIME);
73 HDF_LOGI("UsbdInterfaceTest::[Device] %{public}d SetPortRole=%{public}d", __LINE__, ret);
74 ret = SwitchErrCode(ret);
75 ASSERT_EQ(0, ret);
76 if (ret != 0) {
77 exit(0);
78 }
79
80 subscriber_ = new UsbSubTest();
81 if (g_usbHostInterface->BindUsbdHostSubscriber(subscriber_) != HDF_SUCCESS) {
82 HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__);
83 exit(0);
84 }
85
86 int c;
87 while ((c = getchar()) != '\n' && c != EOF) {}
88 dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
89
90 ret = g_usbHostInterface->OpenDevice(dev_);
91 ASSERT_EQ(0, ret);
92 HDF_LOGI("UsbdInterfaceTest:: %{public}d OpenDevice=%{public}d", __LINE__, ret);
93 ret = g_usbHostInterface->ClaimInterface(dev_, 1, 1);
94 ASSERT_EQ(0, ret);
95 }
96
TearDownTestCase(void)97 void UsbdInterfaceTest::TearDownTestCase(void)
98 {
99 g_usbHostInterface->UnbindUsbdHostSubscriber(subscriber_);
100 dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
101 auto ret = g_usbHostInterface->CloseDevice(dev_);
102 HDF_LOGI("UsbdInterfaceTest:: %{public}d Close=%{public}d", __LINE__, ret);
103 EXPECT_EQ(0, ret);
104 }
105
106 /**
107 * @tc.name: UsbdSetInterface001
108 * @tc.desc: Test functions to SetInterface
109 * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
110 * @tc.desc: Positive test: parameters correctly
111 * @tc.type: FUNC
112 */
113 HWTEST_F(UsbdInterfaceTest, UsbdSetInterface001, TestSize.Level1)
114 {
115 uint8_t interfaceId = INTERFACEID_OK;
116 uint8_t altIndex = INDEX_0;
117 struct UsbDev dev = dev_;
118 auto ret = g_usbHostInterface->SetInterface(dev, interfaceId, altIndex);
119 HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface001 %{public}d ret=%{public}d", __LINE__, ret);
120 EXPECT_EQ(0, ret);
121 }
122
123 /**
124 * @tc.name: UsbdSetInterface002
125 * @tc.desc: Test functions to SetInterface
126 * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
127 * @tc.desc: Negative test: parameters exception, busNum error
128 * @tc.type: FUNC
129 */
130 HWTEST_F(UsbdInterfaceTest, UsbdSetInterface002, TestSize.Level1)
131 {
132 uint8_t interfaceId = INTERFACEID_OK;
133 uint8_t altIndex = INDEX_0;
134 struct UsbDev dev = dev_;
135 ;
136 dev.busNum = BUS_NUM_INVALID;
137 auto ret = g_usbHostInterface->SetInterface(dev, interfaceId, altIndex);
138 HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface002 %{public}d ret=%{public}d", __LINE__, ret);
139 EXPECT_NE(ret, 0);
140 }
141
142 /**
143 * @tc.name: UsbdSetInterface003
144 * @tc.desc: Test functions to SetInterface
145 * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
146 * @tc.desc: Negative test: parameters exception, devAddr error
147 * @tc.type: FUNC
148 */
149 HWTEST_F(UsbdInterfaceTest, UsbdSetInterface003, TestSize.Level1)
150 {
151 uint8_t interfaceId = INTERFACEID_OK;
152 uint8_t altIndex = INDEX_INVALID;
153 struct UsbDev dev = dev_;
154 dev.devAddr = DEV_ADDR_INVALID;
155 auto ret = g_usbHostInterface->SetInterface(dev, interfaceId, altIndex);
156 HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface003 %{public}d ret=%{public}d", __LINE__, ret);
157 EXPECT_NE(ret, 0);
158 }
159
160 /**
161 * @tc.name: UsbdSetInterface004
162 * @tc.desc: Test functions to SetInterface
163 * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
164 * @tc.desc: Negative test: parameters exception, interfaceId error
165 * @tc.type: FUNC
166 */
167 HWTEST_F(UsbdInterfaceTest, UsbdSetInterface004, TestSize.Level1)
168 {
169 uint8_t interfaceId = INTERFACEID_INVALID;
170 uint8_t altIndex = INDEX_INVALID;
171 struct UsbDev dev = dev_;
172 auto ret = g_usbHostInterface->SetInterface(dev, interfaceId, altIndex);
173 HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface004 %{public}d ret=%{public}d", __LINE__, ret);
174 EXPECT_NE(ret, 0);
175 }
176
177 /**
178 * @tc.name: UsbdSetInterface005
179 * @tc.desc: Test functions to SetInterface
180 * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
181 * @tc.desc: Negative test: parameters exception, busNum && devAddr error
182 * @tc.type: FUNC
183 */
184 HWTEST_F(UsbdInterfaceTest, UsbdSetInterface005, TestSize.Level1)
185 {
186 uint8_t interfaceId = INTERFACEID_OK;
187 uint8_t altIndex = INDEX_0;
188 struct UsbDev dev = dev_;
189 dev.busNum = BUS_NUM_INVALID;
190 dev.devAddr = DEV_ADDR_INVALID;
191 auto ret = g_usbHostInterface->SetInterface(dev, interfaceId, altIndex);
192 HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface005 %{public}d ret=%{public}d", __LINE__, ret);
193 EXPECT_NE(ret, 0);
194 }
195
196 /**
197 * @tc.name: UsbdSetInterface006
198 * @tc.desc: Test functions to SetInterface
199 * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
200 * @tc.desc: Negative test: parameters exception, busNum && interfaceId error
201 * @tc.type: FUNC
202 */
203 HWTEST_F(UsbdInterfaceTest, UsbdSetInterface006, TestSize.Level1)
204 {
205 int32_t interfaceId = INTERFACEID_INVALID;
206 uint8_t altIndex = INDEX_1;
207 struct UsbDev dev = dev_;
208 dev.busNum = BUS_NUM_INVALID;
209 auto ret = g_usbHostInterface->SetInterface(dev, interfaceId, altIndex);
210 HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface006 %{public}d ret=%{public}d", __LINE__, ret);
211 EXPECT_NE(ret, 0);
212 }
213
214 /**
215 * @tc.name: UsbdSetInterface007
216 * @tc.desc: Test functions to SetInterface
217 * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
218 * @tc.desc: Negative test: parameters exception, devAddr && interfaceId error
219 * @tc.type: FUNC
220 */
221 HWTEST_F(UsbdInterfaceTest, UsbdSetInterface007, TestSize.Level1)
222 {
223 int32_t interfaceId = INTERFACEID_INVALID;
224 uint8_t altIndex = INDEX_INVALID;
225 struct UsbDev dev = dev_;
226 dev.devAddr = DEV_ADDR_INVALID;
227 auto ret = g_usbHostInterface->SetInterface(dev, interfaceId, altIndex);
228 HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface007 %{public}d ret=%{public}d", __LINE__, ret);
229 EXPECT_NE(ret, 0);
230 }
231
232 /**
233 * @tc.name: UsbdSetInterface008
234 * @tc.desc: Test functions to SetInterface
235 * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
236 * @tc.desc: Negative test: parameters exception, busNum && devAddr && interfaceId error
237 * @tc.type: FUNC
238 */
239 HWTEST_F(UsbdInterfaceTest, UsbdSetInterface008, TestSize.Level1)
240 {
241 uint8_t altIndex = INDEX_INVALID;
242 int32_t interfaceId = INTERFACEID_INVALID;
243 struct UsbDev dev = dev_;
244 dev.busNum = BUS_NUM_INVALID;
245 dev.devAddr = DEV_ADDR_INVALID;
246 auto ret = g_usbHostInterface->SetInterface(dev, interfaceId, altIndex);
247 HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface008 %{public}d ret=%{public}d", __LINE__, ret);
248 EXPECT_NE(ret, 0);
249 }
250 } // namespace