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

Unified Diff: webrtc/base/rtccertificate_unittests.cc

Issue 1494103003: RTCCertificate::Expires() and ::HasExpired() implemented (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Test w FakeSSLCertificate w expires setter. HasExpired tested with expires 10s from now Created 5 years 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 side-by-side diff with in-line comments
Download patch
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

Powered by Google App Engine
This is Rietveld 408576698