blob: 04a76764ec98c8a6fd3c889304fe0f203aac0977 [file] [log] [blame]
hbos3980d462015-12-09 05:26:49 -08001/*
2 * Copyright 2015 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
jbauch555604a2016-04-26 03:13:22 -070011#include <memory>
kwiberg0eb15ed2015-12-17 03:04:15 -080012#include <utility>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
15#include "rtc_base/fakesslidentity.h"
16#include "rtc_base/gunit.h"
17#include "rtc_base/logging.h"
18#include "rtc_base/rtccertificate.h"
19#include "rtc_base/safe_conversions.h"
20#include "rtc_base/sslidentity.h"
21#include "rtc_base/thread.h"
22#include "rtc_base/timeutils.h"
hbos3980d462015-12-09 05:26:49 -080023
24namespace rtc {
25
26namespace {
27
28static const char* kTestCertCommonName = "RTCCertificateTest's certificate";
29
30} // namespace
31
32class RTCCertificateTest : public testing::Test {
hbos3980d462015-12-09 05:26:49 -080033 protected:
hbos6b470a92016-04-28 05:14:21 -070034 scoped_refptr<RTCCertificate> GenerateECDSA() {
35 std::unique_ptr<SSLIdentity> identity(
36 SSLIdentity::Generate(kTestCertCommonName, KeyParams::ECDSA()));
37 RTC_CHECK(identity);
38 return RTCCertificate::Create(std::move(identity));
39 }
40
hbos3980d462015-12-09 05:26:49 -080041 // Timestamp note:
42 // All timestamps in this unittest are expressed in number of seconds since
43 // epoch, 1970-01-01T00:00:00Z (UTC). The RTCCertificate interface uses ms,
44 // but only seconds-precision is supported by SSLCertificate. To make the
45 // tests clearer we convert everything to seconds since the precision matters
46 // when generating certificates or comparing timestamps.
47 // As a result, ExpiresSeconds and HasExpiredSeconds are used instead of
48 // RTCCertificate::Expires and ::HasExpired for ms -> s conversion.
49
50 uint64_t NowSeconds() const {
51 return TimeNanos() / kNumNanosecsPerSec;
52 }
53
54 uint64_t ExpiresSeconds(const scoped_refptr<RTCCertificate>& cert) const {
55 uint64_t exp_ms = cert->Expires();
56 uint64_t exp_s = exp_ms / kNumMillisecsPerSec;
57 // Make sure this did not result in loss of precision.
58 RTC_CHECK_EQ(exp_s * kNumMillisecsPerSec, exp_ms);
59 return exp_s;
60 }
61
62 bool HasExpiredSeconds(const scoped_refptr<RTCCertificate>& cert,
63 uint64_t now_s) const {
64 return cert->HasExpired(now_s * kNumMillisecsPerSec);
65 }
66
67 // An RTC_CHECK ensures that |expires_s| this is in valid range of time_t as
68 // is required by SSLIdentityParams. On some 32-bit systems time_t is limited
69 // to < 2^31. On such systems this will fail for expiration times of year 2038
70 // or later.
71 scoped_refptr<RTCCertificate> GenerateCertificateWithExpires(
72 uint64_t expires_s) const {
73 RTC_CHECK(IsValueInRangeForNumericType<time_t>(expires_s));
74
75 SSLIdentityParams params;
76 params.common_name = kTestCertCommonName;
77 params.not_before = 0;
78 params.not_after = static_cast<time_t>(expires_s);
79 // Certificate type does not matter for our purposes, using ECDSA because it
80 // is fast to generate.
81 params.key_params = KeyParams::ECDSA();
82
jbauch555604a2016-04-26 03:13:22 -070083 std::unique_ptr<SSLIdentity> identity(SSLIdentity::GenerateForTest(params));
kwiberg0eb15ed2015-12-17 03:04:15 -080084 return RTCCertificate::Create(std::move(identity));
hbos3980d462015-12-09 05:26:49 -080085 }
86};
87
88TEST_F(RTCCertificateTest, NewCertificateNotExpired) {
89 // Generate a real certificate without specifying the expiration time.
90 // Certificate type doesn't matter, using ECDSA because it's fast to generate.
hbos6b470a92016-04-28 05:14:21 -070091 scoped_refptr<RTCCertificate> certificate = GenerateECDSA();
hbos3980d462015-12-09 05:26:49 -080092
93 uint64_t now = NowSeconds();
94 EXPECT_FALSE(HasExpiredSeconds(certificate, now));
95 // Even without specifying the expiration time we would expect it to be valid
96 // for at least half an hour.
97 EXPECT_FALSE(HasExpiredSeconds(certificate, now + 30*60));
98}
99
100TEST_F(RTCCertificateTest, UsesExpiresAskedFor) {
101 uint64_t now = NowSeconds();
102 scoped_refptr<RTCCertificate> certificate =
103 GenerateCertificateWithExpires(now);
104 EXPECT_EQ(now, ExpiresSeconds(certificate));
105}
106
107TEST_F(RTCCertificateTest, ExpiresInOneSecond) {
108 // Generate a certificate that expires in 1s.
109 uint64_t now = NowSeconds();
110 scoped_refptr<RTCCertificate> certificate =
111 GenerateCertificateWithExpires(now + 1);
112 // Now it should not have expired.
113 EXPECT_FALSE(HasExpiredSeconds(certificate, now));
114 // In 2s it should have expired.
115 EXPECT_TRUE(HasExpiredSeconds(certificate, now + 2));
116}
117
hbos6b470a92016-04-28 05:14:21 -0700118TEST_F(RTCCertificateTest, DifferentCertificatesNotEqual) {
119 scoped_refptr<RTCCertificate> a = GenerateECDSA();
120 scoped_refptr<RTCCertificate> b = GenerateECDSA();
121 EXPECT_TRUE(*a != *b);
122}
123
124TEST_F(RTCCertificateTest, CloneWithPEMSerialization) {
125 scoped_refptr<RTCCertificate> orig = GenerateECDSA();
126
127 // To PEM.
128 RTCCertificatePEM orig_pem = orig->ToPEM();
129 // Clone from PEM.
130 scoped_refptr<RTCCertificate> clone = RTCCertificate::FromPEM(orig_pem);
131 EXPECT_TRUE(clone);
132 EXPECT_TRUE(*orig == *clone);
133 EXPECT_EQ(orig->Expires(), clone->Expires());
134}
135
jbromanb9eaeba2016-10-20 10:27:21 -0700136TEST_F(RTCCertificateTest, FromPEMWithInvalidPEM) {
137 RTCCertificatePEM pem("not a valid PEM", "not a valid PEM");
138 scoped_refptr<RTCCertificate> certificate = RTCCertificate::FromPEM(pem);
139 EXPECT_FALSE(certificate);
140}
141
hbos3980d462015-12-09 05:26:49 -0800142} // namespace rtc