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 #include "permission_policy_test.h"
16
17 #include <string>
18 #include "dlp_permission.h"
19 #include "dlp_permission_log.h"
20
21 namespace OHOS {
22 namespace Security {
23 namespace DlpPermission {
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::Security::DlpPermission;
27 using namespace std::chrono;
28
29 namespace {
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
31 LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "PermissionPolicyTest"};
32 const uint32_t MAX_ACCOUNT_SIZE = 1024;
33 const uint32_t MAX_ACCOUNT_NUM = 100;
34 const uint32_t AES_KEY_LEN = 16;
35 const uint32_t IV_LEN = 16;
36 const uint64_t EXPIRY_TEN_MINUTE = 60 * 10;
37
GetCurrentTimeSec(void)38 uint64_t GetCurrentTimeSec(void)
39 {
40 return static_cast<uint64_t>(duration_cast<seconds>(system_clock::now().time_since_epoch()).count());
41 }
42
NewUserSample(AuthUserInfo & user)43 void NewUserSample(AuthUserInfo& user)
44 {
45 user.authAccount = "allowAccountA";
46 user.authPerm = FULL_CONTROL;
47 user.permExpiryTime = GetCurrentTimeSec() + EXPIRY_TEN_MINUTE;
48 user.authAccountType = CLOUD_ACCOUNT;
49 }
50
InitNormalPolicy(std::shared_ptr<PermissionPolicy> & policy)51 void InitNormalPolicy(std::shared_ptr<PermissionPolicy>& policy)
52 {
53 policy->ownerAccount_ = "testAccount";
54 policy->ownerAccountId_ = "testAccountId";
55 policy->ownerAccountType_ = CLOUD_ACCOUNT;
56 policy->aeskey_ = new (std::nothrow) uint8_t[16];
57 policy->aeskeyLen_ = AES_KEY_LEN;
58 policy->iv_ = new (std::nothrow) uint8_t[16];
59 policy->ivLen_ = IV_LEN;
60
61 AuthUserInfo user;
62 NewUserSample(user);
63 policy->authUsers_.emplace_back(user);
64 }
65 }
66
SetUpTestCase()67 void PermissionPolicyTest::SetUpTestCase() {}
68
TearDownTestCase()69 void PermissionPolicyTest::TearDownTestCase() {}
70
SetUp()71 void PermissionPolicyTest::SetUp() {}
72
TearDown()73 void PermissionPolicyTest::TearDown() {}
74
75 /**
76 * @tc.name: PermissionPolicy001
77 * @tc.desc: PermissionPolicy construct test
78 * @tc.type: FUNC
79 * @tc.require:
80 */
81 HWTEST_F(PermissionPolicyTest, PermissionPolicyConstruct001, TestSize.Level1)
82 {
83 DLP_LOG_INFO(LABEL, "PermissionPolicyConstruct001");
84 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
85 ASSERT_NE(policy, nullptr);
86 policy = nullptr;
87 }
88
89 /**
90 ,* @tc.name: IsValid001
91 * @tc.desc: IsValid normal test
92 * @tc.type: FUNC
93 * @tc.require:
94 */
95 HWTEST_F(PermissionPolicyTest, IsValid001, TestSize.Level1)
96 {
97 DLP_LOG_INFO(LABEL, "IsValid001");
98
99 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
100 ASSERT_NE(policy, nullptr);
101
102 InitNormalPolicy(policy);
103
104 ASSERT_TRUE(policy->IsValid());
105 }
106
107 /**
108 * @tc.name: IsValid002
109 * @tc.desc: IsValid owner abnormal test
110 * @tc.type: FUNC
111 * @tc.require:
112 */
113 HWTEST_F(PermissionPolicyTest, IsValid002, TestSize.Level1)
114 {
115 DLP_LOG_INFO(LABEL, "IsValid002");
116 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
117 ASSERT_NE(policy, nullptr);
118
119 InitNormalPolicy(policy);
120
121 // empty ownerAccount
122 policy->ownerAccount_ = "";
123 ASSERT_FALSE(policy->IsValid());
124
125 // ownerAccount name len > MAX_ACCOUNT_SIZE
126 std::string invalidPerm(MAX_ACCOUNT_SIZE + 1, 'a');
127 policy->ownerAccount_ = invalidPerm;
128 ASSERT_FALSE(policy->IsValid());
129 }
130
131 /**
132 * @tc.name: IsValid003
133 * @tc.desc: IsValid account type abnormal test
134 * @tc.type: FUNC
135 * @tc.require:
136 */
137 HWTEST_F(PermissionPolicyTest, IsValid003, TestSize.Level1)
138 {
139 DLP_LOG_INFO(LABEL, "IsValid003");
140 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
141 ASSERT_NE(policy, nullptr);
142
143 InitNormalPolicy(policy);
144
145 // account INVALID_ACCOUNT
146 policy->ownerAccountType_ = INVALID_ACCOUNT;
147 ASSERT_FALSE(policy->IsValid());
148
149 // account APPLICATION_ACCOUNT + 1
150 policy->ownerAccountType_ = static_cast<DlpAccountType>(APPLICATION_ACCOUNT + 1);
151 ASSERT_FALSE(policy->IsValid());
152 }
153
154 /**
155 * @tc.name: IsValid004
156 * @tc.desc: IsValid aes key abnormal test
157 * @tc.type: FUNC
158 * @tc.require:
159 */
160 HWTEST_F(PermissionPolicyTest, IsValid004, TestSize.Level1)
161 {
162 DLP_LOG_INFO(LABEL, "IsValid004");
163 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
164 ASSERT_NE(policy, nullptr);
165
166 InitNormalPolicy(policy);
167
168 // aeskey len is 0
169 policy->aeskeyLen_ = 0;
170 ASSERT_FALSE(policy->IsValid());
171
172 // aeskey is null
173 policy->aeskey_ = nullptr;
174 policy->aeskeyLen_ = AES_KEY_LEN;
175 ASSERT_FALSE(policy->IsValid());
176 }
177
178 /**
179 * @tc.name: IsValid005
180 * @tc.desc: IsValid iv key abnormal test
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 HWTEST_F(PermissionPolicyTest, IsValid005, TestSize.Level1)
185 {
186 DLP_LOG_INFO(LABEL, "IsValid005");
187 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
188 ASSERT_NE(policy, nullptr);
189
190 InitNormalPolicy(policy);
191
192 // iv len is 0
193 policy->ivLen_ = 0;
194 ASSERT_FALSE(policy->IsValid());
195
196 // iv is null
197 policy->iv_ = nullptr;
198 policy->ivLen_ = AES_KEY_LEN;
199 ASSERT_FALSE(policy->IsValid());
200 }
201
202 /**
203 * @tc.name: IsValid006
204 * @tc.desc: IsValid auth info key abnormal test
205 * @tc.type: FUNC
206 * @tc.require:
207 */
208 HWTEST_F(PermissionPolicyTest, IsValid006, TestSize.Level1)
209 {
210 DLP_LOG_INFO(LABEL, "IsValid006");
211 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
212 ASSERT_NE(policy, nullptr);
213
214 InitNormalPolicy(policy);
215
216 // 1. test user account
217 // auth user account empty
218 policy->authUsers_[0].authAccount = "";
219 EXPECT_FALSE(policy->IsValid());
220
221 // restore
222 policy->authUsers_[0].authAccount = "test";
223
224 // 2. test auth perm
225 // auth perm DEFAULT
226 policy->authUsers_[0].authPerm = NO_PERMISSION;
227 EXPECT_FALSE(policy->IsValid());
228
229 // restore
230 policy->authUsers_[0].authPerm = FULL_CONTROL;
231
232 // 3. test expiryTime
233 // expiryTime 0
234 policy->authUsers_[0].permExpiryTime = 0;
235 EXPECT_FALSE(policy->IsValid());
236
237 // restore
238 policy->authUsers_[0].permExpiryTime = GetCurrentTimeSec() + 1000;
239
240 // 4. test auth account type
241 policy->authUsers_[0].authAccountType = INVALID_ACCOUNT;
242 EXPECT_FALSE(policy->IsValid());
243
244 // restore
245 policy->authUsers_[0].authAccountType = APPLICATION_ACCOUNT;
246
247 // 5. max + 1 user size
248 for (unsigned int i = 0; i < MAX_ACCOUNT_NUM; i++) {
249 AuthUserInfo user;
250 NewUserSample(user);
251 policy->authUsers_.emplace_back(user);
252 }
253 EXPECT_FALSE(policy->IsValid());
254 }
255
256 /**
257 * @tc.name: IsValid007
258 * @tc.desc: IsValid owner abnormal test
259 * @tc.type: FUNC
260 * @tc.require:
261 */
262 HWTEST_F(PermissionPolicyTest, IsValid007, TestSize.Level1)
263 {
264 DLP_LOG_INFO(LABEL, "IsValid002");
265 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
266 ASSERT_NE(policy, nullptr);
267
268 InitNormalPolicy(policy);
269
270 // empty ownerAccount
271 policy->ownerAccountId_ = "";
272 ASSERT_FALSE(policy->IsValid());
273
274 // ownerAccount name len > MAX_ACCOUNT_SIZE
275 std::string invalidPerm(MAX_ACCOUNT_SIZE + 1, 'a');
276 policy->ownerAccountId_ = invalidPerm;
277 ASSERT_FALSE(policy->IsValid());
278 }
279
280 /**
281 * @tc.name: SetAesKey001
282 * @tc.desc: SetAesKey test
283 * @tc.type: FUNC
284 * @tc.require:
285 */
286
287 HWTEST_F(PermissionPolicyTest, SetAesKey001, TestSize.Level1)
288 {
289 DLP_LOG_INFO(LABEL, "SetAesKey001");
290 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
291 ASSERT_NE(policy, nullptr);
292
293 InitNormalPolicy(policy);
294
295 // set aes key len invalid
296 uint8_t tmpAeskey[AES_KEY_LEN] = {0};
297 policy->SetAeskey(tmpAeskey, AES_KEY_LEN + 1);
298 ASSERT_NE(policy->aeskeyLen_, AES_KEY_LEN + 1);
299
300 // set aes key null
301 policy->SetAeskey(nullptr, AES_KEY_LEN);
302 ASSERT_EQ(policy->aeskey_, nullptr);
303 }
304
305 /**
306 * @tc.name: SetIv001
307 * @tc.desc: SetIv test
308 * @tc.type: FUNC
309 * @tc.require:
310 */
311 HWTEST_F(PermissionPolicyTest, SetIv001, TestSize.Level1)
312 {
313 DLP_LOG_INFO(LABEL, "SetIv001");
314 std::shared_ptr<PermissionPolicy> policy = std::make_shared<PermissionPolicy>();
315 ASSERT_NE(policy, nullptr);
316
317 InitNormalPolicy(policy);
318
319 // set iv len invalid
320 uint8_t tmpIvkey[IV_LEN] = {0};
321 policy->SetIv(tmpIvkey, IV_LEN + 1);
322 ASSERT_NE(policy->ivLen_, AES_KEY_LEN + 1);
323
324 // set iv null
325 policy->SetIv(nullptr, IV_LEN);
326 ASSERT_EQ(policy->iv_, nullptr);
327 }
328
329 /**
330 * @tc.name: CopyPermissionPolicy001
331 * @tc.desc: SetIv test
332 * @tc.type: FUNC
333 * @tc.require:
334 */
335 HWTEST_F(PermissionPolicyTest, CopyPermissionPolicy001, TestSize.Level1)
336 {
337 DLP_LOG_INFO(LABEL, "CopyPermissionPolicy001");
338 std::shared_ptr<PermissionPolicy> policySrc = std::make_shared<PermissionPolicy>();
339 ASSERT_NE(policySrc, nullptr);
340 InitNormalPolicy(policySrc);
341
342 std::shared_ptr<PermissionPolicy> policyDest = std::make_shared<PermissionPolicy>();
343 ASSERT_NE(policyDest, nullptr);
344
345 // 1. make policySrc invalid
346 policySrc->ownerAccount_ = "";
347 policyDest->CopyPermissionPolicy(*policySrc);
348 ASSERT_EQ(policyDest->aeskey_, nullptr);
349
350 // 2. make policySrc valid
351 policySrc->ownerAccount_ = "testAccount";
352 policyDest->CopyPermissionPolicy(*policySrc);
353 ASSERT_NE(policyDest->aeskey_, nullptr);
354 }
355 } // namespace DlpPermission
356 } // namespace Security
357 } // namespace OHOS