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 <memory> 17 #include <unistd.h> 18 19 #include "gtest/gtest.h" 20 #include "network/session_pool.h" 21 #include "network/softbus/softbus_session.h" 22 #include "network/kernel_talker.h" 23 #include "utils_log.h" 24 25 namespace OHOS { 26 namespace Storage { 27 namespace DistributedFile { 28 namespace Test { 29 using namespace testing::ext; 30 using namespace std; 31 32 constexpr int USER_ID = 100; 33 constexpr int TEST_SESSION_ID = 10; 34 35 class SessionPoolTest : public testing::Test { 36 public: SetUpTestCase(void)37 static void SetUpTestCase(void) {}; TearDownTestCase(void)38 static void TearDownTestCase(void) {}; SetUp()39 void SetUp() {}; TearDown()40 void TearDown() {}; 41 }; 42 43 /** 44 * @tc.name: SessionPoolTest_HoldSession_0100 45 * @tc.desc: Verify the HoldSession function. 46 * @tc.type: FUNC 47 * @tc.require: SR000H0387 48 */ 49 HWTEST_F(SessionPoolTest, SessionPoolTest_HoldSession_0100, TestSize.Level1) 50 { 51 GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0100 start"; 52 auto session = make_shared<SoftbusSession>(TEST_SESSION_ID); 53 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account")); 54 weak_ptr<MountPoint> wmp = smp; __anon354fc2b40102(NotifyParam ¶m) 55 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](const std::string &cid) {}); 56 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker); 57 58 bool res = true; 59 try { 60 pool->HoldSession(session); 61 } catch (const exception &e) { 62 res = false; 63 LOGE("%{public}s", e.what()); 64 } 65 66 EXPECT_TRUE(res == true); 67 GTEST_LOG_(INFO) << "SessionPoolTest_HoldSession_0100 end"; 68 } 69 70 /** 71 * @tc.name: SessionPoolTest_ReleaseSession_Fd_0100 72 * @tc.desc: Verify the ReleaseSession by Fd function. 73 * @tc.type: FUNC 74 * @tc.require: SR000H0387 75 */ 76 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Fd_0100, TestSize.Level1) 77 { 78 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Fd_0100 start"; 79 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account")); 80 weak_ptr<MountPoint> wmp = smp; __anon354fc2b40302(NotifyParam ¶m) 81 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](const std::string &cid) {}); 82 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker); 83 84 bool res = true; 85 try { 86 pool->ReleaseSession(1); 87 } catch (const exception &e) { 88 res = false; 89 LOGE("%{public}s", e.what()); 90 } 91 92 EXPECT_TRUE(res == true); 93 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Fd_0100 end"; 94 } 95 96 /** 97 * @tc.name: SessionPoolTest_ReleaseSession_Cid_0100 98 * @tc.desc: Verify the ReleaseSession by Cid function. 99 * @tc.type: FUNC 100 * @tc.require: SR000H0387 101 */ 102 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseSession_Cid_0100, TestSize.Level1) 103 { 104 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0100 start"; 105 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account")); 106 weak_ptr<MountPoint> wmp = smp; __anon354fc2b40502(NotifyParam ¶m) 107 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](const std::string &cid) {}); 108 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker); 109 110 bool res = true; 111 try { 112 pool->ReleaseSession("testSession"); 113 } catch (const exception &e) { 114 res = false; 115 LOGE("%{public}s", e.what()); 116 } 117 118 EXPECT_TRUE(res == true); 119 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseSession_Cid_0100 end"; 120 } 121 122 /** 123 * @tc.name: SessionPoolTest_ReleaseAllSession_0100 124 * @tc.desc: Verify the ReleaseAllSession function. 125 * @tc.type: FUNC 126 * @tc.require: SR000H0387 127 */ 128 HWTEST_F(SessionPoolTest, SessionPoolTest_ReleaseAllSession_0100, TestSize.Level1) 129 { 130 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseAllSession_0100 start"; 131 auto smp = make_shared<MountPoint>(Utils::DfsuMountArgumentDescriptors::Alpha(USER_ID, "account")); 132 weak_ptr<MountPoint> wmp = smp; __anon354fc2b40702(NotifyParam ¶m) 133 auto kernelTalker = std::make_shared<KernelTalker>(wmp, [](NotifyParam ¶m) {}, [](const std::string &cid) {}); 134 shared_ptr<SessionPool> pool = make_shared<SessionPool>(kernelTalker); 135 136 bool res = true; 137 try { 138 pool->ReleaseAllSession(); 139 } catch (const exception &e) { 140 res = false; 141 LOGE("%{public}s", e.what()); 142 } 143 144 EXPECT_TRUE(res == true); 145 GTEST_LOG_(INFO) << "SessionPoolTest_ReleaseAllSession_0100 end"; 146 } 147 } // namespace Test 148 } // namespace DistributedFile 149 } // namespace Storage 150 } // namespace OHOS 151