1 /*
2 * Copyright (c) 2024 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 "set_perm_dialog_cap_test.h"
17 #include "gtest/gtest.h"
18 #include <thread>
19 #include <unistd.h>
20
21 #include "access_token.h"
22 #include "access_token_error.h"
23 #include "accesstoken_common_log.h"
24 #include "accesstoken_service_ipc_interface_code.h"
25 #include "permission_grant_info.h"
26 #include "permission_state_change_info_parcel.h"
27 #include "string_ex.h"
28 #include "test_common.h"
29 #include "tokenid_kit.h"
30 #include "token_setproc.h"
31
32 using namespace testing::ext;
33 namespace OHOS {
34 namespace Security {
35 namespace AccessToken {
36 namespace {
37 static uint64_t g_selfTokenId = 0;
38 PermissionStateFull g_permTestState = {
39 .permissionName = "ohos.permission.LOCATION",
40 .isGeneral = true,
41 .resDeviceID = {"local"},
42 .grantStatus = {PermissionState::PERMISSION_DENIED},
43 .grantFlags = {PermissionFlag::PERMISSION_DEFAULT_FLAG},
44 };
45
46 HapInfoParams g_infoManager = {
47 .userID = 1,
48 .bundleName = "accesstoken_test",
49 .instIndex = 0,
50 .appIDDesc = "test2",
51 .apiVersion = 8 // 8: api version
52 };
53
54 HapPolicyParams g_policy = {
55 .apl = APL_NORMAL,
56 .domain = "domain",
57 .permStateList = {g_permTestState}
58 };
59 }
60
SetUpTestCase()61 void SetPermDialogCapTest::SetUpTestCase()
62 {
63 g_selfTokenId = GetSelfTokenID();
64 TestCommon::SetTestEvironment(g_selfTokenId);
65 }
66
TearDownTestCase()67 void SetPermDialogCapTest::TearDownTestCase()
68 {
69 SetSelfTokenID(g_selfTokenId);
70 TestCommon::ResetTestEvironment();
71 }
72
SetUp()73 void SetPermDialogCapTest::SetUp()
74 {
75 }
76
TearDown()77 void SetPermDialogCapTest::TearDown()
78 {
79 }
80
81 /**
82 * @tc.name: SetPermDialogCapAbnormalTest001
83 * @tc.desc: Set permission dialog capability with noexist app.
84 * @tc.type: FUNC
85 * @tc.require: Issue Number
86 */
87 HWTEST_F(SetPermDialogCapTest, SetPermDialogCapAbnormalTest001, TestSize.Level1)
88 {
89 std::vector<std::string> reqPerm;
90 reqPerm.emplace_back("ohos.permission.DISABLE_PERMISSION_DIALOG");
91 MockHapToken mock("SetPermDialogCapAbnormalTest001", reqPerm, true);
92
93 LOGI(ATM_DOMAIN, ATM_TAG, "SetPermDialogCapAbnormalTest001");
94 HapBaseInfo hapBaseInfo = {
95 .userID = 111, // 111: user id
96 .bundleName = "noexist bundle",
97 .instIndex = 0,
98 };
99
100 HapTokenInfo hapInfo;
101 ASSERT_EQ(ERR_TOKENID_NOT_EXIST, AccessTokenKit::SetPermDialogCap(hapBaseInfo, true));
102 }
103
104 /**
105 * @tc.name: SetPermDialogCapFuncTest001
106 * @tc.desc: Set permission dialog capability, and get set permissionState.
107 * @tc.type: FUNC
108 * @tc.require:
109 */
110 HWTEST_F(SetPermDialogCapTest, SetPermDialogCapFuncTest001, TestSize.Level1)
111 {
112 std::vector<std::string> reqPerm;
113 reqPerm.emplace_back("ohos.permission.DISABLE_PERMISSION_DIALOG");
114 MockHapToken mock("SetPermDialogCapAbnormalTest001", reqPerm, true);
115 uint64_t selfToken = GetSelfTokenID();
116
117 LOGI(ATM_DOMAIN, ATM_TAG, "SetPermDialogCapFuncTest001");
118 AccessTokenIDEx tokenIdEx = {0};
119 ASSERT_EQ(RET_SUCCESS, TestCommon::AllocTestHapToken(g_infoManager, g_policy, tokenIdEx));
120 ASSERT_NE(INVALID_TOKENID, tokenIdEx.tokenIDEx);
121 HapBaseInfo hapBaseInfo = {
122 .userID = g_infoManager.userID,
123 .bundleName = g_infoManager.bundleName,
124 .instIndex = g_infoManager.instIndex,
125 };
126
127 std::vector<PermissionListState> permsList;
128 PermissionListState tmp = {
129 .permissionName = g_policy.permStateList[0].permissionName,
130 .state = PASS_OPER
131 };
132 permsList.emplace_back(tmp);
133
134 // test dialog is forbiddedn
135 ASSERT_EQ(RET_SUCCESS, AccessTokenKit::SetPermDialogCap(hapBaseInfo, true));
136 ASSERT_EQ(RET_SUCCESS, SetSelfTokenID(tokenIdEx.tokenIDEx));
137 PermissionGrantInfo info;
138 ASSERT_EQ(FORBIDDEN_OPER, AccessTokenKit::GetSelfPermissionsState(permsList, info));
139 ASSERT_EQ(RET_SUCCESS, SetSelfTokenID(selfToken));
140
141 // test dialog is not forbiddedn
142 ASSERT_EQ(RET_SUCCESS, AccessTokenKit::SetPermDialogCap(hapBaseInfo, false));
143 ASSERT_EQ(RET_SUCCESS, SetSelfTokenID(tokenIdEx.tokenIDEx));
144 ASSERT_NE(FORBIDDEN_OPER, AccessTokenKit::GetSelfPermissionsState(permsList, info));
145 ASSERT_EQ(RET_SUCCESS, SetSelfTokenID(selfToken));
146
147 ASSERT_EQ(RET_SUCCESS, TestCommon::DeleteTestHapToken(tokenIdEx.tokenIdExStruct.tokenID));
148 }
149 } // namespace AccessToken
150 } // namespace Security
151 } // namespace OHOS