OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/origin_trials/trial_token_validator.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/strings/string_util.h" |
| 10 #include "base/test/simple_test_clock.h" |
| 11 #include "base/time/time.h" |
| 12 #include "content/public/renderer/content_renderer_client.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // 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 // |
| 22 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13, |
| 23 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba, |
| 24 // 0xfd, 0xbb, 0xfa, 0xbc, 0x92, 0x76, 0x8a, 0x2c, 0x4b, 0xc7, 0x75, |
| 25 // 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, 0x9a, |
| 26 // 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, 0x64, |
| 27 // 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0 |
| 28 const uint8_t kTestPublicKey[] = { |
| 29 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, |
| 30 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, |
| 31 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0, |
| 32 }; |
| 33 |
| 34 // 0x21, 0xee, 0xfa, 0x81, 0x6a, 0xff, 0xdf, 0xb8, 0xc1, 0xdd, 0x75, |
| 35 // 0x05, 0x04, 0x29, 0x68, 0x67, 0x60, 0x85, 0x91, 0xd0, 0x50, 0x16, |
| 36 // 0x0a, 0xcf, 0xa2, 0x37, 0xa3, 0x2e, 0x11, 0x7a, 0x17, 0x96, 0x50, |
| 37 // 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, 0x47, |
| 38 // 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, 0x3e, |
| 39 // 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca |
| 40 const uint8_t kTestPublicKey2[] = { |
| 41 0x50, 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, |
| 42 0x47, 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, |
| 43 0x3e, 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca, |
| 44 }; |
| 45 |
| 46 // This is a good trial token, signed with the above test private key. |
| 47 // TODO(iclelland): This token expires in 2033. Update it or find a way |
| 48 // to autogenerate it before then. |
| 49 const char kSampleToken[] = |
| 50 "1|w694328Rl8l2vd96nkbAumpwvOOnvhWTj9/pfBRkvcWMDAsmiMEhZGEPzdBRy5Yao6il5qC" |
| 51 "OyS6Ah7uuHf7JAQ==|https://valid.example.com|Frobulate|2000000000"; |
| 52 |
| 53 // The token should be valid for this origin and for this feature. |
| 54 const char kAppropriateOrigin[] = "https://valid.example.com"; |
| 55 const char kAppropriateFeatureName[] = "Frobulate"; |
| 56 |
| 57 const char kInappropriateFeatureName[] = "Grokalyze"; |
| 58 const char kInappropriateOrigin[] = "https://invalid.example.com"; |
| 59 const char kInsecureOrigin[] = "http://valid.example.com"; |
| 60 |
| 61 // Well-formed trial token with an invalid signature. |
| 62 const char kInvalidSignatureToken[] = |
| 63 "1|CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/h" |
| 64 "nxmba765hz0mXMc==|https://valid.example.com|Frobulate|2000000000"; |
| 65 |
| 66 // Well-formed, but expired, trial token. (Expired in 2001) |
| 67 const char kExpiredToken[] = |
| 68 "1|Vtzq/H0qMxsMXPThIgGEvI13d3Fd8K3W11/0E+FrJJXqBpx6n/dFkeFkEUsPaP3KeT8PCPF" |
| 69 "1zpZ7kVgWYRLpAA==|https://valid.example.com|Frobulate|1000000000"; |
| 70 |
| 71 const char kUnparsableToken[] = "abcde"; |
| 72 |
| 73 class TestContentRendererClient : public content::ContentRendererClient { |
| 74 public: |
| 75 base::StringPiece GetOriginTrialPublicKey() override { |
| 76 return base::StringPiece(reinterpret_cast<const char*>(key_), |
| 77 arraysize(kTestPublicKey)); |
| 78 } |
| 79 void SetOriginTrialPublicKey(const uint8_t* key) { key_ = key; } |
| 80 const uint8_t* key_ = nullptr; |
| 81 }; |
| 82 |
| 83 } // namespace |
| 84 |
| 85 class TrialTokenValidatorTest : public testing::Test { |
| 86 public: |
| 87 TrialTokenValidatorTest() { |
| 88 SetPublicKey(kTestPublicKey); |
| 89 SetRendererClientForTesting(&test_content_renderer_client_); |
| 90 } |
| 91 |
| 92 void SetPublicKey(const uint8_t* key) { |
| 93 test_content_renderer_client_.SetOriginTrialPublicKey(key); |
| 94 } |
| 95 |
| 96 TrialTokenValidator trial_token_validator_; |
| 97 |
| 98 private: |
| 99 TestContentRendererClient test_content_renderer_client_; |
| 100 }; |
| 101 |
| 102 TEST_F(TrialTokenValidatorTest, ValidateValidToken) { |
| 103 EXPECT_TRUE(trial_token_validator_.validateToken( |
| 104 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 105 } |
| 106 |
| 107 TEST_F(TrialTokenValidatorTest, ValidateInappropriateOrigin) { |
| 108 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 109 kSampleToken, kInappropriateOrigin, kAppropriateFeatureName)); |
| 110 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 111 kSampleToken, kInsecureOrigin, kAppropriateFeatureName)); |
| 112 } |
| 113 |
| 114 TEST_F(TrialTokenValidatorTest, ValidateInappropriateFeature) { |
| 115 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 116 kSampleToken, kAppropriateOrigin, kInappropriateFeatureName)); |
| 117 } |
| 118 |
| 119 TEST_F(TrialTokenValidatorTest, ValidateInvalidSignature) { |
| 120 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 121 kInvalidSignatureToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 122 } |
| 123 |
| 124 TEST_F(TrialTokenValidatorTest, ValidateUnparsableToken) { |
| 125 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 126 kUnparsableToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 127 } |
| 128 |
| 129 TEST_F(TrialTokenValidatorTest, ValidateExpiredToken) { |
| 130 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 131 kExpiredToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 132 } |
| 133 |
| 134 TEST_F(TrialTokenValidatorTest, ValidateValidTokenWithIncorrectKey) { |
| 135 SetPublicKey(kTestPublicKey2); |
| 136 EXPECT_FALSE(TrialTokenValidator().validateToken( |
| 137 kSampleToken, kAppropriateOrigin, kAppropriateFeatureName)); |
| 138 } |
| 139 |
| 140 } // namespace content |
OLD | NEW |