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