1 /*
2  * Copyright (c) 2022 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 #include <thread>
20 
21 #include "adaptor_time.h"
22 #include "user_auth_funcs.h"
23 
24 extern "C" {
25     extern LinkedList *g_userInfoList;
26     extern UnlockAuthResultCache g_unlockAuthResult;
27     extern ResultCode GetReuseUnlockResult(const ReuseUnlockParamHal *info, ReuseUnlockResult *reuseResult);
28     extern void CacheUnlockAuthResult(int32_t userId, const UserAuthTokenHal *unlockToken);
29     extern void SetAuthResult(uint64_t credentialId,
30         const UserAuthContext *context, const ExecutorResultInfo *info, AuthResult *result);
31 }
32 
33 namespace OHOS {
34 namespace UserIam {
35 namespace UserAuth {
36 using namespace testing;
37 using namespace testing::ext;
38 
39 class UserAuthFuncsTest : public testing::Test {
40 public:
SetUpTestCase()41     static void SetUpTestCase() {};
42 
TearDownTestCase()43     static void TearDownTestCase() {};
44 
SetUp()45     void SetUp() {};
46 
TearDown()47     void TearDown() {};
48 };
49 
50 HWTEST_F(UserAuthFuncsTest, TestGenerateSolutionFunc, TestSize.Level0)
51 {
52     AuthParamHal param = {};
53     EXPECT_EQ(GenerateSolutionFunc(param, nullptr), RESULT_BAD_PARAM);
54 }
55 
56 HWTEST_F(UserAuthFuncsTest, TestRequestAuthResultFunc, TestSize.Level0)
57 {
58     constexpr uint64_t contextId = 2131;
59     constexpr uint32_t bufferSize = 10;
60     EXPECT_EQ(RequestAuthResultFunc(contextId, nullptr, nullptr, nullptr), RESULT_BAD_PARAM);
61     Buffer *scheduleResult = CreateBufferBySize(bufferSize);
62     UserAuthTokenHal token = {};
63     AuthResult result = {};
64     EXPECT_EQ(RequestAuthResultFunc(contextId, scheduleResult, nullptr, nullptr), RESULT_BAD_PARAM);
65     EXPECT_EQ(RequestAuthResultFunc(contextId, scheduleResult, &token, nullptr), RESULT_BAD_PARAM);
66     result.rootSecret = CreateBufferBySize(bufferSize);
67     EXPECT_EQ(RequestAuthResultFunc(contextId, scheduleResult, &token, &result), RESULT_BAD_PARAM);
68 }
69 
70 HWTEST_F(UserAuthFuncsTest, TestSetAuthResult, TestSize.Level0)
71 {
72     uint64_t credentialId = 1;
73     UserAuthContext context = {};
74     context.userId = 123;
75     context.authType = 4;
76     ExecutorResultInfo info = {};
77     info.result = 0;
78     info.freezingTime = 0;
79     info.remainTimes = 5;
80     AuthResult result = {0};
81     SetAuthResult(credentialId, &context, &info, &result);
82     EXPECT_EQ(result.credentialId, 1);
83     EXPECT_EQ(result.userId, 123);
84     EXPECT_EQ(result.authType, 4);
85     EXPECT_EQ(result.freezingTime, 0);
86     EXPECT_EQ(result.remainTimes, 5);
87     EXPECT_EQ(result.result, 0);
88 }
89 
90 HWTEST_F(UserAuthFuncsTest, TestGetEnrolledStateFunc, TestSize.Level0)
91 {
92     constexpr int32_t userId = 1;
93     constexpr uint32_t authType = 1;
94     EnrolledStateHal enrolledStateHal = {};
95     EXPECT_EQ(GetEnrolledStateFunc(userId, authType, &enrolledStateHal), RESULT_NOT_ENROLLED);
96 }
97 
98 HWTEST_F(UserAuthFuncsTest, TestGetReuseUnlockResult, TestSize.Level0)
99 {
100     ReuseUnlockParamHal info;
101     info.reuseUnlockResultMode = AUTH_TYPE_IRRELEVANT;
102     ReuseUnlockResult reuseResult;
103     EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_GENERAL_ERROR);
104 
105     int32_t userIdCached = 0;
106     UserAuthTokenHal userAuthTokenCached;
107     userAuthTokenCached.tokenDataPlain.time = GetSystemTime() + 600;
108     userAuthTokenCached.tokenDataPlain.authType = 1;
109     userAuthTokenCached.tokenDataPlain.authTrustLevel = ATL4;
110     CacheUnlockAuthResult(userIdCached, &userAuthTokenCached);
111     EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_VERIFY_TOKEN_FAIL);
112 
113     userAuthTokenCached.tokenDataPlain.authMode = SCHEDULE_MODE_AUTH;
114     userAuthTokenCached.tokenDataPlain.tokenType = TOKEN_TYPE_LOCAL_AUTH;
115     CacheUnlockAuthResult(userIdCached, &userAuthTokenCached);
116     EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_GENERAL_ERROR);
117 
118     userAuthTokenCached.tokenDataPlain.time = GetSystemTime();
119     CacheUnlockAuthResult(userIdCached, &userAuthTokenCached);
120     EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_SUCCESS);
121     EXPECT_EQ(reuseResult.enrolledState.credentialCount, 0);
122     EXPECT_EQ(reuseResult.enrolledState.credentialDigest, 0);
123     (void)memset_s(&g_unlockAuthResult, sizeof(UnlockAuthResultCache), 0, sizeof(UnlockAuthResultCache));
124 }
125 
126 HWTEST_F(UserAuthFuncsTest, TestCheckReuseUnlockResultFunc001, TestSize.Level0)
127 {
128     ReuseUnlockParamHal info;
129     ReuseUnlockResult reuseResult;
130     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, nullptr), RESULT_BAD_PARAM);
131 
132     info.reuseUnlockResultDuration = 10;
133     info.reuseUnlockResultMode = AUTH_TYPE_IRRELEVANT;
134     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR);
135     (void)memset_s(&g_unlockAuthResult, sizeof(UnlockAuthResultCache), 0, sizeof(UnlockAuthResultCache));
136 }
137 
138 HWTEST_F(UserAuthFuncsTest, TestCheckReuseUnlockResultFunc002, TestSize.Level0)
139 {
140     int32_t userIdCached = 0;
141     UserAuthTokenHal userAuthTokenCached;
142     userAuthTokenCached.tokenDataPlain.authType = 1;
143     userAuthTokenCached.tokenDataPlain.authTrustLevel = ATL3;
144     userAuthTokenCached.tokenDataPlain.time = GetSystemTime() + 300;
145     userAuthTokenCached.tokenDataPlain.authMode = SCHEDULE_MODE_AUTH;
146     userAuthTokenCached.tokenDataPlain.tokenType = TOKEN_TYPE_LOCAL_AUTH;
147     CacheUnlockAuthResult(userIdCached, &userAuthTokenCached);
148 
149     ReuseUnlockParamHal info;
150     ReuseUnlockResult reuseResult;
151     info.reuseUnlockResultDuration = 200;
152     info.userId = 1;
153     info.authTrustLevel = ATL4;
154     info.reuseUnlockResultMode = AUTH_TYPE_IRRELEVANT;
155     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR);
156 
157     userAuthTokenCached.tokenDataPlain.time = GetSystemTime();
158     std::this_thread::sleep_for(std::chrono::milliseconds(300));
159     CacheUnlockAuthResult(userIdCached, &userAuthTokenCached);
160     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_TOKEN_TIMEOUT);
161 
162     userAuthTokenCached.tokenDataPlain.time = GetSystemTime();
163     CacheUnlockAuthResult(userIdCached, &userAuthTokenCached);
164     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR);
165 
166     info.userId = 0;
167     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR);
168 
169     info.authTrustLevel = ATL2;
170     info.authTypes[0] = 2;
171     info.authTypeSize = 1;
172     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_SUCCESS);
173 
174     info.reuseUnlockResultMode = AUTH_TYPE_RELEVANT;
175     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR);
176 
177     info.authTypes[1] = 1;
178     info.authTypeSize = 2;
179     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_SUCCESS);
180     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
181     EXPECT_NE(g_userInfoList, nullptr);
182     UserInfo userInfo = {};
183     userInfo.userId = 1;
184     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
185     EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_SUCCESS);
186     (void)memset_s(&g_unlockAuthResult, sizeof(UnlockAuthResultCache), 0, sizeof(UnlockAuthResultCache));
187 }
188 
189 HWTEST_F(UserAuthFuncsTest, TestSetGlobalConfigParamFunc, TestSize.Level0)
190 {
191     GlobalConfigParamHal param = {};
192     EXPECT_EQ(SetGlobalConfigParamFunc(nullptr), RESULT_BAD_PARAM);
193     EXPECT_EQ(SetGlobalConfigParamFunc(&param), RESULT_GENERAL_ERROR);
194 }
195 } // namespace UserAuth
196 } // namespace UserIam
197 } // namespace OHOS
198