| OLD | NEW |
| 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 "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h" | 18 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // This is a sample public key for testing the API. The corresponding private | 25 // This is a sample public key for testing the API. The corresponding private |
| 25 // key (use this to generate new samples for this test file) is: | 26 // key (use this to generate new samples for this test file) is: |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 // Generate this token with the command (in tools/origin_trials): | 80 // Generate this token with the command (in tools/origin_trials): |
| 80 // generate_token.py valid.example.com Frobulate --expire-timestamp=1000000000 | 81 // generate_token.py valid.example.com Frobulate --expire-timestamp=1000000000 |
| 81 const char kExpiredToken[] = | 82 const char kExpiredToken[] = |
| 82 "AmHPUIXMaXe9jWW8kJeDFXolVjT93p4XMnK4+jMYd2pjqtFcYB1bUmdD8PunQKM+" | 83 "AmHPUIXMaXe9jWW8kJeDFXolVjT93p4XMnK4+jMYd2pjqtFcYB1bUmdD8PunQKM+" |
| 83 "RrOtlAwa0gPqqn+A8GTD3AQAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" | 84 "RrOtlAwa0gPqqn+A8GTD3AQAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 84 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" | 85 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 85 "IjogMTAwMDAwMDAwMH0="; | 86 "IjogMTAwMDAwMDAwMH0="; |
| 86 | 87 |
| 87 const char kUnparsableToken[] = "abcde"; | 88 const char kUnparsableToken[] = "abcde"; |
| 88 | 89 |
| 89 class TestContentClient : public ContentClient { | 90 class TestOriginTrialPolicy : public OriginTrialPolicy { |
| 90 public: | 91 public: |
| 91 // ContentRendererClient methods | 92 base::StringPiece GetPublicKey() const override { |
| 92 base::StringPiece GetOriginTrialPublicKey() const override { | |
| 93 return base::StringPiece(reinterpret_cast<const char*>(key_), | 93 return base::StringPiece(reinterpret_cast<const char*>(key_), |
| 94 arraysize(kTestPublicKey)); | 94 arraysize(kTestPublicKey)); |
| 95 } | 95 } |
| 96 bool IsOriginTrialFeatureDisabled(base::StringPiece feature) const override { | 96 bool IsFeatureDisabled(base::StringPiece feature) const override { |
| 97 return disabled_features_.count(feature.as_string()) > 0; | 97 return disabled_features_.count(feature.as_string()) > 0; |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Test setup methods | 100 // Test setup methods |
| 101 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } | 101 void SetPublicKey(const uint8_t* key) { key_ = key; } |
| 102 void DisableFeature(const std::string& feature) { | 102 void DisableFeature(const std::string& feature) { |
| 103 disabled_features_.insert(feature); | 103 disabled_features_.insert(feature); |
| 104 } | 104 } |
| 105 | 105 |
| 106 private: | 106 private: |
| 107 const uint8_t* key_ = nullptr; | 107 const uint8_t* key_ = nullptr; |
| 108 std::set<std::string> disabled_features_; | 108 std::set<std::string> disabled_features_; |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 class TestContentClient : public ContentClient { |
| 112 public: |
| 113 // ContentRendererClient methods |
| 114 const OriginTrialPolicy* GetOriginTrialPolicy() const override { |
| 115 return &origin_trial_policy_; |
| 116 } |
| 117 // Test setup methods |
| 118 void SetOriginTrialPublicKey(const uint8_t* key) { |
| 119 origin_trial_policy_.SetPublicKey(key); |
| 120 } |
| 121 void DisableFeature(const std::string& feature) { |
| 122 origin_trial_policy_.DisableFeature(feature); |
| 123 } |
| 124 |
| 125 private: |
| 126 TestOriginTrialPolicy origin_trial_policy_; |
| 127 }; |
| 128 |
| 111 } // namespace | 129 } // namespace |
| 112 | 130 |
| 113 class TrialTokenValidatorTest : public testing::Test { | 131 class TrialTokenValidatorTest : public testing::Test { |
| 114 public: | 132 public: |
| 115 TrialTokenValidatorTest() | 133 TrialTokenValidatorTest() |
| 116 : appropriate_origin_(GURL(kAppropriateOrigin)), | 134 : appropriate_origin_(GURL(kAppropriateOrigin)), |
| 117 inappropriate_origin_(GURL(kInappropriateOrigin)), | 135 inappropriate_origin_(GURL(kInappropriateOrigin)), |
| 118 insecure_origin_(GURL(kInsecureOrigin)) { | 136 insecure_origin_(GURL(kInsecureOrigin)) { |
| 119 SetPublicKey(kTestPublicKey); | 137 SetPublicKey(kTestPublicKey); |
| 120 SetContentClient(&test_content_client_); | 138 SetContentClient(&test_content_client_); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 TrialTokenValidator::ValidateToken( | 211 TrialTokenValidator::ValidateToken( |
| 194 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); | 212 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 195 // Disable the token's feature; it should no longer be valid | 213 // Disable the token's feature; it should no longer be valid |
| 196 DisableFeature(kAppropriateFeatureName); | 214 DisableFeature(kAppropriateFeatureName); |
| 197 EXPECT_EQ(blink::WebOriginTrialTokenStatus::FeatureDisabled, | 215 EXPECT_EQ(blink::WebOriginTrialTokenStatus::FeatureDisabled, |
| 198 TrialTokenValidator::ValidateToken( | 216 TrialTokenValidator::ValidateToken( |
| 199 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); | 217 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 200 } | 218 } |
| 201 | 219 |
| 202 } // namespace content | 220 } // namespace content |
| OLD | NEW |