Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/rtccertificategenerator.h" |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 12 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "api/optional.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/gunit.h" |
| 18 | #include "rtc_base/logging.h" |
Niels Möller | 84255bb | 2017-10-06 13:43:23 +0200 | [diff] [blame] | 19 | #include "rtc_base/refcountedobject.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/thread.h" |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | class RTCCertificateGeneratorFixture : public RTCCertificateGeneratorCallback { |
| 25 | public: |
| 26 | RTCCertificateGeneratorFixture() |
| 27 | : signaling_thread_(Thread::Current()), |
tommi | e725159 | 2017-07-14 14:44:46 -0700 | [diff] [blame] | 28 | worker_thread_(Thread::Create()), |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 29 | generate_async_completed_(false) { |
| 30 | RTC_CHECK(signaling_thread_); |
| 31 | RTC_CHECK(worker_thread_->Start()); |
| 32 | generator_.reset( |
| 33 | new RTCCertificateGenerator(signaling_thread_, worker_thread_.get())); |
| 34 | } |
| 35 | ~RTCCertificateGeneratorFixture() override {} |
| 36 | |
| 37 | RTCCertificateGenerator* generator() const { return generator_.get(); } |
| 38 | RTCCertificate* certificate() const { return certificate_.get(); } |
| 39 | |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 40 | void OnSuccess(const scoped_refptr<RTCCertificate>& certificate) override { |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 41 | RTC_CHECK(signaling_thread_->IsCurrent()); |
| 42 | RTC_CHECK(certificate); |
| 43 | certificate_ = certificate; |
| 44 | generate_async_completed_ = true; |
| 45 | } |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 46 | void OnFailure() override { |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 47 | 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_; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 64 | std::unique_ptr<Thread> worker_thread_; |
| 65 | std::unique_ptr<RTCCertificateGenerator> generator_; |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 66 | scoped_refptr<RTCCertificate> certificate_; |
| 67 | bool generate_async_completed_; |
| 68 | }; |
| 69 | |
| 70 | class RTCCertificateGeneratorTest |
| 71 | : public testing::Test { |
| 72 | public: |
| 73 | RTCCertificateGeneratorTest() |
| 74 | : fixture_(new RefCountedObject<RTCCertificateGeneratorFixture>()) {} |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 75 | |
| 76 | protected: |
Henrik Boström | 300450b | 2016-05-25 14:06:30 +0200 | [diff] [blame] | 77 | static const int kGenerationTimeoutMs = 10000; |
Henrik Boström | da3a1da | 2016-04-15 17:55:21 +0200 | [diff] [blame] | 78 | |
| 79 | scoped_refptr<RTCCertificateGeneratorFixture> fixture_; |
| 80 | }; |
| 81 | |
| 82 | TEST_F(RTCCertificateGeneratorTest, GenerateECDSA) { |
| 83 | EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate( |
| 84 | KeyParams::ECDSA(), |
| 85 | Optional<uint64_t>())); |
| 86 | } |
| 87 | |
| 88 | TEST_F(RTCCertificateGeneratorTest, GenerateRSA) { |
| 89 | EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate( |
| 90 | KeyParams::RSA(), |
| 91 | Optional<uint64_t>())); |
| 92 | } |
| 93 | |
| 94 | TEST_F(RTCCertificateGeneratorTest, GenerateAsyncECDSA) { |
| 95 | EXPECT_FALSE(fixture_->certificate()); |
| 96 | fixture_->generator()->GenerateCertificateAsync( |
| 97 | KeyParams::ECDSA(), |
| 98 | Optional<uint64_t>(), |
| 99 | fixture_); |
| 100 | // Until generation has completed, the certificate is null. Since this is an |
| 101 | // async call, generation must not have completed until we process messages |
| 102 | // posted to this thread (which is done by |EXPECT_TRUE_WAIT|). |
| 103 | EXPECT_FALSE(fixture_->GenerateAsyncCompleted()); |
| 104 | EXPECT_FALSE(fixture_->certificate()); |
| 105 | EXPECT_TRUE_WAIT(fixture_->GenerateAsyncCompleted(), kGenerationTimeoutMs); |
| 106 | EXPECT_TRUE(fixture_->certificate()); |
| 107 | } |
| 108 | |
| 109 | TEST_F(RTCCertificateGeneratorTest, GenerateWithExpires) { |
| 110 | // By generating two certificates with different expiration we can compare the |
| 111 | // two expiration times relative to each other without knowing the current |
| 112 | // time relative to epoch, 1970-01-01T00:00:00Z. This verifies that the |
| 113 | // expiration parameter is correctly used relative to the generator's clock, |
| 114 | // but does not verify that this clock is relative to epoch. |
| 115 | |
| 116 | // Generate a certificate that expires immediately. |
| 117 | scoped_refptr<RTCCertificate> cert_a = |
| 118 | RTCCertificateGenerator::GenerateCertificate( |
| 119 | KeyParams::ECDSA(), Optional<uint64_t>(0)); |
| 120 | EXPECT_TRUE(cert_a); |
| 121 | |
| 122 | // Generate a certificate that expires in one minute. |
| 123 | const uint64_t kExpiresMs = 60000; |
| 124 | scoped_refptr<RTCCertificate> cert_b = |
| 125 | RTCCertificateGenerator::GenerateCertificate( |
| 126 | KeyParams::ECDSA(), Optional<uint64_t>(kExpiresMs)); |
| 127 | EXPECT_TRUE(cert_b); |
| 128 | |
| 129 | // Verify that |cert_b| expires approximately |kExpiresMs| after |cert_a| |
| 130 | // (allowing a +/- 1 second plus maximum generation time difference). |
| 131 | EXPECT_GT(cert_b->Expires(), cert_a->Expires()); |
| 132 | uint64_t expires_diff = cert_b->Expires() - cert_a->Expires(); |
| 133 | EXPECT_GE(expires_diff, kExpiresMs); |
| 134 | EXPECT_LE(expires_diff, kExpiresMs + 2*kGenerationTimeoutMs + 1000); |
| 135 | } |
| 136 | |
| 137 | TEST_F(RTCCertificateGeneratorTest, GenerateWithInvalidParamsShouldFail) { |
| 138 | KeyParams invalid_params = KeyParams::RSA(0, 0); |
| 139 | EXPECT_FALSE(invalid_params.IsValid()); |
| 140 | |
| 141 | EXPECT_FALSE(RTCCertificateGenerator::GenerateCertificate( |
| 142 | invalid_params, Optional<uint64_t>())); |
| 143 | |
| 144 | fixture_->generator()->GenerateCertificateAsync( |
| 145 | invalid_params, |
| 146 | Optional<uint64_t>(), |
| 147 | fixture_); |
| 148 | EXPECT_TRUE_WAIT(fixture_->GenerateAsyncCompleted(), kGenerationTimeoutMs); |
| 149 | EXPECT_FALSE(fixture_->certificate()); |
| 150 | } |
| 151 | |
| 152 | } // namespace rtc |