• 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 "delete_token_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 "iaccess_token_manager.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 static const std::string TEST_BUNDLE_NAME = "ohos";
39 static const unsigned int TEST_TOKENID_INVALID = 0;
40 static const int TEST_USER_ID = 0;
41 static constexpr int32_t DEFAULT_API_VERSION = 8;
42 static const std::string TEST_PERMISSION = "ohos.permission.ALPHA";
43 static MockNativeToken* g_mock;
44 
45 HapInfoParams g_infoParms = {
46     .userID = 1,
47     .bundleName = "GetHapTokenInfoFromRemoteTest",
48     .instIndex = 0,
49     .appIDDesc = "test.bundle",
50     .apiVersion = 8,
51     .appDistributionType = "enterprise_mdm"
52 };
53 
54 HapPolicyParams g_policyPrams = {
55     .apl = APL_NORMAL,
56     .domain = "test.domain",
57 };
58 }
59 
SetUpTestCase()60 void DeleteTokenTest::SetUpTestCase()
61 {
62     g_selfTokenId = GetSelfTokenID();
63     TestCommon::SetTestEvironment(g_selfTokenId);
64 
65     // native process with MANAGER_HAP_ID
66     g_mock = new (std::nothrow) MockNativeToken("foundation");
67 
68     // clean up test cases
69     AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(TEST_USER_ID, TEST_BUNDLE_NAME, 0);
70     AccessTokenKit::DeleteToken(tokenID);
71 }
72 
TearDownTestCase()73 void DeleteTokenTest::TearDownTestCase()
74 {
75     AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(TEST_USER_ID, TEST_BUNDLE_NAME, 0);
76     AccessTokenKit::DeleteToken(tokenID);
77 
78     if (g_mock != nullptr) {
79         delete g_mock;
80         g_mock = nullptr;
81     }
82     EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
83     TestCommon::ResetTestEvironment();
84 }
85 
SetUp()86 void DeleteTokenTest::SetUp()
87 {
88     LOGI(ATM_DOMAIN, ATM_TAG, "SetUp ok.");
89     HapInfoParams info = {
90         .userID = TEST_USER_ID,
91         .bundleName = TEST_BUNDLE_NAME,
92         .instIndex = 0,
93         .appIDDesc = "appIDDesc",
94         .apiVersion = DEFAULT_API_VERSION
95     };
96 
97     HapPolicyParams policy = {
98         .apl = APL_NORMAL,
99         .domain = "domain"
100     };
101     PermissionDef permissionDefAlpha = {
102         .permissionName = TEST_PERMISSION,
103         .bundleName = TEST_BUNDLE_NAME,
104         .grantMode = GrantMode::USER_GRANT,
105         .availableLevel = APL_NORMAL,
106         .provisionEnable = false,
107         .distributedSceneEnable = false
108     };
109     policy.permList.emplace_back(permissionDefAlpha);
110     TestCommon::TestPreparePermStateList(policy);
111     AccessTokenIDEx tokenIdEx = {0};
112     ASSERT_EQ(RET_SUCCESS, AccessTokenKit::InitHapToken(info, policy, tokenIdEx));
113     ASSERT_NE(INVALID_TOKENID, tokenIdEx.tokenIdExStruct.tokenID);
114 }
115 
TearDown()116 void DeleteTokenTest::TearDown()
117 {
118     AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(TEST_USER_ID, TEST_BUNDLE_NAME, 0);
119     AccessTokenKit::DeleteToken(tokenID);
120 }
121 
122 /**
123  * @tc.name: DeleteTokenFuncTest001
124  * @tc.desc: Cannot get permission definition info after DeleteToken function has been invoked.
125  * @tc.type: FUNC
126  * @tc.require: Issue Number
127  */
128 HWTEST_F(DeleteTokenTest, DeleteTokenFuncTest001, TestSize.Level0)
129 {
130     LOGI(ATM_DOMAIN, ATM_TAG, "DeleteTokenFuncTest001");
131 
132     AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(TEST_USER_ID, TEST_BUNDLE_NAME, 0);
133     ASSERT_NE(INVALID_TOKENID, tokenID);
134 
135     PermissionDef permDefResultAlpha;
136     ASSERT_EQ(AccessTokenError::ERR_PERMISSION_NOT_EXIST, AccessTokenKit::GetDefPermission(
137         "ohos.permission.ALPHA", permDefResultAlpha));
138 
139     ASSERT_EQ(RET_SUCCESS, AccessTokenKit::DeleteToken(tokenID));
140 
141     PermissionDef defResult;
142     ASSERT_EQ(AccessTokenError::ERR_PERMISSION_NOT_EXIST, AccessTokenKit::GetDefPermission(
143         "ohos.permission.ALPHA", defResult));
144 }
145 
146 /**
147  * @tc.name: DeleteTokenFuncTest002
148  * @tc.desc: Cannot get haptoken info after DeleteToken function has been invoked.
149  * @tc.type: FUNC
150  * @tc.require: Issue Number
151  */
152 HWTEST_F(DeleteTokenTest, DeleteTokenFuncTest002, TestSize.Level0)
153 {
154     LOGI(ATM_DOMAIN, ATM_TAG, "DeleteTokenFuncTest002");
155 
156     HapTokenInfo hapTokenInfoRes;
157     AccessTokenID tokenID = AccessTokenKit::GetHapTokenID(TEST_USER_ID, TEST_BUNDLE_NAME, 0);
158     ASSERT_NE(INVALID_TOKENID, tokenID);
159 
160     int ret = AccessTokenKit::GetHapTokenInfo(tokenID, hapTokenInfoRes);
161     ASSERT_EQ(RET_SUCCESS, ret);
162 
163     ret = AccessTokenKit::DeleteToken(tokenID);
164     ASSERT_EQ(RET_SUCCESS, ret);
165 
166     ret = AccessTokenKit::GetHapTokenInfo(tokenID, hapTokenInfoRes);
167     ASSERT_EQ(AccessTokenError::ERR_TOKENID_NOT_EXIST, ret);
168 }
169 
170 /**
171  * @tc.name: DeleteTokenAbnormalTest001
172  * @tc.desc: Delete invalid tokenID.
173  * @tc.type: FUNC
174  * @tc.require: Issue Number
175  */
176 HWTEST_F(DeleteTokenTest, DeleteTokenAbnormalTest001, TestSize.Level0)
177 {
178     LOGI(ATM_DOMAIN, ATM_TAG, "DeleteTokenAbnormalTest001");
179 
180     int ret = AccessTokenKit::DeleteToken(TEST_TOKENID_INVALID);
181     ASSERT_EQ(AccessTokenError::ERR_PARAM_INVALID, ret);
182 }
183 
184 /**
185  * @tc.name: DeleteTokenAbnormalTest002
186  * @tc.desc: Delete invalid tokenID, tokenID != TOKEN_HAP
187  * @tc.type: FUNC
188  * @tc.require: Issue Number
189  */
190 HWTEST_F(DeleteTokenTest, DeleteTokenAbnormalTest002, TestSize.Level0)
191 {
192     LOGI(ATM_DOMAIN, ATM_TAG, "DeleteTokenAbnormalTest002");
193     AccessTokenID tokenID = GetSelfTokenID(); // native token
194     // tokenID != TOKEN_HAP
195     ASSERT_EQ(ERR_PARAM_INVALID, AccessTokenKit::DeleteToken(tokenID));
196 }
197 
198 /**
199  * @tc.name: DeleteTokenSpecTest001
200  * @tc.desc: alloc a tokenId successfully, delete it successfully the first time and fail to delete it again.
201  * @tc.type: FUNC
202  * @tc.require: Issue Number
203  */
204 HWTEST_F(DeleteTokenTest, DeleteTokenSpecTest001, TestSize.Level0)
205 {
206     LOGI(ATM_DOMAIN, ATM_TAG, "DeleteTokenSpecTest001");
207 
208     AccessTokenIDEx tokenIdEx = {0};
209     ASSERT_EQ(RET_SUCCESS, AccessTokenKit::InitHapToken(g_infoParms, g_policyPrams, tokenIdEx));
210     AccessTokenID tokenID = tokenIdEx.tokenIdExStruct.tokenID;
211     ASSERT_NE(INVALID_TOKENID, tokenID);
212 
213     ASSERT_EQ(RET_SUCCESS, AccessTokenKit::DeleteToken(tokenID));
214     ASSERT_NE(RET_SUCCESS, AccessTokenKit::DeleteToken(tokenID));
215 }
216 } // namespace AccessToken
217 } // namespace Security
218 } // namespace OHOS