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 static 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     char infoName[] = "testInfoName";
108     char infoNetworkId[] = "testInfoNetworkId";
109     char infoPkgName[] = "testInfoPkgName";
110     PeerSocketInfo info = {
111         .name = infoName,
112         .networkId = infoNetworkId,
113         .pkgName = infoPkgName,
114         .dataType = DATA_TYPE_BYTES,
115     };
116     distributed_->SessionOpened(sessionId, info);
117     distributed_->SessionClosed(sessionId);
118     std::string data = "111";
119     distributed_->MessageReceived(sessionId, data);
120     distributed_->BytesReceived(sessionId, data);
121     distributed_->OnSessionServerOpened();
122     distributed_->OnSessionServerClosed(sessionId);
123     distributed_->OnMessageHandleReceived(sessionId, data);
124     distributed_->OnBytesServerReceived(data);
125 
126     distributed_->ReleaseServer(server);
127     EXPECT_EQ(distributed_->serverMap_.size() == 0, true);
128 
129     distributed_->DestroySessionServer(pkg);
130     SLOGI("SoftbusDistributedTest001 end");
131 }
132 
133 /**
134 * @tc.name: CreateSessionServer001
135 * @tc.desc:
136 * @tc.type: FUNC
137 * @tc.require:
138 */
139 static HWTEST_F(SoftbusSessionManagerTest, CreateSessionServer001, TestSize.Level1)
140 {
141     SLOGI("CreateSessionServer001 begin");
142     std::string pkg = "111";
143     int32_t ret = manager_->Socket(pkg);
144     EXPECT_EQ(ret <= 0, true);
145     SLOGI("CreateSessionServer001 end");
146 }
147 
148 /**
149 * @tc.name: RemoveSessionServer001
150 * @tc.desc:
151 * @tc.type: FUNC
152 * @tc.require:
153 */
154 static HWTEST_F(SoftbusSessionManagerTest, RemoveSessionServer001, TestSize.Level1)
155 {
156     SLOGI("RemoveSessionServer001 begin");
157     int32_t sessionId = 123;
158     manager_->Shutdown(sessionId);
159     SLOGI("RemoveSessionServer001 end");
160 }
161 
162 /**
163 * @tc.name: SendMessage001
164 * @tc.desc:
165 * @tc.type: FUNC
166 * @tc.require:
167 */
168 static HWTEST_F(SoftbusSessionManagerTest, SendMessage001, TestSize.Level1)
169 {
170     SLOGI("SendMessage001 begin");
171     int32_t sessionId = 123;
172     std::string data = "";
173     int32_t ret = manager_->SendMessage(sessionId, data);
174     EXPECT_EQ(ret, -1);
175     SLOGI("SendMessage001 end");
176 }
177 
178 /**
179 * @tc.name: SendByte001
180 * @tc.desc:
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 static HWTEST_F(SoftbusSessionManagerTest, SendByte001, TestSize.Level1)
185 {
186     SLOGI("SendByte001 begin");
187     int32_t sessionId = 123;
188     std::string data = "";
189     int32_t ret = manager_->SendBytes(sessionId, data);
190     EXPECT_EQ(ret, -1);
191     SLOGI("SendByte001 end");
192 }
193 
194 /**
195 * @tc.name: ObtainPeerDeviceId001
196 * @tc.desc:
197 * @tc.type: FUNC
198 * @tc.require:
199 */
200 static HWTEST_F(SoftbusSessionManagerTest, ObtainPeerDeviceId001, TestSize.Level1)
201 {
202     SLOGI("ObtainPeerDeviceId001 begin");
203     int32_t sessionId = 0;
204     std::string deviceId;
205     int32_t ret = manager_->ObtainPeerDeviceId(sessionId, deviceId);
206     EXPECT_EQ(ret, -1);
207     SLOGI("ObtainPeerDeviceId001 end");
208 }
209 
210 /**
211 * @tc.name: OnSessionOpened001
212 * @tc.desc:
213 * @tc.type: FUNC
214 * @tc.require:
215 */
216 static HWTEST_F(SoftbusSessionManagerTest, OnSessionOpened001, TestSize.Level1)
217 {
218     SLOGI("OnSessionOpened001 begin");
219     int32_t sessionId = 123;
220     char infoName[] = "testInfoName";
221     char infoNetworkId[] = "testInfoNetworkId";
222     char infoPkgName[] = "testInfoPkgName";
223     PeerSocketInfo info = {
224         .name = infoName,
225         .networkId = infoNetworkId,
226         .pkgName = infoPkgName,
227         .dataType = DATA_TYPE_BYTES,
228     };
229     manager_->OnBind(sessionId, info);
230     SLOGI("OnSessionOpened001 end");
231 }