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
16 #include <gtest/gtest.h>
17
18 #include "db_errno.h"
19 #include "distributeddb_data_generate_unit_test.h"
20 #include "distributeddb_tools_unit_test.h"
21 #include "iprocess_system_api_adapter.h"
22 #include "log_print.h"
23 #include "process_system_api_adapter_impl.h"
24 #include "runtime_context.h"
25 #include "virtual_communicator_aggregator.h"
26
27 using namespace testing::ext;
28 using namespace DistributedDB;
29 using namespace DistributedDBUnitTest;
30 using namespace std;
31
32 namespace {
33 const std::string DATA_FILE_PATH = "/data/test/";
34 SecurityOption g_option = {0, 0};
35 const std::string DEV_ID = "devId";
36 std::shared_ptr<ProcessSystemApiAdapterImpl> g_adapter;
37 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
38 string g_testDir;
39 KvStoreConfig g_config;
40
41 // define the g_kvDelegateCallback, used to get some information when open a kv store.
42 DBStatus g_kvDelegateStatus = INVALID_ARGS;
43 KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
44 auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
45 placeholders::_1, placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
46 }
47
48 class RuntimeContextProcessSystemApiAdapterImplTest : public testing::Test {
49 public:
50 static void SetUpTestCase(void);
51 static void TearDownTestCase(void);
52 void SetUp();
53 };
54
SetUpTestCase(void)55 void RuntimeContextProcessSystemApiAdapterImplTest::SetUpTestCase(void)
56 {
57 /**
58 * @tc.setup: Get an adapter
59 */
60 g_adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
61 EXPECT_TRUE(g_adapter != nullptr);
62 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
63 }
64
TearDownTestCase(void)65 void RuntimeContextProcessSystemApiAdapterImplTest::TearDownTestCase(void)
66 {
67 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
68 DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir);
69 }
70
SetUp(void)71 void RuntimeContextProcessSystemApiAdapterImplTest::SetUp(void)
72 {
73 DistributedDBToolsUnitTest::PrintTestCaseInfo();
74 g_adapter->ResetAdapter();
75 }
76
77 /**
78 * @tc.name: SetSecurityOption001
79 * @tc.desc: Set SecurityOption.
80 * @tc.type: FUNC
81 * @tc.require: AR000EV1G2
82 */
83 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, SetSecurityOption001, TestSize.Level1)
84 {
85 /**
86 * @tc.steps: step1. call SetSecurityOption to set SecurityOption before set g_adapter
87 * @tc.expected: step1. function return E_NOT_SUPPORT
88 */
89 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
90 int errCode = RuntimeContext::GetInstance()->SetSecurityOption(g_testDir, g_option);
91 EXPECT_TRUE(errCode == -E_NOT_SUPPORT);
92
93 /**
94 * @tc.steps: step2. call SetSecurityOption to set SecurityOption after set g_adapter
95 * @tc.expected: step2. function return E_OK
96 */
97 EXPECT_TRUE(g_adapter != nullptr);
98 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
99 errCode = RuntimeContext::GetInstance()->SetSecurityOption(g_testDir, g_option);
100 EXPECT_EQ(errCode, E_OK);
101 }
102
103 /**
104 * @tc.name: GetSecurityOption001
105 * @tc.desc: Get SecurityOption.
106 * @tc.type: FUNC
107 * @tc.require: AR000EV1G2
108 */
109 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, GetSecurityOption001, TestSize.Level1)
110 {
111 /**
112 * @tc.steps: step1. call GetSecurityOption to get SecurityOption before set g_adapter
113 * @tc.expected: step1. function return E_NOT_SUPPORT
114 */
115 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
116 int errCode = RuntimeContext::GetInstance()->GetSecurityOption(DATA_FILE_PATH, g_option);
117 EXPECT_TRUE(errCode == -E_NOT_SUPPORT);
118
119 /**
120 * @tc.steps: step2. call GetSecurityOption to get SecurityOption after set g_adapter
121 * @tc.expected: step2. function return E_OK
122 */
123 EXPECT_TRUE(g_adapter != nullptr);
124 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
125 errCode = RuntimeContext::GetInstance()->GetSecurityOption(DATA_FILE_PATH, g_option);
126 EXPECT_TRUE(errCode == E_OK);
127 }
128
129 /**
130 * @tc.name: RegisterLockStatusLister001
131 * @tc.desc: Register a listener.
132 * @tc.type: FUNC
133 * @tc.require: AR000EV1G2
134 */
135 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, RegisterLockStatusLister001, TestSize.Level1)
136 {
137 int errCode = E_OK;
138 bool lockStatus = false;
__anon226589e30202(void *isLock) 139 auto onEventFunction1 = [&lockStatus](void *isLock) {
140 LOGI("lock status 1 changed %d", *(static_cast<bool *>(isLock)));
141 lockStatus = *(static_cast<bool *>(isLock));
142 };
143
__anon226589e30302(void *isLock) 144 auto onEventFunction2 = [&lockStatus](void *isLock) {
145 LOGI("lock status 2 changed %d", *(static_cast<bool *>(isLock)));
146 lockStatus = *(static_cast<bool *>(isLock));
147 };
148 /**
149 * @tc.steps: step1. call RegisterLockStatusLister to register a listener before set adapter
150 * @tc.expected: step1. function return ok
151 */
152 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
153 NotificationChain::Listener *listener =
154 RuntimeContext::GetInstance()->RegisterLockStatusLister(onEventFunction1, errCode);
155 EXPECT_NE(listener, nullptr);
156 EXPECT_EQ(errCode, E_OK);
157
158 /**
159 * @tc.steps: step2. call RegisterLockStatusLister to register a listener after set g_adapter
160 * @tc.expected: step2. function return a not null listener
161 */
162 EXPECT_TRUE(g_adapter != nullptr);
163 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
164
165 auto listener1 = RuntimeContext::GetInstance()->RegisterLockStatusLister(onEventFunction1, errCode);
166 EXPECT_TRUE(errCode == E_OK);
167 EXPECT_NE(listener1, nullptr);
168 listener1->Drop();
169
170 /**
171 * @tc.steps: step3. call SetLockStatus to change lock status
172 * @tc.expected: step3. the listener's callback should be called
173 */
174 g_adapter->SetLockStatus(false);
175 EXPECT_TRUE(!lockStatus);
176
177 /**
178 * @tc.steps: step4. call RegisterLockStatusLister to register another listener after set g_adapter
179 * @tc.expected: step4. function return a not null listener
180 */
181 listener->Drop();
182 listener = RuntimeContext::GetInstance()->RegisterLockStatusLister(onEventFunction2, errCode);
183 EXPECT_NE(listener, nullptr);
184 listener->Drop();
185 }
186
187 /**
188 * @tc.name: IsAccessControlled001
189 * @tc.desc: Get Access Lock Status.
190 * @tc.type: FUNC
191 * @tc.require: AR000EV1G2
192 */
193 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, IsAccessControlled001, TestSize.Level1)
194 {
195 /**
196 * @tc.steps: step1. call IsAccessControlled to get Access lock status before set g_adapter
197 * @tc.expected: step1. function return true
198 */
199 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
200 bool isLocked = RuntimeContext::GetInstance()->IsAccessControlled();
201 EXPECT_FALSE(isLocked);
202
203 /**
204 * @tc.steps: step2. IsAccessControlled to get Access lock status after set g_adapter
205 * @tc.expected: step2. function return false
206 */
207 EXPECT_TRUE(g_adapter != nullptr);
208 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
209 isLocked = RuntimeContext::GetInstance()->IsAccessControlled();
210 EXPECT_TRUE(!isLocked);
211 }
212
213 /**
214 * @tc.name: CheckDeviceSecurityAbility001
215 * @tc.desc: Check device security ability.
216 * @tc.type: FUNC
217 * @tc.require: AR000EV1G2
218 */
219 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, CheckDeviceSecurityAbility001, TestSize.Level1)
220 {
221 /**
222 * @tc.steps: step1. call CheckDeviceSecurityAbility to check device security ability before set g_adapter
223 * @tc.expected: step1. function return true
224 */
225 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
226 bool isSupported = RuntimeContext::GetInstance()->CheckDeviceSecurityAbility(DEV_ID, g_option);
227 EXPECT_TRUE(isSupported);
228
229 /**
230 * @tc.steps: step2. IsAccessControlled to check device security ability after set g_adapter
231 * @tc.expected: step2. function return true
232 */
233 EXPECT_TRUE(g_adapter != nullptr);
234 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
235 isSupported = RuntimeContext::GetInstance()->CheckDeviceSecurityAbility(DEV_ID, g_option);
236 EXPECT_TRUE(isSupported);
237 }
238
239 namespace {
FuncCheckDeviceSecurityAbility()240 void FuncCheckDeviceSecurityAbility()
241 {
242 RuntimeContext::GetInstance()->CheckDeviceSecurityAbility("", SecurityOption());
243 return;
244 }
245
CheckDeviceSecurityAbility002()246 void CheckDeviceSecurityAbility002()
247 {
248 g_config.dataDir = g_testDir;
249 EXPECT_EQ(g_mgr.SetKvStoreConfig(g_config), OK);
250
251 EXPECT_EQ(RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter), E_OK);
252 g_adapter->SetNeedCreateDb(true);
253
254 const std::string storeId = "CheckDeviceSecurityAbility002";
255 std::thread t1(FuncCheckDeviceSecurityAbility);
256 std::thread t2([&]() {
257 for (int i = 0; i < 100; i++) { // open close 100 times
258 LOGI("open store!!");
259 KvStoreNbDelegate::Option option1 = {true, false, false};
260 g_mgr.GetKvStore(storeId, option1, g_kvNbDelegateCallback);
261 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
262 }
263 });
264
265 t1.join();
266 t2.join();
267 }
268 }
269
270 /**
271 * @tc.name: CheckDeviceSecurityAbility002
272 * @tc.desc: Check device security ability with getkvstore frequency.
273 * @tc.type: FUNC
274 * @tc.require: AR000EV1G2
275 */
276 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, CheckDeviceSecurityAbility002, TestSize.Level1)
277 {
278 ASSERT_NO_FATAL_FAILURE(CheckDeviceSecurityAbility002());
279 }
280
281 /**
282 * @tc.name: SetSystemApiAdapterTest001
283 * @tc.desc: Set SecurityOption.
284 * @tc.type: FUNC
285 * @tc.require:
286 */
287 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, SetSystemApiAdapterTest001, TestSize.Level1)
288 {
289 /**
290 * @tc.steps: step1. remove system api adapter
291 * @tc.expected: step1. return false
292 */
293 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
294 EXPECT_FALSE(g_mgr.IsProcessSystemApiAdapterValid());
295
296 /**
297 * @tc.steps: step2. set g_adapter
298 * @tc.expected: step2. return true
299 */
300 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
301 EXPECT_TRUE(g_mgr.IsProcessSystemApiAdapterValid());
302 }
303
304 /**
305 * @tc.name: SecurityOptionUpgrade001
306 * @tc.desc: Test upgrade security label from s1 to s3.
307 * @tc.type: FUNC
308 * @tc.require:
309 */
310 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, SecurityOptionUpgrade001, TestSize.Level0)
311 {
312 /**
313 * @tc.steps: step1. set g_adapter and open with s1
314 * @tc.expected: step1. return true
315 */
316 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
317 EXPECT_TRUE(g_mgr.IsProcessSystemApiAdapterValid());
318 g_config.dataDir = g_testDir;
319 EXPECT_EQ(g_mgr.SetKvStoreConfig(g_config), OK);
320
321 const std::string storeId = "SecurityOptionUpgrade001";
322 KvStoreNbDelegate::Option option = {true, false, false};
323 option.secOption = { S1, ECE };
324 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
325 EXPECT_EQ(g_kvDelegateStatus, OK);
326 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
327 /**
328 * @tc.steps: step2. re open with s3
329 * @tc.expected: step2. open ok
330 */
331 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
332 option.secOption = { S3, SECE };
333 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
334 EXPECT_EQ(g_kvDelegateStatus, OK);
335 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
336 /**
337 * @tc.steps: step3. re open with s4
338 * @tc.expected: step3. open ok
339 */
340 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
341 option.secOption = { S4, SECE };
342 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
343 EXPECT_EQ(g_kvDelegateStatus, OK);
344 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
345 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
346 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
347 }
348
349 /**
350 * @tc.name: SecurityOptionUpgrade003
351 * @tc.desc: Test upgrade with error security label.
352 * @tc.type: FUNC
353 * @tc.require:
354 */
355 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, SecurityOptionUpgrade003, TestSize.Level0)
356 {
357 /**
358 * @tc.steps: step1. set g_adapter and open with not set
359 * @tc.expected: step1. return true
360 */
361 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
362 RuntimeContext::GetInstance()->SetCommunicatorAggregator(new(std::nothrow) VirtualCommunicatorAggregator);
363 EXPECT_TRUE(g_mgr.IsProcessSystemApiAdapterValid());
364 g_config.dataDir = g_testDir;
365 EXPECT_EQ(g_mgr.SetKvStoreConfig(g_config), OK);
366
367 const std::string storeId = "SecurityOptionUpgrade003";
368 KvStoreNbDelegate::Option option = {true, false, false};
369 option.secOption = { NOT_SET, ECE };
370 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
371 EXPECT_EQ(g_kvDelegateStatus, OK);
372 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
373 /**
374 * @tc.steps: step2. re open with s3
375 * @tc.expected: step2. open ok
376 */
377 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
378 auto existSecOpt = g_adapter->GetExistSecOpt();
379 option.secOption = { S3, SECE };
380 for (const auto &item : existSecOpt) {
381 g_adapter->SetSecurityOption(item.first, option.secOption);
382 }
383
384 g_mgr.GetKvStore(storeId, option, g_kvNbDelegateCallback);
385 EXPECT_EQ(g_kvDelegateStatus, OK);
386 ASSERT_NE(g_kvNbDelegatePtr, nullptr);
387 EXPECT_EQ(g_mgr.CloseKvStore(g_kvNbDelegatePtr), OK);
388 EXPECT_EQ(g_mgr.DeleteKvStore(storeId), OK);
389 RuntimeContext::GetInstance()->SetCommunicatorAggregator(nullptr);
390 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
391 }
392