| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/quic/test_tools/crypto_test_utils.h" | 5 #include "net/quic/test_tools/crypto_test_utils.h" |
| 6 | 6 |
| 7 #include "net/quic/crypto/channel_id.h" | 7 #include "net/quic/crypto/channel_id.h" |
| 8 #include "net/quic/crypto/common_cert_set.h" | 8 #include "net/quic/crypto/common_cert_set.h" |
| 9 #include "net/quic/crypto/crypto_handshake.h" | 9 #include "net/quic/crypto/crypto_handshake.h" |
| 10 #include "net/quic/crypto/quic_crypto_server_config.h" | 10 #include "net/quic/crypto/quic_crypto_server_config.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 | 47 |
| 48 const vector<CryptoHandshakeMessage>& messages() const { return messages_; } | 48 const vector<CryptoHandshakeMessage>& messages() const { return messages_; } |
| 49 | 49 |
| 50 private: | 50 private: |
| 51 bool error_; | 51 bool error_; |
| 52 vector<CryptoHandshakeMessage> messages_; | 52 vector<CryptoHandshakeMessage> messages_; |
| 53 }; | 53 }; |
| 54 | 54 |
| 55 // HexChar parses |c| as a hex character. If valid, it sets |*value| to the | 55 // HexChar parses |c| as a hex character. If valid, it sets |*value| to the |
| 56 // value of the hex character and returns true. Otherwise it returns false. | 56 // value of the hex character and returns true. Otherwise it returns false. |
| 57 bool HexChar(char c, uint8* value) { | 57 bool HexChar(char c, uint8_t* value) { |
| 58 if (c >= '0' && c <= '9') { | 58 if (c >= '0' && c <= '9') { |
| 59 *value = c - '0'; | 59 *value = c - '0'; |
| 60 return true; | 60 return true; |
| 61 } | 61 } |
| 62 if (c >= 'a' && c <= 'f') { | 62 if (c >= 'a' && c <= 'f') { |
| 63 *value = c - 'a' + 10; | 63 *value = c - 'a' + 10; |
| 64 return true; | 64 return true; |
| 65 } | 65 } |
| 66 if (c >= 'A' && c <= 'F') { | 66 if (c >= 'A' && c <= 'F') { |
| 67 *value = c - 'A' + 10; | 67 *value = c - 'A' + 10; |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 QuicTag tag) { | 281 QuicTag tag) { |
| 282 QuicTagValueMap::const_iterator it = message.tag_value_map().find(tag); | 282 QuicTagValueMap::const_iterator it = message.tag_value_map().find(tag); |
| 283 if (it == message.tag_value_map().end()) { | 283 if (it == message.tag_value_map().end()) { |
| 284 return string(); | 284 return string(); |
| 285 } | 285 } |
| 286 return it->second; | 286 return it->second; |
| 287 } | 287 } |
| 288 | 288 |
| 289 class MockCommonCertSets : public CommonCertSets { | 289 class MockCommonCertSets : public CommonCertSets { |
| 290 public: | 290 public: |
| 291 MockCommonCertSets(StringPiece cert, uint64 hash, uint32 index) | 291 MockCommonCertSets(StringPiece cert, uint64_t hash, uint32_t index) |
| 292 : cert_(cert.as_string()), hash_(hash), index_(index) {} | 292 : cert_(cert.as_string()), hash_(hash), index_(index) {} |
| 293 | 293 |
| 294 StringPiece GetCommonHashes() const override { | 294 StringPiece GetCommonHashes() const override { |
| 295 CHECK(false) << "not implemented"; | 295 CHECK(false) << "not implemented"; |
| 296 return StringPiece(); | 296 return StringPiece(); |
| 297 } | 297 } |
| 298 | 298 |
| 299 StringPiece GetCert(uint64 hash, uint32 index) const override { | 299 StringPiece GetCert(uint64_t hash, uint32_t index) const override { |
| 300 if (hash == hash_ && index == index_) { | 300 if (hash == hash_ && index == index_) { |
| 301 return cert_; | 301 return cert_; |
| 302 } | 302 } |
| 303 return StringPiece(); | 303 return StringPiece(); |
| 304 } | 304 } |
| 305 | 305 |
| 306 bool MatchCert(StringPiece cert, | 306 bool MatchCert(StringPiece cert, |
| 307 StringPiece common_set_hashes, | 307 StringPiece common_set_hashes, |
| 308 uint64* out_hash, | 308 uint64_t* out_hash, |
| 309 uint32* out_index) const override { | 309 uint32_t* out_index) const override { |
| 310 if (cert != cert_) { | 310 if (cert != cert_) { |
| 311 return false; | 311 return false; |
| 312 } | 312 } |
| 313 | 313 |
| 314 if (common_set_hashes.size() % sizeof(uint64) != 0) { | 314 if (common_set_hashes.size() % sizeof(uint64_t) != 0) { |
| 315 return false; | 315 return false; |
| 316 } | 316 } |
| 317 bool client_has_set = false; | 317 bool client_has_set = false; |
| 318 for (size_t i = 0; i < common_set_hashes.size(); i += sizeof(uint64)) { | 318 for (size_t i = 0; i < common_set_hashes.size(); i += sizeof(uint64_t)) { |
| 319 uint64 hash; | 319 uint64_t hash; |
| 320 memcpy(&hash, common_set_hashes.data() + i, sizeof(hash)); | 320 memcpy(&hash, common_set_hashes.data() + i, sizeof(hash)); |
| 321 if (hash == hash_) { | 321 if (hash == hash_) { |
| 322 client_has_set = true; | 322 client_has_set = true; |
| 323 break; | 323 break; |
| 324 } | 324 } |
| 325 } | 325 } |
| 326 | 326 |
| 327 if (!client_has_set) { | 327 if (!client_has_set) { |
| 328 return false; | 328 return false; |
| 329 } | 329 } |
| 330 | 330 |
| 331 *out_hash = hash_; | 331 *out_hash = hash_; |
| 332 *out_index = index_; | 332 *out_index = index_; |
| 333 return true; | 333 return true; |
| 334 } | 334 } |
| 335 | 335 |
| 336 private: | 336 private: |
| 337 const string cert_; | 337 const string cert_; |
| 338 const uint64 hash_; | 338 const uint64_t hash_; |
| 339 const uint32 index_; | 339 const uint32_t index_; |
| 340 }; | 340 }; |
| 341 | 341 |
| 342 CommonCertSets* CryptoTestUtils::MockCommonCertSets(StringPiece cert, | 342 CommonCertSets* CryptoTestUtils::MockCommonCertSets(StringPiece cert, |
| 343 uint64 hash, | 343 uint64_t hash, |
| 344 uint32 index) { | 344 uint32_t index) { |
| 345 return new class MockCommonCertSets(cert, hash, index); | 345 return new class MockCommonCertSets(cert, hash, index); |
| 346 } | 346 } |
| 347 | 347 |
| 348 // static | 348 // static |
| 349 void CryptoTestUtils::FillInDummyReject(CryptoHandshakeMessage* rej, | 349 void CryptoTestUtils::FillInDummyReject(CryptoHandshakeMessage* rej, |
| 350 bool reject_is_stateless) { | 350 bool reject_is_stateless) { |
| 351 if (reject_is_stateless) { | 351 if (reject_is_stateless) { |
| 352 rej->set_tag(kSREJ); | 352 rej->set_tag(kSREJ); |
| 353 } else { | 353 } else { |
| 354 rej->set_tag(kREJ); | 354 rej->set_tag(kREJ); |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 | 498 |
| 499 QuicTag tag = 0; | 499 QuicTag tag = 0; |
| 500 | 500 |
| 501 if (tagstr[0] == '#') { | 501 if (tagstr[0] == '#') { |
| 502 CHECK_EQ(static_cast<size_t>(1 + 2 * 4), len); | 502 CHECK_EQ(static_cast<size_t>(1 + 2 * 4), len); |
| 503 tagstr++; | 503 tagstr++; |
| 504 | 504 |
| 505 for (size_t i = 0; i < 8; i++) { | 505 for (size_t i = 0; i < 8; i++) { |
| 506 tag <<= 4; | 506 tag <<= 4; |
| 507 | 507 |
| 508 uint8 v = 0; | 508 uint8_t v = 0; |
| 509 CHECK(HexChar(tagstr[i], &v)); | 509 CHECK(HexChar(tagstr[i], &v)); |
| 510 tag |= v; | 510 tag |= v; |
| 511 } | 511 } |
| 512 | 512 |
| 513 return tag; | 513 return tag; |
| 514 } | 514 } |
| 515 | 515 |
| 516 CHECK_LE(len, 4u); | 516 CHECK_LE(len, 4u); |
| 517 for (size_t i = 0; i < 4; i++) { | 517 for (size_t i = 0; i < 4; i++) { |
| 518 tag >>= 8; | 518 tag >>= 8; |
| 519 if (i < len) { | 519 if (i < len) { |
| 520 tag |= static_cast<uint32>(tagstr[i]) << 24; | 520 tag |= static_cast<uint32_t>(tagstr[i]) << 24; |
| 521 } | 521 } |
| 522 } | 522 } |
| 523 | 523 |
| 524 return tag; | 524 return tag; |
| 525 } | 525 } |
| 526 | 526 |
| 527 // static | 527 // static |
| 528 CryptoHandshakeMessage CryptoTestUtils::Message(const char* message_tag, ...) { | 528 CryptoHandshakeMessage CryptoTestUtils::Message(const char* message_tag, ...) { |
| 529 va_list ap; | 529 va_list ap; |
| 530 va_start(ap, message_tag); | 530 va_start(ap, message_tag); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 553 | 553 |
| 554 const QuicTag tag = ParseTag(tagstr); | 554 const QuicTag tag = ParseTag(tagstr); |
| 555 const char* valuestr = va_arg(ap, const char*); | 555 const char* valuestr = va_arg(ap, const char*); |
| 556 | 556 |
| 557 size_t len = strlen(valuestr); | 557 size_t len = strlen(valuestr); |
| 558 if (len > 0 && valuestr[0] == '#') { | 558 if (len > 0 && valuestr[0] == '#') { |
| 559 valuestr++; | 559 valuestr++; |
| 560 len--; | 560 len--; |
| 561 | 561 |
| 562 CHECK_EQ(0u, len % 2); | 562 CHECK_EQ(0u, len % 2); |
| 563 scoped_ptr<uint8[]> buf(new uint8[len / 2]); | 563 scoped_ptr<uint8_t[]> buf(new uint8_t[len / 2]); |
| 564 | 564 |
| 565 for (size_t i = 0; i < len / 2; i++) { | 565 for (size_t i = 0; i < len / 2; i++) { |
| 566 uint8 v = 0; | 566 uint8_t v = 0; |
| 567 CHECK(HexChar(valuestr[i * 2], &v)); | 567 CHECK(HexChar(valuestr[i * 2], &v)); |
| 568 buf[i] = v << 4; | 568 buf[i] = v << 4; |
| 569 CHECK(HexChar(valuestr[i * 2 + 1], &v)); | 569 CHECK(HexChar(valuestr[i * 2 + 1], &v)); |
| 570 buf[i] |= v; | 570 buf[i] |= v; |
| 571 } | 571 } |
| 572 | 572 |
| 573 msg.SetStringPiece( | 573 msg.SetStringPiece( |
| 574 tag, StringPiece(reinterpret_cast<char*>(buf.get()), len / 2)); | 574 tag, StringPiece(reinterpret_cast<char*>(buf.get()), len / 2)); |
| 575 continue; | 575 continue; |
| 576 } | 576 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 627 | 627 |
| 628 ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); | 628 ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); |
| 629 | 629 |
| 630 for (const CryptoHandshakeMessage& message : crypto_visitor.messages()) { | 630 for (const CryptoHandshakeMessage& message : crypto_visitor.messages()) { |
| 631 dest_stream->OnHandshakeMessage(message); | 631 dest_stream->OnHandshakeMessage(message); |
| 632 } | 632 } |
| 633 } | 633 } |
| 634 | 634 |
| 635 } // namespace test | 635 } // namespace test |
| 636 } // namespace net | 636 } // namespace net |
| OLD | NEW |