| 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> |
| 8 |
| 7 #include "base/macros.h" | 9 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 10 #include "base/test/simple_test_clock.h" | 11 #include "base/test/simple_test_clock.h" |
| 11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 12 #include "content/public/common/content_client.h" | 13 #include "content/public/common/content_client.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 40 // 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca | 41 // 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca |
| 41 const uint8_t kTestPublicKey2[] = { | 42 const uint8_t kTestPublicKey2[] = { |
| 42 0x50, 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, | 43 0x50, 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, |
| 43 0x47, 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, | 44 0x47, 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, |
| 44 0x3e, 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca, | 45 0x3e, 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca, |
| 45 }; | 46 }; |
| 46 | 47 |
| 47 // This is a good trial token, signed with the above test private key. | 48 // This is a good trial token, signed with the above test private key. |
| 48 // TODO(iclelland): This token expires in 2033. Update it or find a way | 49 // TODO(iclelland): This token expires in 2033. Update it or find a way |
| 49 // to autogenerate it before then. | 50 // to autogenerate it before then. |
| 51 // Generate this token with the command (in tools/origin_trials): |
| 52 // generate_token.py valid.example.com Frobulate --expire-timestamp=2000000000 |
| 50 const char kSampleToken[] = | 53 const char kSampleToken[] = |
| 51 "1|w694328Rl8l2vd96nkbAumpwvOOnvhWTj9/pfBRkvcWMDAsmiMEhZGEPzdBRy5Yao6il5qC" | 54 "AuR/1mg+/w5ROLN54Ok20rApK3opgR7Tq9ZfzhATQmnCa+BtPA1RRw4Nigf336r+" |
| 52 "OyS6Ah7uuHf7JAQ==|https://valid.example.com|Frobulate|2000000000"; | 55 "O4fM3Sa+MEd+5JcIgSZafw8AAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 56 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 57 "IjogMjAwMDAwMDAwMH0="; |
| 53 | 58 |
| 54 // The token should be valid for this origin and for this feature. | 59 // The token should be valid for this origin and for this feature. |
| 55 const char kAppropriateOrigin[] = "https://valid.example.com"; | 60 const char kAppropriateOrigin[] = "https://valid.example.com"; |
| 56 const char kAppropriateFeatureName[] = "Frobulate"; | 61 const char kAppropriateFeatureName[] = "Frobulate"; |
| 57 | 62 |
| 58 const char kInappropriateFeatureName[] = "Grokalyze"; | 63 const char kInappropriateFeatureName[] = "Grokalyze"; |
| 59 const char kInappropriateOrigin[] = "https://invalid.example.com"; | 64 const char kInappropriateOrigin[] = "https://invalid.example.com"; |
| 60 const char kInsecureOrigin[] = "http://valid.example.com"; | 65 const char kInsecureOrigin[] = "http://valid.example.com"; |
| 61 | 66 |
| 62 // Well-formed trial token with an invalid signature. | 67 // Well-formed trial token with an invalid signature. |
| 68 // This token is a corruption of the above valid token. |
| 63 const char kInvalidSignatureToken[] = | 69 const char kInvalidSignatureToken[] = |
| 64 "1|CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/h" | 70 "AuR/1mg+/w5ROLN54Ok20rApK3opgR7Tq9ZfzhATQmnCa+BtPA1RRw4Nigf336r+" |
| 65 "nxmba765hz0mXMc==|https://valid.example.com|Frobulate|2000000000"; | 71 "RrOtlAwa0gPqqn+A8GTD3AQAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 72 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 73 "IjogMjAwMDAwMDAwMH0="; |
| 66 | 74 |
| 67 // Well-formed, but expired, trial token. (Expired in 2001) | 75 // Well-formed, but expired, trial token. (Expired in 2001) |
| 76 // Generate this token with the command (in tools/origin_trials): |
| 77 // generate_token.py valid.example.com Frobulate --expire-timestamp=1000000000 |
| 68 const char kExpiredToken[] = | 78 const char kExpiredToken[] = |
| 69 "1|Vtzq/H0qMxsMXPThIgGEvI13d3Fd8K3W11/0E+FrJJXqBpx6n/dFkeFkEUsPaP3KeT8PCPF" | 79 "AmHPUIXMaXe9jWW8kJeDFXolVjT93p4XMnK4+jMYd2pjqtFcYB1bUmdD8PunQKM+" |
| 70 "1zpZ7kVgWYRLpAA==|https://valid.example.com|Frobulate|1000000000"; | 80 "RrOtlAwa0gPqqn+A8GTD3AQAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 81 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 82 "IjogMTAwMDAwMDAwMH0="; |
| 71 | 83 |
| 72 const char kUnparsableToken[] = "abcde"; | 84 const char kUnparsableToken[] = "abcde"; |
| 73 | 85 |
| 74 class TestContentClient : public ContentClient { | 86 class TestContentClient : public ContentClient { |
| 75 public: | 87 public: |
| 76 base::StringPiece GetOriginTrialPublicKey() override { | 88 base::StringPiece GetOriginTrialPublicKey() override { |
| 77 return base::StringPiece(reinterpret_cast<const char*>(key_), | 89 return base::StringPiece(reinterpret_cast<const char*>(key_), |
| 78 arraysize(kTestPublicKey)); | 90 arraysize(kTestPublicKey)); |
| 79 } | 91 } |
| 80 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } | 92 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); | 151 kExpiredToken, appropriate_origin_, kAppropriateFeatureName)); |
| 140 } | 152 } |
| 141 | 153 |
| 142 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { | 154 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { |
| 143 SetPublicKey(kTestPublicKey2); | 155 SetPublicKey(kTestPublicKey2); |
| 144 EXPECT_FALSE(TrialTokenValidator::ValidateToken( | 156 EXPECT_FALSE(TrialTokenValidator::ValidateToken( |
| 145 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); | 157 kSampleToken, appropriate_origin_, kAppropriateFeatureName)); |
| 146 } | 158 } |
| 147 | 159 |
| 148 } // namespace content | 160 } // namespace content |
| OLD | NEW |