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
26 using namespace testing::ext;
27 using namespace DistributedDB;
28 using namespace DistributedDBUnitTest;
29 using namespace std;
30
31 namespace {
32 const std::string DATA_FILE_PATH = "/data/test/";
33 SecurityOption g_option = {0, 0};
34 const std::string DEV_ID = "devId";
35 std::shared_ptr<ProcessSystemApiAdapterImpl> g_adapter;
36 KvStoreDelegateManager g_mgr(APP_ID, USER_ID);
37 string g_testDir;
38 KvStoreConfig g_config;
39
40 // define the g_kvDelegateCallback, used to get some information when open a kv store.
41 DBStatus g_kvDelegateStatus = INVALID_ARGS;
42 KvStoreNbDelegate *g_kvNbDelegatePtr = nullptr;
43 auto g_kvNbDelegateCallback = bind(&DistributedDBToolsUnitTest::KvStoreNbDelegateCallback,
44 placeholders::_1, placeholders::_2, std::ref(g_kvDelegateStatus), std::ref(g_kvNbDelegatePtr));
45 }
46
47 class RuntimeContextProcessSystemApiAdapterImplTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 };
53
SetUpTestCase(void)54 void RuntimeContextProcessSystemApiAdapterImplTest::SetUpTestCase(void)
55 {
56 /**
57 * @tc.setup: Get an adapter
58 */
59 g_adapter = std::make_shared<ProcessSystemApiAdapterImpl>();
60 EXPECT_TRUE(g_adapter != nullptr);
61 DistributedDBToolsUnitTest::TestDirInit(g_testDir);
62 }
63
TearDownTestCase(void)64 void RuntimeContextProcessSystemApiAdapterImplTest::TearDownTestCase(void)
65 {
66 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
67 DistributedDBToolsUnitTest::RemoveTestDbFiles(g_testDir);
68 }
69
SetUp(void)70 void RuntimeContextProcessSystemApiAdapterImplTest::SetUp(void)
71 {
72 DistributedDBToolsUnitTest::PrintTestCaseInfo();
73 g_adapter->ResetAdapter();
74 }
75
76 /**
77 * @tc.name: SetSecurityOption001
78 * @tc.desc: Set SecurityOption.
79 * @tc.type: FUNC
80 * @tc.require: AR000EV1G2
81 */
82 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, SetSecurityOption001, TestSize.Level1)
83 {
84 /**
85 * @tc.steps: step1. call SetSecurityOption to set SecurityOption before set g_adapter
86 * @tc.expected: step1. function return E_NOT_SUPPORT
87 */
88 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
89 int errCode = RuntimeContext::GetInstance()->SetSecurityOption(DATA_FILE_PATH, g_option);
90 EXPECT_TRUE(errCode == -E_NOT_SUPPORT);
91
92 /**
93 * @tc.steps: step2. call SetSecurityOption to set SecurityOption after set g_adapter
94 * @tc.expected: step2. function return E_OK
95 */
96 EXPECT_TRUE(g_adapter != nullptr);
97 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
98 errCode = RuntimeContext::GetInstance()->SetSecurityOption(DATA_FILE_PATH, g_option);
99 EXPECT_EQ(errCode, E_OK);
100 }
101
102 /**
103 * @tc.name: GetSecurityOption001
104 * @tc.desc: Get SecurityOption.
105 * @tc.type: FUNC
106 * @tc.require: AR000EV1G2
107 */
108 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, GetSecurityOption001, TestSize.Level1)
109 {
110 /**
111 * @tc.steps: step1. call GetSecurityOption to get SecurityOption before set g_adapter
112 * @tc.expected: step1. function return E_NOT_SUPPORT
113 */
114 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
115 int errCode = RuntimeContext::GetInstance()->GetSecurityOption(DATA_FILE_PATH, g_option);
116 EXPECT_TRUE(errCode == -E_NOT_SUPPORT);
117
118 /**
119 * @tc.steps: step2. call GetSecurityOption to get SecurityOption after set g_adapter
120 * @tc.expected: step2. function return E_OK
121 */
122 EXPECT_TRUE(g_adapter != nullptr);
123 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
124 errCode = RuntimeContext::GetInstance()->GetSecurityOption(DATA_FILE_PATH, g_option);
125 EXPECT_TRUE(errCode == E_OK);
126 }
127
128 /**
129 * @tc.name: RegisterLockStatusLister001
130 * @tc.desc: Register a listener.
131 * @tc.type: FUNC
132 * @tc.require: AR000EV1G2
133 */
134 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, RegisterLockStatusLister001, TestSize.Level1)
135 {
136 int errCode = E_OK;
137 bool lockStatus = false;
__anon4051f62b0202(void *isLock) 138 auto onEventFunction1 = [&lockStatus](void *isLock) {
139 LOGI("lock status 1 changed %d", *(static_cast<bool *>(isLock)));
140 lockStatus = *(static_cast<bool *>(isLock));
141 };
142
__anon4051f62b0302(void *isLock) 143 auto onEventFunction2 = [&lockStatus](void *isLock) {
144 LOGI("lock status 2 changed %d", *(static_cast<bool *>(isLock)));
145 lockStatus = *(static_cast<bool *>(isLock));
146 };
147 /**
148 * @tc.steps: step1. call RegisterLockStatusLister to register a listener before set adapter
149 * @tc.expected: step1. function return ok
150 */
151 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
152 NotificationChain::Listener *listener =
153 RuntimeContext::GetInstance()->RegisterLockStatusLister(onEventFunction1, errCode);
154 EXPECT_NE(listener, nullptr);
155 EXPECT_EQ(errCode, E_OK);
156
157 /**
158 * @tc.steps: step2. call RegisterLockStatusLister to register a listener after set g_adapter
159 * @tc.expected: step2. function return a not null listener
160 */
161 EXPECT_TRUE(g_adapter != nullptr);
162 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
163
164 auto listener1 = RuntimeContext::GetInstance()->RegisterLockStatusLister(onEventFunction1, errCode);
165 EXPECT_TRUE(errCode == E_OK);
166 EXPECT_NE(listener1, nullptr);
167 listener1->Drop();
168
169 /**
170 * @tc.steps: step3. call SetLockStatus to change lock status
171 * @tc.expected: step3. the listener's callback should be called
172 */
173 g_adapter->SetLockStatus(false);
174 EXPECT_TRUE(!lockStatus);
175
176 /**
177 * @tc.steps: step4. call RegisterLockStatusLister to register another listener after set g_adapter
178 * @tc.expected: step4. function return a not null listener
179 */
180 listener->Drop();
181 listener = RuntimeContext::GetInstance()->RegisterLockStatusLister(onEventFunction2, errCode);
182 EXPECT_NE(listener, nullptr);
183 listener->Drop();
184 }
185
186 /**
187 * @tc.name: IsAccessControlled001
188 * @tc.desc: Get Access Lock Status.
189 * @tc.type: FUNC
190 * @tc.require: AR000EV1G2
191 */
192 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, IsAccessControlled001, TestSize.Level1)
193 {
194 /**
195 * @tc.steps: step1. call IsAccessControlled to get Access lock status before set g_adapter
196 * @tc.expected: step1. function return true
197 */
198 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
199 bool isLocked = RuntimeContext::GetInstance()->IsAccessControlled();
200 EXPECT_FALSE(isLocked);
201
202 /**
203 * @tc.steps: step2. IsAccessControlled to get Access lock status after set g_adapter
204 * @tc.expected: step2. function return false
205 */
206 EXPECT_TRUE(g_adapter != nullptr);
207 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
208 isLocked = RuntimeContext::GetInstance()->IsAccessControlled();
209 EXPECT_TRUE(!isLocked);
210 }
211
212 /**
213 * @tc.name: CheckDeviceSecurityAbility001
214 * @tc.desc: Check device security ability.
215 * @tc.type: FUNC
216 * @tc.require: AR000EV1G2
217 */
218 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, CheckDeviceSecurityAbility001, TestSize.Level1)
219 {
220 /**
221 * @tc.steps: step1. call CheckDeviceSecurityAbility to check device security ability before set g_adapter
222 * @tc.expected: step1. function return true
223 */
224 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
225 bool isSupported = RuntimeContext::GetInstance()->CheckDeviceSecurityAbility(DEV_ID, g_option);
226 EXPECT_TRUE(isSupported);
227
228 /**
229 * @tc.steps: step2. IsAccessControlled to check device security ability after set g_adapter
230 * @tc.expected: step2. function return true
231 */
232 EXPECT_TRUE(g_adapter != nullptr);
233 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
234 isSupported = RuntimeContext::GetInstance()->CheckDeviceSecurityAbility(DEV_ID, g_option);
235 EXPECT_TRUE(isSupported);
236 }
237
238 namespace {
FuncCheckDeviceSecurityAbility()239 void FuncCheckDeviceSecurityAbility()
240 {
241 RuntimeContext::GetInstance()->CheckDeviceSecurityAbility("", SecurityOption());
242 return;
243 }
244 }
245
246 /**
247 * @tc.name: CheckDeviceSecurityAbility002
248 * @tc.desc: Check device security ability with getkvstore frequency.
249 * @tc.type: FUNC
250 * @tc.require: AR000EV1G2
251 */
252 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, CheckDeviceSecurityAbility002, TestSize.Level1)
253 {
254 g_config.dataDir = g_testDir;
255 g_mgr.SetKvStoreConfig(g_config);
256
257 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
258 g_adapter->SetNeedCreateDb(true);
259
260 const std::string storeId = "CheckDeviceSecurityAbility002";
261 std::thread t1(FuncCheckDeviceSecurityAbility);
__anon4051f62b0502() 262 std::thread t2([&]() {
263 for (int i = 0; i < 100; i++) { // open close 100 times
264 LOGI("open store!!");
265 KvStoreNbDelegate::Option option1 = {true, false, false};
266 g_mgr.GetKvStore(storeId, option1, g_kvNbDelegateCallback);
267 g_mgr.CloseKvStore(g_kvNbDelegatePtr);
268 }
269 });
270 std::thread t3(FuncCheckDeviceSecurityAbility);
271
272 t1.join();
273 t2.join();
274 t3.join();
275 }
276
277 /**
278 * @tc.name: SetSystemApiAdapterTest001
279 * @tc.desc: Set SecurityOption.
280 * @tc.type: FUNC
281 * @tc.require:
282 */
283 HWTEST_F(RuntimeContextProcessSystemApiAdapterImplTest, SetSystemApiAdapterTest001, TestSize.Level1)
284 {
285 /**
286 * @tc.steps: step1. remove system api adapter
287 * @tc.expected: step1. return false
288 */
289 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(nullptr);
290 EXPECT_FALSE(g_mgr.IsProcessSystemApiAdapterValid());
291
292 /**
293 * @tc.steps: step2. set g_adapter
294 * @tc.expected: step2. return true
295 */
296 RuntimeContext::GetInstance()->SetProcessSystemApiAdapter(g_adapter);
297 EXPECT_TRUE(g_mgr.IsProcessSystemApiAdapterValid());
298 }