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

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: Rebase 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..055ca5b3ad6229c28f8fdf27e34343ccfac03397
--- /dev/null
+++ b/content/browser/experiments/api_key_unittest.cc
@@ -0,0 +1,81 @@
+// 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* kFrobulateAPIName = "Frobulate";
+const char* kFrobulateEnabledOrigin = "https://jpchase.github.io";
+
+const char* kGoodAPIKey =
+ "Signature|https://jpchase.github.io|Frobulate|1458766277";
chasej 2015/12/15 19:43:41 I think it would help test readability/understandi
iclelland 2015/12/15 21:22:24 How about if I add another constant, above these o
chasej 2015/12/16 15:41:17 Adding another constant for "right now" is a good
iclelland 2015/12/16 17:04:37 Done -- I've reworked the tests, and now have kVal
+const char* kGoodAPIKeySignature = "Signature";
+const char* kGoodAPIKeyData =
+ "https://jpchase.github.io|Frobulate|1458766277";
+
+const char* kBadDateAPIKey =
+ "Signature|https://jpchase.github.io|Frobulate|1458766275";
+} // namespace
+
+class ApiKeyTest : public testing::Test {
+ protected:
+ ApiKeyTest() {}
+
+ ~ApiKeyTest() override {}
+
+ void SetUp() override {
+ clock_.SetNow(base::Time::FromDoubleT(1458766276.0));
+ }
+
+ base::SimpleTestClock& clock() { return clock_; }
+
+ private:
+ base::SimpleTestClock clock_;
+};
+
+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(kGoodAPIKey);
+ ASSERT_TRUE(key);
+ EXPECT_EQ(kFrobulateAPIName, key->api_name());
+ EXPECT_EQ(kGoodAPIKeySignature, key->signature());
+ EXPECT_EQ(kGoodAPIKeyData, key->data());
+ EXPECT_EQ(GURL(kFrobulateEnabledOrigin), key->origin());
+}
+
+TEST_F(ApiKeyTest, ValidateValidKey) {
+ scoped_ptr<ApiKey> key = ApiKey::Parse(kGoodAPIKey);
+ ASSERT_TRUE(key);
+ EXPECT_TRUE(key->IsValidNow(clock().Now()));
+}
+
+TEST_F(ApiKeyTest, ValidateBadDate) {
+ scoped_ptr<ApiKey> bad_key = ApiKey::Parse(kBadDateAPIKey);
+ ASSERT_TRUE(bad_key);
+ EXPECT_FALSE(bad_key->IsValidNow(clock().Now()));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698