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

Side by Side Diff: content/common/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: Nits Created 4 years, 11 months 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
« no previous file with comments | « content/common/experiments/api_key.cc ('k') | content/content_common.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "content/common/experiments/api_key.h"
6
7 #include "base/macros.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/string_util.h"
10 #include "base/test/simple_test_clock.h"
11 #include "base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace content {
15
16 namespace {
17
18 const char* kSampleAPIKey =
19 "Signature|https://valid.example.com|Frobulate|1458766277";
20
21 const char* kExpectedAPIKeySignature = "Signature";
22 const char* kExpectedAPIKeyData =
23 "https://valid.example.com|Frobulate|1458766277";
24 const char* kExpectedAPIName = "Frobulate";
25 const char* kExpectedOrigin = "https://valid.example.com";
26 const uint64_t kExpectedExpiry = 1458766277;
27
28 // The key should not be valid for this origin, or for this API.
29 const char* kInvalidOrigin = "https://invalid.example.com";
30 const char* kInsecureOrigin = "http://valid.example.com";
31 const char* kInvalidAPIName = "Grokalyze";
32
33 // The key should be valid if the current time is kValidTimestamp or earlier.
34 double kValidTimestamp = 1458766276.0;
35
36 // The key should be invalid if the current time is kInvalidTimestamp or later.
37 double kInvalidTimestamp = 1458766278.0;
38
39 // Various ill-formed API keys. These should all fail to parse.
40 const char* kInvalidAPIKeys[] = {
41 // Invalid - only one part
42 "abcde",
43 // Not enough parts
44 "https://valid.example.com|APIName|1458766277",
45 // Delimiter in API Name
46 "Signature|https://valid.example.com|API|Name|1458766277",
47 // Extra string field
48 "Signature|https://valid.example.com|APIName|1458766277|SomethingElse",
49 // Extra numeric field
50 "Signature|https://valid.example.com|APIName|1458766277|1458766277",
51 // Non-numeric expiry timestamp
52 "Signature|https://valid.example.com|APIName|abcdefghij",
53 "Signature|https://valid.example.com|APIName|1458766277x",
54 // Negative expiry timestamp
55 "Signature|https://valid.example.com|APIName|-1458766277",
56 // Origin not a proper origin URL
57 "Signature|abcdef|APIName|1458766277",
58 "Signature|data:text/plain,abcdef|APIName|1458766277",
59 "Signature|javascript:alert(1)|APIName|1458766277"};
60 const size_t kNumInvalidAPIKeys = arraysize(kInvalidAPIKeys);
61
62 } // namespace
63
64 class ApiKeyTest : public testing::Test {
65 protected:
66 bool ValidateOrigin(ApiKey* api_key, const char* origin) {
67 return api_key->ValidateOrigin(origin);
68 }
69
70 bool ValidateApiName(ApiKey* api_key, const char* api_name) {
71 return api_key->ValidateApiName(api_name);
72 }
73
74 bool ValidateDate(ApiKey* api_key, const base::Time& now) {
75 return api_key->ValidateDate(now);
76 }
77 };
78
79 TEST_F(ApiKeyTest, ParseEmptyString) {
80 scoped_ptr<ApiKey> empty_key = ApiKey::Parse("");
81 EXPECT_FALSE(empty_key);
82 }
83
84 TEST_F(ApiKeyTest, ParseInvalidStrings) {
85 for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) {
86 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(kInvalidAPIKeys[i]);
87 EXPECT_FALSE(empty_key) << "Invalid API Key should not parse: "
88 << kInvalidAPIKeys[i];
89 }
90 }
91
92 TEST_F(ApiKeyTest, ParseValidKeyString) {
93 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
94 ASSERT_TRUE(key);
95 EXPECT_EQ(kExpectedAPIName, key->api_name());
96 EXPECT_EQ(kExpectedAPIKeySignature, key->signature());
97 EXPECT_EQ(kExpectedAPIKeyData, key->data());
98 EXPECT_EQ(GURL(kExpectedOrigin), key->origin());
99 EXPECT_EQ(kExpectedExpiry, key->expiry_timestamp());
100 }
101
102 TEST_F(ApiKeyTest, ValidateValidKey) {
103 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
104 ASSERT_TRUE(key);
105 EXPECT_TRUE(ValidateOrigin(key.get(), kExpectedOrigin));
106 EXPECT_FALSE(ValidateOrigin(key.get(), kInvalidOrigin));
107 EXPECT_FALSE(ValidateOrigin(key.get(), kInsecureOrigin));
108 EXPECT_TRUE(ValidateApiName(key.get(), kExpectedAPIName));
109 EXPECT_FALSE(ValidateApiName(key.get(), kInvalidAPIName));
110 EXPECT_TRUE(
111 ValidateDate(key.get(), base::Time::FromDoubleT(kValidTimestamp)));
112 EXPECT_FALSE(
113 ValidateDate(key.get(), base::Time::FromDoubleT(kInvalidTimestamp)));
114 }
115
116 TEST_F(ApiKeyTest, KeyIsAppropriateForOriginAndAPI) {
117 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
118 ASSERT_TRUE(key);
119 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, kExpectedAPIName));
120 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin,
121 base::ToUpperASCII(kExpectedAPIName)));
122 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin,
123 base::ToLowerASCII(kExpectedAPIName)));
124 EXPECT_FALSE(key->IsAppropriate(kInvalidOrigin, kExpectedAPIName));
125 EXPECT_FALSE(key->IsAppropriate(kInsecureOrigin, kExpectedAPIName));
126 EXPECT_FALSE(key->IsAppropriate(kExpectedOrigin, kInvalidAPIName));
127 }
128
129 } // namespace content
OLDNEW
« no previous file with comments | « content/common/experiments/api_key.cc ('k') | content/content_common.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698