| 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> |
| 8 |
| 7 #include "base/macros.h" | 9 #include "base/macros.h" |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "base/test/simple_test_clock.h" | 12 #include "base/test/simple_test_clock.h" |
| 12 #include "base/time/time.h" | 13 #include "base/time/time.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 22 matching lines...) Expand all Loading... |
| 41 // 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, 0x47, | 42 // 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, 0x47, |
| 42 // 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, 0x3e, | 43 // 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, 0x3e, |
| 43 // 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca | 44 // 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca |
| 44 const uint8_t kTestPublicKey2[] = { | 45 const uint8_t kTestPublicKey2[] = { |
| 45 0x50, 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, | 46 0x50, 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, |
| 46 0x47, 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, | 47 0x47, 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, |
| 47 0x3e, 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca, | 48 0x3e, 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca, |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 // This is a good trial token, signed with the above test private key. | 51 // This is a good trial token, signed with the above test private key. |
| 52 // Generate this token with the command (in tools/origin_trials): |
| 53 // generate_token.py valid.example.com Frobulate --expire-timestamp=1458766277 |
| 51 const char* kSampleToken = | 54 const char* kSampleToken = |
| 52 "1|UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+" | 55 "Ap+Q/Qm0ELadZql+dlEGSwnAVsFZKgCEtUZg8idQC3uekkIeSZIY1tftoYdrwhqj" |
| 53 "SdUwk7Dyk/qbDw==|https://valid.example.com|Frobulate|1458766277"; | 56 "7FO5L22sNvkZZnacLvmfNwsAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 54 const uint8_t kExpectedVersion = 1; | 57 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 55 const char* kExpectedSignature = | 58 "IjogMTQ1ODc2NjI3N30="; |
| 56 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S" | 59 |
| 57 "dUwk7Dyk/qbDw=="; | |
| 58 const char* kExpectedData = "https://valid.example.com|Frobulate|1458766277"; | |
| 59 const char* kExpectedFeatureName = "Frobulate"; | 60 const char* kExpectedFeatureName = "Frobulate"; |
| 60 const char* kExpectedOrigin = "https://valid.example.com"; | 61 const char* kExpectedOrigin = "https://valid.example.com"; |
| 61 const uint64_t kExpectedExpiry = 1458766277; | 62 const uint64_t kExpectedExpiry = 1458766277; |
| 62 | 63 |
| 63 // The token should not be valid for this origin, or for this feature. | 64 // The token should not be valid for this origin, or for this feature. |
| 64 const char* kInvalidOrigin = "https://invalid.example.com"; | 65 const char* kInvalidOrigin = "https://invalid.example.com"; |
| 65 const char* kInsecureOrigin = "http://valid.example.com"; | 66 const char* kInsecureOrigin = "http://valid.example.com"; |
| 66 const char* kInvalidFeatureName = "Grokalyze"; | 67 const char* kInvalidFeatureName = "Grokalyze"; |
| 67 | 68 |
| 68 // The token should be valid if the current time is kValidTimestamp or earlier. | 69 // The token should be valid if the current time is kValidTimestamp or earlier. |
| 69 double kValidTimestamp = 1458766276.0; | 70 double kValidTimestamp = 1458766276.0; |
| 70 | 71 |
| 71 // The token should be invalid if the current time is kInvalidTimestamp or | 72 // The token should be invalid if the current time is kInvalidTimestamp or |
| 72 // later. | 73 // later. |
| 73 double kInvalidTimestamp = 1458766278.0; | 74 double kInvalidTimestamp = 1458766278.0; |
| 74 | 75 |
| 75 // Well-formed trial token with an invalid signature. | 76 // Well-formed trial token with an invalid signature. |
| 76 const char* kInvalidSignatureToken = | 77 const char* kInvalidSignatureToken = |
| 77 "1|CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/" | 78 "AYeNXSDktgG9p4ns5B1WKsLq2TytMxfR4whfbi+oyT0rXyzh+qXYfxbDMGmyjU2Z" |
| 78 "hnxmba765hz0mXMc==|https://valid.example.com|Frobulate|1458766277"; | 79 "lEJ16vQObMXJoOaYUqd8xwkAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 80 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 81 "IjogMTQ1ODc2NjI3N30="; |
| 82 |
| 83 // Trial token truncated in the middle of the length field; too short to |
| 84 // possibly be valid. |
| 85 const char kTruncatedToken[] = |
| 86 "Ap+Q/Qm0ELadZql+dlEGSwnAVsFZKgCEtUZg8idQC3uekkIeSZIY1tftoYdrwhqj" |
| 87 "7FO5L22sNvkZZnacLvmfNwsA"; |
| 88 |
| 89 // Trial token with an incorrectly-declared length, but with a valid signature. |
| 90 const char kIncorrectLengthToken[] = |
| 91 "Ao06eNl/CZuM88qurWKX4RfoVEpHcVHWxdOTrEXZkaC1GUHyb/8L4sthADiVWdc9" |
| 92 "kXFyF1BW5bbraqp6MBVr3wEAAABaeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 93 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 94 "IjogMTQ1ODc2NjI3N30="; |
| 95 |
| 96 // Trial token with a misidentified version (42). |
| 97 const char kIncorrectVersionToken[] = |
| 98 "KlH8wVLT5o59uDvlJESorMDjzgWnvG1hmIn/GiT9Ng3f45ratVeiXCNTeaJheOaG" |
| 99 "A6kX4ir4Amv8aHVC+OJHZQkAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l" |
| 100 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5" |
| 101 "IjogMTQ1ODc2NjI3N30="; |
| 102 |
| 103 const char kSampleTokenJSON[] = |
| 104 "{\"origin\": \"https://valid.example.com:443\", \"feature\": " |
| 105 "\"Frobulate\", \"expiry\": 1458766277}"; |
| 79 | 106 |
| 80 // Various ill-formed trial tokens. These should all fail to parse. | 107 // Various ill-formed trial tokens. These should all fail to parse. |
| 81 const char* kInvalidTokens[] = { | 108 const char* kInvalidTokens[] = { |
| 82 // Invalid - only one part | 109 // Invalid - Not JSON at all |
| 83 "abcde", | 110 "abcde", |
| 84 // Not enough parts | 111 // Invalid JSON |
| 85 "https://valid.example.com|FeatureName|1458766277", | 112 "{", |
| 86 "Signature|https://valid.example.com|FeatureName|1458766277", | 113 // Not an object |
| 87 // Non-numeric version | 114 "\"abcde\"", |
| 88 "a|Signature|https://valid.example.com|FeatureName|1458766277", | 115 "123.4", |
| 89 "1x|Signature|https://valid.example.com|FeatureName|1458766277", | 116 "[0, 1, 2]", |
| 90 // Unsupported version (< min, > max, negative, overflow) | 117 // Missing keys |
| 91 "0|Signature|https://valid.example.com|FeatureName|1458766277", | 118 "{}", |
| 92 "2|Signature|https://valid.example.com|FeatureName|1458766277", | 119 "{\"something\": 1}", |
| 93 "-1|Signature|https://valid.example.com|FeatureName|1458766277", | 120 "{\"origin\": \"https://a.a\"}", |
| 94 "99999|Signature|https://valid.example.com|FeatureName|1458766277", | 121 "{\"origin\": \"https://a.a\", \"feature\": \"a\"}" |
| 95 // Delimiter in feature name | 122 "{\"origin\": \"https://a.a\", \"expiry\": 1458766277}", |
| 96 "1|Signature|https://valid.example.com|Feature|Name|1458766277", | 123 "{\"feature\": \"FeatureName\", \"expiry\": 1458766277}", |
| 97 // Extra string field | 124 // Incorrect types |
| 98 "1|Signature|https://valid.example.com|FeatureName|1458766277|ExtraField", | 125 "{\"origin\": 1, \"feature\": \"a\", \"expiry\": 1458766277}", |
| 99 // Extra numeric field | 126 "{\"origin\": \"https://a.a\", \"feature\": 1, \"expiry\": 1458766277}", |
| 100 "1|Signature|https://valid.example.com|FeatureName|1458766277|1458766277", | 127 "{\"origin\": \"https://a.a\", \"feature\": \"a\", \"expiry\": \"1\"}", |
| 101 // Non-numeric expiry timestamp | |
| 102 "1|Signature|https://valid.example.com|FeatureName|abcdefghij", | |
| 103 "1|Signature|https://valid.example.com|FeatureName|1458766277x", | |
| 104 // Negative expiry timestamp | 128 // Negative expiry timestamp |
| 105 "1|Signature|https://valid.example.com|FeatureName|-1458766277", | 129 "{\"origin\": \"https://a.a\", \"feature\": \"a\", \"expiry\": -1}", |
| 106 // Origin not a proper origin URL | 130 // Origin not a proper origin URL |
| 107 "1|Signature|abcdef|FeatureName|1458766277", | 131 "{\"origin\": \"abcdef\", \"feature\": \"a\", \"expiry\": 1458766277}", |
| 108 "1|Signature|data:text/plain,abcdef|FeatureName|1458766277", | 132 "{\"origin\": \"data:text/plain,abcdef\", \"feature\": \"a\", \"expiry\": " |
| 109 "1|Signature|javascript:alert(1)|FeatureName|1458766277"}; | 133 "1458766277}", |
| 110 const size_t kNumInvalidTokens = arraysize(kInvalidTokens); | 134 "{\"origin\": \"javascript:alert(1)\", \"feature\": \"a\", \"expiry\": " |
| 135 "1458766277}"}; |
| 111 | 136 |
| 112 } // namespace | 137 } // namespace |
| 113 | 138 |
| 114 class TrialTokenTest : public testing::Test { | 139 class TrialTokenTest : public testing::TestWithParam<const char*> { |
| 115 public: | 140 public: |
| 116 TrialTokenTest() | 141 TrialTokenTest() |
| 117 : expected_origin_(GURL(kExpectedOrigin)), | 142 : expected_origin_(GURL(kExpectedOrigin)), |
| 118 invalid_origin_(GURL(kInvalidOrigin)), | 143 invalid_origin_(GURL(kInvalidOrigin)), |
| 119 insecure_origin_(GURL(kInsecureOrigin)), | 144 insecure_origin_(GURL(kInsecureOrigin)), |
| 145 expected_expiry_(base::Time::FromDoubleT(kExpectedExpiry)), |
| 146 valid_timestamp_(base::Time::FromDoubleT(kValidTimestamp)), |
| 147 invalid_timestamp_(base::Time::FromDoubleT(kInvalidTimestamp)), |
| 120 correct_public_key_( | 148 correct_public_key_( |
| 121 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), | 149 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), |
| 122 arraysize(kTestPublicKey))), | 150 arraysize(kTestPublicKey))), |
| 123 incorrect_public_key_( | 151 incorrect_public_key_( |
| 124 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2), | 152 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2), |
| 125 arraysize(kTestPublicKey2))) {} | 153 arraysize(kTestPublicKey2))) {} |
| 126 | 154 |
| 127 protected: | 155 protected: |
| 156 std::unique_ptr<std::string> Extract(const std::string& token_text, |
| 157 base::StringPiece public_key) { |
| 158 return TrialToken::Extract(token_text, public_key); |
| 159 } |
| 160 std::unique_ptr<TrialToken> Parse(const std::string& token_payload) { |
| 161 return TrialToken::Parse(token_payload); |
| 162 } |
| 163 |
| 128 bool ValidateOrigin(TrialToken* token, const url::Origin origin) { | 164 bool ValidateOrigin(TrialToken* token, const url::Origin origin) { |
| 129 return token->ValidateOrigin(origin); | 165 return token->ValidateOrigin(origin); |
| 130 } | 166 } |
| 131 | 167 |
| 132 bool ValidateFeatureName(TrialToken* token, const char* feature_name) { | 168 bool ValidateFeatureName(TrialToken* token, const char* feature_name) { |
| 133 return token->ValidateFeatureName(feature_name); | 169 return token->ValidateFeatureName(feature_name); |
| 134 } | 170 } |
| 135 | 171 |
| 136 bool ValidateDate(TrialToken* token, const base::Time& now) { | 172 bool ValidateDate(TrialToken* token, const base::Time& now) { |
| 137 return token->ValidateDate(now); | 173 return token->ValidateDate(now); |
| 138 } | 174 } |
| 139 | 175 |
| 140 bool ValidateSignature(TrialToken* token, | |
| 141 const base::StringPiece& public_key) { | |
| 142 return token->ValidateSignature(public_key); | |
| 143 } | |
| 144 | |
| 145 base::StringPiece correct_public_key() { return correct_public_key_; } | 176 base::StringPiece correct_public_key() { return correct_public_key_; } |
| 146 base::StringPiece incorrect_public_key() { return incorrect_public_key_; } | 177 base::StringPiece incorrect_public_key() { return incorrect_public_key_; } |
| 147 | 178 |
| 148 const url::Origin expected_origin_; | 179 const url::Origin expected_origin_; |
| 149 const url::Origin invalid_origin_; | 180 const url::Origin invalid_origin_; |
| 150 const url::Origin insecure_origin_; | 181 const url::Origin insecure_origin_; |
| 151 | 182 |
| 183 const base::Time expected_expiry_; |
| 184 const base::Time valid_timestamp_; |
| 185 const base::Time invalid_timestamp_; |
| 186 |
| 152 private: | 187 private: |
| 153 base::StringPiece correct_public_key_; | 188 base::StringPiece correct_public_key_; |
| 154 base::StringPiece incorrect_public_key_; | 189 base::StringPiece incorrect_public_key_; |
| 155 }; | 190 }; |
| 156 | 191 |
| 192 // Test the extraction of the signed payload from token strings. This includes |
| 193 // checking the included version identifier, payload length, and cryptographic |
| 194 // signature. |
| 195 |
| 196 // Test verification of signature and extraction of token JSON from signed |
| 197 // token. |
| 198 TEST_F(TrialTokenTest, ValidateValidSignature) { |
| 199 std::unique_ptr<std::string> token_payload = |
| 200 Extract(kSampleToken, correct_public_key()); |
| 201 ASSERT_TRUE(token_payload); |
| 202 EXPECT_STREQ(kSampleTokenJSON, token_payload.get()->c_str()); |
| 203 } |
| 204 |
| 205 TEST_F(TrialTokenTest, ValidateInvalidSignature) { |
| 206 std::unique_ptr<std::string> token_payload = |
| 207 Extract(kInvalidSignatureToken, correct_public_key()); |
| 208 ASSERT_FALSE(token_payload); |
| 209 } |
| 210 |
| 211 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectKey) { |
| 212 std::unique_ptr<std::string> token_payload = |
| 213 Extract(kSampleToken, incorrect_public_key()); |
| 214 ASSERT_FALSE(token_payload); |
| 215 } |
| 216 |
| 217 TEST_F(TrialTokenTest, ValidateEmptyToken) { |
| 218 std::unique_ptr<std::string> token_payload = |
| 219 Extract("", correct_public_key()); |
| 220 ASSERT_FALSE(token_payload); |
| 221 } |
| 222 |
| 223 TEST_F(TrialTokenTest, ValidateShortToken) { |
| 224 std::unique_ptr<std::string> token_payload = |
| 225 Extract(kTruncatedToken, correct_public_key()); |
| 226 ASSERT_FALSE(token_payload); |
| 227 } |
| 228 |
| 229 TEST_F(TrialTokenTest, ValidateUnsupportedVersion) { |
| 230 std::unique_ptr<std::string> token_payload = |
| 231 Extract(kIncorrectVersionToken, correct_public_key()); |
| 232 ASSERT_FALSE(token_payload); |
| 233 } |
| 234 |
| 235 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectLength) { |
| 236 std::unique_ptr<std::string> token_payload = |
| 237 Extract(kIncorrectLengthToken, correct_public_key()); |
| 238 ASSERT_FALSE(token_payload); |
| 239 } |
| 240 |
| 241 // Test parsing of fields from JSON token. |
| 242 |
| 157 TEST_F(TrialTokenTest, ParseEmptyString) { | 243 TEST_F(TrialTokenTest, ParseEmptyString) { |
| 158 scoped_ptr<TrialToken> empty_token = TrialToken::Parse(""); | 244 std::unique_ptr<TrialToken> empty_token = Parse(""); |
| 159 EXPECT_FALSE(empty_token); | 245 EXPECT_FALSE(empty_token); |
| 160 } | 246 } |
| 161 | 247 |
| 162 TEST_F(TrialTokenTest, ParseInvalidStrings) { | 248 TEST_P(TrialTokenTest, ParseInvalidString) { |
| 163 for (size_t i = 0; i < kNumInvalidTokens; ++i) { | 249 std::unique_ptr<TrialToken> empty_token = Parse(GetParam()); |
| 164 scoped_ptr<TrialToken> empty_token = TrialToken::Parse(kInvalidTokens[i]); | 250 EXPECT_FALSE(empty_token) << "Invalid trial token should not parse."; |
| 165 EXPECT_FALSE(empty_token) << "Invalid trial token should not parse: " | |
| 166 << kInvalidTokens[i]; | |
| 167 } | |
| 168 } | 251 } |
| 169 | 252 |
| 253 INSTANTIATE_TEST_CASE_P(, TrialTokenTest, ::testing::ValuesIn(kInvalidTokens)); |
| 254 |
| 170 TEST_F(TrialTokenTest, ParseValidToken) { | 255 TEST_F(TrialTokenTest, ParseValidToken) { |
| 171 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); | 256 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON); |
| 172 ASSERT_TRUE(token); | 257 ASSERT_TRUE(token); |
| 173 EXPECT_EQ(kExpectedVersion, token->version()); | |
| 174 EXPECT_EQ(kExpectedFeatureName, token->feature_name()); | 258 EXPECT_EQ(kExpectedFeatureName, token->feature_name()); |
| 175 EXPECT_EQ(kExpectedSignature, token->signature()); | |
| 176 EXPECT_EQ(kExpectedData, token->data()); | |
| 177 EXPECT_EQ(expected_origin_, token->origin()); | 259 EXPECT_EQ(expected_origin_, token->origin()); |
| 178 EXPECT_EQ(base::Time::FromDoubleT(kExpectedExpiry), token->expiry_time()); | 260 EXPECT_EQ(expected_expiry_, token->expiry_time()); |
| 179 } | 261 } |
| 180 | 262 |
| 181 TEST_F(TrialTokenTest, ValidateValidToken) { | 263 TEST_F(TrialTokenTest, ValidateValidToken) { |
| 182 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); | 264 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON); |
| 183 ASSERT_TRUE(token); | 265 ASSERT_TRUE(token); |
| 184 EXPECT_TRUE(ValidateOrigin(token.get(), expected_origin_)); | 266 EXPECT_TRUE(ValidateOrigin(token.get(), expected_origin_)); |
| 185 EXPECT_FALSE(ValidateOrigin(token.get(), invalid_origin_)); | 267 EXPECT_FALSE(ValidateOrigin(token.get(), invalid_origin_)); |
| 186 EXPECT_FALSE(ValidateOrigin(token.get(), insecure_origin_)); | 268 EXPECT_FALSE(ValidateOrigin(token.get(), insecure_origin_)); |
| 187 EXPECT_TRUE(ValidateFeatureName(token.get(), kExpectedFeatureName)); | 269 EXPECT_TRUE(ValidateFeatureName(token.get(), kExpectedFeatureName)); |
| 188 EXPECT_FALSE(ValidateFeatureName(token.get(), kInvalidFeatureName)); | 270 EXPECT_FALSE(ValidateFeatureName(token.get(), kInvalidFeatureName)); |
| 189 EXPECT_FALSE(ValidateFeatureName( | 271 EXPECT_FALSE(ValidateFeatureName( |
| 190 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str())); | 272 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str())); |
| 191 EXPECT_FALSE(ValidateFeatureName( | 273 EXPECT_FALSE(ValidateFeatureName( |
| 192 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str())); | 274 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str())); |
| 193 EXPECT_TRUE( | 275 EXPECT_TRUE(ValidateDate(token.get(), valid_timestamp_)); |
| 194 ValidateDate(token.get(), base::Time::FromDoubleT(kValidTimestamp))); | 276 EXPECT_FALSE(ValidateDate(token.get(), invalid_timestamp_)); |
| 195 EXPECT_FALSE( | |
| 196 ValidateDate(token.get(), base::Time::FromDoubleT(kInvalidTimestamp))); | |
| 197 } | 277 } |
| 198 | 278 |
| 199 TEST_F(TrialTokenTest, TokenIsAppropriateForOriginAndFeature) { | 279 TEST_F(TrialTokenTest, TokenIsValidForFeature) { |
| 200 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); | 280 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON); |
| 201 ASSERT_TRUE(token); | 281 ASSERT_TRUE(token); |
| 202 EXPECT_TRUE(token->IsAppropriate(expected_origin_, kExpectedFeatureName)); | 282 EXPECT_TRUE(token->IsValidForFeature(expected_origin_, kExpectedFeatureName, |
| 203 EXPECT_FALSE(token->IsAppropriate(expected_origin_, | 283 valid_timestamp_)); |
| 204 base::ToUpperASCII(kExpectedFeatureName))); | 284 EXPECT_FALSE(token->IsValidForFeature( |
| 205 EXPECT_FALSE(token->IsAppropriate(expected_origin_, | 285 expected_origin_, base::ToUpperASCII(kExpectedFeatureName), |
| 206 base::ToLowerASCII(kExpectedFeatureName))); | 286 valid_timestamp_)); |
| 207 EXPECT_FALSE(token->IsAppropriate(invalid_origin_, kExpectedFeatureName)); | 287 EXPECT_FALSE(token->IsValidForFeature( |
| 208 EXPECT_FALSE(token->IsAppropriate(insecure_origin_, kExpectedFeatureName)); | 288 expected_origin_, base::ToLowerASCII(kExpectedFeatureName), |
| 209 EXPECT_FALSE(token->IsAppropriate(expected_origin_, kInvalidFeatureName)); | 289 valid_timestamp_)); |
| 210 } | 290 EXPECT_FALSE(token->IsValidForFeature(invalid_origin_, kExpectedFeatureName, |
| 211 | 291 valid_timestamp_)); |
| 212 TEST_F(TrialTokenTest, ValidateValidSignature) { | 292 EXPECT_FALSE(token->IsValidForFeature(insecure_origin_, kExpectedFeatureName, |
| 213 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); | 293 valid_timestamp_)); |
| 214 ASSERT_TRUE(token); | 294 EXPECT_FALSE(token->IsValidForFeature(expected_origin_, kInvalidFeatureName, |
| 215 EXPECT_TRUE(ValidateSignature(token.get(), correct_public_key())); | 295 valid_timestamp_)); |
| 216 } | 296 EXPECT_FALSE(token->IsValidForFeature(expected_origin_, kExpectedFeatureName, |
| 217 | 297 invalid_timestamp_)); |
| 218 TEST_F(TrialTokenTest, ValidateInvalidSignature) { | |
| 219 scoped_ptr<TrialToken> token = TrialToken::Parse(kInvalidSignatureToken); | |
| 220 ASSERT_TRUE(token); | |
| 221 EXPECT_FALSE(ValidateSignature(token.get(), correct_public_key())); | |
| 222 } | |
| 223 | |
| 224 TEST_F(TrialTokenTest, ValidateTokenWithCorrectKey) { | |
| 225 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); | |
| 226 ASSERT_TRUE(token); | |
| 227 EXPECT_TRUE(token->IsValid(base::Time::FromDoubleT(kValidTimestamp), | |
| 228 correct_public_key())); | |
| 229 } | |
| 230 | |
| 231 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectKey) { | |
| 232 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); | |
| 233 ASSERT_TRUE(token); | |
| 234 EXPECT_FALSE(token->IsValid(base::Time::FromDoubleT(kValidTimestamp), | |
| 235 incorrect_public_key())); | |
| 236 } | |
| 237 | |
| 238 TEST_F(TrialTokenTest, ValidateWhenNotExpired) { | |
| 239 scoped_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); | |
| 240 ASSERT_TRUE(token); | |
| 241 } | 298 } |
| 242 | 299 |
| 243 } // namespace content | 300 } // namespace content |
| OLD | NEW |