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