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 namespace OHOS {
30 enum TEST_PROCESS {
31 TEST_INICIAL = 0,
32 TEST_BEGIN,
33 TEST_DEVICEFOUND,
34 TEST_SESSIONOPEN,
35 TEST_DATARECEIVE,
36 TEST_SESSIONCLOSE,
37 };
38 const char *g_pkgName = "com.plrdtest";
39 const char *g_sessionName = "com.plrdtest.dsoftbus";
40 const char *g_testData = "{\"data\":\"open auth session test!!!\"}";
41 bool g_state = false;
42 int g_sessionId = -1;
43 int g_testCount = 0;
44 int g_testTimes = 0;
45 static void Wait(void);
46 static void Start(void);
47
48 ConnectionAddr g_addr;
49 ConnectionAddr g_addr1;
50 class BleAuthChannelPhoneTest : public testing::Test {
51 public:
BleAuthChannelPhoneTest()52 BleAuthChannelPhoneTest()
53 {}
~BleAuthChannelPhoneTest()54 ~BleAuthChannelPhoneTest()
55 {}
56 static void SetUpTestCase(void);
57 static void TearDownTestCase(void);
SetUp()58 void SetUp() override
59 {}
TearDown()60 void TearDown() override
61 {}
62 };
63
SetUpTestCase(void)64 void BleAuthChannelPhoneTest::SetUpTestCase(void)
65 {
66 printf("input test times:");
67 if (scanf_s("%d", &g_testTimes, sizeof(g_testTimes)) < 0) {
68 printf("input error!\n");
69 }
70 getchar();
71 }
72
TearDownTestCase(void)73 void BleAuthChannelPhoneTest::TearDownTestCase(void)
74 {}
75
76 static SubscribeInfo g_sInfo = {
77 .subscribeId = 1,
78 .medium = BLE,
79 .mode = DISCOVER_MODE_ACTIVE,
80 .freq = MID,
81 .capability = "dvKit",
82 .capabilityData = (unsigned char *)"capdata3",
83 .dataLen = sizeof("capdata3"),
84 .isSameAccount = false,
85 .isWakeRemote = false
86 };
87
TestDeviceFound(const DeviceInfo * device)88 static void TestDeviceFound(const DeviceInfo *device)
89 {
90 if (ConvertBtMacToStr(g_addr.info.ble.bleMac, 18, (const uint8_t *)&(device->addr[0].info.ble.bleMac[0]), 6) != 0) {
91 return;
92 }
93 if (strcmp(g_addr1.info.ble.bleMac, g_addr.info.ble.bleMac) != 0) {
94 EXPECT_TRUE(g_testCount == TEST_BEGIN);
95 g_testCount++;
96 if (strcpy_s(g_addr1.info.ble.bleMac, BT_MAC_LEN, g_addr.info.ble.bleMac) != EOK) {
97 return;
98 }
99 printf("[client]TestDeviceFound\r\n");
100 printf("addr mac = %s\r\n", g_addr1.info.ble.bleMac);
101 printf("account = %s\r\n", device->hwAccountHash);
102 g_state = true;
103 }
104 }
105
TestDiscoverFailed(int subscribeId,DiscoveryFailReason failReason)106 static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason)
107 {
108 printf("[test]TestDiscoverFailed\r\n");
109 }
110
TestDiscoverySuccess(int subscribeId)111 static void TestDiscoverySuccess(int subscribeId)
112 {
113 printf("[test]TestDiscoverySuccess\r\n");
114 }
115
TestPublishSuccess(int publishId)116 static void TestPublishSuccess(int publishId)
117 {
118 printf("[test]TestPublishSuccess\r\n");
119 }
120
TestPublishFail(int publishId,PublishFailReason reason)121 static void TestPublishFail(int publishId, PublishFailReason reason)
122 {
123 printf("[test]TestPublishFail\r\n");
124 }
125
126 static IDiscoveryCallback g_subscribeCb = {
127 .OnDeviceFound = TestDeviceFound,
128 .OnDiscoverFailed = TestDiscoverFailed,
129 .OnDiscoverySuccess = TestDiscoverySuccess
130 };
131
132 static IPublishCallback g_publishCb = {
133 .OnPublishSuccess = TestPublishSuccess,
134 .OnPublishFail = TestPublishFail
135 };
136
OnSessionOpened(int sessionId,int result)137 static int OnSessionOpened(int sessionId, int result)
138 {
139 printf("[test]session opened,sesison id = %d\r\n", sessionId);
140 EXPECT_TRUE(g_sessionId == sessionId);
141 EXPECT_TRUE(g_testCount == TEST_DEVICEFOUND);
142 g_testCount++;
143 Start();
144 return 0;
145 }
146
OnSessionClosed(int sessionId)147 static void OnSessionClosed(int sessionId)
148 {
149 printf("[test]session closed, session id = %d\r\n", sessionId);
150 }
151
OnBytesReceived(int sessionId,const void * data,unsigned int len)152 static void OnBytesReceived(int sessionId, const void *data, unsigned int len)
153 {
154 printf("[test]session bytes received, session id = %d data =%s\r\n", sessionId, data);
155 }
156
OnMessageReceived(int sessionId,const void * data,unsigned int len)157 static void OnMessageReceived(int sessionId, const void *data, unsigned int len)
158 {
159 printf("[test]session msg received, session id = %d data =%s\r\n", sessionId, data);
160 }
161
162 static ISessionListener g_sessionlistener = {
163 .OnSessionOpened = OnSessionOpened,
164 .OnSessionClosed = OnSessionClosed,
165 .OnBytesReceived = OnBytesReceived,
166 .OnMessageReceived = OnMessageReceived,
167 };
168
Wait(void)169 static void Wait(void)
170 {
171 printf("[test]wait enter...\r\n");
172 do {
173 sleep(1);
174 } while (!g_state);
175 printf("[test]wait end!\r\n");
176 g_state = false;
177 }
178
Start(void)179 static void Start(void)
180 {
181 g_state = true;
182 }
183
TestStartDiscovery()184 static int32_t TestStartDiscovery()
185 {
186 printf("[test]TestStartDiscovery enter\r\n");
187 g_sInfo.mode = DISCOVER_MODE_ACTIVE;
188 int32_t ret = StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
189 EXPECT_TRUE(ret == 0);
190 printf("[test]TestStartDiscovery end\r\n");
191 return ret;
192 }
193
TestCreateSessionServer()194 static int32_t TestCreateSessionServer()
195 {
196 printf("[test]TestCreateSessionServer enter\r\n");
197 int32_t ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
198 EXPECT_TRUE(ret == 0);
199 printf("[test]TestCreateSessionServer end\r\n");
200 return ret;
201 }
202
TestOpenSession()203 static int32_t TestOpenSession()
204 {
205 printf("[test]TestOpenSession enter\r\n");
206 g_addr1.type = CONNECTION_ADDR_BLE;
207 int32_t ret = OpenAuthSession(g_sessionName, &g_addr1, 1, NULL);
208 EXPECT_TRUE(ret >= 0);
209 printf("[test]TestOpenSession end\r\n");
210 return ret;
211 }
212
TestSendData(const char * data,int32_t len)213 static int32_t TestSendData(const char *data, int32_t len)
214 {
215 printf("[test]TestSendData enter\r\n");
216 int32_t ret = SendBytes(g_sessionId, data, len);
217 EXPECT_TRUE(ret == 0);
218 printf("[test]TestSendData end\r\n");
219 return ret;
220 }
221
TestCloseSeeesion()222 static void TestCloseSeeesion()
223 {
224 printf("[test]TestCloseSession enter\n");
225 if (g_sessionId > 0) {
226 CloseSession(g_sessionId);
227 g_sessionId = -1;
228 }
229 printf("[test]TestCloseSession end\n");
230 }
231
TestRemoveSessionServer()232 static int32_t TestRemoveSessionServer()
233 {
234 printf("[test]TestRemoveSessionServer enter\r\n");
235 int32_t ret = RemoveSessionServer(g_pkgName, g_sessionName);
236 EXPECT_TRUE(ret == 0);
237 printf("[test]TestRemoveSessionServer end\r\n");
238 return ret;
239 }
240
241 /**
242 * @tc.name: PublishServiceTest001
243 * @tc.desc: Verify wrong parameter
244 * @tc.type: FUNC
245 * @tc.require:
246 */
247 HWTEST_F(BleAuthChannelPhoneTest, ProcessPhoneActive001, TestSize.Level0)
248 {
249 int32_t ret;
250 g_testCount = TEST_BEGIN;
251 ret = TestStartDiscovery();
252 EXPECT_TRUE(ret == 0);
253 Wait();
254 ret = TestCreateSessionServer();
255 EXPECT_TRUE(ret == 0);
256 EXPECT_TRUE(g_testCount == TEST_DEVICEFOUND);
257 for (int i = 0; i < g_testTimes; i++) {
258 g_testCount = TEST_DEVICEFOUND;
259 g_sessionId = TestOpenSession();
260 EXPECT_TRUE(g_sessionId >= 0);
261 Wait();
262 EXPECT_TRUE(g_testCount == TEST_SESSIONOPEN);
263 ret = TestSendData(g_testData, strlen(g_testData) + 1);
264 EXPECT_TRUE(ret == 0);
265 sleep(3);
266 TestCloseSeeesion();
267 sleep(5);
268 }
269 ret = TestRemoveSessionServer();
270 EXPECT_TRUE(ret == 0);
271 END:
272 EXPECT_TRUE(TEST_INICIAL == 0);
273 };
274 }
275