| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/common/origin_trials/trial_token.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 "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 namespace { |
| 17 |
| 18 // This is a sample public key for testing the API. The corresponding private |
| 19 // key (use this to generate new samples for this test file) is: |
| 20 // |
| 21 // 0x83, 0x67, 0xf4, 0xcd, 0x2a, 0x1f, 0x0e, 0x04, 0x0d, 0x43, 0x13, |
| 22 // 0x4c, 0x67, 0xc4, 0xf4, 0x28, 0xc9, 0x90, 0x15, 0x02, 0xe2, 0xba, |
| 23 // 0xfd, 0xbb, 0xfa, 0xbc, 0x92, 0x76, 0x8a, 0x2c, 0x4b, 0xc7, 0x75, |
| 24 // 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, 0x9a, |
| 25 // 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, 0x64, |
| 26 // 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0 |
| 27 const uint8_t kTestPublicKey[] = { |
| 28 0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2, |
| 29 0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f, |
| 30 0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0, |
| 31 }; |
| 32 |
| 33 // This is a good trial token, signed with the above test private key. |
| 34 const char* kSampleToken = |
| 35 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S" |
| 36 "dUwk7Dyk/qbDw==|https://valid.example.com|Frobulate|1458766277"; |
| 37 const char* kExpectedSignature = |
| 38 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S" |
| 39 "dUwk7Dyk/qbDw=="; |
| 40 const char* kExpectedData = "https://valid.example.com|Frobulate|1458766277"; |
| 41 const char* kExpectedFeatureName = "Frobulate"; |
| 42 const char* kExpectedOrigin = "https://valid.example.com"; |
| 43 const uint64_t kExpectedExpiry = 1458766277; |
| 44 |
| 45 // The token should not be valid for this origin, or for this feature. |
| 46 const char* kInvalidOrigin = "https://invalid.example.com"; |
| 47 const char* kInsecureOrigin = "http://valid.example.com"; |
| 48 const char* kInvalidFeatureName = "Grokalyze"; |
| 49 |
| 50 // The token should be valid if the current time is kValidTimestamp or earlier. |
| 51 double kValidTimestamp = 1458766276.0; |
| 52 |
| 53 // The token should be invalid if the current time is kInvalidTimestamp or |
| 54 // later. |
| 55 double kInvalidTimestamp = 1458766278.0; |
| 56 |
| 57 // Well-formed trial token with an invalid signature. |
| 58 const char* kInvalidSignatureToken = |
| 59 "CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/hnx" |
| 60 "mba765hz0mXMc==|https://valid.example.com|Frobulate|1458766277"; |
| 61 |
| 62 // Various ill-formed trial tokens. These should all fail to parse. |
| 63 const char* kInvalidTokens[] = { |
| 64 // Invalid - only one part |
| 65 "abcde", |
| 66 // Not enough parts |
| 67 "https://valid.example.com|FeatuerName|1458766277", |
| 68 // Delimiter in feature name |
| 69 "Signature|https://valid.example.com|Feature|Name|1458766277", |
| 70 // Extra string field |
| 71 "Signature|https://valid.example.com|FeatureName|1458766277|SomethingElse", |
| 72 // Extra numeric field |
| 73 "Signature|https://valid.example.com|FeatureName|1458766277|1458766277", |
| 74 // Non-numeric expiry timestamp |
| 75 "Signature|https://valid.example.com|FeatureName|abcdefghij", |
| 76 "Signature|https://valid.example.com|FeatureName|1458766277x", |
| 77 // Negative expiry timestamp |
| 78 "Signature|https://valid.example.com|FeatureName|-1458766277", |
| 79 // Origin not a proper origin URL |
| 80 "Signature|abcdef|FeatureName|1458766277", |
| 81 "Signature|data:text/plain,abcdef|FeatureName|1458766277", |
| 82 "Signature|javascript:alert(1)|FeatureName|1458766277"}; |
| 83 const size_t kNumInvalidTokens = arraysize(kInvalidTokens); |
| 84 |
| 85 } // namespace |
| 86 |
| 87 class TrialTokenTest : public testing::Test { |
| 88 public: |
| 89 TrialTokenTest() |
| 90 : public_key_( |
| 91 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), |
| 92 arraysize(kTestPublicKey))) {} |
| 93 |
| 94 protected: |
| 95 bool ValidateOrigin(TrialToken* token, const char* origin) { |
| 96 return token->ValidateOrigin(origin); |
| 97 } |
| 98 |
| 99 bool ValidateFeatureName(TrialToken* token, const char* feature_name) { |
| 100 return token->ValidateFeatureName(feature_name); |
| 101 } |
| 102 |
| 103 bool ValidateDate(TrialToken* token, const base::Time& now) { |
| 104 return token->ValidateDate(now); |
| 105 } |
| 106 |
| 107 bool ValidateSignature(TrialToken* token, |
| 108 const base::StringPiece& public_key) { |
| 109 return token->ValidateSignature(public_key); |
| 110 } |
| 111 |
| 112 const base::StringPiece& public_key() { return public_key_; }; |
| 113 |
| 114 private: |
| 115 base::StringPiece public_key_; |
| 116 }; |
| 117 |
| 118 TEST_F(TrialTokenTest, ParseEmptyString) { |
| 119 scoped_ptr<TrialToken> empty_token = TrialToken::Parse(""); |
| 120 EXPECT_FALSE(empty_token); |
| 121 } |
| 122 |
| 123 TEST_F(TrialTokenTest, ParseInvalidStrings) { |
| 124 for (size_t i = 0; i < kNumInvalidTokens; ++i) { |
| 125 scoped_ptr<TrialToken> empty_token = TrialToken::Parse(kInvalidTokens[i]); |
| 126 EXPECT_FALSE(empty_token) << "Invalid trial token should not parse: " |
| 127 << kInvalidTokens[i]; |
| 128 } |
| 129 } |
| 130 |
| 131 TEST_F(TrialTokenTest, ParseValidToken) { |
| 132 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); |
| 133 ASSERT_TRUE(token); |
| 134 EXPECT_EQ(kExpectedFeatureName, token->feature_name()); |
| 135 EXPECT_EQ(kExpectedSignature, token->signature()); |
| 136 EXPECT_EQ(kExpectedData, token->data()); |
| 137 EXPECT_EQ(GURL(kExpectedOrigin), token->origin()); |
| 138 EXPECT_EQ(kExpectedExpiry, token->expiry_timestamp()); |
| 139 } |
| 140 |
| 141 TEST_F(TrialTokenTest, ValidateValidToken) { |
| 142 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); |
| 143 ASSERT_TRUE(token); |
| 144 EXPECT_TRUE(ValidateOrigin(token.get(), kExpectedOrigin)); |
| 145 EXPECT_FALSE(ValidateOrigin(token.get(), kInvalidOrigin)); |
| 146 EXPECT_FALSE(ValidateOrigin(token.get(), kInsecureOrigin)); |
| 147 EXPECT_TRUE(ValidateFeatureName(token.get(), kExpectedFeatureName)); |
| 148 EXPECT_FALSE(ValidateFeatureName(token.get(), kInvalidFeatureName)); |
| 149 EXPECT_TRUE( |
| 150 ValidateDate(token.get(), base::Time::FromDoubleT(kValidTimestamp))); |
| 151 EXPECT_FALSE( |
| 152 ValidateDate(token.get(), base::Time::FromDoubleT(kInvalidTimestamp))); |
| 153 } |
| 154 |
| 155 TEST_F(TrialTokenTest, TokenIsAppropriateForOriginAndFeature) { |
| 156 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); |
| 157 ASSERT_TRUE(token); |
| 158 EXPECT_TRUE(token->IsAppropriate(kExpectedOrigin, kExpectedFeatureName)); |
| 159 EXPECT_TRUE(token->IsAppropriate(kExpectedOrigin, |
| 160 base::ToUpperASCII(kExpectedFeatureName))); |
| 161 EXPECT_TRUE(token->IsAppropriate(kExpectedOrigin, |
| 162 base::ToLowerASCII(kExpectedFeatureName))); |
| 163 EXPECT_FALSE(token->IsAppropriate(kInvalidOrigin, kExpectedFeatureName)); |
| 164 EXPECT_FALSE(token->IsAppropriate(kInsecureOrigin, kExpectedFeatureName)); |
| 165 EXPECT_FALSE(token->IsAppropriate(kExpectedOrigin, kInvalidFeatureName)); |
| 166 } |
| 167 |
| 168 TEST_F(TrialTokenTest, ValidateValidSignature) { |
| 169 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); |
| 170 ASSERT_TRUE(token); |
| 171 EXPECT_TRUE(ValidateSignature(token.get(), public_key())); |
| 172 } |
| 173 |
| 174 TEST_F(TrialTokenTest, ValidateInvalidSignature) { |
| 175 scoped_ptr<TrialToken> token = TrialToken::Parse(kInvalidSignatureToken); |
| 176 ASSERT_TRUE(token); |
| 177 EXPECT_FALSE(ValidateSignature(token.get(), public_key())); |
| 178 } |
| 179 |
| 180 TEST_F(TrialTokenTest, ValidateSignatureOnWrongKey) { |
| 181 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); |
| 182 ASSERT_TRUE(token); |
| 183 // Signature will be invalid if tested against the real public key |
| 184 EXPECT_FALSE(token->IsValid(base::Time::FromDoubleT(kValidTimestamp))); |
| 185 } |
| 186 |
| 187 TEST_F(TrialTokenTest, ValidateWhenNotExpired) { |
| 188 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); |
| 189 ASSERT_TRUE(token); |
| 190 } |
| 191 |
| 192 } // namespace content |
| OLD | NEW |