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/common/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/common/content_client.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // 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 |
21 // 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: |
22 // | 22 // |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 "1|CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/h" | 64 "1|CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/h" |
65 "nxmba765hz0mXMc==|https://valid.example.com|Frobulate|2000000000"; | 65 "nxmba765hz0mXMc==|https://valid.example.com|Frobulate|2000000000"; |
66 | 66 |
67 // Well-formed, but expired, trial token. (Expired in 2001) | 67 // Well-formed, but expired, trial token. (Expired in 2001) |
68 const char kExpiredToken[] = | 68 const char kExpiredToken[] = |
69 "1|Vtzq/H0qMxsMXPThIgGEvI13d3Fd8K3W11/0E+FrJJXqBpx6n/dFkeFkEUsPaP3KeT8PCPF" | 69 "1|Vtzq/H0qMxsMXPThIgGEvI13d3Fd8K3W11/0E+FrJJXqBpx6n/dFkeFkEUsPaP3KeT8PCPF" |
70 "1zpZ7kVgWYRLpAA==|https://valid.example.com|Frobulate|1000000000"; | 70 "1zpZ7kVgWYRLpAA==|https://valid.example.com|Frobulate|1000000000"; |
71 | 71 |
72 const char kUnparsableToken[] = "abcde"; | 72 const char kUnparsableToken[] = "abcde"; |
73 | 73 |
74 class TestContentRendererClient : public content::ContentRendererClient { | 74 class TestContentClient : public ContentClient { |
75 public: | 75 public: |
76 base::StringPiece GetOriginTrialPublicKey() override { | 76 base::StringPiece GetOriginTrialPublicKey() override { |
77 return base::StringPiece(reinterpret_cast<const char*>(key_), | 77 return base::StringPiece(reinterpret_cast<const char*>(key_), |
78 arraysize(kTestPublicKey)); | 78 arraysize(kTestPublicKey)); |
79 } | 79 } |
80 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } | 80 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } |
81 const uint8_t* key_ = nullptr; | 81 const uint8_t* key_ = nullptr; |
82 }; | 82 }; |
83 | 83 |
84 } // namespace | 84 } // namespace |
85 | 85 |
86 class TrialTokenValidatorTest : public testing::Test { | 86 class TrialTokenValidatorTest : public testing::Test { |
87 public: | 87 public: |
88 TrialTokenValidatorTest() | 88 TrialTokenValidatorTest() |
89 : appropriate_origin_(GURL(kAppropriateOrigin)), | 89 : appropriate_origin_(GURL(kAppropriateOrigin)), |
90 inappropriate_origin_(GURL(kInappropriateOrigin)), | 90 inappropriate_origin_(GURL(kInappropriateOrigin)), |
91 insecure_origin_(GURL(kInsecureOrigin)) { | 91 insecure_origin_(GURL(kInsecureOrigin)) { |
92 SetPublicKey(kTestPublicKey); | 92 SetPublicKey(kTestPublicKey); |
93 SetRendererClientForTesting(&test_content_renderer_client_); | 93 SetContentClient(&test_content_client_); |
94 } | 94 } |
95 | 95 |
| 96 ~TrialTokenValidatorTest() override { SetContentClient(nullptr); } |
| 97 |
96 void SetPublicKey(const uint8_t* key) { | 98 void SetPublicKey(const uint8_t* key) { |
97 test_content_renderer_client_.SetOriginTrialPublicKey(key); | 99 test_content_client_.SetOriginTrialPublicKey(key); |
98 } | 100 } |
99 | 101 |
100 TrialTokenValidator trial_token_validator_; | |
101 const url::Origin appropriate_origin_; | 102 const url::Origin appropriate_origin_; |
102 const url::Origin inappropriate_origin_; | 103 const url::Origin inappropriate_origin_; |
103 const url::Origin insecure_origin_; | 104 const url::Origin insecure_origin_; |
104 | 105 |
105 private: | 106 private: |
106 TestContentRendererClient test_content_renderer_client_; | 107 TestContentClient test_content_client_; |
107 }; | 108 }; |
108 | 109 |
109 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { | 110 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { |
110 EXPECT_TRUE(trial_token_validator_.validateToken( | 111 EXPECT_TRUE(TrialTokenValidator::ValidateToken( |
111 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); | 112 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
112 } | 113 } |
113 | 114 |
114 TEST_F(TrialTokenValidatorTest, ValidateInappropriateOrigin) { | 115 TEST_F(TrialTokenValidatorTest, ValidateInappropriateOrigin) { |
115 EXPECT_FALSE(TrialTokenValidator().validateToken( | 116 EXPECT_FALSE(TrialTokenValidator::ValidateToken( |
116 kSampleToken, inappropriate_origin_, kAppropriateFeatureName)); | 117 kSampleToken, inappropriate_origin_, kAppropriateFeatureName)); |
117 EXPECT_FALSE(TrialTokenValidator().validateToken( | 118 EXPECT_FALSE(TrialTokenValidator::ValidateToken( |
118 kSampleToken, insecure_origin_, kAppropriateFeatureName)); | 119 kSampleToken, insecure_origin_, kAppropriateFeatureName)); |
119 } | 120 } |
120 | 121 |
121 TEST_F(TrialTokenValidatorTest, ValidateInappropriateFeature) { | 122 TEST_F(TrialTokenValidatorTest, ValidateInappropriateFeature) { |
122 EXPECT_FALSE(TrialTokenValidator().validateToken( | 123 EXPECT_FALSE(TrialTokenValidator::ValidateToken( |
123 kSampleToken, appropriate_origin_, kInappropriateFeatureName)); | 124 kSampleToken, appropriate_origin_, kInappropriateFeatureName)); |
124 } | 125 } |
125 | 126 |
126 TEST_F(TrialTokenValidatorTest, ValidateInvalidSignature) { | 127 TEST_F(TrialTokenValidatorTest, ValidateInvalidSignature) { |
127 EXPECT_FALSE(TrialTokenValidator().validateToken( | 128 EXPECT_FALSE(TrialTokenValidator::ValidateToken( |
128 kInvalidSignatureToken, appropriate_origin_, kAppropriateFeatureName)); | 129 kInvalidSignatureToken, appropriate_origin_, kAppropriateFeatureName)); |
129 } | 130 } |
130 | 131 |
131 TEST_F(TrialTokenValidatorTest, ValidateUnparsableToken) { | 132 TEST_F(TrialTokenValidatorTest, ValidateUnparsableToken) { |
132 EXPECT_FALSE(TrialTokenValidator().validateToken( | 133 EXPECT_FALSE(TrialTokenValidator::ValidateToken( |
133 kUnparsableToken, appropriate_origin_, kAppropriateFeatureName)); | 134 kUnparsableToken, appropriate_origin_, kAppropriateFeatureName)); |
134 } | 135 } |
135 | 136 |
136 TEST_F(TrialTokenValidatorTest, ValidateExpiredToken) { | 137 TEST_F(TrialTokenValidatorTest, ValidateExpiredToken) { |
137 EXPECT_FALSE(TrialTokenValidator().validateToken( | 138 EXPECT_FALSE(TrialTokenValidator::ValidateToken( |
138 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); | 139 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); |
139 } | 140 } |
140 | 141 |
141 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { | 142 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { |
142 SetPublicKey(kTestPublicKey2); | 143 SetPublicKey(kTestPublicKey2); |
143 EXPECT_FALSE(TrialTokenValidator().validateToken( | 144 EXPECT_FALSE(TrialTokenValidator::ValidateToken( |
144 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); | 145 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
145 } | 146 } |
146 | 147 |
147 } // namespace content | 148 } // namespace content |
OLD | NEW |