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