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 #ifndef OMIT_MULTI_VER
16 #include <gtest/gtest.h>
17
18 #include "db_errno.h"
19 #include "default_factory.h"
20 #include "distributeddb_tools_unit_test.h"
21 #include "ikvdb_factory.h"
22
23 using namespace testing::ext;
24 using namespace DistributedDB;
25 using namespace DistributedDBUnitTest;
26 using namespace std;
27
28 namespace {
29 IKvDBCommitStorage::Property g_prop;
30 IKvDBCommitStorage *g_commitStorage = nullptr;
31 bool g_createFactory = false;
32 Version g_defaultCommitVer1 = 1;
33 Version g_defaultCommitVer2 = 2;
34 Version g_defaultCommitVer3 = 3;
35 Version g_defaultCommitVer4 = 4;
36 Version g_defaultCommitVer5 = 5;
37 Version g_defaultCommitVer6 = 6;
38 Version g_defaultCommitVer7 = 7;
39 Version g_defaultCommitVer8 = 8;
40 Version g_defaultCommitVer9 = 9;
41 Version g_defaultCommitVer10 = 10;
42 Version g_defaultCommitVer11 = 11;
43 Version g_defaultCommitVer12 = 12;
44 string g_defaultCommitID0 = "";
45 string g_defaultCommitID1 = "commit_ID_1";
46 string g_defaultCommitID2 = "commit_ID_2";
47 string g_defaultCommitID3 = "commit_ID_3";
48 string g_defaultCommitID4 = "commit_ID_4";
49 string g_defaultCommitID5 = "commit_ID_5";
50 string g_defaultCommitID6 = "commit_ID_6";
51 string g_defaultCommitID7 = "commit_ID_7";
52 string g_defaultCommitID8 = "commit_ID_8";
53 string g_defaultCommitID9 = "commit_ID_9";
54 string g_defaultCommitID10 = "commit_ID_10";
55 string g_defaultCommitID11 = "commit_ID_11";
56 string g_defaultCommitID12 = "commit_ID_12";
57 string g_defaultCommitID13 = "commit_ID_13";
58 string g_defaultCommitID14 = "commit_ID_14";
59 DeviceID g_localDevice = "local";
60 DeviceID g_remoteDeviceA = "remote_device_A";
61 DeviceID g_remoteDeviceB = "remote_device_B";
62 DeviceID g_remoteDeviceC = "remote_device_C";
63 DeviceID g_remoteDeviceD = "remote_device_D";
64 const Timestamp TIME_STAMP1 = 100;
65 const Timestamp TIME_STAMP2 = 200;
66 const Timestamp TIME_STAMP3 = 300;
67 const Timestamp TIME_STAMP4 = 400;
68 const Timestamp TIME_STAMP5 = 500;
69 const Timestamp TIME_STAMP6 = 600;
70 const Timestamp TIME_STAMP7 = 700;
71 const Timestamp TIME_STAMP8 = 800;
72 const Timestamp TIME_STAMP9 = 900;
73 const Timestamp TIME_STAMP10 = 1000;
74 const Timestamp TIME_STAMP11 = 1100;
75 const Timestamp TIME_STAMP12 = 1200;
76
77 struct CommitInfo {
78 Version version;
79 string commitID;
80 string leftCommitID;
81 string rightCommitID;
82 Timestamp timestamp;
83 bool localFlag;
84 DeviceID deviceInfo;
85 };
86
TransCommitIDToStr(const CommitID & inputCommitID)87 string TransCommitIDToStr(const CommitID &inputCommitID)
88 {
89 string commitIDStr = "";
90 if (inputCommitID.size() != 0) {
91 commitIDStr.resize(inputCommitID.size());
92 commitIDStr.assign(inputCommitID.begin(), inputCommitID.end());
93 }
94 return commitIDStr;
95 }
96
CompareCommitWithExpectation(const IKvDBCommit * commit,const CommitInfo & commitInfo)97 void CompareCommitWithExpectation(const IKvDBCommit *commit, const CommitInfo &commitInfo)
98 {
99 Version versionInfo = commit->GetCommitVersion();
100 ASSERT_EQ(versionInfo, commitInfo.version);
101 CommitID commitID = commit->GetCommitId();
102 string commitIDStr = TransCommitIDToStr(commitID);
103 ASSERT_STREQ(commitIDStr.c_str(), commitInfo.commitID.c_str());
104 CommitID leftCommitID = commit->GetLeftParentId();
105 string leftCommitIDStr = TransCommitIDToStr(leftCommitID);
106 ASSERT_STREQ(leftCommitIDStr.c_str(), commitInfo.leftCommitID.c_str());
107 CommitID rightCommitID = commit->GetRightParentId();
108 string rightCommitIDStr = TransCommitIDToStr(rightCommitID);
109 ASSERT_STREQ(rightCommitIDStr.c_str(), commitInfo.rightCommitID.c_str());
110 Timestamp timestamp = commit->GetTimestamp();
111 ASSERT_EQ(timestamp, commitInfo.timestamp);
112 bool localFlag = commit->GetLocalFlag();
113 ASSERT_EQ(localFlag, commitInfo.localFlag);
114 DeviceID deviceInfo = commit->GetDeviceInfo();
115 ASSERT_EQ(deviceInfo == commitInfo.deviceInfo, true);
116 }
117
TestLatestCommitOfDevice(const std::map<DeviceID,IKvDBCommit * > & latestCommits,const DeviceID & deviceInfo,const CommitInfo & expectCommitInfo)118 void TestLatestCommitOfDevice(const std::map<DeviceID, IKvDBCommit*> &latestCommits,
119 const DeviceID &deviceInfo, const CommitInfo &expectCommitInfo)
120 {
121 auto latestCommit = latestCommits.find(deviceInfo);
122 ASSERT_EQ(latestCommit != latestCommits.end(), true);
123 CompareCommitWithExpectation(latestCommit->second, expectCommitInfo);
124 }
125
TransStrToCommitID(const string & commitIDStr)126 CommitID TransStrToCommitID(const string &commitIDStr)
127 {
128 CommitID commitID;
129 commitID.resize(commitIDStr.size());
130 if (commitIDStr.size() != 0) {
131 commitID.assign(commitIDStr.begin(), commitIDStr.end());
132 }
133 return commitID;
134 }
135
InsertCommitToCommitStorage(const CommitInfo & commitInfo,int expectedResult)136 void InsertCommitToCommitStorage(const CommitInfo &commitInfo, int expectedResult)
137 {
138 int result;
139 IKvDBCommit *commit = g_commitStorage->AllocCommit(result);
140 ASSERT_EQ(result, E_OK);
141 ASSERT_NE(commit, nullptr);
142 if (result != E_OK || commit == nullptr) {
143 return;
144 }
145 CommitID commitID = TransStrToCommitID(commitInfo.commitID);
146 CommitID leftCommitID = TransStrToCommitID(commitInfo.leftCommitID);
147 CommitID rightCommitID = TransStrToCommitID(commitInfo.rightCommitID);
148 commit->SetCommitVersion(commitInfo.version);
149 commit->SetLeftParentId(leftCommitID);
150 commit->SetRightParentId(rightCommitID);
151 commit->SetCommitId(commitID);
152 commit->SetTimestamp(commitInfo.timestamp);
153 commit->SetLocalFlag(commitInfo.localFlag);
154 commit->SetDeviceInfo(commitInfo.deviceInfo);
155 result = g_commitStorage->AddCommit(*commit, false);
156 ASSERT_EQ(result, expectedResult);
157 g_commitStorage->ReleaseCommit(commit);
158 commit = nullptr;
159 }
160
DeleteCommit(const string & inputCommitID,int expectedResult)161 void DeleteCommit(const string &inputCommitID, int expectedResult)
162 {
163 CommitID commitID = TransStrToCommitID(inputCommitID);
164 int result = g_commitStorage->RemoveCommit(commitID);
165 ASSERT_EQ(result, expectedResult);
166 }
167
TestCommit(const CommitInfo & commitInfo,int expectedResult)168 void TestCommit(const CommitInfo &commitInfo, int expectedResult)
169 {
170 int result;
171 CommitID inputCommitID = TransStrToCommitID(commitInfo.commitID);
172 IKvDBCommit *commit = g_commitStorage->GetCommit(inputCommitID, result);
173 ASSERT_EQ(result, expectedResult);
174 if (expectedResult == E_OK) {
175 ASSERT_NE(commit, nullptr);
176 } else {
177 ASSERT_EQ(commit, nullptr);
178 }
179 if (result != E_OK || commit == nullptr) {
180 return;
181 }
182 CompareCommitWithExpectation(commit, commitInfo);
183 g_commitStorage->ReleaseCommit(commit);
184 commit = nullptr;
185 }
186
SetCommitStorageHeader(const string & inputHeader,int expectedResult)187 void SetCommitStorageHeader(const string &inputHeader, int expectedResult)
188 {
189 CommitID header = TransStrToCommitID(inputHeader);
190 int result = g_commitStorage->SetHeader(header);
191 ASSERT_EQ(result, expectedResult);
192 }
193
TestCommitStorageHeader(const string & expectedHeader)194 void TestCommitStorageHeader(const string &expectedHeader)
195 {
196 int errCode = E_OK;
197 CommitID header = g_commitStorage->GetHeader(errCode);
198 string headerStr = TransCommitIDToStr(header);
199 ASSERT_STREQ(headerStr.c_str(), expectedHeader.c_str());
200 }
201
202 /*
203 * commit tree is as below:
204 * L A B C D
205 * 1 2 4 8 d
206 * |/|/| |
207 * 3 / | 9
208 * |X| |/|
209 * 5 6 a e
210 * |/|/
211 * 7 b
212 * |/
213 * c
214 */
PrepareCommitTree()215 void PrepareCommitTree()
216 {
217 CommitInfo commitInfo1 = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
218 TIME_STAMP1, true, g_localDevice};
219 CommitInfo commitInfo2 = {g_defaultCommitVer2, g_defaultCommitID2, g_defaultCommitID0, g_defaultCommitID0,
220 TIME_STAMP2, false, g_remoteDeviceA};
221 CommitInfo commitInfo3 = {g_defaultCommitVer3, g_defaultCommitID3, g_defaultCommitID1, g_defaultCommitID2,
222 TIME_STAMP3, true, g_localDevice};
223 CommitInfo commitInfo4 = {g_defaultCommitVer4, g_defaultCommitID4, g_defaultCommitID0, g_defaultCommitID0,
224 TIME_STAMP4, false, g_remoteDeviceB};
225 CommitInfo commitInfo5 = {g_defaultCommitVer5, g_defaultCommitID5, g_defaultCommitID3, g_defaultCommitID4,
226 TIME_STAMP5, true, g_localDevice};
227 CommitInfo commitInfo6 = {g_defaultCommitVer6, g_defaultCommitID6, g_defaultCommitID2, g_defaultCommitID3,
228 TIME_STAMP6, false, g_remoteDeviceA};
229 CommitInfo commitInfo7 = {g_defaultCommitVer7, g_defaultCommitID7, g_defaultCommitID5, g_defaultCommitID6,
230 TIME_STAMP7, true, g_localDevice};
231 CommitInfo commitInfo8 = {g_defaultCommitVer8, g_defaultCommitID8, g_defaultCommitID0, g_defaultCommitID0,
232 TIME_STAMP8, false, g_remoteDeviceC};
233 CommitInfo commitInfo9 = {g_defaultCommitVer9, g_defaultCommitID9, g_defaultCommitID8, g_defaultCommitID0,
234 TIME_STAMP9, false, g_remoteDeviceC};
235 CommitInfo commitInfo10 = {g_defaultCommitVer10, g_defaultCommitID10, g_defaultCommitID4, g_defaultCommitID9,
236 TIME_STAMP10, false, g_remoteDeviceB};
237 CommitInfo commitInfo11 = {g_defaultCommitVer11, g_defaultCommitID11, g_defaultCommitID6, g_defaultCommitID10,
238 TIME_STAMP11, false, g_remoteDeviceA};
239 CommitInfo commitInfo12 = {g_defaultCommitVer12, g_defaultCommitID12, g_defaultCommitID7, g_defaultCommitID11,
240 TIME_STAMP12, true, g_localDevice};
241 InsertCommitToCommitStorage(commitInfo1, E_OK);
242 InsertCommitToCommitStorage(commitInfo2, E_OK);
243 InsertCommitToCommitStorage(commitInfo3, E_OK);
244 InsertCommitToCommitStorage(commitInfo4, E_OK);
245 InsertCommitToCommitStorage(commitInfo5, E_OK);
246 InsertCommitToCommitStorage(commitInfo6, E_OK);
247 InsertCommitToCommitStorage(commitInfo7, E_OK);
248 InsertCommitToCommitStorage(commitInfo8, E_OK);
249 InsertCommitToCommitStorage(commitInfo9, E_OK);
250 InsertCommitToCommitStorage(commitInfo10, E_OK);
251 InsertCommitToCommitStorage(commitInfo11, E_OK);
252 InsertCommitToCommitStorage(commitInfo12, E_OK);
253 SetCommitStorageHeader(g_defaultCommitID12, E_OK);
254 }
255 }
256
257 class DistributedDBStorageCommitStorageTest : public testing::Test {
258 public:
259 static void SetUpTestCase(void);
260 static void TearDownTestCase(void);
261 void SetUp();
262 void TearDown();
263 };
264
SetUpTestCase(void)265 void DistributedDBStorageCommitStorageTest::SetUpTestCase(void)
266 {
267 DistributedDBToolsUnitTest::TestDirInit(g_prop.path);
268 g_prop.isNeedCreate = true;
269 if (IKvDBFactory::GetCurrent() == nullptr) {
270 IKvDBFactory *factory = new (std::nothrow) DefaultFactory();
271 ASSERT_NE(factory, nullptr);
272 if (factory == nullptr) {
273 LOGE("failed to new DefaultFactory!");
274 return;
275 }
276 IKvDBFactory::Register(factory);
277 g_createFactory = true;
278 }
279 }
280
TearDownTestCase(void)281 void DistributedDBStorageCommitStorageTest::TearDownTestCase(void)
282 {
283 if (g_createFactory) {
284 if (IKvDBFactory::GetCurrent() != nullptr) {
285 delete IKvDBFactory::GetCurrent();
286 IKvDBFactory::Register(nullptr);
287 }
288 }
289 if (DistributedDBToolsUnitTest::RemoveTestDbFiles(g_prop.path) != 0) {
290 LOGE("rm test db files error!");
291 }
292 }
293
SetUp(void)294 void DistributedDBStorageCommitStorageTest::SetUp(void)
295 {
296 DistributedDBToolsUnitTest::PrintTestCaseInfo();
297 IKvDBFactory *factory = IKvDBFactory::GetCurrent();
298 ASSERT_NE(factory, nullptr);
299 if (factory == nullptr) {
300 LOGE("failed to get DefaultFactory!");
301 return;
302 }
303 int result;
304 g_commitStorage = factory->CreateMultiVerCommitStorage(result);
305 ASSERT_EQ(result, E_OK);
306 ASSERT_NE(g_commitStorage, nullptr);
307 if (g_commitStorage == nullptr) {
308 return;
309 }
310
311 int errCode = g_commitStorage->Open(g_prop);
312 ASSERT_EQ(errCode, E_OK);
313 if (errCode != E_OK) {
314 delete g_commitStorage;
315 g_commitStorage = nullptr;
316 return;
317 }
318 }
319
TearDown(void)320 void DistributedDBStorageCommitStorageTest::TearDown(void)
321 {
322 if (g_commitStorage != nullptr) {
323 (void)g_commitStorage->Remove(g_prop);
324 delete g_commitStorage;
325 g_commitStorage = nullptr;
326 }
327 }
328
329 /**
330 * @tc.name: MultiVerCommitStorage001
331 * @tc.desc: Open a commit storage when it has been opened.
332 * @tc.type: FUNC
333 * @tc.require:
334 * @tc.author: liujialei
335 */
336 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage001, TestSize.Level1)
337 {
338 /**
339 * @tc.steps:step1/2. Open commit log database
340 * @tc.expected: step1/2. Return OK.
341 */
342 int result = g_commitStorage->Open(g_prop);
343 ASSERT_EQ(result, E_OK);
344 return;
345 }
346
347 /**
348 * @tc.name: MultiVerCommitStorage002
349 * @tc.desc: Remove a commit storage database, then try to add, delete and query commit, set and get header.
350 * @tc.type: FUNC
351 * @tc.require:
352 * @tc.author: liujialei
353 */
354 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage002, TestSize.Level1)
355 {
356 /**
357 * @tc.steps:step1/2. Remove commit log database
358 * @tc.expected: step1/2. Return OK.
359 */
360 int result = g_commitStorage->Remove(g_prop);
361 ASSERT_EQ(result, E_OK);
362 CommitInfo commitInfo = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
363 TIME_STAMP1, true, g_localDevice};
364
365 /**
366 * @tc.steps:step3/4. Insert recording commit log database
367 * @tc.expected: step3/4. Return E_INVALID_DB.
368 */
369 InsertCommitToCommitStorage(commitInfo, -E_INVALID_DB);
370
371 /**
372 * @tc.steps:step5/6. Delete commit History to commit log database
373 * @tc.expected: step5/6. Return E_INVALID_DB.
374 */
375 DeleteCommit(g_defaultCommitID1, -E_INVALID_DB);
376
377 /**
378 * @tc.steps:step7/8. Cheeck commit History to commit log database
379 * @tc.expected: step7/8. Return E_INVALID_DB.
380 */
381 TestCommit(commitInfo, -E_INVALID_DB);
382
383 /**
384 * @tc.steps:step9/10. Cheeck change commit header
385 * @tc.expected: step7/10. Return E_INVALID_DB.
386 */
387 SetCommitStorageHeader(g_defaultCommitID1, -E_INVALID_DB);
388
389 /**
390 * @tc.steps:step11/12. Cheeck query commit header
391 * @tc.expected: step11/12. Return failed.
392 */
393 TestCommitStorageHeader(g_defaultCommitID0);
394 }
395
396 /**
397 * @tc.name: MultiVerCommitStorage003
398 * @tc.desc: Insert a commit to commit storage, and get it.
399 * @tc.type: FUNC
400 * @tc.require:
401 * @tc.author: liujialei
402 */
403 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage003, TestSize.Level1)
404 {
405 CommitInfo commitInfo = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
406 TIME_STAMP1, true, g_localDevice};
407
408 /**
409 * @tc.steps:step1. Insert recording commit log database
410 * @tc.expected: step1. Return E_OK.
411 */
412 InsertCommitToCommitStorage(commitInfo, E_OK);
413
414 /**
415 * @tc.steps:step2. Cheeck commit History to commit log database
416 * @tc.expected: step2. Return E_OK.
417 */
418 TestCommit(commitInfo, E_OK);
419 return;
420 }
421
422 /**
423 * @tc.name: MultiVerCommitStorage004
424 * @tc.desc: Set header of commit storage, and get it.
425 * @tc.type: FUNC
426 * @tc.require:
427 * @tc.author: liujialei
428 */
429 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage004, TestSize.Level1)
430 {
431 CommitInfo commitInfo = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
432 TIME_STAMP1, true, g_localDevice};
433
434 /**
435 * @tc.steps:step1. Insert recording commit log database
436 * @tc.expected: step1. Return E_OK.
437 */
438 InsertCommitToCommitStorage(commitInfo, E_OK);
439
440 /**
441 * @tc.steps:step2. Cheeck change commit header
442 * @tc.expected: step2. Return E_OK.
443 */
444 SetCommitStorageHeader(g_defaultCommitID1, E_OK);
445
446 /**
447 * @tc.steps:step3. Cheeck query commit header
448 * @tc.expected: step3. Return success.
449 */
450 TestCommitStorageHeader(g_defaultCommitID1);
451 return;
452 }
453
454 /**
455 * @tc.name: MultiVerCommitStorage005
456 * @tc.desc: Delete the header commit, test if it can be get, and get the new header.
457 * @tc.type: FUNC
458 * @tc.require:
459 * @tc.author: liujialei
460 */
461 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage005, TestSize.Level1)
462 {
463 CommitInfo commitInfo1 = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
464 TIME_STAMP1, true, g_localDevice};
465
466 /**
467 * @tc.steps:step1. Insert recording commitInfo1 commit log database
468 * @tc.expected: step1. Return E_OK.
469 */
470 InsertCommitToCommitStorage(commitInfo1, E_OK);
471 CommitInfo commitInfo2 = {g_defaultCommitVer2, g_defaultCommitID2, g_defaultCommitID1, g_defaultCommitID0,
472 TIME_STAMP2, true, g_localDevice};
473
474 /**
475 * @tc.steps:step2. Insert recording commitInfo2 commit log database
476 * @tc.expected: step2. Return E_OK.
477 */
478 InsertCommitToCommitStorage(commitInfo2, E_OK);
479
480 /**
481 * @tc.steps:step3. Cheeck change commit header
482 * @tc.expected: step3. Return E_OK.
483 */
484 SetCommitStorageHeader(g_defaultCommitID2, E_OK);
485
486 /**
487 * @tc.steps:step4. Delete commit History to commit log database
488 * @tc.expected: step4. Return E_INVALID_DB.
489 */
490 DeleteCommit(g_defaultCommitID2, E_OK);
491
492 /**
493 * @tc.steps:step5. Cheeck query commit header
494 * @tc.expected: step5. Return success.
495 */
496 TestCommitStorageHeader(g_defaultCommitID1);
497
498 /**
499 * @tc.steps:step6. Cheeck commit History to commit log database
500 * @tc.expected: step6. Return E_OK.
501 */
502 TestCommit(commitInfo1, E_OK);
503
504 /**
505 * @tc.steps:step7. Cheeck commit History to commit log database
506 * @tc.expected: step7. Return E_OK.
507 */
508 TestCommit(commitInfo2, -E_NOT_FOUND);
509 return;
510 }
511
512 /**
513 * @tc.name: MultiVerCommitStorage006
514 * @tc.desc: Add commit with empty commit ID, and it will not be added.
515 * @tc.type: FUNC
516 * @tc.require:
517 * @tc.author: liujialei
518 */
519 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage006, TestSize.Level1)
520 {
521 CommitInfo commitInfo = {g_defaultCommitVer1, g_defaultCommitID0, g_defaultCommitID0, g_defaultCommitID0,
522 TIME_STAMP1, true, g_localDevice};
523
524 /**
525 * @tc.steps:step1/2. Insert commit ID is null to commit log database
526 * @tc.expected: step1/2. Return E_UNEXPECTED_DATA.
527 */
528 InsertCommitToCommitStorage(commitInfo, -E_UNEXPECTED_DATA);
529 }
530
531 /**
532 * @tc.name: MultiVerCommitStorage008
533 * @tc.desc: Add commit with the same commit ID as its left parent, and it will not be added.
534 * @tc.type: FUNC
535 * @tc.require:
536 * @tc.author: liujialei
537 */
538 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage008, TestSize.Level1)
539 {
540 CommitInfo commitInfo1 = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
541 TIME_STAMP1, true, g_localDevice};
542
543 /**
544 * @tc.steps:step1. Insert a recording to commit log database
545 * @tc.expected: step1. Return E_OK.
546 */
547 InsertCommitToCommitStorage(commitInfo1, E_OK);
548 CommitInfo commitInfo2 = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID1, g_defaultCommitID0,
549 TIME_STAMP2, true, g_localDevice};
550
551 /**
552 * @tc.steps:step2. Add commit with the same commit ID as its right parent
553 * @tc.expected: step2. Return E_UNEXPECTED_DATA.
554 */
555 InsertCommitToCommitStorage(commitInfo2, -E_UNEXPECTED_DATA);
556
557 /**
558 * @tc.steps:step3. Cheeck commit History to commit log database
559 * @tc.expected: step3. Return E_OK.
560 */
561 TestCommit(commitInfo1, E_OK);
562 }
563
564 /**
565 * @tc.name: MultiVerCommitStorage009
566 * @tc.desc: Add commit with the same commit ID as its right parent, and it will not be added.
567 * @tc.type: FUNC
568 * @tc.require:
569 * @tc.author: liujialei
570 */
571 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage009, TestSize.Level1)
572 {
573 CommitInfo commitInfo1 = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
574 TIME_STAMP1, true, g_localDevice};
575
576 /**
577 * @tc.steps:step1. Insert a recording to commit log database
578 * @tc.expected: step1. Return E_OK.
579 */
580 InsertCommitToCommitStorage(commitInfo1, E_OK);
581 CommitInfo commitInfo2 = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID1,
582 TIME_STAMP2, true, g_localDevice};
583
584 /**
585 * @tc.steps:step2. Add commit with the same commit ID as its right parent
586 * @tc.expected: step2. Return E_UNEXPECTED_DATA.
587 */
588 InsertCommitToCommitStorage(commitInfo2, -E_UNEXPECTED_DATA);
589
590 /**
591 * @tc.steps:step3. Cheeck commit History to commit log database
592 * @tc.expected: step3. Return E_OK.
593 */
594 TestCommit(commitInfo1, E_OK);
595 }
596
597 /**
598 * @tc.name: MultiVerCommitStorage010
599 * @tc.desc: Add commit whose left parent and right parent is the same, and it will not be added.
600 * @tc.type: FUNC
601 * @tc.require:
602 * @tc.author: liujialei
603 */
604 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage010, TestSize.Level1)
605 {
606 CommitInfo commitInfo1 = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
607 TIME_STAMP1, true, g_localDevice};
608
609 /**
610 * @tc.steps:step1. Insert a recording to commit log database
611 * @tc.expected: step1. Return E_OK.
612 */
613 InsertCommitToCommitStorage(commitInfo1, E_OK);
614 CommitInfo commitInfo2 = {g_defaultCommitVer1, g_defaultCommitID2, g_defaultCommitID1, g_defaultCommitID1,
615 TIME_STAMP2, true, g_localDevice};
616
617 /**
618 * @tc.steps:step2. Add commit whose left parent and right parent is the same
619 * @tc.expected: step2. Return E_UNEXPECTED_DATA.
620 */
621 InsertCommitToCommitStorage(commitInfo2, -E_UNEXPECTED_DATA);
622
623 /**
624 * @tc.steps:step3. Cheeck commit History of commitInfo1 to commit log database
625 * @tc.expected: step3. Return E_OK.
626 */
627 TestCommit(commitInfo1, E_OK);
628
629 /**
630 * @tc.steps:step3. Cheeck commit History of commitInfo2 to commit log database
631 * @tc.expected: step3. Return E_NOT_FOUND.
632 */
633 TestCommit(commitInfo2, -E_NOT_FOUND);
634 }
635
636 /**
637 * @tc.name: MultiVerCommitStorage011
638 * @tc.desc: Add commit with a non exist left parent, and it will not be added.
639 * @tc.type: FUNC
640 * @tc.require:
641 * @tc.author: liujialei
642 */
643 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage011, TestSize.Level1)
644 {
645 CommitInfo commitInfo = {g_defaultCommitVer1, g_defaultCommitID2, g_defaultCommitID1, g_defaultCommitID0,
646 TIME_STAMP1, true, g_localDevice};
647
648 /**
649 * @tc.steps:step1. Add commit with a non exist left parent
650 * @tc.expected: step1. Return E_NOT_FOUND.
651 */
652 InsertCommitToCommitStorage(commitInfo, -E_NOT_FOUND);
653
654 /**
655 * @tc.steps:step2. Cheeck commit History to commit log database
656 * @tc.expected: step2. Return E_NOT_FOUND.
657 */
658 TestCommit(commitInfo, -E_NOT_FOUND);
659 }
660
661 /**
662 * @tc.name: MultiVerCommitStorage012
663 * @tc.desc: Add commit with a non exist right parent, and it will not be added.
664 * @tc.type: FUNC
665 * @tc.require:
666 * @tc.author: liujialei
667 */
668 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage012, TestSize.Level1)
669 {
670 CommitInfo commitInfo = {g_defaultCommitVer1, g_defaultCommitID2, g_defaultCommitID0, g_defaultCommitID1,
671 TIME_STAMP1, true, g_localDevice};
672
673 /**
674 * @tc.steps:step1. Add commit with a non exist right parent
675 * @tc.expected: step1. Return E_NOT_FOUND.
676 */
677 InsertCommitToCommitStorage(commitInfo, -E_NOT_FOUND);
678
679 /**
680 * @tc.steps:step2. Cheeck commit History to commit log database
681 * @tc.expected: step2. Return E_NOT_FOUND.
682 */
683 TestCommit(commitInfo, -E_NOT_FOUND);
684 }
685
686 /**
687 * @tc.name: MultiVerCommitStorage013
688 * @tc.desc: Delete a commit which is not header, and it will not be deleted.
689 * @tc.type: FUNC
690 * @tc.require:
691 * @tc.author: liujialei
692 */
693 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage013, TestSize.Level1)
694 {
695 CommitInfo commitInfo1 = {g_defaultCommitVer1, g_defaultCommitID1, g_defaultCommitID0, g_defaultCommitID0,
696 TIME_STAMP1, true, g_localDevice};
697
698 /**
699 * @tc.steps:step1. Insert a recording to commit log database
700 */
701 InsertCommitToCommitStorage(commitInfo1, E_OK);
702 CommitInfo commitInfo2 = {g_defaultCommitVer2, g_defaultCommitID2, g_defaultCommitID1, g_defaultCommitID0,
703 TIME_STAMP2, true, g_localDevice};
704
705 /**
706 * @tc.steps:step2. Insert a left parent to commit log database
707 */
708 InsertCommitToCommitStorage(commitInfo2, E_OK);
709
710 /**
711 * @tc.steps:step3. Set g_defaultCommitID2 is parent to commit log database
712 */
713 SetCommitStorageHeader(g_defaultCommitID2, E_OK);
714
715 /**
716 * @tc.steps:step4. Delete g_defaultCommitID1
717 * @tc.expected: step4. Return E_UNEXPECTED_DATA.
718 */
719 DeleteCommit(g_defaultCommitID1, -E_UNEXPECTED_DATA);
720
721 /**
722 * @tc.steps:step5. Cheeck commit header is same as g_defaultCommitID2
723 */
724 TestCommitStorageHeader(g_defaultCommitID2);
725
726 TestCommit(commitInfo1, E_OK);
727 TestCommit(commitInfo2, E_OK);
728 return;
729 }
730
731 /**
732 * @tc.name: MultiVerCommitStorage014
733 * @tc.desc: Set unexist commit to header, and it will not success.
734 * @tc.type: FUNC
735 * @tc.require:
736 * @tc.author: liujialei
737 */
738 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage014, TestSize.Level1)
739 {
740 SetCommitStorageHeader(g_defaultCommitID2, -E_NOT_FOUND);
741 TestCommitStorageHeader(g_defaultCommitID0);
742 }
743
744 /**
745 * @tc.name: MultiVerCommitStorage015
746 * @tc.desc: SDetermine whether commit exists
747 * @tc.type: FUNC
748 * @tc.require:
749 * @tc.author: liujialei
750 */
751 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage015, TestSize.Level1)
752 {
753 PrepareCommitTree();
754 int errCode = E_OK;
755 EXPECT_EQ(g_commitStorage->CommitExist(TransStrToCommitID(g_defaultCommitID7), errCode), true);
756 EXPECT_EQ(g_commitStorage->CommitExist(TransStrToCommitID(g_defaultCommitID13), errCode), false);
757 }
758
759 /**
760 * @tc.name: MultiVerCommitStorage016
761 * @tc.desc: Get latest commit of each device from commit storage
762 * @tc.type: FUNC
763 * @tc.require:
764 * @tc.author: liujialei
765 */
766 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage016, TestSize.Level1)
767 {
768 PrepareCommitTree();
769 std::map<DeviceID, IKvDBCommit*> latestCommits;
770 int result = g_commitStorage->GetLatestCommits(latestCommits);
771 EXPECT_EQ(result, E_OK);
772 CommitInfo commitInfoLocal = {g_defaultCommitVer12, g_defaultCommitID12, g_defaultCommitID7,
773 g_defaultCommitID11, TIME_STAMP12, true, g_localDevice};
774 CommitInfo commitInfoA = {g_defaultCommitVer11, g_defaultCommitID11, g_defaultCommitID6,
775 g_defaultCommitID10, TIME_STAMP11, false, g_remoteDeviceA};
776 CommitInfo commitInfoB = {g_defaultCommitVer10, g_defaultCommitID10, g_defaultCommitID4,
777 g_defaultCommitID9, TIME_STAMP10, false, g_remoteDeviceB};
778 CommitInfo commitInfoC = {g_defaultCommitVer9, g_defaultCommitID9, g_defaultCommitID8,
779 g_defaultCommitID0, TIME_STAMP9, false, g_remoteDeviceC};
780 TestLatestCommitOfDevice(latestCommits, g_localDevice, commitInfoLocal);
781 TestLatestCommitOfDevice(latestCommits, g_remoteDeviceA, commitInfoA);
782 TestLatestCommitOfDevice(latestCommits, g_remoteDeviceB, commitInfoB);
783 TestLatestCommitOfDevice(latestCommits, g_remoteDeviceC, commitInfoC);
784 for (auto latestCommit : latestCommits) {
785 g_commitStorage->ReleaseCommit(latestCommit.second);
786 }
787 }
788
789 /**
790 * @tc.name: MultiVerCommitStorage017
791 * @tc.desc: Get commit tree from commit storage by latest commits
792 * @tc.type: FUNC
793 * @tc.require:
794 * @tc.author: liujialei
795 */
796 HWTEST_F(DistributedDBStorageCommitStorageTest, MultiVerCommitStorage017, TestSize.Level1)
797 {
798 PrepareCommitTree();
799 map<DeviceID, CommitID> latestCommits;
800 latestCommits.insert(make_pair(g_localDevice, TransStrToCommitID(g_defaultCommitID3)));
801 latestCommits.insert(make_pair(g_remoteDeviceA, TransStrToCommitID(g_defaultCommitID2)));
802 latestCommits.insert(make_pair(g_remoteDeviceC, TransStrToCommitID(g_defaultCommitID14)));
803 latestCommits.insert(make_pair(g_remoteDeviceD, TransStrToCommitID(g_defaultCommitID13)));
804 list<IKvDBCommit *> commits;
805 int result = g_commitStorage->GetCommitTree(latestCommits, commits);
806 EXPECT_EQ(result, E_OK);
807 vector<IKvDBCommit *> commitsVector(commits.begin(), commits.end());
808 LOGD("Commits.size%zu", commits.size());
809 ASSERT_GT(commits.size(), 6UL);
810 CommitInfo commitInfo4 = {g_defaultCommitVer4, g_defaultCommitID4, g_defaultCommitID0, g_defaultCommitID0,
811 TIME_STAMP4, false, g_remoteDeviceB};
812 CommitInfo commitInfo5 = {g_defaultCommitVer5, g_defaultCommitID5, g_defaultCommitID3, g_defaultCommitID4,
813 TIME_STAMP5, true, g_localDevice};
814 CommitInfo commitInfo6 = {g_defaultCommitVer6, g_defaultCommitID6, g_defaultCommitID2, g_defaultCommitID3,
815 TIME_STAMP6, false, g_remoteDeviceA};
816 CommitInfo commitInfo7 = {g_defaultCommitVer7, g_defaultCommitID7, g_defaultCommitID5, g_defaultCommitID6,
817 TIME_STAMP7, true, g_localDevice};
818 CommitInfo commitInfo10 = {g_defaultCommitVer10, g_defaultCommitID10, g_defaultCommitID4, g_defaultCommitID9,
819 TIME_STAMP10, false, g_remoteDeviceB};
820 CommitInfo commitInfo11 = {g_defaultCommitVer11, g_defaultCommitID11, g_defaultCommitID6, g_defaultCommitID10,
821 TIME_STAMP11, false, g_remoteDeviceA};
822 CommitInfo commitInfo12 = {g_defaultCommitVer12, g_defaultCommitID12, g_defaultCommitID7, g_defaultCommitID11,
823 TIME_STAMP12, true, g_localDevice};
824 CompareCommitWithExpectation(commitsVector[0], commitInfo4);
825 CompareCommitWithExpectation(commitsVector[1], commitInfo5);
826 CompareCommitWithExpectation(commitsVector[2], commitInfo6);
827 CompareCommitWithExpectation(commitsVector[3], commitInfo7);
828 CompareCommitWithExpectation(commitsVector[4], commitInfo10);
829 CompareCommitWithExpectation(commitsVector[5], commitInfo11);
830 CompareCommitWithExpectation(commitsVector[6], commitInfo12);
831 for (auto commit : commits) {
832 g_commitStorage->ReleaseCommit(commit);
833 commit = nullptr;
834 }
835 }
836 #endif // OMIT_MULTI_VER
837