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

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

Issue 1909633003: Collect UMA data for Origin Trials (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 valid_timestamp_(base::Time::FromDoubleT(kValidTimestamp)), 146 valid_timestamp_(base::Time::FromDoubleT(kValidTimestamp)),
147 invalid_timestamp_(base::Time::FromDoubleT(kInvalidTimestamp)), 147 invalid_timestamp_(base::Time::FromDoubleT(kInvalidTimestamp)),
148 correct_public_key_( 148 correct_public_key_(
149 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey), 149 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey),
150 arraysize(kTestPublicKey))), 150 arraysize(kTestPublicKey))),
151 incorrect_public_key_( 151 incorrect_public_key_(
152 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2), 152 base::StringPiece(reinterpret_cast<const char*>(kTestPublicKey2),
153 arraysize(kTestPublicKey2))) {} 153 arraysize(kTestPublicKey2))) {}
154 154
155 protected: 155 protected:
156 std::unique_ptr<std::string> Extract(const std::string& token_text, 156 TrialTokenStatus Extract(const std::string& token_text,
157 base::StringPiece public_key) { 157 base::StringPiece public_key,
158 return TrialToken::Extract(token_text, public_key); 158 std::unique_ptr<std::string>& token_payload) {
159 return TrialToken::Extract(token_text, public_key, token_payload);
159 } 160 }
161
162 TrialTokenStatus ExtractIgnorePayload(const std::string& token_text,
163 base::StringPiece public_key) {
164 std::unique_ptr<std::string> token_payload;
165 return Extract(token_text, public_key, token_payload);
166 }
167
160 std::unique_ptr<TrialToken> Parse(const std::string& token_payload) { 168 std::unique_ptr<TrialToken> Parse(const std::string& token_payload) {
161 return TrialToken::Parse(token_payload); 169 return TrialToken::Parse(token_payload);
162 } 170 }
163 171
164 bool ValidateOrigin(TrialToken* token, const url::Origin origin) { 172 bool ValidateOrigin(TrialToken* token, const url::Origin origin) {
165 return token->ValidateOrigin(origin); 173 return token->ValidateOrigin(origin);
166 } 174 }
167 175
168 bool ValidateFeatureName(TrialToken* token, const char* feature_name) { 176 bool ValidateFeatureName(TrialToken* token, const char* feature_name) {
169 return token->ValidateFeatureName(feature_name); 177 return token->ValidateFeatureName(feature_name);
(...skipping 19 matching lines...) Expand all
189 base::StringPiece incorrect_public_key_; 197 base::StringPiece incorrect_public_key_;
190 }; 198 };
191 199
192 // Test the extraction of the signed payload from token strings. This includes 200 // Test the extraction of the signed payload from token strings. This includes
193 // checking the included version identifier, payload length, and cryptographic 201 // checking the included version identifier, payload length, and cryptographic
194 // signature. 202 // signature.
195 203
196 // Test verification of signature and extraction of token JSON from signed 204 // Test verification of signature and extraction of token JSON from signed
197 // token. 205 // token.
198 TEST_F(TrialTokenTest, ValidateValidSignature) { 206 TEST_F(TrialTokenTest, ValidateValidSignature) {
199 std::unique_ptr<std::string> token_payload = 207 std::unique_ptr<std::string> token_payload;
200 Extract(kSampleToken, correct_public_key()); 208 TrialTokenStatus status =
201 ASSERT_TRUE(token_payload); 209 Extract(kSampleToken, correct_public_key(), token_payload);
210 ASSERT_EQ(TRIAL_TOKEN_STATUS_SUCCESS, status);
202 EXPECT_STREQ(kSampleTokenJSON, token_payload.get()->c_str()); 211 EXPECT_STREQ(kSampleTokenJSON, token_payload.get()->c_str());
203 } 212 }
204 213
205 TEST_F(TrialTokenTest, ValidateInvalidSignature) { 214 TEST_F(TrialTokenTest, ValidateInvalidSignature) {
206 std::unique_ptr<std::string> token_payload = 215 TrialTokenStatus status =
207 Extract(kInvalidSignatureToken, correct_public_key()); 216 ExtractIgnorePayload(kInvalidSignatureToken, correct_public_key());
208 ASSERT_FALSE(token_payload); 217 // TODO(chasej): Recreate the invalid token, and change to
218 // TRIAL_TOKEN_STATUS_INVALID_SIGNATURE
219 EXPECT_EQ(TRIAL_TOKEN_STATUS_MALFORMED, status);
209 } 220 }
210 221
211 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectKey) { 222 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectKey) {
212 std::unique_ptr<std::string> token_payload = 223 TrialTokenStatus status =
213 Extract(kSampleToken, incorrect_public_key()); 224 ExtractIgnorePayload(kSampleToken, incorrect_public_key());
214 ASSERT_FALSE(token_payload); 225 EXPECT_EQ(TRIAL_TOKEN_STATUS_INVALID_SIGNATURE, status);
215 } 226 }
216 227
217 TEST_F(TrialTokenTest, ValidateEmptyToken) { 228 TEST_F(TrialTokenTest, ValidateEmptyToken) {
218 std::unique_ptr<std::string> token_payload = 229 TrialTokenStatus status = ExtractIgnorePayload("", correct_public_key());
219 Extract("", correct_public_key()); 230 EXPECT_EQ(TRIAL_TOKEN_STATUS_MALFORMED, status);
220 ASSERT_FALSE(token_payload);
221 } 231 }
222 232
223 TEST_F(TrialTokenTest, ValidateShortToken) { 233 TEST_F(TrialTokenTest, ValidateShortToken) {
224 std::unique_ptr<std::string> token_payload = 234 TrialTokenStatus status =
225 Extract(kTruncatedToken, correct_public_key()); 235 ExtractIgnorePayload(kTruncatedToken, correct_public_key());
226 ASSERT_FALSE(token_payload); 236 EXPECT_EQ(TRIAL_TOKEN_STATUS_MALFORMED, status);
227 } 237 }
228 238
229 TEST_F(TrialTokenTest, ValidateUnsupportedVersion) { 239 TEST_F(TrialTokenTest, ValidateUnsupportedVersion) {
230 std::unique_ptr<std::string> token_payload = 240 TrialTokenStatus status =
231 Extract(kIncorrectVersionToken, correct_public_key()); 241 ExtractIgnorePayload(kIncorrectVersionToken, correct_public_key());
232 ASSERT_FALSE(token_payload); 242 EXPECT_EQ(TRIAL_TOKEN_STATUS_MALFORMED, status);
233 } 243 }
234 244
235 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectLength) { 245 TEST_F(TrialTokenTest, ValidateSignatureWithIncorrectLength) {
236 std::unique_ptr<std::string> token_payload = 246 TrialTokenStatus status =
237 Extract(kIncorrectLengthToken, correct_public_key()); 247 ExtractIgnorePayload(kIncorrectLengthToken, correct_public_key());
238 ASSERT_FALSE(token_payload); 248 EXPECT_EQ(TRIAL_TOKEN_STATUS_MALFORMED, status);
239 } 249 }
240 250
241 // Test parsing of fields from JSON token. 251 // Test parsing of fields from JSON token.
242 252
243 TEST_F(TrialTokenTest, ParseEmptyString) { 253 TEST_F(TrialTokenTest, ParseEmptyString) {
244 std::unique_ptr<TrialToken> empty_token = Parse(""); 254 std::unique_ptr<TrialToken> empty_token = Parse("");
245 EXPECT_FALSE(empty_token); 255 EXPECT_FALSE(empty_token);
246 } 256 }
247 257
248 TEST_P(TrialTokenTest, ParseInvalidString) { 258 TEST_P(TrialTokenTest, ParseInvalidString) {
(...skipping 23 matching lines...) Expand all
272 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str())); 282 token.get(), base::ToUpperASCII(kExpectedFeatureName).c_str()));
273 EXPECT_FALSE(ValidateFeatureName( 283 EXPECT_FALSE(ValidateFeatureName(
274 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str())); 284 token.get(), base::ToLowerASCII(kExpectedFeatureName).c_str()));
275 EXPECT_TRUE(ValidateDate(token.get(), valid_timestamp_)); 285 EXPECT_TRUE(ValidateDate(token.get(), valid_timestamp_));
276 EXPECT_FALSE(ValidateDate(token.get(), invalid_timestamp_)); 286 EXPECT_FALSE(ValidateDate(token.get(), invalid_timestamp_));
277 } 287 }
278 288
279 TEST_F(TrialTokenTest, TokenIsValidForFeature) { 289 TEST_F(TrialTokenTest, TokenIsValidForFeature) {
280 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON); 290 std::unique_ptr<TrialToken> token = Parse(kSampleTokenJSON);
281 ASSERT_TRUE(token); 291 ASSERT_TRUE(token);
282 EXPECT_TRUE(token->IsValidForFeature(expected_origin_, kExpectedFeatureName, 292 EXPECT_EQ(TRIAL_TOKEN_STATUS_SUCCESS,
283 valid_timestamp_)); 293 token->IsValidForFeature(expected_origin_, kExpectedFeatureName,
284 EXPECT_FALSE(token->IsValidForFeature( 294 valid_timestamp_));
285 expected_origin_, base::ToUpperASCII(kExpectedFeatureName), 295 EXPECT_EQ(TRIAL_TOKEN_STATUS_WRONG_FEATURE,
286 valid_timestamp_)); 296 token->IsValidForFeature(expected_origin_,
287 EXPECT_FALSE(token->IsValidForFeature( 297 base::ToUpperASCII(kExpectedFeatureName),
288 expected_origin_, base::ToLowerASCII(kExpectedFeatureName), 298 valid_timestamp_));
289 valid_timestamp_)); 299 EXPECT_EQ(TRIAL_TOKEN_STATUS_WRONG_FEATURE,
290 EXPECT_FALSE(token->IsValidForFeature(invalid_origin_, kExpectedFeatureName, 300 token->IsValidForFeature(expected_origin_,
291 valid_timestamp_)); 301 base::ToLowerASCII(kExpectedFeatureName),
292 EXPECT_FALSE(token->IsValidForFeature(insecure_origin_, kExpectedFeatureName, 302 valid_timestamp_));
293 valid_timestamp_)); 303 EXPECT_EQ(TRIAL_TOKEN_STATUS_WRONG_ORIGIN,
294 EXPECT_FALSE(token->IsValidForFeature(expected_origin_, kInvalidFeatureName, 304 token->IsValidForFeature(invalid_origin_, kExpectedFeatureName,
295 valid_timestamp_)); 305 valid_timestamp_));
296 EXPECT_FALSE(token->IsValidForFeature(expected_origin_, kExpectedFeatureName, 306 EXPECT_EQ(TRIAL_TOKEN_STATUS_WRONG_ORIGIN,
297 invalid_timestamp_)); 307 token->IsValidForFeature(insecure_origin_, kExpectedFeatureName,
308 valid_timestamp_));
309 EXPECT_EQ(TRIAL_TOKEN_STATUS_WRONG_FEATURE,
310 token->IsValidForFeature(expected_origin_, kInvalidFeatureName,
311 valid_timestamp_));
312 EXPECT_EQ(TRIAL_TOKEN_STATUS_EXPIRED,
313 token->IsValidForFeature(expected_origin_, kExpectedFeatureName,
314 invalid_timestamp_));
298 } 315 }
299 316
300 } // namespace content 317 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698