1 /*
2 * Copyright (c) 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 "sandbox_json_manager_test.h"
17 #include <cerrno>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include "dlp_permission.h"
21 #include "dlp_permission_log.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::Security::DlpPermission;
26 using namespace std;
27
28 namespace {
29 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
30 LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "SandboxJsonManagerTest"};
31 }
32
SetUpTestCase()33 void SandboxJsonManagerTest::SetUpTestCase() {}
34
TearDownTestCase()35 void SandboxJsonManagerTest::TearDownTestCase() {}
36
SetUp()37 void SandboxJsonManagerTest::SetUp() {}
38
TearDown()39 void SandboxJsonManagerTest::TearDown() {}
40
41 /**
42 * @tc.name: AddSandboxInfo001
43 * @tc.desc: AddSandboxInfo test
44 * @tc.type: FUNC
45 * @tc.require:
46 */
47 HWTEST_F(SandboxJsonManagerTest, AddSandboxInfo001, TestSize.Level1)
48 {
49 DLP_LOG_INFO(LABEL, "AddSandboxInfo001");
50
51 std::shared_ptr<SandboxJsonManager> sandboxJsonManager_ = std::make_shared<SandboxJsonManager>();
52 RetentionInfo retentionInfo = {
53 .appIndex = -1,
54 .tokenId = 827818,
55 .bundleName = "testbundle1",
56 .dlpFileAccess = DLPFileAccess::CONTENT_EDIT,
57 .userId = 10000
58 };
59 int32_t ret = sandboxJsonManager_->AddSandboxInfo(retentionInfo);
60 ASSERT_EQ(DLP_INSERT_FILE_ERROR, ret);
61
62 retentionInfo.appIndex = 1;
63 retentionInfo.userId = -1;
64 ret = sandboxJsonManager_->AddSandboxInfo(retentionInfo);
65 ASSERT_EQ(DLP_INSERT_FILE_ERROR, ret);
66
67 retentionInfo.userId = 10000;
68 retentionInfo.tokenId = 0;
69 ret = sandboxJsonManager_->AddSandboxInfo(retentionInfo);
70 ASSERT_EQ(DLP_INSERT_FILE_ERROR, ret);
71 }
72
73 /**
74 * @tc.name: UpdateReadFlag001
75 * @tc.desc: UpdateReadFlag test
76 * @tc.type: FUNC
77 * @tc.require:
78 */
79 HWTEST_F(SandboxJsonManagerTest, UpdateReadFlag001, TestSize.Level1)
80 {
81 DLP_LOG_INFO(LABEL, "UpdateReadFlag001");
82
83 std::shared_ptr<SandboxJsonManager> sandboxJsonManager_ = std::make_shared<SandboxJsonManager>();
84 std::set<std::string> docUriSet = {"testUri"};
85 RetentionInfo retentionInfo = {
86 .appIndex = 0,
87 .tokenId = 827818,
88 .bundleName = "testbundle1",
89 .dlpFileAccess = DLPFileAccess::CONTENT_EDIT,
90 .userId = 10000
91 };
92 int32_t ret = sandboxJsonManager_->AddSandboxInfo(retentionInfo);
93 ASSERT_EQ(DLP_OK, ret);
94
95 // tokenId == iter->tokenId && iter->docUriSet.empty()
96 ret = sandboxJsonManager_->UpdateReadFlag(retentionInfo.tokenId);
97 ASSERT_EQ(DLP_FILE_NO_NEED_UPDATE, ret);
98
99 // tokenId != iter->tokenId && iter->docUriSet.empty()
100 ret = sandboxJsonManager_->UpdateReadFlag(retentionInfo.tokenId - 1);
101 ASSERT_EQ(DLP_FILE_NO_NEED_UPDATE, ret);
102
103 retentionInfo.tokenId = 123;
104 retentionInfo.docUriSet = docUriSet;
105 ret = sandboxJsonManager_->AddSandboxInfo(retentionInfo);
106 ASSERT_EQ(DLP_OK, ret);
107 // tokenId != iter->tokenId && !iter->docUriSet.empty()
108 ret = sandboxJsonManager_->UpdateReadFlag(retentionInfo.tokenId - 1);
109 ASSERT_EQ(DLP_FILE_NO_NEED_UPDATE, ret);
110
111 // tokenId == iter->tokenId && !iter->docUriSet.empty()
112 ret = sandboxJsonManager_->UpdateReadFlag(retentionInfo.tokenId);
113 ASSERT_EQ(DLP_OK, ret);
114 }
115
116 /**
117 * @tc.name: FromJson001
118 * @tc.desc: FromJson test
119 * @tc.type: FUNC
120 * @tc.require:
121 */
122 HWTEST_F(SandboxJsonManagerTest, FromJson001, TestSize.Level1)
123 {
124 DLP_LOG_INFO(LABEL, "FromJson001");
125 std::shared_ptr<SandboxJsonManager> sandboxJsonManager_ = std::make_shared<SandboxJsonManager>();
126 std::set<std::string> docUriSet = {"dlp_cert", "dlp_general_info", "encrypted_data"};
127
128 // info.bundleName.empty()
129 RetentionInfo info = {
130 .appIndex = 0,
131 .tokenId = 827818,
132 .bundleName = "",
133 .dlpFileAccess = DLPFileAccess::CONTENT_EDIT,
134 .docUriSet = docUriSet,
135 .userId = 10000,
136 .hasRead = false
137 };
138 Json infoJson;
139 sandboxJsonManager_->RetentionInfoToJson(infoJson, info);
140 Json jsonObject;
141 jsonObject["retention"].push_back(infoJson);
142 sandboxJsonManager_->FromJson(jsonObject);
143 ASSERT_TRUE(sandboxJsonManager_->infoVec_.empty());
144 jsonObject.erase("retention");
145
146 // info.appIndex < 0
147 info.bundleName = "com";
148 info.appIndex = -1;
149 sandboxJsonManager_->RetentionInfoToJson(infoJson, info);
150 jsonObject["retention"].push_back(infoJson);
151 sandboxJsonManager_->FromJson(jsonObject);
152 ASSERT_TRUE(sandboxJsonManager_->infoVec_.empty());
153 jsonObject.erase("retention");
154
155 // info.userId < 0
156 info.userId = -1;
157 info.appIndex = 1;
158 sandboxJsonManager_->RetentionInfoToJson(infoJson, info);
159 jsonObject["retention"].push_back(infoJson);
160 sandboxJsonManager_->FromJson(jsonObject);
161 ASSERT_TRUE(sandboxJsonManager_->infoVec_.empty());
162 jsonObject.erase("retention");
163
164 // info.tokenId == 0
165 info.userId = 1;
166 info.tokenId = 0;
167 sandboxJsonManager_->RetentionInfoToJson(infoJson, info);
168 jsonObject["retention"].push_back(infoJson);
169 sandboxJsonManager_->FromJson(jsonObject);
170 ASSERT_TRUE(sandboxJsonManager_->infoVec_.empty());
171 jsonObject.erase("retention");
172
173 // succ
174 info.userId = 1;
175 info.tokenId = 1;
176 sandboxJsonManager_->RetentionInfoToJson(infoJson, info);
177 jsonObject["retention"].push_back(infoJson);
178 sandboxJsonManager_->FromJson(jsonObject);
179 ASSERT_TRUE(sandboxJsonManager_->infoVec_.size() == 1);
180 }