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