• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "connect_count/connect_count.h"
17 
18 #include "gtest/gtest.h"
19 #include "gmock/gmock.h"
20 #include "ipc/i_file_dfs_listener.h"
21 #include "dfs_error.h"
22 #include "utils_directory.h"
23 #include "utils_log.h"
24 
25 namespace OHOS {
26 namespace Storage {
27 namespace DistributedFile {
28 namespace Test {
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace OHOS::FileManagement;
32 
33 class MockFileDfsListener : public IFileDfsListener {
34 public:
35     MOCK_METHOD2(OnStatus, void(const std::string &networkId, int32_t status));
36     MOCK_METHOD0(AsObject, sptr<IRemoteObject>());
37 };
38 
39 class ConnectCountTest : public testing::Test {
40 public:
41     static void SetUpTestCase(void);
42     static void TearDownTestCase(void);
43     void SetUp();
44     void TearDown();
45 
46 public:
47     static inline std::shared_ptr<ConnectCount> connectCount_ = nullptr;
48     static inline uint32_t testCallingTokenId = 1234;
49     static inline std::string testNetworkId = "testNetworkId";
50     static inline sptr<IFileDfsListener> testListener = nullptr;
51 
52     const int32_t ON_STATUS_OFFLINE = 13900046;
53 };
54 
SetUpTestCase(void)55 void ConnectCountTest::SetUpTestCase(void)
56 {
57     GTEST_LOG_(INFO) << "ConnectCountTest SetUpTestCase";
58     connectCount_ = ConnectCount::GetInstance();
59     testListener = new MockFileDfsListener();  // Use Mock class
60 }
61 
TearDownTestCase(void)62 void ConnectCountTest::TearDownTestCase(void)
63 {
64     GTEST_LOG_(INFO) << "ConnectCountTest TearDownTestCase";
65 
66     connectCount_ = nullptr;
67     testListener = nullptr;
68 }
69 
SetUp(void)70 void ConnectCountTest::SetUp(void)
71 {
72     GTEST_LOG_(INFO) << "SetUp";
73 }
74 
TearDown(void)75 void ConnectCountTest::TearDown(void)
76 {
77     GTEST_LOG_(INFO) << "TearDown";
78     connectCount_->RemoveAllConnect();
79 }
80 
81 /**
82  * @tc.name: ConnectCountTest_GetInstance_001
83  * @tc.desc: Verify GetInstance returns the same instance.
84  * @tc.type: FUNC
85  * @tc.require: I7TDJK
86  */
87 HWTEST_F(ConnectCountTest, GetInstance_001, TestSize.Level1)
88 {
89     GTEST_LOG_(INFO) << "GetInstance_001 start";
90     auto instance1 = ConnectCount::GetInstance();
91     auto instance2 = ConnectCount::GetInstance();
92     EXPECT_EQ(instance1, instance2);  // Verify singleton instance
93     GTEST_LOG_(INFO) << "GetInstance_001 end";
94 }
95 
96 /**
97  * @tc.name: ConnectCountTest_AddConnect_001
98  * @tc.desc: Verify AddConnect adds a new connection.
99  * @tc.type: FUNC
100  * @tc.require: I7TDJK
101  */
102 HWTEST_F(ConnectCountTest, AddConnect_001, TestSize.Level1)
103 {
104     GTEST_LOG_(INFO) << "AddConnect_001 start";
105     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
106     EXPECT_TRUE(connectCount_->CheckCount(testNetworkId));  // Verify connection is added
107     GTEST_LOG_(INFO) << "AddConnect_001 end";
108 }
109 
110 /**
111  * @tc.name: ConnectCountTest_AddConnect_002
112  * @tc.desc: Verify AddConnect increments the count for existing connection.
113  * @tc.type: FUNC
114  * @tc.require: I7TDJK
115  */
116 HWTEST_F(ConnectCountTest, AddConnect_002, TestSize.Level1)
117 {
118     GTEST_LOG_(INFO) << "AddConnect_002 start";
119     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
120     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
121     auto networkIds = connectCount_->GetNetworkIds(testCallingTokenId);
122     EXPECT_EQ(networkIds.size(), 1);  // Verify only one networkId is added
123     GTEST_LOG_(INFO) << "AddConnect_002 end";
124 }
125 
126 /**
127  * @tc.name: ConnectCountTest_RemoveConnect_001
128  * @tc.desc: Verify RemoveConnect removes a connection by callingTokenId.
129  * @tc.type: FUNC
130  * @tc.require: I7TDJK
131  */
132 HWTEST_F(ConnectCountTest, RemoveConnect_001, TestSize.Level1)
133 {
134     GTEST_LOG_(INFO) << "RemoveConnect_001 start";
135     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
136     auto networkIds = connectCount_->RemoveConnect(testCallingTokenId);
137     EXPECT_EQ(networkIds.size(), 1);  // Verify networkId is returned
138     EXPECT_FALSE(connectCount_->CheckCount(testNetworkId));  // Verify connection is removed
139     GTEST_LOG_(INFO) << "RemoveConnect_001 end";
140 }
141 
142 /**
143  * @tc.name: ConnectCountTest_RemoveConnect_002
144  * @tc.desc: Verify RemoveConnect removes a connection by networkId.
145  * @tc.type: FUNC
146  * @tc.require: I7TDJK
147  */
148 HWTEST_F(ConnectCountTest, RemoveConnect_002, TestSize.Level1)
149 {
150     GTEST_LOG_(INFO) << "RemoveConnect_002 start";
151     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
152     connectCount_->RemoveConnect(testNetworkId);
153     EXPECT_FALSE(connectCount_->CheckCount(testNetworkId));  // Verify connection is removed
154     GTEST_LOG_(INFO) << "RemoveConnect_002 end";
155 }
156 
157 /**
158  * @tc.name: ConnectCountTest_RemoveConnect_003
159  * @tc.desc: Verify RemoveConnect removes a connection by callingTokenId and networkId.
160  * @tc.type: FUNC
161  * @tc.require: I7TDJK
162  */
163 HWTEST_F(ConnectCountTest, RemoveConnect_003, TestSize.Level1)
164 {
165     GTEST_LOG_(INFO) << "RemoveConnect_003 start";
166     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
167     connectCount_->RemoveConnect(testCallingTokenId, testNetworkId);
168     EXPECT_FALSE(connectCount_->CheckCount(testNetworkId));  // Verify connection is removed
169     GTEST_LOG_(INFO) << "RemoveConnect_003 end";
170 }
171 
172 /**
173  * @tc.name: ConnectCountTest_RemoveAllConnect_001
174  * @tc.desc: Verify RemoveAllConnect removes all connections.
175  * @tc.type: FUNC
176  * @tc.require: I7TDJK
177  */
178 HWTEST_F(ConnectCountTest, RemoveAllConnect_001, TestSize.Level1)
179 {
180     GTEST_LOG_(INFO) << "RemoveAllConnect_001 start";
181     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
182     connectCount_->RemoveAllConnect();
183     EXPECT_FALSE(connectCount_->CheckCount(testNetworkId));  // Verify all connections are removed
184     GTEST_LOG_(INFO) << "RemoveAllConnect_001 end";
185 }
186 
187 /**
188  * @tc.name: ConnectCountTest_NotifyRemoteReverseObj_001
189  * @tc.desc: Verify NotifyRemoteReverseObj notifies the listener.
190  * @tc.type: FUNC
191  * @tc.require: I7TDJK
192  */
193 HWTEST_F(ConnectCountTest, NotifyRemoteReverseObj_001, TestSize.Level1)
194 {
195     GTEST_LOG_(INFO) << "NotifyRemoteReverseObj_001 start";
196 
197     // 设置 EXPECT_CALL
198     EXPECT_CALL(*static_cast<MockFileDfsListener*>(testListener.GetRefPtr()),
199                 OnStatus(testNetworkId, ON_STATUS_OFFLINE))
200                 .Times(1);  // 仅在当前测试用例中生效
201 
202     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
203     connectCount_->NotifyRemoteReverseObj(testNetworkId, ON_STATUS_OFFLINE);
204 
205     // 清除 EXPECT_CALL 断言
206     testing::Mock::VerifyAndClearExpectations(testListener.GetRefPtr());
207 
208     GTEST_LOG_(INFO) << "NotifyRemoteReverseObj_001 end";
209 }
210 
211 /**
212  * @tc.name: ConnectCountTest_FindCallingTokenIdForListerner_001
213  * @tc.desc: Verify FindCallingTokenIdForListerner finds the correct callingTokenId.
214  * @tc.type: FUNC
215  * @tc.require: I7TDJK
216  */
217 HWTEST_F(ConnectCountTest, FindCallingTokenIdForListerner_001, TestSize.Level1)
218 {
219     GTEST_LOG_(INFO) << "FindCallingTokenIdForListerner_001 start";
220     connectCount_->AddConnect(testCallingTokenId, testNetworkId, testListener);
221     uint32_t foundCallingTokenId = 0;
222     int32_t ret = connectCount_->FindCallingTokenIdForListerner(testListener->AsObject(), foundCallingTokenId);
223     EXPECT_EQ(ret, FileManagement::E_OK);  // Verify success
224     EXPECT_EQ(foundCallingTokenId, testCallingTokenId);  // Verify correct tokenId is found
225     GTEST_LOG_(INFO) << "FindCallingTokenIdForListerner_001 end";
226 }
227 
228 /**
229  * @tc.name: ConnectCountTest_FindCallingTokenIdForListerner_002
230  * @tc.desc: Verify FindCallingTokenIdForListerner returns error for invalid listener.
231  * @tc.type: FUNC
232  * @tc.require: I7TDJK
233  */
234 HWTEST_F(ConnectCountTest, FindCallingTokenIdForListerner_002, TestSize.Level1)
235 {
236     GTEST_LOG_(INFO) << "FindCallingTokenIdForListerner_002 start";
237     sptr<IRemoteObject> invalidListener = nullptr;
238     uint32_t foundCallingTokenId = 0;
239     int32_t ret = connectCount_->FindCallingTokenIdForListerner(invalidListener, foundCallingTokenId);
240     EXPECT_EQ(ret, FileManagement::ERR_BAD_VALUE);  // Verify error is returned
241     GTEST_LOG_(INFO) << "FindCallingTokenIdForListerner_002 end";
242 }
243 } // namespace Test
244 } // namespace DistributedFile
245 } // namespace Storage
246 } // namespace OHOS