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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/payments/content/browser_binding/browser_bound_key_desktop.h"
#include <memory>
#include "base/memory/raw_ptr.h"
#include "base/test/gtest_util.h"
#include "components/unexportable_keys/mock_unexportable_key.h"
#include "crypto/cose.h"
#include "crypto/test_support.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using crypto::SignatureVerifier;
using testing::Return;
using unexportable_keys::MockUnexportableKey;
static const SignatureVerifier::SignatureAlgorithm kAllSignatureAlgorithms[] = {
SignatureVerifier::SignatureAlgorithm::RSA_PKCS1_SHA1,
SignatureVerifier::SignatureAlgorithm::RSA_PKCS1_SHA256,
SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256,
SignatureVerifier::SignatureAlgorithm::RSA_PSS_SHA256};
} // namespace
namespace payments {
class BrowserBoundKeyDesktopTest : public ::testing::Test {
public:
BrowserBoundKeyDesktopTest() {
auto key = std::make_unique<MockUnexportableKey>();
EXPECT_CALL(*key, Algorithm())
.WillRepeatedly(
Return(SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256));
key_ = key.get();
browser_bound_key_ =
std::make_unique<BrowserBoundKeyDesktop>(std::move(key));
}
~BrowserBoundKeyDesktopTest() override = default;
MockUnexportableKey* key() { return key_; }
BrowserBoundKeyDesktop* browser_bound_key() {
return browser_bound_key_.get();
}
private:
std::unique_ptr<BrowserBoundKeyDesktop> browser_bound_key_;
raw_ptr<MockUnexportableKey> key_;
};
TEST_F(BrowserBoundKeyDesktopTest, UnexportableSigningKey_AlgorithmValidation) {
std::unique_ptr<MockUnexportableKey> key;
for (const auto algorithm : kAllSignatureAlgorithms) {
key = std::make_unique<MockUnexportableKey>();
EXPECT_CALL(*key, Algorithm()).WillRepeatedly(Return(algorithm));
if (algorithm == SignatureVerifier::SignatureAlgorithm::ECDSA_SHA256 ||
algorithm == SignatureVerifier::SignatureAlgorithm::RSA_PKCS1_SHA256) {
EXPECT_NO_FATAL_FAILURE(BrowserBoundKeyDesktop(std::move(key)));
} else {
EXPECT_CHECK_DEATH(BrowserBoundKeyDesktop(std::move(key)));
}
}
}
TEST_F(BrowserBoundKeyDesktopTest, GetIdentifier) {
const std::vector<uint8_t> wrapped_key = {
0, 1, 2, 3, 4,
};
EXPECT_CALL(*key(), GetWrappedKey()).WillRepeatedly(Return(wrapped_key));
EXPECT_EQ(browser_bound_key()->GetIdentifier(), wrapped_key);
}
TEST_F(BrowserBoundKeyDesktopTest, Sign) {
const std::vector<uint8_t> signed_data = {
0, 1, 2, 3, 4,
};
const std::vector<uint8_t> client_data = {
5, 6, 7, 8, 9,
};
EXPECT_CALL(*key(),
SignSlowly(static_cast<base::span<const uint8_t>>(client_data)))
.WillRepeatedly(Return(signed_data));
EXPECT_EQ(browser_bound_key()->Sign(client_data), signed_data);
}
TEST_F(BrowserBoundKeyDesktopTest, Sign_SignSlowlyReturnsNullopt) {
const std::vector<uint8_t> client_data = {
0, 1, 2, 3, 4,
};
EXPECT_CALL(*key(),
SignSlowly(static_cast<base::span<const uint8_t>>(client_data)))
.WillRepeatedly(Return(std::nullopt));
EXPECT_EQ(browser_bound_key()->Sign(client_data), std::vector<uint8_t>());
}
TEST_F(BrowserBoundKeyDesktopTest, GetPublicKeyAsCoseKey) {
crypto::keypair::PublicKey public_key =
crypto::test::FixedRsa2048PublicKeyForTesting();
EXPECT_CALL(*key(), GetSubjectPublicKeyInfo())
.WillRepeatedly(Return(public_key.ToSubjectPublicKeyInfo()));
EXPECT_EQ(browser_bound_key()->GetPublicKeyAsCoseKey(),
crypto::PublicKeyToCoseKey(public_key));
}
} // namespace payments
|