1 /*
2  * Copyright (c) 2023 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 "dlp_kv_storage_test.h"
17 #define protected public
18 #include "sandbox_config_kv_data_storage.h"
19 #undef protected
20 #include "dlp_permission.h"
21 #include "dlp_permission_log.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace DlpPermission {
26 using namespace testing::ext;
27 using namespace OHOS;
28 using namespace OHOS::Security::DlpPermission;
29 namespace {
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
31     LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpKvStorageTest"};
32 static const std::string BUNDLE_NAME = "test";
33 static const std::string TOKENID = "123456";
34 static const std::string CONFIG = "testConfig";
35 static const std::string APP_CONFIG_STORE_ID = "sandbox_app_config_info";
36 KvDataStorageOptions options = { .autoSync = false };
37 } // namespace
38 
SetUpTestCase()39 void DlpKvStorageTest::SetUpTestCase() {}
40 
TearDownTestCase()41 void DlpKvStorageTest::TearDownTestCase() {}
42 
SetUp()43 void DlpKvStorageTest::SetUp() {}
44 
TearDown()45 void DlpKvStorageTest::TearDown() {}
46 
47 /**
48  * @tc.name: DlpKvStorageTest001
49  * @tc.desc: test GetBundleInfo failed with param is invalid
50  * @tc.type: FUNC
51  * @tc.require:
52  */
53 HWTEST_F(DlpKvStorageTest, DlpKvStorageTest001, TestSize.Level1)
54 {
55     std::string configInfo;
56     int32_t res = SandboxConfigKvDataStorage::GetInstance().AddSandboxConfigIntoDataStorage(100,
57         BUNDLE_NAME, CONFIG, TOKENID);
58     ASSERT_EQ(res, DLP_OK);
59     res = SandboxConfigKvDataStorage::GetInstance().GetSandboxConfigFromDataStorage(100,
60         BUNDLE_NAME, configInfo, TOKENID);
61     ASSERT_EQ(res, DLP_OK);
62     res = SandboxConfigKvDataStorage::GetInstance().GetSandboxConfigFromDataStorage(1000,
63         BUNDLE_NAME, configInfo, TOKENID);
64     ASSERT_EQ(res, DLP_KV_GET_DATA_NOT_FOUND);
65     std::map<std::string, std::string> keyMap;
66     res = SandboxConfigKvDataStorage::GetInstance().GetKeyMapByUserId(100, keyMap);
67     ASSERT_EQ(res, DLP_OK);
68     res = SandboxConfigKvDataStorage::GetInstance().GetKeyMapByUserId(101, keyMap);
69     ASSERT_EQ(res, DLP_OK);
70     res = SandboxConfigKvDataStorage::GetInstance().DeleteSandboxConfigFromDataStorage(1000, BUNDLE_NAME, TOKENID);
71     ASSERT_EQ(res, DLP_OK);
72     res = SandboxConfigKvDataStorage::GetInstance().DeleteSandboxConfigFromDataStorage(100, BUNDLE_NAME, TOKENID);
73     ASSERT_EQ(res, DLP_OK);
74     res = SandboxConfigKvDataStorage::GetInstance().RemoveValueFromKvStore("testKey");
75     ASSERT_EQ(res, DLP_OK);
76     res = SandboxConfigKvDataStorage::GetInstance().DeleteKvStore();
77     ASSERT_EQ(res, DLP_OK);
78 }
79 
80 /**
81  * @tc.name: DlpKvStorageTest002
82  * @tc.desc: test  DlpKvStorageTest
83  * @tc.type: FUNC
84  * @tc.require:
85  */
86 HWTEST_F(DlpKvStorageTest, DlpKvStorageTest002, TestSize.Level1)
87 {
88     int32_t res = SandboxConfigKvDataStorage::GetInstance().AddOrUpdateValue("", CONFIG);
89     ASSERT_EQ(res, DLP_KV_DATE_INFO_EMPTY_ERROR);
90     res = SandboxConfigKvDataStorage::GetInstance().AddOrUpdateValue(BUNDLE_NAME, "");
91     ASSERT_EQ(res, DLP_KV_DATE_INFO_EMPTY_ERROR);
92     bool result = SandboxConfigKvDataStorage::GetInstance().IsKeyExists("");
93     ASSERT_EQ(result, false);
94     res = SandboxConfigKvDataStorage::GetInstance().RemoveValueFromKvStore("testKey");
95     ASSERT_EQ(res, DLP_OK);
96     res = SandboxConfigKvDataStorage::GetInstance().DeleteKvStore();
97     ASSERT_EQ(res, DLP_OK);
98 }
99 
100 /**
101  * @tc.name: DlpKvStorageTest003
102  * @tc.desc: test DlpKvDataStorage
103  * @tc.type: FUNC
104  * @tc.require:
105  */
106 HWTEST_F(DlpKvStorageTest, DlpKvStorageTest003, TestSize.Level1)
107 {
108     DLP_LOG_INFO(LABEL, "DlpKvStorageTest003");
109     std::string config;
110     int32_t res = SandboxConfigKvDataStorage::GetInstance().GetSandboxConfigFromDataStorage(100, "", config, TOKENID);
111     ASSERT_NE(res, DLP_OK);
112     res = SandboxConfigKvDataStorage::GetInstance().AddSandboxConfigIntoDataStorage(100, "", config, TOKENID);
113     ASSERT_NE(res, DLP_OK);
114     res = SandboxConfigKvDataStorage::GetInstance().DeleteSandboxConfigFromDataStorage(100, "", TOKENID);
115     ASSERT_NE(res, DLP_OK);
116 }
117 }  // namespace DlpPermission
118 }  // namespace Security
119 }  // namespace OHOS
120