• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
70 
71 /**
72  * @tc.name: AddPermissionUsedRecord001
73  * @tc.desc: Test AddPermissionUsedRecord with no permssion.
74  * @tc.type: FUNC
75  * @tc.require: issueI5SRUO
76  */
77 HWTEST_F(PermDenyTest, AddPermissionUsedRecord001, TestSize.Level1)
78 {
79     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED,
80         PrivacyKit::AddPermissionUsedRecord(g_testTokenId, "ohos.permission.CAMERA", 1, 0));
81 }
82 
83 /**
84  * @tc.name: RemovePermissionUsedRecords001
85  * @tc.desc: Test RemovePermissionUsedRecords with no permssion.
86  * @tc.type: FUNC
87  * @tc.require: issueI5SRUO
88  */
89 HWTEST_F(PermDenyTest, RemovePermissionUsedRecords001, TestSize.Level1)
90 {
91     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::RemovePermissionUsedRecords(g_testTokenId, ""));
92 }
93 
94 class CbPermDenyTest : public StateCustomizedCbk {
95 public:
CbPermDenyTest()96     CbPermDenyTest()
97     {}
98 
~CbPermDenyTest()99     ~CbPermDenyTest()
100     {}
101 
StateChangeNotify(AccessTokenID tokenId,bool isShow)102     virtual void StateChangeNotify(AccessTokenID tokenId, bool isShow)
103     {}
104 };
105 
106 /**
107 * @tc.name: StarAndStoptUsingPermission001
108 * @tc.desc: Test StartUsingPermission/StopUsingPermission with no permssion.
109 * @tc.type: FUNC
110 * @tc.require: issueI5SRUO
111 */
112 HWTEST_F(PermDenyTest, StarAndStoptUsingPermission001, TestSize.Level1)
113 {
114     auto callbackPtr = std::make_shared<CbPermDenyTest>();
115     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED,
116         PrivacyKit::StartUsingPermission(g_testTokenId, "ohos.permission.CAMERA", callbackPtr));
117     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED,
118         PrivacyKit::StartUsingPermission(g_testTokenId, "ohos.permission.CAMERA"));
119     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED,
120         PrivacyKit::StopUsingPermission(g_testTokenId, "ohos.permission.CAMERA"));
121 }
122 
123 class TestCallBack : public OnPermissionUsedRecordCallbackStub {
124 public:
125     TestCallBack() = default;
126     virtual ~TestCallBack() = default;
127 
OnQueried(ErrCode code,PermissionUsedResult & result)128     void OnQueried(ErrCode code, PermissionUsedResult& result)
129     {
130         GTEST_LOG_(INFO) << "TestCallBack, code :" << code << ", bundleSize :" << result.bundleRecords.size();
131     }
132 };
133 
134 /**
135  * @tc.name: GetPermissionUsedRecords001
136  * @tc.desc: Test GetPermissionUsedRecords with no permssion.
137  * @tc.type: FUNC
138  * @tc.require: issueI5SRUO
139  */
140 HWTEST_F(PermDenyTest, GetPermissionUsedRecords001, TestSize.Level1)
141 {
142     PermissionUsedRequest request;
143     request.tokenId = g_testTokenId;
144     PermissionUsedResult result;
145     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::GetPermissionUsedRecords(request, result));
146 
147     OHOS::sptr<TestCallBack> callback(new TestCallBack());
148     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::GetPermissionUsedRecords(request, callback));
149 }
150 
151 class CbCustomizeTest : public PermActiveStatusCustomizedCbk {
152 public:
CbCustomizeTest(const std::vector<std::string> & permList)153     explicit CbCustomizeTest(const std::vector<std::string> &permList)
154         : PermActiveStatusCustomizedCbk(permList)
155     {
156         GTEST_LOG_(INFO) << "CbCustomizeTest2 create";
157     }
158 
~CbCustomizeTest()159     ~CbCustomizeTest() {}
160 
ActiveStatusChangeCallback(ActiveChangeResponse & result)161     virtual void ActiveStatusChangeCallback(ActiveChangeResponse& result)
162     {
163         GTEST_LOG_(INFO) << "tokenid: " << result.tokenID <<
164             ", permissionName: " << result.permissionName <<
165             ", deviceId " << result.deviceId << ", type " << result.type;
166     }
167 };
168 
169 /**
170 * @tc.name: RegisterAndUnregister001
171 * @tc.desc: Test RegisterPermActiveStatusCallback/UnRegisterPermActiveStatusCallback with no permssion.
172 * @tc.type: FUNC
173 * @tc.require: issueI5SRUO
174 */
175 HWTEST_F(PermDenyTest, RegisterAndUnregister001, TestSize.Level1)
176 {
177     std::vector<std::string> permList = {"ohos.permission.CAMERA"};
178     auto callbackPtr = std::make_shared<CbCustomizeTest>(permList);
179 
180     // register success with no permission
181     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::RegisterPermActiveStatusCallback(callbackPtr));
182 
183     // register success with permission
184     EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
185     ASSERT_EQ(NO_ERROR, PrivacyKit::RegisterPermActiveStatusCallback(callbackPtr));
186 
187     // unregister fail with no permission
188     EXPECT_EQ(0, SetSelfTokenID(g_FullTokenId));
189     ASSERT_EQ(PrivacyError::ERR_PERMISSION_DENIED, PrivacyKit::UnRegisterPermActiveStatusCallback(callbackPtr));
190 
191     // unregister success with permission
192     EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
193     ASSERT_EQ(NO_ERROR, PrivacyKit::UnRegisterPermActiveStatusCallback(callbackPtr));
194 }
195 
196 /**
197 * @tc.name: IsAllowedUsingPermission001
198 * @tc.desc: Test IsAllowedUsingPermission with no permssion.
199 * @tc.type: FUNC
200 * @tc.require: issueI5SRUO
201 */
202 HWTEST_F(PermDenyTest, IsAllowedUsingPermission001, TestSize.Level1)
203 {
204     ASSERT_EQ(false, PrivacyKit::IsAllowedUsingPermission(123, "ohos.permission.CAMERA"));
205 }
206 } // namespace AccessToken
207 } // namespace Security
208 } // namespace OHOS
209 
210