1 /*
2 * Copyright (c) 2025 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 "avsession_errors.h"
22 #include "avsession_log.h"
23 #include "avsession_service.h"
24 #include "migrate_avsession_constant.h"
25 #include "migrate_avsession_proxy.h"
26 #include "nativetoken_kit.h"
27 #include "token_setproc.h"
28 #include "softbus_distributed_data_manager.h"
29 #include "softbus_session_manager.h"
30
31 using namespace testing::ext;
32 using namespace OHOS::AVSession;
33
34 namespace OHOS {
35 namespace AVSession {
36
37 std::shared_ptr<SoftbusDistributedDataManager> g_SoftbusDistributedDataManager {nullptr};
38 std::shared_ptr<MigrateAVSessionProxy> g_MigrateAVSessionProxy {nullptr};
39 std::shared_ptr<AVSessionService> g_AVSessionService {nullptr};
40
41 class SoftbusSessionManagerSupplementTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp();
46 void TearDown();
47 };
48
SetUpTestCase()49 void SoftbusSessionManagerSupplementTest::SetUpTestCase()
50 {
51 g_SoftbusDistributedDataManager = std::make_shared<SoftbusDistributedDataManager>();
52 g_AVSessionService = std::make_shared<AVSessionService>(OHOS::AVSESSION_SERVICE_ID);
53 g_MigrateAVSessionProxy = std::make_shared<MigrateAVSessionProxy>(g_AVSessionService.get());
54 }
55
TearDownTestCase()56 void SoftbusSessionManagerSupplementTest::TearDownTestCase()
57 {
58 g_SoftbusDistributedDataManager = nullptr;
59 g_MigrateAVSessionProxy = nullptr;
60 g_AVSessionService = nullptr;
61 }
62
SetUp()63 void SoftbusSessionManagerSupplementTest::SetUp() {}
64
TearDown()65 void SoftbusSessionManagerSupplementTest::TearDown() {}
66
67 /**
68 * @tc.name: CreateProxy001
69 * @tc.desc: fail to create
70 * @tc.type: FUNC
71 * @tc.require:
72 */
73 static HWTEST_F(SoftbusSessionManagerSupplementTest, CreateProxy001, TestSize.Level0)
74 {
75 std::string peerNetworkId = "";
76 std::string packageName = "";
77 bool ret = g_SoftbusDistributedDataManager->CreateProxy(nullptr, peerNetworkId, packageName);
78 EXPECT_EQ(ret, false);
79 }
80
81 /**
82 * @tc.name: CreateProxy002
83 * @tc.desc: fail to create
84 * @tc.type: FUNC
85 * @tc.require:
86 */
87 static HWTEST_F(SoftbusSessionManagerSupplementTest, CreateProxy002, TestSize.Level0)
88 {
89 std::string peerNetworkId = "";
90 std::string packageName = "";
91 bool ret = g_SoftbusDistributedDataManager->CreateProxy(g_MigrateAVSessionProxy, peerNetworkId, packageName);
92 EXPECT_EQ(ret, false);
93 }
94
95 /**
96 * @tc.name: CreateProxy003
97 * @tc.desc: fail to create
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 static HWTEST_F(SoftbusSessionManagerSupplementTest, CreateProxy003, TestSize.Level0)
102 {
103 std::string peerNetworkId = "";
104 std::string packageName = "test";
105 bool ret = g_SoftbusDistributedDataManager->CreateProxy(g_MigrateAVSessionProxy, peerNetworkId, packageName);
106 EXPECT_EQ(ret, false);
107 }
108
109 /**
110 * @tc.name: CreateProxy004
111 * @tc.desc: fail to create
112 * @tc.type: FUNC
113 * @tc.require:
114 */
115 static HWTEST_F(SoftbusSessionManagerSupplementTest, CreateProxy004, TestSize.Level0)
116 {
117 std::string peerNetworkId = "test";
118 std::string packageName = "";
119 bool ret = g_SoftbusDistributedDataManager->CreateProxy(g_MigrateAVSessionProxy, peerNetworkId, packageName);
120 EXPECT_EQ(ret, false);
121 }
122
123 /**
124 * @tc.name: CreateProxy005
125 * @tc.desc: fail to create
126 * @tc.type: FUNC
127 * @tc.require:
128 */
129 static HWTEST_F(SoftbusSessionManagerSupplementTest, CreateProxy005, TestSize.Level0)
130 {
131 std::string peerNetworkId = "0.0.0.0";
132 std::string packageName = "packageName";
133 std::map<int32_t, std::shared_ptr<SoftbusSessionProxy>> proxyMap;
134 proxyMap.insert({0, g_MigrateAVSessionProxy});
135 g_SoftbusDistributedDataManager->mDeviceToProxyMap_.insert({peerNetworkId, proxyMap});
136 g_SoftbusDistributedDataManager->mProxySocketMap_.insert({peerNetworkId, 0});
137 bool ret = g_SoftbusDistributedDataManager->CreateProxy(g_MigrateAVSessionProxy, peerNetworkId, packageName);
138 EXPECT_EQ(ret, false);
139 }
140
141 /**
142 * @tc.name: CreateProxy006
143 * @tc.desc: have proxy in map
144 * @tc.type: FUNC
145 * @tc.require:
146 */
147 static HWTEST_F(SoftbusSessionManagerSupplementTest, CreateProxy006, TestSize.Level0)
148 {
149 std::string peerNetworkId = "0.0.0.0";
150 std::string packageName = "packageName";
151 int32_t socketId = 100;
152 std::map<int32_t, std::shared_ptr<SoftbusSessionProxy>> proxyMap;
153 proxyMap.insert({socketId, g_MigrateAVSessionProxy});
154 g_SoftbusDistributedDataManager->mDeviceToProxyMap_.insert({peerNetworkId, proxyMap});
155 g_SoftbusDistributedDataManager->mProxySocketMap_.insert({peerNetworkId, socketId});
156 bool ret = g_SoftbusDistributedDataManager->CreateProxy(g_MigrateAVSessionProxy, peerNetworkId, packageName);
157 EXPECT_EQ(ret, false);
158 }
159
160
161 /**
162 * @tc.name: ReleaseProxy001
163 * @tc.desc: proxy is nullptr
164 * @tc.type: FUNC
165 * @tc.require:
166 */
167 static HWTEST_F(SoftbusSessionManagerSupplementTest, ReleaseProxy001, TestSize.Level0)
168 {
169 std::string peerNetworkId = "0.0.0.0";
170 bool ret = g_SoftbusDistributedDataManager->ReleaseProxy(nullptr, peerNetworkId);
171 EXPECT_EQ(ret, true);
172 }
173
174 /**
175 * @tc.name: ReleaseProxy002
176 * @tc.desc: fail to release proxy
177 * @tc.type: FUNC
178 * @tc.require:
179 */
180 static HWTEST_F(SoftbusSessionManagerSupplementTest, ReleaseProxy002, TestSize.Level0)
181 {
182 std::string peerNetworkId = "";
183 bool ret = g_SoftbusDistributedDataManager->ReleaseProxy(g_MigrateAVSessionProxy, peerNetworkId);
184 EXPECT_EQ(ret, false);
185 }
186
187 /**
188 * @tc.name: ReleaseProxy003
189 * @tc.desc: ReleaseProxy fail fo proxyMap is null
190 * @tc.type: FUNC
191 * @tc.require:
192 */
193 static HWTEST_F(SoftbusSessionManagerSupplementTest, ReleaseProxy003, TestSize.Level0)
194 {
195 std::string peerNetworkId = "0.0.0.0";
196 bool ret = g_SoftbusDistributedDataManager->ReleaseProxy(g_MigrateAVSessionProxy, peerNetworkId);
197 EXPECT_EQ(ret, false);
198 }
199
200 /**
201 * @tc.name: ReleaseProxy004
202 * @tc.desc: fail to release proxy
203 * @tc.type: FUNC
204 * @tc.require:
205 */
206 static HWTEST_F(SoftbusSessionManagerSupplementTest, ReleaseProxy004, TestSize.Level0)
207 {
208 std::string peerNetworkId = "0.0.0.0";
209 int32_t socketId = 1;
210 std::map<int32_t, std::shared_ptr<SoftbusSessionProxy>> proxyMap;
211 proxyMap.insert({socketId, g_MigrateAVSessionProxy});
212 g_SoftbusDistributedDataManager->mDeviceToProxyMap_.insert({peerNetworkId, proxyMap});
213 bool ret = g_SoftbusDistributedDataManager->ReleaseProxy(g_MigrateAVSessionProxy, peerNetworkId);
214 EXPECT_EQ(ret, false);
215 }
216
217 /**
218 * @tc.name: ReleaseProxy005
219 * @tc.desc: success to release proxy
220 * @tc.type: FUNC
221 * @tc.require:
222 */
223 static HWTEST_F(SoftbusSessionManagerSupplementTest, ReleaseProxy005, TestSize.Level0)
224 {
225 std::string peerNetworkId = "0.0.0.0";
226 int32_t characteristic = g_MigrateAVSessionProxy->GetCharacteristic();
227 std::map<int32_t, std::shared_ptr<SoftbusSessionProxy>> proxyMap;
228 proxyMap.insert({characteristic, g_MigrateAVSessionProxy});
229 g_SoftbusDistributedDataManager->mDeviceToProxyMap_.insert({peerNetworkId, proxyMap});
230 bool ret = g_SoftbusDistributedDataManager->ReleaseProxy(g_MigrateAVSessionProxy, peerNetworkId);
231 EXPECT_EQ(ret, false);
232 }
233
234 /**
235 * @tc.name: BytesReceived001
236 * @tc.desc: proxy to received
237 * @tc.type: FUNC
238 * @tc.require:
239 */
240 static HWTEST_F(SoftbusSessionManagerSupplementTest, BytesReceived001, TestSize.Level0)
241 {
242 int32_t socket = 1;
243 std::string data = "test";
244 g_SoftbusDistributedDataManager->isServer_ = false;
245 g_SoftbusDistributedDataManager->BytesReceived(socket, data);
246 EXPECT_EQ(g_SoftbusDistributedDataManager->isServer_, false);
247 }
248 } //AVSession
249 } //OHOS