1 /*
2 * Copyright (c) 2022-2025 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 "token_sync_kit_test.h"
17
18 #include "access_token.h"
19 #include "access_token_error.h"
20 #include "accesstoken_kit.h"
21 #include "i_token_sync_manager.h"
22 #include "nativetoken_kit.h"
23 #include "token_setproc.h"
24 #include "token_sync_manager_client.h"
25
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 namespace {
32 static const int32_t TIME_500_MS = 1000 * 500; // 0.5 second
33 }
SetNativeTokenId(const std::string & process)34 static void SetNativeTokenId(const std::string &process)
35 {
36 std::string dumpInfo;
37 AtmToolsParamInfo info;
38 info.processName = process;
39 AccessTokenKit::DumpTokenInfo(info, dumpInfo);
40 size_t pos = dumpInfo.find("\"tokenID\": ");
41 if (pos == std::string::npos) {
42 return;
43 }
44 pos += std::string("\"tokenID\": ").length();
45 std::string numStr;
46 while (pos < dumpInfo.length() && std::isdigit(dumpInfo[pos])) {
47 numStr += dumpInfo[pos];
48 ++pos;
49 }
50
51 std::istringstream iss(numStr);
52 AccessTokenID tokenID;
53 iss >> tokenID;
54
55 SetSelfTokenID(tokenID);
56 }
57
StartOrStopTokenSyncService(bool start)58 static void StartOrStopTokenSyncService(bool start)
59 {
60 pid_t pid = fork();
61 int ret = 0;
62 if (pid == 0) {
63 if (start) {
64 ret = execlp("service_control", "service_control", "start", "token_sync_service", nullptr);
65 } else {
66 ret = execlp("service_control", "service_control", "stop", "token_sync_service", nullptr);
67 }
68 if (ret == -1) {
69 std::cout << "execlp failed" << std::endl;
70 }
71 exit(0);
72 }
73 usleep(TIME_500_MS);
74 }
75
SetUpTestCase()76 void TokenSyncKitTest::SetUpTestCase()
77 {}
78
TearDownTestCase()79 void TokenSyncKitTest::TearDownTestCase()
80 {}
81
SetUp()82 void TokenSyncKitTest::SetUp()
83 {
84 StartOrStopTokenSyncService(false);
85 }
86
TearDown()87 void TokenSyncKitTest::TearDown()
88 {}
89
90 /**
91 * @tc.name: UpdateRemoteHapTokenInfo001
92 * @tc.desc: TokenSyncManagerProxy::UpdateRemoteHapTokenInfo function test
93 * @tc.type: FUNC
94 * @tc.require:
95 */
96 HWTEST_F(TokenSyncKitTest, UpdateRemoteHapTokenInfo001, TestSize.Level0)
97 {
98 HapTokenInfoForSync tokenInfo;
99 uint64_t selfTokenId = GetSelfTokenID();
100
101 // proxy is nullptr
102 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_IPC_ERROR,
103 TokenSyncManagerClient::GetInstance().UpdateRemoteHapTokenInfo(tokenInfo));
104
105 StartOrStopTokenSyncService(true);
106
107 // service is starting, but no permission(shell process)
108 SetNativeTokenId("hdcd");
109 int32_t selfUid = getuid();
110 setuid(10001); // 10001: UID
111 ASSERT_EQ(ERR_IDENTITY_CHECK_FAILED, TokenSyncManagerClient::GetInstance().UpdateRemoteHapTokenInfo(tokenInfo));
112 setuid(selfUid);
113
114 // service is starting, and has permission(native process)
115 SetNativeTokenId("accesstoken_service");
116 ASSERT_EQ(0, TokenSyncManagerClient::GetInstance().UpdateRemoteHapTokenInfo(tokenInfo));
117
118 StartOrStopTokenSyncService(false);
119 SetSelfTokenID(selfTokenId);
120 }
121
122 /**
123 * @tc.name: GetRemoteHapTokenInfo001
124 * @tc.desc: TokenSyncManagerProxy::GetRemoteHapTokenInfo function test
125 * @tc.type: FUNC
126 * @tc.require:
127 */
128 HWTEST_F(TokenSyncKitTest, GetRemoteHapTokenInfo001, TestSize.Level0)
129 {
130 uint64_t selfTokenId = GetSelfTokenID();
131
132 // proxy is nullptr
133 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_IPC_ERROR,
134 TokenSyncManagerClient::GetInstance().GetRemoteHapTokenInfo("", 0));
135
136 StartOrStopTokenSyncService(true);
137
138 // service is starting, but no permission(shell process)
139 SetNativeTokenId("hdcd");
140 int32_t selfUid = getuid();
141 setuid(10001); // 10001: UID
142 ASSERT_EQ(ERR_IDENTITY_CHECK_FAILED, TokenSyncManagerClient::GetInstance().GetRemoteHapTokenInfo("", 0));
143 setuid(selfUid);
144
145 // service is starting, and has permission(native process)
146 SetNativeTokenId("accesstoken_service");
147 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_PARAMS_INVALID,
148 TokenSyncManagerClient::GetInstance().GetRemoteHapTokenInfo("", 0));
149
150 StartOrStopTokenSyncService(false);
151 SetSelfTokenID(selfTokenId);
152 }
153
154 /**
155 * @tc.name: DeleteRemoteHapTokenInfo001
156 * @tc.desc: TokenSyncManagerProxy::DeleteRemoteHapTokenInfo function test
157 * @tc.type: FUNC
158 * @tc.require:
159 */
160 HWTEST_F(TokenSyncKitTest, DeleteRemoteHapTokenInfo001, TestSize.Level0)
161 {
162 uint64_t selfTokenId = GetSelfTokenID();
163
164 // proxy is nullptr
165 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_IPC_ERROR,
166 TokenSyncManagerClient::GetInstance().DeleteRemoteHapTokenInfo(0));
167
168 StartOrStopTokenSyncService(true);
169
170 // service is starting, but no permission(shell process)
171 SetNativeTokenId("hdcd");
172 int32_t selfUid = getuid();
173 setuid(10001); // 10001: UID
174 ASSERT_EQ(ERR_IDENTITY_CHECK_FAILED, TokenSyncManagerClient::GetInstance().DeleteRemoteHapTokenInfo(0));
175 setuid(selfUid);
176
177 // service is starting, and has permission(native process)
178 SetNativeTokenId("accesstoken_service");
179 ASSERT_EQ(TokenSyncError::TOKEN_SYNC_PARAMS_INVALID,
180 TokenSyncManagerClient::GetInstance().DeleteRemoteHapTokenInfo(0));
181
182 StartOrStopTokenSyncService(false);
183 SetSelfTokenID(selfTokenId);
184 }
185 } // namespace AccessToken
186 } // namespace Security
187 } // namespace OHOS
188