OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... | |
28 static const char* kTestCertCommonName = "RTCCertificateTest's certificate"; | 28 static const char* kTestCertCommonName = "RTCCertificateTest's certificate"; |
29 | 29 |
30 } // namespace | 30 } // namespace |
31 | 31 |
32 class RTCCertificateTest : public testing::Test { | 32 class RTCCertificateTest : public testing::Test { |
33 public: | 33 public: |
34 RTCCertificateTest() {} | 34 RTCCertificateTest() {} |
35 ~RTCCertificateTest() {} | 35 ~RTCCertificateTest() {} |
36 | 36 |
37 protected: | 37 protected: |
38 scoped_refptr<RTCCertificate> GenerateECDSA() { | |
39 scoped_ptr<SSLIdentity> identity( | |
nisse-webrtc
2016/04/22 11:38:25
Do you really need the local variable?
hbos
2016/04/22 13:19:29
I do now that I RTC_CHECK. (I think I did before o
nisse-webrtc
2016/04/22 13:44:06
Ok. Can Generate ever fail?
hbos
2016/04/25 14:23:23
While it shouldn't happen under normal circumstanc
| |
40 SSLIdentity::Generate(kTestCertCommonName, KeyParams::ECDSA())); | |
41 return RTCCertificate::Create(std::move(identity)); | |
42 } | |
43 | |
38 // Timestamp note: | 44 // Timestamp note: |
39 // All timestamps in this unittest are expressed in number of seconds since | 45 // All timestamps in this unittest are expressed in number of seconds since |
40 // epoch, 1970-01-01T00:00:00Z (UTC). The RTCCertificate interface uses ms, | 46 // epoch, 1970-01-01T00:00:00Z (UTC). The RTCCertificate interface uses ms, |
41 // but only seconds-precision is supported by SSLCertificate. To make the | 47 // but only seconds-precision is supported by SSLCertificate. To make the |
42 // tests clearer we convert everything to seconds since the precision matters | 48 // tests clearer we convert everything to seconds since the precision matters |
43 // when generating certificates or comparing timestamps. | 49 // when generating certificates or comparing timestamps. |
44 // As a result, ExpiresSeconds and HasExpiredSeconds are used instead of | 50 // As a result, ExpiresSeconds and HasExpiredSeconds are used instead of |
45 // RTCCertificate::Expires and ::HasExpired for ms -> s conversion. | 51 // RTCCertificate::Expires and ::HasExpired for ms -> s conversion. |
46 | 52 |
47 uint64_t NowSeconds() const { | 53 uint64_t NowSeconds() const { |
(...skipping 30 matching lines...) Expand all Loading... | |
78 params.key_params = KeyParams::ECDSA(); | 84 params.key_params = KeyParams::ECDSA(); |
79 | 85 |
80 scoped_ptr<SSLIdentity> identity(SSLIdentity::GenerateForTest(params)); | 86 scoped_ptr<SSLIdentity> identity(SSLIdentity::GenerateForTest(params)); |
81 return RTCCertificate::Create(std::move(identity)); | 87 return RTCCertificate::Create(std::move(identity)); |
82 } | 88 } |
83 }; | 89 }; |
84 | 90 |
85 TEST_F(RTCCertificateTest, NewCertificateNotExpired) { | 91 TEST_F(RTCCertificateTest, NewCertificateNotExpired) { |
86 // Generate a real certificate without specifying the expiration time. | 92 // Generate a real certificate without specifying the expiration time. |
87 // Certificate type doesn't matter, using ECDSA because it's fast to generate. | 93 // Certificate type doesn't matter, using ECDSA because it's fast to generate. |
88 scoped_ptr<SSLIdentity> identity( | 94 scoped_refptr<RTCCertificate> certificate = GenerateECDSA(); |
89 SSLIdentity::Generate(kTestCertCommonName, KeyParams::ECDSA())); | |
90 scoped_refptr<RTCCertificate> certificate = | |
91 RTCCertificate::Create(std::move(identity)); | |
92 | 95 |
93 uint64_t now = NowSeconds(); | 96 uint64_t now = NowSeconds(); |
94 EXPECT_FALSE(HasExpiredSeconds(certificate, now)); | 97 EXPECT_FALSE(HasExpiredSeconds(certificate, now)); |
95 // Even without specifying the expiration time we would expect it to be valid | 98 // Even without specifying the expiration time we would expect it to be valid |
96 // for at least half an hour. | 99 // for at least half an hour. |
97 EXPECT_FALSE(HasExpiredSeconds(certificate, now + 30*60)); | 100 EXPECT_FALSE(HasExpiredSeconds(certificate, now + 30*60)); |
98 } | 101 } |
99 | 102 |
100 TEST_F(RTCCertificateTest, UsesExpiresAskedFor) { | 103 TEST_F(RTCCertificateTest, UsesExpiresAskedFor) { |
101 uint64_t now = NowSeconds(); | 104 uint64_t now = NowSeconds(); |
102 scoped_refptr<RTCCertificate> certificate = | 105 scoped_refptr<RTCCertificate> certificate = |
103 GenerateCertificateWithExpires(now); | 106 GenerateCertificateWithExpires(now); |
104 EXPECT_EQ(now, ExpiresSeconds(certificate)); | 107 EXPECT_EQ(now, ExpiresSeconds(certificate)); |
105 } | 108 } |
106 | 109 |
107 TEST_F(RTCCertificateTest, ExpiresInOneSecond) { | 110 TEST_F(RTCCertificateTest, ExpiresInOneSecond) { |
108 // Generate a certificate that expires in 1s. | 111 // Generate a certificate that expires in 1s. |
109 uint64_t now = NowSeconds(); | 112 uint64_t now = NowSeconds(); |
110 scoped_refptr<RTCCertificate> certificate = | 113 scoped_refptr<RTCCertificate> certificate = |
111 GenerateCertificateWithExpires(now + 1); | 114 GenerateCertificateWithExpires(now + 1); |
112 // Now it should not have expired. | 115 // Now it should not have expired. |
113 EXPECT_FALSE(HasExpiredSeconds(certificate, now)); | 116 EXPECT_FALSE(HasExpiredSeconds(certificate, now)); |
114 // In 2s it should have expired. | 117 // In 2s it should have expired. |
115 EXPECT_TRUE(HasExpiredSeconds(certificate, now + 2)); | 118 EXPECT_TRUE(HasExpiredSeconds(certificate, now + 2)); |
116 } | 119 } |
117 | 120 |
121 TEST_F(RTCCertificateTest, CloneWithPemSerialization) { | |
122 scoped_refptr<RTCCertificate> orig = GenerateECDSA(); | |
123 | |
124 // To PEM. | |
125 RTCCertificatePem orig_pem = orig->ToPem(); | |
126 // Clone from PEM. | |
127 scoped_refptr<RTCCertificate> clone = RTCCertificate::FromPem(orig_pem); | |
128 EXPECT_TRUE(clone); | |
129 // Make sure the clone's PEM is identical to the original. | |
torbjorng (webrtc)
2016/04/21 15:16:00
I'm not sure this is a robust comparison, since pr
nisse-webrtc
2016/04/22 11:38:25
I have been trying to forget asn.1... So I take it
hbos
2016/04/22 13:19:29
X509_cmp only compares the certificate? Ah, there'
| |
130 RTCCertificatePem clone_pem = clone->ToPem(); | |
131 EXPECT_EQ(orig_pem.private_key(), clone_pem.private_key()); | |
132 EXPECT_EQ(orig_pem.certificate(), clone_pem.certificate()); | |
133 // Make sure the clone's expiration time is the same as the original. | |
134 EXPECT_EQ(orig->Expires(), clone->Expires()); | |
135 } | |
136 | |
118 } // namespace rtc | 137 } // namespace rtc |
OLD | NEW |