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

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: Addressing feedback from PS#8 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(std::string(origin));
Marijn Kruisselbrink 2016/01/06 18:52:30 no need to explicitly convert the const char* to a
iclelland 2016/01/06 19:15:55 Done. (And below, as well)
68 }
69
70 bool ValidateApiName(ApiKey* api_key, const char* api_name) {
71 return api_key->ValidateApiName(std::string(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, ParseNullString) {
80 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(std::string());
Marijn Kruisselbrink 2016/01/06 18:52:30 std::string doesn't really have the concept of a n
iclelland 2016/01/06 19:15:55 Removed, thanks.
81 EXPECT_FALSE(empty_key);
82 }
83
84 TEST_F(ApiKeyTest, ParseEmptyString) {
85 scoped_ptr<ApiKey> empty_key = ApiKey::Parse("");
86 EXPECT_FALSE(empty_key);
87 }
88
89 TEST_F(ApiKeyTest, ParseInvalidStrings) {
90 for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) {
91 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(kInvalidAPIKeys[i]);
92 EXPECT_FALSE(empty_key) << "Invalid API Key should not parse: "
93 << kInvalidAPIKeys[i];
94 }
95 }
96
97 TEST_F(ApiKeyTest, ParseValidKeyString) {
98 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
99 ASSERT_TRUE(key);
100 EXPECT_EQ(kExpectedAPIName, key->api_name());
101 EXPECT_EQ(kExpectedAPIKeySignature, key->signature());
102 EXPECT_EQ(kExpectedAPIKeyData, key->data());
103 EXPECT_EQ(GURL(kExpectedOrigin), key->origin());
104 EXPECT_EQ(kExpectedExpiry, key->expiry_timestamp());
105 }
106
107 TEST_F(ApiKeyTest, ValidateValidKey) {
108 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
109 ASSERT_TRUE(key);
110 EXPECT_TRUE(ValidateOrigin(key.get(), kExpectedOrigin));
111 EXPECT_FALSE(ValidateOrigin(key.get(), kInvalidOrigin));
112 EXPECT_FALSE(ValidateOrigin(key.get(), kInsecureOrigin));
113 EXPECT_TRUE(ValidateApiName(key.get(), kExpectedAPIName));
114 EXPECT_FALSE(ValidateApiName(key.get(), kInvalidAPIName));
115 EXPECT_TRUE(
116 ValidateDate(key.get(), base::Time::FromDoubleT(kValidTimestamp)));
117 EXPECT_FALSE(
118 ValidateDate(key.get(), base::Time::FromDoubleT(kInvalidTimestamp)));
119 }
120
121 TEST_F(ApiKeyTest, KeyIsAppropriateForOriginAndAPI) {
122 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
123 ASSERT_TRUE(key);
124 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin, kExpectedAPIName));
125 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin,
126 base::ToUpperASCII(kExpectedAPIName)));
127 EXPECT_TRUE(key->IsAppropriate(kExpectedOrigin,
128 base::ToLowerASCII(kExpectedAPIName)));
129 EXPECT_FALSE(key->IsAppropriate(kInvalidOrigin, kExpectedAPIName));
130 EXPECT_FALSE(key->IsAppropriate(kInsecureOrigin, kExpectedAPIName));
131 EXPECT_FALSE(key->IsAppropriate(kExpectedOrigin, kInvalidAPIName));
132 }
133
134 } // 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