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