Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: webrtc/base/rtccertificategenerator_unittest.cc

Issue 1883813002: RTCCertificateGenerator added. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Addressed comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/rtccertificategenerator.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
11 #include "webrtc/base/rtccertificategenerator.h"
12
13 #include "webrtc/base/checks.h"
14 #include "webrtc/base/gunit.h"
15 #include "webrtc/base/logging.h"
16 #include "webrtc/base/optional.h"
17 #include "webrtc/base/scoped_ptr.h"
18 #include "webrtc/base/thread.h"
19
20 namespace rtc {
21
22 class RTCCertificateGeneratorFixture : public RTCCertificateGeneratorCallback {
23 public:
24 RTCCertificateGeneratorFixture()
25 : signaling_thread_(Thread::Current()),
26 worker_thread_(new Thread()),
27 generate_async_completed_(false) {
28 RTC_CHECK(signaling_thread_);
29 RTC_CHECK(worker_thread_->Start());
30 generator_.reset(
31 new RTCCertificateGenerator(signaling_thread_, worker_thread_.get()));
32 }
33 ~RTCCertificateGeneratorFixture() override {}
34
35 RTCCertificateGenerator* generator() const { return generator_.get(); }
36 RTCCertificate* certificate() const { return certificate_.get(); }
37
38 void OnSuccess(const scoped_refptr<RTCCertificate>& certificate) {
39 RTC_CHECK(signaling_thread_->IsCurrent());
40 RTC_CHECK(certificate);
41 certificate_ = certificate;
42 generate_async_completed_ = true;
43 }
44 void OnFailure() {
45 RTC_CHECK(signaling_thread_->IsCurrent());
46 certificate_ = nullptr;
47 generate_async_completed_ = true;
48 }
49
50 bool GenerateAsyncCompleted() {
51 RTC_CHECK(signaling_thread_->IsCurrent());
52 if (generate_async_completed_) {
53 // Reset flag so that future generation requests are not considered done.
54 generate_async_completed_ = false;
55 return true;
56 }
57 return false;
58 }
59
60 protected:
61 Thread* const signaling_thread_;
62 scoped_ptr<Thread> worker_thread_;
63 scoped_ptr<RTCCertificateGenerator> generator_;
64 scoped_refptr<RTCCertificate> certificate_;
65 bool generate_async_completed_;
66 };
67
68 class RTCCertificateGeneratorTest
69 : public testing::Test {
70 public:
71 RTCCertificateGeneratorTest()
72 : fixture_(new RefCountedObject<RTCCertificateGeneratorFixture>()) {}
73 ~RTCCertificateGeneratorTest() {}
74
75 protected:
76 static const int kGenerationTimeoutMs = 1000;
77
78 scoped_refptr<RTCCertificateGeneratorFixture> fixture_;
79 };
80
81 TEST_F(RTCCertificateGeneratorTest, GenerateECDSA) {
82 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate(
83 KeyParams::ECDSA(),
84 Optional<uint64_t>()));
85 }
86
87 TEST_F(RTCCertificateGeneratorTest, GenerateRSA) {
88 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificate(
89 KeyParams::RSA(),
90 Optional<uint64_t>()));
91 }
92
93 TEST_F(RTCCertificateGeneratorTest, GenerateAsyncECDSA) {
94 EXPECT_FALSE(fixture_->certificate());
95 fixture_->generator()->GenerateCertificateAsync(
96 KeyParams::ECDSA(),
97 Optional<uint64_t>(),
98 fixture_);
99 // Until generation has completed, the certificate is null. Since this is an
100 // async call, generation must not have completed until we process messages
101 // posted to this thread (which is done by |EXPECT_TRUE_WAIT|).
102 EXPECT_FALSE(fixture_->GenerateAsyncCompleted());
103 EXPECT_FALSE(fixture_->certificate());
104 EXPECT_TRUE_WAIT(fixture_->GenerateAsyncCompleted(), kGenerationTimeoutMs);
105 EXPECT_TRUE(fixture_->certificate());
106 }
107
108 TEST_F(RTCCertificateGeneratorTest, GenerateWithExpires) {
109 // By generating two certificates with different expiration we can compare the
110 // two expiration times relative to each other without knowing the current
111 // time relative to epoch, 1970-01-01T00:00:00Z. This verifies that the
112 // expiration parameter is correctly used relative to the generator's clock,
113 // but does not verify that this clock is relative to epoch.
114
115 // Generate a certificate that expires immediately.
116 scoped_refptr<RTCCertificate> cert_a =
117 RTCCertificateGenerator::GenerateCertificate(
118 KeyParams::ECDSA(), Optional<uint64_t>(0));
119 EXPECT_TRUE(cert_a);
120
121 // Generate a certificate that expires in one minute.
122 const uint64_t kExpiresMs = 60000;
123 scoped_refptr<RTCCertificate> cert_b =
124 RTCCertificateGenerator::GenerateCertificate(
125 KeyParams::ECDSA(), Optional<uint64_t>(kExpiresMs));
126 EXPECT_TRUE(cert_b);
127
128 // Verify that |cert_b| expires approximately |kExpiresMs| after |cert_a|
129 // (allowing a +/- 1 second plus maximum generation time difference).
130 EXPECT_GT(cert_b->Expires(), cert_a->Expires());
131 uint64_t expires_diff = cert_b->Expires() - cert_a->Expires();
132 EXPECT_GE(expires_diff, kExpiresMs);
133 EXPECT_LE(expires_diff, kExpiresMs + 2*kGenerationTimeoutMs + 1000);
134 }
135
136 TEST_F(RTCCertificateGeneratorTest, GenerateWithInvalidParamsShouldFail) {
137 KeyParams invalid_params = KeyParams::RSA(0, 0);
138 EXPECT_FALSE(invalid_params.IsValid());
139
140 EXPECT_FALSE(RTCCertificateGenerator::GenerateCertificate(
141 invalid_params, Optional<uint64_t>()));
142
143 fixture_->generator()->GenerateCertificateAsync(
144 invalid_params,
145 Optional<uint64_t>(),
146 fixture_);
147 EXPECT_TRUE_WAIT(fixture_->GenerateAsyncCompleted(), kGenerationTimeoutMs);
148 EXPECT_FALSE(fixture_->certificate());
149 }
150
151 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/rtccertificategenerator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698