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 UserInfo *g_currentUser; 27 extern UnlockAuthResultCache g_unlockAuthResult; 28 extern UnlockAuthResultCache g_anyAuthResult; 29 extern ResultCode GetReuseUnlockResult(const ReuseUnlockParamHal *info, ReuseUnlockResult *reuseResult); 30 extern void CacheUnlockAuthResult(int32_t userId, uint64_t secureUid, const UserAuthTokenHal *unlockToken); 31 extern void SetAuthResult(uint64_t credentialId, 32 const UserAuthContext *context, const ExecutorResultInfo *info, AuthResult *result); 33 extern void CacheAnyAuthResult(int32_t userId, uint64_t secureUid, const UserAuthTokenHal *unlockToken); 34 } 35 36 namespace OHOS { 37 namespace UserIam { 38 namespace UserAuth { 39 using namespace testing; 40 using namespace testing::ext; 41 42 class UserAuthFuncsTest : public testing::Test { 43 public: SetUpTestCase()44 static void SetUpTestCase() {}; 45 TearDownTestCase()46 static void TearDownTestCase() {}; 47 SetUp()48 void SetUp() {}; 49 TearDown()50 void TearDown() {}; 51 }; 52 53 HWTEST_F(UserAuthFuncsTest, TestGenerateSolutionFunc, TestSize.Level0) 54 { 55 AuthParamHal param = {}; 56 EXPECT_EQ(GenerateSolutionFunc(param, nullptr), RESULT_BAD_PARAM); 57 } 58 59 HWTEST_F(UserAuthFuncsTest, TestRequestAuthResultFunc, TestSize.Level0) 60 { 61 constexpr uint64_t contextId = 2131; 62 constexpr uint32_t bufferSize = 10; 63 EXPECT_EQ(RequestAuthResultFunc(contextId, nullptr, nullptr, nullptr), RESULT_BAD_PARAM); 64 Buffer *scheduleResult = CreateBufferBySize(bufferSize); 65 UserAuthTokenHal token = {}; 66 AuthResult result = {}; 67 EXPECT_EQ(RequestAuthResultFunc(contextId, scheduleResult, nullptr, nullptr), RESULT_BAD_PARAM); 68 EXPECT_EQ(RequestAuthResultFunc(contextId, scheduleResult, &token, nullptr), RESULT_BAD_PARAM); 69 result.rootSecret = CreateBufferBySize(bufferSize); 70 EXPECT_EQ(RequestAuthResultFunc(contextId, scheduleResult, &token, &result), RESULT_BAD_PARAM); 71 } 72 73 HWTEST_F(UserAuthFuncsTest, TestSetAuthResult, TestSize.Level0) 74 { 75 uint64_t credentialId = 1; 76 UserAuthContext context = {}; 77 context.userId = 123; 78 context.authType = 4; 79 ExecutorResultInfo info = {}; 80 info.result = 0; 81 info.freezingTime = 0; 82 info.remainTimes = 5; 83 AuthResult result = {0}; 84 SetAuthResult(credentialId, &context, &info, &result); 85 EXPECT_EQ(result.credentialId, 1); 86 EXPECT_EQ(result.userId, 123); 87 EXPECT_EQ(result.authType, 4); 88 EXPECT_EQ(result.freezingTime, 0); 89 EXPECT_EQ(result.remainTimes, 5); 90 EXPECT_EQ(result.result, 0); 91 } 92 93 HWTEST_F(UserAuthFuncsTest, TestGetEnrolledStateFunc, TestSize.Level0) 94 { 95 constexpr int32_t userId = 1; 96 constexpr uint32_t authType = 1; 97 EnrolledStateHal enrolledStateHal = {}; 98 EXPECT_EQ(GetEnrolledStateFunc(userId, authType, &enrolledStateHal), RESULT_NOT_ENROLLED); 99 } 100 101 HWTEST_F(UserAuthFuncsTest, TestGetReuseUnlockResult, TestSize.Level0) 102 { 103 ReuseUnlockParamHal info; 104 info.reuseUnlockResultMode = AUTH_TYPE_IRRELEVANT; 105 ReuseUnlockResult reuseResult; 106 EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_GENERAL_ERROR); 107 108 int32_t userIdCached = 0; 109 UserAuthTokenHal userAuthTokenCached; 110 userAuthTokenCached.tokenDataPlain.time = GetSystemTime() + 600; 111 userAuthTokenCached.tokenDataPlain.authType = 1; 112 userAuthTokenCached.tokenDataPlain.authTrustLevel = ATL4; 113 CacheUnlockAuthResult(userIdCached, 1, &userAuthTokenCached); 114 EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_GENERAL_ERROR); 115 116 userAuthTokenCached.tokenDataPlain.authMode = SCHEDULE_MODE_AUTH; 117 userAuthTokenCached.tokenDataPlain.tokenType = TOKEN_TYPE_LOCAL_AUTH; 118 CacheUnlockAuthResult(userIdCached, 1, &userAuthTokenCached); 119 EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_GENERAL_ERROR); 120 121 userAuthTokenCached.tokenDataPlain.time = GetSystemTime(); 122 CacheUnlockAuthResult(userIdCached, 1, &userAuthTokenCached); 123 EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_GENERAL_ERROR); 124 EXPECT_EQ(reuseResult.enrolledState.credentialCount, 0); 125 EXPECT_EQ(reuseResult.enrolledState.credentialDigest, 0); 126 (void)memset_s(&g_unlockAuthResult, sizeof(UnlockAuthResultCache), 0, sizeof(UnlockAuthResultCache)); 127 } 128 129 HWTEST_F(UserAuthFuncsTest, TestGetReuseUnlockResult_001, TestSize.Level0) 130 { 131 ReuseUnlockParamHal info; 132 info.reuseUnlockResultMode = CALLER_IRRELEVANT_AUTH_TYPE_IRRELEVANT; 133 ReuseUnlockResult reuseResult; 134 EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_GENERAL_ERROR); 135 136 UserInfo user = {}; 137 user.userId = 0; 138 user.secUid = 999; 139 g_currentUser = &user; 140 141 int32_t userIdCached = 0; 142 UserAuthTokenHal userAuthTokenCached; 143 userAuthTokenCached.tokenDataPlain.time = GetSystemTime() + 600; 144 userAuthTokenCached.tokenDataPlain.authType = 1; 145 userAuthTokenCached.tokenDataPlain.authTrustLevel = ATL4; 146 CacheAnyAuthResult(userIdCached, 1, &userAuthTokenCached); 147 EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_GENERAL_ERROR); 148 149 userAuthTokenCached.tokenDataPlain.authMode = SCHEDULE_MODE_AUTH; 150 userAuthTokenCached.tokenDataPlain.tokenType = TOKEN_TYPE_LOCAL_AUTH; 151 CacheAnyAuthResult(userIdCached, 999, &userAuthTokenCached); 152 EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_REUSE_AUTH_RESULT_FAILED); 153 154 userAuthTokenCached.tokenDataPlain.time = GetSystemTime(); 155 CacheAnyAuthResult(userIdCached, 999, &userAuthTokenCached); 156 EXPECT_EQ(GetReuseUnlockResult(&info, &reuseResult), RESULT_SUCCESS); 157 EXPECT_EQ(reuseResult.enrolledState.credentialCount, 0); 158 EXPECT_EQ(reuseResult.enrolledState.credentialDigest, 0); 159 (void)memset_s(&g_anyAuthResult, sizeof(UnlockAuthResultCache), 0, sizeof(UnlockAuthResultCache)); 160 } 161 162 HWTEST_F(UserAuthFuncsTest, TestCheckReuseUnlockResultFunc001, TestSize.Level0) 163 { 164 ReuseUnlockParamHal info; 165 ReuseUnlockResult reuseResult; 166 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, nullptr), RESULT_BAD_PARAM); 167 168 info.reuseUnlockResultDuration = 10; 169 info.reuseUnlockResultMode = AUTH_TYPE_IRRELEVANT; 170 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_REUSE_AUTH_RESULT_FAILED); 171 (void)memset_s(&g_unlockAuthResult, sizeof(UnlockAuthResultCache), 0, sizeof(UnlockAuthResultCache)); 172 } 173 174 HWTEST_F(UserAuthFuncsTest, TestCheckReuseUnlockResultFunc002, TestSize.Level0) 175 { 176 int32_t userIdCached = 0; 177 UserAuthTokenHal userAuthTokenCached; 178 userAuthTokenCached.tokenDataPlain.authType = 1; 179 userAuthTokenCached.tokenDataPlain.authTrustLevel = ATL3; 180 userAuthTokenCached.tokenDataPlain.time = GetSystemTime() + 300; 181 userAuthTokenCached.tokenDataPlain.authMode = SCHEDULE_MODE_AUTH; 182 userAuthTokenCached.tokenDataPlain.tokenType = TOKEN_TYPE_LOCAL_AUTH; 183 CacheUnlockAuthResult(userIdCached, 1, &userAuthTokenCached); 184 185 ReuseUnlockParamHal info; 186 ReuseUnlockResult reuseResult; 187 info.reuseUnlockResultDuration = 200; 188 info.userId = 1; 189 info.authTrustLevel = ATL4; 190 info.reuseUnlockResultMode = AUTH_TYPE_IRRELEVANT; 191 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR); 192 193 UserInfo user = {}; 194 user.userId = 0; 195 user.secUid = 999; 196 g_currentUser = &user; 197 198 userAuthTokenCached.tokenDataPlain.time = GetSystemTime(); 199 std::this_thread::sleep_for(std::chrono::milliseconds(300)); 200 CacheUnlockAuthResult(userIdCached, 1, &userAuthTokenCached); 201 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR); 202 203 userAuthTokenCached.tokenDataPlain.time = GetSystemTime(); 204 CacheUnlockAuthResult(userIdCached, 999, &userAuthTokenCached); 205 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR); 206 207 info.userId = 0; 208 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_REUSE_AUTH_RESULT_FAILED); 209 210 info.authTrustLevel = ATL2; 211 info.authTypes[0] = 2; 212 info.authTypeSize = 1; 213 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_SUCCESS); 214 215 info.reuseUnlockResultMode = AUTH_TYPE_RELEVANT; 216 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_REUSE_AUTH_RESULT_FAILED); 217 218 info.authTypes[1] = 1; 219 info.authTypeSize = 2; 220 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_SUCCESS); 221 g_userInfoList = CreateLinkedList(DestroyUserInfoNode); 222 EXPECT_NE(g_userInfoList, nullptr); 223 UserInfo userInfo = {}; 224 userInfo.userId = 1; 225 g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo)); 226 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_SUCCESS); 227 (void)memset_s(&g_unlockAuthResult, sizeof(UnlockAuthResultCache), 0, sizeof(UnlockAuthResultCache)); 228 g_currentUser = NULL; 229 } 230 231 HWTEST_F(UserAuthFuncsTest, TestCheckReuseUnlockResultFunc003, TestSize.Level0) 232 { 233 int32_t userIdCached = 0; 234 UserAuthTokenHal userAuthTokenCached; 235 userAuthTokenCached.tokenDataPlain.authType = 1; 236 userAuthTokenCached.tokenDataPlain.authTrustLevel = ATL3; 237 userAuthTokenCached.tokenDataPlain.time = GetSystemTime() + 300; 238 userAuthTokenCached.tokenDataPlain.authMode = SCHEDULE_MODE_AUTH; 239 userAuthTokenCached.tokenDataPlain.tokenType = TOKEN_TYPE_LOCAL_AUTH; 240 CacheAnyAuthResult(userIdCached, 1, &userAuthTokenCached); 241 242 UserInfo user = {}; 243 user.userId = 0; 244 user.secUid = 999; 245 g_currentUser = &user; 246 247 ReuseUnlockParamHal info; 248 ReuseUnlockResult reuseResult; 249 info.reuseUnlockResultDuration = 200; 250 info.userId = 1; 251 info.authTrustLevel = ATL4; 252 info.reuseUnlockResultMode = CALLER_IRRELEVANT_AUTH_TYPE_IRRELEVANT; 253 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR); 254 255 userAuthTokenCached.tokenDataPlain.time = GetSystemTime(); 256 std::this_thread::sleep_for(std::chrono::milliseconds(300)); 257 CacheAnyAuthResult(userIdCached, 999, &userAuthTokenCached); 258 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR); 259 userAuthTokenCached.tokenDataPlain.time = GetSystemTime(); 260 CacheAnyAuthResult(userIdCached, 999, &userAuthTokenCached); 261 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_GENERAL_ERROR); 262 info.userId = 0; 263 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_REUSE_AUTH_RESULT_FAILED); 264 info.authTrustLevel = ATL2; 265 info.authTypes[0] = 2; 266 info.authTypeSize = 1; 267 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_SUCCESS); 268 info.reuseUnlockResultMode = CALLER_IRRELEVANT_AUTH_TYPE_RELEVANT; 269 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_REUSE_AUTH_RESULT_FAILED); 270 info.authTypes[1] = 1; 271 info.authTypeSize = 2; 272 EXPECT_EQ(CheckReuseUnlockResultFunc(&info, &reuseResult), RESULT_SUCCESS); 273 (void)memset_s(&g_anyAuthResult, sizeof(UnlockAuthResultCache), 0, sizeof(UnlockAuthResultCache)); 274 g_currentUser = NULL; 275 } 276 277 HWTEST_F(UserAuthFuncsTest, TestSetGlobalConfigParamFunc, TestSize.Level0) 278 { 279 GlobalConfigParamHal param = {}; 280 EXPECT_EQ(SetGlobalConfigParamFunc(nullptr), RESULT_BAD_PARAM); 281 EXPECT_EQ(SetGlobalConfigParamFunc(¶m), RESULT_BAD_PARAM); 282 } 283 } // namespace UserAuth 284 } // namespace UserIam 285 } // namespace OHOS 286