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