1 /*
2 * Copyright (c) 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 "request_permission_on_setting_test.h"
17
18 #include "access_token.h"
19 #include "access_token_error.h"
20 #include "accesstoken_kit.h"
21 #include "test_common.h"
22 #include "token_setproc.h"
23
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Security {
28 namespace AccessToken {
29 namespace {
30 static uint64_t g_selfTokenId = 0;
31 }
SetUpTestCase()32 void RequestPermissionOnSettingTest::SetUpTestCase()
33 {
34 g_selfTokenId = GetSelfTokenID();
35 TestCommon::SetTestEvironment(g_selfTokenId);
36 }
37
TearDownTestCase()38 void RequestPermissionOnSettingTest::TearDownTestCase()
39 {
40 TestCommon::ResetTestEvironment();
41 }
42
SetUp()43 void RequestPermissionOnSettingTest::SetUp()
44 {
45 }
46
TearDown()47 void RequestPermissionOnSettingTest::TearDown()
48 {
49 }
50
51 /**
52 * @tc.name: RequestAppPermOnSettingTest001
53 * @tc.desc: RequestAppPermOnSetting invalid token.
54 * @tc.type: FUNC
55 * @tc.require: Issue
56 */
57 HWTEST_F(RequestPermissionOnSettingTest, RequestAppPermOnSettingTest001, TestSize.Level1)
58 {
59 std::vector<std::string> reqPerm;
60 MockHapToken mock("RequestAppPermOnSettingTest001", reqPerm, true);
61
62 // invalid tokenID in client
63 uint64_t tokenID = 0;
64 ASSERT_EQ(AccessTokenError::ERR_PARAM_INVALID, AccessTokenKit::RequestAppPermOnSetting(tokenID));
65
66 tokenID = 123; // 123: invalid token
67 ASSERT_EQ(AccessTokenError::ERR_TOKENID_NOT_EXIST, AccessTokenKit::RequestAppPermOnSetting(tokenID));
68 }
69
70 /**
71 * @tc.name: RequestAppPermOnSettingTest002
72 * @tc.desc: RequestAppPermOnSetting not system app.
73 * @tc.type: FUNC
74 * @tc.require: Issue Number
75 */
76 HWTEST_F(RequestPermissionOnSettingTest, RequestAppPermOnSettingTest002, TestSize.Level0)
77 {
78 std::vector<std::string> reqPerm;
79 MockHapToken mock("RequestAppPermOnSettingTest002", reqPerm, false);
80
81 AccessTokenID tokenID = 123;
82 ASSERT_EQ(ERR_NOT_SYSTEM_APP, AccessTokenKit::RequestAppPermOnSetting(tokenID));
83 }
84
85 /**
86 * @tc.name: RequestAppPermOnSettingTest003
87 * @tc.desc: RequestAppPermOnSetting add hap and call function.
88 * @tc.type: FUNC
89 * @tc.require: Issue Number
90 */
91 HWTEST_F(RequestPermissionOnSettingTest, RequestAppPermOnSettingTest003, TestSize.Level0)
92 {
93 std::vector<std::string> reqPerm;
94 MockHapToken mock("RequestAppPermOnSettingTest003", reqPerm, true);
95
96 HapInfoParams infoManager = {
97 .userID = 1,
98 .bundleName = "accesstoken_test",
99 .instIndex = 0,
100 .appIDDesc = "test2",
101 .apiVersion = 8 // 8: api version
102 };
103
104 PermissionStateFull permState = {
105 .permissionName = "ohos.permission.CAMERA",
106 .isGeneral = true,
107 .resDeviceID = {"local2"},
108 .grantStatus = {PermissionState::PERMISSION_GRANTED},
109 .grantFlags = {1}
110 };
111
112 HapPolicyParams policyPrams = {
113 .apl = APL_NORMAL,
114 .domain = "test.domain2",
115 .permStateList = {permState}
116 };
117 AccessTokenIDEx tokenIdEx = {0};
118 ASSERT_EQ(RET_SUCCESS, TestCommon::AllocTestHapToken(infoManager, policyPrams, tokenIdEx));
119 AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID;
120 ASSERT_NE(INVALID_TOKENID, tokenID);
121 AccessTokenKit::RequestAppPermOnSetting(tokenID);
122
123 EXPECT_EQ(RET_SUCCESS, TestCommon::DeleteTestHapToken(tokenID));
124 }
125 } // namespace AccessToken
126 } // namespace Security
127 } // namespace OHOS
128