• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include <string>
18 
19 #include "dfs_session.h"
20 #include "inner_session.h"
21 #include "session.h"
22 #include "softbus_common.h"
23 #include "softbus_def.h"
24 #include "softbus_errcode.h"
25 #include "softbus_log.h"
26 #include "client_trans_session_manager.h"
27 #include "client_trans_session_service.h"
28 
29 using namespace testing::ext;
30 
31 namespace OHOS {
32 ConnectionAddr g_addrInfo;
33 
34 const char *g_testSessionName = "ohos.distributedschedule.dms.test";
35 std::string g_testData = "TranSessionTest_GetSessionKeyTestData";
36 
37 #define TEST_FILE_NAME "test.filename.01"
38 #define TEST_PKG_NAME_LEN (64)
39 #define TEST_SESSION_NAME_LEN (64)
40 #define TEST_NETWORK_ID_LEN (64)
41 #define TEST_GROUP_ID_LEN (64)
42 
43 class TransSessionTest : public testing::Test {
44 public:
TransSessionTest()45     TransSessionTest()
46     {}
~TransSessionTest()47     ~TransSessionTest()
48     {}
49     static void SetUpTestCase(void);
50     static void TearDownTestCase(void);
SetUp()51     void SetUp() override
52     {}
TearDown()53     void TearDown() override
54     {}
55 };
56 
SetUpTestCase(void)57 void TransSessionTest::SetUpTestCase(void)
58 {
59     (void)TransClientInit();
60 }
61 
TearDownTestCase(void)62 void TransSessionTest::TearDownTestCase(void)
63 {
64 }
OnSessionOpened(int sessionId,int result)65 static int OnSessionOpened(int sessionId, int result)
66 {
67     LOG_INFO("session opened,sesison id = %d\r\n", sessionId);
68     return SOFTBUS_OK;
69 }
70 
OnSessionClosed(int sessionId)71 static void OnSessionClosed(int sessionId)
72 {
73     LOG_INFO("session closed, session id = %d\r\n", sessionId);
74 }
75 
76 static ISessionListener g_sessionlistener = {
77     .OnSessionOpened = OnSessionOpened,
78     .OnSessionClosed = OnSessionClosed,
79 };
80 
81 /**
82  * @tc.name: GetSessionKeyTest001
83  * @tc.desc: use the wrong parameter.
84  * @tc.type: FUNC
85  * @tc.require: I5HZ6N
86  */
87 HWTEST_F(TransSessionTest, GetSessionKeyTest001, TestSize.Level0)
88 {
89     int32_t ret;
90     int32_t sessionId = 1;
91     char *key = (char *)g_testData.c_str();
92     unsigned int len = strlen(key);
93 
94     ret = GetSessionKey(-1, key, len);
95     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
96 
97     ret = GetSessionKey(sessionId, NULL, len);
98     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
99 
100     ret = GetSessionKey(sessionId, key, 0);
101     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
102 
103     ret = GetSessionKey(sessionId, key, SESSION_KEY_LEN - 1);
104     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
105 }
106 
107 /**
108  * @tc.name: GetSessionKeyTest002
109  * @tc.desc: use the normal parameter.
110  * @tc.type: FUNC
111  * @tc.require: I5HZ6N
112  */
113 HWTEST_F(TransSessionTest, GetSessionKeyTest002, TestSize.Level0)
114 {
115     int32_t ret;
116     int32_t sessionId = 1;
117     char *key = (char *)g_testData.c_str();
118     unsigned int len = strlen(key);
119 
120     ret = GetSessionKey(sessionId, key, len);
121     EXPECT_EQ(SOFTBUS_TRANS_FUNC_NOT_SUPPORT, ret);
122 }
123 
124 /**
125  * @tc.name: GetSessionHandleTest001
126  * @tc.desc: use the wrong parameter.
127  * @tc.type: FUNC
128  * @tc.require: I5HZ6N
129  */
130 HWTEST_F(TransSessionTest, GetSessionHandleTest001, TestSize.Level0)
131 {
132     int32_t ret;
133     int32_t sessionId = 1;
134     int handle = 1;
135 
136     ret = GetSessionHandle(-1, &handle);
137     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
138 
139     ret = GetSessionHandle(sessionId, NULL);
140     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
141 }
142 
143 /**
144  * @tc.name: GetSessionHandleTest002
145  * @tc.desc: use the normal parameter.
146  * @tc.type: FUNC
147  * @tc.require: I5HZ6N
148  */
149 HWTEST_F(TransSessionTest, GetSessionHandleTest002, TestSize.Level0)
150 {
151     int32_t ret;
152     int32_t sessionId = 1;
153     int handle = 1;
154 
155     ret = GetSessionHandle(sessionId, &handle);
156     EXPECT_EQ(SOFTBUS_TRANS_FUNC_NOT_SUPPORT, ret);
157 }
158 
159 /**
160  * @tc.name: DisableSessionListenerTest001
161  * @tc.desc: use the wrong parameter.
162  * @tc.type: FUNC
163  * @tc.require: I5HZ6N
164  */
165 HWTEST_F(TransSessionTest, DisableSessionListenerTest001, TestSize.Level0)
166 {
167     int32_t ret;
168 
169     ret = DisableSessionListener(-1);
170     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
171 }
172 
173 /**
174  * @tc.name: DisableSessionListenerTest002
175  * @tc.desc: use the normal parameter.
176  * @tc.type: FUNC
177  * @tc.require: I5HZ6N
178  */
179 HWTEST_F(TransSessionTest, DisableSessionListenerTest002, TestSize.Level0)
180 {
181     int32_t ret;
182     int32_t sessionId = 1;
183 
184     ret = DisableSessionListener(sessionId);
185     EXPECT_EQ(SOFTBUS_TRANS_FUNC_NOT_SUPPORT, ret);
186 }
187 
188 /**
189  * @tc.name: OpenAuthSessionTest001
190  * @tc.desc: use the wrong parameter.
191  * @tc.type: FUNC
192  * @tc.require: I5HZ6N
193  */
194 HWTEST_F(TransSessionTest, OpenAuthSessionTest001, TestSize.Level0)
195 {
196     int ret;
197 
198     ret = OpenAuthSession(NULL, &(g_addrInfo), 1, NULL);
199     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
200 
201     ret = OpenAuthSession(g_testSessionName, NULL, 1, NULL);
202     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
203 
204     ret = OpenAuthSession(g_testSessionName, &(g_addrInfo), -1, NULL);
205     EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
206 }
207 
208 /**
209  * @tc.name: OpenAuthSessionTest002
210  * @tc.desc: use the normal parameter.
211  * @tc.type: FUNC
212  * @tc.require:
213  */
214 HWTEST_F(TransSessionTest, OpenAuthSessionTest002, TestSize.Level0)
215 {
216     int ret;
217 
218     ret = OpenAuthSession(g_testSessionName, &(g_addrInfo), 1, NULL);
219     EXPECT_GE(SOFTBUS_OK, ret);
220 }
221 
222 /**
223  * @tc.name: NotifyAuthSuccessTest001
224  * @tc.desc: use the normal parameter.
225  * @tc.type: FUNC
226  * @tc.require:
227  */
228 HWTEST_F(TransSessionTest, NotifyAuthSuccessTest001, TestSize.Level0)
229 {
230     int32_t sessionId = 1;
231 
232     NotifyAuthSuccess(sessionId);
233 }
234 
235 /**
236  * @tc.name: SendFileTest001
237  * @tc.desc: use the normal parameter.
238  * @tc.type: FUNC
239  * @tc.require:
240  */
241 HWTEST_F(TransSessionTest, SendFileTest001, TestSize.Level0)
242 {
243     int32_t sessionId = 1;
244     int32_t ret = SendFile(sessionId, NULL, NULL, 1);
245     EXPECT_NE(SOFTBUS_OK, ret);
246 
247     const char *sFileList[] = { TEST_FILE_NAME };
248     ret = SendFile(sessionId, sFileList, NULL, 0);
249     EXPECT_NE(SOFTBUS_OK, ret);
250 
251     ret = SendFile(sessionId, sFileList, NULL, 1);
252     EXPECT_NE(SOFTBUS_OK, ret);
253 }
254 
255 /**
256  * @tc.name: SendFileTest002
257  * @tc.desc: use the normal parameter.
258  * @tc.type: FUNC
259  * @tc.require:
260  */
261 HWTEST_F(TransSessionTest, SendFileTest002, TestSize.Level0)
262 {
263     char pkgName[TEST_PKG_NAME_LEN] = "com.test.trans.session";
264     char mySessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
265     char peerSessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
266     char peerNetworkId[TEST_NETWORK_ID_LEN] = "1234567789";
267     char groupId[TEST_GROUP_ID_LEN] = "123";
268     SessionAttribute attr;
269     attr.dataType = 1;
270     attr.linkTypeNum = 0;
271     SessionParam param = {
272         .sessionName = mySessionName,
273         .peerSessionName = peerSessionName,
274         .peerDeviceId = peerNetworkId,
275         .groupId = groupId,
276         .attr = &attr,
277     };
278 
279     const char *sFileList[] = { TEST_FILE_NAME };
280     int32_t sessionId = INVALID_SESSION_ID;
281     bool isEnabled = false;
282     ISessionListener listener;
283     (void)ClientAddSessionServer(SEC_TYPE_CIPHERTEXT, pkgName, mySessionName, &listener);
284     (void)ClientAddSession(&param, &sessionId, &isEnabled);
285 
286     int32_t ret = SendFile(sessionId, sFileList, NULL, 1);
287     EXPECT_NE(SOFTBUS_OK, ret);
288 }
289 
290 /**
291  * @tc.name: QosReportTest001
292  * @tc.desc: use the normal parameter.
293  * @tc.type: FUNC
294  * @tc.require:
295  */
296 HWTEST_F(TransSessionTest, QosReportTest001, TestSize.Level0)
297 {
298     int32_t sessionId = 1;
299     int32_t appType = 1;
300     int32_t quality = -1;
301     int32_t ret = QosReport(sessionId, appType, quality);
302     EXPECT_NE(SOFTBUS_OK, ret);
303 
304     quality = 1;
305     ret = QosReport(sessionId, appType, quality);
306     EXPECT_NE(SOFTBUS_OK, ret);
307 }
308 
309 /**
310  * @tc.name: ClientCleanAllSessionWhenServerDeathTest001
311  * @tc.desc: use the normal parameter.
312  * @tc.type: FUNC
313  * @tc.require:
314  */
315 HWTEST_F(TransSessionTest, ClientCleanAllSessionWhenServerDeathTest001, TestSize.Level0)
316 {
317     ClientCleanAllSessionWhenServerDeath();
318 
319     char pkgName[TEST_PKG_NAME_LEN] = "com.test.trans.session";
320     char mySessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
321     char peerSessionName[TEST_SESSION_NAME_LEN] = "com.test.trans.session.sendfile";
322     char peerNetworkId[TEST_NETWORK_ID_LEN] = "1234567789";
323     char groupId[TEST_GROUP_ID_LEN] = "123";
324     SessionAttribute attr;
325     attr.dataType = 1;
326     attr.linkTypeNum = 0;
327     SessionParam param = {
328         .sessionName = mySessionName,
329         .peerSessionName = peerSessionName,
330         .peerDeviceId = peerNetworkId,
331         .groupId = groupId,
332         .attr = &attr,
333     };
334 
335     int32_t sessionId = INVALID_SESSION_ID;
336     bool isEnabled = false;
337     (void)ClientAddSessionServer(SEC_TYPE_CIPHERTEXT, pkgName, mySessionName, &g_sessionlistener);
338     (void)ClientAddSession(&param, &sessionId, &isEnabled);
339     ClientCleanAllSessionWhenServerDeath();
340     EXPECT_TRUE(true);
341 }
342 } // namespace OHOS
343