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