1 /*
2 * Copyright (c) 2023 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 <chrono>
18 #include <thread>
19
20 #include "accesstoken_kit.h"
21 #include "nativetoken_kit.h"
22 #include "token_setproc.h"
23 #include "avsession_errors.h"
24 #include "avsession_log.h"
25 #include "migrate_avsession_constant.h"
26 #include "migrate_avsession_server.h"
27 #define private public
28 #define protected public
29 #include "softbus_distributed_data_manager.h"
30 #include "softbus_session_manager.h"
31 #undef protected
32 #undef private
33
34 using namespace testing::ext;
35 using namespace OHOS::AVSession;
36
37 static const char* g_perms[] = {"ohos.permission.DISTRIBUTED_SOFTBUS_CENTER", "ohos.permission.DISTRIBUTED_DATASYNC",
38 "ohos.permission.ACCESS_SERVICE_DM"};
39 static const int SIZE = 3;
40 class SoftbusSessionManagerTest : public testing::Test {
41 public:
42 static void SetUpTestCase();
43 static void TearDownTestCase();
44 void SetUp();
45 void TearDown();
46 void NativeTokenGet(const char *perms[], int size);
47
48 std::shared_ptr<SoftbusSessionManager> manager_;
49 std::shared_ptr<SoftbusDistributedDataManager> distributed_;
50 };
51
SetUpTestCase()52 void SoftbusSessionManagerTest::SetUpTestCase() {}
53
TearDownTestCase()54 void SoftbusSessionManagerTest::TearDownTestCase() {}
55
SetUp()56 void SoftbusSessionManagerTest::SetUp()
57 {
58 manager_ = std::make_shared<SoftbusSessionManager>();
59 distributed_ = std::make_shared<SoftbusDistributedDataManager>();
60 NativeTokenGet(g_perms, SIZE);
61 }
62
TearDown()63 void SoftbusSessionManagerTest::TearDown()
64 {
65 manager_ = nullptr;
66 distributed_ = nullptr;
67 NativeTokenGet(nullptr, 0);
68 }
69
NativeTokenGet(const char * perms[],int size)70 void SoftbusSessionManagerTest::NativeTokenGet(const char *perms[], int size)
71 {
72 uint64_t tokenId;
73 NativeTokenInfoParams infoInstance = {
74 .dcapsNum = 0,
75 .permsNum = size,
76 .aclsNum = 0,
77 .dcaps = nullptr,
78 .perms = perms,
79 .acls = nullptr,
80 .aplStr = "system_basic",
81 };
82
83 infoInstance.processName = "softbus_session_manager_test";
84 tokenId = GetAccessTokenId(&infoInstance);
85 SetSelfTokenID(tokenId);
86 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
87 }
88
89 /**
90 * @tc.name: SoftbusDistributedTest001
91 * @tc.desc:
92 * @tc.type: FUNC
93 * @tc.require:
94 */
95 HWTEST_F(SoftbusSessionManagerTest, SoftbusDistributedTest001, TestSize.Level1)
96 {
97 SLOGI("SoftbusDistributedTest001 begin");
98 distributed_->Init();
99 std::string pkg = "AVSESSION";
100 distributed_->InitSessionServer(pkg);
101
102 std::shared_ptr<MigrateAVSessionServer> server = std::make_shared<MigrateAVSessionServer>();
103 distributed_->CreateServer(server);
104 EXPECT_EQ(distributed_->serverMap_.size() > 0, true);
105
106 int32_t sessionId = 1;
107 distributed_->SessionOpened(sessionId);
108 distributed_->SessionClosed(sessionId);
109 std::string data = "111";
110 distributed_->MessageReceived(sessionId, data);
111 distributed_->BytesReceived(sessionId, data);
112 distributed_->OnSessionServerOpened(sessionId);
113 std::string deviceId = "1";
114 distributed_->OnSessionServerClosed(sessionId, deviceId);
115 distributed_->OnMessageHandleReceived(sessionId, data);
116 distributed_->OnBytesServerReceived(sessionId, data);
117
118 distributed_->ReleaseServer(server);
119 EXPECT_EQ(distributed_->serverMap_.size() == 0, true);
120
121 distributed_->DestroySessionServer(pkg);
122 SLOGI("SoftbusDistributedTest001 end");
123 }
124
125 /**
126 * @tc.name: CreateSessionServer001
127 * @tc.desc:
128 * @tc.type: FUNC
129 * @tc.require:
130 */
131 HWTEST_F(SoftbusSessionManagerTest, CreateSessionServer001, TestSize.Level1)
132 {
133 SLOGI("CreateSessionServer001 begin");
134 std::string pkg = "111";
135 int32_t ret = manager_->CreateSessionServer(pkg);
136 EXPECT_EQ(ret, -1);
137 SLOGI("CreateSessionServer001 end");
138 }
139
140 /**
141 * @tc.name: RemoveSessionServer001
142 * @tc.desc:
143 * @tc.type: FUNC
144 * @tc.require:
145 */
146 HWTEST_F(SoftbusSessionManagerTest, RemoveSessionServer001, TestSize.Level1)
147 {
148 SLOGI("RemoveSessionServer001 begin");
149 std::string pkg = "111";
150 int32_t ret = manager_->RemoveSessionServer(pkg);
151 EXPECT_EQ(ret, -1);
152 SLOGI("RemoveSessionServer001 end");
153 }
154
155 /**
156 * @tc.name: OpenSession001
157 * @tc.desc:
158 * @tc.type: FUNC
159 * @tc.require:
160 */
161 HWTEST_F(SoftbusSessionManagerTest, OpenSession001, TestSize.Level1)
162 {
163 SLOGI("OpenSession001 begin");
164 int32_t ret = manager_->OpenSession("aaa", CONFIG_SOFTBUS_SESSION_TAG);
165 EXPECT_EQ(ret, -1);
166 SLOGI("OpenSession001 end");
167 }
168
169 /**
170 * @tc.name: CreateSessionServer001
171 * @tc.desc:
172 * @tc.type: FUNC
173 * @tc.require:
174 */
175 HWTEST_F(SoftbusSessionManagerTest, CloseSession001, TestSize.Level1)
176 {
177 SLOGI("CloseSession001 begin");
178 int32_t sessionId = 123;
179 std::string pkg = "123";
180 manager_->RemoveSessionServer(pkg);
181 int32_t ret = manager_->CloseSession(sessionId);
182 EXPECT_EQ(ret, -1);
183 SLOGI("CloseSession001 end");
184 }
185
186 /**
187 * @tc.name: SendMessage001
188 * @tc.desc:
189 * @tc.type: FUNC
190 * @tc.require:
191 */
192 HWTEST_F(SoftbusSessionManagerTest, SendMessage001, TestSize.Level1)
193 {
194 SLOGI("SendMessage001 begin");
195 int32_t sessionId = 123;
196 std::string data = "";
197 int32_t ret = manager_->SendMessage(sessionId, data);
198 EXPECT_EQ(ret, -1);
199 SLOGI("SendMessage001 end");
200 }
201
202 /**
203 * @tc.name: SendByte001
204 * @tc.desc:
205 * @tc.type: FUNC
206 * @tc.require:
207 */
208 HWTEST_F(SoftbusSessionManagerTest, SendByte001, TestSize.Level1)
209 {
210 SLOGI("SendByte001 begin");
211 int32_t sessionId = 123;
212 std::string data = "";
213 int32_t ret = manager_->SendBytes(sessionId, data);
214 EXPECT_EQ(ret, -1);
215 SLOGI("SendByte001 end");
216 }
217
218 /**
219 * @tc.name: ObtainPeerDeviceId001
220 * @tc.desc:
221 * @tc.type: FUNC
222 * @tc.require:
223 */
224 HWTEST_F(SoftbusSessionManagerTest, ObtainPeerDeviceId001, TestSize.Level1)
225 {
226 SLOGI("ObtainPeerDeviceId001 begin");
227 int32_t sessionId = 0;
228 std::string deviceId;
229 int32_t ret = manager_->ObtainPeerDeviceId(sessionId, deviceId);
230 EXPECT_EQ(ret, -1);
231 SLOGI("ObtainPeerDeviceId001 end");
232 }
233
234 /**
235 * @tc.name: GetPeerSessionName001
236 * @tc.desc:
237 * @tc.type: FUNC
238 * @tc.require:
239 */
240 HWTEST_F(SoftbusSessionManagerTest, GetPeerSessionName001, TestSize.Level1)
241 {
242 SLOGI("GetPeerSessionName001 begin");
243 int32_t sessionId = 0;
244 std::string sessionName;
245 int32_t ret = manager_->GetPeerSessionName(sessionId, sessionName);
246 EXPECT_EQ(ret, -1);
247 SLOGI("GetPeerSessionName001 end");
248 }
249
250 /**
251 * @tc.name: IsServerSide001
252 * @tc.desc:
253 * @tc.type: FUNC
254 * @tc.require:
255 */
256 HWTEST_F(SoftbusSessionManagerTest, IsServerSide001, TestSize.Level1)
257 {
258 SLOGI("IsServerSide001 begin");
259 int32_t sessionId = 123;
260 int32_t ret = manager_->IsServerSide(sessionId);
261 EXPECT_EQ(ret, false);
262 SLOGI("IsServerSide001 end");
263 }
264
265 /**
266 * @tc.name: OnSessionOpened001
267 * @tc.desc:
268 * @tc.type: FUNC
269 * @tc.require:
270 */
271 HWTEST_F(SoftbusSessionManagerTest, OnSessionOpened001, TestSize.Level1)
272 {
273 SLOGI("OnSessionOpened001 begin");
274 int32_t sessionId = 123;
275 int32_t ret = manager_->OnSessionOpened(sessionId, AVSESSION_ERROR);
276 EXPECT_EQ(ret, AVSESSION_ERROR);
277 SLOGI("OnSessionOpened001 end");
278 }
279
280 /**
281 * @tc.name: OnSessionOpened002
282 * @tc.desc:
283 * @tc.type: FUNC
284 * @tc.require:
285 */
286 HWTEST_F(SoftbusSessionManagerTest, OnSessionOpened002, TestSize.Level1)
287 {
288 SLOGI("OnSessionOpened002 begin");
289 int32_t sessionId = 456;
290 int32_t ret = manager_->OnSessionOpened(sessionId, AVSESSION_SUCCESS);
291 EXPECT_EQ(ret, AVSESSION_SUCCESS);
292 SLOGI("OnSessionOpened002 end");
293 }