Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 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 | |
| 11 #include "webrtc/base/checks.h" | |
| 12 #include "webrtc/base/fakesslidentity.h" | |
| 13 #include "webrtc/base/gunit.h" | |
| 14 #include "webrtc/base/logging.h" | |
| 15 #include "webrtc/base/rtccertificate.h" | |
| 16 #include "webrtc/base/safe_conversions.h" | |
| 17 #include "webrtc/base/scoped_ptr.h" | |
| 18 #include "webrtc/base/sslidentity.h" | |
| 19 #include "webrtc/base/thread.h" | |
| 20 #include "webrtc/base/timeutils.h" | |
| 21 | |
| 22 namespace rtc { | |
| 23 | |
| 24 class RTCCertificateTest : public testing::Test { | |
| 25 public: | |
| 26 RTCCertificateTest() {} | |
| 27 ~RTCCertificateTest() {} | |
| 28 | |
| 29 protected: | |
| 30 // Gets the current time in ms since epoch, 1970-01-01T00:00:00Z, rounded down | |
| 31 // to seconds-precision (to match CreateCertificate being limited to | |
| 32 // seconds-precision). | |
| 33 uint64_t Now() const { | |
| 34 uint64_t now = TimeNanos() / kNumNanosecsPerMillisec; | |
| 35 return (now / 1000) * 1000; | |
| 36 } | |
| 37 | |
| 38 // Creates a certificate with the specified |expiration_time|, expressed in | |
| 39 // number of ms since epoch, 1970-01-01T00:00:00Z. The expiration time is | |
| 40 // converted to seconds. RTC_CHECKs ensure that this does not result in loss | |
| 41 // of precision and that the number of seconds is in valid range of int64_t as | |
| 42 // required by FakeSSLIdentity. | |
| 43 scoped_refptr<RTCCertificate> CreateCertificate(uint64_t expires_time) const { | |
| 44 uint64_t expires_time_s = expires_time / kNumMillisecsPerSec; // ms -> s | |
| 45 RTC_CHECK_EQ(expires_time_s * kNumMillisecsPerSec, expires_time); | |
| 46 | |
| 47 // We don't care what data our FakeSSLCertificate hold, we only care about | |
| 48 // CertificateExpirationTime. | |
| 49 FakeSSLCertificate ssl_cert("<invalid data>"); | |
| 50 RTC_CHECK(rtc::IsValueInRangeForNumericType<int64_t>(expires_time_s)); | |
| 51 ssl_cert.SetCertificateExpirationTime(expires_time_s); | |
| 52 scoped_ptr<SSLIdentity> identity(new FakeSSLIdentity(ssl_cert)); | |
| 53 | |
| 54 return RTCCertificate::Create(identity.Pass()); | |
| 55 } | |
| 56 }; | |
| 57 | |
| 58 TEST_F(RTCCertificateTest, Expires) { | |
| 59 uint64_t now = Now(); | |
| 60 scoped_refptr<RTCCertificate> certificate = CreateCertificate(now); | |
| 61 EXPECT_EQ(now, certificate->Expires()); | |
| 62 } | |
| 63 | |
| 64 TEST_F(RTCCertificateTest, HasExpired) { | |
| 65 uint64_t now = Now() - 10 * 1000; // 10s in the past | |
| 66 EXPECT_TRUE(CreateCertificate(now)->HasExpired()); | |
| 67 } | |
| 68 | |
| 69 TEST_F(RTCCertificateTest, HasNotExpired) { | |
| 70 uint64_t now = Now() + 10 * 1000; // 10s in the future | |
|
hta-webrtc
2015/12/04 11:27:39
10 sec is probably OK. It's unlikely that we're go
| |
| 71 EXPECT_FALSE(CreateCertificate(now)->HasExpired()); | |
| 72 } | |
| 73 | |
| 74 } // namespace rtc | |
| OLD | NEW |