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: AR000F4GVG
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: Register callback 001
165  * @tc.desc: Test register callback from CommunicatorProxy.
166  * @tc.type: FUNC
167  * @tc.require: AR000F4GVG
168  * @tc.author: xushaohua
169  */
170 HWTEST_F(DistributedDBCommunicatorProxyTest, RegCallBack001, TestSize.Level1)
171 {
172     OnMessageCallback msgCallback;
173     OnConnectCallback connCallback;
174     std::function<void(void)> sendableCallback;
175     Finalizer finalizer;
176 
177     /**
178      * @tc.steps: step1. Call RegOnMessageCallback from CommProxy.
179      * @tc.expected: step1. mainComm and extComm's RegOnMessageCallback should be called once.
180      */
181     EXPECT_CALL(extComm_, RegOnMessageCallback(_, _)).Times(1);
182     EXPECT_CALL(mainComm_, RegOnMessageCallback(_, _)).Times(1);
183     commProxy_->RegOnMessageCallback(msgCallback, finalizer);
184 
185     /**
186      * @tc.steps: step2. Call RegOnConnectCallback from CommProxy.
187      * @tc.expected: step2. mainComm and extComm's RegOnConnectCallback should be called once.
188      */
189     EXPECT_CALL(extComm_, RegOnConnectCallback(_, _)).Times(1);
190     EXPECT_CALL(mainComm_, RegOnConnectCallback(_, _)).Times(1);
191     commProxy_->RegOnConnectCallback(connCallback, finalizer);
192 
193     /**
194      * @tc.steps: step3. Call RegOnSendableCallback from CommProxy.
195      * @tc.expected: step3. mainComm and extComm's RegOnSendableCallback should be called once.
196      */
197     EXPECT_CALL(extComm_, RegOnSendableCallback(_, _)).Times(1);
198     EXPECT_CALL(mainComm_, RegOnSendableCallback(_, _)).Times(1);
199     commProxy_->RegOnSendableCallback(sendableCallback, finalizer);
200 }
201 
202 /**
203  * @tc.name: Activate 001
204  * @tc.desc: Test Activate called from CommunicatorProxy.
205  * @tc.type: FUNC
206  * @tc.require: AR000F4GVG
207  * @tc.author: xushaohua
208  */
209 HWTEST_F(DistributedDBCommunicatorProxyTest, Activate001, TestSize.Level1)
210 {
211     /**
212      * @tc.steps: step1. Call Activate from CommProxy.
213      * @tc.expected: step1. mainComm and extComm's Activate should be called once.
214      */
215     EXPECT_CALL(extComm_, Activate()).Times(1);
216     EXPECT_CALL(mainComm_, Activate()).Times(1);
217     commProxy_->Activate();
218 }
219 
220 /**
221  * @tc.name: Get mtu 001
222  * @tc.desc: Test mtu called from CommunicatorProxy.
223  * @tc.type: FUNC
224  * @tc.require: AR000F4GVG
225  * @tc.author: xushaohua
226  */
227 HWTEST_F(DistributedDBCommunicatorProxyTest, GetMtu001, TestSize.Level1)
228 {
229     /**
230      * @tc.steps: step1. Call GetCommunicatorMtuSize from CommProxy with no param.
231      * @tc.expected: step1. GetCommunicatorMtuSize return DBConstant::MIN_MTU_SIZE.
232      */
233     EXPECT_CALL(mainComm_, GetCommunicatorMtuSize()).WillOnce(Return(DBConstant::MIN_MTU_SIZE));
234     EXPECT_EQ(commProxy_->GetCommunicatorMtuSize(), DBConstant::MIN_MTU_SIZE);
235 
236     /**
237      * @tc.steps: step2. Call GetCommunicatorMtuSize from CommProxy with param DEVICE_C.
238      * @tc.expected: step2. GetCommunicatorMtuSize return DBConstant::MAX_MTU_SIZE.
239      */
240     EXPECT_CALL(extComm_, GetCommunicatorMtuSize(DEVICE_C)).WillOnce(Return(DBConstant::MAX_MTU_SIZE));
241     EXPECT_EQ(commProxy_->GetCommunicatorMtuSize(DEVICE_C), DBConstant::MAX_MTU_SIZE);
242 }
243 
244 /**
245  * @tc.name: Get local identify 001
246  * @tc.desc: Test Get local identify from CommunicatorProxy.
247  * @tc.type: FUNC
248  * @tc.require: AR000F4GVG
249  * @tc.author: xushaohua
250  */
251 HWTEST_F(DistributedDBCommunicatorProxyTest, GetLocalIdentity001, TestSize.Level1)
252 {
253     /**
254      * @tc.steps: step1. Call GetLocalIdentity from CommProxy, and set mainComm return DEVICE_B.
255      * @tc.expected: step1. GetCommunicatorMtuSize return DEVICE_B and function call return E_OK.
256      */
257     EXPECT_CALL(mainComm_, GetLocalIdentity(_)).WillOnce(DoAll(SetArgReferee<0>(DEVICE_B), Return(E_OK)));
258     std::string localId;
259     EXPECT_EQ(commProxy_->GetLocalIdentity(localId), E_OK);
260     EXPECT_EQ(localId, DEVICE_B);
261 }
262 
263 /**
264  * @tc.name: Get remote version 001
265  * @tc.desc: Test Get remote version from CommunicatorProxy.
266  * @tc.type: FUNC
267  * @tc.require: AR000F4GVG
268  * @tc.author: xushaohua
269  */
270 HWTEST_F(DistributedDBCommunicatorProxyTest, GetRemoteVersion001, TestSize.Level1)
271 {
272     /**
273      * @tc.steps: step1. Set mainComm called GetRemoteCommunicatorVersion will return SOFTWARE_VERSION_BASE.
274      */
275     EXPECT_CALL(mainComm_, GetRemoteCommunicatorVersion(DEVICE_B, _))
276         .WillOnce(DoAll(SetArgReferee<1>(SOFTWARE_VERSION_BASE), Return(E_OK)));
277 
278     /**
279      * @tc.steps: step2. Call GetRemoteCommunicatorVersion from CommProxy with param DEVICE_B.
280      * @tc.expected: step2. GetRemoteCommunicatorVersion return SOFTWARE_VERSION_BASE and function call return E_OK.
281      */
282     uint16_t version = 0;
283     EXPECT_EQ(commProxy_->GetRemoteCommunicatorVersion(DEVICE_B, version), E_OK);
284     EXPECT_EQ(version, SOFTWARE_VERSION_BASE);
285 
286     /**
287      * @tc.steps: step3. Set extComm called GetRemoteCommunicatorVersion will return SOFTWARE_VERSION_CURRENT.
288      */
289     EXPECT_CALL(extComm_, GetRemoteCommunicatorVersion(DEVICE_C, _))
290         .WillOnce(DoAll(SetArgReferee<1>(SOFTWARE_VERSION_CURRENT), Return(E_OK)));
291 
292     /**
293      * @tc.steps: step4. Call GetRemoteCommunicatorVersion from CommProxy with param DEVICE_C.
294      * @tc.expected: step4. GetRemoteCommunicatorVersion return SOFTWARE_VERSION_CURRENT and function call return E_OK.
295      */
296     EXPECT_EQ(commProxy_->GetRemoteCommunicatorVersion(DEVICE_C, version), E_OK);
297     EXPECT_EQ(version, SOFTWARE_VERSION_CURRENT);
298 }
299 
300 /**
301  * @tc.name: Send message 001
302  * @tc.desc: Test Send message from CommunicatorProxy.
303  * @tc.type: FUNC
304  * @tc.require: AR000F4GVG
305  * @tc.author: xushaohua
306  */
307 HWTEST_F(DistributedDBCommunicatorProxyTest, SendMessage001, TestSize.Level1)
308 {
309     /**
310      * @tc.steps: step1. Call SendMessage from CommProxy with param DEVICE_B.
311      * @tc.expected: step1. MainComm's SendMessage willed called and return E_OK.
312      */
313     SendConfig conf = {true, false, 0};
314     EXPECT_CALL(mainComm_, SendMessage(DEVICE_B, _, _, _)).WillOnce(Return(E_OK));
315     EXPECT_EQ(commProxy_->SendMessage(DEVICE_B, nullptr, conf, nullptr), E_OK);
316 
317     /**
318      * @tc.steps: step1. Call SendMessage from CommProxy with param DEVICE_C.
319      * @tc.expected: step1. ExtComm's SendMessage willed called and return E_OK.
320      */
321     EXPECT_CALL(extComm_, SendMessage(DEVICE_C, _, _, _)).WillOnce(Return(E_OK));
322     EXPECT_EQ(commProxy_->SendMessage(DEVICE_C, nullptr, conf, nullptr), E_OK);
323 }
324 
325 /**
326  * @tc.name: Get timeout time 001
327  * @tc.desc: Test get timeout called from CommunicatorProxy.
328  * @tc.type: FUNC
329  * @tc.require: AR000F4GVG
330  * @tc.author: xushaohua
331  */
332 HWTEST_F(DistributedDBCommunicatorProxyTest, GetTimeout001, TestSize.Level1)
333 {
334     /**
335      * @tc.steps: step1. Call GetTimeout from CommProxy with no param.
336      * @tc.expected: step1. GetTimeout return DBConstant::MIN_TIMEOUT.
337      */
338     EXPECT_CALL(mainComm_, GetTimeout()).WillOnce(Return(DBConstant::MIN_TIMEOUT));
339     EXPECT_EQ(commProxy_->GetTimeout(), DBConstant::MIN_TIMEOUT);
340 
341     /**
342      * @tc.steps: step2. Call GetTimeout from CommProxy with param DEVICE_C.
343      * @tc.expected: step2. GetTimeout return DBConstant::MAX_MTU_SIZE.
344      */
345     EXPECT_CALL(extComm_, GetTimeout(DEVICE_C)).WillOnce(Return(DBConstant::MAX_TIMEOUT));
346     EXPECT_EQ(commProxy_->GetTimeout(DEVICE_C), DBConstant::MAX_TIMEOUT);
347 }
348