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 #include "mock_access_token.h"
16 #include "accesstoken_kit.h"
17 #include "access_token_error.h"
18 #include "nativetoken_kit.h"
19 #include "token_setproc.h"
20 #include "tokenid_kit.h"
21 #include "gtest/gtest.h"
22 
23 namespace OHOS::AppDomainVerify {
24 using namespace OHOS::Security::AccessToken;
25 static const std::string TEST_PERMISSION_GRANT = "ohos.permission.GET_APP_DOMAIN_BUNDLE_INFO";
mockSystemApp()26 int MockAccessToken::mockSystemApp()
27 {
28     HapInfoParams infoParmsEnvironment = { .userID = 1,
29         .bundleName = "InterfaceQueryPermission",
30         .instIndex = 0,
31         .dlpType = DLP_COMMON,
32         .appIDDesc = "InterfaceQueryPermission",
33         .isSystemApp = true };
34     PermissionStateFull stateGrant = { .permissionName = TEST_PERMISSION_GRANT,
35         .isGeneral = true,
36         .resDeviceID = { "local" },
37         .grantStatus = { PermissionState::PERMISSION_GRANTED },
38         .grantFlags = { 0 } };
39     HapPolicyParams policyParams = {
40         .apl = APL_NORMAL, .domain = "test.domain", .permList = {}, .permStateList = { stateGrant }
41     };
42     AccessTokenIDEx tokenIdEx = { 0 };
43     tokenIdEx = AccessTokenKit::AllocHapToken(infoParmsEnvironment, policyParams);
44     EXPECT_NE(0, tokenIdEx.tokenIdExStruct.tokenID);
45     EXPECT_EQ(true, TokenIdKit::IsSystemAppByFullTokenID(tokenIdEx.tokenIDEx));
46     EXPECT_EQ(0, SetSelfTokenID(tokenIdEx.tokenIDEx));
47     return 0;
48 }
mockSA()49 int MockAccessToken::mockSA()
50 {
51     uint64_t tokenId;
52     const char* perms[] = {};
53 
54     NativeTokenInfoParams infoInstance = {
55         .dcapsNum = 0,
56         .permsNum = static_cast<int32_t>(sizeof(perms) / sizeof(perms[0])),
57         .aclsNum = 0,
58         .dcaps = nullptr,
59         .perms = perms,
60         .acls = nullptr,
61         .aplStr = "system_core",
62     };
63     infoInstance.processName = "memmgrservice";
64     tokenId = GetAccessTokenId(&infoInstance);
65     SetSelfTokenID(tokenId);
66     Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
67     return 0;
68 }
69 }