• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class DistributedDBBasicKVTest : public KVGeneralUt {
21 public:
22     void SetUp() override;
23 };
24 
SetUp()25 void DistributedDBBasicKVTest::SetUp()
26 {
27     KVGeneralUt::SetUp();
28     auto storeInfo1 = GetStoreInfo1();
29     ASSERT_EQ(BasicUnitTest::InitDelegate(storeInfo1, "dev1"), E_OK);
30     auto storeInfo2 = GetStoreInfo2();
31     ASSERT_EQ(BasicUnitTest::InitDelegate(storeInfo2, "dev2"), E_OK);
32     auto storeInfo3 = GetStoreInfo3();
33     ASSERT_EQ(BasicUnitTest::InitDelegate(storeInfo3, "dev3"), E_OK);
34 }
35 
36 /**
37  * @tc.name: ExampleSync001
38  * @tc.desc: Test sync from dev1 to dev2.
39  * @tc.type: FUNC
40  * @tc.require:
41  * @tc.author: zqq
42  */
43 HWTEST_F(DistributedDBBasicKVTest, ExampleSync001, TestSize.Level0)
44 {
45     /**
46      * @tc.steps: step1. dev1 put (k,v) and sync to dev2
47      * @tc.expected: step1. sync should return OK and dev2 exist (k,v).
48      */
49     auto storeInfo1 = GetStoreInfo1();
50     auto storeInfo2 = GetStoreInfo2();
51     auto store1 = GetDelegate(storeInfo1);
52     ASSERT_NE(store1, nullptr);
53     auto store2 = GetDelegate(storeInfo2);
54     ASSERT_NE(store2, nullptr);
55     Value expectValue = {'v'};
56     EXPECT_EQ(store1->Put({'k'}, expectValue), OK);
57     BlockPush(storeInfo1, storeInfo2);
58     Value actualValue;
59     EXPECT_EQ(store2->Get({'k'}, actualValue), OK);
60     EXPECT_EQ(actualValue, expectValue);
61     /**
62      * @tc.steps: step2. dev2's schemaVersion and softwareVersion both equal to dev1's meta
63      * @tc.expected: step2. version both equal.
64      */
65     auto [errCode, version] = GetRemoteSoftwareVersion(storeInfo1, "dev2", DBConstant::DEFAULT_USER);
66     EXPECT_EQ(errCode, OK);
67     EXPECT_EQ(version, static_cast<uint64_t>(SOFTWARE_VERSION_CURRENT));
68     std::tie(errCode, version) = GetRemoteSchemaVersion(storeInfo1, "dev2", DBConstant::DEFAULT_USER);
69     EXPECT_EQ(errCode, OK);
70     EXPECT_NE(version, 0);
71     uint64_t store2SchemaVersion = 0;
72     std::tie(errCode, store2SchemaVersion) = GetLocalSchemaVersion(storeInfo2);
73     EXPECT_EQ(errCode, E_OK);
74     EXPECT_EQ(version, store2SchemaVersion);
75 }
76 
77 /**
78  * @tc.name: ExampleSync002
79  * @tc.desc: Test sync from dev1 to dev2 with 2 packet.
80  * @tc.type: FUNC
81  * @tc.author: zqq
82  */
83 HWTEST_F(DistributedDBBasicKVTest, ExampleSync002, TestSize.Level0)
84 {
85     /**
86      * @tc.steps: step1. dev1 put (k1,v1) and (k2,v2)
87      * @tc.expected: step1. put ok.
88      */
89     auto storeInfo1 = GetStoreInfo1();
90     auto storeInfo2 = GetStoreInfo2();
91     auto store1 = GetDelegate(storeInfo1);
92     ASSERT_NE(store1, nullptr);
93     auto store2 = GetDelegate(storeInfo2);
94     ASSERT_NE(store2, nullptr);
95     Key k1 = {'k', '1'};
96     Value v1 = {'v', '1'};
97     EXPECT_EQ(store1->Put(k1, v1), OK);
98     Key k2 = {'k', '2'};
99     Value v2 = {'v', '2'};
100     EXPECT_EQ(store1->Put(k2, v2), OK);
101     /**
102      * @tc.steps: step2. dev1 sync to dev2 with mtu=1
103      * @tc.expected: step2. sync ok.
104      */
105     SetMtu("dev1", 1);
106     BlockPush(storeInfo1, storeInfo2);
107 }
108 
109 /**
110  * @tc.name: WhitelistKvGet001
111  * @tc.desc: Test kv get interface for whitelist.
112  * @tc.type: FUNC
113  * @tc.require:
114  * @tc.author: xd
115  */
116 HWTEST_F(DistributedDBBasicKVTest, WhitelistKvGet001, TestSize.Level0)
117 {
118     /**
119      * @tc.steps: step1. set whitelist appId, put (k,v)
120      * @tc.expected: step1. get (k,v) result.
121      */
122     auto storeInfo3 = GetStoreInfo3();
123     auto store3 = GetDelegate(storeInfo3);
124     ASSERT_NE(store3, nullptr);
125     Value expectValue = {'v'};
126     EXPECT_EQ(store3->Put({'k'}, expectValue), OK);
127     Value actualValue;
128     EXPECT_EQ(store3->Get({'k'}, actualValue), OK);
129     EXPECT_EQ(actualValue, expectValue);
130     /**
131      * @tc.steps: step2. with transaction, set whitelist appId, put (k2,v)
132      * @tc.expected: step2. get (k2,v) result.
133      */
134     store3->StartTransaction();
135     EXPECT_EQ(store3->Put({'k', '2'}, expectValue), OK);
136     Value actualValue2;
137     EXPECT_EQ(store3->Get({'k', '2'}, actualValue2), OK);
138     EXPECT_EQ(actualValue2, expectValue);
139     store3->Commit();
140     /**
141      * @tc.steps: step3. do not set whitelist appId, put (k,v)
142      * @tc.expected: step3. get (k,v) result.
143      */
144     auto storeInfo2 = GetStoreInfo2();
145     auto store2 = GetDelegate(storeInfo2);
146     ASSERT_NE(store2, nullptr);
147     EXPECT_EQ(store2->Put({'k'}, expectValue), OK);
148     Value actualValue3;
149     EXPECT_EQ(store2->Get({'k'}, actualValue3), OK);
150     EXPECT_EQ(actualValue3, expectValue);
151 }
152 } // namespace DistributedDB