1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/webauthn/local_credential_management_mac.h"
#include <optional>
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/test/browser_task_environment.h"
#include "crypto/apple_keychain_v2.h"
#include "crypto/scoped_fake_apple_keychain_v2.h"
#include "device/fido/mac/authenticator_config.h"
#include "device/fido/mac/credential_store.h"
#include "device/fido/public_key_credential_user_entity.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using base::test::TestFuture;
static const device::PublicKeyCredentialUserEntity kUser({1, 2, 3},
"doe@example.com",
"John Doe");
constexpr char kRpId[] = "example.com";
class LocalCredentialManagementTest : public testing::Test {
protected:
LocalCredentialManagementTest() = default;
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
device::fido::mac::AuthenticatorConfig config_{
.keychain_access_group = "test-keychain-access-group",
.metadata_secret = "TestMetadataSecret"};
LocalCredentialManagementMac local_cred_man_{config_};
crypto::ScopedFakeAppleKeychainV2 keychain_{config_.keychain_access_group};
device::fido::mac::TouchIdCredentialStore store_{config_};
};
TEST_F(LocalCredentialManagementTest, NoCredentials) {
TestFuture<bool> future;
local_cred_man_.HasCredentials(future.GetCallback());
EXPECT_FALSE(future.IsReady());
EXPECT_TRUE(future.Wait());
EXPECT_FALSE(future.Get());
TestFuture<std::optional<std::vector<device::DiscoverableCredentialMetadata>>>
enumerate_future;
local_cred_man_.Enumerate(enumerate_future.GetCallback());
EXPECT_FALSE(enumerate_future.IsReady());
EXPECT_TRUE(enumerate_future.Wait());
auto result = enumerate_future.Get();
ASSERT_TRUE(result.has_value());
EXPECT_TRUE(result->empty());
}
TEST_F(LocalCredentialManagementTest, OneCredential) {
TestFuture<bool> future;
auto credential = store_.CreateCredential(
kRpId, kUser, device::fido::mac::TouchIdCredentialStore::kDiscoverable);
EXPECT_TRUE(credential);
local_cred_man_.HasCredentials(future.GetCallback());
EXPECT_FALSE(future.IsReady());
EXPECT_TRUE(future.Wait());
EXPECT_TRUE(future.Get());
TestFuture<std::optional<std::vector<device::DiscoverableCredentialMetadata>>>
enumerate_future;
local_cred_man_.Enumerate(enumerate_future.GetCallback());
EXPECT_FALSE(enumerate_future.IsReady());
EXPECT_TRUE(enumerate_future.Wait());
const std::optional<std::vector<device::DiscoverableCredentialMetadata>>
result = enumerate_future.Get();
ASSERT_TRUE(result.has_value());
ASSERT_EQ(result->size(), 1u);
EXPECT_EQ(result->at(0).rp_id, kRpId);
}
TEST_F(LocalCredentialManagementTest, DeleteCredential) {
auto credential = store_.CreateCredential(
kRpId, kUser, device::fido::mac::TouchIdCredentialStore::kDiscoverable);
ASSERT_TRUE(credential);
auto credentials = store_.FindResidentCredentials(kRpId);
ASSERT_TRUE(credentials);
EXPECT_EQ(credentials->size(), 1u);
{
TestFuture<bool> future;
local_cred_man_.Delete(credential->first.credential_id,
future.GetCallback());
EXPECT_FALSE(future.IsReady());
EXPECT_TRUE(future.Wait());
EXPECT_TRUE(future.Get());
}
credentials = store_.FindResidentCredentials(kRpId);
ASSERT_TRUE(credentials);
EXPECT_EQ(store_.FindResidentCredentials(kRpId)->size(), 0u);
{
TestFuture<bool> future;
local_cred_man_.Delete(std::vector<uint8_t>{8}, future.GetCallback());
EXPECT_FALSE(future.IsReady());
EXPECT_TRUE(future.Wait());
EXPECT_FALSE(future.Get());
}
}
TEST_F(LocalCredentialManagementTest, EditCredential) {
TestFuture<bool> future;
auto credential = store_.CreateCredential(
kRpId, kUser, device::fido::mac::TouchIdCredentialStore::kDiscoverable);
ASSERT_TRUE(credential);
auto credentials =
device::fido::mac::TouchIdCredentialStore::FindCredentialsForTesting(
config_, kRpId);
EXPECT_EQ(credentials.size(), 1u);
local_cred_man_.Edit(credential->first.credential_id, "new-username",
future.GetCallback());
EXPECT_FALSE(future.IsReady());
EXPECT_TRUE(future.Wait());
EXPECT_TRUE(future.Get());
credentials =
device::fido::mac::TouchIdCredentialStore::FindCredentialsForTesting(
config_, kRpId);
EXPECT_EQ(credentials.size(), 1u);
EXPECT_EQ(credentials.front().metadata.user_name, "new-username");
}
TEST_F(LocalCredentialManagementTest, EditLongCredential) {
TestFuture<bool> future;
auto credential = store_.CreateCredential(
kRpId, kUser, device::fido::mac::TouchIdCredentialStore::kDiscoverable);
ASSERT_TRUE(credential);
auto credentials =
device::fido::mac::TouchIdCredentialStore::FindCredentialsForTesting(
config_, kRpId);
EXPECT_EQ(credentials.size(), 1u);
local_cred_man_.Edit(
credential->first.credential_id,
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
future.GetCallback());
EXPECT_FALSE(future.IsReady());
EXPECT_TRUE(future.Wait());
EXPECT_TRUE(future.Get());
credentials =
device::fido::mac::TouchIdCredentialStore::FindCredentialsForTesting(
config_, kRpId);
EXPECT_EQ(credentials.size(), 1u);
EXPECT_EQ(
credentials.front().metadata.user_name,
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA…");
}
TEST_F(LocalCredentialManagementTest, EditUnknownCredential) {
TestFuture<bool> future;
auto credential = store_.CreateCredential(
kRpId, kUser, device::fido::mac::TouchIdCredentialStore::kDiscoverable);
ASSERT_TRUE(credential);
auto credentials =
device::fido::mac::TouchIdCredentialStore::FindCredentialsForTesting(
config_, kRpId);
EXPECT_EQ(credentials.size(), 1u);
uint8_t credential_id[] = {0xa};
local_cred_man_.Edit(credential_id, "new-username", future.GetCallback());
EXPECT_FALSE(future.IsReady());
EXPECT_TRUE(future.Wait());
EXPECT_FALSE(future.Get());
}
class ScopedMockKeychain : crypto::AppleKeychainV2 {
public:
ScopedMockKeychain() { SetInstanceOverride(this); }
~ScopedMockKeychain() override { ClearInstanceOverride(); }
MOCK_METHOD(OSStatus,
ItemCopyMatching,
(CFDictionaryRef query, CFTypeRef* result),
(override));
};
class MockKeychainLocalCredentialManagementTest : public testing::Test {
protected:
content::BrowserTaskEnvironment task_environment_;
ScopedMockKeychain mock_keychain_;
LocalCredentialManagementMac local_cred_man_{
{.keychain_access_group = "test-keychain-access-group",
.metadata_secret = "TestMetadataSecret"}};
};
// Regression test for crbug.com/1401342.
TEST_F(MockKeychainLocalCredentialManagementTest, KeychainError) {
EXPECT_CALL(mock_keychain_, ItemCopyMatching)
.WillOnce(testing::Return(errSecInternalComponent));
TestFuture<bool> future;
local_cred_man_.HasCredentials(future.GetCallback());
EXPECT_FALSE(future.IsReady());
EXPECT_TRUE(future.Wait());
EXPECT_FALSE(future.Get());
testing::Mock::VerifyAndClearExpectations(&mock_keychain_);
EXPECT_CALL(mock_keychain_, ItemCopyMatching)
.WillOnce(testing::Return(errSecInternalComponent));
TestFuture<std::optional<std::vector<device::DiscoverableCredentialMetadata>>>
enumerate_future;
local_cred_man_.Enumerate(enumerate_future.GetCallback());
EXPECT_FALSE(enumerate_future.IsReady());
EXPECT_TRUE(enumerate_future.Wait());
EXPECT_FALSE(enumerate_future.Get());
}
} // namespace
|