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