• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "json/json.h"
18 #include "hilog_wrapper.h"
19 #define private public
20 #include "ibus_extension.h"
21 #include "usb_driver_info.h"
22 #include "usb_device_info.h"
23 #include "usb_bus_extension.h"
24 #undef private
25 
26 namespace OHOS {
27 namespace ExternalDeviceManager {
28 using namespace std;
29 using namespace testing::ext;
30 
31 class UsbBusExtensionTest : public testing::Test {
32 public:
SetUp()33     void SetUp() override
34     {
35         EDM_LOGD(MODULE_BUS_USB, "UsbBusExtensionTest SetUp");
36     }
TearDown()37     void TearDown() override
38     {
39         EDM_LOGD(MODULE_BUS_USB, "UsbBusExtensionTest TearDown");
40     }
41 };
42 
43 static const map<string, string> g_testMetaDatas = {
44     {"bus", "usb"},
45     {"desc", "test usb driver extension"},
46     {"vendor", "testVendor"},
47     {"pid", "0x1234,0x5678"},
48     {"vid", "0x1111,0x2222"}
49 };
50 
51 /**
52  * @tc.number: SUB_Driver_Ext_BusExtensionUSB_0200
53  * @tc.name: GetExtensionInstanceTest
54  * @tc.desc: Get UsbBusExtension instance
55  * @tc.size: MediumTest
56  * @tc.type: Function
57  */
58 HWTEST_F(UsbBusExtensionTest, SUB_Driver_Ext_BusExtensionUSB_0200, TestSize.Level1)
59 {
60     auto usbBus = make_shared<UsbBusExtension>();
61     ASSERT_NE(usbBus, nullptr);
62 }
63 
64 /**
65  * @tc.number: SUB_Driver_Ext_BusExtensionUSB_0300
66  * @tc.name: SetDevChangeCallbackTest
67  * @tc.desc: Test USB hot plug event callback
68  * @tc.size: MediumTest
69  * @tc.type: Function
70  */
71 HWTEST_F(UsbBusExtensionTest, SUB_Driver_Ext_BusExtensionUSB_0300, TestSize.Level1)
72 {
73     auto usbBus = make_shared<UsbBusExtension>();
74     ASSERT_NE(usbBus, nullptr);
75     auto ret = usbBus->SetDevChangeCallback(nullptr);
76     ASSERT_EQ(ret, 0);
77 }
78 
79 /**
80  * @tc.number: SUB_Driver_Ext_BusExtensionUSB_0400
81  * @tc.name: ParseDriverInfoTest
82  * @tc.desc: Test the extension DriverInfo in the parsing package
83  * @tc.size: MediumTest
84  * @tc.type: Function
85  */
86 HWTEST_F(UsbBusExtensionTest, SUB_Driver_Ext_BusExtensionUSB_0400, TestSize.Level1)
87 {
88     EDM_LOGI(MODULE_BUS_USB, "PraseDriverInfoTest Start");
89     auto usbBus = make_shared<UsbBusExtension>();
90     ASSERT_NE(usbBus, nullptr);
91     auto driverInfoExt = usbBus->ParseDriverInfo(g_testMetaDatas);
92     EDM_LOGD(MODULE_BUS_USB, "parse driver info done");
93     ASSERT_NE(driverInfoExt, nullptr);
94     UsbDriverInfo *usbDriverinfo = static_cast<UsbDriverInfo*>(driverInfoExt.get());
95     ASSERT_NE(usbDriverinfo, nullptr);
96     ASSERT_EQ(usbDriverinfo->pids_.size(), (size_t)2);
97     ASSERT_EQ(usbDriverinfo->vids_.size(), (size_t)2);
98     ASSERT_EQ(usbDriverinfo->pids_[0], 0x1234);
99     ASSERT_EQ(usbDriverinfo->pids_[1], 0x5678);
100     ASSERT_EQ(usbDriverinfo->vids_[0], 0x1111);
101     ASSERT_EQ(usbDriverinfo->vids_[1], 0x2222);
102 }
103 
104 /**
105  * @tc.number: SUB_Driver_Ext_BusExtensionUSB_0500
106  * @tc.name: MatchDriverTest
107  * @tc.desc: Test equipment and driver matching function
108  * @tc.size: MediumTest
109  * @tc.type: Function
110  */
111 HWTEST_F(UsbBusExtensionTest, SUB_Driver_Ext_BusExtensionUSB_0500, TestSize.Level1)
112 {
113     auto usbDrvInfo = make_shared<UsbDriverInfo>();
114     usbDrvInfo->pids_.push_back(0x1234);
115     usbDrvInfo->pids_.push_back(0x5678);
116     usbDrvInfo->vids_.push_back(0x1111);
117     usbDrvInfo->vids_.push_back(0x2222);
118     auto drvInfo = make_shared<DriverInfo>();
119     drvInfo->bus_ = "USB";
120     drvInfo->vendor_ = "TestVendor";
121     drvInfo->version_ = "0.1.1";
122     drvInfo->driverInfoExt_ = usbDrvInfo;
123     string drvInfoStr;
124     EDM_LOGD(MODULE_BUS_USB, "build driverInfo Done");
125 
126     auto deviceInfo = make_shared<UsbDeviceInfo>(0);
127     deviceInfo->devInfo_.devBusInfo.busType = BusType::BUS_TYPE_USB;
128     deviceInfo->idProduct_ = 0x1234;
129     deviceInfo->idVendor_ = 0x1111;
130     deviceInfo->deviceClass_ = 0;
131     deviceInfo->bcdUSB_ = 0x1122;
132 
133     auto usbBus = make_shared<UsbBusExtension>();
134     bool isMatched = usbBus->MatchDriver(*drvInfo, *deviceInfo);
135     ASSERT_EQ(isMatched, true);
136 
137     UsbDeviceInfo deviceInfo2 = *deviceInfo;
138     deviceInfo2.idProduct_ = 0x9999;
139     isMatched = usbBus->MatchDriver(*drvInfo, deviceInfo2);
140     ASSERT_EQ(isMatched, false);
141 
142     UsbDeviceInfo deviceInfo3 = *deviceInfo;
143     deviceInfo3.idVendor_ = 0x9999;
144     isMatched = usbBus->MatchDriver(*drvInfo, deviceInfo3);
145     ASSERT_EQ(isMatched, false);
146 
147     UsbDeviceInfo deviceInfo4 = *deviceInfo;
148     deviceInfo4.devInfo_.devBusInfo.busType = BusType::BUS_TYPE_INVALID;
149     isMatched = usbBus->MatchDriver(*drvInfo, deviceInfo4);
150     ASSERT_EQ(isMatched, false);
151 
152     drvInfo->bus_ = BusType::BUS_TYPE_INVALID;
153     isMatched = usbBus->MatchDriver(*drvInfo, *deviceInfo);
154     ASSERT_EQ(isMatched, false);
155 }
156 }
157 }
158