blob: e3c820b90c02f7e3d74dbd1b36c1bc89d2b1520b [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/rtccertificategenerator.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"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
17#include "rtc_base/gunit.h"
Niels Möller84255bb2017-10-06 13:43:23 +020018#include "rtc_base/refcountedobject.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
24class RTCCertificateGeneratorFixture : public RTCCertificateGeneratorCallback {
25 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 }
35 ~RTCCertificateGeneratorFixture() override {}
36
37 RTCCertificateGenerator* generator() const { return generator_.get(); }
38 RTCCertificate* certificate() const { return certificate_.get(); }
39
nisseef8b61e2016-04-29 06:09:15 -070040 void OnSuccess(const scoped_refptr<RTCCertificate>& certificate) override {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020041 RTC_CHECK(signaling_thread_->IsCurrent());
42 RTC_CHECK(certificate);
43 certificate_ = certificate;
44 generate_async_completed_ = true;
45 }
nisseef8b61e2016-04-29 06:09:15 -070046 void OnFailure() override {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020047 RTC_CHECK(signaling_thread_->IsCurrent());
48 certificate_ = nullptr;
49 generate_async_completed_ = true;
50 }
51
52 bool GenerateAsyncCompleted() {
53 RTC_CHECK(signaling_thread_->IsCurrent());
54 if (generate_async_completed_) {
55 // Reset flag so that future generation requests are not considered done.
56 generate_async_completed_ = false;
57 return true;
58 }
59 return false;
60 }
61
62 protected:
63 Thread* const signaling_thread_;
jbauch555604a2016-04-26 03:13:22 -070064 std::unique_ptr<Thread> worker_thread_;
65 std::unique_ptr<RTCCertificateGenerator> generator_;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020066 scoped_refptr<RTCCertificate> certificate_;
67 bool generate_async_completed_;
68};
69
Yves Gerey665174f2018-06-19 15:03:05 +020070class RTCCertificateGeneratorTest : public testing::Test {
Henrik Boströmda3a1da2016-04-15 17:55:21 +020071 public:
72 RTCCertificateGeneratorTest()
73 : fixture_(new RefCountedObject<RTCCertificateGeneratorFixture>()) {}
Henrik Boströmda3a1da2016-04-15 17:55:21 +020074
75 protected:
Henrik Boström300450b2016-05-25 14:06:30 +020076 static const int kGenerationTimeoutMs = 10000;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020077
78 scoped_refptr<RTCCertificateGeneratorFixture> fixture_;
79};
80
81TEST_F(RTCCertificateGeneratorTest, GenerateECDSA) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020082 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate(KeyParams::ECDSA(),
83 absl::nullopt));
Henrik Boströmda3a1da2016-04-15 17:55:21 +020084}
85
86TEST_F(RTCCertificateGeneratorTest, GenerateRSA) {
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020087 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate(KeyParams::RSA(),
88 absl::nullopt));
Henrik Boströmda3a1da2016-04-15 17:55:21 +020089}
90
91TEST_F(RTCCertificateGeneratorTest, GenerateAsyncECDSA) {
92 EXPECT_FALSE(fixture_->certificate());
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020093 fixture_->generator()->GenerateCertificateAsync(KeyParams::ECDSA(),
94 absl::nullopt, fixture_);
Henrik Boströmda3a1da2016-04-15 17:55:21 +020095 // Until generation has completed, the certificate is null. Since this is an
96 // async call, generation must not have completed until we process messages
97 // posted to this thread (which is done by |EXPECT_TRUE_WAIT|).
98 EXPECT_FALSE(fixture_->GenerateAsyncCompleted());
99 EXPECT_FALSE(fixture_->certificate());
100 EXPECT_TRUE_WAIT(fixture_->GenerateAsyncCompleted(), kGenerationTimeoutMs);
101 EXPECT_TRUE(fixture_->certificate());
102}
103
104TEST_F(RTCCertificateGeneratorTest, GenerateWithExpires) {
105 // By generating two certificates with different expiration we can compare the
106 // two expiration times relative to each other without knowing the current
107 // time relative to epoch, 1970-01-01T00:00:00Z. This verifies that the
108 // expiration parameter is correctly used relative to the generator's clock,
109 // but does not verify that this clock is relative to epoch.
110
111 // Generate a certificate that expires immediately.
112 scoped_refptr<RTCCertificate> cert_a =
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200113 RTCCertificateGenerator::GenerateCertificate(KeyParams::ECDSA(), 0);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200114 EXPECT_TRUE(cert_a);
115
116 // Generate a certificate that expires in one minute.
117 const uint64_t kExpiresMs = 60000;
118 scoped_refptr<RTCCertificate> cert_b =
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200119 RTCCertificateGenerator::GenerateCertificate(KeyParams::ECDSA(),
120 kExpiresMs);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200121 EXPECT_TRUE(cert_b);
122
123 // Verify that |cert_b| expires approximately |kExpiresMs| after |cert_a|
124 // (allowing a +/- 1 second plus maximum generation time difference).
125 EXPECT_GT(cert_b->Expires(), cert_a->Expires());
126 uint64_t expires_diff = cert_b->Expires() - cert_a->Expires();
127 EXPECT_GE(expires_diff, kExpiresMs);
Yves Gerey665174f2018-06-19 15:03:05 +0200128 EXPECT_LE(expires_diff, kExpiresMs + 2 * kGenerationTimeoutMs + 1000);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200129}
130
131TEST_F(RTCCertificateGeneratorTest, GenerateWithInvalidParamsShouldFail) {
132 KeyParams invalid_params = KeyParams::RSA(0, 0);
133 EXPECT_FALSE(invalid_params.IsValid());
134
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200135 EXPECT_FALSE(RTCCertificateGenerator::GenerateCertificate(invalid_params,
136 absl::nullopt));
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200137
Danil Chapovalov0a1d1892018-06-21 11:48:25 +0200138 fixture_->generator()->GenerateCertificateAsync(invalid_params, absl::nullopt,
139 fixture_);
Henrik Boströmda3a1da2016-04-15 17:55:21 +0200140 EXPECT_TRUE_WAIT(fixture_->GenerateAsyncCompleted(), kGenerationTimeoutMs);
141 EXPECT_FALSE(fixture_->certificate());
142}
143
144} // namespace rtc