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 "securec.h"
18
19 #include "client_trans_session_manager.h"
20 #include "session_impl.h"
21 #include "session_service_impl.h"
22 #include "ISessionListener.h"
23 #include "softbus_def.h"
24 #include "softbus_errcode.h"
25 #include "softbus_access_token_test.h"
26
27 #define TEST_DATA_LENGTH 1024
28 #define MAX_BYTE_LENGTH (128 * 1024 * 1024)
29
30 using namespace std;
31 using namespace testing::ext;
32
33 namespace OHOS {
34 std::string g_sessionName1;
35 std::string g_sessionName2 = "security.dpms_channel";
36 std::string g_pkgName1;
37 std::string g_pkgName2 = "ohos.security.distributed_permission";
38 std::string g_pkgName3 = "test";
39 std::string g_peerNetWorkId1;
40 std::string g_peerNetWorkId2 = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
41 std::string g_groupId;
42 int g_flags = 0;
43
44 class ISessionListenerTest : public Communication::SoftBus::ISessionListener {
45 public:
46 ISessionListenerTest() = default;
47 ~ISessionListenerTest() = default;
48
OnSessionOpened(std::shared_ptr<Communication::SoftBus::Session> session)49 int OnSessionOpened(std::shared_ptr<Communication::SoftBus::Session> session)
50 {
51 return SOFTBUS_OK;
52 }
53
OnSessionClosed(std::shared_ptr<Communication::SoftBus::Session> session)54 void OnSessionClosed(std::shared_ptr<Communication::SoftBus::Session> session)
55 {
56 return;
57 }
58
OnMessageReceived(std::shared_ptr<Communication::SoftBus::Session> session,const char * data,ssize_t len)59 void OnMessageReceived(std::shared_ptr<Communication::SoftBus::Session> session, const char *data, ssize_t len)
60 {
61 return;
62 }
63
OnBytesReceived(std::shared_ptr<Communication::SoftBus::Session> session,const char * data,ssize_t len)64 void OnBytesReceived(std::shared_ptr<Communication::SoftBus::Session> session, const char *data, ssize_t len)
65 {
66 return;
67 }
68
OnDataAvailable(std::shared_ptr<Communication::SoftBus::Session> session,uint32_t status)69 bool OnDataAvailable(std::shared_ptr<Communication::SoftBus::Session> session, uint32_t status)
70 {
71 return true;
72 }
73 };
74
75 class ClientTransSessionImplTest : public testing::Test {
76 public:
ClientTransSessionImplTest()77 ClientTransSessionImplTest() {}
~ClientTransSessionImplTest()78 ~ClientTransSessionImplTest() {}
79 static void SetUpTestCase(void);
80 static void TearDownTestCase(void);
SetUp()81 void SetUp() override {}
TearDown()82 void TearDown() override {}
83 };
84
SetUpTestCase(void)85 void ClientTransSessionImplTest::SetUpTestCase(void)
86 {
87 SetAceessTokenPermission("dsoftbusTransTest");
88 }
89
TearDownTestCase(void)90 void ClientTransSessionImplTest::TearDownTestCase(void) {}
91
92 /**
93 * @tc.name: ClientTransSessionServerImplTest001
94 * @tc.desc: client trans session server impl test, use the wrong or normal parameter.
95 * @tc.type: FUNC
96 * @tc.require:
97 */
98 HWTEST_F(ClientTransSessionImplTest, ClientTransSessionServerImplTest001, TestSize.Level0)
99 {
100 Communication::SoftBus::SessionServiceImpl testSessionServiceImpl;
101 std::shared_ptr<Communication::SoftBus::ISessionListener> listern = std::make_shared<ISessionListenerTest>();
102
103 int ret = testSessionServiceImpl.CreateSessionServer(g_pkgName1, g_sessionName1, listern);
104 EXPECT_EQ(SOFTBUS_ERR, ret);
105
106 ret = testSessionServiceImpl.CreateSessionServer(g_pkgName2, g_sessionName1, listern);
107 EXPECT_EQ(SOFTBUS_ERR, ret);
108
109 ret = testSessionServiceImpl.CreateSessionServer(g_pkgName2, g_sessionName2, nullptr);
110 EXPECT_EQ(SOFTBUS_ERR, ret);
111
112 ret = testSessionServiceImpl.CreateSessionServer(g_pkgName3, g_sessionName2, listern);
113 EXPECT_NE(SOFTBUS_OK, ret);
114
115 ret = testSessionServiceImpl.CreateSessionServer(g_pkgName2, g_sessionName2, listern);
116 EXPECT_EQ(SOFTBUS_OK, ret);
117
118 ret = testSessionServiceImpl.RemoveSessionServer(g_pkgName2, g_sessionName2);
119 EXPECT_EQ(SOFTBUS_OK, ret);
120 }
121
122 /**
123 * @tc.name: ClientTransSessionServerImplTest002
124 * @tc.desc: client trans session server impl test, use the wrong or normal parameter.
125 * @tc.type: FUNC
126 * @tc.require:
127 */
128 HWTEST_F(ClientTransSessionImplTest, ClientTransSessionServerImplTest002, TestSize.Level0)
129 {
130 Communication::SoftBus::SessionServiceImpl testSessionServiceImpl;
131 int ret;
132 std::shared_ptr<Communication::SoftBus::Session> session = testSessionServiceImpl.OpenSession(g_sessionName1,
133 g_sessionName2, g_peerNetWorkId2, g_groupId, g_flags);
134 EXPECT_EQ(nullptr, session);
135
136 session = testSessionServiceImpl.OpenSession(g_sessionName2, g_sessionName1, g_peerNetWorkId2, g_groupId, g_flags);
137 EXPECT_EQ(nullptr, session);
138
139 session = testSessionServiceImpl.OpenSession(g_sessionName2, g_sessionName2, g_peerNetWorkId1, g_groupId, g_flags);
140 EXPECT_EQ(nullptr, session);
141
142 session = testSessionServiceImpl.OpenSession(g_sessionName2, g_sessionName2, g_peerNetWorkId2, g_groupId, g_flags);
143 EXPECT_EQ(nullptr, session);
144
145 int uid = 0;
146 int pid = 0;
147 ret = testSessionServiceImpl.GrantPermission(-1, -1, g_pkgName1);
148 EXPECT_EQ(SOFTBUS_ERR, ret);
149
150 ret = testSessionServiceImpl.GrantPermission(uid, pid, g_pkgName1);
151 EXPECT_EQ(SOFTBUS_ERR, ret);
152
153 ret = testSessionServiceImpl.GrantPermission(uid, pid, g_pkgName2);
154 EXPECT_EQ(SOFTBUS_ERR, ret);
155
156 int sessionId = 1;
157 ret = testSessionServiceImpl.OpenSessionCallback(sessionId);
158 EXPECT_EQ(SOFTBUS_ERR, ret);
159
160 testSessionServiceImpl.CloseSessionCallback(sessionId);
161 const char data[TEST_DATA_LENGTH] = "test";
162 testSessionServiceImpl.BytesReceivedCallback(sessionId, data, TEST_DATA_LENGTH);
163 testSessionServiceImpl.MessageReceivedCallback(sessionId, data, TEST_DATA_LENGTH);
164
165 ret = testSessionServiceImpl.CloseSession(session);
166 EXPECT_EQ(SOFTBUS_ERR, ret);
167
168 ret = testSessionServiceImpl.RemoveSessionServer(g_pkgName1, g_sessionName1);
169 EXPECT_EQ(SOFTBUS_ERR, ret);
170
171 ret = testSessionServiceImpl.RemoveSessionServer(g_pkgName2, g_sessionName1);
172 EXPECT_EQ(SOFTBUS_ERR, ret);
173
174 ret = testSessionServiceImpl.RemoveSessionServer(g_pkgName3, g_sessionName2);
175 EXPECT_NE(SOFTBUS_OK, ret);
176 }
177
178 /**
179 * @tc.name: ClientTransSessionServerImplTest003
180 * @tc.desc: client trans session server impl test, use the wrong or normal parameter.
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 HWTEST_F(ClientTransSessionImplTest, ClientTransSessionServerImplTest003, TestSize.Level0)
185 {
186 Communication::SoftBus::SessionImpl testSessionImpl;
187 ssize_t len = 0;
188 int ret = testSessionImpl.SendBytes(nullptr, len);
189 EXPECT_NE(SOFTBUS_OK, ret);
190
191 const char *data = "test";
192 ret = testSessionImpl.SendBytes(data, len);
193 EXPECT_NE(SOFTBUS_OK, ret);
194
195 len = MAX_BYTE_LENGTH + 1;
196 ret = testSessionImpl.SendBytes(data, len);
197 EXPECT_NE(SOFTBUS_OK, ret);
198
199 len = TEST_DATA_LENGTH;
200 ret = testSessionImpl.SendBytes(data, len);
201 EXPECT_NE(SOFTBUS_OK, ret);
202 }
203
204 /**
205 * @tc.name: ClientTransSessionServerImplTest004
206 * @tc.desc: client trans session server impl test, use the wrong or normal parameter.
207 * @tc.type: FUNC
208 * @tc.require:
209 */
210 HWTEST_F(ClientTransSessionImplTest, ClientTransSessionServerImplTest004, TestSize.Level0)
211 {
212 Communication::SoftBus::SessionServiceImpl testSessionServiceImpl;
213 std::shared_ptr<Communication::SoftBus::ISessionListener> listern = std::make_shared<ISessionListenerTest>();
214 int ret = testSessionServiceImpl.CreateSessionServer(g_pkgName2, g_sessionName2, listern);
215 EXPECT_EQ(SOFTBUS_OK, ret);
216
217 const char *groupId = "test";
218 SessionAttribute attr;
219 attr.dataType = 1;
220 attr.linkTypeNum = 0;
221 SessionParam param = {
222 .sessionName = g_sessionName2.c_str(),
223 .peerSessionName = g_sessionName2.c_str(),
224 .peerDeviceId = g_peerNetWorkId2.c_str(),
225 .groupId = groupId,
226 .attr = &attr,
227 };
228 int32_t sessionId = 0;
229 bool isEnabled = 0;
230 ret = ClientAddSession(¶m, &sessionId, &isEnabled);
231 EXPECT_EQ(SOFTBUS_OK, ret);
232
233 ret = testSessionServiceImpl.OpenSessionCallback(sessionId);
234 EXPECT_EQ(SOFTBUS_OK, ret);
235 }
236 } // namespace OHOS