| 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 "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h" | 17 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h" |
| 16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 17 | 19 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 const char kExpiredToken[] = | 81 const char kExpiredToken[] = |
| 80 "AmHPUIXMaXe9jWW8kJeDFXolVjT93p4XMnK4+jMYd2pjqtFcYB1bUmdD8PunQKM+" | 82 "AmHPUIXMaXe9jWW8kJeDFXolVjT93p4XMnK4+jMYd2pjqtFcYB1bUmdD8PunQKM+" |
| 81 "RrOtlAwa0gPqqn+A8GTD3AQAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" | 83 "RrOtlAwa0gPqqn+A8GTD3AQAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 82 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" | 84 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 83 "IjogMTAwMDAwMDAwMH0="; | 85 "IjogMTAwMDAwMDAwMH0="; |
| 84 | 86 |
| 85 const char kUnparsableToken[] = "abcde"; | 87 const char kUnparsableToken[] = "abcde"; |
| 86 | 88 |
| 87 class TestContentClient : public ContentClient { | 89 class TestContentClient : public ContentClient { |
| 88 public: | 90 public: |
| 91 // ContentRendererClient methods |
| 89 base::StringPiece GetOriginTrialPublicKey() override { | 92 base::StringPiece GetOriginTrialPublicKey() override { |
| 90 return base::StringPiece(reinterpret_cast<const char*>(key_), | 93 return base::StringPiece(reinterpret_cast<const char*>(key_), |
| 91 arraysize(kTestPublicKey)); | 94 arraysize(kTestPublicKey)); |
| 92 } | 95 } |
| 96 bool IsOriginTrialFeatureDisabled(base::StringPiece feature) override { |
| 97 return disabled_features_.count(feature.as_string()) > 0; |
| 98 } |
| 99 |
| 100 // Test setup methods |
| 93 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } | 101 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } |
| 102 void DisableFeature(const std::string& feature) { |
| 103 disabled_features_.insert(feature); |
| 104 } |
| 105 |
| 106 private: |
| 94 const uint8_t* key_ = nullptr; | 107 const uint8_t* key_ = nullptr; |
| 108 std::set<std::string> disabled_features_; |
| 95 }; | 109 }; |
| 96 | 110 |
| 97 } // namespace | 111 } // namespace |
| 98 | 112 |
| 99 class TrialTokenValidatorTest : public testing::Test { | 113 class TrialTokenValidatorTest : public testing::Test { |
| 100 public: | 114 public: |
| 101 TrialTokenValidatorTest() | 115 TrialTokenValidatorTest() |
| 102 : appropriate_origin_(GURL(kAppropriateOrigin)), | 116 : appropriate_origin_(GURL(kAppropriateOrigin)), |
| 103 inappropriate_origin_(GURL(kInappropriateOrigin)), | 117 inappropriate_origin_(GURL(kInappropriateOrigin)), |
| 104 insecure_origin_(GURL(kInsecureOrigin)) { | 118 insecure_origin_(GURL(kInsecureOrigin)) { |
| 105 SetPublicKey(kTestPublicKey); | 119 SetPublicKey(kTestPublicKey); |
| 106 SetContentClient(&test_content_client_); | 120 SetContentClient(&test_content_client_); |
| 107 } | 121 } |
| 108 | 122 |
| 109 ~TrialTokenValidatorTest() override { SetContentClient(nullptr); } | 123 ~TrialTokenValidatorTest() override { SetContentClient(nullptr); } |
| 110 | 124 |
| 111 void SetPublicKey(const uint8_t* key) { | 125 void SetPublicKey(const uint8_t* key) { |
| 112 test_content_client_.SetOriginTrialPublicKey(key); | 126 test_content_client_.SetOriginTrialPublicKey(key); |
| 113 } | 127 } |
| 114 | 128 |
| 129 void DisableFeature(const std::string& feature) { |
| 130 test_content_client_.DisableFeature(feature); |
| 131 } |
| 132 |
| 115 const url::Origin appropriate_origin_; | 133 const url::Origin appropriate_origin_; |
| 116 const url::Origin inappropriate_origin_; | 134 const url::Origin inappropriate_origin_; |
| 117 const url::Origin insecure_origin_; | 135 const url::Origin insecure_origin_; |
| 118 | 136 |
| 119 private: | 137 private: |
| 120 TestContentClient test_content_client_; | 138 TestContentClient test_content_client_; |
| 121 }; | 139 }; |
| 122 | 140 |
| 123 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { | 141 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { |
| 124 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, | 142 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); | 179 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); |
| 162 } | 180 } |
| 163 | 181 |
| 164 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { | 182 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { |
| 165 SetPublicKey(kTestPublicKey2); | 183 SetPublicKey(kTestPublicKey2); |
| 166 EXPECT_EQ(blink::WebOriginTrialTokenStatus::InvalidSignature, | 184 EXPECT_EQ(blink::WebOriginTrialTokenStatus::InvalidSignature, |
| 167 TrialTokenValidator::ValidateToken( | 185 TrialTokenValidator::ValidateToken( |
| 168 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); | 186 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 169 } | 187 } |
| 170 | 188 |
| 189 TEST_F(TrialTokenValidatorTest, ValidatorRespectsDisabledFeatures) { |
| 190 // Disable an irrelevant feature; token should still validate |
| 191 DisableFeature(kInappropriateFeatureName); |
| 192 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, |
| 193 TrialTokenValidator::ValidateToken( |
| 194 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 195 // Disable the token's feature; it should no longer be valid |
| 196 DisableFeature(kAppropriateFeatureName); |
| 197 EXPECT_EQ(blink::WebOriginTrialTokenStatus::FeatureDisabled, |
| 198 TrialTokenValidator::ValidateToken( |
| 199 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 200 } |
| 201 |
| 171 } // namespace content | 202 } // namespace content |
| OLD | NEW |