| 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> |
| 9 #include <string> |
| 8 | 10 |
| 9 #include "base/macros.h" | 11 #include "base/macros.h" |
| 10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 11 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" |
| 12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 13 #include "content/public/common/content_client.h" | 15 #include "content/public/common/content_client.h" |
| 14 #include "content/public/common/origin_trial_policy.h" | 16 #include "content/public/common/origin_trial_policy.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h" | 18 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h" |
| 17 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 "IjogMTAwMDAwMDAwMH0="; | 86 "IjogMTAwMDAwMDAwMH0="; |
| 85 | 87 |
| 86 const char kUnparsableToken[] = "abcde"; | 88 const char kUnparsableToken[] = "abcde"; |
| 87 | 89 |
| 88 class TestOriginTrialPolicy : public OriginTrialPolicy { | 90 class TestOriginTrialPolicy : public OriginTrialPolicy { |
| 89 public: | 91 public: |
| 90 base::StringPiece GetPublicKey() const override { | 92 base::StringPiece GetPublicKey() const override { |
| 91 return base::StringPiece(reinterpret_cast<const char*>(key_), | 93 return base::StringPiece(reinterpret_cast<const char*>(key_), |
| 92 arraysize(kTestPublicKey)); | 94 arraysize(kTestPublicKey)); |
| 93 } | 95 } |
| 96 bool IsFeatureDisabled(base::StringPiece feature) const override { |
| 97 return disabled_features_.count(feature.as_string()) > 0; |
| 98 } |
| 94 | 99 |
| 95 // Test setup methods | 100 // Test setup methods |
| 96 void SetPublicKey(const uint8_t* key) { key_ = key; } | 101 void SetPublicKey(const uint8_t* key) { key_ = key; } |
| 102 void DisableFeature(const std::string& feature) { |
| 103 disabled_features_.insert(feature); |
| 104 } |
| 97 | 105 |
| 98 private: | 106 private: |
| 99 const uint8_t* key_ = nullptr; | 107 const uint8_t* key_ = nullptr; |
| 108 std::set<std::string> disabled_features_; |
| 100 }; | 109 }; |
| 101 | 110 |
| 102 class TestContentClient : public ContentClient { | 111 class TestContentClient : public ContentClient { |
| 103 public: | 112 public: |
| 104 // ContentRendererClient methods | 113 // ContentRendererClient methods |
| 105 OriginTrialPolicy* GetOriginTrialPolicy() override { | 114 OriginTrialPolicy* GetOriginTrialPolicy() override { |
| 106 return &origin_trial_policy_; | 115 return &origin_trial_policy_; |
| 107 } | 116 } |
| 108 // Test setup methods | 117 // Test setup methods |
| 109 void SetOriginTrialPublicKey(const uint8_t* key) { | 118 void SetOriginTrialPublicKey(const uint8_t* key) { |
| 110 origin_trial_policy_.SetPublicKey(key); | 119 origin_trial_policy_.SetPublicKey(key); |
| 111 } | 120 } |
| 121 void DisableFeature(const std::string& feature) { |
| 122 origin_trial_policy_.DisableFeature(feature); |
| 123 } |
| 112 | 124 |
| 113 private: | 125 private: |
| 114 TestOriginTrialPolicy origin_trial_policy_; | 126 TestOriginTrialPolicy origin_trial_policy_; |
| 115 }; | 127 }; |
| 116 | 128 |
| 117 } // namespace | 129 } // namespace |
| 118 | 130 |
| 119 class TrialTokenValidatorTest : public testing::Test { | 131 class TrialTokenValidatorTest : public testing::Test { |
| 120 public: | 132 public: |
| 121 TrialTokenValidatorTest() | 133 TrialTokenValidatorTest() |
| 122 : appropriate_origin_(GURL(kAppropriateOrigin)), | 134 : appropriate_origin_(GURL(kAppropriateOrigin)), |
| 123 inappropriate_origin_(GURL(kInappropriateOrigin)), | 135 inappropriate_origin_(GURL(kInappropriateOrigin)), |
| 124 insecure_origin_(GURL(kInsecureOrigin)) { | 136 insecure_origin_(GURL(kInsecureOrigin)) { |
| 125 SetPublicKey(kTestPublicKey); | 137 SetPublicKey(kTestPublicKey); |
| 126 SetContentClient(&test_content_client_); | 138 SetContentClient(&test_content_client_); |
| 127 } | 139 } |
| 128 | 140 |
| 129 ~TrialTokenValidatorTest() override { SetContentClient(nullptr); } | 141 ~TrialTokenValidatorTest() override { SetContentClient(nullptr); } |
| 130 | 142 |
| 131 void SetPublicKey(const uint8_t* key) { | 143 void SetPublicKey(const uint8_t* key) { |
| 132 test_content_client_.SetOriginTrialPublicKey(key); | 144 test_content_client_.SetOriginTrialPublicKey(key); |
| 133 } | 145 } |
| 134 | 146 |
| 147 void DisableFeature(const std::string& feature) { |
| 148 test_content_client_.DisableFeature(feature); |
| 149 } |
| 150 |
| 135 const url::Origin appropriate_origin_; | 151 const url::Origin appropriate_origin_; |
| 136 const url::Origin inappropriate_origin_; | 152 const url::Origin inappropriate_origin_; |
| 137 const url::Origin insecure_origin_; | 153 const url::Origin insecure_origin_; |
| 138 | 154 |
| 139 private: | 155 private: |
| 140 TestContentClient test_content_client_; | 156 TestContentClient test_content_client_; |
| 141 }; | 157 }; |
| 142 | 158 |
| 143 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { | 159 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { |
| 144 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, | 160 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); | 197 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); |
| 182 } | 198 } |
| 183 | 199 |
| 184 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { | 200 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { |
| 185 SetPublicKey(kTestPublicKey2); | 201 SetPublicKey(kTestPublicKey2); |
| 186 EXPECT_EQ(blink::WebOriginTrialTokenStatus::InvalidSignature, | 202 EXPECT_EQ(blink::WebOriginTrialTokenStatus::InvalidSignature, |
| 187 TrialTokenValidator::ValidateToken( | 203 TrialTokenValidator::ValidateToken( |
| 188 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); | 204 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 189 } | 205 } |
| 190 | 206 |
| 207 TEST_F(TrialTokenValidatorTest, ValidatorRespectsDisabledFeatures) { |
| 208 // Disable an irrelevant feature; token should still validate |
| 209 DisableFeature(kInappropriateFeatureName); |
| 210 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, |
| 211 TrialTokenValidator::ValidateToken( |
| 212 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 213 // Disable the token's feature; it should no longer be valid |
| 214 DisableFeature(kAppropriateFeatureName); |
| 215 EXPECT_EQ(blink::WebOriginTrialTokenStatus::FeatureDisabled, |
| 216 TrialTokenValidator::ValidateToken( |
| 217 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 218 } |
| 219 |
| 191 } // namespace content | 220 } // namespace content |
| OLD | NEW |