| Index: third_party/WebKit/Source/core/experiments/APIKeyTest.cpp
|
| diff --git a/third_party/WebKit/Source/core/experiments/APIKeyTest.cpp b/third_party/WebKit/Source/core/experiments/APIKeyTest.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8c274b7e41debbd97c61452531357528d32c0cc4
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/core/experiments/APIKeyTest.cpp
|
| @@ -0,0 +1,121 @@
|
| +// 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 "config.h"
|
| +#include "core/experiments/APIKey.h"
|
| +
|
| +#include <gtest/gtest.h>
|
| +
|
| +namespace blink {
|
| +namespace {
|
| +
|
| + // Various ill-formed API keys. These should all fail to parse
|
| + const char* kInvalidAPIKeys[] = {
|
| + "abcde", // Invalid - only one part
|
| + "https://valid.example.com|Frobulate|1458766277", // Not enough parts
|
| + "Signature|https://valid.example.com|Frobulate|SomethingElse|1458766277", // Delimiter in API Name
|
| + "Signature|https://valid.example.com|SomethingElse|1458766277|SomethingElse", // Extra string field
|
| + "Signature|https://valid.example.com|SomethingElse|1458766277|1458766277", // Extra numeric field
|
| + "Signature|https://valid.example.com|Frobulate|abcdefghij", // Non-numeric expiry timestamp
|
| + "Signature|https://valid.example.com|Frobulate|1458766277x", // Non-numeric expiry timestamp
|
| + "Signature|https://valid.example.com|Frobulate|-1458766277" // Negative expiry timestamp
|
| + };
|
| + const size_t kNumInvalidAPIKeys = sizeof(kInvalidAPIKeys) / sizeof(const char*);
|
| +
|
| + // Well-formed API key for parse testing
|
| + const char* kSampleAPIKey = "AnySignatureWillDo|https://valid.example.com|Frobulate|1458766277";
|
| +
|
| + // The key should be valid for this origin
|
| + const char* kValidOrigin = "https://valid.example.com";
|
| +
|
| + // The key should not be valid for this origin
|
| + const char* kInvalidOrigin = "https://invalid.example.com";
|
| +
|
| + // The key should be valid for this API
|
| + const char* kValidAPIName = "Frobulate";
|
| +
|
| + // The key should not be valid for this API
|
| + const char* kInvalidAPIName = "Grokalyze";
|
| +
|
| + // The key should be valid if the current time is kValidTime or earlier
|
| + uint64_t kValidTime = 1458766276;
|
| +
|
| + // The key should be invalid if the current time is kInvalidTime or later
|
| + uint64_t kInvalidTime = 1458766278;
|
| +
|
| +} // namespace
|
| +
|
| +class APIKeyTest : public ::testing::Test {
|
| +protected:
|
| + APIKeyTest()
|
| + {
|
| + }
|
| +
|
| + ~APIKeyTest()
|
| + {
|
| + }
|
| +
|
| + bool validateAPIName(const APIKey* key, const String& apiName)
|
| + {
|
| + return key->validateApiName(apiName);
|
| + }
|
| +
|
| + bool validateOrigin(const APIKey* key, const String& originText)
|
| + {
|
| + return key->validateOrigin(originText);
|
| + }
|
| +
|
| + bool validateDate(const APIKey* key, uint64_t now)
|
| + {
|
| + return key->validateDate(now);
|
| + }
|
| +};
|
| +
|
| +TEST_F(APIKeyTest, ParseNullString)
|
| +{
|
| + RefPtrWillBeRawPtr<APIKey> emptyKey = APIKey::parse(String());
|
| + EXPECT_FALSE(emptyKey);
|
| +}
|
| +
|
| +TEST_F(APIKeyTest, ParseEmptyString)
|
| +{
|
| + RefPtrWillBeRawPtr<APIKey> emptyKey = APIKey::parse("");
|
| + EXPECT_FALSE(emptyKey);
|
| +}
|
| +
|
| +TEST_F(APIKeyTest, ParseInvalidStrings)
|
| +{
|
| + for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) {
|
| + RefPtrWillBeRawPtr<APIKey> emptyKey = APIKey::parse(kInvalidAPIKeys[i]);
|
| + EXPECT_FALSE(emptyKey) << "Invalid API Key should not parse: " << kInvalidAPIKeys[i];
|
| + }
|
| +}
|
| +
|
| +TEST_F(APIKeyTest, ParseValidKeyString)
|
| +{
|
| + RefPtrWillBeRawPtr<APIKey> key = APIKey::parse(kSampleAPIKey);
|
| + ASSERT_TRUE(key);
|
| +}
|
| +
|
| +TEST_F(APIKeyTest, TestInternalKeyValidation)
|
| +{
|
| + RefPtrWillBeRawPtr<APIKey> key = APIKey::parse(kSampleAPIKey);
|
| + EXPECT_TRUE(validateAPIName(key.get(), kValidAPIName));
|
| + EXPECT_FALSE(validateAPIName(key.get(), kInvalidAPIName));
|
| + EXPECT_TRUE(validateOrigin(key.get(), kValidOrigin));
|
| + EXPECT_FALSE(validateOrigin(key.get(), kInvalidOrigin));
|
| + EXPECT_TRUE(validateDate(key.get(), kValidTime));
|
| + EXPECT_FALSE(validateDate(key.get(), kInvalidTime));
|
| +}
|
| +
|
| +TEST_F(APIKeyTest, TestPublicApiKeyValidation)
|
| +{
|
| + RefPtrWillBeRawPtr<APIKey> key = APIKey::parse(kSampleAPIKey);
|
| + EXPECT_TRUE(key->isValid(kValidOrigin, kValidAPIName, kValidTime));
|
| + EXPECT_FALSE(key->isValid(kInvalidOrigin, kValidAPIName, kValidTime));
|
| + EXPECT_FALSE(key->isValid(kValidOrigin, kInvalidAPIName, kValidTime));
|
| + EXPECT_FALSE(key->isValid(kValidOrigin, kValidAPIName, kInvalidTime));
|
| +}
|
| +
|
| +} // namespace blink
|
|
|