1 /*
2 * Copyright (c) 2022-2024 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 #include <memory>
18 #include <string>
19 #include "accesstoken_log.h"
20 #include "access_token.h"
21 #define private public
22 #include "access_token_db.h"
23 #undef private
24 #include "data_translator.h"
25 #include "permission_def.h"
26 #include "generic_values.h"
27 #include "variant_value.h"
28
29 using namespace testing::ext;
30
31 namespace OHOS {
32 namespace Security {
33 namespace AccessToken {
34 namespace {
35 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "DatabaseTest"};
36 static constexpr int32_t GET_INT64_TRUE_VALUE = -1;
37 static constexpr int32_t ROLLBACK_TRANSACTION_RESULT_ABNORMAL = -1;
38 static constexpr int32_t EXECUTESQL_RESULT_ABNORMAL = -1;
39 static const int32_t DEFAULT_VALUE = -1;
40 static const int32_t TEST_TOKEN_ID = 100;
41 } // namespace
42 class DatabaseTest : public testing::Test {
43 public:
44 static void SetUpTestCase(void);
45 static void TearDownTestCase(void);
46 void SetUp();
47 void TearDown();
48 };
49
SetUpTestCase()50 void DatabaseTest::SetUpTestCase() {}
TearDownTestCase()51 void DatabaseTest::TearDownTestCase() {}
SetUp()52 void DatabaseTest::SetUp() {}
TearDown()53 void DatabaseTest::TearDown() {}
54
55 /**
56 * @tc.name: PutInt64001
57 * @tc.desc: Verify the GenericValues put and get int64 value function.
58 * @tc.type: FUNC
59 * @tc.require: Issue Number
60 */
61 HWTEST_F(DatabaseTest, PutInt64001, TestSize.Level1)
62 {
63 GenericValues genericValues;
64 std::string key = "databasetest";
65 int64_t data = 1;
66 genericValues.Put(key, data);
67 int64_t outdata = genericValues.GetInt64(key);
68 EXPECT_EQ(outdata, data);
69 genericValues.Remove(key);
70 outdata = genericValues.GetInt64(key);
71 EXPECT_EQ(GET_INT64_TRUE_VALUE, outdata);
72 }
73
74 /**
75 * @tc.name: PutVariant001
76 * @tc.desc: Verify the GenericValues put and get variant value function.
77 * @tc.type: FUNC
78 * @tc.require: Issue Number
79 */
80 HWTEST_F(DatabaseTest, PutVariant001, TestSize.Level1)
81 {
82 GenericValues genericValues;
83 std::string key = "databasetest";
84 int64_t testValue = 1;
85 VariantValue Test(testValue);
86 genericValues.Put(key, Test);
87 VariantValue outdata = genericValues.Get(key);
88 EXPECT_EQ(outdata.GetInt64(), testValue);
89 outdata = genericValues.Get("key");
90 EXPECT_EQ(DEFAULT_VALUE, outdata.GetInt64());
91 genericValues.Remove(key);
92 genericValues.Remove("key");
93 }
94
95 /**
96 * @tc.name: RollbackTransaction001
97 * @tc.desc: RollbackTransaction001 Abnormal branch res != SQLITE_OK
98 * @tc.type: FUNC
99 * @tc.require: Issue Number
100 */
101 HWTEST_F(DatabaseTest, RollbackTransaction001, TestSize.Level1)
102 {
103 AccessTokenDb::GetInstance().SetVersion();
104 int32_t result = AccessTokenDb::GetInstance().RollbackTransaction();
105 EXPECT_EQ(result, ROLLBACK_TRANSACTION_RESULT_ABNORMAL);
106 }
107
108 /**
109 * @tc.name: RollbackTransaction002
110 * @tc.desc: RollbackTransaction002 Abnormal branch db_ = nullptr
111 * @tc.type: FUNC
112 * @tc.require: Issue Number
113 */
114 HWTEST_F(DatabaseTest, RollbackTransaction002, TestSize.Level1)
115 {
116 AccessTokenDb::GetInstance().Close();
117 EXPECT_EQ(AccessTokenDb::GetInstance().RollbackTransaction(), ROLLBACK_TRANSACTION_RESULT_ABNORMAL);
118 }
119
120 /**
121 * @tc.name: ExecuteSql001
122 * @tc.desc: ExecuteSql001 Abnormal branch res != SQLITE_OK
123 * @tc.type: FUNC
124 * @tc.require: Issue Number
125 */
126 HWTEST_F(DatabaseTest, ExecuteSql001, TestSize.Level1)
127 {
128 std::string testSql = "test";
129 EXPECT_EQ(AccessTokenDb::GetInstance().ExecuteSql(testSql), EXECUTESQL_RESULT_ABNORMAL);
130 }
131
132 /**
133 * @tc.name: ExecuteSql002
134 * @tc.desc: ExecuteSql002 Abnormal branch db_ = nullptr
135 * @tc.type: FUNC
136 * @tc.require: Issue Number
137 */
138 HWTEST_F(DatabaseTest, ExecuteSql002, TestSize.Level1)
139 {
140 std::string testSql = "test";
141 AccessTokenDb::GetInstance().Close();
142 EXPECT_EQ(AccessTokenDb::GetInstance().ExecuteSql(testSql), EXECUTESQL_RESULT_ABNORMAL);
143 }
144
145 /**
146 * @tc.name: SpitError001
147 * @tc.desc: SpitError001 Abnormal branch db_ = nullptr
148 * @tc.type: FUNC
149 * @tc.require: Issue Number
150 */
151 HWTEST_F(DatabaseTest, SpitError001, TestSize.Level1)
152 {
153 AccessTokenDb::GetInstance().Close();
154 std::string result = AccessTokenDb::GetInstance().SpitError().c_str();
155 EXPECT_EQ(result.empty(), true);
156 }
157
158 /**
159 * @tc.name: SpitError002
160 * @tc.desc: SpitError002 use SpitError
161 * @tc.type: FUNC
162 * @tc.require: Issue Number
163 */
164 HWTEST_F(DatabaseTest, SpitError002, TestSize.Level1)
165 {
166 AccessTokenDb::GetInstance().Open();
167 std::string result = AccessTokenDb::GetInstance().SpitError().c_str();
168 EXPECT_EQ(result.length() > 0, true);
169 }
170
171 /**
172 * @tc.name: VariantValue64001
173 * @tc.desc: VariantValue64001 use VariantValue
174 * @tc.type: FUNC
175 * @tc.require: Issue Number
176 */
177 HWTEST_F(DatabaseTest, VariantValue64001, TestSize.Level1)
178 {
179 int64_t testValue = 1;
180 VariantValue Test(testValue);
181 EXPECT_EQ(Test.GetInt64(), testValue);
182 }
183
184 /**
185 * @tc.name: VariantValue64002
186 * @tc.desc: VariantValue64002 getint and getint64 Abnormal branch
187 * @tc.type: FUNC
188 * @tc.require: Issue Number
189 */
190 HWTEST_F(DatabaseTest, VariantValue64002, TestSize.Level1)
191 {
192 int32_t ntestValue = 1;
193 VariantValue Ntest(ntestValue);
194 EXPECT_EQ(DEFAULT_VALUE, Ntest.GetInt64());
195 int64_t testValue = 1;
196 VariantValue Test(testValue);
197 EXPECT_EQ(DEFAULT_VALUE, Test.GetInt());
198 }
199
200 /**
201 * @tc.name: VariantValue001
202 * @tc.desc: VariantValue001 getstring
203 * @tc.type: FUNC
204 * @tc.require:
205 */
206 HWTEST_F(DatabaseTest, VariantValue001, TestSize.Level1)
207 {
208 VariantValue Test;
209 Test.GetString();
210 EXPECT_EQ(ValueType::TYPE_NULL, Test.GetType());
211 }
212
RemoveTestTokenHapInfo()213 static void RemoveTestTokenHapInfo()
214 {
215 std::vector<GenericValues> hapInfoResults;
216 AccessTokenDb::GetInstance().Find(AccessTokenDb::ACCESSTOKEN_HAP_INFO, hapInfoResults);
217 for (GenericValues hapInfoValue : hapInfoResults) {
218 AccessTokenID tokenId = (AccessTokenID)hapInfoValue.GetInt(TokenFiledConst::FIELD_TOKEN_ID);
219 if (tokenId == TEST_TOKEN_ID) {
220 ASSERT_EQ(AccessTokenDb::SUCCESS,
221 AccessTokenDb::GetInstance().Remove(AccessTokenDb::ACCESSTOKEN_HAP_INFO, hapInfoValue));
222 break;
223 }
224 }
225 }
226
227 /*
228 * @tc.name: SqliteStorageAddTest001
229 * @tc.desc: Add function test
230 * @tc.type: FUNC
231 * @tc.require:
232 */
233 HWTEST_F(DatabaseTest, SqliteStorageAddTest001, TestSize.Level1)
234 {
235 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageAddTest001 begin");
236
237 RemoveTestTokenHapInfo();
238
239 GenericValues genericValues;
240 genericValues.Put(TokenFiledConst::FIELD_TOKEN_ID, TEST_TOKEN_ID);
241 genericValues.Put(TokenFiledConst::FIELD_USER_ID, 100);
242 genericValues.Put(TokenFiledConst::FIELD_BUNDLE_NAME, "test_bundle_name");
243 genericValues.Put(TokenFiledConst::FIELD_API_VERSION, 9);
244 genericValues.Put(TokenFiledConst::FIELD_INST_INDEX, 0);
245 genericValues.Put(TokenFiledConst::FIELD_DLP_TYPE, 0);
246 genericValues.Put(TokenFiledConst::FIELD_APP_ID, "test_app_id");
247 genericValues.Put(TokenFiledConst::FIELD_DEVICE_ID, "test_device_id");
248 genericValues.Put(TokenFiledConst::FIELD_APL, ATokenAplEnum::APL_NORMAL);
249 genericValues.Put(TokenFiledConst::FIELD_TOKEN_VERSION, 0);
250 genericValues.Put(TokenFiledConst::FIELD_TOKEN_ATTR, 0);
251 genericValues.Put(TokenFiledConst::FIELD_FORBID_PERM_DIALOG, "test_perm_dialog_cap_state");
252
253 std::vector<GenericValues> values;
254 values.emplace_back(genericValues);
255 EXPECT_EQ(AccessTokenDb::SUCCESS, AccessTokenDb::GetInstance().Add(AccessTokenDb::ACCESSTOKEN_HAP_INFO, values));
256 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageAddTest001 end");
257 }
258
259 /*
260 * @tc.name: SqliteStorageAddTest002
261 * @tc.desc: Add function test failed
262 * @tc.type: FUNC
263 * @tc.require:
264 */
265 HWTEST_F(DatabaseTest, SqliteStorageAddTest002, TestSize.Level1)
266 {
267 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageAddTest002 begin");
268
269 RemoveTestTokenHapInfo();
270
271 GenericValues genericValues;
272 genericValues.Put(TokenFiledConst::FIELD_TOKEN_ID, TEST_TOKEN_ID);
273
274 std::vector<GenericValues> values;
275 values.emplace_back(genericValues);
276 EXPECT_EQ(AccessTokenDb::FAILURE, AccessTokenDb::GetInstance().Add(AccessTokenDb::ACCESSTOKEN_HAP_INFO, values));
277 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageAddTest002 end");
278 }
279
280 /*
281 * @tc.name: SqliteStorageModifyTest001
282 * @tc.desc: Modify function test
283 * @tc.type: FUNC
284 * @tc.require:
285 */
286 HWTEST_F(DatabaseTest, SqliteStorageModifyTest001, TestSize.Level1)
287 {
288 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageModifyTest001 begin");
289
290 RemoveTestTokenHapInfo();
291
292 GenericValues genericValues;
293 genericValues.Put(TokenFiledConst::FIELD_TOKEN_ID, TEST_TOKEN_ID);
294 genericValues.Put(TokenFiledConst::FIELD_USER_ID, 100);
295 genericValues.Put(TokenFiledConst::FIELD_BUNDLE_NAME, "test_bundle_name");
296 genericValues.Put(TokenFiledConst::FIELD_API_VERSION, 9);
297 genericValues.Put(TokenFiledConst::FIELD_INST_INDEX, 0);
298 genericValues.Put(TokenFiledConst::FIELD_DLP_TYPE, 0);
299 genericValues.Put(TokenFiledConst::FIELD_APP_ID, "test_app_id");
300 genericValues.Put(TokenFiledConst::FIELD_DEVICE_ID, "test_device_id");
301 genericValues.Put(TokenFiledConst::FIELD_APL, ATokenAplEnum::APL_NORMAL);
302 genericValues.Put(TokenFiledConst::FIELD_TOKEN_VERSION, 0);
303 genericValues.Put(TokenFiledConst::FIELD_TOKEN_ATTR, 0);
304 genericValues.Put(TokenFiledConst::FIELD_FORBID_PERM_DIALOG, "test_perm_dialog_cap_state");
305
306 std::vector<GenericValues> values;
307 values.emplace_back(genericValues);
308 EXPECT_EQ(AccessTokenDb::SUCCESS, AccessTokenDb::GetInstance().Add(AccessTokenDb::ACCESSTOKEN_HAP_INFO, values));
309
310 GenericValues modifyValues;
311 modifyValues.Put(TokenFiledConst::FIELD_BUNDLE_NAME, "test_bundle_name_modified");
312
313 GenericValues conditions;
314 conditions.Put(TokenFiledConst::FIELD_TOKEN_ID, TEST_TOKEN_ID);
315 conditions.Put(TokenFiledConst::FIELD_USER_ID, 100);
316
317 ASSERT_EQ(AccessTokenDb::SUCCESS, AccessTokenDb::GetInstance().Modify(AccessTokenDb::ACCESSTOKEN_HAP_INFO,
318 modifyValues, conditions));
319
320 bool modifySuccess = false;
321 std::vector<GenericValues> hapInfoResults;
322 AccessTokenDb::GetInstance().Find(AccessTokenDb::ACCESSTOKEN_HAP_INFO, hapInfoResults);
323 for (GenericValues hapInfoValue : hapInfoResults) {
324 AccessTokenID tokenId = (AccessTokenID)hapInfoValue.GetInt(TokenFiledConst::FIELD_TOKEN_ID);
325 if (tokenId == TEST_TOKEN_ID) {
326 ASSERT_EQ("test_bundle_name_modified", hapInfoValue.GetString(TokenFiledConst::FIELD_BUNDLE_NAME));
327 modifySuccess = true;
328 break;
329 }
330 }
331 EXPECT_TRUE(modifySuccess);
332 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageModifyTest001 end");
333 }
334
335 /*
336 * @tc.name: SqliteStorageRefreshAllTest001
337 * @tc.desc: RefreshAll function test failed
338 * @tc.type: FUNC
339 * @tc.require:
340 */
341 HWTEST_F(DatabaseTest, SqliteStorageRefreshAllTest001, TestSize.Level1)
342 {
343 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageRefreshAllTest001 begin");
344
345 RemoveTestTokenHapInfo();
346
347 GenericValues genericValues;
348 genericValues.Put(TokenFiledConst::FIELD_TOKEN_ID, TEST_TOKEN_ID);
349
350 std::vector<GenericValues> values;
351 values.emplace_back(genericValues);
352 EXPECT_EQ(AccessTokenDb::FAILURE,
353 AccessTokenDb::GetInstance().RefreshAll(AccessTokenDb::ACCESSTOKEN_HAP_INFO, values));
354 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageRefreshAllTest001 end");
355 }
356
357 /*
358 * @tc.name: SqliteStorageCreateInsertPrepareSqlCmd001
359 * @tc.desc: CreateInsertPrepareSqlCmd function test type not found
360 * @tc.type: FUNC
361 * @tc.require:
362 */
363 HWTEST_F(DatabaseTest, SqliteStorageCreateInsertPrepareSqlCmd001, TestSize.Level1)
364 {
365 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateInsertPrepareSqlCmdTest001 begin");
366 AccessTokenDb::DataType type = static_cast<AccessTokenDb::DataType>(100);
367 ASSERT_EQ("", AccessTokenDb::GetInstance().CreateInsertPrepareSqlCmd(type));
368 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateInsertPrepareSqlCmdTest001 end");
369 }
370
371 /*
372 * @tc.name: SqliteStorageCreateDeletePrepareSqlCmd001
373 * @tc.desc: CreateDeletePrepareSqlCmd function test type not found
374 * @tc.type: FUNC
375 * @tc.require:
376 */
377 HWTEST_F(DatabaseTest, SqliteStorageCreateDeletePrepareSqlCmd001, TestSize.Level1)
378 {
379 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateDeletePrepareSqlCmdTest001 begin");
380 AccessTokenDb::DataType type = static_cast<AccessTokenDb::DataType>(100);
381 ASSERT_EQ("", AccessTokenDb::GetInstance().CreateDeletePrepareSqlCmd(type));
382 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateDeletePrepareSqlCmdTest001 end");
383 }
384
385 /*
386 * @tc.name: SqliteStorageCreateUpdatePrepareSqlCmd001
387 * @tc.desc: CreateUpdatePrepareSqlCmd function test type not found
388 * @tc.type: FUNC
389 * @tc.require:
390 */
391 HWTEST_F(DatabaseTest, SqliteStorageCreateUpdatePrepareSqlCmd001, TestSize.Level1)
392 {
393 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateUpdatePrepareSqlCmdTest001 begin");
394
395 AccessTokenDb::DataType type = static_cast<AccessTokenDb::DataType>(100);
396
397 GenericValues conditions;
398 conditions.Put(TokenFiledConst::FIELD_TOKEN_ID, TEST_TOKEN_ID);
399 conditions.Put(TokenFiledConst::FIELD_USER_ID, 100);
400
401 GenericValues modifyValues;
402 modifyValues.Put(TokenFiledConst::FIELD_BUNDLE_NAME, "test_bundle_name_modified");
403
404 std::vector<std::string> modifyColumns = modifyValues.GetAllKeys();
405 std::vector<std::string> conditionColumns = conditions.GetAllKeys();
406
407 ASSERT_EQ("", AccessTokenDb::GetInstance().CreateUpdatePrepareSqlCmd(type, modifyColumns, conditionColumns));
408 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateUpdatePrepareSqlCmdTest001 end");
409 }
410
411 /**
412 * @tc.name: SqliteStorageCreateUpdatePrepareSqlCmd002
413 * @tc.desc: AccessTokenDb::CreateUpdatePrepareSqlCmd function test
414 * @tc.type: FUNC
415 * @tc.require:
416 */
417 HWTEST_F(DatabaseTest, SqliteStorageCreateUpdatePrepareSqlCmd002, TestSize.Level1)
418 {
419 AccessTokenDb::DataType type = AccessTokenDb::DataType::ACCESSTOKEN_HAP_INFO;
420 std::vector<std::string> modifyColumns;
421 std::vector<std::string> conditionColumns;
422
423 // modifyColumns is empty
424 ASSERT_EQ("", AccessTokenDb::GetInstance().CreateUpdatePrepareSqlCmd(type, modifyColumns, conditionColumns));
425
426 type = AccessTokenDb::DataType::ACCESSTOKEN_HAP_INFO;
427 modifyColumns.emplace_back(TokenFiledConst::FIELD_TOKEN_ID);
428 modifyColumns.emplace_back(TokenFiledConst::FIELD_USER_ID);
429 // modifyColumns is not empty + modifyColumns.size > 1 + conditionColumns is empty
430 ASSERT_NE("", AccessTokenDb::GetInstance().CreateUpdatePrepareSqlCmd(type, modifyColumns, conditionColumns));
431 }
432
433 /*
434 * @tc.name: SqliteStorageCreateSelectPrepareSqlCmd001
435 * @tc.desc: CreateSelectPrepareSqlCmd function test type not found
436 * @tc.type: FUNC
437 * @tc.require:
438 */
439 HWTEST_F(DatabaseTest, SqliteStorageCreateSelectPrepareSqlCmd001, TestSize.Level1)
440 {
441 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateSelectPrepareSqlCmdTest001 begin");
442 AccessTokenDb::DataType type = static_cast<AccessTokenDb::DataType>(100);
443 ASSERT_EQ("", AccessTokenDb::GetInstance().CreateSelectPrepareSqlCmd(type));
444 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateSelectPrepareSqlCmdTest001 end");
445 }
446
447 /*
448 * @tc.name: SqliteStorageCreateHapTokenInfoTable001
449 * @tc.desc: CreateHapTokenInfoTable function test
450 * @tc.type: FUNC
451 * @tc.require:
452 */
453 HWTEST_F(DatabaseTest, SqliteStorageCreateHapTokenInfoTable001, TestSize.Level1)
454 {
455 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateHapTokenInfoTableTest001 begin");
456 ASSERT_EQ(AccessTokenDb::SUCCESS, AccessTokenDb::GetInstance().CreateHapTokenInfoTable());
457 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateHapTokenInfoTableTest001 end");
458 }
459
460 /**
461 * @tc.name: SqliteStorageCreateHapTokenInfoTable002
462 * @tc.desc: AccessTokenDb::CreateHapTokenInfoTable function test
463 * @tc.type: FUNC
464 * @tc.require:
465 */
466 HWTEST_F(DatabaseTest, SqliteStorageCreateHapTokenInfoTable002, TestSize.Level1)
467 {
468 std::map<AccessTokenDb::DataType, AccessTokenDb::SqliteTable> dataTypeToSqlTable;
469 dataTypeToSqlTable = AccessTokenDb::GetInstance().dataTypeToSqlTable_; // backup
470 AccessTokenDb::GetInstance().dataTypeToSqlTable_.clear();
471
472 ASSERT_EQ(AccessTokenDb::FAILURE, AccessTokenDb::GetInstance().CreateHapTokenInfoTable());
473 ASSERT_EQ(AccessTokenDb::FAILURE, AccessTokenDb::GetInstance().CreateNativeTokenInfoTable());
474 ASSERT_EQ(AccessTokenDb::FAILURE, AccessTokenDb::GetInstance().CreatePermissionDefinitionTable());
475 ASSERT_EQ(AccessTokenDb::FAILURE, AccessTokenDb::GetInstance().CreatePermissionStateTable());
476 ASSERT_EQ(AccessTokenDb::FAILURE, AccessTokenDb::GetInstance().CreatePermissionRequestToggleStatusTable());
477
478 AccessTokenDb::GetInstance().dataTypeToSqlTable_ = dataTypeToSqlTable; // recovery
479 }
480
481 /*
482 * @tc.name: SqliteStorageCreateNativeTokenInfoTable001
483 * @tc.desc: CreateNativeTokenInfoTable function test
484 * @tc.type: FUNC
485 * @tc.require:
486 */
487 HWTEST_F(DatabaseTest, SqliteStorageCreateNativeTokenInfoTable001, TestSize.Level1)
488 {
489 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateNativeTokenInfoTableTest001 begin");
490 ASSERT_EQ(AccessTokenDb::SUCCESS, AccessTokenDb::GetInstance().CreateNativeTokenInfoTable());
491 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreateNativeTokenInfoTableTest001 end");
492 }
493
494 /*
495 * @tc.name: SqliteStorageCreatePermissionDefinitionTable001
496 * @tc.desc: CreatePermissionDefinitionTable function test
497 * @tc.type: FUNC
498 * @tc.require:
499 */
500 HWTEST_F(DatabaseTest, SqliteStorageCreatePermissionDefinitionTable001, TestSize.Level1)
501 {
502 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreatePermissionDefinitionTableTest001 begin");
503 ASSERT_EQ(AccessTokenDb::SUCCESS, AccessTokenDb::GetInstance().CreatePermissionDefinitionTable());
504 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreatePermissionDefinitionTableTest001 end");
505 }
506
507 /*
508 * @tc.name: SqliteStorageCreatePermissionStateTable001
509 * @tc.desc: CreatePermissionStateTable function test
510 * @tc.type: FUNC
511 * @tc.require:
512 */
513 HWTEST_F(DatabaseTest, SqliteStorageCreatePermissionStateTable001, TestSize.Level1)
514 {
515 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreatePermissionStateTableTest001 begin");
516 ASSERT_EQ(AccessTokenDb::SUCCESS, AccessTokenDb::GetInstance().CreatePermissionStateTable());
517 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreatePermissionStateTableTest001 end");
518 }
519
520 /*
521 * @tc.name: SqliteStorageCreatePermissionRequestToggleStatusTable001
522 * @tc.desc: CreatePermissionRequestToggleStatusTable function test
523 * @tc.type: FUNC
524 * @tc.require:
525 */
526 HWTEST_F(DatabaseTest, SqliteStorageCreatePermissionRequestToggleStatusTable001, TestSize.Level1)
527 {
528 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreatePermissionRequestToggleStatusTableTest001 begin");
529 ASSERT_EQ(AccessTokenDb::SUCCESS, AccessTokenDb::GetInstance().CreatePermissionRequestToggleStatusTable());
530 ACCESSTOKEN_LOG_INFO(LABEL, "SqliteStorageCreatePermissionRequestToggleStatusTableTest001 end");
531 }
532
533 /*
534 * @tc.name: DataTranslatorTranslationIntoPermissionDef001
535 * @tc.desc: TranslationIntoPermissionDef function test
536 * @tc.type: FUNC
537 * @tc.require:
538 */
539 HWTEST_F(DatabaseTest, DataTranslatorTranslationIntoPermissionDef001, TestSize.Level1)
540 {
541 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionDefTest001 begin");
542
543 RemoveTestTokenHapInfo();
544
545 GenericValues genericValues;
546 genericValues.Put(TokenFiledConst::FIELD_TOKEN_ID, TEST_TOKEN_ID);
547 genericValues.Put(TokenFiledConst::FIELD_AVAILABLE_LEVEL, ATokenAplEnum::APL_INVALID);
548
549 PermissionDef outPermissionDef;
550 ASSERT_NE(RET_SUCCESS, DataTranslator::TranslationIntoPermissionDef(genericValues, outPermissionDef));
551 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionDefTest001 end");
552 }
553
554 /*
555 * @tc.name: DataTranslatorTranslationIntoGenericValues001
556 * @tc.desc: DataTranslatorTranslationIntoGenericValues function test
557 * @tc.type: FUNC
558 * @tc.require:
559 */
560 HWTEST_F(DatabaseTest, DataTranslatorTranslationIntoGenericValues001, TestSize.Level1)
561 {
562 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoGenericValues001 begin");
563 PermissionStateFull grantPermissionReq = {
564 .permissionName = "ohos.permission.GRANT_SENSITIVE_PERMISSIONS",
565 .isGeneral = true,
566 .resDeviceID = {"device1"},
567 .grantStatus = {PermissionState::PERMISSION_GRANTED},
568 .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}
569 };
570 int grantIndex = 1;
571 GenericValues genericValues;
572 ASSERT_NE(RET_SUCCESS,
573 DataTranslator::TranslationIntoGenericValues(grantPermissionReq, grantIndex, genericValues));
574 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoGenericValues001 end");
575 }
576
577 /*
578 * @tc.name: DataTranslatorTranslationIntoGenericValues002
579 * @tc.desc: DataTranslatorTranslationIntoGenericValues function test
580 * @tc.type: FUNC
581 * @tc.require:
582 */
583 HWTEST_F(DatabaseTest, DataTranslatorTranslationIntoGenericValues002, TestSize.Level1)
584 {
585 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoGenericValues002 begin");
586 PermissionStateFull grantPermissionReq = {
587 .permissionName = "ohos.permission.GRANT_SENSITIVE_PERMISSIONS",
588 .isGeneral = true,
589 .resDeviceID = {"device1", "device2"},
590 .grantStatus = {PermissionState::PERMISSION_GRANTED},
591 .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}
592 };
593 int grantIndex = 1;
594 GenericValues genericValues;
595 ASSERT_NE(RET_SUCCESS,
596 DataTranslator::TranslationIntoGenericValues(grantPermissionReq, grantIndex, genericValues));
597 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoGenericValues002 end");
598 }
599
600 /*
601 * @tc.name: DataTranslatorTranslationIntoGenericValues003
602 * @tc.desc: DataTranslatorTranslationIntoGenericValues function test
603 * @tc.type: FUNC
604 * @tc.require:
605 */
606 HWTEST_F(DatabaseTest, DataTranslatorTranslationIntoGenericValues003, TestSize.Level1)
607 {
608 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoGenericValues003 begin");
609 PermissionStateFull grantPermissionReq = {
610 .permissionName = "ohos.permission.GRANT_SENSITIVE_PERMISSIONS",
611 .isGeneral = true,
612 .resDeviceID = {"device1", "device2"},
613 .grantStatus = {PermissionState::PERMISSION_GRANTED, PermissionState::PERMISSION_GRANTED},
614 .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED}
615 };
616 int grantIndex = 1;
617 GenericValues genericValues;
618 ASSERT_NE(RET_SUCCESS,
619 DataTranslator::TranslationIntoGenericValues(grantPermissionReq, grantIndex, genericValues));
620 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoGenericValues003 end");
621 }
622
623 /*
624 * @tc.name: DataTranslatorTranslationIntoPermissionStateFull001
625 * @tc.desc: TranslationIntoPermissionStateFull function test
626 * @tc.type: FUNC
627 * @tc.require:
628 */
629 HWTEST_F(DatabaseTest, DataTranslatorTranslationIntoPermissionStateFull001, TestSize.Level1)
630 {
631 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionStateFullTest001 begin");
632
633 PermissionStateFull outPermissionState;
634
635 GenericValues inGenericValues;
636 inGenericValues.Put(TokenFiledConst::FIELD_GRANT_IS_GENERAL, 1);
637 inGenericValues.Put(TokenFiledConst::FIELD_PERMISSION_NAME, "");
638
639 PermissionDef outPermissionDef;
640 ASSERT_NE(RET_SUCCESS, DataTranslator::TranslationIntoPermissionStateFull(inGenericValues, outPermissionState));
641 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionStateFullTest001 end");
642 }
643
644 /*
645 * @tc.name: DataTranslatorTranslationIntoPermissionStateFull002
646 * @tc.desc: TranslationIntoPermissionStateFull function test
647 * @tc.type: FUNC
648 * @tc.require:
649 */
650 HWTEST_F(DatabaseTest, DataTranslatorTranslationIntoPermissionStateFull002, TestSize.Level1)
651 {
652 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionStateFullTest002 begin");
653
654 PermissionStateFull outPermissionState;
655
656 GenericValues inGenericValues;
657 inGenericValues.Put(TokenFiledConst::FIELD_GRANT_IS_GENERAL, 1);
658 inGenericValues.Put(TokenFiledConst::FIELD_PERMISSION_NAME, "test_permission_name");
659 inGenericValues.Put(TokenFiledConst::FIELD_DEVICE_ID, "");
660
661 PermissionDef outPermissionDef;
662 ASSERT_NE(RET_SUCCESS, DataTranslator::TranslationIntoPermissionStateFull(inGenericValues, outPermissionState));
663 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionStateFullTest002 end");
664 }
665
666 /*
667 * @tc.name: DataTranslatorTranslationIntoPermissionStateFull003
668 * @tc.desc: TranslationIntoPermissionStateFull function test
669 * @tc.type: FUNC
670 * @tc.require:
671 */
672 HWTEST_F(DatabaseTest, DataTranslatorTranslationIntoPermissionStateFull003, TestSize.Level1)
673 {
674 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionStateFullTest003 begin");
675
676 PermissionStateFull outPermissionState;
677
678 GenericValues inGenericValues;
679 inGenericValues.Put(TokenFiledConst::FIELD_GRANT_IS_GENERAL, 1);
680 inGenericValues.Put(TokenFiledConst::FIELD_PERMISSION_NAME, "test_permission_name");
681 inGenericValues.Put(TokenFiledConst::FIELD_DEVICE_ID, "test_device_id");
682 inGenericValues.Put(TokenFiledConst::FIELD_GRANT_STATE, 100);
683
684 PermissionDef outPermissionDef;
685 ASSERT_NE(RET_SUCCESS, DataTranslator::TranslationIntoPermissionStateFull(inGenericValues, outPermissionState));
686 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionStateFullTest003 end");
687 }
688
689 /*
690 * @tc.name: DataTranslatorTranslationIntoPermissionStateFull004
691 * @tc.desc: TranslationIntoPermissionStateFull function test
692 * @tc.type: FUNC
693 * @tc.require:
694 */
695 HWTEST_F(DatabaseTest, DataTranslatorTranslationIntoPermissionStateFull004, TestSize.Level1)
696 {
697 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionStateFullTest004 begin");
698
699 PermissionStateFull outPermissionState;
700
701 GenericValues inGenericValues;
702 inGenericValues.Put(TokenFiledConst::FIELD_GRANT_IS_GENERAL, 1);
703 inGenericValues.Put(TokenFiledConst::FIELD_PERMISSION_NAME, "test_permission_name");
704 inGenericValues.Put(TokenFiledConst::FIELD_DEVICE_ID, "test_device_id");
705 inGenericValues.Put(TokenFiledConst::FIELD_GRANT_STATE, PermissionState::PERMISSION_GRANTED);
706 inGenericValues.Put(TokenFiledConst::FIELD_GRANT_FLAG, 100);
707
708 PermissionDef outPermissionDef;
709 ASSERT_NE(RET_SUCCESS, DataTranslator::TranslationIntoPermissionStateFull(inGenericValues, outPermissionState));
710 ACCESSTOKEN_LOG_INFO(LABEL, "DataTranslatorTranslationIntoPermissionStateFullTest004 end");
711 }
712 } // namespace AccessToken
713 } // namespace Security
714 } // namespace OHOS
715