1 /*
2 * Copyright (c) 2022-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 <gmock/gmock.h>
17 #include "accesstoken_kit.h"
18 #define private public
19 #include "account_iam_callback_service.h"
20 #include "account_iam_client.h"
21 #undef private
22 #ifdef PROXY_MOCK
23 #define private public
24 #include "account_iam_service.h"
25 #include "account_iam_mgr_proxy.h"
26 #undef private
27 #endif
28 #include "account_log_wrapper.h"
29 #include "token_setproc.h"
30
31 namespace OHOS {
32 namespace AccountTest {
33 namespace {
34 const int32_t TEST_USER_ID = 200;
35 const uint64_t TEST_CONTEXT_ID = 122;
36 const std::vector<uint8_t> TEST_CHALLENGE = {1, 2, 3, 4};
37 }
38
39 using namespace testing;
40 using namespace testing::ext;
41 using namespace OHOS::AccountSA;
42 using namespace OHOS::Security::AccessToken;
43
44 class MockIDMCallback final : public IDMCallback {
45 public:
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)46 void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) override
47 {
48 return;
49 }
OnResult(int32_t result,const Attributes & extraInfo)50 void OnResult(int32_t result, const Attributes &extraInfo) override
51 {
52 result_ = result;
53 return;
54 }
55
56 public:
57 int32_t result_ = -1;
58 };
59
60 class MockGetCredInfoCallback final : public GetCredInfoCallback {
61 public:
OnCredentialInfo(int32_t result,const std::vector<CredentialInfo> & infoList)62 void OnCredentialInfo(int32_t result, const std::vector<CredentialInfo> &infoList)override
63 {
64 return;
65 }
66 };
67
68 class MockGetSetPropCallback final : public GetSetPropCallback {
69 public:
OnResult(int32_t result,const Attributes & extraInfo)70 void OnResult(int32_t result, const Attributes &extraInfo) override
71 {
72 result_ = result;
73 return;
74 }
75
76 public:
77 int32_t result_ = -1;
78 };
79
80 class MockGetEnrolledIdCallback final : public GetEnrolledIdCallback {
81 public:
OnEnrolledId(int32_t result,uint64_t enrolledId)82 void OnEnrolledId(int32_t result, uint64_t enrolledId) override
83 {
84 result_ = result;
85 return;
86 }
87
88 public:
89 int32_t result_ = -1;
90 };
91
92 #ifdef HAS_PIN_AUTH_PART
93 class MockIInputer : public OHOS::AccountSA::IInputer {
94 public:
~MockIInputer()95 virtual ~MockIInputer() {}
OnGetData(int32_t authSubType,std::vector<uint8_t> challenge,std::shared_ptr<IInputerData> inputerData)96 void OnGetData(int32_t authSubType, std::vector<uint8_t> challenge,
97 std::shared_ptr<IInputerData> inputerData) override
98 {
99 return;
100 }
101 };
102 #endif
103
104 class MockPreRemoteAuthCallback final : public AccountSA::PreRemoteAuthCallback {
105 public:
OnResult(int32_t result)106 void OnResult(int32_t result) override
107 {
108 result_ = result;
109 }
110
111 public:
112 int32_t result_ = -1;
113 };
114
115 class AccountIAMClientNoPermissionTest : public testing::Test {
116 public:
117 static void SetUpTestCase(void);
118 static void TearDownTestCase(void);
119 void SetUp(void) override;
120 void TearDown(void) override;
121 };
122
SetUpTestCase(void)123 void AccountIAMClientNoPermissionTest::SetUpTestCase(void)
124 {
125 #ifdef PROXY_MOCK
126 sptr<IAccountIAM> service = new (std::nothrow) AccountIAMService();
127 ASSERT_NE(service, nullptr);
128 AccountIAMClient::GetInstance().proxy_ = new (std::nothrow) AccountIAMMgrProxy(service->AsObject());
129 ASSERT_NE(AccountIAMClient::GetInstance().proxy_, nullptr);
130 #endif
131 }
132
TearDownTestCase(void)133 void AccountIAMClientNoPermissionTest::TearDownTestCase(void)
134 {}
135
SetUp(void)136 void AccountIAMClientNoPermissionTest::SetUp(void) __attribute__((no_sanitize("cfi")))
137 {
138 testing::UnitTest *test = testing::UnitTest::GetInstance();
139 ASSERT_NE(test, nullptr);
140 const testing::TestInfo *testinfo = test->current_test_info();
141 ASSERT_NE(testinfo, nullptr);
142 string testCaseName = string(testinfo->name());
143 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
144 }
145
TearDown(void)146 void AccountIAMClientNoPermissionTest::TearDown(void)
147 {}
148
149 /**
150 * @tc.name: AccountIAMClientNoPermission_OpenSession_0100
151 * @tc.desc: Open Session without permission.
152 * @tc.type: FUNC
153 * @tc.require:
154 */
155 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_OpenSession_0100, TestSize.Level0)
156 {
157 std::vector<uint8_t> challenge;
158 int32_t res = AccountIAMClient::GetInstance().OpenSession(0, challenge);
159 EXPECT_TRUE(res == ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
160 AccountIAMClient::GetInstance().CloseSession(0);
161 }
162
163 /**
164 * @tc.name: AccountIAMClientNoPermission_CloseSession_0100
165 * @tc.desc: Close Session without permission.
166 * @tc.type: FUNC
167 * @tc.require:
168 */
169 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_CloseSession_0100, TestSize.Level0)
170 {
171 std::vector<uint8_t> challenge;
172 int32_t res = AccountIAMClient::GetInstance().CloseSession(0);
173 EXPECT_TRUE(res == ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
174 }
175
176 /**
177 * @tc.name: AccountIAMClientNoPermission_AddCredential_0100
178 * @tc.desc: Add credential without permission.
179 * @tc.type: FUNC
180 * @tc.require:
181 */
182 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_AddCredential_0100, TestSize.Level0)
183 {
184 CredentialParameters testPara = {};
185 auto callback = std::make_shared<MockIDMCallback>();
186 ASSERT_NE(callback, nullptr);
187 AccountIAMClient::GetInstance().AddCredential(TEST_USER_ID, testPara, callback);
188 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
189 }
190
191 /**
192 * @tc.name: AccountIAMClientNoPermission_UpdateCredential_0100
193 * @tc.desc: Update credential without permission.
194 * @tc.type: FUNC
195 * @tc.require:
196 */
197 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_UpdateCredential_0100, TestSize.Level0)
198 {
199 CredentialParameters testPara = {};
200 auto callback = std::make_shared<MockIDMCallback>();
201 ASSERT_NE(callback, nullptr);
202 AccountIAMClient::GetInstance().UpdateCredential(TEST_USER_ID, testPara, callback);
203 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
204 }
205
206 /**
207 * @tc.name: AccountIAMClientNoPermission_Cancel_0100
208 * @tc.desc: Cancel without permission.
209 * @tc.type: FUNC
210 * @tc.require:
211 */
212 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_Cancel_0100, TestSize.Level0)
213 {
214 int32_t res = AccountIAMClient::GetInstance().Cancel(TEST_USER_ID);
215 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
216 }
217
218 /**
219 * @tc.name: AccountIAMClientNoPermission_DelCred_0100
220 * @tc.desc: Delete credential without permission.
221 * @tc.type: FUNC
222 * @tc.require:
223 */
224 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_DelCred_0100, TestSize.Level0)
225 {
226 uint64_t testCredentialId = 111;
227 std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
228 auto callback = std::make_shared<MockIDMCallback>();
229 ASSERT_NE(callback, nullptr);
230 AccountIAMClient::GetInstance().DelCred(TEST_USER_ID, testCredentialId, testAuthToken, callback);
231 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
232 }
233
234 /**
235 * @tc.name: AccountIAMClientNoPermission_DelUser_0100
236 * @tc.desc: Delete user without permission.
237 * @tc.type: FUNC
238 * @tc.require:
239 */
240 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_DelUser_0100, TestSize.Level0)
241 {
242 std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
243 auto callback = std::make_shared<MockIDMCallback>();
244 ASSERT_NE(callback, nullptr);
245 AccountIAMClient::GetInstance().DelUser(TEST_USER_ID, testAuthToken, callback);
246 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
247 }
248
249 /**
250 * @tc.name: AccountIAMClientNoPermission_GetCredentialInfo_0100
251 * @tc.desc: Get credential info without permission.
252 * @tc.type: FUNC
253 * @tc.require:
254 */
255 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_GetCredentialInfo_0100, TestSize.Level0)
256 {
257 auto callback = std::make_shared<MockGetCredInfoCallback>();
258 ASSERT_NE(callback, nullptr);
259 int32_t res = AccountIAMClient::GetInstance().GetCredentialInfo(TEST_USER_ID, AuthType::PIN, callback);
260 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
261 }
262
263 /**
264 * @tc.name: AccountIAMClientNoPermission_GetEnrolledId_0100
265 * @tc.desc: Get credential info without permission.
266 * @tc.type: FUNC
267 * @tc.require:
268 */
269 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_GetEnrolledId_0100, TestSize.Level0)
270 {
271 auto callback = std::make_shared<MockGetEnrolledIdCallback>();
272 ASSERT_NE(callback, nullptr);
273 AccountIAMClient::GetInstance().GetEnrolledId(TEST_USER_ID, AuthType::PIN, callback);
274 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
275 }
276
277 /**
278 * @tc.name: AccountIAMClientNoPermission_GetAvailableStatus_0100
279 * @tc.desc: Get available status without permission.
280 * @tc.type: FUNC
281 * @tc.require:
282 */
283 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_GetAvailableStatus_0100, TestSize.Level0)
284 {
285 int32_t status;
286 int32_t res = AccountIAMClient::GetInstance().GetAvailableStatus(AuthType::FACE, AuthTrustLevel::ATL1, status);
287 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
288 }
289
290 /**
291 * @tc.name: AccountIAMClientNoPermission_GetProperty_0100
292 * @tc.desc: Get property without permission.
293 * @tc.type: FUNC
294 * @tc.require:
295 */
296 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_GetProperty_0100, TestSize.Level0)
297 {
298 GetPropertyRequest testRequest = {};
299 auto callback = std::make_shared<MockGetSetPropCallback>();
300 ASSERT_NE(callback, nullptr);
301 AccountIAMClient::GetInstance().GetProperty(TEST_USER_ID, testRequest, callback);
302 }
303
304 /**
305 * @tc.name: AccountIAMClientNoPermission_SetProperty_0100
306 * @tc.desc: Set property without permission.
307 * @tc.type: FUNC
308 * @tc.require:
309 */
310 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_SetProperty_0100, TestSize.Level0)
311 {
312 SetPropertyRequest testRequest = {};
313 auto callback = std::make_shared<MockGetSetPropCallback>();
314 ASSERT_NE(callback, nullptr);
315 AccountIAMClient::GetInstance().SetProperty(TEST_USER_ID, testRequest, callback);
316 }
317
318 #ifdef HAS_PIN_AUTH_PART
319 /**
320 * @tc.name: AccountIAMClientNoPermission_RegisterInputer_0100
321 * @tc.desc: RegisterInputer without permission.
322 * @tc.type: FUNC
323 * @tc.require:
324 */
325 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_RegisterInputer_0100, TestSize.Level0)
326 {
327 std::shared_ptr<MockIInputer> inputer = std::make_shared<MockIInputer>();
328 int32_t res = AccountIAMClient::GetInstance().RegisterInputer(AuthType::PIN, inputer);
329 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
330 AccountIAMClient::GetInstance().UnregisterInputer(AuthType::PIN);
331 }
332 #endif
333
334 /**
335 * @tc.name: AccountIAMClientNoPermission_AuthUser_0100
336 * @tc.desc: Auth user without permission.
337 * @tc.type: FUNC
338 * @tc.require:
339 */
340 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_AuthUser_0100, TestSize.Level0)
341 {
342 SetPropertyRequest testRequest = {};
343 auto callback = std::make_shared<MockIDMCallback>();
344 ASSERT_NE(callback, nullptr);
345 AuthOptions authOptions;
346 authOptions.accountId = TEST_USER_ID;
347 int32_t res = AccountIAMClient::GetInstance().AuthUser(
348 authOptions, TEST_CHALLENGE, AuthType::PIN, AuthTrustLevel::ATL1, callback);
349 EXPECT_EQ(res, 0);
350 }
351
352 /**
353 * @tc.name: AccountIAMClientNoPermission_CancelAuth_0100
354 * @tc.desc: Cancel authentication without permission.
355 * @tc.type: FUNC
356 * @tc.require:
357 */
358 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_CancelAuth_0100, TestSize.Level0)
359 {
360 int32_t res = AccountIAMClient::GetInstance().CancelAuth(TEST_CONTEXT_ID);
361 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
362 }
363
364 /**
365 * @tc.name: AccountIAMClientNoPermission_prepareRemoteAuth_0100
366 * @tc.desc: prepare remote authentication without permission.
367 * @tc.type: FUNC
368 * @tc.require:
369 */
370 HWTEST_F(AccountIAMClientNoPermissionTest, AccountIAMClientNoPermission_prepareRemoteAuth_0100, TestSize.Level0)
371 {
372 auto callback = std::make_shared<MockPreRemoteAuthCallback>();
373 ASSERT_NE(callback, nullptr);
374 int32_t res = AccountIAMClient::GetInstance().PrepareRemoteAuth("testString", callback);
375 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_PERMISSION_DENIED);
376 }
377 } // namespace AccountTest
378 } // namespace OHOS