1 /*
2 * Copyright (c) 2021-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 <cstdio>
17 #include <cstring>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20
21 #include "common_list.h"
22 #include "softbus_conn_interface.h"
23 #include "softbus_conn_manager.h"
24 #include "softbus_def.h"
25 #include "softbus_errcode.h"
26 #include "softbus_feature_config.h"
27 #include "softbus_adapter_mem.h"
28 #include "softbus_conn_ble_send_queue.h"
29 #include "softbus_conn_ble_trans.h"
30 #include "connection_ble_mock.h"
31
32 using namespace testing;
33 using namespace testing::ext;
34 namespace OHOS {
35 static uint32_t g_connId;
36
ConnectedCB(unsigned int connectionId,const ConnectionInfo * info)37 void ConnectedCB(unsigned int connectionId, const ConnectionInfo *info)
38 {
39 if (info->type == CONNECT_BLE) {
40 g_connId = connectionId;
41 }
42 }
43
DisConnectCB(unsigned int connectionId,const ConnectionInfo * info)44 void DisConnectCB(unsigned int connectionId, const ConnectionInfo *info) {}
DataReceivedCB(unsigned int connectionId,ConnModule moduleId,int64_t seq,char * data,int len)45 void DataReceivedCB(unsigned int connectionId, ConnModule moduleId, int64_t seq, char *data, int len) {}
46
47 class ConnectionBleTest : public testing::Test {
48 public:
49 static void SetUpTestCase();
50 static void TearDownTestCase();
SetUp()51 void SetUp() override {}
TearDown()52 void TearDown() override {}
53 };
54
SetUpTestCase()55 void ConnectionBleTest::SetUpTestCase()
56 {
57 LooperInit();
58 SoftbusConfigInit();
59 ConnServerInit();
60 }
61
TearDownTestCase()62 void ConnectionBleTest::TearDownTestCase()
63 {
64 LooperDeinit();
65 }
66
67 /*
68 * @tc.name: ManagerTest001
69 * @tc.desc: test ConnTypeIsSupport
70 * @tc.type: FUNC
71 * @tc.require:
72 */
73 HWTEST_F(ConnectionBleTest, ManagerTest001, TestSize.Level1)
74 {
75 int ret;
76 ret = ConnTypeIsSupport(CONNECT_BLE);
77 EXPECT_EQ(ret, SOFTBUS_OK);
78 }
79
80 /*
81 * @tc.name: ManagerTest002
82 * @tc.desc: test invalid param
83 * @tc.type: FUNC
84 * @tc.require:
85 */
86 HWTEST_F(ConnectionBleTest, ManagerTest002, TestSize.Level1)
87 {
88 int ret = ConnSetConnectCallback(static_cast<ConnModule>(0), nullptr);
89 ASSERT_TRUE(ret != SOFTBUS_OK);
90 ret = ConnConnectDevice(nullptr, 0, nullptr);
91 ASSERT_TRUE(ret != SOFTBUS_OK);
92 ret = ConnDisconnectDevice(0);
93 ASSERT_TRUE(ret != SOFTBUS_OK);
94 ret = ConnPostBytes(0, nullptr);
95 ASSERT_TRUE(ret != SOFTBUS_OK);
96 ret = ConnStartLocalListening(nullptr);
97 ASSERT_TRUE(ret != SOFTBUS_OK);
98 ret = ConnStopLocalListening(nullptr);
99 ASSERT_TRUE(ret != SOFTBUS_OK);
100 }
101
102 /*
103 * @tc.name: ManagerTest003
104 * @tc.desc: test set unset callback and post disconnect without connect
105 * @tc.type: FUNC
106 * @tc.require:
107 */
108 HWTEST_F(ConnectionBleTest, ManagerTest003, TestSize.Level1)
109 {
110 ConnectCallback connCb;
111 connCb.OnConnected = ConnectedCB;
112 connCb.OnDisconnected = DisConnectCB;
113 connCb.OnDataReceived = DataReceivedCB;
114 int ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
115 EXPECT_EQ(SOFTBUS_OK, ret);
116 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
117 g_connId = 0;
118 }
119
120 /*
121 * @tc.name: ManagerTest004
122 * @tc.desc: Test start stop listening.
123 * @tc.in: Test module, Test number,Test Levels.
124 * @tc.out: NonZero
125 * @tc.type: FUNC
126 * @tc.require: The ConnStartLocalListening and ConnStopLocalListening operates normally.
127 */
128 HWTEST_F(ConnectionBleTest, ManagerTest004, TestSize.Level1)
129 {
130 ConnectCallback connCb;
131 connCb.OnConnected = ConnectedCB;
132 connCb.OnDisconnected = DisConnectCB;
133 connCb.OnDataReceived = DataReceivedCB;
134 int ret = ConnSetConnectCallback(MODULE_TRUST_ENGINE, &connCb);
135 EXPECT_EQ(ret, SOFTBUS_OK);
136
137 NiceMock<ConnectionBleInterfaceMock> bleMock;
138 EXPECT_CALL(bleMock, BleGattsAddService).WillRepeatedly(Return(SOFTBUS_OK));
139 LocalListenerInfo info;
140 info.type = CONNECT_BLE;
141 ret = ConnStartLocalListening(&info);
142 EXPECT_EQ(ret, SOFTBUS_OK);
143 ret = ConnStopLocalListening(&info);
144 EXPECT_EQ(ret, SOFTBUS_OK);
145 ConnUnSetConnectCallback(MODULE_TRUST_ENGINE);
146 }
147
148 /*
149 * @tc.name: ManagerTest005
150 * @tc.desc: Test ConnTypeIsSupport.
151 * @tc.in: Test module, Test number, Test Levels.
152 * @tc.out: NonZero
153 * @tc.type: FUNC
154 * @tc.require: The ConnTypeIsSupport operates normally.
155 */
156 HWTEST_F(ConnectionBleTest, ManagerTest005, TestSize.Level1)
157 {
158 int ret = ConnTypeIsSupport(CONNECT_P2P);
159 EXPECT_EQ(SOFTBUS_CONN_MANAGER_OP_NOT_SUPPORT, ret);
160 }
161
162 /*
163 * @tc.name: ManagerTest006
164 * @tc.desc: Test ConnTypeIsSupport.
165 * @tc.in: Test module, Test number, Test Levels.
166 * @tc.out: Zero
167 * @tc.type: FUNC
168 * @tc.require: The ConnTypeIsSupport operates normally.
169 */
170 HWTEST_F(ConnectionBleTest, ManagerTest006, TestSize.Level1)
171 {
172 int ret = ConnTypeIsSupport(CONNECT_BR);
173 EXPECT_EQ(SOFTBUS_OK, ret);
174 }
175
176 /*
177 * @tc.name: ManagerTest007
178 * @tc.desc: Test ConnTypeIsSupport.
179 * @tc.in: Test module, Test number, Test Levels.
180 * @tc.out: Zero
181 * @tc.type: FUNC
182 * @tc.require: The ConnTypeIsSupport operates normally.
183 */
184 HWTEST_F(ConnectionBleTest, ManagerTest007, TestSize.Level1)
185 {
186 int ret = ConnTypeIsSupport(CONNECT_TCP);
187 EXPECT_EQ(SOFTBUS_OK, ret);
188 }
189 }
190