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 "user_auth_service_test.h" 17 18 #include <future> 19 20 #include "iam_common_defines.h" 21 #include "iam_ptr.h" 22 23 #include "executor_messenger_service.h" 24 #include "accesstoken_kit.h" 25 #include "mock_auth_event_listener.h" 26 #include "mock_context.h" 27 #include "mock_iuser_auth_interface.h" 28 #include "mock_ipc_common.h" 29 #include "mock_user_auth_callback.h" 30 #include "mock_user_auth_service.h" 31 #include "mock_resource_node.h" 32 #include "mock_widget_callback_interface.h" 33 #include "resource_node_pool.h" 34 #include "user_auth_service.h" 35 36 namespace OHOS { 37 namespace UserIam { 38 namespace UserAuth { 39 using namespace testing; 40 using namespace testing::ext; 41 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById001, TestSize.Level0) 42 { 43 UserAuthService service; 44 uint64_t testCredentialId = 1; 45 std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE}; 46 sptr<MockGetExecutorPropertyCallback> testCallback(new (std::nothrow) MockGetExecutorPropertyCallback()); 47 EXPECT_NE(testCallback, nullptr); 48 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); 49 EXPECT_NE(mockHdi, nullptr); 50 EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(1); 51 EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(2); 52 sptr<GetExecutorPropertyCallbackInterface> callbackInterface = testCallback; 53 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 54 IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION); 55 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 56 IpcCommon::DeleteAllPermission(); 57 } 58 59 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById002, TestSize.Level0) 60 { 61 UserAuthService service; 62 uint64_t testCredentialId = 1; 63 std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE}; 64 sptr<MockGetExecutorPropertyCallback> testCallback(nullptr); 65 sptr<GetExecutorPropertyCallbackInterface> callbackInterface = testCallback; 66 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 67 68 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); 69 EXPECT_NE(mockHdi, nullptr); 70 EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(2); 71 ON_CALL(*mockHdi, GetCredentialById) 72 .WillByDefault( __anon424fe9cb0102(uint64_t credentialId, HdiCredentialInfo &info) 73 [](uint64_t credentialId, HdiCredentialInfo &info) { 74 HdiCredentialInfo tempInfo = { 75 .credentialId = 1, 76 .executorIndex = 2, 77 .templateId = 3, 78 .authType = static_cast<HdiAuthType>(1), 79 .executorMatcher = 2, 80 .executorSensorHint = 3, 81 }; 82 info = tempInfo; 83 return HDF_SUCCESS; 84 } 85 ); 86 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2); 87 EXPECT_NE(resourceNode, nullptr); 88 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode)); 89 MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get()); 90 EXPECT_CALL(*node, GetProperty(_, _)) 91 .Times(0) 92 .WillOnce(Return(FAIL)) 93 .WillOnce(Return(SUCCESS)); 94 testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback()); 95 EXPECT_NE(testCallback, nullptr); 96 EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(2); 97 IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION); 98 callbackInterface = testCallback; 99 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 100 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 101 EXPECT_TRUE(ResourceNodePool::Instance().Delete(2)); 102 IpcCommon::DeleteAllPermission(); 103 } 104 105 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById003, TestSize.Level0) 106 { 107 UserAuthService service; 108 uint64_t testCredentialId = 1; 109 std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_REMAIN_TIMES, Attributes::ATTR_SIGNATURE}; 110 sptr<MockGetExecutorPropertyCallback> testCallback(nullptr); 111 sptr<GetExecutorPropertyCallbackInterface> callbackInterface = testCallback; 112 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 113 114 testKeys = {Attributes::ATTR_FREEZING_TIME, Attributes::ATTR_SIGNATURE}; 115 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 116 117 testKeys = {Attributes::ATTR_NEXT_FAIL_LOCKOUT_DURATION, Attributes::ATTR_SIGNATURE}; 118 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 119 120 testKeys = {Attributes::ATTR_SIGNATURE}; 121 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 122 123 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2); 124 EXPECT_NE(resourceNode, nullptr); 125 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode)); 126 MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get()); 127 EXPECT_CALL(*node, GetProperty(_, _)) 128 .Times(0) 129 .WillOnce(Return(FAIL)) 130 .WillOnce(Return(SUCCESS)); 131 testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback()); 132 EXPECT_NE(testCallback, nullptr); 133 EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1); 134 IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION); 135 callbackInterface = testCallback; 136 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 137 EXPECT_TRUE(ResourceNodePool::Instance().Delete(2)); 138 IpcCommon::DeleteAllPermission(); 139 } 140 141 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById004, TestSize.Level0) 142 { 143 UserAuthService service; 144 uint64_t testCredentialId = 1; 145 std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE}; 146 sptr<MockGetExecutorPropertyCallback> testCallback(nullptr); 147 sptr<GetExecutorPropertyCallbackInterface> callbackInterface = testCallback; 148 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 149 150 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); 151 EXPECT_NE(mockHdi, nullptr); 152 EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(1); 153 ON_CALL(*mockHdi, GetCredentialById) 154 .WillByDefault( __anon424fe9cb0202(uint64_t credentialId, HdiCredentialInfo &info) 155 [](uint64_t credentialId, HdiCredentialInfo &info) { 156 HdiCredentialInfo tempInfo = { 157 .credentialId = 1, 158 .executorIndex = 2, 159 .templateId = 3, 160 .authType = static_cast<HdiAuthType>(1), 161 .executorMatcher = 2, 162 .executorSensorHint = 3, 163 }; 164 info = tempInfo; 165 return HDF_SUCCESS; 166 } 167 ); 168 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, FACE, ALL_IN_ONE); 169 EXPECT_NE(resourceNode, nullptr); 170 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode)); 171 MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get()); 172 EXPECT_CALL(*node, GetProperty(_, _)) 173 .Times(0) 174 .WillOnce(Return(FAIL)) 175 .WillOnce(Return(SUCCESS)); 176 testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback()); 177 EXPECT_NE(testCallback, nullptr); 178 EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1); 179 IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION); 180 callbackInterface = testCallback; 181 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 182 EXPECT_TRUE(ResourceNodePool::Instance().Delete(2)); 183 IpcCommon::DeleteAllPermission(); 184 } 185 186 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById005, TestSize.Level0) 187 { 188 UserAuthService service; 189 uint64_t testCredentialId = 1; 190 std::vector<Attributes::AttributeKey> testKeys = {Attributes::ATTR_PIN_SUB_TYPE, Attributes::ATTR_SIGNATURE}; 191 sptr<MockGetExecutorPropertyCallback> testCallback(nullptr); 192 sptr<GetExecutorPropertyCallbackInterface> callbackInterface = testCallback; 193 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 194 195 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); 196 EXPECT_NE(mockHdi, nullptr); 197 EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(1); 198 ON_CALL(*mockHdi, GetCredentialById) 199 .WillByDefault( __anon424fe9cb0302(uint64_t credentialId, HdiCredentialInfo &info) 200 [](uint64_t credentialId, HdiCredentialInfo &info) { 201 HdiCredentialInfo tempInfo = { 202 .credentialId = 1, 203 .executorIndex = 2, 204 .templateId = 3, 205 .authType = static_cast<HdiAuthType>(1), 206 .executorMatcher = 2, 207 .executorSensorHint = 3, 208 }; 209 info = tempInfo; 210 return HDF_SUCCESS; 211 } 212 ); 213 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, PIN, ALL_IN_ONE); 214 EXPECT_NE(resourceNode, nullptr); 215 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode)); 216 MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get()); 217 EXPECT_CALL(*node, GetProperty(_, _)) 218 .Times(1) 219 .WillOnce(Return(FAIL)) 220 .WillOnce(Return(SUCCESS)); 221 testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback()); 222 EXPECT_NE(testCallback, nullptr); 223 EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1); 224 IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION); 225 callbackInterface = testCallback; 226 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 227 EXPECT_TRUE(ResourceNodePool::Instance().Delete(2)); 228 IpcCommon::DeleteAllPermission(); 229 } 230 231 HWTEST_F(UserAuthServiceTest, UserAuthServiceGetPropertyById006, TestSize.Level0) 232 { 233 UserAuthService service; 234 uint64_t testCredentialId = 1; 235 std::vector<Attributes::AttributeKey> testKeys = { 236 Attributes::ATTR_PIN_SUB_TYPE, 237 Attributes::ATTR_SIGNATURE, 238 Attributes::ATTR_NEXT_FAIL_LOCKOUT_DURATION 239 }; 240 sptr<MockGetExecutorPropertyCallback> testCallback(nullptr); 241 sptr<GetExecutorPropertyCallbackInterface> callbackInterface = testCallback; 242 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 243 244 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get(); 245 EXPECT_NE(mockHdi, nullptr); 246 EXPECT_CALL(*mockHdi, GetCredentialById(_, _)).Times(1); 247 ON_CALL(*mockHdi, GetCredentialById) 248 .WillByDefault( __anon424fe9cb0402(uint64_t credentialId, HdiCredentialInfo &info) 249 [](uint64_t credentialId, HdiCredentialInfo &info) { 250 HdiCredentialInfo tempInfo = { 251 .credentialId = 1, 252 .executorIndex = 2, 253 .templateId = 3, 254 .authType = static_cast<HdiAuthType>(2), 255 .executorMatcher = 2, 256 .executorSensorHint = 3, 257 }; 258 info = tempInfo; 259 return HDF_SUCCESS; 260 } 261 ); 262 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(2, FACE, ALL_IN_ONE); 263 EXPECT_NE(resourceNode, nullptr); 264 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode)); 265 MockResourceNode *node = static_cast<MockResourceNode *>(resourceNode.get()); 266 EXPECT_CALL(*node, GetProperty(_, _)) 267 .Times(1) 268 .WillOnce(Return(FAIL)) 269 .WillOnce(Return(SUCCESS)); 270 testCallback = sptr<MockGetExecutorPropertyCallback>(new (std::nothrow) MockGetExecutorPropertyCallback()); 271 EXPECT_NE(testCallback, nullptr); 272 EXPECT_CALL(*testCallback, OnGetExecutorPropertyResult(_, _)).Times(1); 273 IpcCommon::AddPermission(ACCESS_USER_AUTH_INTERNAL_PERMISSION); 274 callbackInterface = testCallback; 275 service.GetPropertyById(testCredentialId, testKeys, callbackInterface); 276 EXPECT_TRUE(ResourceNodePool::Instance().Delete(2)); 277 IpcCommon::DeleteAllPermission(); 278 } 279 } // namespace UserAuth 280 } // namespace UserIam 281 } // namespace OHOS