Chromium Code Reviews| Index: content/common/experiments/api_key_unittest.cc |
| diff --git a/content/common/experiments/api_key_unittest.cc b/content/common/experiments/api_key_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6e2780ff8a88f1d9326880a23c2131e69337f892 |
| --- /dev/null |
| +++ b/content/common/experiments/api_key_unittest.cc |
| @@ -0,0 +1,114 @@ |
| +// 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/common/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; |
| + |
| +// The key should not be valid for this origin, or for this API. |
| +const char* kInvalidOrigin = "https://invalid.example.com"; |
| +const char* kInvalidAPIName = "Grokalyze"; |
| + |
| +// The key should be valid if the current time is kValidTimestamp or earlier. |
| +double kValidTimestamp = 1458766276.0; |
| + |
| +// The key should be invalid if the current time is kInvalidTimestamp or later. |
| +double kInvalidTimestamp = 1458766278.0; |
| + |
| +// Various ill-formed API keys. These should all fail to parse. |
| +const char* kInvalidAPIKeys[] = { |
| + // Invalid - only one part |
| + "abcde", |
| + // Not enough parts |
| + "https://valid.example.com|APIName|1458766277", |
| + // Delimiter in API Name |
| + "Signature|https://valid.example.com|API|Name|1458766277", |
| + // Extra string field |
| + "Signature|https://valid.example.com|APIName|1458766277|SomethingElse", |
| + // Extra numeric field |
| + "Signature|https://valid.example.com|APIName|1458766277|1458766277", |
| + // Non-numeric expiry timestamp |
| + "Signature|https://valid.example.com|APIName|abcdefghij", |
| + // Non-numeric expiry timestamp |
| + "Signature|https://valid.example.com|APIName|1458766277x", |
| + // Negative expiry timestamp |
| + "Signature|https://valid.example.com|APIName|-1458766277"}; |
| +const size_t kNumInvalidAPIKeys = sizeof(kInvalidAPIKeys) / sizeof(const char*); |
|
Marijn Kruisselbrink
2016/01/05 00:59:14
you probably want to use arraysize (from base/macr
iclelland
2016/01/05 20:26:31
I saw other instances in the codebase, but didn't
|
| + |
| +} // namespace |
| + |
| +class ApiKeyTest : public testing::Test { |
| + protected: |
| + bool ValidateOrigin(ApiKey* api_key, const char* origin) { |
| + return api_key->ValidateOrigin(std::string(origin)); |
| + } |
| + |
| + bool ValidateApiName(ApiKey* api_key, const char* api_name) { |
| + return api_key->ValidateApiName(std::string(api_name)); |
| + } |
| + |
| + bool ValidateDate(ApiKey* api_key, const base::Time& now) { |
| + return api_key->ValidateDate(now); |
| + } |
| +}; |
| + |
| +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, ParseInvalidStrings) { |
| + for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) { |
| + scoped_ptr<ApiKey> empty_key = ApiKey::Parse(kInvalidAPIKeys[i]); |
| + EXPECT_FALSE(empty_key) << "Invalid API Key should not parse: " |
| + << kInvalidAPIKeys[i]; |
| + } |
| +} |
| + |
| +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, ValidateValidKey) { |
| + scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey); |
| + ASSERT_TRUE(key); |
| + EXPECT_TRUE(ValidateOrigin(key.get(), kExpectedOrigin)); |
| + EXPECT_FALSE(ValidateOrigin(key.get(), kInvalidOrigin)); |
| + EXPECT_TRUE(ValidateApiName(key.get(), kExpectedAPIName)); |
| + EXPECT_FALSE(ValidateApiName(key.get(), kInvalidAPIName)); |
| + EXPECT_TRUE( |
| + ValidateDate(key.get(), base::Time::FromDoubleT(kValidTimestamp))); |
| + EXPECT_FALSE( |
| + ValidateDate(key.get(), base::Time::FromDoubleT(kInvalidTimestamp))); |
| +} |
| + |
| +} // namespace content |