OLD | NEW |
---|---|
(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 // Using a class separate from |RTCCertificateGeneratorTest| because | |
23 // |RTCCertificateGeneratorCallback| is reference counted, and |testing::Test|s | |
hta-webrtc
2016/04/14 11:54:05
I think this comment just confuses people.
hbos
2016/04/14 15:31:55
Removed it.
| |
24 // are not. | |
25 class RTCCertificateGeneratorTester : public RTCCertificateGeneratorCallback { | |
hta-webrtc
2016/04/14 11:54:05
Naming suggestion: Call it a "fixture" rather than
hbos
2016/04/14 15:31:54
Done.
| |
26 public: | |
27 RTCCertificateGeneratorTester() | |
28 : signaling_thread_(Thread::Current()), | |
29 worker_thread_(new Thread()), | |
30 generate_completed_(false) { | |
31 RTC_CHECK(signaling_thread_); | |
32 RTC_CHECK(worker_thread_->Start()); | |
33 generator_.reset( | |
34 new RTCCertificateGenerator(signaling_thread_, worker_thread_.get())); | |
35 } | |
36 ~RTCCertificateGeneratorTester() override {} | |
37 | |
38 RTCCertificateGenerator* generator() const { return generator_.get(); } | |
39 RTCCertificate* certificate() const { return certificate_.get(); } | |
40 | |
41 void OnSuccess(const scoped_refptr<RTCCertificate>& certificate) { | |
42 RTC_CHECK(signaling_thread_->IsCurrent()); | |
43 RTC_CHECK(certificate); | |
44 certificate_ = certificate; | |
45 generate_completed_ = true; | |
46 } | |
47 void OnFailure() { | |
48 RTC_CHECK(signaling_thread_->IsCurrent()); | |
49 certificate_ = nullptr; | |
50 generate_completed_ = true; | |
51 } | |
52 | |
53 bool GenerationCompleted() { | |
54 RTC_CHECK(signaling_thread_->IsCurrent()); | |
55 if (generate_completed_) { | |
56 // Reset flag so that future generation requests are not considered done. | |
57 generate_completed_ = false; | |
58 return true; | |
59 } | |
60 return false; | |
61 } | |
62 | |
63 protected: | |
64 Thread* const signaling_thread_; | |
65 scoped_ptr<Thread> worker_thread_; | |
66 scoped_ptr<RTCCertificateGenerator> generator_; | |
67 scoped_refptr<RTCCertificate> certificate_; | |
68 bool generate_completed_; | |
69 }; | |
70 | |
71 class RTCCertificateGeneratorTest | |
72 : public testing::Test { | |
73 public: | |
74 RTCCertificateGeneratorTest() { | |
75 tester_ = new RefCountedObject<RTCCertificateGeneratorTester>(); | |
76 } | |
77 ~RTCCertificateGeneratorTest() {} | |
78 | |
79 protected: | |
80 static const int kGenerationTimeoutMs = 1000; | |
81 | |
82 scoped_refptr<RTCCertificateGeneratorTester> tester_; | |
83 }; | |
84 | |
85 TEST_F(RTCCertificateGeneratorTest, GenerateECDSABlockingly) { | |
86 EXPECT_TRUE(RTCCertificateGenerator::GenerateCertificateBlockingly( | |
87 KeyParams::ECDSA(), | |
88 Optional<uint64_t>())); | |
89 } | |
90 | |
91 TEST_F(RTCCertificateGeneratorTest, GenerateECDSA) { | |
92 tester_->generator()->GenerateCertificate( | |
93 KeyParams::ECDSA(), | |
94 Optional<uint64_t>(), | |
95 tester_); | |
96 EXPECT_TRUE_WAIT(tester_->GenerationCompleted(), kGenerationTimeoutMs); | |
97 EXPECT_TRUE(tester_->certificate()); | |
98 } | |
99 | |
100 TEST_F(RTCCertificateGeneratorTest, GenerateRSA) { | |
101 tester_->generator()->GenerateCertificate( | |
102 KeyParams::RSA(), | |
103 Optional<uint64_t>(), | |
104 tester_); | |
105 EXPECT_TRUE_WAIT(tester_->GenerationCompleted(), kGenerationTimeoutMs); | |
106 EXPECT_TRUE(tester_->certificate()); | |
107 } | |
hta-webrtc
2016/04/14 11:54:05
Should you have a test that checks the timeout pat
hbos
2016/04/14 15:31:54
Done. I'm not sure I understand what you mean by "
| |
108 | |
109 TEST_F(RTCCertificateGeneratorTest, GenerateECDSAWithExpires) { | |
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. | |
hbos
2016/04/13 13:40:34
This approach was due to not finding a "get curren
| |
115 | |
116 // Generate a certificate that expires immediately. | |
117 tester_->generator()->GenerateCertificate( | |
118 KeyParams::ECDSA(), | |
119 Optional<uint64_t>(0), | |
120 tester_); | |
121 EXPECT_TRUE_WAIT(tester_->GenerationCompleted(), kGenerationTimeoutMs); | |
122 EXPECT_TRUE(tester_->certificate()); | |
hta-webrtc
2016/04/14 11:54:05
Query: Why are you using the non-blocking version
hbos
2016/04/14 15:31:54
It is more complicated but it guarantees (with EXP
| |
123 scoped_refptr<RTCCertificate> cert_a = tester_->certificate(); | |
124 | |
125 // Generate a certificate that expires in 1 minute. | |
126 const uint64_t kExpiresMs = 60000; | |
127 tester_->generator()->GenerateCertificate( | |
128 KeyParams::ECDSA(), | |
129 Optional<uint64_t>(kExpiresMs), | |
130 tester_); | |
131 EXPECT_TRUE_WAIT(tester_->GenerationCompleted(), kGenerationTimeoutMs); | |
132 EXPECT_TRUE(tester_->certificate()); | |
133 scoped_refptr<RTCCertificate> cert_b = tester_->certificate(); | |
134 | |
135 // Verify that |cert_b| expires approximately |kExpiresMs| after |cert_a| | |
136 // (allowing a +/- 1 second plus maximum generation time difference). | |
137 EXPECT_GT(cert_b->Expires(), cert_a->Expires()); | |
138 uint64_t expires_diff = cert_b->Expires() - cert_a->Expires(); | |
139 EXPECT_GE(expires_diff, kExpiresMs - 2*kGenerationTimeoutMs - 1000); | |
140 EXPECT_LE(expires_diff, kExpiresMs + 2*kGenerationTimeoutMs + 1000); | |
141 } | |
142 | |
143 TEST_F(RTCCertificateGeneratorTest, GenerateWithInvalidParamsShouldFail) { | |
144 KeyParams invalid_params = KeyParams::RSA(0, 0); | |
145 EXPECT_FALSE(invalid_params.IsValid()); | |
146 | |
147 EXPECT_FALSE(RTCCertificateGenerator::GenerateCertificateBlockingly( | |
148 invalid_params, Optional<uint64_t>())); | |
149 | |
150 tester_->generator()->GenerateCertificate( | |
151 invalid_params, | |
152 Optional<uint64_t>(), | |
153 tester_); | |
154 EXPECT_TRUE_WAIT(tester_->GenerationCompleted(), kGenerationTimeoutMs); | |
155 EXPECT_FALSE(tester_->certificate()); | |
156 } | |
157 | |
158 } // namespace rtc | |
OLD | NEW |