• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <fcntl.h>
19 #include <gtest/gtest.h>
20 #include <sys/stat.h>
21 #include <vector>
22 
23 #include "store_manager.h"
24 #include "types.h"
25 namespace OHOS::Test {
26 using namespace testing::ext;
27 using namespace OHOS::DistributedKv;
28 
29 class StoreUtilTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33 
34     void SetUp();
35     void TearDown();
36 };
37 
SetUpTestCase(void)38 void StoreUtilTest::SetUpTestCase(void) { }
39 
TearDownTestCase(void)40 void StoreUtilTest::TearDownTestCase(void) { }
41 
SetUp(void)42 void StoreUtilTest::SetUp(void) { }
43 
TearDown(void)44 void StoreUtilTest::TearDown(void) { }
45 /**
46  * @tc.name: GetDBSecurity
47  * @tc.desc: get db security
48  * @tc.type: FUNC
49  * @tc.require:
50  * @tc.author: Wang Kai
51  */
52 HWTEST_F(StoreUtilTest, GetDBSecurity, TestSize.Level1)
53 {
54     StoreUtil storeUtil_;
55     auto dbsecurity = storeUtil_.GetDBSecurity(-1);
56     ASSERT_EQ(dbsecurity.securityLabel, DistributedDB::NOT_SET);
57     ASSERT_EQ(dbsecurity.securityFlag, DistributedDB::ECE);
58 
59     dbsecurity = storeUtil_.GetDBSecurity(7);
60     ASSERT_EQ(dbsecurity.securityLabel, DistributedDB::NOT_SET);
61     ASSERT_EQ(dbsecurity.securityFlag, DistributedDB::ECE);
62 
63     dbsecurity = storeUtil_.GetDBSecurity(5);
64     ASSERT_EQ(dbsecurity.securityLabel, DistributedDB::S3);
65     ASSERT_EQ(dbsecurity.securityFlag, DistributedDB::SECE);
66 
67     dbsecurity = storeUtil_.GetDBSecurity(6);
68     ASSERT_EQ(dbsecurity.securityLabel, DistributedDB::S4);
69     ASSERT_EQ(dbsecurity.securityFlag, DistributedDB::ECE);
70 }
71 /**
72  * @tc.name: GetSecLevel
73  * @tc.desc: get secLevel
74  * @tc.type: FUNC
75  * @tc.require:
76  * @tc.author: Wang Kai
77  */
78 HWTEST_F(StoreUtilTest, GetSecLevel, TestSize.Level1)
79 {
80     StoreUtil storeUtil_;
81     StoreUtil::DBSecurity dbSec = { DistributedDB::NOT_SET, DistributedDB::ECE };
82     int32_t security = storeUtil_.GetSecLevel(dbSec);
83     ASSERT_EQ(security, dbSec.securityLabel);
84 
85     dbSec = { DistributedDB::S3, DistributedDB::ECE };
86     security = storeUtil_.GetSecLevel(dbSec);
87     ASSERT_EQ(security, S3_EX);
88     dbSec = { DistributedDB::S3, DistributedDB::SECE };
89     security = storeUtil_.GetSecLevel(dbSec);
90     ASSERT_EQ(security, S3);
91 
92     dbSec = { DistributedDB::S4, DistributedDB::ECE };
93     security = storeUtil_.GetSecLevel(dbSec);
94     ASSERT_EQ(security, S4);
95 }
96 /**
97  * @tc.name: GetDBMode
98  * @tc.desc: get db mode
99  * @tc.type: FUNC
100  * @tc.require:
101  * @tc.author: Wang Kai
102  */
103 HWTEST_F(StoreUtilTest, GetDBMode, TestSize.Level1)
104 {
105     StoreUtil storeUtil_;
106     StoreUtil::DBMode dbMode = storeUtil_.GetDBMode(SyncMode::PUSH);
107     ASSERT_EQ(dbMode, StoreUtil::DBMode::SYNC_MODE_PUSH_ONLY);
108 
109     dbMode = storeUtil_.GetDBMode(SyncMode::PULL);
110     ASSERT_EQ(dbMode, StoreUtil::DBMode::SYNC_MODE_PULL_ONLY);
111 
112     dbMode = storeUtil_.GetDBMode(SyncMode::PUSH_PULL);
113     ASSERT_EQ(dbMode, StoreUtil::DBMode::SYNC_MODE_PUSH_PULL);
114 }
115 /**
116  * @tc.name: GetObserverMode
117  * @tc.desc: get observer mode
118  * @tc.type: FUNC
119  * @tc.require:
120  * @tc.author: Wang Kai
121  */
122 HWTEST_F(StoreUtilTest, GetObserverMode, TestSize.Level1)
123 {
124     StoreUtil storeUtil_;
125     uint32_t mode = storeUtil_.GetObserverMode(SubscribeType::SUBSCRIBE_TYPE_LOCAL);
126     ASSERT_EQ(mode, DistributedDB::OBSERVER_CHANGES_NATIVE);
127 
128     mode = storeUtil_.GetObserverMode(SubscribeType::SUBSCRIBE_TYPE_REMOTE);
129     ASSERT_EQ(mode, DistributedDB::OBSERVER_CHANGES_FOREIGN);
130 
131     mode = storeUtil_.GetObserverMode(SUBSCRIBE_TYPE_ALL);
132     ASSERT_EQ(mode, DistributedDB::OBSERVER_CHANGES_FOREIGN | DistributedDB::OBSERVER_CHANGES_NATIVE);
133 }
134 /**
135  * @tc.name: CheckPermissions001
136  * @tc.desc: Check if the permissions for the first file creation are normal
137  * @tc.type: FUNC
138  * @tc.require:
139  * @tc.author: Shao Yuanzhao
140  */
141 HWTEST_F(StoreUtilTest, CheckPermissions001, TestSize.Level1)
142 {
143     StoreUtil storeUtil_;
144     std::string path = "/data/store_utils_test1";
145     bool success = storeUtil_.InitPath(path);
146     ASSERT_TRUE(success);
147 
148     struct stat buf;
149     int ret = stat(path.c_str(), &buf);
150     ASSERT_GE(ret, 0);
151     ASSERT_FALSE(buf.st_mode & S_IRWXO);
152 
153     std::string fileName = path + "/test1.txt";
154     success = storeUtil_.CreateFile(fileName);
155     ASSERT_TRUE(success);
156     ret = stat(fileName.c_str(), &buf);
157     ASSERT_GE(ret, 0);
158     ASSERT_FALSE(buf.st_mode & S_IRWXO);
159 
160     remove(fileName.c_str());
161     rmdir(path.c_str());
162 }
163 /**
164  * @tc.name: CheckPermissions002
165  * @tc.desc: Check if updating existing file permissions is correct
166  * @tc.type: FUNC
167  * @tc.require:
168  * @tc.author: Shao Yuanzhao
169  */
170 HWTEST_F(StoreUtilTest, CheckPermissions002, TestSize.Level1)
171 {
172     std::string path = "/data/store_utils_test2";
173     int ret = mkdir(path.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
174     struct stat buf;
175     ret = stat(path.c_str(), &buf);
176     ASSERT_GE(ret, 0);
177     ASSERT_TRUE(buf.st_mode & S_IRWXO);
178 
179     StoreUtil storeUtil_;
180     bool success = storeUtil_.InitPath(path);
181     ASSERT_TRUE(success);
182     ret = stat(path.c_str(), &buf);
183     ASSERT_GE(ret, 0);
184     ASSERT_FALSE(buf.st_mode & S_IRWXO);
185 
186     std::string fileName = path + "/test2.txt";
187     int fp = open(fileName.c_str(), (O_WRONLY | O_CREAT), (S_IRWXU | S_IRWXG | S_IRWXO));
188     ASSERT_GE(fp, 0);
189     close(fp);
190     ret = stat(fileName.c_str(), &buf);
191     ASSERT_GE(ret, 0);
192     ASSERT_TRUE(buf.st_mode & S_IRWXO);
193 
194     success = storeUtil_.CreateFile(fileName);
195     ASSERT_TRUE(success);
196     ret = stat(fileName.c_str(), &buf);
197     ASSERT_GE(ret, 0);
198     ASSERT_FALSE(buf.st_mode & S_IRWXO);
199 
200     remove(fileName.c_str());
201     rmdir(path.c_str());
202 }
203 } // namespace OHOS::Test