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

Unified Diff: content/browser/experiments/api_key_unittest.cc

Issue 1521063003: Add API Key parsing for experimental APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing feedback from PS#6 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: content/browser/experiments/api_key_unittest.cc
diff --git a/content/browser/experiments/api_key_unittest.cc b/content/browser/experiments/api_key_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f236c0eac7ef4b719d080b87b206df4e9f2bbecd
--- /dev/null
+++ b/content/browser/experiments/api_key_unittest.cc
@@ -0,0 +1,70 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/experiments/api_key.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "base/test/simple_test_clock.h"
+#include "base/time/time.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+namespace {
+
+const char* kSampleAPIKey =
+ "Signature|https://valid.example.com|Frobulate|1458766277";
+
+const char* kExpectedAPIKeySignature = "Signature";
+const char* kExpectedAPIKeyData =
+ "https://valid.example.com|Frobulate|1458766277";
+const char* kExpectedAPIName = "Frobulate";
+const char* kExpectedOrigin = "https://valid.example.com";
+const uint64_t kExpectedExpiry = 1458766277;
+
+double kValidTimestamp = 1458766276.0;
+double kInvalidTimestamp = 1458766278.0;
+
+} // namespace
+
+class ApiKeyTest : public testing::Test {};
+
+TEST_F(ApiKeyTest, ParseNullString) {
+ scoped_ptr<ApiKey> empty_key = ApiKey::Parse(std::string());
+ EXPECT_FALSE(empty_key);
+}
+
+TEST_F(ApiKeyTest, ParseEmptyString) {
+ scoped_ptr<ApiKey> empty_key = ApiKey::Parse("");
+ EXPECT_FALSE(empty_key);
+}
+
+TEST_F(ApiKeyTest, ParseInvalidString) {
+ scoped_ptr<ApiKey> empty_key = ApiKey::Parse("abcdef");
+ EXPECT_FALSE(empty_key);
+}
+
+TEST_F(ApiKeyTest, ParseValidKeyString) {
+ scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
+ ASSERT_TRUE(key);
+ EXPECT_EQ(kExpectedAPIName, key->api_name());
+ EXPECT_EQ(kExpectedAPIKeySignature, key->signature());
+ EXPECT_EQ(kExpectedAPIKeyData, key->data());
+ EXPECT_EQ(GURL(kExpectedOrigin), key->origin());
+ EXPECT_EQ(kExpectedExpiry, key->expiry_timestamp());
+}
+
+TEST_F(ApiKeyTest, ValidateWhenNotExpired) {
+ scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
+ ASSERT_TRUE(key);
+ EXPECT_TRUE(key->IsValidNow(base::Time::FromDoubleT(kValidTimestamp)));
+}
+
+TEST_F(ApiKeyTest, ValidateWhenExpired) {
+ scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
+ ASSERT_TRUE(key);
+ EXPECT_FALSE(key->IsValidNow(base::Time::FromDoubleT(kInvalidTimestamp)));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698