1 /*
2 * Copyright (c) 2021 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 #include <gtest/gtest.h>
16 #include <gmock/gmock.h>
17
18 #include "communicator_proxy.h"
19 #include "db_constant.h"
20 #include "distributeddb_data_generate_unit_test.h"
21 #include "distributeddb_tools_unit_test.h"
22 #include "kv_store_nb_delegate.h"
23 #include "mock_communicator.h"
24 #include "platform_specific.h"
25 #include "virtual_communicator_aggregator.h"
26
27 using namespace testing::ext;
28 using namespace testing;
29 using namespace DistributedDB;
30 using namespace DistributedDBUnitTest;
31 using namespace std;
32 using ::testing::Matcher;
33 using ::testing::DoAll;
34
35 namespace {
36 string g_testDir;
37 const string STORE_ID = "kv_store_sync_test";
38 const std::string DEVICE_B = "deviceB";
39 const std::string DEVICE_C = "deviceC";
40 const std::string DEVICE_D = "deviceD";
41 const std::string DEVICE_E = "deviceE";
42
43
44 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
45 KvStoreConfig g_config;
46 DistributedDBToolsUnitTest g_tool;
47 DBStatus g_kvDelegateStatus = INVALID_ARGS;
48 KvStoreNbDelegate* g_kvDelegatePtr = nullptr;
49
50 // the type of g_kvDelegateCallback is function<void(DBStatus, KvStoreDelegate*)>
51 auto g_kvDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
52 placeholders::_1, placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvDelegatePtr));
53 }
54
55 class DistributedDBCommunicatorProxyTest : public testing::Test {
56 public:
57 static void SetUpTestCase(void);
58 static void TearDownTestCase(void);
59 void SetUp();
60 void TearDown();
61
62 protected:
63 MockCommunicator extComm_;
64 MockCommunicator mainComm_;
65 CommunicatorProxy *commProxy_ = nullptr;
66 };
67
SetUpTestCase(void)68 void DistributedDBCommunicatorProxyTest::SetUpTestCase(void)
69 {
70 /**
71 * @tc.setup: Init datadir and Virtual Communicator.
72 */
73 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
74 g_config.dataDir = g_testDir;
75 g_mgr.SetKvStoreConfig(g_config);
76
77 string dir = g_testDir + "/single_ver";
78 DIR* dirTmp = opendir(dir.c_str());
79 if (dirTmp == nullptr) {
80 OS::MakeDBDirectory(dir);
81 } else {
82 closedir(dirTmp);
83 }
84
85 auto communicatorAggregator = new (std::nothrow) VirtualCommunicatorAggregator();
86 ASSERT_TRUE(communicatorAggregator != nullptr);
87 RuntimeContext::GetInstance()->SetCommunicatorAggregator(communicatorAggregator);
88 }
89
TearDownTestCase(void)90 void DistributedDBCommunicatorProxyTest::TearDownTestCase(void)
91 {
92 /**
93 * @tc.teardown: Release virtual Communicator and clear data dir.
94 */
95 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir) != 0) {
96 LOGE("rm test db files error!");
97 }
98 RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
99 }
100
SetUp(void)101 void DistributedDBCommunicatorProxyTest::SetUp(void)
102 {
103 DistributedDBToolsUnitTest::PrintTestCaseInfo();
104 /**
105 * @tc.setup: Get a KvStoreNbDelegate and init the CommunicatorProxy
106 */
107 KvStoreNbDelegate::Option option;
108 g_mgr.GetKvStore(STORE_ID, option, g_kvDelegateCallback);
109 std::string identifier = g_mgr.GetKvStoreIdentifier(USER_ID, APP_ID, STORE_ID);
110 ASSERT_TRUE(g_kvDelegateStatus == OK);
111 ASSERT_TRUE(g_kvDelegatePtr != nullptr);
112 commProxy_ = new (std::nothrow) CommunicatorProxy();
113 ASSERT_TRUE(commProxy_ != nullptr);
114 commProxy_->SetMainCommunicator(&mainComm_);
115 commProxy_->SetEqualCommunicator(&extComm_, identifier, { DEVICE_C });
116 }
117
TearDown(void)118 void DistributedDBCommunicatorProxyTest::TearDown(void)
119 {
120 /**
121 * @tc.teardown: Release the KvStoreNbDelegate and CommunicatorProxy
122 */
123 if (g_kvDelegatePtr != nullptr) {
124 ASSERT_EQ(g_mgr.CloseKvStore(g_kvDelegatePtr), OK);
125 g_kvDelegatePtr = nullptr;
126 DBStatus status = g_mgr.DeleteKvStore(STORE_ID);
127 LOGD("delete kv store status %d", status);
128 ASSERT_TRUE(status == OK);
129 }
130 if (commProxy_ != nullptr) {
131 RefObject::DecObjRef(commProxy_);
132 }
133 commProxy_ = nullptr;
134 }
135
136 /**
137 * @tc.name: Interface set equal 001
138 * @tc.desc: Test set equal identifier from interface.
139 * @tc.type: FUNC
140 * @tc.require: AR000F4GVG
141 * @tc.author: xushaohua
142 */
143 HWTEST_F(DistributedDBCommunicatorProxyTest, InterfaceSetEqualId001, TestSize.Level1)
144 {
145 /**
146 * @tc.steps: step1. Call GetKvStoreIdentifier to make a store identifier.
147 */
148 std::string identifier = g_mgr.GetKvStoreIdentifier("default", APP_ID, STORE_ID);
149
150 /**
151 * @tc.steps: step2. Call SetEqualIdentifier to set the store identifier B, D, E.
152 * @tc.expected: step2. SetEqualIdentifier return OK.
153 */
154 DBStatus status = g_kvDelegatePtr->SetEqualIdentifier(identifier, { DEVICE_B, DEVICE_D, DEVICE_E });
155 EXPECT_EQ(status, DBStatus::OK);
156
157 /**
158 * @tc.steps: step2. Call SetEqualIdentifier to set the store identifier B.
159 * @tc.expected: step2. SetEqualIdentifier return OK and D, E will offline.
160 */
161 status = g_kvDelegatePtr->SetEqualIdentifier(identifier, { DEVICE_B });
162 EXPECT_EQ(status, DBStatus::OK);
163 }
164
165 /**
166 * @tc.name: Register callback 001
167 * @tc.desc: Test register callback from CommunicatorProxy.
168 * @tc.type: FUNC
169 * @tc.require: AR000F4GVG
170 * @tc.author: xushaohua
171 */
172 HWTEST_F(DistributedDBCommunicatorProxyTest, RegCallBack001, TestSize.Level1)
173 {
174 OnMessageCallback msgCallback;
175 OnConnectCallback connCallback;
176 std::function<void(void)> sendableCallback;
177 Finalizer finalizer;
178
179 /**
180 * @tc.steps: step1. Call RegOnMessageCallback from CommProxy.
181 * @tc.expected: step1. mainComm and extComm's RegOnMessageCallback should be called once.
182 */
183 EXPECT_CALL(extComm_, RegOnMessageCallback(_, _)).Times(1);
184 EXPECT_CALL(mainComm_, RegOnMessageCallback(_, _)).Times(1);
185 commProxy_->RegOnMessageCallback(msgCallback, finalizer);
186
187 /**
188 * @tc.steps: step2. Call RegOnConnectCallback from CommProxy.
189 * @tc.expected: step2. mainComm and extComm's RegOnConnectCallback should be called once.
190 */
191 EXPECT_CALL(extComm_, RegOnConnectCallback(_, _)).Times(1);
192 EXPECT_CALL(mainComm_, RegOnConnectCallback(_, _)).Times(1);
193 commProxy_->RegOnConnectCallback(connCallback, finalizer);
194
195 /**
196 * @tc.steps: step3. Call RegOnSendableCallback from CommProxy.
197 * @tc.expected: step3. mainComm and extComm's RegOnSendableCallback should be called once.
198 */
199 EXPECT_CALL(extComm_, RegOnSendableCallback(_, _)).Times(1);
200 EXPECT_CALL(mainComm_, RegOnSendableCallback(_, _)).Times(1);
201 commProxy_->RegOnSendableCallback(sendableCallback, finalizer);
202 }
203
204 /**
205 * @tc.name: Activate 001
206 * @tc.desc: Test Activate called from CommunicatorProxy.
207 * @tc.type: FUNC
208 * @tc.require: AR000F4GVG
209 * @tc.author: xushaohua
210 */
211 HWTEST_F(DistributedDBCommunicatorProxyTest, Activate001, TestSize.Level1)
212 {
213 /**
214 * @tc.steps: step1. Call Activate from CommProxy.
215 * @tc.expected: step1. mainComm and extComm's Activate should be called once.
216 */
217 EXPECT_CALL(extComm_, Activate()).Times(1);
218 EXPECT_CALL(mainComm_, Activate()).Times(1);
219 commProxy_->Activate();
220 }
221
222 /**
223 * @tc.name: Get mtu 001
224 * @tc.desc: Test mtu called from CommunicatorProxy.
225 * @tc.type: FUNC
226 * @tc.require: AR000F4GVG
227 * @tc.author: xushaohua
228 */
229 HWTEST_F(DistributedDBCommunicatorProxyTest, GetMtu001, TestSize.Level1)
230 {
231 /**
232 * @tc.steps: step1. Call GetCommunicatorMtuSize from CommProxy with no param.
233 * @tc.expected: step1. GetCommunicatorMtuSize return DBConstant::MIN_MTU_SIZE.
234 */
235 EXPECT_CALL(mainComm_, GetCommunicatorMtuSize()).WillOnce(Return(DBConstant::MIN_MTU_SIZE));
236 EXPECT_EQ(commProxy_->GetCommunicatorMtuSize(), DBConstant::MIN_MTU_SIZE);
237
238 /**
239 * @tc.steps: step2. Call GetCommunicatorMtuSize from CommProxy with param DEVICE_C.
240 * @tc.expected: step2. GetCommunicatorMtuSize return DBConstant::MAX_MTU_SIZE.
241 */
242 EXPECT_CALL(extComm_, GetCommunicatorMtuSize(DEVICE_C)).WillOnce(Return(DBConstant::MAX_MTU_SIZE));
243 EXPECT_EQ(commProxy_->GetCommunicatorMtuSize(DEVICE_C), DBConstant::MAX_MTU_SIZE);
244 }
245
246 /**
247 * @tc.name: Get local identify 001
248 * @tc.desc: Test Get local identify from CommunicatorProxy.
249 * @tc.type: FUNC
250 * @tc.require: AR000F4GVG
251 * @tc.author: xushaohua
252 */
253 HWTEST_F(DistributedDBCommunicatorProxyTest, GetLocalIdentity001, TestSize.Level1)
254 {
255 /**
256 * @tc.steps: step1. Call GetLocalIdentity from CommProxy, and set mainComm return DEVICE_B.
257 * @tc.expected: step1. GetCommunicatorMtuSize return DEVICE_B and function call return E_OK.
258 */
259 EXPECT_CALL(mainComm_, GetLocalIdentity(_)).WillOnce(DoAll(SetArgReferee<0>(DEVICE_B), Return(E_OK)));
260 std::string localId;
261 EXPECT_EQ(commProxy_->GetLocalIdentity(localId), E_OK);
262 EXPECT_EQ(localId, DEVICE_B);
263 }
264
265 /**
266 * @tc.name: Get remote version 001
267 * @tc.desc: Test Get remote version from CommunicatorProxy.
268 * @tc.type: FUNC
269 * @tc.require: AR000F4GVG
270 * @tc.author: xushaohua
271 */
272 HWTEST_F(DistributedDBCommunicatorProxyTest, GetRemoteVersion001, TestSize.Level1)
273 {
274 /**
275 * @tc.steps: step1. Set mainComm called GetRemoteCommunicatorVersion will return SOFTWARE_VERSION_BASE.
276 */
277 EXPECT_CALL(mainComm_, GetRemoteCommunicatorVersion(DEVICE_B, _))
278 .WillOnce(DoAll(SetArgReferee<1>(SOFTWARE_VERSION_BASE), Return(E_OK)));
279
280 /**
281 * @tc.steps: step2. Call GetRemoteCommunicatorVersion from CommProxy with param DEVICE_B.
282 * @tc.expected: step2. GetRemoteCommunicatorVersion return SOFTWARE_VERSION_BASE and function call return E_OK.
283 */
284 uint16_t version = 0;
285 EXPECT_EQ(commProxy_->GetRemoteCommunicatorVersion(DEVICE_B, version), E_OK);
286 EXPECT_EQ(version, SOFTWARE_VERSION_BASE);
287
288 /**
289 * @tc.steps: step3. Set extComm called GetRemoteCommunicatorVersion will return SOFTWARE_VERSION_CURRENT.
290 */
291 EXPECT_CALL(extComm_, GetRemoteCommunicatorVersion(DEVICE_C, _))
292 .WillOnce(DoAll(SetArgReferee<1>(SOFTWARE_VERSION_CURRENT), Return(E_OK)));
293
294 /**
295 * @tc.steps: step4. Call GetRemoteCommunicatorVersion from CommProxy with param DEVICE_C.
296 * @tc.expected: step4. GetRemoteCommunicatorVersion return SOFTWARE_VERSION_CURRENT and function call return E_OK.
297 */
298 EXPECT_EQ(commProxy_->GetRemoteCommunicatorVersion(DEVICE_C, version), E_OK);
299 EXPECT_EQ(version, SOFTWARE_VERSION_CURRENT);
300 }
301
302 /**
303 * @tc.name: Send message 001
304 * @tc.desc: Test Send message from CommunicatorProxy.
305 * @tc.type: FUNC
306 * @tc.require: AR000F4GVG
307 * @tc.author: xushaohua
308 */
309 HWTEST_F(DistributedDBCommunicatorProxyTest, SendMessage001, TestSize.Level1)
310 {
311 /**
312 * @tc.steps: step1. Call SendMessage from CommProxy with param DEVICE_B.
313 * @tc.expected: step1. MainComm's SendMessage willed called and return E_OK.
314 */
315 SendConfig conf = {true, false, 0};
316 EXPECT_CALL(mainComm_, SendMessage(DEVICE_B, _, _, _)).WillOnce(Return(E_OK));
317 EXPECT_EQ(commProxy_->SendMessage(DEVICE_B, nullptr, conf, nullptr), E_OK);
318
319 /**
320 * @tc.steps: step1. Call SendMessage from CommProxy with param DEVICE_C.
321 * @tc.expected: step1. ExtComm's SendMessage willed called and return E_OK.
322 */
323 EXPECT_CALL(extComm_, SendMessage(DEVICE_C, _, _, _)).WillOnce(Return(E_OK));
324 EXPECT_EQ(commProxy_->SendMessage(DEVICE_C, nullptr, conf, nullptr), E_OK);
325 }
326
327 /**
328 * @tc.name: Get timeout time 001
329 * @tc.desc: Test get timeout called from CommunicatorProxy.
330 * @tc.type: FUNC
331 * @tc.require: AR000F4GVG
332 * @tc.author: xushaohua
333 */
334 HWTEST_F(DistributedDBCommunicatorProxyTest, GetTimeout001, TestSize.Level1)
335 {
336 /**
337 * @tc.steps: step1. Call GetTimeout from CommProxy with no param.
338 * @tc.expected: step1. GetTimeout return DBConstant::MIN_TIMEOUT.
339 */
340 EXPECT_CALL(mainComm_, GetTimeout()).WillOnce(Return(DBConstant::MIN_TIMEOUT));
341 EXPECT_EQ(commProxy_->GetTimeout(), DBConstant::MIN_TIMEOUT);
342
343 /**
344 * @tc.steps: step2. Call GetTimeout from CommProxy with param DEVICE_C.
345 * @tc.expected: step2. GetTimeout return DBConstant::MAX_MTU_SIZE.
346 */
347 EXPECT_CALL(extComm_, GetTimeout(DEVICE_C)).WillOnce(Return(DBConstant::MAX_TIMEOUT));
348 EXPECT_EQ(commProxy_->GetTimeout(DEVICE_C), DBConstant::MAX_TIMEOUT);
349 }
350