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

Side by Side Diff: third_party/WebKit/Source/core/experiments/APIKeyTest.cpp

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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "core/experiments/APIKey.h"
7
8 #include <gtest/gtest.h>
9
10 namespace blink {
11 namespace {
12
13 // Various ill-formed API keys. These should all fail to parse
14 const char* kInvalidAPIKeys[] = {
15 "abcde", // Invalid - only one part
16 "https://valid.example.com|Frobulate|1458766277", // Not enough parts
17 "Signature|https://valid.example.com|Frobulate|SomethingElse|1458766277" , // Delimiter in API Name
18 "Signature|https://valid.example.com|SomethingElse|1458766277|SomethingE lse", // Extra string field
19 "Signature|https://valid.example.com|SomethingElse|1458766277|1458766277 ", // Extra numeric field
20 "Signature|https://valid.example.com|Frobulate|abcdefghij", // Non-numer ic expiry timestamp
21 "Signature|https://valid.example.com|Frobulate|1458766277x", // Non-nume ric expiry timestamp
22 "Signature|https://valid.example.com|Frobulate|-1458766277" // Negative expiry timestamp
23 };
24 const size_t kNumInvalidAPIKeys = sizeof(kInvalidAPIKeys) / sizeof(const cha r*);
25
26 // Well-formed API key for parse testing
27 const char* kSampleAPIKey = "AnySignatureWillDo|https://valid.example.com|Fr obulate|1458766277";
28
29 // The key should be valid for this origin
30 const char* kValidOrigin = "https://valid.example.com";
31
32 // The key should not be valid for this origin
33 const char* kInvalidOrigin = "https://invalid.example.com";
34
35 // The key should be valid for this API
36 const char* kValidAPIName = "Frobulate";
37
38 // The key should not be valid for this API
39 const char* kInvalidAPIName = "Grokalyze";
40
41 // The key should be valid if the current time is kValidTime or earlier
42 uint64_t kValidTime = 1458766276;
43
44 // The key should be invalid if the current time is kInvalidTime or later
45 uint64_t kInvalidTime = 1458766278;
46
47 } // namespace
48
49 class APIKeyTest : public ::testing::Test {
50 protected:
51 APIKeyTest()
52 {
53 }
54
55 ~APIKeyTest()
56 {
57 }
58
59 bool validateAPIName(const APIKey* key, const String& apiName)
60 {
61 return key->validateApiName(apiName);
62 }
63
64 bool validateOrigin(const APIKey* key, const String& originText)
65 {
66 return key->validateOrigin(originText);
67 }
68
69 bool validateDate(const APIKey* key, uint64_t now)
70 {
71 return key->validateDate(now);
72 }
73 };
74
75 TEST_F(APIKeyTest, ParseNullString)
76 {
77 RefPtrWillBeRawPtr<APIKey> emptyKey = APIKey::parse(String());
78 EXPECT_FALSE(emptyKey);
79 }
80
81 TEST_F(APIKeyTest, ParseEmptyString)
82 {
83 RefPtrWillBeRawPtr<APIKey> emptyKey = APIKey::parse("");
84 EXPECT_FALSE(emptyKey);
85 }
86
87 TEST_F(APIKeyTest, ParseInvalidStrings)
88 {
89 for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) {
90 RefPtrWillBeRawPtr<APIKey> emptyKey = APIKey::parse(kInvalidAPIKeys[i]);
91 EXPECT_FALSE(emptyKey) << "Invalid API Key should not parse: " << kInval idAPIKeys[i];
92 }
93 }
94
95 TEST_F(APIKeyTest, ParseValidKeyString)
96 {
97 RefPtrWillBeRawPtr<APIKey> key = APIKey::parse(kSampleAPIKey);
98 ASSERT_TRUE(key);
99 }
100
101 TEST_F(APIKeyTest, TestInternalKeyValidation)
102 {
103 RefPtrWillBeRawPtr<APIKey> key = APIKey::parse(kSampleAPIKey);
104 EXPECT_TRUE(validateAPIName(key.get(), kValidAPIName));
105 EXPECT_FALSE(validateAPIName(key.get(), kInvalidAPIName));
106 EXPECT_TRUE(validateOrigin(key.get(), kValidOrigin));
107 EXPECT_FALSE(validateOrigin(key.get(), kInvalidOrigin));
108 EXPECT_TRUE(validateDate(key.get(), kValidTime));
109 EXPECT_FALSE(validateDate(key.get(), kInvalidTime));
110 }
111
112 TEST_F(APIKeyTest, TestPublicApiKeyValidation)
113 {
114 RefPtrWillBeRawPtr<APIKey> key = APIKey::parse(kSampleAPIKey);
115 EXPECT_TRUE(key->isValid(kValidOrigin, kValidAPIName, kValidTime));
116 EXPECT_FALSE(key->isValid(kInvalidOrigin, kValidAPIName, kValidTime));
117 EXPECT_FALSE(key->isValid(kValidOrigin, kInvalidAPIName, kValidTime));
118 EXPECT_FALSE(key->isValid(kValidOrigin, kValidAPIName, kInvalidTime));
119 }
120
121 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698