19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <gtest/gtest.h>
179762338dSopenharmony_ci#include <iostream>
189762338dSopenharmony_ci
199762338dSopenharmony_ci#include "UsbSubscriberTest.h"
209762338dSopenharmony_ci#include "hdf_log.h"
219762338dSopenharmony_ci#include "usbd_request_test.h"
229762338dSopenharmony_ci#include "v1_0/iusb_interface.h"
239762338dSopenharmony_ci#include "v1_0/usb_types.h"
249762338dSopenharmony_ci
259762338dSopenharmony_ciusing OHOS::HDI::Usb::V1_0::UsbDev;
269762338dSopenharmony_ciusing namespace testing::ext;
279762338dSopenharmony_ciusing namespace OHOS;
289762338dSopenharmony_ciusing namespace OHOS::USB;
299762338dSopenharmony_ciusing namespace std;
309762338dSopenharmony_ciusing namespace OHOS::HDI::Usb::V1_0;
319762338dSopenharmony_ci
329762338dSopenharmony_cinamespace {
339762338dSopenharmony_ciconst uint8_t INDEX_0 = 0;
349762338dSopenharmony_ciconst uint8_t INDEX_1 = 1;
359762338dSopenharmony_ciconst uint8_t INDEX_INVALID = 255;
369762338dSopenharmony_ciconst uint8_t BUS_NUM_INVALID = 255;
379762338dSopenharmony_ciconst uint8_t DEV_ADDR_INVALID = 255;
389762338dSopenharmony_ciconst uint8_t INTERFACEID_OK = 1;
399762338dSopenharmony_ciconst uint8_t INTERFACEID_INVALID = 255;
409762338dSopenharmony_ciconst int SLEEP_TIME = 3;
419762338dSopenharmony_ci
429762338dSopenharmony_ciclass UsbdInterfaceTest : public testing::Test {
439762338dSopenharmony_cipublic:
449762338dSopenharmony_ci    static void SetUpTestCase();
459762338dSopenharmony_ci    static void TearDownTestCase();
469762338dSopenharmony_ci    void SetUp();
479762338dSopenharmony_ci    void TearDown();
489762338dSopenharmony_ci
499762338dSopenharmony_ci    static UsbDev dev_;
509762338dSopenharmony_ci    static OHOS::sptr<OHOS::USB::UsbSubscriberTest> subscriber_;
519762338dSopenharmony_ci};
529762338dSopenharmony_ciUsbDev UsbdInterfaceTest::dev_ = {0, 0};
539762338dSopenharmony_ciOHOS::sptr<OHOS::USB::UsbSubscriberTest> UsbdInterfaceTest::subscriber_ = nullptr;
549762338dSopenharmony_ci
559762338dSopenharmony_cisptr<IUsbInterface> g_usbInterface = nullptr;
569762338dSopenharmony_ci
579762338dSopenharmony_ciint32_t SwitchErrCode(int32_t ret)
589762338dSopenharmony_ci{
599762338dSopenharmony_ci    return ret == HDF_ERR_NOT_SUPPORT ? HDF_SUCCESS : ret;
609762338dSopenharmony_ci}
619762338dSopenharmony_ci
629762338dSopenharmony_civoid UsbdInterfaceTest::SetUpTestCase(void)
639762338dSopenharmony_ci{
649762338dSopenharmony_ci    g_usbInterface = IUsbInterface::Get();
659762338dSopenharmony_ci    if (g_usbInterface == nullptr) {
669762338dSopenharmony_ci        HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
679762338dSopenharmony_ci        exit(0);
689762338dSopenharmony_ci    }
699762338dSopenharmony_ci    auto ret = g_usbInterface->SetPortRole(1, 1, 1);
709762338dSopenharmony_ci    sleep(SLEEP_TIME);
719762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::[Device] %{public}d SetPortRole=%{public}d", __LINE__, ret);
729762338dSopenharmony_ci    ret = SwitchErrCode(ret);
739762338dSopenharmony_ci    ASSERT_EQ(0, ret);
749762338dSopenharmony_ci    if (ret != 0) {
759762338dSopenharmony_ci        exit(0);
769762338dSopenharmony_ci    }
779762338dSopenharmony_ci
789762338dSopenharmony_ci    subscriber_ = new UsbSubscriberTest();
799762338dSopenharmony_ci    if (g_usbInterface->BindUsbdSubscriber(subscriber_) != HDF_SUCCESS) {
809762338dSopenharmony_ci        HDF_LOGE("%{public}s: bind usbd subscriber_ failed", __func__);
819762338dSopenharmony_ci        exit(0);
829762338dSopenharmony_ci    }
839762338dSopenharmony_ci
849762338dSopenharmony_ci    std::cout << "please connect device, press enter to continue" << std::endl;
859762338dSopenharmony_ci    int c;
869762338dSopenharmony_ci    while ((c = getchar()) != '\n' && c != EOF) {}
879762338dSopenharmony_ci    dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
889762338dSopenharmony_ci
899762338dSopenharmony_ci    ret = g_usbInterface->OpenDevice(dev_);
909762338dSopenharmony_ci    ASSERT_EQ(0, ret);
919762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest:: %{public}d OpenDevice=%{public}d", __LINE__, ret);
929762338dSopenharmony_ci    ret = g_usbInterface->ClaimInterface(dev_, 1, 1);
939762338dSopenharmony_ci    ASSERT_EQ(0, ret);
949762338dSopenharmony_ci}
959762338dSopenharmony_ci
969762338dSopenharmony_civoid UsbdInterfaceTest::TearDownTestCase(void)
979762338dSopenharmony_ci{
989762338dSopenharmony_ci    g_usbInterface->UnbindUsbdSubscriber(subscriber_);
999762338dSopenharmony_ci    dev_ = {subscriber_->busNum_, subscriber_->devAddr_};
1009762338dSopenharmony_ci    auto ret = g_usbInterface->CloseDevice(dev_);
1019762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest:: %{public}d Close=%{public}d", __LINE__, ret);
1029762338dSopenharmony_ci    ASSERT_EQ(0, ret);
1039762338dSopenharmony_ci}
1049762338dSopenharmony_ci
1059762338dSopenharmony_civoid UsbdInterfaceTest::SetUp(void) {}
1069762338dSopenharmony_ci
1079762338dSopenharmony_civoid UsbdInterfaceTest::TearDown(void) {}
1089762338dSopenharmony_ci
1099762338dSopenharmony_ci/**
1109762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Func_0900
1119762338dSopenharmony_ci * @tc.desc: Test functions to SetInterface
1129762338dSopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
1139762338dSopenharmony_ci * @tc.desc: Positive test: parameters correctly
1149762338dSopenharmony_ci * @tc.type: FUNC
1159762338dSopenharmony_ci */
1169762338dSopenharmony_ciHWTEST_F(UsbdInterfaceTest, SUB_USB_HostManager_HDI_Func_0900, Function | MediumTest | Level1)
1179762338dSopenharmony_ci{
1189762338dSopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK;
1199762338dSopenharmony_ci    uint8_t altIndex = INDEX_0;
1209762338dSopenharmony_ci    struct UsbDev dev = dev_;
1219762338dSopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
1229762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::SUB_USB_HostManager_HDI_Func_0900 %{public}d ret=%{public}d", __LINE__, ret);
1239762338dSopenharmony_ci    ASSERT_EQ(0, ret);
1249762338dSopenharmony_ci}
1259762338dSopenharmony_ci
1269762338dSopenharmony_ci/**
1279762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Compatibility_4700
1289762338dSopenharmony_ci * @tc.desc: Test functions to SetInterface
1299762338dSopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
1309762338dSopenharmony_ci * @tc.desc: Negative test: parameters exception, busNum error
1319762338dSopenharmony_ci * @tc.type: FUNC
1329762338dSopenharmony_ci */
1339762338dSopenharmony_ciHWTEST_F(UsbdInterfaceTest, SUB_USB_HostManager_HDI_Compatibility_4700, Function | MediumTest | Level1)
1349762338dSopenharmony_ci{
1359762338dSopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK;
1369762338dSopenharmony_ci    uint8_t altIndex = INDEX_0;
1379762338dSopenharmony_ci    struct UsbDev dev = dev_;
1389762338dSopenharmony_ci    ;
1399762338dSopenharmony_ci    dev.busNum = BUS_NUM_INVALID;
1409762338dSopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
1419762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::SUB_USB_HostManager_HDI_Compatibility_4700 %{public}d ret=%{public}d", __LINE__, ret);
1429762338dSopenharmony_ci    ASSERT_NE(ret, 0);
1439762338dSopenharmony_ci}
1449762338dSopenharmony_ci
1459762338dSopenharmony_ci/**
1469762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Compatibility_4800
1479762338dSopenharmony_ci * @tc.desc: Test functions to SetInterface
1489762338dSopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
1499762338dSopenharmony_ci * @tc.desc: Negative test: parameters exception, devAddr error
1509762338dSopenharmony_ci * @tc.type: FUNC
1519762338dSopenharmony_ci */
1529762338dSopenharmony_ciHWTEST_F(UsbdInterfaceTest, SUB_USB_HostManager_HDI_Compatibility_4800, Function | MediumTest | Level1)
1539762338dSopenharmony_ci{
1549762338dSopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK;
1559762338dSopenharmony_ci    uint8_t altIndex = INDEX_INVALID;
1569762338dSopenharmony_ci    struct UsbDev dev = dev_;
1579762338dSopenharmony_ci    dev.devAddr = DEV_ADDR_INVALID;
1589762338dSopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
1599762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::SUB_USB_HostManager_HDI_Compatibility_4800 %{public}d ret=%{public}d", __LINE__, ret);
1609762338dSopenharmony_ci    ASSERT_NE(ret, 0);
1619762338dSopenharmony_ci}
1629762338dSopenharmony_ci
1639762338dSopenharmony_ci/**
1649762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Compatibility_4900
1659762338dSopenharmony_ci * @tc.desc: Test functions to SetInterface
1669762338dSopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
1679762338dSopenharmony_ci * @tc.desc: Negative test: parameters exception, interfaceId error
1689762338dSopenharmony_ci * @tc.type: FUNC
1699762338dSopenharmony_ci */
1709762338dSopenharmony_ciHWTEST_F(UsbdInterfaceTest, SUB_USB_HostManager_HDI_Compatibility_4900, Function | MediumTest | Level1)
1719762338dSopenharmony_ci{
1729762338dSopenharmony_ci    uint8_t interfaceId = INTERFACEID_INVALID;
1739762338dSopenharmony_ci    uint8_t altIndex = INDEX_INVALID;
1749762338dSopenharmony_ci    struct UsbDev dev = dev_;
1759762338dSopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
1769762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::SUB_USB_HostManager_HDI_Compatibility_4900 %{public}d ret=%{public}d", __LINE__, ret);
1779762338dSopenharmony_ci    ASSERT_NE(ret, 0);
1789762338dSopenharmony_ci}
1799762338dSopenharmony_ci
1809762338dSopenharmony_ci/**
1819762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Compatibility_5000
1829762338dSopenharmony_ci * @tc.desc: Test functions to SetInterface
1839762338dSopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
1849762338dSopenharmony_ci * @tc.desc: Negative test: parameters exception, busNum && devAddr error
1859762338dSopenharmony_ci * @tc.type: FUNC
1869762338dSopenharmony_ci */
1879762338dSopenharmony_ciHWTEST_F(UsbdInterfaceTest, SUB_USB_HostManager_HDI_Compatibility_5000, Function | MediumTest | Level1)
1889762338dSopenharmony_ci{
1899762338dSopenharmony_ci    uint8_t interfaceId = INTERFACEID_OK;
1909762338dSopenharmony_ci    uint8_t altIndex = INDEX_0;
1919762338dSopenharmony_ci    struct UsbDev dev = dev_;
1929762338dSopenharmony_ci    dev.busNum = BUS_NUM_INVALID;
1939762338dSopenharmony_ci    dev.devAddr = DEV_ADDR_INVALID;
1949762338dSopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
1959762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::SUB_USB_HostManager_HDI_Compatibility_5000 %{public}d ret=%{public}d", __LINE__, ret);
1969762338dSopenharmony_ci    ASSERT_NE(ret, 0);
1979762338dSopenharmony_ci}
1989762338dSopenharmony_ci
1999762338dSopenharmony_ci/**
2009762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Compatibility_5100
2019762338dSopenharmony_ci * @tc.desc: Test functions to SetInterface
2029762338dSopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
2039762338dSopenharmony_ci * @tc.desc: Negative test: parameters exception, busNum && interfaceId error
2049762338dSopenharmony_ci * @tc.type: FUNC
2059762338dSopenharmony_ci */
2069762338dSopenharmony_ciHWTEST_F(UsbdInterfaceTest, SUB_USB_HostManager_HDI_Compatibility_5100, Function | MediumTest | Level1)
2079762338dSopenharmony_ci{
2089762338dSopenharmony_ci    int32_t interfaceId = INTERFACEID_INVALID;
2099762338dSopenharmony_ci    uint8_t altIndex = INDEX_1;
2109762338dSopenharmony_ci    struct UsbDev dev = dev_;
2119762338dSopenharmony_ci    dev.busNum = BUS_NUM_INVALID;
2129762338dSopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
2139762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::SUB_USB_HostManager_HDI_Compatibility_5100 %{public}d ret=%{public}d", __LINE__, ret);
2149762338dSopenharmony_ci    ASSERT_NE(ret, 0);
2159762338dSopenharmony_ci}
2169762338dSopenharmony_ci
2179762338dSopenharmony_ci/**
2189762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Compatibility_5200
2199762338dSopenharmony_ci * @tc.desc: Test functions to SetInterface
2209762338dSopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
2219762338dSopenharmony_ci * @tc.desc: Negative test: parameters exception, devAddr && interfaceId error
2229762338dSopenharmony_ci * @tc.type: FUNC
2239762338dSopenharmony_ci */
2249762338dSopenharmony_ciHWTEST_F(UsbdInterfaceTest, SUB_USB_HostManager_HDI_Compatibility_5200, Function | MediumTest | Level1)
2259762338dSopenharmony_ci{
2269762338dSopenharmony_ci    int32_t interfaceId = INTERFACEID_INVALID;
2279762338dSopenharmony_ci    uint8_t altIndex = INDEX_INVALID;
2289762338dSopenharmony_ci    struct UsbDev dev = dev_;
2299762338dSopenharmony_ci    dev.devAddr = DEV_ADDR_INVALID;
2309762338dSopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
2319762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::SUB_USB_HostManager_HDI_Compatibility_5200 %{public}d ret=%{public}d", __LINE__, ret);
2329762338dSopenharmony_ci    ASSERT_NE(ret, 0);
2339762338dSopenharmony_ci}
2349762338dSopenharmony_ci
2359762338dSopenharmony_ci/**
2369762338dSopenharmony_ci * @tc.name: SUB_USB_HostManager_HDI_Compatibility_5300
2379762338dSopenharmony_ci * @tc.desc: Test functions to SetInterface
2389762338dSopenharmony_ci * @tc.desc: int32_t SetInterface(const UsbDev &dev, uint8_t interfaceId, uint8_t altIndex);
2399762338dSopenharmony_ci * @tc.desc: Negative test: parameters exception, busNum && devAddr && interfaceId error
2409762338dSopenharmony_ci * @tc.type: FUNC
2419762338dSopenharmony_ci */
2429762338dSopenharmony_ciHWTEST_F(UsbdInterfaceTest, SUB_USB_HostManager_HDI_Compatibility_5300, Function | MediumTest | Level1)
2439762338dSopenharmony_ci{
2449762338dSopenharmony_ci    uint8_t altIndex = INDEX_INVALID;
2459762338dSopenharmony_ci    int32_t interfaceId = INTERFACEID_INVALID;
2469762338dSopenharmony_ci    struct UsbDev dev = dev_;
2479762338dSopenharmony_ci    dev.busNum = BUS_NUM_INVALID;
2489762338dSopenharmony_ci    dev.devAddr = DEV_ADDR_INVALID;
2499762338dSopenharmony_ci    auto ret = g_usbInterface->SetInterface(dev, interfaceId, altIndex);
2509762338dSopenharmony_ci    HDF_LOGI("UsbdInterfaceTest::SUB_USB_HostManager_HDI_Compatibility_5300 %{public}d ret=%{public}d", __LINE__, ret);
2519762338dSopenharmony_ci    ASSERT_NE(ret, 0);
2529762338dSopenharmony_ci}
2539762338dSopenharmony_ci} // namespace