xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/crypto/certificate_util_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "quiche/quic/core/crypto/certificate_util.h"
6 
7 #include "openssl/ssl.h"
8 #include "quiche/quic/core/crypto/certificate_view.h"
9 #include "quiche/quic/platform/api/quic_test.h"
10 #include "quiche/quic/platform/api/quic_test_output.h"
11 
12 namespace quic {
13 namespace test {
14 namespace {
15 
TEST(CertificateUtilTest,CreateSelfSignedCertificate)16 TEST(CertificateUtilTest, CreateSelfSignedCertificate) {
17   bssl::UniquePtr<EVP_PKEY> key = MakeKeyPairForSelfSignedCertificate();
18   ASSERT_NE(key, nullptr);
19 
20   CertificatePrivateKey cert_key(std::move(key));
21 
22   CertificateOptions options;
23   options.subject = "CN=subject";
24   options.serial_number = 0x12345678;
25   options.validity_start = {2020, 1, 1, 0, 0, 0};
26   options.validity_end = {2049, 12, 31, 0, 0, 0};
27   std::string der_cert =
28       CreateSelfSignedCertificate(*cert_key.private_key(), options);
29   ASSERT_FALSE(der_cert.empty());
30 
31   QuicSaveTestOutput("CertificateUtilTest_CreateSelfSignedCert.crt", der_cert);
32 
33   std::unique_ptr<CertificateView> cert_view =
34       CertificateView::ParseSingleCertificate(der_cert);
35   ASSERT_NE(cert_view, nullptr);
36   EXPECT_EQ(cert_view->public_key_type(), PublicKeyType::kP256);
37 
38   std::optional<std::string> subject = cert_view->GetHumanReadableSubject();
39   ASSERT_TRUE(subject.has_value());
40   EXPECT_EQ(*subject, options.subject);
41 
42   EXPECT_TRUE(
43       cert_key.ValidForSignatureAlgorithm(SSL_SIGN_ECDSA_SECP256R1_SHA256));
44   EXPECT_TRUE(cert_key.MatchesPublicKey(*cert_view));
45 }
46 
47 }  // namespace
48 }  // namespace test
49 }  // namespace quic
50