| 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/renderer/origin_trials/trial_token_validator.h" | 5 #include "content/renderer/origin_trials/trial_token_validator.h" |
| 6 | 6 |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 7 #include "base/macros.h" | 10 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 10 #include "base/test/simple_test_clock.h" | 13 #include "base/test/simple_test_clock.h" |
| 11 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 12 #include "content/public/renderer/content_renderer_client.h" | 15 #include "content/public/renderer/content_renderer_client.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 17 |
| 15 namespace content { | 18 namespace content { |
| 16 | 19 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 68 |
| 66 // Well-formed, but expired, trial token. (Expired in 2001) | 69 // Well-formed, but expired, trial token. (Expired in 2001) |
| 67 const char kExpiredToken[] = | 70 const char kExpiredToken[] = |
| 68 "1|Vtzq/H0qMxsMXPThIgGEvI13d3Fd8K3W11/0E+FrJJXqBpx6n/dFkeFkEUsPaP3KeT8PCPF" | 71 "1|Vtzq/H0qMxsMXPThIgGEvI13d3Fd8K3W11/0E+FrJJXqBpx6n/dFkeFkEUsPaP3KeT8PCPF" |
| 69 "1zpZ7kVgWYRLpAA==|https://valid.example.com|Frobulate|1000000000"; | 72 "1zpZ7kVgWYRLpAA==|https://valid.example.com|Frobulate|1000000000"; |
| 70 | 73 |
| 71 const char kUnparsableToken[] = "abcde"; | 74 const char kUnparsableToken[] = "abcde"; |
| 72 | 75 |
| 73 class TestContentRendererClient : public content::ContentRendererClient { | 76 class TestContentRendererClient : public content::ContentRendererClient { |
| 74 public: | 77 public: |
| 78 // ContentRendererClient methods |
| 75 base::StringPiece GetOriginTrialPublicKey() override { | 79 base::StringPiece GetOriginTrialPublicKey() override { |
| 76 return base::StringPiece(reinterpret_cast<const char*>(key_), | 80 return base::StringPiece(reinterpret_cast<const char*>(key_), |
| 77 arraysize(kTestPublicKey)); | 81 arraysize(kTestPublicKey)); |
| 78 } | 82 } |
| 83 bool IsOriginTrialFeatureDisabled(const std::string& feature) override { |
| 84 return disabled_features_.count(feature) > 0; |
| 85 } |
| 86 |
| 87 // Test setup methods |
| 79 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } | 88 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } |
| 89 void DisableFeature(const std::string& feature) { |
| 90 disabled_features_.insert(feature); |
| 91 } |
| 92 private: |
| 80 const uint8_t* key_ = nullptr; | 93 const uint8_t* key_ = nullptr; |
| 94 std::set<std::string> disabled_features_; |
| 81 }; | 95 }; |
| 82 | 96 |
| 83 } // namespace | 97 } // namespace |
| 84 | 98 |
| 85 class TrialTokenValidatorTest : public testing::Test { | 99 class TrialTokenValidatorTest : public testing::Test { |
| 86 public: | 100 public: |
| 87 TrialTokenValidatorTest() { | 101 TrialTokenValidatorTest() { |
| 88 SetPublicKey(kTestPublicKey); | 102 SetPublicKey(kTestPublicKey); |
| 89 SetRendererClientForTesting(&test_content_renderer_client_); | 103 SetRendererClientForTesting(&test_content_renderer_client_); |
| 90 } | 104 } |
| 91 | 105 |
| 92 void SetPublicKey(const uint8_t* key) { | 106 void SetPublicKey(const uint8_t* key) { |
| 93 test_content_renderer_client_.SetOriginTrialPublicKey(key); | 107 test_content_renderer_client_.SetOriginTrialPublicKey(key); |
| 94 } | 108 } |
| 95 | 109 |
| 110 void DisableFeature(const std::string& feature) { |
| 111 test_content_renderer_client_.DisableFeature(feature); |
| 112 } |
| 113 |
| 96 TrialTokenValidator trial_token_validator_; | 114 TrialTokenValidator trial_token_validator_; |
| 97 | 115 |
| 98 private: | 116 private: |
| 99 TestContentRendererClient test_content_renderer_client_; | 117 TestContentRendererClient test_content_renderer_client_; |
| 100 }; | 118 }; |
| 101 | 119 |
| 102 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { | 120 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { |
| 103 EXPECT_TRUE(trial_token_validator_.validateToken( | 121 EXPECT_TRUE(trial_token_validator_.validateToken( |
| 104 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); | 122 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 105 } | 123 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 130 EXPECT_FALSE(TrialTokenValidator().validateToken( | 148 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 131 kExpiredToken, kAppropriateOrigin, kAppropriateFeatureName)); | 149 kExpiredToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 132 } | 150 } |
| 133 | 151 |
| 134 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { | 152 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { |
| 135 SetPublicKey(kTestPublicKey2); | 153 SetPublicKey(kTestPublicKey2); |
| 136 EXPECT_FALSE(TrialTokenValidator().validateToken( | 154 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 137 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); | 155 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 138 } | 156 } |
| 139 | 157 |
| 158 TEST_F(TrialTokenValidatorTest, ValidatorRespectsDisabledFeatures) { |
| 159 // Disable an irrelevant feature; token should still validate |
| 160 DisableFeature(kInappropriateFeatureName); |
| 161 EXPECT_TRUE(trial_token_validator_.validateToken( |
| 162 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 163 // Disable the token's feature; it should no longer be valid |
| 164 DisableFeature(kAppropriateFeatureName); |
| 165 EXPECT_FALSE(trial_token_validator_.validateToken( |
| 166 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 167 } |
| 168 |
| 140 } // namespace content | 169 } // namespace content |
| OLD | NEW |