1 /*
2 * Copyright (c) 2022 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 "store_util.h"
17
18 #include <gtest/gtest.h>
19 #include <vector>
20
21 #include "store_manager.h"
22 #include "types.h"
23 namespace OHOS::Test {
24 using namespace testing::ext;
25 using namespace OHOS::DistributedKv;
26
27 class StoreUtilTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30 static void TearDownTestCase(void);
31
32 void SetUp();
33 void TearDown();
34 };
35
SetUpTestCase(void)36 void StoreUtilTest::SetUpTestCase(void) {}
37
TearDownTestCase(void)38 void StoreUtilTest::TearDownTestCase(void) {}
39
SetUp(void)40 void StoreUtilTest::SetUp(void) {}
41
TearDown(void)42 void StoreUtilTest::TearDown(void) {}
43 /**
44 * @tc.name: GetDBSecurity
45 * @tc.desc: get db security
46 * @tc.type: FUNC
47 * @tc.require:
48 * @tc.author: Wang Kai
49 */
50 HWTEST_F(StoreUtilTest, GetDBSecurity, TestSize.Level1)
51 {
52 StoreUtil storeUtil_;
53 auto dbsecurity = storeUtil_.GetDBSecurity(-1);
54 ASSERT_EQ(dbsecurity.securityLabel, DistributedDB::NOT_SET);
55 ASSERT_EQ(dbsecurity.securityFlag, DistributedDB::ECE);
56
57 dbsecurity = storeUtil_.GetDBSecurity(7);
58 ASSERT_EQ(dbsecurity.securityLabel, DistributedDB::NOT_SET);
59 ASSERT_EQ(dbsecurity.securityFlag, DistributedDB::ECE);
60
61 dbsecurity = storeUtil_.GetDBSecurity(5);
62 ASSERT_EQ(dbsecurity.securityLabel, DistributedDB::S3);
63 ASSERT_EQ(dbsecurity.securityFlag, DistributedDB::SECE);
64
65 dbsecurity = storeUtil_.GetDBSecurity(6);
66 ASSERT_EQ(dbsecurity.securityLabel, DistributedDB::S4);
67 ASSERT_EQ(dbsecurity.securityFlag, DistributedDB::ECE);
68 }
69 /**
70 * @tc.name: GetSecLevel
71 * @tc.desc: get secLevel
72 * @tc.type: FUNC
73 * @tc.require:
74 * @tc.author: Wang Kai
75 */
76 HWTEST_F(StoreUtilTest, GetSecLevel, TestSize.Level1)
77 {
78 StoreUtil storeUtil_;
79 StoreUtil::DBSecurity dbSec = { DistributedDB::NOT_SET, DistributedDB::ECE };
80 int32_t security = storeUtil_.GetSecLevel(dbSec);
81 ASSERT_EQ(security, dbSec.securityLabel);
82
83 dbSec = { DistributedDB::S3, DistributedDB::ECE };
84 security = storeUtil_.GetSecLevel(dbSec);
85 ASSERT_EQ(security, S3_EX);
86 dbSec = { DistributedDB::S3, DistributedDB::SECE };
87 security = storeUtil_.GetSecLevel(dbSec);
88 ASSERT_EQ(security, S3);
89
90 dbSec = { DistributedDB::S4, DistributedDB::ECE };
91 security = storeUtil_.GetSecLevel(dbSec);
92 ASSERT_EQ(security, S4);
93 }
94 /**
95 * @tc.name: GetDBMode
96 * @tc.desc: get db mode
97 * @tc.type: FUNC
98 * @tc.require:
99 * @tc.author: Wang Kai
100 */
101 HWTEST_F(StoreUtilTest, GetDBMode, TestSize.Level1)
102 {
103 StoreUtil storeUtil_;
104 StoreUtil::DBMode dbMode = storeUtil_.GetDBMode(SyncMode::PUSH);
105 ASSERT_EQ(dbMode, StoreUtil::DBMode::SYNC_MODE_PUSH_ONLY);
106
107 dbMode = storeUtil_.GetDBMode(SyncMode::PULL);
108 ASSERT_EQ(dbMode, StoreUtil::DBMode::SYNC_MODE_PULL_ONLY);
109
110 dbMode = storeUtil_.GetDBMode(SyncMode::PUSH_PULL);
111 ASSERT_EQ(dbMode, StoreUtil::DBMode::SYNC_MODE_PUSH_PULL);
112 }
113 /**
114 * @tc.name: GetObserverMode
115 * @tc.desc: get observer mode
116 * @tc.type: FUNC
117 * @tc.require:
118 * @tc.author: Wang Kai
119 */
120 HWTEST_F(StoreUtilTest, GetObserverMode, TestSize.Level1)
121 {
122 StoreUtil storeUtil_;
123 uint32_t mode = storeUtil_.GetObserverMode(SubscribeType::SUBSCRIBE_TYPE_LOCAL);
124 ASSERT_EQ(mode, DistributedDB::OBSERVER_CHANGES_NATIVE);
125
126 mode = storeUtil_.GetObserverMode(SubscribeType::SUBSCRIBE_TYPE_REMOTE);
127 ASSERT_EQ(mode, DistributedDB::OBSERVER_CHANGES_FOREIGN);
128
129 mode = storeUtil_.GetObserverMode(SUBSCRIBE_TYPE_ALL);
130 ASSERT_EQ(mode, DistributedDB::OBSERVER_CHANGES_FOREIGN | DistributedDB::OBSERVER_CHANGES_NATIVE);
131 }
132 } // namespace OHOS::Test