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 "get_version_test.h"
17 #include <thread>
18
19 #include "access_token.h"
20 #include "access_token_error.h"
21 #include "accesstoken_common_log.h"
22 #include "iaccess_token_manager.h"
23 #include "hap_token_info.h"
24 #include "nativetoken_kit.h"
25 #include "permission_grant_info.h"
26 #include "permission_state_change_info_parcel.h"
27 #include "string_ex.h"
28 #include "tokenid_kit.h"
29 #include "token_setproc.h"
30 #include "accesstoken_manager_client.h"
31 #include "test_common.h"
32
33 using namespace testing::ext;
34
35 namespace OHOS {
36 namespace Security {
37 namespace AccessToken {
38 namespace {
39 uint64_t g_selfShellTokenId;
40 }
41
SetUpTestCase()42 void GetVersionTest::SetUpTestCase()
43 {
44 g_selfShellTokenId = GetSelfTokenID();
45 TestCommon::SetTestEvironment(g_selfShellTokenId);
46 }
47
TearDownTestCase()48 void GetVersionTest::TearDownTestCase()
49 {
50 SetSelfTokenID(g_selfShellTokenId);
51 TestCommon::ResetTestEvironment();
52 }
53
SetUp()54 void GetVersionTest::SetUp()
55 {
56 LOGI(ATM_DOMAIN, ATM_TAG, "SetUp ok.");
57 }
58
TearDown()59 void GetVersionTest::TearDown()
60 {
61 }
62
63 /**
64 * @tc.name: GetVersionFuncTest001
65 * @tc.desc: GetVersion caller is normal app.
66 * @tc.type: FUNC
67 * @tc.require: issueI5NT1X
68 */
69 HWTEST_F(GetVersionTest, GetVersionFuncTest001, TestSize.Level1)
70 {
71 LOGI(ATM_DOMAIN, ATM_TAG, "GetVersionFuncTest001");
72 std::vector<std::string> reqPerm;
73 MockHapToken mock("GetVersionFuncTest001", reqPerm, false);
74
75 uint32_t version;
76 ASSERT_EQ(ERR_NOT_SYSTEM_APP, AccessTokenKit::GetVersion(version));
77 }
78
79 /**
80 * @tc.name: GetVersionFuncTest002
81 * @tc.desc: GetVersion caller is system app.
82 * @tc.type: FUNC
83 * @tc.require: issueI5NT1X
84 */
85 HWTEST_F(GetVersionTest, GetVersionFuncTest002, TestSize.Level1)
86 {
87 LOGI(ATM_DOMAIN, ATM_TAG, "GetVersionFuncTest002");
88 std::vector<std::string> reqPerm;
89 MockHapToken mock("GetVersionFuncTest002", reqPerm, true);
90
91 uint32_t version;
92 int32_t res = AccessTokenKit::GetVersion(version);
93 ASSERT_EQ(RET_SUCCESS, res);
94 ASSERT_EQ(DEFAULT_TOKEN_VERSION, version);
95 }
96 } // namespace AccessToken
97 } // namespace Security
98 } // namespace OHOS