OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.h" | 5 #include "content/common/origin_trials/trial_token.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/test/simple_test_clock.h" | 12 #include "base/test/simple_test_clock.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/WebKit/public/platform/WebOriginTrialTokenStatus.h" | |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 // This is a sample public key for testing the API. The corresponding private | 22 // This is a sample public key for testing the API. The corresponding private |
22 // key (use this to generate new samples for this test file) is: | 23 // key (use this to generate new samples for this test file) is: |
23 // | 24 // |
24 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13, | 25 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13, |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
146 valid_timestamp_(base::Time::FromDoubleT(kValidTimestamp)), | 147 valid_timestamp_(base::Time::FromDoubleT(kValidTimestamp)), |
147 invalid_timestamp_(base::Time::FromDoubleT(kInvalidTimestamp)), | 148 invalid_timestamp_(base::Time::FromDoubleT(kInvalidTimestamp)), |
148 correct_public_key_( | 149 correct_public_key_( |
149 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), | 150 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), |
150 arraysize(kTestPublicKey))), | 151 arraysize(kTestPublicKey))), |
151 incorrect_public_key_( | 152 incorrect_public_key_( |
152 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2), | 153 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2), |
153 arraysize(kTestPublicKey2))) {} | 154 arraysize(kTestPublicKey2))) {} |
154 | 155 |
155 protected: | 156 protected: |
156 std::unique_ptr<std::string> Extract(const std::string& token_text, | 157 blink::WebOriginTrialTokenStatus Extract(const std::string& token_text, |
157 base::StringPiece public_key) { | 158 base::StringPiece public_key, |
158 return TrialToken::Extract(token_text, public_key); | 159 std::string* token_payload) { |
160 return TrialToken::Extract(token_text, public_key, token_payload); | |
159 } | 161 } |
162 | |
163 blink::WebOriginTrialTokenStatus ExtractIgnorePayload( | |
164 const std::string& token_text, | |
165 base::StringPiece public_key) { | |
166 std::string token_payload; | |
167 return Extract(token_text, public_key, &token_payload); | |
168 } | |
169 | |
160 std::unique_ptr<TrialToken> Parse(const std::string& token_payload) { | 170 std::unique_ptr<TrialToken> Parse(const std::string& token_payload) { |
161 return TrialToken::Parse(token_payload); | 171 return TrialToken::Parse(token_payload); |
162 } | 172 } |
163 | 173 |
164 bool ValidateOrigin(TrialToken* token, const url::Origin origin) { | 174 bool ValidateOrigin(TrialToken* token, const url::Origin origin) { |
165 return token->ValidateOrigin(origin); | 175 return token->ValidateOrigin(origin); |
166 } | 176 } |
167 | 177 |
168 bool ValidateFeatureName(TrialToken* token, const char* feature_name) { | 178 bool ValidateFeatureName(TrialToken* token, const char* feature_name) { |
169 return token->ValidateFeatureName(feature_name); | 179 return token->ValidateFeatureName(feature_name); |
(...skipping 19 matching lines...) Expand all Loading... | |
189 base::StringPiece incorrect_public_key_; | 199 base::StringPiece incorrect_public_key_; |
190 }; | 200 }; |
191 | 201 |
192 // Test the extraction of the signed payload from token strings. This includes | 202 // Test the extraction of the signed payload from token strings. This includes |
193 // checking the included version identifier, payload length, and cryptographic | 203 // checking the included version identifier, payload length, and cryptographic |
194 // signature. | 204 // signature. |
195 | 205 |
196 // Test verification of signature and extraction of token JSON from signed | 206 // Test verification of signature and extraction of token JSON from signed |
197 // token. | 207 // token. |
198 TEST_F(TrialTokenTest, ValidateValidSignature) { | 208 TEST_F(TrialTokenTest, ValidateValidSignature) { |
199 std::unique_ptr<std::string> token_payload = | 209 std::string token_payload; |
200 Extract(kSampleToken, correct_public_key()); | 210 blink::WebOriginTrialTokenStatus status = |
201 ASSERT_TRUE(token_payload); | 211 Extract(kSampleToken, correct_public_key(), &token_payload); |
202 EXPECT_STREQ(kSampleTokenJSON, token_payload.get()->c_str()); | 212 ASSERT_EQ(blink::WebOriginTrialTokenStatus::Success, status); |
213 EXPECT_STREQ(kSampleTokenJSON, token_payload.c_str()); | |
203 } | 214 } |
204 | 215 |
205 TEST_F(TrialTokenTest, ValidateInvalidSignature) { | 216 TEST_F(TrialTokenTest, ValidateInvalidSignature) { |
206 std::unique_ptr<std::string> token_payload = | 217 blink::WebOriginTrialTokenStatus status = |
207 Extract(kInvalidSignatureToken, correct_public_key()); | 218 ExtractIgnorePayload(kInvalidSignatureToken, correct_public_key()); |
208 ASSERT_FALSE(token_payload); | 219 // TODO(chasej): Recreate the invalid token, and change to |
220 // blink::WebOriginTrialTokenStatus::InvalidSignature | |
221 EXPECT_EQ(blink::WebOriginTrialTokenStatus::WrongVersion, status); | |
iclelland
2016/05/02 16:17:55
Let's fix this todo in this CL. It shouldn't be ha
chasej
2016/05/03 16:55:10
Yes, it was my intent to fix in this CL, once the
| |
209 } | 222 } |
210 | 223 |
211 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectKey) { | 224 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectKey) { |
212 std::unique_ptr<std::string> token_payload = | 225 blink::WebOriginTrialTokenStatus status = |
213 Extract(kSampleToken, incorrect_public_key()); | 226 ExtractIgnorePayload(kSampleToken, incorrect_public_key()); |
214 ASSERT_FALSE(token_payload); | 227 EXPECT_EQ(blink::WebOriginTrialTokenStatus::InvalidSignature, status); |
215 } | 228 } |
216 | 229 |
217 TEST_F(TrialTokenTest, ValidateEmptyToken) { | 230 TEST_F(TrialTokenTest, ValidateEmptyToken) { |
218 std::unique_ptr<std::string> token_payload = | 231 blink::WebOriginTrialTokenStatus status = |
219 Extract("", correct_public_key()); | 232 ExtractIgnorePayload("", correct_public_key()); |
220 ASSERT_FALSE(token_payload); | 233 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Malformed, status); |
221 } | 234 } |
222 | 235 |
223 TEST_F(TrialTokenTest, ValidateShortToken) { | 236 TEST_F(TrialTokenTest, ValidateShortToken) { |
224 std::unique_ptr<std::string> token_payload = | 237 blink::WebOriginTrialTokenStatus status = |
225 Extract(kTruncatedToken, correct_public_key()); | 238 ExtractIgnorePayload(kTruncatedToken, correct_public_key()); |
226 ASSERT_FALSE(token_payload); | 239 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Malformed, status); |
227 } | 240 } |
228 | 241 |
229 TEST_F(TrialTokenTest, ValidateUnsupportedVersion) { | 242 TEST_F(TrialTokenTest, ValidateUnsupportedVersion) { |
230 std::unique_ptr<std::string> token_payload = | 243 blink::WebOriginTrialTokenStatus status = |
231 Extract(kIncorrectVersionToken, correct_public_key()); | 244 ExtractIgnorePayload(kIncorrectVersionToken, correct_public_key()); |
232 ASSERT_FALSE(token_payload); | 245 EXPECT_EQ(blink::WebOriginTrialTokenStatus::WrongVersion, status); |
233 } | 246 } |
234 | 247 |
235 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectLength) { | 248 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectLength) { |
236 std::unique_ptr<std::string> token_payload = | 249 blink::WebOriginTrialTokenStatus status = |
237 Extract(kIncorrectLengthToken, correct_public_key()); | 250 ExtractIgnorePayload(kIncorrectLengthToken, correct_public_key()); |
238 ASSERT_FALSE(token_payload); | 251 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Malformed, status); |
239 } | 252 } |
240 | 253 |
241 // Test parsing of fields from JSON token. | 254 // Test parsing of fields from JSON token. |
242 | 255 |
243 TEST_F(TrialTokenTest, ParseEmptyString) { | 256 TEST_F(TrialTokenTest, ParseEmptyString) { |
244 std::unique_ptr<TrialToken> empty_token = Parse(""); | 257 std::unique_ptr<TrialToken> empty_token = Parse(""); |
245 EXPECT_FALSE(empty_token); | 258 EXPECT_FALSE(empty_token); |
246 } | 259 } |
247 | 260 |
248 TEST_P(TrialTokenTest, ParseInvalidString) { | 261 TEST_P(TrialTokenTest, ParseInvalidString) { |
(...skipping 23 matching lines...) Expand all Loading... | |
272 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str())); | 285 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str())); |
273 EXPECT_FALSE(ValidateFeatureName( | 286 EXPECT_FALSE(ValidateFeatureName( |
274 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str())); | 287 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str())); |
275 EXPECT_TRUE(ValidateDate(token.get(), valid_timestamp_)); | 288 EXPECT_TRUE(ValidateDate(token.get(), valid_timestamp_)); |
276 EXPECT_FALSE(ValidateDate(token.get(), invalid_timestamp_)); | 289 EXPECT_FALSE(ValidateDate(token.get(), invalid_timestamp_)); |
277 } | 290 } |
278 | 291 |
279 TEST_F(TrialTokenTest, TokenIsValidForFeature) { | 292 TEST_F(TrialTokenTest, TokenIsValidForFeature) { |
280 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON); | 293 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON); |
281 ASSERT_TRUE(token); | 294 ASSERT_TRUE(token); |
282 EXPECT_TRUE(token->IsValidForFeature(expected_origin_, kExpectedFeatureName, | 295 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, |
283 valid_timestamp_)); | 296 token->IsValidForFeature(expected_origin_, kExpectedFeatureName, |
284 EXPECT_FALSE(token->IsValidForFeature( | 297 valid_timestamp_)); |
285 expected_origin_, base::ToUpperASCII(kExpectedFeatureName), | 298 EXPECT_EQ(blink::WebOriginTrialTokenStatus::WrongFeature, |
286 valid_timestamp_)); | 299 token->IsValidForFeature(expected_origin_, |
287 EXPECT_FALSE(token->IsValidForFeature( | 300 base::ToUpperASCII(kExpectedFeatureName), |
288 expected_origin_, base::ToLowerASCII(kExpectedFeatureName), | 301 valid_timestamp_)); |
289 valid_timestamp_)); | 302 EXPECT_EQ(blink::WebOriginTrialTokenStatus::WrongFeature, |
290 EXPECT_FALSE(token->IsValidForFeature(invalid_origin_, kExpectedFeatureName, | 303 token->IsValidForFeature(expected_origin_, |
291 valid_timestamp_)); | 304 base::ToLowerASCII(kExpectedFeatureName), |
292 EXPECT_FALSE(token->IsValidForFeature(insecure_origin_, kExpectedFeatureName, | 305 valid_timestamp_)); |
293 valid_timestamp_)); | 306 EXPECT_EQ(blink::WebOriginTrialTokenStatus::WrongOrigin, |
294 EXPECT_FALSE(token->IsValidForFeature(expected_origin_, kInvalidFeatureName, | 307 token->IsValidForFeature(invalid_origin_, kExpectedFeatureName, |
295 valid_timestamp_)); | 308 valid_timestamp_)); |
296 EXPECT_FALSE(token->IsValidForFeature(expected_origin_, kExpectedFeatureName, | 309 EXPECT_EQ(blink::WebOriginTrialTokenStatus::WrongOrigin, |
297 invalid_timestamp_)); | 310 token->IsValidForFeature(insecure_origin_, kExpectedFeatureName, |
311 valid_timestamp_)); | |
312 EXPECT_EQ(blink::WebOriginTrialTokenStatus::WrongFeature, | |
313 token->IsValidForFeature(expected_origin_, kInvalidFeatureName, | |
314 valid_timestamp_)); | |
315 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Expired, | |
316 token->IsValidForFeature(expected_origin_, kExpectedFeatureName, | |
317 invalid_timestamp_)); | |
318 } | |
319 | |
320 // Test overall extraction, to ensure output status matches returned token | |
321 TEST_F(TrialTokenTest, ExtractValidToken) { | |
322 blink::WebOriginTrialTokenStatus status; | |
323 std::unique_ptr<TrialToken> token = | |
324 TrialToken::From(kSampleToken, correct_public_key(), &status); | |
325 EXPECT_TRUE(token); | |
326 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Success, status); | |
327 } | |
328 | |
329 TEST_F(TrialTokenTest, ExtractInvalidSignature) { | |
330 blink::WebOriginTrialTokenStatus status; | |
331 std::unique_ptr<TrialToken> token = | |
332 TrialToken::From(kSampleToken, incorrect_public_key(), &status); | |
333 EXPECT_FALSE(token); | |
334 EXPECT_EQ(blink::WebOriginTrialTokenStatus::InvalidSignature, status); | |
335 } | |
336 | |
337 TEST_F(TrialTokenTest, ExtractMalformedToken) { | |
338 blink::WebOriginTrialTokenStatus status; | |
339 std::unique_ptr<TrialToken> token = | |
340 TrialToken::From(kIncorrectLengthToken, correct_public_key(), &status); | |
341 EXPECT_FALSE(token); | |
342 EXPECT_EQ(blink::WebOriginTrialTokenStatus::Malformed, status); | |
298 } | 343 } |
299 | 344 |
300 } // namespace content | 345 } // namespace content |
OLD | NEW |