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