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 <memory> 17 #include <unistd.h> 18 19 #include "gtest/gtest.h" 20 #include "network/softbus/softbus_agent.h" 21 #include "network/softbus/softbus_session_dispatcher.h" 22 #include "utils_log.h" 23 24 namespace OHOS { 25 namespace Storage { 26 namespace DistributedFile { 27 namespace Test { 28 using namespace testing::ext; 29 using namespace std; 30 namespace { 31 const std::string PEER_SESSION_NAME = "peersessionname"; 32 const std::string REMOTE_DEV_ID = "f6d4c0864707aefte7a78f09473aa122ff57fc8"; 33 const std::string PKG_NAME_TEST = "pkgname"; 34 } 35 36 constexpr int TEST_SESSION_ID = 10; 37 constexpr int TEST_INVALID_SESSION_ID = -1; 38 static const string SAME_ACCOUNT = "account"; 39 40 class SoftbusSessionDispatcherTest : public testing::Test { 41 public: SetUpTestCase(void)42 static void SetUpTestCase(void) {}; TearDownTestCase(void)43 static void TearDownTestCase(void) {}; SetUp()44 void SetUp() {}; TearDown()45 void TearDown() {}; 46 }; 47 48 /** 49 * @tc.name: SoftbusSessionDispatcherTest_RegisterSessionListener_0100 50 * @tc.desc: Verify the RegisterSessionListener/UnregisterSessionListener function. 51 * @tc.type: FUNC 52 * @tc.require: SR000H0387 53 */ 54 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_RegisterSessionListener_0100, TestSize.Level1) 55 { 56 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_RegisterSessionListener_0100 start"; 57 constexpr int userId = 100; 58 auto mp = make_unique<MountPoint>( 59 OHOS::Storage::DistributedFile::Utils::DfsuMountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT)); 60 shared_ptr<MountPoint> smp = move(mp); 61 weak_ptr<MountPoint> wmp(smp); 62 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 63 weak_ptr<SoftbusAgent> wsba(agent); 64 const string busName = "testBus"; 65 bool res = true; 66 67 try { 68 SoftbusSessionDispatcher::RegisterSessionListener(busName, wsba); 69 SoftbusSessionDispatcher::UnregisterSessionListener(busName); 70 } catch (const exception &e) { 71 LOGE("%{public}s", e.what()); 72 res = false; 73 } 74 75 EXPECT_TRUE(res == true); 76 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_RegisterSessionListener_0100 end"; 77 } 78 79 /** 80 * @tc.name: SoftbusSessionDispatcherTest_RegisterSessionListener_0200 81 * @tc.desc: Verify the RegisterSessionListener/UnregisterSessionListener function. 82 * @tc.type: FUNC 83 * @tc.require: issueI7SP3A 84 */ 85 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_RegisterSessionListener_0200, TestSize.Level1) 86 { 87 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_RegisterSessionListener_0200 start"; 88 constexpr int userId = 100; 89 auto mp = make_unique<MountPoint>( 90 OHOS::Storage::DistributedFile::Utils::DfsuMountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT)); 91 shared_ptr<MountPoint> smp = move(mp); 92 weak_ptr<MountPoint> wmp(smp); 93 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 94 weak_ptr<SoftbusAgent> wsba(agent); 95 const string busName = ""; 96 97 try { 98 SoftbusSessionDispatcher::RegisterSessionListener(busName, wsba); 99 EXPECT_TRUE(false); 100 } catch (const exception &e) { 101 LOGE("%{public}s", e.what()); 102 EXPECT_TRUE(true); 103 } 104 105 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_RegisterSessionListener_0200 end"; 106 } 107 108 /** 109 * @tc.name: SoftbusSessionDispatcherTest_RegisterSessionListener_0300 110 * @tc.desc: Verify the RegisterSessionListener/UnregisterSessionListener function. 111 * @tc.type: FUNC 112 * @tc.require: issueI7SP3A 113 */ 114 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_RegisterSessionListener_0300, TestSize.Level1) 115 { 116 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_RegisterSessionListener_0300 start"; 117 constexpr int userId = 100; 118 auto mp = make_unique<MountPoint>( 119 OHOS::Storage::DistributedFile::Utils::DfsuMountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT)); 120 shared_ptr<MountPoint> smp = move(mp); 121 weak_ptr<MountPoint> wmp(smp); 122 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 123 weak_ptr<SoftbusAgent> wsba(agent); 124 const string busName = "testBus"; 125 try { 126 SoftbusSessionDispatcher::busNameToAgent_.insert(make_pair(busName, wsba)); 127 SoftbusSessionDispatcher::RegisterSessionListener(busName, wsba); 128 EXPECT_TRUE(false); 129 } catch (const exception &e) { 130 SoftbusSessionDispatcher::busNameToAgent_.erase(busName); 131 LOGE("%{public}s", e.what()); 132 EXPECT_TRUE(true); 133 } 134 135 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_RegisterSessionListener_0300 end"; 136 } 137 138 /** 139 * @tc.name: SoftbusSessionDispatcherTest_UnregisterSessionListener_0100 140 * @tc.desc: Verify the RegisterSessionListener/UnregisterSessionListener function. 141 * @tc.type: FUNC 142 * @tc.require: issueI7SP3A 143 */ 144 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_UnregisterSessionListener_0100, TestSize.Level1) 145 { 146 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_UnregisterSessionListener_0100 start"; 147 const string busName = "testBus"; 148 try { 149 SoftbusSessionDispatcher::UnregisterSessionListener(busName); 150 EXPECT_TRUE(false); 151 } catch (const exception &e) { 152 LOGE("%{public}s", e.what()); 153 EXPECT_TRUE(true); 154 } 155 156 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_UnregisterSessionListener_0100 end"; 157 } 158 159 /** 160 * @tc.name: SoftbusSessionDispatcherTest_GetAgent_0100 161 * @tc.desc: Verify the GetAgent function. 162 * @tc.type: FUNC 163 * @tc.require: SR000H0387 164 */ 165 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_GetAgent_0100, TestSize.Level1) 166 { 167 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_GetAgent_0100 start"; 168 std::string peerSessionName = "peerSessionName"; 169 try { 170 weak_ptr<SoftbusAgent> wp = SoftbusSessionDispatcher::GetAgent(TEST_SESSION_ID, peerSessionName); 171 EXPECT_TRUE(wp.expired() == true); 172 } catch (const exception &e) { 173 LOGE("%{public}s", e.what()); 174 } 175 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_GetAgent_0100 end"; 176 } 177 178 /** 179 * @tc.name: SoftbusSessionDispatcherTest_GetAgent_0200 180 * @tc.desc: Verify the GetAgent function. 181 * @tc.type: FUNC 182 * @tc.require: issueI7SP3A 183 */ 184 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_GetAgent_0200, TestSize.Level1) 185 { 186 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_GetAgent_0200 start"; 187 std::string peerSessionName = "peerSessionName"; 188 try { 189 weak_ptr<SoftbusAgent> wp = SoftbusSessionDispatcher::GetAgent(TEST_INVALID_SESSION_ID, peerSessionName); 190 auto ptr = wp.lock(); 191 EXPECT_EQ(ptr, nullptr); 192 } catch (const exception &e) { 193 LOGE("%{public}s", e.what()); 194 EXPECT_TRUE(false); 195 } 196 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_GetAgent_0200 end"; 197 } 198 199 /** 200 * @tc.name: SoftbusSessionDispatcherTest_GetAgent_0300 201 * @tc.desc: Verify the GetAgent function. 202 * @tc.type: FUNC 203 * @tc.require: issueI7SP3A 204 */ 205 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_GetAgent_0300, TestSize.Level1) 206 { 207 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_GetAgent_0300 start"; 208 constexpr int userId = 100; 209 auto mp = make_unique<MountPoint>( 210 OHOS::Storage::DistributedFile::Utils::DfsuMountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT)); 211 shared_ptr<MountPoint> smp = move(mp); 212 weak_ptr<MountPoint> wmp(smp); 213 std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp); 214 weak_ptr<SoftbusAgent> wsba(agent); 215 const string busName = "testBus"; 216 try { 217 SoftbusSessionDispatcher::RegisterSessionListener(busName, wsba); 218 SoftbusSessionDispatcher::busNameToAgent_.erase(busName); 219 std::string peerSessionName = "peerSessionName"; 220 weak_ptr<SoftbusAgent> wp = SoftbusSessionDispatcher::GetAgent(TEST_SESSION_ID, peerSessionName); 221 auto ptr = wp.lock(); 222 EXPECT_EQ(ptr, nullptr); 223 } catch (const exception &e) { 224 LOGE("%{public}s", e.what()); 225 EXPECT_TRUE(false); 226 } 227 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_GetAgent_0300 end"; 228 } 229 230 /** 231 * @tc.name: SoftbusSessionDispatcherTest_OnSessionOpened_0100 232 * @tc.desc: Verify the OnSessionOpened function. 233 * @tc.type: FUNC 234 * @tc.require: SR000H0387 235 */ 236 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_OnSessionOpened_0100, TestSize.Level1) 237 { 238 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionOpened_0100 start"; 239 bool res = true; 240 PeerSocketInfo peerSocketInfo = { 241 .name = const_cast<char*>(PEER_SESSION_NAME.c_str()), 242 .networkId = const_cast<char*>(REMOTE_DEV_ID.c_str()), 243 .pkgName = const_cast<char*>(PKG_NAME_TEST.c_str()), 244 .dataType = DATA_TYPE_BYTES 245 }; 246 try { 247 SoftbusSessionDispatcher::OnSessionOpened(TEST_SESSION_ID, peerSocketInfo); 248 } catch (const exception &e) { 249 res = false; 250 LOGE("%{public}s", e.what()); 251 } 252 253 EXPECT_TRUE(res == true); 254 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionOpened_0100 end"; 255 } 256 257 /** 258 * @tc.name: SoftbusSessionDispatcherTest_OnSessionOpened_0200 259 * @tc.desc: Verify the OnSessionOpened function. 260 * @tc.type: FUNC 261 * @tc.require: SR000H0387 262 */ 263 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_OnSessionOpened_0200, TestSize.Level1) 264 { 265 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionOpened_0200 start"; 266 bool res = true; 267 PeerSocketInfo peerSocketInfo = { 268 .name = const_cast<char*>(PEER_SESSION_NAME.c_str()), 269 .networkId = const_cast<char*>(REMOTE_DEV_ID.c_str()), 270 .pkgName = const_cast<char*>(PKG_NAME_TEST.c_str()), 271 .dataType = DATA_TYPE_BYTES 272 }; 273 try { 274 SoftbusSessionDispatcher::OnSessionOpened(TEST_SESSION_ID, peerSocketInfo); 275 } catch (const exception &e) { 276 res = false; 277 LOGE("%{public}s", e.what()); 278 } 279 280 EXPECT_TRUE(res == true); 281 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionOpened_0200 end"; 282 } 283 284 /** 285 * @tc.name: SoftbusSessionDispatcherTest_OnSessionClosed_0100 286 * @tc.desc: Verify the OnSessionClosed function. 287 * @tc.type: FUNC 288 * @tc.require: SR000H0387 289 */ 290 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_OnSessionClosed_0100, TestSize.Level1) 291 { 292 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionClosed_0100 start"; 293 bool res = true; 294 ShutdownReason reason = SHUTDOWN_REASON_UNKNOWN; 295 try { 296 SoftbusSessionDispatcher::OnSessionClosed(TEST_SESSION_ID, reason); 297 } catch (const exception &e) { 298 res = false; 299 LOGE("%{public}s", e.what()); 300 } 301 302 EXPECT_TRUE(res == true); 303 GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionClosed_0100 end"; 304 } 305 } // namespace Test 306 } // namespace DistributedFile 307 } // namespace Storage 308 } // namespace OHOS 309