1 /*
2  * Copyright (c) 2024 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 
18 #include "session_impl.h"
19 #include "softbus_error_code.h"
20 
21 using namespace testing::ext;
22 
23 namespace Communication {
24 namespace SoftBus {
25 
26 static constexpr int32_t TEST_EXPEXT_CHANNELD_ID = 123456789;
27 static constexpr int32_t TEST_MAX_BYTES_LENGTH = 128 * 1024 * 1024;
28 static constexpr int32_t TEST_INVALID_BUF_LEN = 0;
29 static constexpr int32_t TEST_SEND_BUF_LENGTYH = 10;
30 static constexpr pid_t TEST_EXPEXTED_PEERPID = 38356;
31 static constexpr int32_t TEST_SESSIONID_003 = 1234;
32 static constexpr int32_t TEST_SESSIONID_004 = -123;
33 static constexpr int32_t TEST_SESSIONID_005 = 0;
34 
35 class TransClientSessionImplTest : public testing::Test {
36 public:
TransClientSessionImplTest()37     TransClientSessionImplTest() {}
~TransClientSessionImplTest()38     ~TransClientSessionImplTest() {};
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
SetUp()41     void SetUp() override
42     {}
TearDown()43     void TearDown() override
44     {}
45 
46 protected:
47     SessionImpl sessionImpl_;
48 };
49 
SetUpTestCase(void)50 void TransClientSessionImplTest::SetUpTestCase(void) {}
51 
TearDownTestCase(void)52 void TransClientSessionImplTest::TearDownTestCase(void) {}
53 
54 /**
55  * @tc.name: TransClientSessionImplTest001
56  * @tc.desc: Transmission sdk session impl initialization parameters.
57  * @tc.type: FUNC
58  * @tc.require:
59  */
60 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest001, TestSize.Level0)
61 {
62     EXPECT_EQ(-1, sessionImpl_.GetSessionId());
63     EXPECT_FALSE(sessionImpl_.IsServerSide());
64     EXPECT_EQ(-1, sessionImpl_.GetPeerUid());
65     EXPECT_EQ(-1, sessionImpl_.GetPeerPid());
66 }
67 
68 /**
69  * @tc.name: TransClientSessionImplTest002
70  * @tc.desc: Transmission sdk test GetChannelId method.
71  * @tc.type: FUNC
72  * @tc.require:
73  */
74 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest002, TestSize.Level0)
75 {
76     sessionImpl_.SetSessionId(TEST_EXPEXT_CHANNELD_ID);
77     EXPECT_EQ(sessionImpl_.GetChannelId(), TEST_EXPEXT_CHANNELD_ID);
78 }
79 
80 /**
81  * @tc.name: TransClientSessionImplTest003
82  * @tc.desc: Transmission sdk test SetSessionId method.
83  * @tc.type: FUNC
84  * @tc.require:
85  */
86 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest003, TestSize.Level0)
87 {
88     int32_t sessionId = TEST_SESSIONID_003;
89     sessionImpl_.SetSessionId(sessionId);
90     EXPECT_EQ(sessionImpl_.GetSessionId(), sessionId);
91 }
92 
93 
94 /**
95  * @tc.name: TransClientSessionImplTest004
96  * @tc.desc: Transmission sdk test SetSessionId method.
97  * @tc.type: FUNC
98  * @tc.require:
99  */
100 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest004, TestSize.Level0)
101 {
102     int32_t sessionId = TEST_SESSIONID_004;
103     sessionImpl_.SetSessionId(sessionId);
104     EXPECT_EQ(sessionImpl_.GetSessionId(), sessionId);
105 }
106 
107 /**
108  * @tc.name: TransClientSessionImplTest005
109  * @tc.desc: Transmission sdk test SetSessionId method.
110  * @tc.type: FUNC
111  * @tc.require:
112  */
113 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest005, TestSize.Level0)
114 {
115     int32_t sessionId = TEST_SESSIONID_005;
116     sessionImpl_.SetSessionId(sessionId);
117     EXPECT_EQ(sessionImpl_.GetSessionId(), sessionId);
118 }
119 
120 /**
121  * @tc.name: TransClientSessionImplTest006
122  * @tc.desc: Test when GetMySessionName is called then it returns the correct session name.
123  * @tc.type: FUNC
124  * @tc.require:
125  */
126 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest006, TestSize.Level0)
127 {
128     std::string expectedSessionName = "trans.client.session.test";
129     sessionImpl_.SetMySessionName(expectedSessionName);
130     const std::string &actualSessionName = sessionImpl_.GetMySessionName();
131     EXPECT_EQ(expectedSessionName, actualSessionName);
132 }
133 
134 /**
135  * @tc.name: TransClientSessionImplTest007
136  * @tc.desc: Test when GetMySessionName is called then it returns an empty string if no session name is set.
137  * @tc.type: FUNC
138  * @tc.require:
139  */
140 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest007, TestSize.Level0)
141 {
142     const std::string &actualSessionName = sessionImpl_.GetMySessionName();
143     EXPECT_EQ("", actualSessionName);
144 }
145 
146 /**
147  * @tc.name: TransClientSessionImplTest008
148  * @tc.desc: Test SetPeerDeviceId when name is empty.
149  * @tc.type: FUNC
150  * @tc.require:
151  */
152 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest008, TestSize.Level0)
153 {
154     std::string name = "";
155     sessionImpl_.SetPeerDeviceId(name);
156     EXPECT_EQ(sessionImpl_.GetPeerDeviceId(), name);
157 }
158 
159 /**
160  * @tc.name: TransClientSessionImplTest009
161  * @tc.desc: Test SetPeerDeviceId when name is not empty.
162  * @tc.type: FUNC
163  * @tc.require:
164  */
165 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest009, TestSize.Level0)
166 {
167     std::string name = "1111222223333333333333test";
168     sessionImpl_.SetPeerDeviceId(name);
169     EXPECT_EQ(sessionImpl_.GetPeerDeviceId(), name);
170 }
171 
172 /**
173  * @tc.name: TransClientSessionImplTest010
174  * @tc.desc: Test when isServer is true.
175  * @tc.type: FUNC
176  * @tc.require:
177  */
178 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest010, TestSize.Level0)
179 {
180     sessionImpl_.SetIsServer(true);
181     EXPECT_TRUE(sessionImpl_.IsServerSide());
182 }
183 
184 /**
185  * @tc.name: TransClientSessionImplTest011
186  * @tc.desc: Test when isServer is false.
187  * @tc.type: FUNC
188  * @tc.require:
189  */
190 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest011, TestSize.Level0)
191 {
192     sessionImpl_.SetIsServer(false);
193     EXPECT_FALSE(sessionImpl_.IsServerSide());
194 }
195 
196 /**
197  * @tc.name: TransClientSessionImplTest012
198  * @tc.desc: Test when GetDeviceId is called then return deviceId_.
199  * @tc.type: FUNC
200  * @tc.require:
201  */
202 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest012, TestSize.Level0)
203 {
204     std::string deviceId = "11111111111111222222222222test";
205     sessionImpl_.SetDeviceId(deviceId);
206     const std::string &result = sessionImpl_.GetDeviceId();
207     EXPECT_EQ(result, deviceId);
208 }
209 
210 /**
211  * @tc.name: TransClientSessionImplTest013
212  * @tc.desc: Test when GetPeerUid is called then it returns the correct peerUid.
213  * @tc.type: FUNC
214  * @tc.require:
215  */
216 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest013, TestSize.Level0)
217 {
218     uid_t expectedPeerUid = TEST_EXPEXTED_PEERPID;
219     sessionImpl_.SetPeerUid(expectedPeerUid);
220     uid_t actualPeerUid = sessionImpl_.GetPeerUid();
221     EXPECT_EQ(actualPeerUid, expectedPeerUid);
222 }
223 
224 /**
225  * @tc.name: TransClientSessionImplTest014
226  * @tc.desc: Test GetPeerPid method
227  * @tc.type: FUNC
228  * @tc.require:
229  */
230 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest014, TestSize.Level0)
231 {
232     pid_t expectedPeerPid = TEST_EXPEXTED_PEERPID;
233     sessionImpl_.SetPeerPid(expectedPeerPid);
234     EXPECT_EQ(sessionImpl_.GetPeerPid(), expectedPeerPid);
235 }
236 
237 /**
238  * @tc.name: TransClientSessionImplTest015
239  * @tc.desc: Test when isServer_ is true then IsServerSide returns true.
240  * @tc.type: FUNC
241  * @tc.require:
242  */
243 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest015, TestSize.Level0)
244 {
245     sessionImpl_.SetIsServer(true);
246     ASSERT_EQ(sessionImpl_.IsServerSide(), true);
247 }
248 
249 /**
250  * @tc.name: TransClientSessionImplTest016
251  * @tc.desc:Test when isServer_ is false then IsServerSide returns false.
252  * @tc.type: FUNC
253  * @tc.require:
254  */
255 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest016, TestSize.Level0)
256 {
257     sessionImpl_.SetIsServer(false);
258     ASSERT_EQ(sessionImpl_.IsServerSide(), false);
259 }
260 
261 /**
262  * @tc.name: TransClientSessionImplTest017
263  * @tc.desc: Test when buf is nullptr then SendBytes returns SOFTBUS_INVALID_PARAM.
264  * @tc.type: FUNC
265  * @tc.require:
266  */
267 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest017, TestSize.Level0)
268 {
269     const void *buf = nullptr;
270     int32_t ret = sessionImpl_.SendBytes(buf, TEST_SEND_BUF_LENGTYH);
271     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
272 }
273 
274 /**
275  * @tc.name: TransClientSessionImplTest018
276  * @tc.desc: Test when len is less than or equal to 0 then SendBytes returns SOFTBUS_INVALID_PARAM.
277  * @tc.type: FUNC
278  * @tc.require:
279  */
280 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest018, TestSize.Level0)
281 {
282     const void *buf = "sessionimpltestbuf";
283     int32_t ret = sessionImpl_.SendBytes(buf, TEST_INVALID_BUF_LEN);
284     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
285 }
286 
287 /**
288  * @tc.name: TransClientSessionImplTest019
289  * @tc.desc: Test when len is greater than TEST_MAX_BYTES_LENGTH then SendBytes returns SOFTBUS_INVALID_PARAM.
290  * @tc.type: FUNC
291  * @tc.require:
292  */
293 HWTEST_F(TransClientSessionImplTest, TransClientSessionImplTest019, TestSize.Level0)
294 {
295     const void *buf = "sessionimpltestbuf";
296     ssize_t len = TEST_MAX_BYTES_LENGTH + 1;
297     int32_t ret = sessionImpl_.SendBytes(buf, len);
298     EXPECT_EQ(ret, SOFTBUS_INVALID_PARAM);
299 }
300 } // SoftBus
301 } // Communication
302