1 /*
2 * Copyright (c) 1922 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("RequestPermissionOnSettingTest", 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 GTEST_LOG_(INFO) << "------------2, tokenID is " << GetSelfTokenID();
67 tokenID = 123; // 123: invalid token
68 ASSERT_EQ(AccessTokenError::ERR_TOKENID_NOT_EXIST, AccessTokenKit::RequestAppPermOnSetting(tokenID));
69 }
70
71 /**
72 * @tc.name: RequestAppPermOnSettingTest002
73 * @tc.desc: RequestAppPermOnSetting not system app.
74 * @tc.type: FUNC
75 * @tc.require: Issue Number
76 */
77 HWTEST_F(RequestPermissionOnSettingTest, RequestAppPermOnSettingTest002, TestSize.Level0)
78 {
79 std::vector<std::string> reqPerm;
80 MockHapToken("RequestPermissionOnSettingTest", reqPerm, false);
81
82 AccessTokenID tokenID = 123;
83 ASSERT_EQ(ERR_NOT_SYSTEM_APP, AccessTokenKit::RequestAppPermOnSetting(tokenID));
84 }
85
86 /**
87 * @tc.name: RequestAppPermOnSettingTest003
88 * @tc.desc: RequestAppPermOnSetting add hap and call function.
89 * @tc.type: FUNC
90 * @tc.require: Issue Number
91 */
92 HWTEST_F(RequestPermissionOnSettingTest, RequestAppPermOnSettingTest003, TestSize.Level0)
93 {
94 std::vector<std::string> reqPerm;
95 MockHapToken("RequestPermissionOnSettingTest", reqPerm, true);
96
97 HapInfoParams infoManager = {
98 .userID = 1,
99 .bundleName = "accesstoken_test",
100 .instIndex = 0,
101 .appIDDesc = "test2",
102 .apiVersion = 8 // 8: api version
103 };
104
105 PermissionStateFull permState = {
106 .permissionName = "ohos.permission.CAMERA",
107 .isGeneral = true,
108 .resDeviceID = {"local2"},
109 .grantStatus = {PermissionState::PERMISSION_GRANTED},
110 .grantFlags = {1}
111 };
112
113 HapPolicyParams policyPrams = {
114 .apl = APL_NORMAL,
115 .domain = "test.domain2",
116 .permStateList = {permState}
117 };
118 AccessTokenIDEx tokenIdEx = {0};
119 ASSERT_EQ(RET_SUCCESS, TestCommon::AllocTestHapToken(infoManager, policyPrams, tokenIdEx));
120 AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID;
121 ASSERT_NE(INVALID_TOKENID, tokenID);
122 AccessTokenKit::RequestAppPermOnSetting(tokenID);
123
124 ASSERT_EQ(RET_SUCCESS, TestCommon::DeleteTestHapToken(tokenID));
125 }
126
127 /**
128 * @tc.name: RequestAppPermOnSettingTest004
129 * @tc.desc: RequestAppPermOnSetting call function with self token.
130 * @tc.type: FUNC
131 * @tc.require: Issue Number
132 */
133 HWTEST_F(RequestPermissionOnSettingTest, RequestAppPermOnSettingTest004, TestSize.Level0)
134 {
135 std::vector<std::string> reqPerm;
136 MockHapToken("RequestPermissionOnSettingTest", reqPerm, true);
137
138 AccessTokenKit::RequestAppPermOnSetting(GetSelfTokenID());
139 }
140 } // namespace AccessToken
141 } // namespace Security
142 } // namespace OHOS
143