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 "alloc_token.h"
17
18#include <string>
19#include "access_token.h"
20#include "accesstoken_kit.h"
21#include "nativetoken_kit.h"
22#include "permission_def.h"
23#include "permission_state_full.h"
24#include "token_setproc.h"
25
26namespace OHOS {
27    const std::string SET_POLICY_PERMISSION = "ohos.permission.SET_SANDBOX_POLICY";
28    const std::string ACCESS_PERSIST_PERMISSION = "ohos.permission.FILE_ACCESS_PERSIST";
29    uint64_t g_mockToken;
30    Security::AccessToken::PermissionStateFull g_testState1 = {
31        .permissionName = SET_POLICY_PERMISSION,
32        .isGeneral = true,
33        .resDeviceID = {"1"},
34        .grantStatus = {0},
35        .grantFlags = {0},
36    };
37    Security::AccessToken::PermissionStateFull g_testState2 = {
38        .permissionName = ACCESS_PERSIST_PERMISSION,
39        .isGeneral = true,
40        .resDeviceID = {"1"},
41        .grantStatus = {0},
42        .grantFlags = {0},
43    };
44    Security::AccessToken::HapInfoParams g_testInfoParms = {
45        .userID = 1,
46        .bundleName = "sandbox_manager_test",
47        .instIndex = 0,
48        .appIDDesc = "test"
49    };
50
51    Security::AccessToken::HapPolicyParams g_testPolicyPrams = {
52        .apl = Security::AccessToken::APL_NORMAL,
53        .domain = "test.domain",
54        .permList = {},
55        .permStateList = {g_testState1, g_testState2}
56    };
57
58    bool AllocTokenWithFuzz(const uint8_t *data, size_t size, bool(*func)(const uint8_t *, size_t))
59    {
60        AllocToken();
61        bool ret = func(data, size);
62        DeleteToken();
63        return ret;
64    }
65
66    void AllocToken()
67    {
68        Security::AccessToken::AccessTokenIDEx tokenIdEx = {0};
69        tokenIdEx = Security::AccessToken::AccessTokenKit::AllocHapToken(g_testInfoParms, g_testPolicyPrams);
70        g_mockToken = tokenIdEx.tokenIdExStruct.tokenID;
71        SetSelfTokenID(tokenIdEx.tokenIdExStruct.tokenID);
72    }
73
74    void DeleteToken()
75    {
76        Security::AccessToken::AccessTokenKit::DeleteToken(g_mockToken);
77    }
78}
79
80