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