• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/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 
31 constexpr int TEST_SESSION_ID = 10;
32 constexpr int E_OK = 0;
33 constexpr int E_UNKNOWN = -1;
34 static const string SAME_ACCOUNT = "account";
35 
36 class SoftbusSessionDispatcherTest : public testing::Test {
37 public:
SetUpTestCase(void)38     static void SetUpTestCase(void) {};
TearDownTestCase(void)39     static void TearDownTestCase(void) {};
SetUp()40     void SetUp() {};
TearDown()41     void TearDown() {};
42 };
43 
44 /**
45  * @tc.name: SoftbusSessionDispatcherTest_RegisterSessionListener_0100
46  * @tc.desc: Verify the RegisterSessionListener/UnregisterSessionListener function.
47  * @tc.type: FUNC
48  * @tc.require: SR000H0387
49  */
50 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_RegisterSessionListener_0100, TestSize.Level1)
51 {
52     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_RegisterSessionListener_0100 start";
53     constexpr int userId = 100;
54     auto mp = make_unique<MountPoint>(
55             OHOS::Storage::DistributedFile::Utils::DfsuMountArgumentDescriptors::Alpha(userId, SAME_ACCOUNT));
56     shared_ptr<MountPoint> smp = move(mp);
57     weak_ptr<MountPoint> wmp(smp);
58     std::shared_ptr<SoftbusAgent> agent = std::make_shared<SoftbusAgent>(wmp);
59     weak_ptr<SoftbusAgent> wsba(agent);
60     const string busName = "testBus";
61     bool res = true;
62 
63     try {
64         SoftbusSessionDispatcher::RegisterSessionListener(busName, wsba);
65         SoftbusSessionDispatcher::UnregisterSessionListener(busName);
66     } catch (const exception &e) {
67         LOGE("%{public}s", e.what());
68         res = false;
69     }
70 
71     EXPECT_TRUE(res == true);
72     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_RegisterSessionListener_0100 end";
73 }
74 
75 /**
76  * @tc.name: SoftbusSessionDispatcherTest_GetAgent_0100
77  * @tc.desc: Verify the GetAgent function.
78  * @tc.type: FUNC
79  * @tc.require: SR000H0387
80  */
81 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_GetAgent_0100, TestSize.Level1)
82 {
83     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_GetAgent_0100 start";
84     try {
85         weak_ptr<SoftbusAgent> wp = SoftbusSessionDispatcher::GetAgent(TEST_SESSION_ID);
86         EXPECT_TRUE(wp.expired() == true);
87     } catch (const exception &e) {
88         LOGE("%{public}s", e.what());
89     }
90     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_GetAgent_0100 end";
91 }
92 
93 /**
94  * @tc.name: SoftbusSessionDispatcherTest_OnSessionOpened_0100
95  * @tc.desc: Verify the OnSessionOpened function.
96  * @tc.type: FUNC
97  * @tc.require: SR000H0387
98  */
99 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_OnSessionOpened_0100, TestSize.Level1)
100 {
101     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionOpened_0100 start";
102     bool res = true;
103 
104     try {
105         SoftbusSessionDispatcher::OnSessionOpened(TEST_SESSION_ID, E_OK);
106     } catch (const exception &e) {
107         res = false;
108         LOGE("%{public}s", e.what());
109     }
110 
111     EXPECT_TRUE(res == true);
112     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionOpened_0100 end";
113 }
114 
115 /**
116  * @tc.name: SoftbusSessionDispatcherTest_OnSessionOpened_0200
117  * @tc.desc: Verify the OnSessionOpened function.
118  * @tc.type: FUNC
119  * @tc.require: SR000H0387
120  */
121 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_OnSessionOpened_0200, TestSize.Level1)
122 {
123     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionOpened_0200 start";
124     bool res = true;
125 
126     try {
127         SoftbusSessionDispatcher::OnSessionOpened(TEST_SESSION_ID, E_UNKNOWN);
128     } catch (const exception &e) {
129         res = false;
130         LOGE("%{public}s", e.what());
131     }
132 
133     EXPECT_TRUE(res == true);
134     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionOpened_0200 end";
135 }
136 
137 /**
138  * @tc.name: SoftbusSessionDispatcherTest_OnSessionClosed_0100
139  * @tc.desc: Verify the OnSessionClosed function.
140  * @tc.type: FUNC
141  * @tc.require: SR000H0387
142  */
143 HWTEST_F(SoftbusSessionDispatcherTest, SoftbusSessionDispatcherTest_OnSessionClosed_0100, TestSize.Level1)
144 {
145     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionClosed_0100 start";
146     bool res = true;
147 
148     try {
149         SoftbusSessionDispatcher::OnSessionClosed(TEST_SESSION_ID);
150     } catch (const exception &e) {
151         res = false;
152         LOGE("%{public}s", e.what());
153     }
154 
155     EXPECT_TRUE(res == true);
156     GTEST_LOG_(INFO) << "SoftbusSessionDispatcherTest_OnSessionClosed_0100 end";
157 }
158 } // namespace Test
159 } // namespace DistributedFile
160 } // namespace Storage
161 } // namespace OHOS
162