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