| 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 "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "content/public/renderer/content_renderer_client.h" | 12 #include "content/public/renderer/content_renderer_client.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // This is a sample public key for testing the API. The corresponding private | 20 // This is a sample public key for testing the API. The corresponding private |
| 20 // key (use this to generate new samples for this test file) is: | 21 // key (use this to generate new samples for this test file) is: |
| 21 // | 22 // |
| 22 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13, | 23 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13, |
| 23 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba, | 24 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 arraysize(kTestPublicKey)); | 78 arraysize(kTestPublicKey)); |
| 78 } | 79 } |
| 79 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } | 80 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } |
| 80 const uint8_t* key_ = nullptr; | 81 const uint8_t* key_ = nullptr; |
| 81 }; | 82 }; |
| 82 | 83 |
| 83 } // namespace | 84 } // namespace |
| 84 | 85 |
| 85 class TrialTokenValidatorTest : public testing::Test { | 86 class TrialTokenValidatorTest : public testing::Test { |
| 86 public: | 87 public: |
| 87 TrialTokenValidatorTest() { | 88 TrialTokenValidatorTest() |
| 89 : appropriate_origin_(GURL(kAppropriateOrigin)), |
| 90 inappropriate_origin_(GURL(kInappropriateOrigin)), |
| 91 insecure_origin_(GURL(kInsecureOrigin)) { |
| 88 SetPublicKey(kTestPublicKey); | 92 SetPublicKey(kTestPublicKey); |
| 89 SetRendererClientForTesting(&test_content_renderer_client_); | 93 SetRendererClientForTesting(&test_content_renderer_client_); |
| 90 } | 94 } |
| 91 | 95 |
| 92 void SetPublicKey(const uint8_t* key) { | 96 void SetPublicKey(const uint8_t* key) { |
| 93 test_content_renderer_client_.SetOriginTrialPublicKey(key); | 97 test_content_renderer_client_.SetOriginTrialPublicKey(key); |
| 94 } | 98 } |
| 95 | 99 |
| 96 TrialTokenValidator trial_token_validator_; | 100 TrialTokenValidator trial_token_validator_; |
| 101 const url::Origin appropriate_origin_; |
| 102 const url::Origin inappropriate_origin_; |
| 103 const url::Origin insecure_origin_; |
| 97 | 104 |
| 98 private: | 105 private: |
| 99 TestContentRendererClient test_content_renderer_client_; | 106 TestContentRendererClient test_content_renderer_client_; |
| 100 }; | 107 }; |
| 101 | 108 |
| 102 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { | 109 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { |
| 103 EXPECT_TRUE(trial_token_validator_.validateToken( | 110 EXPECT_TRUE(trial_token_validator_.validateToken( |
| 104 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); | 111 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 105 } | 112 } |
| 106 | 113 |
| 107 TEST_F(TrialTokenValidatorTest, ValidateInappropriateOrigin) { | 114 TEST_F(TrialTokenValidatorTest, ValidateInappropriateOrigin) { |
| 108 EXPECT_FALSE(TrialTokenValidator().validateToken( | 115 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 109 kSampleToken, kInappropriateOrigin, kAppropriateFeatureName)); | 116 kSampleToken, inappropriate_origin_, kAppropriateFeatureName)); |
| 110 EXPECT_FALSE(TrialTokenValidator().validateToken( | 117 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 111 kSampleToken, kInsecureOrigin, kAppropriateFeatureName)); | 118 kSampleToken, insecure_origin_, kAppropriateFeatureName)); |
| 112 } | 119 } |
| 113 | 120 |
| 114 TEST_F(TrialTokenValidatorTest, ValidateInappropriateFeature) { | 121 TEST_F(TrialTokenValidatorTest, ValidateInappropriateFeature) { |
| 115 EXPECT_FALSE(TrialTokenValidator().validateToken( | 122 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 116 kSampleToken, kAppropriateOrigin, kInappropriateFeatureName)); | 123 kSampleToken, appropriate_origin_, kInappropriateFeatureName)); |
| 117 } | 124 } |
| 118 | 125 |
| 119 TEST_F(TrialTokenValidatorTest, ValidateInvalidSignature) { | 126 TEST_F(TrialTokenValidatorTest, ValidateInvalidSignature) { |
| 120 EXPECT_FALSE(TrialTokenValidator().validateToken( | 127 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 121 kInvalidSignatureToken, kAppropriateOrigin, kAppropriateFeatureName)); | 128 kInvalidSignatureToken, appropriate_origin_, kAppropriateFeatureName)); |
| 122 } | 129 } |
| 123 | 130 |
| 124 TEST_F(TrialTokenValidatorTest, ValidateUnparsableToken) { | 131 TEST_F(TrialTokenValidatorTest, ValidateUnparsableToken) { |
| 125 EXPECT_FALSE(TrialTokenValidator().validateToken( | 132 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 126 kUnparsableToken, kAppropriateOrigin, kAppropriateFeatureName)); | 133 kUnparsableToken, appropriate_origin_, kAppropriateFeatureName)); |
| 127 } | 134 } |
| 128 | 135 |
| 129 TEST_F(TrialTokenValidatorTest, ValidateExpiredToken) { | 136 TEST_F(TrialTokenValidatorTest, ValidateExpiredToken) { |
| 130 EXPECT_FALSE(TrialTokenValidator().validateToken( | 137 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 131 kExpiredToken, kAppropriateOrigin, kAppropriateFeatureName)); | 138 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); |
| 132 } | 139 } |
| 133 | 140 |
| 134 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { | 141 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { |
| 135 SetPublicKey(kTestPublicKey2); | 142 SetPublicKey(kTestPublicKey2); |
| 136 EXPECT_FALSE(TrialTokenValidator().validateToken( | 143 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 137 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); | 144 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 138 } | 145 } |
| 139 | 146 |
| 140 } // namespace content | 147 } // namespace content |
| OLD | NEW |