1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include <gtest/gtest.h>
17094332d3Sopenharmony_ci#include <iostream>
18094332d3Sopenharmony_ci#include "UsbSubscriberTest.h"
19094332d3Sopenharmony_ci#include "hdf_log.h"
20094332d3Sopenharmony_ci#include "usbd_type.h"
21094332d3Sopenharmony_ci#include "v1_0/iusbd_subscriber.h"
22094332d3Sopenharmony_ci#include "v1_0/iusb_interface.h"
23094332d3Sopenharmony_ci#include "v1_0/usb_types.h"
24094332d3Sopenharmony_ci
25094332d3Sopenharmony_ciusing OHOS::HDI::Usb::V1_0::UsbDev;
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_cinamespace {
28094332d3Sopenharmony_ciconst uint8_t INDEX_0 = 0;
29094332d3Sopenharmony_ciconst uint8_t INDEX_1 = 1;
30094332d3Sopenharmony_ciconst uint8_t INDEX_INVALID = 255;
31094332d3Sopenharmony_ciconst uint8_t BUS_NUM_INVALID = 255;
32094332d3Sopenharmony_ciconst uint8_t DEV_ADDR_INVALID = 255;
33094332d3Sopenharmony_ciconst uint8_t INTERFACEID_OK = 1;
34094332d3Sopenharmony_ciconst uint8_t INTERFACEID_INVALID = 255;
35094332d3Sopenharmony_ciconst int SLEEP_TIME = 3;
36094332d3Sopenharmony_ciclass UsbdInterfaceTest : public testing::Test {
37094332d3Sopenharmony_cipublic:
38094332d3Sopenharmony_ci    static void SetUpTestCase();
39094332d3Sopenharmony_ci    static void TearDownTestCase();
40094332d3Sopenharmony_ci
41094332d3Sopenharmony_ci    static UsbDev dev_;
42094332d3Sopenharmony_ci    static OHOS::sptr<OHOS::USB::UsbSubscriberTest> subscriber_;
43094332d3Sopenharmony_ci};
44094332d3Sopenharmony_ciUsbDev UsbdInterfaceTest::dev_ = {0, 0};
45094332d3Sopenharmony_ciOHOS::sptr<OHOS::USB::UsbSubscriberTest> UsbdInterfaceTest::subscriber_ = nullptr;
46094332d3Sopenharmony_ci
47094332d3Sopenharmony_ciusing namespace testing::ext;
48094332d3Sopenharmony_ciusing namespace OHOS;
49094332d3Sopenharmony_ciusing namespace OHOS::USB;
50094332d3Sopenharmony_ciusing namespace std;
51094332d3Sopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0;
52094332d3Sopenharmony_ci
53094332d3Sopenharmony_cisptr<IUsbInterface> g_usbInterface = nullptr;
54094332d3Sopenharmony_ci
55094332d3Sopenharmony_ciint32_t SwitchErrCode(int32_t ret)
56094332d3Sopenharmony_ci{
57094332d3Sopenharmony_ci    return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
58094332d3Sopenharmony_ci}
59094332d3Sopenharmony_ci
60094332d3Sopenharmony_civoid UsbdInterfaceTest::SetUpTestCase(void)
61094332d3Sopenharmony_ci{
62094332d3Sopenharmony_ci    g_usbInterface = IUsbInterface::Get();
63094332d3Sopenharmony_ci    if (g_usbInterface == nullptr) {
64094332d3Sopenharmony_ci        HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
65094332d3Sopenharmony_ci        exit(0);
66094332d3Sopenharmony_ci    }
67094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetPortRole(1, 1, 1);
68094332d3Sopenharmony_ci    sleep(SLEEP_TIME);
69094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::[Device] %{public}d SetPortRole=%{public}d", __LINE__, ret);
70094332d3Sopenharmony_ci    ret = SwitchErrCode(ret);
71094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
72094332d3Sopenharmony_ci    if (ret != 0) {
73094332d3Sopenharmony_ci        exit(0);
74094332d3Sopenharmony_ci    }
75094332d3Sopenharmony_ci
76094332d3Sopenharmony_ci    subscriber_ = new UsbSubscriberTest();
77094332d3Sopenharmony_ci    if (g_usbInterface->BindUsbdSubscriber(subscriber_) != HDF_SUCCESS) {
78094332d3Sopenharmony_ci        HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__);
79094332d3Sopenharmony_ci        exit(0);
80094332d3Sopenharmony_ci    }
81094332d3Sopenharmony_ci
82094332d3Sopenharmony_ci    int c;
83094332d3Sopenharmony_ci    while ((c = getchar()) != '\n' && c != EOF) {}
84094332d3Sopenharmony_ci    dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
85094332d3Sopenharmony_ci
86094332d3Sopenharmony_ci    ret = g_usbInterface->OpenDevice(dev_);
87094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
88094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest:: %{public}d OpenDevice=%{public}d", __LINE__, ret);
89094332d3Sopenharmony_ci    ret = g_usbInterface->ClaimInterface(dev_, 1, 1);
90094332d3Sopenharmony_ci    ASSERT_EQ(0, ret);
91094332d3Sopenharmony_ci}
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_civoid UsbdInterfaceTest::TearDownTestCase(void)
94094332d3Sopenharmony_ci{
95094332d3Sopenharmony_ci    g_usbInterface->UnbindUsbdSubscriber(subscriber_);
96094332d3Sopenharmony_ci    dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
97094332d3Sopenharmony_ci    auto ret = g_usbInterface->CloseDevice(dev_);
98094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest:: %{public}d Close=%{public}d", __LINE__, ret);
99094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
100094332d3Sopenharmony_ci}
101094332d3Sopenharmony_ci
102094332d3Sopenharmony_ci/**
103094332d3Sopenharmony_ci * @tc.name: UsbdSetInterface001
104094332d3Sopenharmony_ci * @tc.desc: Test functions to SetInterface
105094332d3Sopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
106094332d3Sopenharmony_ci * @tc.desc: Positive test: parameters correctly
107094332d3Sopenharmony_ci * @tc.type: FUNC
108094332d3Sopenharmony_ci */
109094332d3Sopenharmony_ciHWTEST_F(UsbdInterfaceTest, UsbdSetInterface001, TestSize.Level1)
110094332d3Sopenharmony_ci{
111094332d3Sopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK;
112094332d3Sopenharmony_ci    uint8_t altIndex = INDEX_0;
113094332d3Sopenharmony_ci    struct UsbDev dev = dev_;
114094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
115094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface001 %{public}d ret=%{public}d", __LINE__, ret);
116094332d3Sopenharmony_ci    EXPECT_EQ(0, ret);
117094332d3Sopenharmony_ci}
118094332d3Sopenharmony_ci
119094332d3Sopenharmony_ci/**
120094332d3Sopenharmony_ci * @tc.name: UsbdSetInterface002
121094332d3Sopenharmony_ci * @tc.desc: Test functions to SetInterface
122094332d3Sopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
123094332d3Sopenharmony_ci * @tc.desc: Negative test: parameters exception, busNum error
124094332d3Sopenharmony_ci * @tc.type: FUNC
125094332d3Sopenharmony_ci */
126094332d3Sopenharmony_ciHWTEST_F(UsbdInterfaceTest, UsbdSetInterface002, TestSize.Level1)
127094332d3Sopenharmony_ci{
128094332d3Sopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK;
129094332d3Sopenharmony_ci    uint8_t altIndex = INDEX_0;
130094332d3Sopenharmony_ci    struct UsbDev dev = dev_;
131094332d3Sopenharmony_ci    ;
132094332d3Sopenharmony_ci    dev.busNum = BUS_NUM_INVALID;
133094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
134094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface002 %{public}d ret=%{public}d", __LINE__, ret);
135094332d3Sopenharmony_ci    EXPECT_NE(ret, 0);
136094332d3Sopenharmony_ci}
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_ci/**
139094332d3Sopenharmony_ci * @tc.name: UsbdSetInterface003
140094332d3Sopenharmony_ci * @tc.desc: Test functions to SetInterface
141094332d3Sopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
142094332d3Sopenharmony_ci * @tc.desc: Negative test: parameters exception, devAddr error
143094332d3Sopenharmony_ci * @tc.type: FUNC
144094332d3Sopenharmony_ci */
145094332d3Sopenharmony_ciHWTEST_F(UsbdInterfaceTest, UsbdSetInterface003, TestSize.Level1)
146094332d3Sopenharmony_ci{
147094332d3Sopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK;
148094332d3Sopenharmony_ci    uint8_t altIndex = INDEX_INVALID;
149094332d3Sopenharmony_ci    struct UsbDev dev = dev_;
150094332d3Sopenharmony_ci    dev.devAddr = DEV_ADDR_INVALID;
151094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
152094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface003 %{public}d ret=%{public}d", __LINE__, ret);
153094332d3Sopenharmony_ci    EXPECT_NE(ret, 0);
154094332d3Sopenharmony_ci}
155094332d3Sopenharmony_ci
156094332d3Sopenharmony_ci/**
157094332d3Sopenharmony_ci * @tc.name: UsbdSetInterface004
158094332d3Sopenharmony_ci * @tc.desc: Test functions to SetInterface
159094332d3Sopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
160094332d3Sopenharmony_ci * @tc.desc: Negative test: parameters exception, interfaceId error
161094332d3Sopenharmony_ci * @tc.type: FUNC
162094332d3Sopenharmony_ci */
163094332d3Sopenharmony_ciHWTEST_F(UsbdInterfaceTest, UsbdSetInterface004, TestSize.Level1)
164094332d3Sopenharmony_ci{
165094332d3Sopenharmony_ci    uint8_t interfaceId = INTERFACEID_INVALID;
166094332d3Sopenharmony_ci    uint8_t altIndex = INDEX_INVALID;
167094332d3Sopenharmony_ci    struct UsbDev dev = dev_;
168094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
169094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface004 %{public}d ret=%{public}d", __LINE__, ret);
170094332d3Sopenharmony_ci    EXPECT_NE(ret, 0);
171094332d3Sopenharmony_ci}
172094332d3Sopenharmony_ci
173094332d3Sopenharmony_ci/**
174094332d3Sopenharmony_ci * @tc.name: UsbdSetInterface005
175094332d3Sopenharmony_ci * @tc.desc: Test functions to SetInterface
176094332d3Sopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
177094332d3Sopenharmony_ci * @tc.desc: Negative test: parameters exception, busNum && devAddr error
178094332d3Sopenharmony_ci * @tc.type: FUNC
179094332d3Sopenharmony_ci */
180094332d3Sopenharmony_ciHWTEST_F(UsbdInterfaceTest, UsbdSetInterface005, TestSize.Level1)
181094332d3Sopenharmony_ci{
182094332d3Sopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK;
183094332d3Sopenharmony_ci    uint8_t altIndex = INDEX_0;
184094332d3Sopenharmony_ci    struct UsbDev dev = dev_;
185094332d3Sopenharmony_ci    dev.busNum = BUS_NUM_INVALID;
186094332d3Sopenharmony_ci    dev.devAddr = DEV_ADDR_INVALID;
187094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
188094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface005 %{public}d ret=%{public}d", __LINE__, ret);
189094332d3Sopenharmony_ci    EXPECT_NE(ret, 0);
190094332d3Sopenharmony_ci}
191094332d3Sopenharmony_ci
192094332d3Sopenharmony_ci/**
193094332d3Sopenharmony_ci * @tc.name: UsbdSetInterface006
194094332d3Sopenharmony_ci * @tc.desc: Test functions to SetInterface
195094332d3Sopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
196094332d3Sopenharmony_ci * @tc.desc: Negative test: parameters exception, busNum && interfaceId error
197094332d3Sopenharmony_ci * @tc.type: FUNC
198094332d3Sopenharmony_ci */
199094332d3Sopenharmony_ciHWTEST_F(UsbdInterfaceTest, UsbdSetInterface006, TestSize.Level1)
200094332d3Sopenharmony_ci{
201094332d3Sopenharmony_ci    int32_t interfaceId = INTERFACEID_INVALID;
202094332d3Sopenharmony_ci    uint8_t altIndex = INDEX_1;
203094332d3Sopenharmony_ci    struct UsbDev dev = dev_;
204094332d3Sopenharmony_ci    dev.busNum = BUS_NUM_INVALID;
205094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
206094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface006 %{public}d ret=%{public}d", __LINE__, ret);
207094332d3Sopenharmony_ci    EXPECT_NE(ret, 0);
208094332d3Sopenharmony_ci}
209094332d3Sopenharmony_ci
210094332d3Sopenharmony_ci/**
211094332d3Sopenharmony_ci * @tc.name: UsbdSetInterface007
212094332d3Sopenharmony_ci * @tc.desc: Test functions to SetInterface
213094332d3Sopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
214094332d3Sopenharmony_ci * @tc.desc: Negative test: parameters exception, devAddr && interfaceId error
215094332d3Sopenharmony_ci * @tc.type: FUNC
216094332d3Sopenharmony_ci */
217094332d3Sopenharmony_ciHWTEST_F(UsbdInterfaceTest, UsbdSetInterface007, TestSize.Level1)
218094332d3Sopenharmony_ci{
219094332d3Sopenharmony_ci    int32_t interfaceId = INTERFACEID_INVALID;
220094332d3Sopenharmony_ci    uint8_t altIndex = INDEX_INVALID;
221094332d3Sopenharmony_ci    struct UsbDev dev = dev_;
222094332d3Sopenharmony_ci    dev.devAddr = DEV_ADDR_INVALID;
223094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
224094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface007 %{public}d ret=%{public}d", __LINE__, ret);
225094332d3Sopenharmony_ci    EXPECT_NE(ret, 0);
226094332d3Sopenharmony_ci}
227094332d3Sopenharmony_ci
228094332d3Sopenharmony_ci/**
229094332d3Sopenharmony_ci * @tc.name: UsbdSetInterface008
230094332d3Sopenharmony_ci * @tc.desc: Test functions to SetInterface
231094332d3Sopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
232094332d3Sopenharmony_ci * @tc.desc: Negative test: parameters exception, busNum && devAddr && interfaceId error
233094332d3Sopenharmony_ci * @tc.type: FUNC
234094332d3Sopenharmony_ci */
235094332d3Sopenharmony_ciHWTEST_F(UsbdInterfaceTest, UsbdSetInterface008, TestSize.Level1)
236094332d3Sopenharmony_ci{
237094332d3Sopenharmony_ci    uint8_t altIndex = INDEX_INVALID;
238094332d3Sopenharmony_ci    int32_t interfaceId = INTERFACEID_INVALID;
239094332d3Sopenharmony_ci    struct UsbDev dev = dev_;
240094332d3Sopenharmony_ci    dev.busNum = BUS_NUM_INVALID;
241094332d3Sopenharmony_ci    dev.devAddr = DEV_ADDR_INVALID;
242094332d3Sopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
243094332d3Sopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::UsbdSetInterface008 %{public}d ret=%{public}d", __LINE__, ret);
244094332d3Sopenharmony_ci    EXPECT_NE(ret, 0);
245094332d3Sopenharmony_ci}
246094332d3Sopenharmony_ci} // namespace