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