1 /*
2  * Copyright (c) 2023 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 <iostream>
17 #include <gtest/gtest.h>
18 #include "hdf_log.h"
19 #include "if_system_ability_manager.h"
20 #include "system_ability_definition.h"
21 #include "usbd_function.h"
22 #include "usbd_port.h"
23 #include "v1_0/iusb_interface.h"
24 #include "v1_0/usb_types.h"
25 
26 constexpr int32_t PORT_ID_INVALID = 2;
27 constexpr int32_t POWER_ROLE_INVALID = 4;
28 constexpr int32_t DATA_ROLE_INVALID = 5;
29 constexpr int32_t USB_FUNCTION_UNSUPPORTED = 128;
30 
31 using namespace testing::ext;
32 using namespace OHOS;
33 using namespace std;
34 using namespace OHOS::HDI::Usb::V1_0;
35 
36 namespace {
37 sptr<IUsbInterface> g_usbInterface = nullptr;
38 
39 class UsbdFunctionTest : public testing::Test {
40 public:
41     static void SetUpTestCase(void);
42     static void TearDownTestCase(void);
43 };
44 
SetUpTestCase(void)45 void UsbdFunctionTest::SetUpTestCase(void)
46 {
47     g_usbInterface = IUsbInterface::Get();
48     if (g_usbInterface == nullptr) {
49         HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
50         exit(0);
51     }
52 }
53 
TearDownTestCase(void)54 void UsbdFunctionTest::TearDownTestCase(void) {}
55 
56 /**
57  * @tc.name: UsbdSetCurrentFunctions001
58  * @tc.desc: Test functions to SetCurrentFunctions
59  * @tc.desc: int32_t SetCurrentFunctions(int32_t funcs)
60  * @tc.desc: Negative test: parameters exception, funcs error
61  * @tc.type: FUNC
62  */
63 HWTEST_F(UsbdFunctionTest, UsbdSetCurrentFunctions001, TestSize.Level1)
64 {
65     auto ret = g_usbInterface->SetCurrentFunctions(USB_FUNCTION_UNSUPPORTED);
66     HDF_LOGI("UsbdFunctionTest::UsbdSetCurrentFunctions001 %{public}d ret=%{public}d", __LINE__, ret);
67     ASSERT_NE(ret, 0);
68 }
69 
70 /**
71  * @tc.name: UsbdSetPortRole001
72  * @tc.desc: Test functions to SetPortRole
73  * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
74  * @tc.desc: Negative test: parameters exception, portId && powerRole && dataRole error
75  * @tc.type: FUNC
76  */
77 HWTEST_F(UsbdFunctionTest, UsbdSetPortRole001, TestSize.Level1)
78 {
79     auto ret = g_usbInterface->SetPortRole(PORT_ID_INVALID, POWER_ROLE_INVALID, DATA_ROLE_INVALID);
80     HDF_LOGI("UsbdFunctionTest::UsbdSetPortRole001 %{public}d ret=%{public}d", __LINE__, ret);
81     ASSERT_NE(ret, 0);
82 }
83 
84 /**
85  * @tc.name: UsbdSetPortRole002
86  * @tc.desc: Test functions to SetPortRole
87  * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
88  * @tc.desc: Negative test: parameters exception, portId error
89  * @tc.type: FUNC
90  */
91 HWTEST_F(UsbdFunctionTest, UsbdSetPortRole002, TestSize.Level1)
92 {
93     auto ret = g_usbInterface->SetPortRole(PORT_ID_INVALID, POWER_ROLE_SOURCE, DATA_ROLE_HOST);
94     HDF_LOGI("UsbdFunctionTest::UsbdSetPortRole002 %{public}d ret=%{public}d", __LINE__, ret);
95     ASSERT_NE(ret, 0);
96 }
97 
98 /**
99  * @tc.name: UsbdSetPortRole003
100  * @tc.desc: Test functions to SetPortRole
101  * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
102  * @tc.desc: Negative test: parameters exception, powerRole error
103  * @tc.type: FUNC
104  */
105 HWTEST_F(UsbdFunctionTest, UsbdSetPortRole003, TestSize.Level1)
106 {
107     auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_INVALID, DATA_ROLE_DEVICE);
108     HDF_LOGI("UsbdFunctionTest::UsbdSetPortRole003 %{public}d ret=%{public}d", __LINE__, ret);
109     ASSERT_NE(ret, 0);
110 }
111 
112 /**
113  * @tc.name: UsbdSetPortRole004
114  * @tc.desc: Test functions to SetPortRole
115  * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
116  * @tc.desc: Negative test: parameters exception, dataRole error
117  * @tc.type: FUNC
118  */
119 HWTEST_F(UsbdFunctionTest, UsbdSetPortRole004, TestSize.Level1)
120 {
121     auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_SOURCE, DATA_ROLE_INVALID);
122     HDF_LOGI("UsbdFunctionTest::UsbdSetPortRole004 %{public}d ret=%{public}d", __LINE__, ret);
123     ASSERT_NE(ret, 0);
124 }
125 
126 /**
127  * @tc.name: UsbdSetPortRole005
128  * @tc.desc: Test functions to SetPortRole
129  * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
130  * @tc.desc: Negative test: parameters exception, portId && powerRole error
131  * @tc.type: FUNC
132  */
133 HWTEST_F(UsbdFunctionTest, UsbdSetPortRole005, TestSize.Level1)
134 {
135     auto ret = g_usbInterface->SetPortRole(DEFAULT_PORT_ID, POWER_ROLE_INVALID, DATA_ROLE_INVALID);
136     HDF_LOGI("UsbdFunctionTest::UsbdSetPortRole005 %{public}d ret=%{public}d", __LINE__, ret);
137     ASSERT_NE(ret, 0);
138 }
139 
140 /**
141  * @tc.name: UsbdSetPortRole006
142  * @tc.desc: Test functions to SetPortRole
143  * @tc.desc: int32_t SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)
144  * @tc.desc: Negative test: parameters exception, portId && dataRole error
145  * @tc.type: FUNC
146  */
147 HWTEST_F(UsbdFunctionTest, UsbdSetPortRole006, TestSize.Level1)
148 {
149     auto ret = g_usbInterface->SetPortRole(PORT_ID_INVALID, POWER_ROLE_SOURCE, DATA_ROLE_INVALID);
150     HDF_LOGI("UsbdFunctionTest::UsbdSetPortRole006 %{public}d ret=%{public}d", __LINE__, ret);
151     ASSERT_NE(ret, 0);
152 }
153 
154 /**
155  * @tc.name: QueryPort001
156  * @tc.desc: Test functions to QueryPort
157  * @tc.desc: int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
158  * @tc.desc: Positive test: parameters correctly
159  * @tc.type: FUNC
160  */
161 HWTEST_F(UsbdFunctionTest, QueryPort001, TestSize.Level1)
162 {
163     int32_t portId = 0;
164     int32_t powerRole = 0;
165     int32_t dataRole = 0;
166     int32_t mode = 0;
167     auto ret = g_usbInterface->QueryPort(portId, powerRole, dataRole, mode);
168     HDF_LOGI("UsbdFunctionTest::QueryPort001 %{public}d ret=%{public}d", __LINE__, ret);
169     ASSERT_EQ(0, ret);
170 }
171 } // namespace
172