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 <gtest/gtest.h>
17 #include "accesstoken_kit.h"
18 #define private public
19 #include "account_iam_service.h"
20 #undef private
21 #include "account_iam_callback_stub.h"
22 #include "account_log_wrapper.h"
23 #include "iremote_stub.h"
24 #include "token_setproc.h"
25
26 namespace OHOS {
27 namespace AccountTest {
28
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace OHOS::AccountSA;
32 using namespace OHOS::Security::AccessToken;
33 namespace {
34 constexpr int32_t TEST_DEFAULT_ID = -1;
35 constexpr int32_t TEST_EXIST_ID = 100;
36 constexpr int32_t TEST_NOT_EXIST_ID = 1;
37 }
38
39 class MockIIDMCallback : public IDMCallbackStub {
40 public:
OnAcquireInfo(int32_t module,uint32_t acquireInfo,const Attributes & extraInfo)41 void OnAcquireInfo(int32_t module, uint32_t acquireInfo, const Attributes &extraInfo) override
42 {
43 return;
44 }
OnResult(int32_t result,const Attributes & extraInfo)45 void OnResult(int32_t result, const Attributes &extraInfo) override
46 {
47 result_ = result;
48 return;
49 }
50
51 public:
52 int32_t result_ = -1;
53 };
54
55 class MockGetCredInfoCallback : public GetCredInfoCallbackStub {
56 public:
OnCredentialInfo(const std::vector<CredentialInfo> & infoList)57 void OnCredentialInfo(const std::vector<CredentialInfo> &infoList)override
58 {
59 return;
60 }
61 };
62
63 class MockGetEnrolledIdCallback : public GetEnrolledIdCallbackStub {
64 public:
OnEnrolledId(int32_t result,uint64_t enrolledId)65 void OnEnrolledId(int32_t result, uint64_t enrolledId) override
66 {
67 result_ = result;
68 return;
69 }
70 public:
71 int32_t result_ = -1;
72 };
73
74 class MockGetSetPropCallback : public GetSetPropCallbackStub {
75 public:
OnResult(int32_t result,const Attributes & extraInfo)76 void OnResult(int32_t result, const Attributes &extraInfo) override
77 {
78 result_ = result;
79 return;
80 }
81
82 public:
83 int32_t result_ = -1;
84 };
85
86 class AccountIamServiceTest : public testing::Test {
87 public:
88 static void SetUpTestCase(void);
89 static void TearDownTestCase(void);
90 void SetUp(void) override;
91 void TearDown(void) override;
92
93 sptr<AccountIAMService> accountIAMService_ = nullptr;
94 };
95
SetUpTestCase(void)96 void AccountIamServiceTest::SetUpTestCase(void)
97 {
98 AccessTokenID tokenId = AccessTokenKit::GetHapTokenID(100, "com.ohos.settings", 0);
99 SetSelfTokenID(tokenId);
100 }
101
TearDownTestCase(void)102 void AccountIamServiceTest::TearDownTestCase(void)
103 {}
104
SetUp(void)105 void AccountIamServiceTest::SetUp(void) __attribute__((no_sanitize("cfi")))
106 {
107 testing::UnitTest *test = testing::UnitTest::GetInstance();
108 ASSERT_NE(test, nullptr);
109 const testing::TestInfo *testinfo = test->current_test_info();
110 ASSERT_NE(testinfo, nullptr);
111 string testCaseName = string(testinfo->name());
112 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
113
114 if (accountIAMService_ == nullptr) {
115 accountIAMService_ = new (std::nothrow) AccountIAMService();
116 }
117 }
118
TearDown(void)119 void AccountIamServiceTest::TearDown(void)
120 {}
121
122 /**
123 * @tc.name: AccountIAMService_OpenSession_0100
124 * @tc.desc: OpenSession test.
125 * @tc.type: FUNC
126 * @tc.require:
127 */
128 HWTEST_F(AccountIamServiceTest, AccountIAMService_OpenSession_0100, TestSize.Level0)
129 {
130 std::vector<uint8_t> challenge;
131 int32_t res = accountIAMService_->OpenSession(-1, challenge);
132 EXPECT_EQ(res, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
133
134 res = accountIAMService_->OpenSession(0, challenge);
135 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_ACCOUNT_IS_RESTRICTED);
136 }
137
138 /**
139 * @tc.name: AccountIAMService_OpenSession_0200
140 * @tc.desc: OpenSession test account not exist.
141 * @tc.type: FUNC
142 * @tc.require:
143 */
144 HWTEST_F(AccountIamServiceTest, AccountIAMService_OpenSession_0200, TestSize.Level0)
145 {
146 std::vector<uint8_t> challenge;
147 int32_t res = accountIAMService_->OpenSession(TEST_NOT_EXIST_ID, challenge);
148 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
149 }
150
151 /**
152 * @tc.name: AccountIAMService_CloseSession_0100
153 * @tc.desc: CloseSession test.
154 * @tc.type: FUNC
155 * @tc.require:
156 */
157 HWTEST_F(AccountIamServiceTest, AccountIAMService_CloseSession_0100, TestSize.Level0)
158 {
159 int32_t res = accountIAMService_->CloseSession(-1);
160 EXPECT_EQ(res, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
161
162 res = accountIAMService_->CloseSession(0);
163 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_ACCOUNT_IS_RESTRICTED);
164 }
165
166 /**
167 * @tc.name: AccountIAMService_CloseSession_0200
168 * @tc.desc: CloseSession test account not exist.
169 * @tc.type: FUNC
170 * @tc.require:
171 */
172 HWTEST_F(AccountIamServiceTest, AccountIAMService_CloseSession_0200, TestSize.Level0)
173 {
174 int32_t res = accountIAMService_->CloseSession(TEST_NOT_EXIST_ID);
175 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
176 }
177
178 /**
179 * @tc.name: AccountIAMService_AddCredential_0100
180 * @tc.desc: AddCredential test.
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 HWTEST_F(AccountIamServiceTest, AccountIAMService_AddCredential_0100, TestSize.Level0)
185 {
186 CredentialParameters creInfo = {};
187 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
188 ASSERT_NE(callback, nullptr);
189 accountIAMService_->AddCredential(-1, creInfo, callback);
190 EXPECT_EQ(callback->result_, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
191 }
192
193 /**
194 * @tc.name: AccountIAMService_AddCredential_01001
195 * @tc.desc: AddCredential test.
196 * @tc.type: FUNC
197 * @tc.require:
198 */
199 HWTEST_F(AccountIamServiceTest, AccountIAMService_AddCredential_01001, TestSize.Level0)
200 {
201 CredentialParameters creInfo = {};
202 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
203 ASSERT_NE(callback, nullptr);
204 accountIAMService_->AddCredential(0, creInfo, callback);
205 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_ACCOUNT_IS_RESTRICTED);
206 }
207
208 /**
209 * @tc.name: AccountIAMService_AddCredential_0200
210 * @tc.desc: AddCredential test account not exist.
211 * @tc.type: FUNC
212 * @tc.require:
213 */
214 HWTEST_F(AccountIamServiceTest, AccountIAMService_AddCredential_0200, TestSize.Level0)
215 {
216 CredentialParameters creInfo = {};
217 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
218 ASSERT_NE(callback, nullptr);
219 accountIAMService_->AddCredential(TEST_NOT_EXIST_ID, creInfo, callback);
220 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
221 }
222
223 /**
224 * @tc.name: AccountIAMService_UpdateCredential_0100
225 * @tc.desc: UpdateCredential test.
226 * @tc.type: FUNC
227 * @tc.require:
228 */
229 HWTEST_F(AccountIamServiceTest, AccountIAMService_UpdateCredential_0100, TestSize.Level0)
230 {
231 CredentialParameters creInfo = {};
232 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
233 ASSERT_NE(callback, nullptr);
234 accountIAMService_->UpdateCredential(-1, creInfo, callback);
235 EXPECT_EQ(callback->result_, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
236 }
237
238 /**
239 * @tc.name: AccountIAMService_UpdateCredential_0200
240 * @tc.desc: UpdateCredential test account not exist.
241 * @tc.type: FUNC
242 * @tc.require:
243 */
244 HWTEST_F(AccountIamServiceTest, AccountIAMService_UpdateCredential_0200, TestSize.Level0)
245 {
246 CredentialParameters creInfo = {};
247 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
248 ASSERT_NE(callback, nullptr);
249 accountIAMService_->UpdateCredential(TEST_NOT_EXIST_ID, creInfo, callback);
250 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
251 }
252
253 /**
254 * @tc.name: AccountIAMService_Cancel_0100
255 * @tc.desc: Cancel test.
256 * @tc.type: FUNC
257 * @tc.require:
258 */
259 HWTEST_F(AccountIamServiceTest, AccountIAMService_Cancel_0100, TestSize.Level0)
260 {
261 int32_t res = accountIAMService_->Cancel(-1);
262 EXPECT_EQ(res, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
263 }
264
265 /**
266 * @tc.name: AccountIAMService_DelCred_0100
267 * @tc.desc: DelCred test.
268 * @tc.type: FUNC
269 * @tc.require:
270 */
271 HWTEST_F(AccountIamServiceTest, AccountIAMService_DelCred_0100, TestSize.Level0)
272 {
273 std::vector<uint8_t> token;
274 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
275 ASSERT_NE(callback, nullptr);
276 accountIAMService_->DelCred(-1, 0, token, callback);
277 EXPECT_EQ(callback->result_, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
278 }
279
280 /**
281 * @tc.name: AccountIAMService_DelUser_0100
282 * @tc.desc: DelUser test.
283 * @tc.type: FUNC
284 * @tc.require:
285 */
286 HWTEST_F(AccountIamServiceTest, AccountIAMService_DelUser_0100, TestSize.Level0)
287 {
288 std::vector<uint8_t> token;
289 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
290 ASSERT_NE(callback, nullptr);
291 accountIAMService_->DelUser(-1, token, callback);
292 EXPECT_EQ(callback->result_, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
293 }
294
295 /**
296 * @tc.name: AccountIAMService_GetCredentialInfo_0100
297 * @tc.desc: GetCredentialInfo test.
298 * @tc.type: FUNC
299 * @tc.require:
300 */
301 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetCredentialInfo_0100, TestSize.Level0)
302 {
303 sptr<MockGetCredInfoCallback> callback = new (std::nothrow) MockGetCredInfoCallback();
304 ASSERT_NE(callback, nullptr);
305 int32_t res = accountIAMService_->GetCredentialInfo(-1, AuthType::PIN, callback);
306 EXPECT_EQ(res, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
307 }
308
309 /**
310 * @tc.name: AccountIAMService_GetCredentialInfo_0200
311 * @tc.desc: GetCredentialInfo test account not exist.
312 * @tc.type: FUNC
313 * @tc.require:
314 */
315 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetCredentialInfo_0200, TestSize.Level0)
316 {
317 sptr<MockGetCredInfoCallback> callback = new (std::nothrow) MockGetCredInfoCallback();
318 ASSERT_NE(callback, nullptr);
319 int32_t res = accountIAMService_->GetCredentialInfo(TEST_NOT_EXIST_ID, AuthType::PIN, callback);
320 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
321 }
322
323 /**
324 * @tc.name: AccountIAMService_GetCredentialInfo_0300
325 * @tc.desc: GetCredentialInfo test authType is invalid.
326 * @tc.type: FUNC
327 * @tc.require:
328 */
329 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetCredentialInfo_0300, TestSize.Level0)
330 {
331 sptr<MockGetCredInfoCallback> callback = new (std::nothrow) MockGetCredInfoCallback();
332 ASSERT_NE(callback, nullptr);
333 int32_t res =
334 accountIAMService_->GetCredentialInfo(TEST_EXIST_ID, static_cast<AuthType>(IAMAuthType::TYPE_END), callback);
335 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
336 }
337
338 /**
339 * @tc.name: AccountIAMService_GetEnrolledId_0100
340 * @tc.desc: GetCredentialInfo test default account.
341 * @tc.type: FUNC
342 * @tc.require:
343 */
344 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetEnrolledId_0100, TestSize.Level0)
345 {
346 sptr<MockGetEnrolledIdCallback> callback = new (std::nothrow) MockGetEnrolledIdCallback();
347 ASSERT_NE(callback, nullptr);
348 // -1 is default account and indicates querying the current account
349 accountIAMService_->GetEnrolledId(TEST_DEFAULT_ID, AuthType::PIN, callback);
350 EXPECT_NE(callback->result_, ERR_OK);
351 }
352
353 /**
354 * @tc.name: AccountIAMService_GetEnrolledId_0200
355 * @tc.desc: GetCredentialInfo test account not exist.
356 * @tc.type: FUNC
357 * @tc.require:
358 */
359 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetEnrolledId_0200, TestSize.Level0)
360 {
361 sptr<MockGetEnrolledIdCallback> callback = new (std::nothrow) MockGetEnrolledIdCallback();
362 ASSERT_NE(callback, nullptr);
363 accountIAMService_->GetEnrolledId(TEST_NOT_EXIST_ID, AuthType::PIN, callback);
364 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
365 }
366
367 /**
368 * @tc.name: AccountIAMService_GetEnrolledId_0300
369 * @tc.desc: GetCredentialInfo test authType is invalid.
370 * @tc.type: FUNC
371 * @tc.require:
372 */
373 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetEnrolledId_0300, TestSize.Level0)
374 {
375 sptr<MockGetEnrolledIdCallback> callback = new (std::nothrow) MockGetEnrolledIdCallback();
376 ASSERT_NE(callback, nullptr);
377 accountIAMService_->GetEnrolledId(TEST_EXIST_ID, static_cast<AuthType>(IAMAuthType::TYPE_END), callback);
378 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
379 }
380
381 /**
382
383 * @tc.name: AccountIAMService_GetEnrolledId_0400
384 * @tc.desc: GetCredentialInfo test authType is not support.
385 * @tc.type: FUNC
386 * @tc.require:
387 */
388 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetEnrolledId_0400, TestSize.Level0)
389 {
390 sptr<MockGetEnrolledIdCallback> callback = new (std::nothrow) MockGetEnrolledIdCallback();
391 ASSERT_NE(callback, nullptr);
392 accountIAMService_->GetEnrolledId(TEST_EXIST_ID, static_cast<AuthType>(IAMAuthType::DOMAIN), callback);
393 EXPECT_EQ(callback->result_, ERR_ACCOUNT_IAM_UNSUPPORTED_AUTH_TYPE);
394 }
395
396 /**
397 * @tc.name: AccountIAMService_AuthUser_0100
398 * @tc.desc: AuthUser test.
399 * @tc.type: FUNC
400 * @tc.require:
401 */
402 HWTEST_F(AccountIamServiceTest, AccountIAMService_AuthUser_0100, TestSize.Level0)
403 {
404 std::vector<uint8_t> challenge;
405 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
406 ASSERT_NE(callback, nullptr);
407 AccountSA::AuthParam authParam = {
408 .userId = -1,
409 .challenge = challenge,
410 .authType = AuthType::PIN,
411 .authTrustLevel = AuthTrustLevel::ATL1
412 };
413 uint64_t contextId = 0;
414 ErrCode res = accountIAMService_->AuthUser(authParam, callback, contextId);
415 EXPECT_EQ(res, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
416 }
417
418 /**
419 * @tc.name: AccountIAMService_AuthUser_0200
420 * @tc.desc: AuthUser test account not exist.
421 * @tc.type: FUNC
422 * @tc.require:
423 */
424 HWTEST_F(AccountIamServiceTest, AccountIAMService_AuthUser_0200, TestSize.Level0)
425 {
426 std::vector<uint8_t> challenge;
427 sptr<MockIIDMCallback> callback = new (std::nothrow) MockIIDMCallback();
428 ASSERT_NE(callback, nullptr);
429 AccountSA::AuthParam authParam = {
430 .userId = TEST_NOT_EXIST_ID,
431 .challenge = challenge,
432 .authType = AuthType::PIN,
433 .authTrustLevel = AuthTrustLevel::ATL1
434 };
435 uint64_t contextId = 0;
436 ErrCode res = accountIAMService_->AuthUser(authParam, callback, contextId);
437 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
438 }
439
440 /**
441 * @tc.name: AccountIAMService_GetAvailableStatus_0100
442 * @tc.desc: GetAvailableStatus test.
443 * @tc.type: FUNC
444 * @tc.require:
445 */
446 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetAvailableStatus_0100, TestSize.Level0)
447 {
448 int32_t status;
449 int32_t res = accountIAMService_->GetAvailableStatus(static_cast<AuthType>(-1), AuthTrustLevel::ATL1, status);
450 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
451 res = accountIAMService_->GetAvailableStatus(AuthType::PIN, static_cast<AuthTrustLevel>(0), status);
452 EXPECT_EQ(res, ERR_ACCOUNT_COMMON_INVALID_PARAMETER);
453 }
454
455 /**
456 * @tc.name: AccountIAMService_GetProperty_0100
457 * @tc.desc: GetProperty test.
458 * @tc.type: FUNC
459 * @tc.require:
460 */
461 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetProperty_0100, TestSize.Level0)
462 {
463 GetPropertyRequest request;
464 sptr<MockGetSetPropCallback> callback = new (std::nothrow) MockGetSetPropCallback();
465 ASSERT_NE(callback, nullptr);
466 accountIAMService_->GetProperty(-1, request, callback);
467 EXPECT_EQ(callback->result_, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
468 }
469
470 /**
471 * @tc.name: AccountIAMService_GetProperty_0200
472 * @tc.desc: GetProperty test account not exist.
473 * @tc.type: FUNC
474 * @tc.require:
475 */
476 HWTEST_F(AccountIamServiceTest, AccountIAMService_GetProperty_0200, TestSize.Level0)
477 {
478 GetPropertyRequest request;
479 sptr<MockGetSetPropCallback> callback = new (std::nothrow) MockGetSetPropCallback();
480 ASSERT_NE(callback, nullptr);
481 accountIAMService_->GetProperty(TEST_NOT_EXIST_ID, request, callback);
482 EXPECT_EQ(callback->result_, ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR);
483 }
484
485 /**
486 * @tc.name: AccountIAMService_SetProperty_0100
487 * @tc.desc: SetProperty test.
488 * @tc.type: FUNC
489 * @tc.require:
490 */
491 HWTEST_F(AccountIamServiceTest, AccountIAMService_SetProperty_0100, TestSize.Level0)
492 {
493 SetPropertyRequest request;
494 sptr<MockGetSetPropCallback> callback = new (std::nothrow) MockGetSetPropCallback();
495 ASSERT_NE(callback, nullptr);
496 accountIAMService_->SetProperty(-1, request, callback);
497 EXPECT_EQ(callback->result_, ERR_ACCOUNT_ZIDL_ACCOUNT_SERVICE_ERROR);
498 }
499 } // namespace AccountTest
500 } // namespace OHOS