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_remote_native_tokenid_test.h"
17 #include "accesstoken_kit.h"
18 #include "accesstoken_common_log.h"
19 #include "access_token_error.h"
20 #include "test_common.h"
21 #include "token_setproc.h"
22 #ifdef TOKEN_SYNC_ENABLE
23 #include "token_sync_kit_interface.h"
24 #endif
25
26 using namespace testing::ext;
27 using namespace OHOS::Security::AccessToken;
28 namespace {
29 static uint64_t g_selfTokenId = 0;
30 static AccessTokenID g_testTokenID = 0;
31
32 static HapPolicyParams g_policyPrams = {
33 .apl = APL_NORMAL,
34 .domain = "test.domain",
35 };
36
37 static HapInfoParams g_infoParms = {
38 .userID = 1,
39 .bundleName = "GetRemoteNativeTokenIDTest",
40 .instIndex = 0,
41 .appIDDesc = "test.bundle",
42 .isSystemApp = true
43 };
44
45 #ifdef TOKEN_SYNC_ENABLE
46 static const int32_t FAKE_SYNC_RET = 0xabcdef;
47 class TokenSyncCallbackImpl : public TokenSyncKitInterface {
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID) const48 int32_t GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) const override
49 {
50 return FAKE_SYNC_RET;
51 };
52
DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const53 int32_t DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const override
54 {
55 return FAKE_SYNC_RET;
56 };
57
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo) const58 int32_t UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) const override
59 {
60 return FAKE_SYNC_RET;
61 };
62 };
63 #endif
64 }
65 using namespace testing::ext;
66
SetUpTestCase()67 void GetRemoteNativeTokenIDTest::SetUpTestCase()
68 {
69 g_selfTokenId = GetSelfTokenID();
70 TestCommon::SetTestEvironment(g_selfTokenId);
71 }
72
TearDownTestCase()73 void GetRemoteNativeTokenIDTest::TearDownTestCase()
74 {
75 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
76 TestCommon::ResetTestEvironment();
77 GTEST_LOG_(INFO) << "PermStateChangeCallback, tokenID is " << GetSelfTokenID();
78 GTEST_LOG_(INFO) << "PermStateChangeCallback, uid is " << getuid();
79 }
80
SetUp()81 void GetRemoteNativeTokenIDTest::SetUp()
82 {
83 AccessTokenIDEx tokenIdEx = {0};
84 ASSERT_EQ(RET_SUCCESS, TestCommon::AllocTestHapToken(g_infoParms, g_policyPrams, tokenIdEx));
85
86 g_testTokenID = tokenIdEx.tokenIdExStruct.tokenID;
87 ASSERT_NE(INVALID_TOKENID, g_testTokenID);
88 EXPECT_EQ(0, SetSelfTokenID(g_testTokenID));
89 }
90
TearDown()91 void GetRemoteNativeTokenIDTest::TearDown()
92 {
93 AccessTokenIDEx tokenIdEx = TestCommon::GetHapTokenIdFromBundle(
94 g_infoParms.userID, g_infoParms.bundleName, g_infoParms.instIndex);
95 TestCommon::DeleteTestHapToken(tokenIdEx.tokenIdExStruct.tokenID);
96 EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
97 }
98 #ifdef TOKEN_SYNC_ENABLE
99 /**
100 * @tc.name: GetRemoteNativeTokenIDAbnormalTest001
101 * @tc.desc: GetRemoteNativeTokenID with no permission
102 * @tc.type: FUNC
103 * @tc.require:
104 */
105 HWTEST_F(GetRemoteNativeTokenIDTest, GetRemoteNativeTokenIDAbnormalTest001, TestSize.Level0)
106 {
107 LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteNativeTokenIDAbnormalTest001 start.");
108 std::string device = "device";
109 AccessTokenID tokenId = 123;
110 ASSERT_EQ(INVALID_TOKENID, AccessTokenKit::GetRemoteNativeTokenID(device, tokenId));
111 }
112 #endif