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

Side by Side Diff: content/common/origin_trials/trial_token_validator_unittest.cc

Issue 2116503004: Make Foreign Fetch an origin trial. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 4 years, 4 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/common/origin_trials/trial_token_validator.h" 5 #include "content/common/origin_trials/trial_token_validator.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <set> 8 #include <set>
9 #include <string> 9 #include <string>
10 10
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/test/simple_test_clock.h" 13 #include "base/test/simple_test_clock.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "content/public/common/content_client.h" 15 #include "content/public/common/content_client.h"
16 #include "content/public/common/origin_trial_policy.h" 16 #include "content/public/common/origin_trial_policy.h"
17 #include "net/http/http_response_headers.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h" 19 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 21
21 namespace content { 22 namespace content {
22 23
23 namespace { 24 namespace {
24 25
25 // This is a sample public key for testing the API. The corresponding private 26 // This is a sample public key for testing the API. The corresponding private
26 // key (use this to generate new samples for this test file) is: 27 // key (use this to generate new samples for this test file) is:
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 // Generate this token with the command (in tools/origin_trials): 81 // Generate this token with the command (in tools/origin_trials):
81 // generate_token.py valid.example.com Frobulate --expire-timestamp=1000000000 82 // generate_token.py valid.example.com Frobulate --expire-timestamp=1000000000
82 const char kExpiredToken[] = 83 const char kExpiredToken[] =
83 "AmHPUIXMaXe9jWW8kJeDFXolVjT93p4XMnK4+jMYd2pjqtFcYB1bUmdD8PunQKM+" 84 "AmHPUIXMaXe9jWW8kJeDFXolVjT93p4XMnK4+jMYd2pjqtFcYB1bUmdD8PunQKM+"
84 "RrOtlAwa0gPqqn+A8GTD3AQAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" 85 "RrOtlAwa0gPqqn+A8GTD3AQAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l"
85 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" 86 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5"
86 "IjogMTAwMDAwMDAwMH0="; 87 "IjogMTAwMDAwMDAwMH0=";
87 88
88 const char kUnparsableToken[] = "abcde"; 89 const char kUnparsableToken[] = "abcde";
89 90
91 // Well-formed token, for an insecure origin.
92 // Generate this token with the command (in tools/origin_trials):
93 // generate_token.py http://valid.example.com Frobulate
94 // --expire-timestamp=2000000000
95 const char kInsecureOriginToken[] =
96 "AjfC47H1q8/Ho5ALFkjkwf9CBK6oUUeRTlFc50Dj+eZEyGGKFIY2WTxMBfy8cLc3"
97 "E0nmFroDA3OmABmO5jMCFgkAAABXeyJvcmlnaW4iOiAiaHR0cDovL3ZhbGlkLmV4"
98 "YW1wbGUuY29tOjgwIiwgImZlYXR1cmUiOiAiRnJvYnVsYXRlIiwgImV4cGlyeSI6"
99 "IDIwMDAwMDAwMDB9";
100
90 class TestOriginTrialPolicy : public OriginTrialPolicy { 101 class TestOriginTrialPolicy : public OriginTrialPolicy {
91 public: 102 public:
92 base::StringPiece GetPublicKey() const override { 103 base::StringPiece GetPublicKey() const override {
93 return base::StringPiece(reinterpret_cast<const char*>(key_), 104 return base::StringPiece(reinterpret_cast<const char*>(key_),
94 arraysize(kTestPublicKey)); 105 arraysize(kTestPublicKey));
95 } 106 }
96 bool IsFeatureDisabled(base::StringPiece feature) const override { 107 bool IsFeatureDisabled(base::StringPiece feature) const override {
97 return disabled_features_.count(feature.as_string()) > 0; 108 return disabled_features_.count(feature.as_string()) > 0;
98 } 109 }
99 110
(...skipping 26 matching lines...) Expand all
126 TestOriginTrialPolicy origin_trial_policy_; 137 TestOriginTrialPolicy origin_trial_policy_;
127 }; 138 };
128 139
129 } // namespace 140 } // namespace
130 141
131 class TrialTokenValidatorTest : public testing::Test { 142 class TrialTokenValidatorTest : public testing::Test {
132 public: 143 public:
133 TrialTokenValidatorTest() 144 TrialTokenValidatorTest()
134 : appropriate_origin_(GURL(kAppropriateOrigin)), 145 : appropriate_origin_(GURL(kAppropriateOrigin)),
135 inappropriate_origin_(GURL(kInappropriateOrigin)), 146 inappropriate_origin_(GURL(kInappropriateOrigin)),
136 insecure_origin_(GURL(kInsecureOrigin)) { 147 insecure_origin_(GURL(kInsecureOrigin)),
148 response_headers_(new net::HttpResponseHeaders("")) {
137 SetPublicKey(kTestPublicKey); 149 SetPublicKey(kTestPublicKey);
138 SetContentClient(&test_content_client_); 150 SetContentClient(&test_content_client_);
139 } 151 }
140 152
141 ~TrialTokenValidatorTest() override { SetContentClient(nullptr); } 153 ~TrialTokenValidatorTest() override { SetContentClient(nullptr); }
142 154
143 void SetPublicKey(const uint8_t* key) { 155 void SetPublicKey(const uint8_t* key) {
144 test_content_client_.SetOriginTrialPublicKey(key); 156 test_content_client_.SetOriginTrialPublicKey(key);
145 } 157 }
146 158
147 void DisableFeature(const std::string& feature) { 159 void DisableFeature(const std::string& feature) {
148 test_content_client_.DisableFeature(feature); 160 test_content_client_.DisableFeature(feature);
149 } 161 }
150 162
151 const url::Origin appropriate_origin_; 163 const url::Origin appropriate_origin_;
152 const url::Origin inappropriate_origin_; 164 const url::Origin inappropriate_origin_;
153 const url::Origin insecure_origin_; 165 const url::Origin insecure_origin_;
154 166
167 scoped_refptr<net::HttpResponseHeaders> response_headers_;
168
155 private: 169 private:
156 TestContentClient test_content_client_; 170 TestContentClient test_content_client_;
157 }; 171 };
158 172
159 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { 173 TEST_F(TrialTokenValidatorTest, ValidateValidToken) {
160 std::string feature; 174 std::string feature;
161 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, 175 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success,
162 TrialTokenValidator::ValidateToken(kSampleToken, 176 TrialTokenValidator::ValidateToken(kSampleToken,
163 appropriate_origin_, &feature)); 177 appropriate_origin_, &feature));
164 EXPECT_EQ(kAppropriateFeatureName, feature); 178 EXPECT_EQ(kAppropriateFeatureName, feature);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 TrialTokenValidator::ValidateToken(kSampleToken, 225 TrialTokenValidator::ValidateToken(kSampleToken,
212 appropriate_origin_, &feature)); 226 appropriate_origin_, &feature));
213 EXPECT_EQ(kAppropriateFeatureName, feature); 227 EXPECT_EQ(kAppropriateFeatureName, feature);
214 // Disable the token's feature; it should no longer be valid 228 // Disable the token's feature; it should no longer be valid
215 DisableFeature(kAppropriateFeatureName); 229 DisableFeature(kAppropriateFeatureName);
216 EXPECT_EQ(blink::WebOriginTrialTokenStatus::FeatureDisabled, 230 EXPECT_EQ(blink::WebOriginTrialTokenStatus::FeatureDisabled,
217 TrialTokenValidator::ValidateToken(kSampleToken, 231 TrialTokenValidator::ValidateToken(kSampleToken,
218 appropriate_origin_, &feature)); 232 appropriate_origin_, &feature));
219 } 233 }
220 234
235 TEST_F(TrialTokenValidatorTest, ValidateRequestInsecure) {
236 response_headers_->AddHeader(std::string("Origin-Trial: ") +
237 kInsecureOriginToken);
238 EXPECT_FALSE(TrialTokenValidator::RequestEnablesFeature(
239 GURL(kInsecureOrigin), response_headers_.get(), kAppropriateFeatureName));
240 }
241
242 TEST_F(TrialTokenValidatorTest, ValidateRequestValidToken) {
243 response_headers_->AddHeader(std::string("Origin-Trial: ") + kSampleToken);
244 EXPECT_TRUE(TrialTokenValidator::RequestEnablesFeature(
245 GURL(kAppropriateOrigin), response_headers_.get(),
246 kAppropriateFeatureName));
247 }
248
249 TEST_F(TrialTokenValidatorTest, ValidateRequestNoTokens) {
250 EXPECT_FALSE(TrialTokenValidator::RequestEnablesFeature(
251 GURL(kAppropriateOrigin), response_headers_.get(),
252 kAppropriateFeatureName));
253 }
254
255 TEST_F(TrialTokenValidatorTest, ValidateRequestMultipleHeaders) {
256 response_headers_->AddHeader(std::string("Origin-Trial: ") + kSampleToken);
257 response_headers_->AddHeader(std::string("Origin-Trial: ") + kExpiredToken);
258 EXPECT_TRUE(TrialTokenValidator::RequestEnablesFeature(
259 GURL(kAppropriateOrigin), response_headers_.get(),
260 kAppropriateFeatureName));
261 EXPECT_FALSE(TrialTokenValidator::RequestEnablesFeature(
262 GURL(kAppropriateOrigin), response_headers_.get(),
263 kInappropriateFeatureName));
264 EXPECT_FALSE(TrialTokenValidator::RequestEnablesFeature(
265 GURL(kInappropriateOrigin), response_headers_.get(),
266 kAppropriateFeatureName));
267 }
268
269 TEST_F(TrialTokenValidatorTest, ValidateRequestMultipleHeaderValues) {
270 response_headers_->AddHeader(std::string("Origin-Trial: ") + kExpiredToken +
271 ", " + kSampleToken);
272 EXPECT_TRUE(TrialTokenValidator::RequestEnablesFeature(
273 GURL(kAppropriateOrigin), response_headers_.get(),
274 kAppropriateFeatureName));
275 EXPECT_FALSE(TrialTokenValidator::RequestEnablesFeature(
276 GURL(kAppropriateOrigin), response_headers_.get(),
277 kInappropriateFeatureName));
278 EXPECT_FALSE(TrialTokenValidator::RequestEnablesFeature(
279 GURL(kInappropriateOrigin), response_headers_.get(),
280 kAppropriateFeatureName));
281 }
282
221 } // namespace content 283 } // namespace content
OLDNEW
« no previous file with comments | « content/common/origin_trials/trial_token_validator.cc ('k') | third_party/WebKit/Source/bindings/core/v8/V8Binding.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698