blob: fb7ec913e5bd1ccc1690952c2c28902870855fff [file] [log] [blame]
Henrik Boströmda3a1da2016-04-15 17:55:21 +02001/*
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/rtc_certificate_generator.h"
Henrik Boströmda3a1da2016-04-15 17:55:21 +020012
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
14
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020015#include "absl/types/optional.h"
Niels Möller105711e2022-06-14 15:48:26 +020016#include "api/make_ref_counted.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/gunit.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/thread.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "test/gtest.h"
Henrik Boströmda3a1da2016-04-15 17:55:21 +020021
22namespace rtc {
23
Danil Chapovalovb7da8162022-08-22 16:39:34 +020024class RTCCertificateGeneratorFixture {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020025 public:
26 RTCCertificateGeneratorFixture()
27 : signaling_thread_(Thread::Current()),
tommie7251592017-07-14 14:44:46 -070028 worker_thread_(Thread::Create()),
Henrik Boströmda3a1da2016-04-15 17:55:21 +020029 generate_async_completed_(false) {
30 RTC_CHECK(signaling_thread_);
31 RTC_CHECK(worker_thread_->Start());
32 generator_.reset(
Yves Gerey665174f2018-06-19 15:03:05 +020033 new RTCCertificateGenerator(signaling_thread_, worker_thread_.get()));
Henrik Boströmda3a1da2016-04-15 17:55:21 +020034 }
Henrik Boströmda3a1da2016-04-15 17:55:21 +020035
36 RTCCertificateGenerator* generator() const { return generator_.get(); }
37 RTCCertificate* certificate() const { return certificate_.get(); }
38
Danil Chapovalovb7da8162022-08-22 16:39:34 +020039 RTCCertificateGeneratorInterface::Callback OnGenerated() {
40 return [this](scoped_refptr<RTCCertificate> certificate) mutable {
41 RTC_CHECK(signaling_thread_->IsCurrent());
42 certificate_ = std::move(certificate);
43 generate_async_completed_ = true;
44 };
Henrik Boströmda3a1da2016-04-15 17:55:21 +020045 }
46
47 bool GenerateAsyncCompleted() {
48 RTC_CHECK(signaling_thread_->IsCurrent());
49 if (generate_async_completed_) {
50 // Reset flag so that future generation requests are not considered done.
51 generate_async_completed_ = false;
52 return true;
53 }
54 return false;
55 }
56
57 protected:
58 Thread* const signaling_thread_;
jbauch555604a2016-04-26 03:13:22 -070059 std::unique_ptr<Thread> worker_thread_;
60 std::unique_ptr<RTCCertificateGenerator> generator_;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020061 scoped_refptr<RTCCertificate> certificate_;
62 bool generate_async_completed_;
63};
64
Mirko Bonadei6a489f22019-04-09 15:11:12 +020065class RTCCertificateGeneratorTest : public ::testing::Test {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020066 public:
Henrik Boströmda3a1da2016-04-15 17:55:21 +020067 protected:
Mirko Bonadeif7f68702020-04-15 09:55:03 +020068 static constexpr int kGenerationTimeoutMs = 10000;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020069
Niels Möller83830f32022-05-20 09:12:57 +020070 rtc::AutoThread main_thread_;
Danil Chapovalovb7da8162022-08-22 16:39:34 +020071 RTCCertificateGeneratorFixture fixture_;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020072};
73
74TEST_F(RTCCertificateGeneratorTest, GenerateECDSA) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020075 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate(KeyParams::ECDSA(),
76 absl::nullopt));
Henrik Boströmda3a1da2016-04-15 17:55:21 +020077}
78
79TEST_F(RTCCertificateGeneratorTest, GenerateRSA) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020080 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate(KeyParams::RSA(),
81 absl::nullopt));
Henrik Boströmda3a1da2016-04-15 17:55:21 +020082}
83
84TEST_F(RTCCertificateGeneratorTest, GenerateAsyncECDSA) {
Danil Chapovalovb7da8162022-08-22 16:39:34 +020085 EXPECT_FALSE(fixture_.certificate());
86 fixture_.generator()->GenerateCertificateAsync(
87 KeyParams::ECDSA(), absl::nullopt, fixture_.OnGenerated());
Henrik Boströmda3a1da2016-04-15 17:55:21 +020088 // Until generation has completed, the certificate is null. Since this is an
89 // async call, generation must not have completed until we process messages
Artem Titov96e3b992021-07-26 16:03:14 +020090 // posted to this thread (which is done by `EXPECT_TRUE_WAIT`).
Danil Chapovalovb7da8162022-08-22 16:39:34 +020091 EXPECT_FALSE(fixture_.GenerateAsyncCompleted());
92 EXPECT_FALSE(fixture_.certificate());
93 EXPECT_TRUE_WAIT(fixture_.GenerateAsyncCompleted(), kGenerationTimeoutMs);
94 EXPECT_TRUE(fixture_.certificate());
Henrik Boströmda3a1da2016-04-15 17:55:21 +020095}
96
97TEST_F(RTCCertificateGeneratorTest, GenerateWithExpires) {
98 // By generating two certificates with different expiration we can compare the
99 // two expiration times relative to each other without knowing the current
100 // time relative to epoch, 1970-01-01T00:00:00Z. This verifies that the
101 // expiration parameter is correctly used relative to the generator's clock,
102 // but does not verify that this clock is relative to epoch.
103
104 // Generate a certificate that expires immediately.
105 scoped_refptr<RTCCertificate> cert_a =
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200106 RTCCertificateGenerator::GenerateCertificate(KeyParams::ECDSA(), 0);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200107 EXPECT_TRUE(cert_a);
108
109 // Generate a certificate that expires in one minute.
110 const uint64_t kExpiresMs = 60000;
111 scoped_refptr<RTCCertificate> cert_b =
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200112 RTCCertificateGenerator::GenerateCertificate(KeyParams::ECDSA(),
113 kExpiresMs);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200114 EXPECT_TRUE(cert_b);
115
Artem Titov96e3b992021-07-26 16:03:14 +0200116 // Verify that `cert_b` expires approximately `kExpiresMs` after `cert_a`
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200117 // (allowing a +/- 1 second plus maximum generation time difference).
118 EXPECT_GT(cert_b->Expires(), cert_a->Expires());
119 uint64_t expires_diff = cert_b->Expires() - cert_a->Expires();
120 EXPECT_GE(expires_diff, kExpiresMs);
Yves Gerey665174f2018-06-19 15:03:05 +0200121 EXPECT_LE(expires_diff, kExpiresMs + 2 * kGenerationTimeoutMs + 1000);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200122}
123
124TEST_F(RTCCertificateGeneratorTest, GenerateWithInvalidParamsShouldFail) {
125 KeyParams invalid_params = KeyParams::RSA(0, 0);
126 EXPECT_FALSE(invalid_params.IsValid());
127
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200128 EXPECT_FALSE(RTCCertificateGenerator::GenerateCertificate(invalid_params,
129 absl::nullopt));
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200130
Danil Chapovalovb7da8162022-08-22 16:39:34 +0200131 fixture_.generator()->GenerateCertificateAsync(invalid_params, absl::nullopt,
132 fixture_.OnGenerated());
133 EXPECT_TRUE_WAIT(fixture_.GenerateAsyncCompleted(), kGenerationTimeoutMs);
134 EXPECT_FALSE(fixture_.certificate());
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200135}
136
137} // namespace rtc