Index: webrtc/base/rtccertificate_unittests.cc |
diff --git a/webrtc/base/rtccertificate_unittests.cc b/webrtc/base/rtccertificate_unittests.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..20bbeeb90b576453d7dc4b5a67866bfce20abb98 |
--- /dev/null |
+++ b/webrtc/base/rtccertificate_unittests.cc |
@@ -0,0 +1,74 @@ |
+/* |
+ * Copyright 2015 The WebRTC Project Authors. All rights reserved. |
+ * |
+ * Use of this source code is governed by a BSD-style license |
+ * that can be found in the LICENSE file in the root of the source |
+ * tree. An additional intellectual property rights grant can be found |
+ * in the file PATENTS. All contributing project authors may |
+ * be found in the AUTHORS file in the root of the source tree. |
+ */ |
+ |
+#include "webrtc/base/checks.h" |
+#include "webrtc/base/fakesslidentity.h" |
+#include "webrtc/base/gunit.h" |
+#include "webrtc/base/logging.h" |
+#include "webrtc/base/rtccertificate.h" |
+#include "webrtc/base/safe_conversions.h" |
+#include "webrtc/base/scoped_ptr.h" |
+#include "webrtc/base/sslidentity.h" |
+#include "webrtc/base/thread.h" |
+#include "webrtc/base/timeutils.h" |
+ |
+namespace rtc { |
+ |
+class RTCCertificateTest : public testing::Test { |
+ public: |
+ RTCCertificateTest() {} |
+ ~RTCCertificateTest() {} |
+ |
+ protected: |
+ // Gets the current time in ms since epoch, 1970-01-01T00:00:00Z, rounded down |
+ // to seconds-precision (to match CreateCertificate being limited to |
+ // seconds-precision). |
+ uint64_t Now() const { |
+ uint64_t now = TimeNanos() / kNumNanosecsPerMillisec; |
+ return (now / 1000) * 1000; |
+ } |
+ |
+ // Creates a certificate with the specified |expiration_time|, expressed in |
+ // number of ms since epoch, 1970-01-01T00:00:00Z. The expiration time is |
+ // converted to seconds. RTC_CHECKs ensure that this does not result in loss |
+ // of precision and that the number of seconds is in valid range of int64_t as |
+ // required by FakeSSLIdentity. |
+ scoped_refptr<RTCCertificate> CreateCertificate(uint64_t expires_time) const { |
+ uint64_t expires_time_s = expires_time / kNumMillisecsPerSec; // ms -> s |
+ RTC_CHECK_EQ(expires_time_s * kNumMillisecsPerSec, expires_time); |
+ |
+ // We don't care what data our FakeSSLCertificate hold, we only care about |
+ // CertificateExpirationTime. |
+ FakeSSLCertificate ssl_cert("<invalid data>"); |
+ RTC_CHECK(rtc::IsValueInRangeForNumericType<int64_t>(expires_time_s)); |
+ ssl_cert.SetCertificateExpirationTime(expires_time_s); |
+ scoped_ptr<SSLIdentity> identity(new FakeSSLIdentity(ssl_cert)); |
+ |
+ return RTCCertificate::Create(identity.Pass()); |
+ } |
+}; |
+ |
+TEST_F(RTCCertificateTest, Expires) { |
+ uint64_t now = Now(); |
+ scoped_refptr<RTCCertificate> certificate = CreateCertificate(now); |
+ EXPECT_EQ(now, certificate->Expires()); |
+} |
+ |
+TEST_F(RTCCertificateTest, HasExpired) { |
+ uint64_t now = Now() - 10 * 1000; // 10s in the past |
+ EXPECT_TRUE(CreateCertificate(now)->HasExpired()); |
+} |
+ |
+TEST_F(RTCCertificateTest, HasNotExpired) { |
+ 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
|
+ EXPECT_FALSE(CreateCertificate(now)->HasExpired()); |
+} |
+ |
+} // namespace rtc |