1 /*
2 * Copyright (c) 2022 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 <ctime>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include <sys/time.h>
21 #include <unistd.h>
22
23 #include "discovery_service.h"
24 #include "inner_session.h"
25 #include "session.h"
26 #include "softbus_utils.h"
27
28 using namespace testing::ext;
29
30 namespace OHOS {
31 int32_t g_testWay = -1;
32 enum TEST_WAY {
33 PASSIVE_OPENAUTHSESSION_WAY = 0,
34 ACTIVE_OPENAUTHSESSION_WAY
35 };
36
37 enum TEST_PROCESS {
38 TEST_INICIAL = 0,
39 TEST_BEGIN,
40 TEST_DEVICEFOUND,
41 TEST_SESSIONOPEN,
42 TEST_DATARECEIVE,
43 TEST_SESSIONCLOSE,
44 };
45 const char *g_pkgName = "com.objectstore.foundation";
46 const char *g_sessionName = "objectstore";
47 const char *g_testData = "{\"data\":\"open auth session test!!!\"}";
48 bool g_state = false;
49 int g_sessionId = -1;
50 int g_testCount = 0;
51 int g_testTimes = 0;
52 static void Wait(void);
53 static void Start(void);
54
55 ConnectionAddr g_addr;
56 ConnectionAddr g_addr1;
57 class BleAuthChannelTest : public testing::Test {
58 public:
BleAuthChannelTest()59 BleAuthChannelTest()
60 {}
~BleAuthChannelTest()61 ~BleAuthChannelTest()
62 {}
63 static void SetUpTestCase(void);
64 static void TearDownTestCase(void);
SetUp()65 void SetUp() override
66 {}
TearDown()67 void TearDown() override
68 {}
69 };
70
SetUpTestCase(void)71 void BleAuthChannelTest::SetUpTestCase(void)
72 {
73 printf("********Ble Test Begin*********\r\n");
74 printf("* 0.passive openAuthSession *\r\n");
75 printf("* 1.active openAuthSession *\r\n");
76 printf("********************************\r\n");
77 printf("input the num:");
78 if (scanf_s("%d", &g_testWay, sizeof(g_testWay)) < 0) {
79 printf("input error!\n");
80 }
81 getchar();
82 printf("input test times:");
83 if (scanf_s("%d", &g_testTimes, sizeof(g_testTimes)) < 0) {
84 printf("input error!\n");
85 }
86 getchar();
87 }
88
TearDownTestCase(void)89 void BleAuthChannelTest::TearDownTestCase(void)
90 {}
91
92 static SubscribeInfo g_sInfo = {
93 .subscribeId = 1,
94 .medium = BLE,
95 .mode = DISCOVER_MODE_ACTIVE,
96 .freq = MID,
97 .capability = "dvKit",
98 .capabilityData = (unsigned char *)"capdata3",
99 .dataLen = sizeof("capdata3"),
100 .isSameAccount = false,
101 .isWakeRemote = false
102 };
103
104 static PublishInfo g_pInfo = {
105 .publishId = 1,
106 .medium = BLE,
107 .mode = DISCOVER_MODE_PASSIVE,
108 .freq = MID,
109 .capability = "dvKit",
110 .capabilityData = (unsigned char *)"capdata4",
111 .dataLen = sizeof("capdata4")
112 };
113
TestDeviceFound(const DeviceInfo * device)114 static void TestDeviceFound(const DeviceInfo *device)
115 {
116 if (ConvertBtMacToStr(g_addr.info.ble.bleMac, 18, (const uint8_t *)&(device->addr[0].info.ble.bleMac[0]), 6) != 0) {
117 return;
118 }
119 if (strcmp(g_addr1.info.ble.bleMac, g_addr.info.ble.bleMac) != 0) {
120 EXPECT_TRUE(g_testCount == TEST_BEGIN);
121 g_testCount++;
122 if (strcpy_s(g_addr1.info.ble.bleMac, BT_MAC_LEN, g_addr.info.ble.bleMac) != EOK) {
123 return;
124 }
125 printf("[client]TestDeviceFound\r\n");
126 printf("addr mac = %s\r\n", g_addr1.info.ble.bleMac);
127 printf("account = %s\r\n", device->hwAccountHash);
128 g_state = true;
129 }
130 }
131
TestDiscoverFailed(int subscribeId,DiscoveryFailReason failReason)132 static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason)
133 {
134 printf("[test]TestDiscoverFailed\r\n");
135 }
136
TestDiscoverySuccess(int subscribeId)137 static void TestDiscoverySuccess(int subscribeId)
138 {
139 printf("[test]TestDiscoverySuccess\r\n");
140 }
141
TestPublishSuccess(int publishId)142 static void TestPublishSuccess(int publishId)
143 {
144 printf("[test]TestPublishSuccess\r\n");
145 }
146
TestPublishFail(int publishId,PublishFailReason reason)147 static void TestPublishFail(int publishId, PublishFailReason reason)
148 {
149 printf("[test]TestPublishFail\r\n");
150 }
151
152 static IDiscoveryCallback g_subscribeCb = {
153 .OnDeviceFound = TestDeviceFound,
154 .OnDiscoverFailed = TestDiscoverFailed,
155 .OnDiscoverySuccess = TestDiscoverySuccess
156 };
157
158 static IPublishCallback g_publishCb = {
159 .OnPublishSuccess = TestPublishSuccess,
160 .OnPublishFail = TestPublishFail
161 };
162
OnSessionOpened(int sessionId,int result)163 static int OnSessionOpened(int sessionId, int result)
164 {
165 printf("[test]session opened,sesison id = %d\r\n", sessionId);
166 EXPECT_TRUE(g_sessionId == sessionId);
167 EXPECT_TRUE(g_testCount == TEST_DEVICEFOUND);
168 g_testCount++;
169 Start();
170 return 0;
171 }
172
OnSessionClosed(int sessionId)173 static void OnSessionClosed(int sessionId)
174 {
175 printf("[test]session closed, session id = %d\r\n", sessionId);
176 EXPECT_TRUE(g_testCount == TEST_DATARECEIVE);
177 g_testCount++;
178 Start();
179 }
180
OnBytesReceived(int sessionId,const void * data,unsigned int len)181 static void OnBytesReceived(int sessionId, const void *data, unsigned int len)
182 {
183 printf("[test]session bytes received, session id = %d data =%s\r\n", sessionId, data);
184 if (g_testWay == PASSIVE_OPENAUTHSESSION_WAY) {
185 SendBytes(sessionId, "{\"received ok\"}", strlen("{\"received ok\"}"));
186 }
187 EXPECT_TRUE(g_testCount == TEST_SESSIONOPEN);
188 g_testCount++;
189 Start();
190 }
191
OnMessageReceived(int sessionId,const void * data,unsigned int len)192 static void OnMessageReceived(int sessionId, const void *data, unsigned int len)
193 {
194 printf("[test]session msg received, session id = %d data =%s\r\n", sessionId, data);
195 }
196
197 static ISessionListener g_sessionlistener = {
198 .OnSessionOpened = OnSessionOpened,
199 .OnSessionClosed = OnSessionClosed,
200 .OnBytesReceived = OnBytesReceived,
201 .OnMessageReceived = OnMessageReceived,
202 };
203
Wait(void)204 static void Wait(void)
205 {
206 printf("[test]wait enter...\r\n");
207 do {
208 sleep(1);
209 } while (!g_state);
210 printf("[test]wait end!\r\n");
211 g_state = false;
212 }
213
Start(void)214 static void Start(void)
215 {
216 g_state = true;
217 }
218
TestPublishServer()219 static int32_t TestPublishServer()
220 {
221 printf("[test]TestPublishServer enter\r\n");
222 g_pInfo.mode = DISCOVER_MODE_ACTIVE;
223 int32_t ret = PublishService(g_pkgName, &g_pInfo, &g_publishCb);
224 EXPECT_TRUE(ret == 0);
225 printf("[test]TestPublishServer end\r\n");
226 return ret;
227 }
228
TestStartDiscovery()229 static int32_t TestStartDiscovery()
230 {
231 printf("[test]TestStartDiscovery enter\r\n");
232 g_sInfo.mode = DISCOVER_MODE_ACTIVE;
233 int32_t ret = StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
234 EXPECT_TRUE(ret == 0);
235 printf("[test]TestStartDiscovery end\r\n");
236 return ret;
237 }
238
TestCreateSessionServer()239 static int32_t TestCreateSessionServer()
240 {
241 printf("[test]TestCreateSessionServer enter\r\n");
242 int32_t ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
243 EXPECT_TRUE(ret == 0);
244 printf("[test]TestCreateSessionServer end\r\n");
245 return ret;
246 }
247
TestOpenSession()248 static int32_t TestOpenSession()
249 {
250 printf("[test]TestOpenSession enter\r\n");
251 g_addr1.type = CONNECTION_ADDR_BLE;
252 int32_t ret = OpenAuthSession(g_sessionName, &g_addr1, 1, NULL);
253 EXPECT_TRUE(ret >= 0);
254 printf("[test]TestOpenSession end\r\n");
255 return ret;
256 }
257
TestSendData(const char * data,int32_t len)258 static int32_t TestSendData(const char *data, int32_t len)
259 {
260 printf("[test]TestSendData enter\r\n");
261 int32_t ret = SendBytes(g_sessionId, data, len);
262 EXPECT_TRUE(ret == 0);
263 printf("[test]TestSendData end\r\n");
264 return ret;
265 }
266
TestCloseSeeesion()267 static void TestCloseSeeesion()
268 {
269 printf("[test]TestCloseSession enter\n");
270 if (g_sessionId > 0) {
271 CloseSession(g_sessionId);
272 g_sessionId = -1;
273 }
274 printf("[test]TestCloseSession end\n");
275 }
276
TestRemoveSessionServer()277 static int32_t TestRemoveSessionServer()
278 {
279 printf("[test]TestRemoveSessionServer enter\r\n");
280 int32_t ret = RemoveSessionServer(g_pkgName, g_sessionName);
281 EXPECT_TRUE(ret == 0);
282 printf("[test]TestRemoveSessionServer end\r\n");
283 return ret;
284 }
285
286 /**
287 * @tc.name: PublishServiceTest001
288 * @tc.desc: Verify wrong parameter
289 * @tc.type: FUNC
290 * @tc.require:
291 */
292 HWTEST_F(BleAuthChannelTest, ProcessActive001, TestSize.Level0)
293 {
294 if (g_testWay != ACTIVE_OPENAUTHSESSION_WAY) {
295 printf("[test]active test skip...\r\n");
296 EXPECT_TRUE(TEST_INICIAL == 0);
297 goto END;
298 }
299 int32_t ret;
300 g_testCount = TEST_BEGIN;
301 ret = TestStartDiscovery();
302 EXPECT_TRUE(ret == 0);
303 Wait();
304 ret = TestCreateSessionServer();
305 EXPECT_TRUE(ret == 0);
306 EXPECT_TRUE(g_testCount == TEST_DEVICEFOUND);
307 for (int i = 0; i < g_testTimes; i++) {
308 g_testCount = TEST_DEVICEFOUND;
309 g_sessionId = TestOpenSession();
310 EXPECT_TRUE(g_sessionId >= 0);
311 Wait();
312 EXPECT_TRUE(g_testCount == TEST_SESSIONOPEN);
313 ret = TestSendData(g_testData, strlen(g_testData) + 1);
314 EXPECT_TRUE(ret == 0);
315 Wait();
316 EXPECT_TRUE(g_testCount == TEST_DATARECEIVE);
317 TestCloseSeeesion();
318 sleep(5);
319 EXPECT_TRUE(g_testCount == TEST_DATARECEIVE);
320 }
321 ret = TestRemoveSessionServer();
322 EXPECT_TRUE(ret == 0);
323 END:
324 EXPECT_TRUE(TEST_INICIAL == 0);
325 };
326
327
328 /**
329 * @tc.name: PublishServiceTest001
330 * @tc.desc: Verify wrong parameter
331 * @tc.type: FUNC
332 * @tc.require:
333 */
334 HWTEST_F(BleAuthChannelTest, ProcessPassive001, TestSize.Level0)
335 {
336 if (g_testWay != PASSIVE_OPENAUTHSESSION_WAY) {
337 printf("[test]passive test skip...\r\n");
338 EXPECT_TRUE(TEST_INICIAL == 0);
339 goto END;
340 }
341 int32_t ret;
342 ret = TestPublishServer();
343 EXPECT_TRUE(ret == 0);
344 ret = TestCreateSessionServer();
345 EXPECT_TRUE(ret == 0);
346 for (int i = 0; i < g_testTimes; i++) {
347 g_testCount = TEST_DEVICEFOUND;
348 printf("[test][times:%d],Waiting for session opening...\r\n", i);
349 Wait();
350 EXPECT_TRUE(g_testCount == TEST_SESSIONOPEN);
351 printf("[test][times:%d],session opened,Waiting for data receiving...\r\n", i);
352 Wait();
353 EXPECT_TRUE(g_testCount == TEST_DATARECEIVE);
354 printf("[test][times:%d],data received,Waiting for session closing...\r\n", i);
355 Wait();
356 EXPECT_TRUE(g_testCount == TEST_SESSIONCLOSE);
357 printf("[test][times:%d],session closed,time time over\r\n", i);
358 }
359 printf("passive test over\r\n");
360 END:
361 EXPECT_TRUE(TEST_INICIAL == 0);
362 };
363 }
364