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 "kv_general_ut.h"
17
18 namespace DistributedDB {
19 using namespace testing::ext;
20
21 class DistributedDBAbnormalKVSyncTest : public KVGeneralUt {
22 public:
23 void SetUp() override;
24 };
25
SetUp()26 void DistributedDBAbnormalKVSyncTest::SetUp()
27 {
28 KVGeneralUt::SetUp();
29 auto storeInfo1 = GetStoreInfo1();
30 ASSERT_EQ(BasicUnitTest::InitDelegate(storeInfo1, "dev1"), E_OK);
31 auto storeInfo2 = GetStoreInfo2();
32 ASSERT_EQ(BasicUnitTest::InitDelegate(storeInfo2, "dev2"), E_OK);
33 }
34
35 /**
36 * @tc.name: SyncWithInvalidSoftwareVersionTest001
37 * @tc.desc: Test ability sync after software version is invalid.
38 * @tc.type: FUNC
39 * @tc.author: zqq
40 */
41 HWTEST_F(DistributedDBAbnormalKVSyncTest, SyncWithInvalidSoftwareVersionTest001, TestSize.Level0)
42 {
43 /**
44 * @tc.steps: step1. open store and sync first
45 * @tc.expected: step1. sync ok.
46 */
47 auto storeInfo1 = GetStoreInfo1();
48 auto storeInfo2 = GetStoreInfo2();
49 BlockPush(storeInfo1, storeInfo2);
50 /**
51 * @tc.steps: step2. set version invalid and sync again after reopen store
52 * @tc.expected: step2. sync ok.
53 */
54 ASSERT_EQ(SetRemoteSoftwareVersion(storeInfo1, "dev2", DBConstant::DEFAULT_USER, INT32_MAX), OK);
55 KVGeneralUt::CloseDelegate(storeInfo1);
56 ASSERT_EQ(BasicUnitTest::InitDelegate(storeInfo1, "dev1"), E_OK);
57 std::atomic<int> msgCount = 0;
__anon968fe2a10102(const std::string &dev, const Message *inMsg) 58 RegBeforeDispatch([&msgCount](const std::string &dev, const Message *inMsg) {
59 if (dev != "dev2" || inMsg->GetMessageId() != ABILITY_SYNC_MESSAGE) {
60 return;
61 }
62 msgCount++;
63 });
64 BlockPush(storeInfo1, storeInfo2);
65 RegBeforeDispatch(nullptr);
66 EXPECT_GT(msgCount, 0);
67 }
68 }