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

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

Issue 1898383003: RTCCertificate serialization. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebase with master (std::unique_ptr) Created 4 years, 7 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/rtccertificate.cc ('k') | webrtc/base/rtccertificate_unittests.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 std::unique_ptr<SSLIdentity> identity(
40 SSLIdentity::Generate(kTestCertCommonName, KeyParams::ECDSA()));
41 RTC_CHECK(identity);
42 return RTCCertificate::Create(std::move(identity));
43 }
44
38 // Timestamp note: 45 // Timestamp note:
39 // All timestamps in this unittest are expressed in number of seconds since 46 // 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, 47 // epoch, 1970-01-01T00:00:00Z (UTC). The RTCCertificate interface uses ms,
41 // but only seconds-precision is supported by SSLCertificate. To make the 48 // but only seconds-precision is supported by SSLCertificate. To make the
42 // tests clearer we convert everything to seconds since the precision matters 49 // tests clearer we convert everything to seconds since the precision matters
43 // when generating certificates or comparing timestamps. 50 // when generating certificates or comparing timestamps.
44 // As a result, ExpiresSeconds and HasExpiredSeconds are used instead of 51 // As a result, ExpiresSeconds and HasExpiredSeconds are used instead of
45 // RTCCertificate::Expires and ::HasExpired for ms -> s conversion. 52 // RTCCertificate::Expires and ::HasExpired for ms -> s conversion.
46 53
47 uint64_t NowSeconds() const { 54 uint64_t NowSeconds() const {
(...skipping 30 matching lines...) Expand all
78 params.key_params = KeyParams::ECDSA(); 85 params.key_params = KeyParams::ECDSA();
79 86
80 std::unique_ptr<SSLIdentity> identity(SSLIdentity::GenerateForTest(params)); 87 std::unique_ptr<SSLIdentity> identity(SSLIdentity::GenerateForTest(params));
81 return RTCCertificate::Create(std::move(identity)); 88 return RTCCertificate::Create(std::move(identity));
82 } 89 }
83 }; 90 };
84 91
85 TEST_F(RTCCertificateTest, NewCertificateNotExpired) { 92 TEST_F(RTCCertificateTest, NewCertificateNotExpired) {
86 // Generate a real certificate without specifying the expiration time. 93 // Generate a real certificate without specifying the expiration time.
87 // Certificate type doesn't matter, using ECDSA because it's fast to generate. 94 // Certificate type doesn't matter, using ECDSA because it's fast to generate.
88 std::unique_ptr<SSLIdentity> identity( 95 scoped_refptr<RTCCertificate> certificate = GenerateECDSA();
89 SSLIdentity::Generate(kTestCertCommonName, KeyParams::ECDSA()));
90 scoped_refptr<RTCCertificate> certificate =
91 RTCCertificate::Create(std::move(identity));
92 96
93 uint64_t now = NowSeconds(); 97 uint64_t now = NowSeconds();
94 EXPECT_FALSE(HasExpiredSeconds(certificate, now)); 98 EXPECT_FALSE(HasExpiredSeconds(certificate, now));
95 // Even without specifying the expiration time we would expect it to be valid 99 // Even without specifying the expiration time we would expect it to be valid
96 // for at least half an hour. 100 // for at least half an hour.
97 EXPECT_FALSE(HasExpiredSeconds(certificate, now + 30*60)); 101 EXPECT_FALSE(HasExpiredSeconds(certificate, now + 30*60));
98 } 102 }
99 103
100 TEST_F(RTCCertificateTest, UsesExpiresAskedFor) { 104 TEST_F(RTCCertificateTest, UsesExpiresAskedFor) {
101 uint64_t now = NowSeconds(); 105 uint64_t now = NowSeconds();
102 scoped_refptr<RTCCertificate> certificate = 106 scoped_refptr<RTCCertificate> certificate =
103 GenerateCertificateWithExpires(now); 107 GenerateCertificateWithExpires(now);
104 EXPECT_EQ(now, ExpiresSeconds(certificate)); 108 EXPECT_EQ(now, ExpiresSeconds(certificate));
105 } 109 }
106 110
107 TEST_F(RTCCertificateTest, ExpiresInOneSecond) { 111 TEST_F(RTCCertificateTest, ExpiresInOneSecond) {
108 // Generate a certificate that expires in 1s. 112 // Generate a certificate that expires in 1s.
109 uint64_t now = NowSeconds(); 113 uint64_t now = NowSeconds();
110 scoped_refptr<RTCCertificate> certificate = 114 scoped_refptr<RTCCertificate> certificate =
111 GenerateCertificateWithExpires(now + 1); 115 GenerateCertificateWithExpires(now + 1);
112 // Now it should not have expired. 116 // Now it should not have expired.
113 EXPECT_FALSE(HasExpiredSeconds(certificate, now)); 117 EXPECT_FALSE(HasExpiredSeconds(certificate, now));
114 // In 2s it should have expired. 118 // In 2s it should have expired.
115 EXPECT_TRUE(HasExpiredSeconds(certificate, now + 2)); 119 EXPECT_TRUE(HasExpiredSeconds(certificate, now + 2));
116 } 120 }
117 121
122 TEST_F(RTCCertificateTest, DifferentCertificatesNotEqual) {
123 scoped_refptr<RTCCertificate> a = GenerateECDSA();
124 scoped_refptr<RTCCertificate> b = GenerateECDSA();
125 EXPECT_TRUE(*a != *b);
126 }
127
128 TEST_F(RTCCertificateTest, CloneWithPEMSerialization) {
129 scoped_refptr<RTCCertificate> orig = GenerateECDSA();
130
131 // To PEM.
132 RTCCertificatePEM orig_pem = orig->ToPEM();
133 // Clone from PEM.
134 scoped_refptr<RTCCertificate> clone = RTCCertificate::FromPEM(orig_pem);
135 EXPECT_TRUE(clone);
136 EXPECT_TRUE(*orig == *clone);
137 EXPECT_EQ(orig->Expires(), clone->Expires());
138 }
139
118 } // namespace rtc 140 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/base/rtccertificate.cc ('k') | webrtc/base/rtccertificate_unittests.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698