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 "sec_comp_perm_manager_test.h"
16
17 #include "accesstoken_kit.h"
18 #include "sec_comp_err.h"
19 #include "sec_comp_log.h"
20
21 using namespace testing::ext;
22 using namespace OHOS;
23 using namespace OHOS::Security::SecurityComponent;
24 using namespace OHOS::Security::AccessToken;
25
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
28 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompPermManagerTest"};
29 }
30
SetUpTestCase()31 void SecCompPermManagerTest::SetUpTestCase()
32 {}
33
TearDownTestCase()34 void SecCompPermManagerTest::TearDownTestCase()
35 {}
36
SetUp()37 void SecCompPermManagerTest::SetUp()
38 {
39 SC_LOG_INFO(LABEL, "setup");
40 }
41
TearDown()42 void SecCompPermManagerTest::TearDown()
43 {
44 }
45
46 /**
47 * @tc.name: DelaySaveRevokePermission001
48 * @tc.desc: Test invalid params
49 * @tc.type: FUNC
50 * @tc.require:
51 */
52 HWTEST_F(SecCompPermManagerTest, DelaySaveRevokePermission001, TestSize.Level1)
53 {
54 SecCompPermManager permMgr;
55 permMgr.secHandler_ = nullptr;
56 ASSERT_FALSE(permMgr.DelaySaveRevokePermission(static_cast<AccessTokenID>(0), ""));
57 }
58
59 /**
60 * @tc.name: RevokeSavePermissionTask001
61 * @tc.desc: Test invalid params
62 * @tc.type: FUNC
63 * @tc.require:
64 */
65 HWTEST_F(SecCompPermManagerTest, RevokeSavePermissionTask001, TestSize.Level1)
66 {
67 SecCompPermManager permMgr;
68 permMgr.secHandler_ = nullptr;
69 ASSERT_FALSE(permMgr.RevokeSavePermissionTask(""));
70 }
71
72 /**
73 * @tc.name: GrantTempSavePermission001
74 * @tc.desc: Test invalid params
75 * @tc.type: FUNC
76 * @tc.require:
77 */
78 HWTEST_F(SecCompPermManagerTest, GrantTempSavePermission001, TestSize.Level1)
79 {
80 SecCompPermManager permMgr;
81 permMgr.secHandler_ = nullptr;
82 ASSERT_EQ(permMgr.GrantTempSavePermission(static_cast<AccessTokenID>(0)),
83 SC_SERVICE_ERROR_PERMISSION_OPER_FAIL);
84 }
85
86 /**
87 * @tc.name: RevokeTempSavePermissionCount001
88 * @tc.desc: Test invalid params
89 * @tc.type: FUNC
90 * @tc.require:
91 */
92 HWTEST_F(SecCompPermManagerTest, RevokeTempSavePermissionCount001, TestSize.Level1)
93 {
94 SecCompPermManager permMgr;
95 permMgr.secHandler_ = nullptr;
96 AccessTokenID id = 0;
97 permMgr.RevokeTempSavePermissionCount(id);
98
99 permMgr.applySaveCountMap_[id] = 1;
100 permMgr.RevokeTempSavePermissionCount(static_cast<AccessTokenID>(0));
101 ASSERT_EQ(permMgr.applySaveCountMap_[id], 1);
102 }
103
104 /**
105 * @tc.name: RevokeTempSavePermission001
106 * @tc.desc: Test invalid params
107 * @tc.type: FUNC
108 * @tc.require:
109 */
110 HWTEST_F(SecCompPermManagerTest, RevokeTempSavePermission001, TestSize.Level1)
111 {
112 SecCompPermManager permMgr;
113 permMgr.secHandler_ = nullptr;
114 AccessTokenID id = 0;
115 permMgr.saveTaskDequeMap_[id].push_back("test");
116 permMgr.RevokeTempSavePermission(id);
117 ASSERT_EQ(permMgr.saveTaskDequeMap_[id].size(), static_cast<size_t>(0));
118
119 permMgr.secHandler_ = std::make_shared<SecEventHandler>(nullptr);
120 permMgr.saveTaskDequeMap_[id].push_back("test1");
121 permMgr.RevokeTempSavePermission(id);
122 permMgr.CancelAppRevokingPermisions(id);
123 ASSERT_EQ(permMgr.saveTaskDequeMap_[id].size(), static_cast<size_t>(0));
124 }
125
126 /**
127 * @tc.name: RemoveAppGrantPermissionRecord001
128 * @tc.desc: Test invalid params
129 * @tc.type: FUNC
130 * @tc.require:
131 */
132 HWTEST_F(SecCompPermManagerTest, RemoveAppGrantPermissionRecord001, TestSize.Level1)
133 {
134 SecCompPermManager permMgr;
135 AccessTokenID id = 0;
136 permMgr.RemoveAppGrantPermissionRecord(id, "test");
137
138 permMgr.grantMap_[id].insert("test");
139 permMgr.RemoveAppGrantPermissionRecord(id, "test");
140 ASSERT_EQ(permMgr.grantMap_[id].size(), static_cast<size_t>(0));
141 }
142
143 /**
144 * @tc.name: RevokeAppPermission001
145 * @tc.desc: Test invalid params
146 * @tc.type: FUNC
147 * @tc.require:
148 */
149 HWTEST_F(SecCompPermManagerTest, RevokeAppPermission001, TestSize.Level1)
150 {
151 SecCompPermManager permMgr;
152 AccessTokenID id = 0;
153 ASSERT_EQ(permMgr.RevokeAppPermission(id, "test"), 0);
154 }
155
156 /**
157 * @tc.name: VerifyPermission001
158 * @tc.desc: Test VerifyPermission
159 * @tc.type: FUNC
160 * @tc.require:
161 */
162 HWTEST_F(SecCompPermManagerTest, VerifyPermission001, TestSize.Level1)
163 {
164 SecCompPermManager permMgr;
165 AccessTokenID id = 1000;
166 ASSERT_FALSE(permMgr.VerifyPermission(id, LOCATION_COMPONENT));
167 ASSERT_FALSE(permMgr.VerifyPermission(id, PASTE_COMPONENT));
168 ASSERT_FALSE(permMgr.VerifyPermission(id, SAVE_COMPONENT));
169 ASSERT_FALSE(permMgr.VerifyPermission(id, static_cast<SecCompType>(-1)));
170 }
171