• 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 "register_token_sync_callback_test.h"
17 #include <thread>
18 
19 #include "accesstoken_kit.h"
20 #include "accesstoken_common_log.h"
21 #include "access_token_error.h"
22 #include "nativetoken_kit.h"
23 #include "test_common.h"
24 #include "token_setproc.h"
25 
26 using namespace testing::ext;
27 using namespace OHOS::Security::AccessToken;
28 
29 namespace {
30 static const std::string TEST_BUNDLE_NAME = "ohos";
31 static const std::string TEST_PKG_NAME = "com.softbus.test";
32 static uint64_t g_selfTokenId = 0;
33 static AccessTokenID g_testTokenId = 0x20100000;
34 
35 HapTokenInfo g_baseInfo = {
36     .ver = 1,
37     .userID = 1,
38     .bundleName = "com.ohos.access_token",
39     .instIndex = 1,
40     .tokenID = g_testTokenId,
41     .tokenAttr = 0
42 };
43 
44 #ifdef TOKEN_SYNC_ENABLE
45 static const int32_t FAKE_SYNC_RET = 0xabcdef;
46 class TokenSyncCallbackImpl : public TokenSyncKitInterface {
47 public:
~TokenSyncCallbackImpl()48     ~TokenSyncCallbackImpl()
49     {}
50 
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID) const51     int32_t GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID) const override
52     {
53         LOGI(ATM_DOMAIN, ATM_TAG, "GetRemoteHapTokenInfo called.");
54         return FAKE_SYNC_RET;
55     };
56 
DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const57     int32_t DeleteRemoteHapTokenInfo(AccessTokenID tokenID) const override
58     {
59         LOGI(ATM_DOMAIN, ATM_TAG, "DeleteRemoteHapTokenInfo called.");
60         return FAKE_SYNC_RET;
61     };
62 
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo) const63     int32_t UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo) const override
64     {
65         LOGI(ATM_DOMAIN, ATM_TAG, "UpdateRemoteHapTokenInfo called.");
66         return FAKE_SYNC_RET;
67     };
68 };
69 #endif
70 }
71 
SetUpTestCase()72 void RegisterTokenSyncCallbackTest::SetUpTestCase()
73 {
74     g_selfTokenId = GetSelfTokenID();
75     TestCommon::SetTestEvironment(g_selfTokenId);
76 
77 #ifdef TOKEN_SYNC_ENABLE
78     {
79         MockNativeToken mock("foundation");
80         std::shared_ptr<TestDmInitCallback> ptrDmInitCallback = std::make_shared<TestDmInitCallback>();
81         int32_t res =
82             DistributedHardware::DeviceManager::GetInstance().InitDeviceManager(TEST_PKG_NAME, ptrDmInitCallback);
83         ASSERT_EQ(res, RET_SUCCESS);
84     }
85 #endif
86 }
87 
TearDownTestCase()88 void RegisterTokenSyncCallbackTest::TearDownTestCase()
89 {
90 #ifdef TOKEN_SYNC_ENABLE
91     {
92         MockNativeToken mock("foundation");
93         int32_t res = DistributedHardware::DeviceManager::GetInstance().UnInitDeviceManager(TEST_PKG_NAME);
94         ASSERT_EQ(res, RET_SUCCESS);
95     }
96 #endif
97     EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
98     TestCommon::ResetTestEvironment();
99 }
100 
SetUp()101 void RegisterTokenSyncCallbackTest::SetUp()
102 {
103 #ifdef TOKEN_SYNC_ENABLE
104     MockNativeToken mock("foundation");
105     DistributedHardware::DmDeviceInfo deviceInfo;
106     int32_t res = DistributedHardware::DeviceManager::GetInstance().GetLocalDeviceInfo(TEST_PKG_NAME, deviceInfo);
107     ASSERT_EQ(res, RET_SUCCESS);
108 
109     networkId_ = std::string(deviceInfo.networkId);
110     ASSERT_NE(networkId_, "");
111 
112     res = DistributedHardware::DeviceManager::GetInstance().GetUdidByNetworkId(TEST_PKG_NAME, networkId_, udid_);
113     ASSERT_EQ(res, RET_SUCCESS);
114     ASSERT_NE(udid_, "");
115 #endif
116 
117     LOGI(ATM_DOMAIN, ATM_TAG, "SetUp ok.");
118 }
119 
TearDown()120 void RegisterTokenSyncCallbackTest::TearDown()
121 {
122     EXPECT_EQ(0, SetSelfTokenID(g_selfTokenId));
123     udid_.clear();
124     networkId_.clear();
125 }
126 
127 #ifdef TOKEN_SYNC_ENABLE
128 /**
129  * @tc.name: RegisterTokenSyncCallbackAbnormalTest001
130  * @tc.desc: set token sync callback with invalid pointer
131  * @tc.type: FUNC
132  * @tc.require:
133  */
134 HWTEST_F(RegisterTokenSyncCallbackTest, RegisterTokenSyncCallbackAbnormalTest001, TestSize.Level1)
135 {
136     LOGI(ATM_DOMAIN, ATM_TAG, "RegisterTokenSyncCallbackAbnormalTest001 start.");
137     int32_t ret = AccessTokenKit::RegisterTokenSyncCallback(nullptr);
138     EXPECT_EQ(ERR_PARAM_INVALID, ret);
139 }
140 
141 /**
142  * @tc.name: RegisterTokenSyncCallbackAbnormalTest002
143  * @tc.desc: RegisterTokenSyncCallback with no permission
144  * @tc.type: FUNC
145  * @tc.require:
146  */
147 HWTEST_F(RegisterTokenSyncCallbackTest, RegisterTokenSyncCallbackAbnormalTest002, TestSize.Level1)
148 {
149     LOGI(ATM_DOMAIN, ATM_TAG, "RegisterTokenSyncCallbackAbnormalTest002 start.");
150     std::shared_ptr<TokenSyncKitInterface> callback = std::make_shared<TokenSyncCallbackImpl>();
151     EXPECT_EQ(AccessTokenError::ERR_PERMISSION_DENIED, AccessTokenKit::RegisterTokenSyncCallback(callback));
152     EXPECT_EQ(AccessTokenError::ERR_PERMISSION_DENIED, AccessTokenKit::UnRegisterTokenSyncCallback());
153 }
154 
155 /**
156  * @tc.name: RegisterTokenSyncCallbackFuncTest001
157  * @tc.desc: set token sync callback with right pointer
158  * @tc.type: FUNC
159  * @tc.require:
160  */
161 HWTEST_F(RegisterTokenSyncCallbackTest, RegisterTokenSyncCallbackFuncTest001, TestSize.Level1)
162 {
163     MockNativeToken mock("token_sync_service");
164     LOGI(ATM_DOMAIN, ATM_TAG, "RegisterTokenSyncCallbackFuncTest001 start.");
165     std::shared_ptr<TokenSyncKitInterface> callback = std::make_shared<TokenSyncCallbackImpl>();
166     EXPECT_EQ(RET_SUCCESS, AccessTokenKit::RegisterTokenSyncCallback(callback));
167     EXPECT_EQ(RET_SUCCESS, AccessTokenKit::AllocLocalTokenID(networkId_, 0)); // invalid input, would ret 0
168     EXPECT_EQ(RET_SUCCESS, AccessTokenKit::UnRegisterTokenSyncCallback());
169 }
170 #endif