Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(992)

Side by Side Diff: content/common/origin_trials/trial_token_unittest.cc

Issue 1858763003: Change origin trial token format (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Nits Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 // 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, 0x47, 42 // 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, 0x47,
43 // 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, 0x3e, 43 // 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, 0x3e,
44 // 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca 44 // 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca
45 const uint8_t kTestPublicKey2[] = { 45 const uint8_t kTestPublicKey2[] = {
46 0x50, 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c, 46 0x50, 0x07, 0x4d, 0x76, 0x55, 0x56, 0x42, 0x17, 0x2d, 0x8a, 0x9c,
47 0x47, 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51, 47 0x47, 0x96, 0x25, 0xda, 0x70, 0xaa, 0xb9, 0xfd, 0x53, 0x5d, 0x51,
48 0x3e, 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca, 48 0x3e, 0x16, 0xab, 0xb4, 0x86, 0xea, 0xf3, 0x35, 0xc6, 0xca,
49 }; 49 };
50 50
51 // 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
52 const char* kSampleToken = 54 const char* kSampleToken =
53 "1|UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+" 55 "Ap+Q/Qm0ELadZql+dlEGSwnAVsFZKgCEtUZg8idQC3uekkIeSZIY1tftoYdrwhqj"
54 "SdUwk7Dyk/qbDw==|https://valid.example.com|Frobulate|1458766277"; 56 "7FO5L22sNvkZZnacLvmfNwsAAABZeyJvcmlnaW4iOiAiaHR0cHM6Ly92YWxpZC5l"
55 const uint8_t kExpectedVersion = 1; 57 "eGFtcGxlLmNvbTo0NDMiLCAiZmVhdHVyZSI6ICJGcm9idWxhdGUiLCAiZXhwaXJ5"
56 const char* kExpectedSignature = 58 "IjogMTQ1ODc2NjI3N30=";
57 "UsEO0cNxoUtBnHDJdGPWTlXuLENjXcEIPL7Bs7sbvicPCcvAtyqhQuTJ9h/u1R3VZpWigtI+S" 59
58 "dUwk7Dyk/qbDw==";
59 const char* kExpectedData = "https://valid.example.com|Frobulate|1458766277";
60 const char* kExpectedFeatureName = "Frobulate"; 60 const char* kExpectedFeatureName = "Frobulate";
61 const char* kExpectedOrigin = "https://valid.example.com"; 61 const char* kExpectedOrigin = "https://valid.example.com";
62 const uint64_t kExpectedExpiry = 1458766277; 62 const uint64_t kExpectedExpiry = 1458766277;
63 63
64 // 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.
65 const char* kInvalidOrigin = "https://invalid.example.com"; 65 const char* kInvalidOrigin = "https://invalid.example.com";
66 const char* kInsecureOrigin = "http://valid.example.com"; 66 const char* kInsecureOrigin = "http://valid.example.com";
67 const char* kInvalidFeatureName = "Grokalyze"; 67 const char* kInvalidFeatureName = "Grokalyze";
68 68
69 // 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.
70 double kValidTimestamp = 1458766276.0; 70 double kValidTimestamp = 1458766276.0;
71 71
72 // 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
73 // later. 73 // later.
74 double kInvalidTimestamp = 1458766278.0; 74 double kInvalidTimestamp = 1458766278.0;
75 75
76 // Well-formed trial token with an invalid signature. 76 // Well-formed trial token with an invalid signature.
77 const char* kInvalidSignatureToken = 77 const char* kInvalidSignatureToken =
78 "1|CO8hDne98QeFeOJ0DbRZCBN3uE0nyaPgaLlkYhSWnbRoDfEAg+TXELaYfQPfEvKYFauBg/" 78 "AYeNXSDktgG9p4ns5B1WKsLq2TytMxfR4whfbi+oyT0rXyzh+qXYfxbDMGmyjU2Z"
79 "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}";
80 106
81 // Various ill-formed trial tokens. These should all fail to parse. 107 // Various ill-formed trial tokens. These should all fail to parse.
82 const char* kInvalidTokens[] = { 108 const char* kInvalidTokens[] = {
83 // Invalid - only one part 109 // Invalid - Not JSON at all
84 "abcde", 110 "abcde",
85 // Not enough parts 111 // Invalid JSON
86 "https://valid.example.com|FeatureName|1458766277", 112 "{",
87 "Signature|https://valid.example.com|FeatureName|1458766277", 113 // Not an object
88 // Non-numeric version 114 "\"abcde\"",
89 "a|Signature|https://valid.example.com|FeatureName|1458766277", 115 "123.4",
90 "1x|Signature|https://valid.example.com|FeatureName|1458766277", 116 "[0, 1, 2]",
91 // Unsupported version (< min, > max, negative, overflow) 117 // Missing keys
92 "0|Signature|https://valid.example.com|FeatureName|1458766277", 118 "{}",
93 "2|Signature|https://valid.example.com|FeatureName|1458766277", 119 "{\"something\": 1}",
94 "-1|Signature|https://valid.example.com|FeatureName|1458766277", 120 "{\"origin\": \"https://a.a\"}",
95 "99999|Signature|https://valid.example.com|FeatureName|1458766277", 121 "{\"origin\": \"https://a.a\", \"feature\": \"a\"}"
96 // Delimiter in feature name 122 "{\"origin\": \"https://a.a\", \"expiry\": 1458766277}",
97 "1|Signature|https://valid.example.com|Feature|Name|1458766277", 123 "{\"feature\": \"FeatureName\", \"expiry\": 1458766277}",
98 // Extra string field 124 // Incorrect types
99 "1|Signature|https://valid.example.com|FeatureName|1458766277|ExtraField", 125 "{\"origin\": 1, \"feature\": \"a\", \"expiry\": 1458766277}",
100 // Extra numeric field 126 "{\"origin\": \"https://a.a\", \"feature\": 1, \"expiry\": 1458766277}",
101 "1|Signature|https://valid.example.com|FeatureName|1458766277|1458766277", 127 "{\"origin\": \"https://a.a\", \"feature\": \"a\", \"expiry\": \"1\"}",
102 // Non-numeric expiry timestamp
103 "1|Signature|https://valid.example.com|FeatureName|abcdefghij",
104 "1|Signature|https://valid.example.com|FeatureName|1458766277x",
105 // Negative expiry timestamp 128 // Negative expiry timestamp
106 "1|Signature|https://valid.example.com|FeatureName|-1458766277", 129 "{\"origin\": \"https://a.a\", \"feature\": \"a\", \"expiry\": -1}",
107 // Origin not a proper origin URL 130 // Origin not a proper origin URL
108 "1|Signature|abcdef|FeatureName|1458766277", 131 "{\"origin\": \"abcdef\", \"feature\": \"a\", \"expiry\": 1458766277}",
109 "1|Signature|data:text/plain,abcdef|FeatureName|1458766277", 132 "{\"origin\": \"data:text/plain,abcdef\", \"feature\": \"a\", \"expiry\": "
110 "1|Signature|javascript:alert(1)|FeatureName|1458766277"}; 133 "1458766277}",
111 const size_t kNumInvalidTokens = arraysize(kInvalidTokens); 134 "{\"origin\": \"javascript:alert(1)\", \"feature\": \"a\", \"expiry\": "
135 "1458766277}"};
112 136
113 } // namespace 137 } // namespace
114 138
115 class TrialTokenTest : public testing::Test { 139 class TrialTokenTest : public testing::TestWithParam<const char*> {
116 public: 140 public:
117 TrialTokenTest() 141 TrialTokenTest()
118 : expected_origin_(GURL(kExpectedOrigin)), 142 : expected_origin_(GURL(kExpectedOrigin)),
119 invalid_origin_(GURL(kInvalidOrigin)), 143 invalid_origin_(GURL(kInvalidOrigin)),
120 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)),
121 correct_public_key_( 148 correct_public_key_(
122 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), 149 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey),
123 arraysize(kTestPublicKey))), 150 arraysize(kTestPublicKey))),
124 incorrect_public_key_( 151 incorrect_public_key_(
125 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2), 152 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2),
126 arraysize(kTestPublicKey2))) {} 153 arraysize(kTestPublicKey2))) {}
127 154
128 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
129 bool ValidateOrigin(TrialToken* token, const url::Origin origin) { 164 bool ValidateOrigin(TrialToken* token, const url::Origin origin) {
130 return token->ValidateOrigin(origin); 165 return token->ValidateOrigin(origin);
131 } 166 }
132 167
133 bool ValidateFeatureName(TrialToken* token, const char* feature_name) { 168 bool ValidateFeatureName(TrialToken* token, const char* feature_name) {
134 return token->ValidateFeatureName(feature_name); 169 return token->ValidateFeatureName(feature_name);
135 } 170 }
136 171
137 bool ValidateDate(TrialToken* token, const base::Time& now) { 172 bool ValidateDate(TrialToken* token, const base::Time& now) {
138 return token->ValidateDate(now); 173 return token->ValidateDate(now);
139 } 174 }
140 175
141 bool ValidateSignature(TrialToken* token,
142 const base::StringPiece& public_key) {
143 return token->ValidateSignature(public_key);
144 }
145
146 base::StringPiece correct_public_key() { return correct_public_key_; } 176 base::StringPiece correct_public_key() { return correct_public_key_; }
147 base::StringPiece incorrect_public_key() { return incorrect_public_key_; } 177 base::StringPiece incorrect_public_key() { return incorrect_public_key_; }
148 178
149 const url::Origin expected_origin_; 179 const url::Origin expected_origin_;
150 const url::Origin invalid_origin_; 180 const url::Origin invalid_origin_;
151 const url::Origin insecure_origin_; 181 const url::Origin insecure_origin_;
152 182
183 const base::Time expected_expiry_;
184 const base::Time valid_timestamp_;
185 const base::Time invalid_timestamp_;
186
153 private: 187 private:
154 base::StringPiece correct_public_key_; 188 base::StringPiece correct_public_key_;
155 base::StringPiece incorrect_public_key_; 189 base::StringPiece incorrect_public_key_;
156 }; 190 };
157 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
158 TEST_F(TrialTokenTest, ParseEmptyString) { 243 TEST_F(TrialTokenTest, ParseEmptyString) {
159 std::unique_ptr<TrialToken> empty_token = TrialToken::Parse(""); 244 std::unique_ptr<TrialToken> empty_token = Parse("");
160 EXPECT_FALSE(empty_token); 245 EXPECT_FALSE(empty_token);
161 } 246 }
162 247
163 TEST_F(TrialTokenTest, ParseInvalidStrings) { 248 TEST_P(TrialTokenTest, ParseInvalidString) {
164 for (size_t i = 0; i < kNumInvalidTokens; ++i) { 249 std::unique_ptr<TrialToken> empty_token = Parse(GetParam());
165 std::unique_ptr<TrialToken> empty_token = 250 EXPECT_FALSE(empty_token) << "Invalid trial token should not parse.";
166 TrialToken::Parse(kInvalidTokens[i]);
167 EXPECT_FALSE(empty_token) << "Invalid trial token should not parse: "
168 << kInvalidTokens[i];
169 }
170 } 251 }
171 252
253 INSTANTIATE_TEST_CASE_P(, TrialTokenTest, ::testing::ValuesIn(kInvalidTokens));
254
172 TEST_F(TrialTokenTest, ParseValidToken) { 255 TEST_F(TrialTokenTest, ParseValidToken) {
173 std::unique_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 256 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON);
174 ASSERT_TRUE(token); 257 ASSERT_TRUE(token);
175 EXPECT_EQ(kExpectedVersion, token->version());
176 EXPECT_EQ(kExpectedFeatureName, token->feature_name()); 258 EXPECT_EQ(kExpectedFeatureName, token->feature_name());
177 EXPECT_EQ(kExpectedSignature, token->signature());
178 EXPECT_EQ(kExpectedData, token->data());
179 EXPECT_EQ(expected_origin_, token->origin()); 259 EXPECT_EQ(expected_origin_, token->origin());
180 EXPECT_EQ(base::Time::FromDoubleT(kExpectedExpiry), token->expiry_time()); 260 EXPECT_EQ(expected_expiry_, token->expiry_time());
181 } 261 }
182 262
183 TEST_F(TrialTokenTest, ValidateValidToken) { 263 TEST_F(TrialTokenTest, ValidateValidToken) {
184 std::unique_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 264 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON);
185 ASSERT_TRUE(token); 265 ASSERT_TRUE(token);
186 EXPECT_TRUE(ValidateOrigin(token.get(), expected_origin_)); 266 EXPECT_TRUE(ValidateOrigin(token.get(), expected_origin_));
187 EXPECT_FALSE(ValidateOrigin(token.get(), invalid_origin_)); 267 EXPECT_FALSE(ValidateOrigin(token.get(), invalid_origin_));
188 EXPECT_FALSE(ValidateOrigin(token.get(), insecure_origin_)); 268 EXPECT_FALSE(ValidateOrigin(token.get(), insecure_origin_));
189 EXPECT_TRUE(ValidateFeatureName(token.get(), kExpectedFeatureName)); 269 EXPECT_TRUE(ValidateFeatureName(token.get(), kExpectedFeatureName));
190 EXPECT_FALSE(ValidateFeatureName(token.get(), kInvalidFeatureName)); 270 EXPECT_FALSE(ValidateFeatureName(token.get(), kInvalidFeatureName));
191 EXPECT_FALSE(ValidateFeatureName( 271 EXPECT_FALSE(ValidateFeatureName(
192 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str())); 272 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str()));
193 EXPECT_FALSE(ValidateFeatureName( 273 EXPECT_FALSE(ValidateFeatureName(
194 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str())); 274 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str()));
195 EXPECT_TRUE( 275 EXPECT_TRUE(ValidateDate(token.get(), valid_timestamp_));
196 ValidateDate(token.get(), base::Time::FromDoubleT(kValidTimestamp))); 276 EXPECT_FALSE(ValidateDate(token.get(), invalid_timestamp_));
197 EXPECT_FALSE(
198 ValidateDate(token.get(), base::Time::FromDoubleT(kInvalidTimestamp)));
199 } 277 }
200 278
201 TEST_F(TrialTokenTest, TokenIsAppropriateForOriginAndFeature) { 279 TEST_F(TrialTokenTest, TokenIsValidForFeature) {
202 std::unique_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 280 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON);
203 ASSERT_TRUE(token); 281 ASSERT_TRUE(token);
204 EXPECT_TRUE(token->IsAppropriate(expected_origin_, kExpectedFeatureName)); 282 EXPECT_TRUE(token->IsValidForFeature(expected_origin_, kExpectedFeatureName,
205 EXPECT_FALSE(token->IsAppropriate(expected_origin_, 283 valid_timestamp_));
206 base::ToUpperASCII(kExpectedFeatureName))); 284 EXPECT_FALSE(token->IsValidForFeature(
207 EXPECT_FALSE(token->IsAppropriate(expected_origin_, 285 expected_origin_, base::ToUpperASCII(kExpectedFeatureName),
208 base::ToLowerASCII(kExpectedFeatureName))); 286 valid_timestamp_));
209 EXPECT_FALSE(token->IsAppropriate(invalid_origin_, kExpectedFeatureName)); 287 EXPECT_FALSE(token->IsValidForFeature(
210 EXPECT_FALSE(token->IsAppropriate(insecure_origin_, kExpectedFeatureName)); 288 expected_origin_, base::ToLowerASCII(kExpectedFeatureName),
211 EXPECT_FALSE(token->IsAppropriate(expected_origin_, kInvalidFeatureName)); 289 valid_timestamp_));
212 } 290 EXPECT_FALSE(token->IsValidForFeature(invalid_origin_, kExpectedFeatureName,
213 291 valid_timestamp_));
214 TEST_F(TrialTokenTest, ValidateValidSignature) { 292 EXPECT_FALSE(token->IsValidForFeature(insecure_origin_, kExpectedFeatureName,
215 std::unique_ptr<TrialToken> token = TrialToken::Parse(kSampleToken); 293 valid_timestamp_));
216 ASSERT_TRUE(token); 294 EXPECT_FALSE(token->IsValidForFeature(expected_origin_, kInvalidFeatureName,
217 EXPECT_TRUE(ValidateSignature(token.get(), correct_public_key())); 295 valid_timestamp_));
218 } 296 EXPECT_FALSE(token->IsValidForFeature(expected_origin_, kExpectedFeatureName,
219 297 invalid_timestamp_));
220 TEST_F(TrialTokenTest, ValidateInvalidSignature) {
221 std::unique_ptr<TrialToken> token = TrialToken::Parse(kInvalidSignatureToken);
222 ASSERT_TRUE(token);
223 EXPECT_FALSE(ValidateSignature(token.get(), correct_public_key()));
224 }
225
226 TEST_F(TrialTokenTest, ValidateTokenWithCorrectKey) {
227 std::unique_ptr<TrialToken> token = TrialToken::Parse(kSampleToken);
228 ASSERT_TRUE(token);
229 EXPECT_TRUE(token->IsValid(base::Time::FromDoubleT(kValidTimestamp),
230 correct_public_key()));
231 }
232
233 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectKey) {
234 std::unique_ptr<TrialToken> token = TrialToken::Parse(kSampleToken);
235 ASSERT_TRUE(token);
236 EXPECT_FALSE(token->IsValid(base::Time::FromDoubleT(kValidTimestamp),
237 incorrect_public_key()));
238 }
239
240 TEST_F(TrialTokenTest, ValidateWhenNotExpired) {
241 std::unique_ptr<TrialToken> token = TrialToken::Parse(kSampleToken);
242 ASSERT_TRUE(token);
243 } 298 }
244 299
245 } // namespace content 300 } // namespace content
OLDNEW
« no previous file with comments | « content/common/origin_trials/trial_token.cc ('k') | content/common/origin_trials/trial_token_validator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698