blob: 9a0ad0cb8d858178e051d425bc329831770b25af [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "api/optional.h"
16#include "rtc_base/checks.h"
17#include "rtc_base/gunit.h"
18#include "rtc_base/logging.h"
Niels Möller84255bb2017-10-06 13:43:23 +020019#include "rtc_base/refcountedobject.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/thread.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(
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
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
70class RTCCertificateGeneratorTest
71 : public testing::Test {
72 public:
73 RTCCertificateGeneratorTest()
74 : fixture_(new RefCountedObject<RTCCertificateGeneratorFixture>()) {}
Henrik Boströmda3a1da2016-04-15 17:55:21 +020075
76 protected:
Henrik Boström300450b2016-05-25 14:06:30 +020077 static const int kGenerationTimeoutMs = 10000;
Henrik Boströmda3a1da2016-04-15 17:55:21 +020078
79 scoped_refptr<RTCCertificateGeneratorFixture> fixture_;
80};
81
82TEST_F(RTCCertificateGeneratorTest, GenerateECDSA) {
83 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate(
84 KeyParams::ECDSA(),
85 Optional<uint64_t>()));
86}
87
88TEST_F(RTCCertificateGeneratorTest, GenerateRSA) {
89 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate(
90 KeyParams::RSA(),
91 Optional<uint64_t>()));
92}
93
94TEST_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
109TEST_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
137TEST_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