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

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

Issue 2877023002: Move webrtc/{base => rtc_base} (Closed)
Patch Set: update presubmit.py and DEPS include rules Created 3 years, 5 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') | webrtc/base/safe_compare.h » ('j') | 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 <memory>
14
15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/gunit.h"
17 #include "webrtc/base/logging.h"
18 #include "webrtc/base/optional.h"
19 #include "webrtc/base/thread.h"
20
21 namespace rtc {
22
23 class RTCCertificateGeneratorFixture : public RTCCertificateGeneratorCallback {
24 public:
25 RTCCertificateGeneratorFixture()
26 : signaling_thread_(Thread::Current()),
27 worker_thread_(new Thread()),
28 generate_async_completed_(false) {
29 RTC_CHECK(signaling_thread_);
30 RTC_CHECK(worker_thread_->Start());
31 generator_.reset(
32 new RTCCertificateGenerator(signaling_thread_, worker_thread_.get()));
33 }
34 ~RTCCertificateGeneratorFixture() override {}
35
36 RTCCertificateGenerator* generator() const { return generator_.get(); }
37 RTCCertificate* certificate() const { return certificate_.get(); }
38
39 void OnSuccess(const scoped_refptr<RTCCertificate>& certificate) override {
40 RTC_CHECK(signaling_thread_->IsCurrent());
41 RTC_CHECK(certificate);
42 certificate_ = certificate;
43 generate_async_completed_ = true;
44 }
45 void OnFailure() override {
46 RTC_CHECK(signaling_thread_->IsCurrent());
47 certificate_ = nullptr;
48 generate_async_completed_ = true;
49 }
50
51 bool GenerateAsyncCompleted() {
52 RTC_CHECK(signaling_thread_->IsCurrent());
53 if (generate_async_completed_) {
54 // Reset flag so that future generation requests are not considered done.
55 generate_async_completed_ = false;
56 return true;
57 }
58 return false;
59 }
60
61 protected:
62 Thread* const signaling_thread_;
63 std::unique_ptr<Thread> worker_thread_;
64 std::unique_ptr<RTCCertificateGenerator> generator_;
65 scoped_refptr<RTCCertificate> certificate_;
66 bool generate_async_completed_;
67 };
68
69 class RTCCertificateGeneratorTest
70 : public testing::Test {
71 public:
72 RTCCertificateGeneratorTest()
73 : fixture_(new RefCountedObject<RTCCertificateGeneratorFixture>()) {}
74 ~RTCCertificateGeneratorTest() {}
75
76 protected:
77 static const int kGenerationTimeoutMs = 10000;
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
OLDNEW
« no previous file with comments | « webrtc/base/rtccertificategenerator.cc ('k') | webrtc/base/safe_compare.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698