1 /*
2 * Copyright (c) 2022 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 "permission_deny_test.h"
17 #include "accesstoken_kit.h"
18 #include "on_permission_used_record_callback_stub.h"
19 #include "privacy_kit.h"
20 #include "privacy_error.h"
21 #include "token_setproc.h"
22
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 namespace {
27 static uint32_t g_selfTokenId = 0;
28 static uint64_t g_FullTokenId = 0;
29 static uint32_t g_testTokenId = 0;
30
31 static HapPolicyParams g_PolicyPrams = {
32 .apl = APL_NORMAL,
33 .domain = "test.domain",
34 };
35
36 static HapInfoParams g_InfoParms = {
37 .userID = 1,
38 .bundleName = "ohos.privacy_test.bundle",
39 .instIndex = 0,
40 .appIDDesc = "privacy_test.bundle",
41 .isSystemApp = true
42 };
43
44 }
45 using namespace testing::ext;
46
SetUpTestCase()47 void PermDenyTest::SetUpTestCase()
48 {
49 g_selfTokenId = GetSelfTokenID();
50 }
51
TearDownTestCase()52 void PermDenyTest::TearDownTestCase()
53 {
54 }
55
SetUp()56 void PermDenyTest::SetUp()
57 {
58 AccessTokenIDEx tokenIDEx = AccessTokenKit::AllocHapToken(g_InfoParms, g_PolicyPrams);
59
60 g_FullTokenId = tokenIDEx.tokenIDEx;
61 g_testTokenId = tokenIDEx.tokenIdExStruct.tokenID;
62 EXPECT_EQ(0, SetSelfTokenID(g_FullTokenId));
63 }
64
TearDown()65 void PermDenyTest::TearDown()
66 {
67 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
68 AccessTokenKit::DeleteToken(g_testTokenId);
69 PrivacyKit::RemovePermissionUsedRecords(g_testTokenId, "");
70 }
71
72 /**
73 * @tc.name: AddPermissionUsedRecord001
74 * @tc.desc: Test AddPermissionUsedRecord with no permssion.
75 * @tc.type: FUNC
76 * @tc.require: issueI5SRUO
77 */
78 HWTEST_F(PermDenyTest, AddPermissionUsedRecord001, TestSize.Level1)
79 {
80 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED,
81 PrivacyKit::AddPermissionUsedRecord(g_testTokenId, "ohos.permission.CAMERA", 1, 0));
82 }
83
84 /**
85 * @tc.name: RemovePermissionUsedRecords001
86 * @tc.desc: Test RemovePermissionUsedRecords with no permssion.
87 * @tc.type: FUNC
88 * @tc.require: issueI5SRUO
89 */
90 HWTEST_F(PermDenyTest, RemovePermissionUsedRecords001, TestSize.Level1)
91 {
92 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::RemovePermissionUsedRecords(g_testTokenId, ""));
93 }
94
95 class CbPermDenyTest : public StateCustomizedCbk {
96 public:
CbPermDenyTest()97 CbPermDenyTest()
98 {}
99
~CbPermDenyTest()100 ~CbPermDenyTest()
101 {}
102
StateChangeNotify(AccessTokenID tokenId,bool isShow)103 virtual void StateChangeNotify(AccessTokenID tokenId, bool isShow)
104 {}
105 };
106
107 /**
108 * @tc.name: StarAndStoptUsingPermission001
109 * @tc.desc: Test StartUsingPermission/StopUsingPermission with no permssion.
110 * @tc.type: FUNC
111 * @tc.require: issueI5SRUO
112 */
113 HWTEST_F(PermDenyTest, StarAndStoptUsingPermission001, TestSize.Level1)
114 {
115 auto callbackPtr = std::make_shared<CbPermDenyTest>();
116 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED,
117 PrivacyKit::StartUsingPermission(g_testTokenId, "ohos.permission.CAMERA", callbackPtr));
118 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED,
119 PrivacyKit::StartUsingPermission(g_testTokenId, "ohos.permission.CAMERA"));
120 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED,
121 PrivacyKit::StopUsingPermission(g_testTokenId, "ohos.permission.CAMERA"));
122 }
123
124 class TestCallBack : public OnPermissionUsedRecordCallbackStub {
125 public:
126 TestCallBack() = default;
127 virtual ~TestCallBack() = default;
128
OnQueried(ErrCode code,PermissionUsedResult & result)129 void OnQueried(ErrCode code, PermissionUsedResult& result)
130 {
131 GTEST_LOG_(INFO) << "TestCallBack, code :" << code << ", bundleSize :" << result.bundleRecords.size();
132 }
133 };
134
135 /**
136 * @tc.name: GetPermissionUsedRecords001
137 * @tc.desc: Test GetPermissionUsedRecords with no permssion.
138 * @tc.type: FUNC
139 * @tc.require: issueI5SRUO
140 */
141 HWTEST_F(PermDenyTest, GetPermissionUsedRecords001, TestSize.Level1)
142 {
143 PermissionUsedRequest request;
144 request.tokenId = g_testTokenId;
145 PermissionUsedResult result;
146 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::GetPermissionUsedRecords(request, result));
147
148 OHOS::sptr<TestCallBack> callback(new TestCallBack());
149 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::GetPermissionUsedRecords(request, callback));
150 }
151
152 class CbCustomizeTest : public PermActiveStatusCustomizedCbk {
153 public:
CbCustomizeTest(const std::vector<std::string> & permList)154 explicit CbCustomizeTest(const std::vector<std::string> &permList)
155 : PermActiveStatusCustomizedCbk(permList)
156 {
157 GTEST_LOG_(INFO) << "CbCustomizeTest2 create";
158 }
159
~CbCustomizeTest()160 ~CbCustomizeTest() {}
161
ActiveStatusChangeCallback(ActiveChangeResponse & result)162 virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result)
163 {
164 GTEST_LOG_(INFO) << "tokenid: " << result.tokenID <<
165 ", permissionName: " << result.permissionName <<
166 ", deviceId " << result.deviceId << ", type " << result.type;
167 }
168 };
169
170 /**
171 * @tc.name: RegisterAndUnregister001
172 * @tc.desc: Test RegisterPermActiveStatusCallback/UnRegisterPermActiveStatusCallback with no permssion.
173 * @tc.type: FUNC
174 * @tc.require: issueI5SRUO
175 */
176 HWTEST_F(PermDenyTest, RegisterAndUnregister001, TestSize.Level1)
177 {
178 std::vector<std::string> permList = {"ohos.permission.CAMERA"};
179 auto callbackPtr = std::make_shared<CbCustomizeTest>(permList);
180
181 // register success with no permission
182 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::RegisterPermActiveStatusCallback(callbackPtr));
183
184 // register success with permission
185 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
186 ASSERT_EQ(NO_ERROR, PrivacyKit::RegisterPermActiveStatusCallback(callbackPtr));
187
188 // unregister fail with no permission
189 EXPECT_EQ(0, SetSelfTokenID(g_FullTokenId));
190 ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::UnRegisterPermActiveStatusCallback(callbackPtr));
191
192 // unregister success with permission
193 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
194 ASSERT_EQ(NO_ERROR, PrivacyKit::UnRegisterPermActiveStatusCallback(callbackPtr));
195 }
196
197 /**
198 * @tc.name: IsAllowedUsingPermission001
199 * @tc.desc: Test IsAllowedUsingPermission with no permssion.
200 * @tc.type: FUNC
201 * @tc.require: issueI5SRUO
202 */
203 HWTEST_F(PermDenyTest, IsAllowedUsingPermission001, TestSize.Level1)
204 {
205 ASSERT_EQ(false, PrivacyKit::IsAllowedUsingPermission(123, "ohos.permission.CAMERA"));
206 }
207 } // namespace AccessToken
208 } // namespace Security
209 } // namespace OHOS
210
211