• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 
18 #include "securec.h"
19 
20 #include "defines.h"
21 #include "adaptor_time.h"
22 #include "adaptor_memory.h"
23 #include "enroll_specification_check.h"
24 #include "idm_common.h"
25 #include "token_key.h"
26 
27 extern "C" {
28     extern struct SessionInfo {
29         int32_t userId;
30         uint32_t authType;
31         uint64_t time;
32         uint64_t validAuthTokenTime;
33         uint8_t challenge[CHALLENGE_LEN];
34         uint64_t scheduleId;
35         bool isUpdate;
36         bool isScheduleValid;
37     } *g_session;
38     extern LinkedList *g_userInfoList;
39     extern UserInfo *g_currentUser;
40     extern ResultCode GenerateChallenge(uint8_t *challenge, uint32_t challengeLen);
41     extern ResultCode UserAuthTokenSign(UserAuthTokenHal *userAuthToken, HksAuthTokenKey *tokenKey);
42     extern ResultCode GetTokenDataCipherResult(const TokenDataToEncrypt *data, UserAuthTokenHal *authToken,
43         const HksAuthTokenKey *tokenKey);
44 }
45 
46 namespace OHOS {
47 namespace UserIam {
48 namespace UserAuth {
49 using namespace testing;
50 using namespace testing::ext;
51 
52 class EnrollCheckTest : public testing::Test {
53 public:
SetUpTestCase()54     static void SetUpTestCase() {};
55 
TearDownTestCase()56     static void TearDownTestCase() {};
57 
SetUp()58     void SetUp() {};
59 
TearDown()60     void TearDown() {};
61 };
62 
63 #define GENERATE_TOKEN(dataToEncrypt, userAuthTokenHal, tokenKey) \
64 { \
65     EXPECT_EQ(GetTokenDataCipherResult(&(dataToEncrypt), &(userAuthTokenHal), &(tokenKey)), RESULT_SUCCESS); \
66     EXPECT_EQ(UserAuthTokenSign(&(userAuthTokenHal), &(tokenKey)), RESULT_SUCCESS); \
67 }
68 
69 HWTEST_F(EnrollCheckTest, TestCheckIdmOperationToken_001, TestSize.Level0)
70 {
71     g_session = nullptr;
72     EXPECT_EQ(CheckIdmOperationToken(0, nullptr), RESULT_BAD_PARAM);
73     UserAuthTokenHal token = {};
74     token.tokenDataPlain.authType = FACE_AUTH;
75     EXPECT_EQ(CheckIdmOperationToken(0, &token), RESULT_BAD_MATCH);
76     TokenDataToEncrypt data = {};
77     token.tokenDataPlain.time = GetSystemTime();
78     HksAuthTokenKey tokenKey = {};
79     EXPECT_EQ(GetTokenKey(&tokenKey), RESULT_SUCCESS);
80     GENERATE_TOKEN(data, token, tokenKey);
81     EXPECT_EQ(CheckIdmOperationToken(0, &token), RESULT_VERIFY_TOKEN_FAIL);
82     token.tokenDataPlain.authType = PIN_AUTH;
83     GENERATE_TOKEN(data, token, tokenKey);
84     EXPECT_EQ(CheckIdmOperationToken(0, &token), RESULT_BAD_MATCH);
85 }
86 
87 HWTEST_F(EnrollCheckTest, TestCheckIdmOperationToken_002, TestSize.Level0)
88 {
89     g_userInfoList = nullptr;
90     struct SessionInfo session = {};
91     session.userId = 2661;
92     session.validAuthTokenTime = 100;
93     g_session = &session;
94     EXPECT_EQ(GenerateChallenge(session.challenge, CHALLENGE_LEN), RESULT_SUCCESS);
95     UserAuthTokenHal token = {};
96     token.tokenDataPlain.authType = 1;
97     token.tokenDataPlain.time = GetSystemTime();
98     TokenDataToEncrypt data = {
99         .userId = 0,
100         .secureUid = 10,
101         .enrolledId = 2,
102         .credentialId = 3,
103     };
104     EXPECT_EQ(memcpy_s(token.tokenDataPlain.challenge, CHALLENGE_LEN, session.challenge, CHALLENGE_LEN), EOK);
105     HksAuthTokenKey tokenKey = {};
106     EXPECT_EQ(GetTokenKey(&tokenKey), RESULT_SUCCESS);
107     GENERATE_TOKEN(data, token, tokenKey);
108     EXPECT_EQ(CheckIdmOperationToken(0, &token), RESULT_BAD_MATCH);
109     EXPECT_EQ(CheckIdmOperationToken(session.userId, &token), RESULT_BAD_MATCH);
110 
111     data.userId = session.userId;
112     GENERATE_TOKEN(data, token, tokenKey);
113     EXPECT_EQ(CheckIdmOperationToken(session.userId, &token), RESULT_BAD_MATCH);
114 
115     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
116     EXPECT_NE(g_userInfoList, nullptr);
117     UserInfo *userInfo = static_cast<UserInfo *>(Malloc(sizeof(UserInfo)));
118     userInfo->userId = session.userId;
119     userInfo->secUid = 20;
120     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
121     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
122     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
123     EXPECT_EQ(CheckIdmOperationToken(session.userId, &token), RESULT_BAD_MATCH);
124 
125     userInfo->secUid = 10;
126     EXPECT_EQ(CheckIdmOperationToken(session.userId, &token), RESULT_SUCCESS);
127     g_session = nullptr;
128     DestroyLinkedList(g_userInfoList);
129     g_userInfoList = nullptr;
130 }
131 
132 HWTEST_F(EnrollCheckTest, TestCheckSpecification, TestSize.Level0)
133 {
134     g_userInfoList = nullptr;
135     g_currentUser = nullptr;
136     int32_t userId = 2361;
137     uint32_t authType = 1;
138     EXPECT_EQ(CheckSpecification(userId, authType), RESULT_UNKNOWN);
139 }
140 } // namespace UserAuth
141 } // namespace UserIam
142 } // namespace OHOS
143