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

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: Merge blink and chromium code; fixing issues from review 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 "content/common/experiments/api_key.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/test/simple_test_clock.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace content {
13
14 namespace {
15
16 const char* kSampleAPIKey =
17 "Signature|https://valid.example.com|Frobulate|1458766277";
18
19 const char* kExpectedAPIKeySignature = "Signature";
20 const char* kExpectedAPIKeyData =
21 "https://valid.example.com|Frobulate|1458766277";
22 const char* kExpectedAPIName = "Frobulate";
23 const char* kExpectedOrigin = "https://valid.example.com";
24 const uint64_t kExpectedExpiry = 1458766277;
25
26 // The key should not be valid for this origin, or for this API.
27 const char* kInvalidOrigin = "https://invalid.example.com";
28 const char* kInvalidAPIName = "Grokalyze";
29
30 // The key should be valid if the current time is kValidTimestamp or earlier.
31 double kValidTimestamp = 1458766276.0;
32
33 // The key should be invalid if the current time is kInvalidTimestamp or later.
34 double kInvalidTimestamp = 1458766278.0;
35
36 // Various ill-formed API keys. These should all fail to parse.
37 const char* kInvalidAPIKeys[] = {
38 // Invalid - only one part
39 "abcde",
40 // Not enough parts
41 "https://valid.example.com|APIName|1458766277",
42 // Delimiter in API Name
43 "Signature|https://valid.example.com|API|Name|1458766277",
44 // Extra string field
45 "Signature|https://valid.example.com|APIName|1458766277|SomethingElse",
46 // Extra numeric field
47 "Signature|https://valid.example.com|APIName|1458766277|1458766277",
48 // Non-numeric expiry timestamp
49 "Signature|https://valid.example.com|APIName|abcdefghij",
50 // Non-numeric expiry timestamp
51 "Signature|https://valid.example.com|APIName|1458766277x",
52 // Negative expiry timestamp
53 "Signature|https://valid.example.com|APIName|-1458766277"};
54 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
55
56 } // namespace
57
58 class ApiKeyTest : public testing::Test {
59 protected:
60 bool ValidateOrigin(ApiKey* api_key, const char* origin) {
61 return api_key->ValidateOrigin(std::string(origin));
62 }
63
64 bool ValidateApiName(ApiKey* api_key, const char* api_name) {
65 return api_key->ValidateApiName(std::string(api_name));
66 }
67
68 bool ValidateDate(ApiKey* api_key, const base::Time& now) {
69 return api_key->ValidateDate(now);
70 }
71 };
72
73 TEST_F(ApiKeyTest, ParseNullString) {
74 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(std::string());
75 EXPECT_FALSE(empty_key);
76 }
77
78 TEST_F(ApiKeyTest, ParseEmptyString) {
79 scoped_ptr<ApiKey> empty_key = ApiKey::Parse("");
80 EXPECT_FALSE(empty_key);
81 }
82
83 TEST_F(ApiKeyTest, ParseInvalidStrings) {
84 for (size_t i = 0; i < kNumInvalidAPIKeys; ++i) {
85 scoped_ptr<ApiKey> empty_key = ApiKey::Parse(kInvalidAPIKeys[i]);
86 EXPECT_FALSE(empty_key) << "Invalid API Key should not parse: "
87 << kInvalidAPIKeys[i];
88 }
89 }
90
91 TEST_F(ApiKeyTest, ParseValidKeyString) {
92 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
93 ASSERT_TRUE(key);
94 EXPECT_EQ(kExpectedAPIName, key->api_name());
95 EXPECT_EQ(kExpectedAPIKeySignature, key->signature());
96 EXPECT_EQ(kExpectedAPIKeyData, key->data());
97 EXPECT_EQ(GURL(kExpectedOrigin), key->origin());
98 EXPECT_EQ(kExpectedExpiry, key->expiry_timestamp());
99 }
100
101 TEST_F(ApiKeyTest, ValidateValidKey) {
102 scoped_ptr<ApiKey> key = ApiKey::Parse(kSampleAPIKey);
103 ASSERT_TRUE(key);
104 EXPECT_TRUE(ValidateOrigin(key.get(), kExpectedOrigin));
105 EXPECT_FALSE(ValidateOrigin(key.get(), kInvalidOrigin));
106 EXPECT_TRUE(ValidateApiName(key.get(), kExpectedAPIName));
107 EXPECT_FALSE(ValidateApiName(key.get(), kInvalidAPIName));
108 EXPECT_TRUE(
109 ValidateDate(key.get(), base::Time::FromDoubleT(kValidTimestamp)));
110 EXPECT_FALSE(
111 ValidateDate(key.get(), base::Time::FromDoubleT(kInvalidTimestamp)));
112 }
113
114 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698