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/quic_packet_creator.h" | 5 #include "net/quic/quic_packet_creator.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
572 QuicPacketCreatorPeer::SetPacketNumberLength(&creator_, | 572 QuicPacketCreatorPeer::SetPacketNumberLength(&creator_, |
573 PACKET_2BYTE_PACKET_NUMBER); | 573 PACKET_2BYTE_PACKET_NUMBER); |
574 QuicStreamFrame* stream_frame = | 574 QuicStreamFrame* stream_frame = |
575 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, 0u, StringPiece()); | 575 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, 0u, StringPiece()); |
576 QuicFrames frames; | 576 QuicFrames frames; |
577 frames.push_back(QuicFrame(stream_frame)); | 577 frames.push_back(QuicFrame(stream_frame)); |
578 char buffer[kMaxPacketSize]; | 578 char buffer[kMaxPacketSize]; |
579 PendingRetransmission retransmission(CreateRetransmission( | 579 PendingRetransmission retransmission(CreateRetransmission( |
580 frames, true /* has_crypto_handshake */, true /* needs padding */, | 580 frames, true /* has_crypto_handshake */, true /* needs padding */, |
581 ENCRYPTION_NONE, PACKET_1BYTE_PACKET_NUMBER)); | 581 ENCRYPTION_NONE, PACKET_1BYTE_PACKET_NUMBER)); |
582 if (FLAGS_quic_retransmit_via_onserializedpacket) { | 582 EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
583 EXPECT_CALL(delegate_, OnSerializedPacket(_)) | 583 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
584 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); | 584 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); |
585 } | |
586 SerializedPacket serialized = | |
587 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); | |
588 if (FLAGS_quic_retransmit_via_onserializedpacket) { | |
589 serialized = serialized_packet_; | |
590 } | |
591 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, | 585 EXPECT_EQ(PACKET_4BYTE_PACKET_NUMBER, |
592 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_)); | 586 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_)); |
593 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, | 587 EXPECT_EQ(PACKET_2BYTE_PACKET_NUMBER, |
594 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); | 588 QuicPacketCreatorPeer::GetPacketNumberLength(&creator_)); |
595 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, serialized.packet_number_length); | 589 EXPECT_EQ(PACKET_1BYTE_PACKET_NUMBER, |
| 590 serialized_packet_.packet_number_length); |
596 | 591 |
597 { | 592 { |
598 InSequence s; | 593 InSequence s; |
599 EXPECT_CALL(framer_visitor_, OnPacket()); | 594 EXPECT_CALL(framer_visitor_, OnPacket()); |
600 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); | 595 EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
601 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); | 596 EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
602 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); | 597 EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
603 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); | 598 EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
604 EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); | 599 EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
605 EXPECT_CALL(framer_visitor_, OnPacketComplete()); | 600 EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
606 } | 601 } |
607 ProcessPacket(serialized); | 602 ProcessPacket(serialized_packet_); |
608 delete stream_frame; | 603 delete stream_frame; |
609 } | 604 } |
610 | 605 |
611 TEST_P(QuicPacketCreatorTest, ReserializeCryptoFrameWithForwardSecurity) { | 606 TEST_P(QuicPacketCreatorTest, ReserializeCryptoFrameWithForwardSecurity) { |
612 QuicStreamFrame* stream_frame = | 607 QuicStreamFrame* stream_frame = |
613 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, 0u, StringPiece()); | 608 new QuicStreamFrame(kCryptoStreamId, /*fin=*/false, 0u, StringPiece()); |
614 QuicFrames frames; | 609 QuicFrames frames; |
615 frames.push_back(QuicFrame(stream_frame)); | 610 frames.push_back(QuicFrame(stream_frame)); |
616 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); | 611 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
617 char buffer[kMaxPacketSize]; | 612 char buffer[kMaxPacketSize]; |
618 PendingRetransmission retransmission(CreateRetransmission( | 613 PendingRetransmission retransmission(CreateRetransmission( |
619 frames, true /* has_crypto_handshake */, true /* needs padding */, | 614 frames, true /* has_crypto_handshake */, true /* needs padding */, |
620 ENCRYPTION_NONE, | 615 ENCRYPTION_NONE, |
621 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_))); | 616 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_))); |
622 if (FLAGS_quic_retransmit_via_onserializedpacket) { | 617 EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
623 EXPECT_CALL(delegate_, OnSerializedPacket(_)) | 618 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
624 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); | 619 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); |
625 } | 620 EXPECT_EQ(ENCRYPTION_NONE, serialized_packet_.encryption_level); |
626 SerializedPacket serialized = | |
627 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); | |
628 if (FLAGS_quic_retransmit_via_onserializedpacket) { | |
629 serialized = serialized_packet_; | |
630 } | |
631 EXPECT_EQ(ENCRYPTION_NONE, serialized.encryption_level); | |
632 delete stream_frame; | 621 delete stream_frame; |
633 } | 622 } |
634 | 623 |
635 TEST_P(QuicPacketCreatorTest, ReserializeFrameWithForwardSecurity) { | 624 TEST_P(QuicPacketCreatorTest, ReserializeFrameWithForwardSecurity) { |
636 QuicStreamFrame* stream_frame = | 625 QuicStreamFrame* stream_frame = |
637 new QuicStreamFrame(0u, /*fin=*/false, 0u, StringPiece()); | 626 new QuicStreamFrame(0u, /*fin=*/false, 0u, StringPiece()); |
638 QuicFrames frames; | 627 QuicFrames frames; |
639 frames.push_back(QuicFrame(stream_frame)); | 628 frames.push_back(QuicFrame(stream_frame)); |
640 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); | 629 creator_.set_encryption_level(ENCRYPTION_FORWARD_SECURE); |
641 char buffer[kMaxPacketSize]; | 630 char buffer[kMaxPacketSize]; |
642 PendingRetransmission retransmission(CreateRetransmission( | 631 PendingRetransmission retransmission(CreateRetransmission( |
643 frames, false /* has_crypto_handshake */, false /* needs padding */, | 632 frames, false /* has_crypto_handshake */, false /* needs padding */, |
644 ENCRYPTION_NONE, | 633 ENCRYPTION_NONE, |
645 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_))); | 634 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_))); |
646 if (FLAGS_quic_retransmit_via_onserializedpacket) { | 635 EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
647 EXPECT_CALL(delegate_, OnSerializedPacket(_)) | 636 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
648 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); | 637 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); |
649 } | 638 EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, serialized_packet_.encryption_level); |
650 SerializedPacket serialized = | |
651 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); | |
652 if (FLAGS_quic_retransmit_via_onserializedpacket) { | |
653 serialized = serialized_packet_; | |
654 } | |
655 EXPECT_EQ(ENCRYPTION_FORWARD_SECURE, serialized.encryption_level); | |
656 delete stream_frame; | 639 delete stream_frame; |
657 } | 640 } |
658 | 641 |
659 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithPadding) { | 642 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithPadding) { |
660 QuicFrame frame; | 643 QuicFrame frame; |
661 QuicIOVector io_vector(MakeIOVector("fake handshake message data")); | 644 QuicIOVector io_vector(MakeIOVector("fake handshake message data")); |
662 QuicPacketCreatorPeer::CreateStreamFrame(&creator_, kCryptoStreamId, | 645 QuicPacketCreatorPeer::CreateStreamFrame(&creator_, kCryptoStreamId, |
663 io_vector, 0u, 0u, false, &frame); | 646 io_vector, 0u, 0u, false, &frame); |
664 QuicFrames frames; | 647 QuicFrames frames; |
665 frames.push_back(frame); | 648 frames.push_back(frame); |
666 char buffer[kMaxPacketSize]; | 649 char buffer[kMaxPacketSize]; |
667 PendingRetransmission retransmission(CreateRetransmission( | 650 PendingRetransmission retransmission(CreateRetransmission( |
668 frames, true /* has_crypto_handshake */, true /* needs padding */, | 651 frames, true /* has_crypto_handshake */, true /* needs padding */, |
669 ENCRYPTION_NONE, | 652 ENCRYPTION_NONE, |
670 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_))); | 653 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_))); |
671 if (FLAGS_quic_retransmit_via_onserializedpacket) { | 654 EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
672 EXPECT_CALL(delegate_, OnSerializedPacket(_)) | 655 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
673 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); | 656 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); |
674 } | 657 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length); |
675 SerializedPacket serialized = | |
676 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); | |
677 if (FLAGS_quic_retransmit_via_onserializedpacket) { | |
678 serialized = serialized_packet_; | |
679 } | |
680 EXPECT_EQ(kDefaultMaxPacketSize, serialized.encrypted_length); | |
681 delete frame.stream_frame; | 658 delete frame.stream_frame; |
682 } | 659 } |
683 | 660 |
684 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPacketAndPadding) { | 661 TEST_P(QuicPacketCreatorTest, ReserializeFramesWithFullPacketAndPadding) { |
685 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP) + | 662 const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP) + |
686 GetEncryptionOverhead() + | 663 GetEncryptionOverhead() + |
687 GetStreamFrameOverhead(NOT_IN_FEC_GROUP); | 664 GetStreamFrameOverhead(NOT_IN_FEC_GROUP); |
688 size_t capacity = kDefaultMaxPacketSize - overhead; | 665 size_t capacity = kDefaultMaxPacketSize - overhead; |
689 for (int delta = -5; delta <= 0; ++delta) { | 666 for (int delta = -5; delta <= 0; ++delta) { |
690 string data(capacity + delta, 'A'); | 667 string data(capacity + delta, 'A'); |
691 size_t bytes_free = 0 - delta; | 668 size_t bytes_free = 0 - delta; |
692 | 669 |
693 QuicFrame frame; | 670 QuicFrame frame; |
694 QuicIOVector io_vector(MakeIOVector(data)); | 671 QuicIOVector io_vector(MakeIOVector(data)); |
695 UniqueStreamBuffer stream_buffer; | 672 UniqueStreamBuffer stream_buffer; |
696 QuicPacketCreatorPeer::CreateStreamFrame( | 673 QuicPacketCreatorPeer::CreateStreamFrame( |
697 &creator_, kCryptoStreamId, io_vector, 0, kOffset, false, &frame); | 674 &creator_, kCryptoStreamId, io_vector, 0, kOffset, false, &frame); |
698 QuicFrames frames; | 675 QuicFrames frames; |
699 frames.push_back(frame); | 676 frames.push_back(frame); |
700 char buffer[kMaxPacketSize]; | 677 char buffer[kMaxPacketSize]; |
701 PendingRetransmission retransmission(CreateRetransmission( | 678 PendingRetransmission retransmission(CreateRetransmission( |
702 frames, true /* has_crypto_handshake */, true /* needs padding */, | 679 frames, true /* has_crypto_handshake */, true /* needs padding */, |
703 ENCRYPTION_NONE, | 680 ENCRYPTION_NONE, |
704 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_))); | 681 QuicPacketCreatorPeer::NextPacketNumberLength(&creator_))); |
705 if (FLAGS_quic_retransmit_via_onserializedpacket) { | 682 EXPECT_CALL(delegate_, OnSerializedPacket(_)) |
706 EXPECT_CALL(delegate_, OnSerializedPacket(_)) | 683 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); |
707 .WillOnce(Invoke(this, &QuicPacketCreatorTest::SaveSerializedPacket)); | 684 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); |
708 } | |
709 SerializedPacket serialized = | |
710 creator_.ReserializeAllFrames(retransmission, buffer, kMaxPacketSize); | |
711 if (FLAGS_quic_retransmit_via_onserializedpacket) { | |
712 serialized = serialized_packet_; | |
713 } | |
714 | 685 |
715 // If there is not enough space in the packet to fit a padding frame | 686 // If there is not enough space in the packet to fit a padding frame |
716 // (1 byte) and to expand the stream frame (another 2 bytes) the packet | 687 // (1 byte) and to expand the stream frame (another 2 bytes) the packet |
717 // will not be padded. | 688 // will not be padded. |
718 if (bytes_free < 3) { | 689 if (bytes_free < 3) { |
719 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free, | 690 EXPECT_EQ(kDefaultMaxPacketSize - bytes_free, |
720 serialized.encrypted_length); | 691 serialized_packet_.encrypted_length); |
721 } else { | 692 } else { |
722 EXPECT_EQ(kDefaultMaxPacketSize, serialized.encrypted_length); | 693 EXPECT_EQ(kDefaultMaxPacketSize, serialized_packet_.encrypted_length); |
723 } | 694 } |
724 | 695 |
725 delete frame.stream_frame; | 696 delete frame.stream_frame; |
726 frames_.clear(); | 697 frames_.clear(); |
727 } | 698 } |
728 } | 699 } |
729 | 700 |
730 TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) { | 701 TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) { |
731 QuicConnectionCloseFrame frame; | 702 QuicConnectionCloseFrame frame; |
732 frame.error_code = QUIC_NO_ERROR; | 703 frame.error_code = QUIC_NO_ERROR; |
(...skipping 964 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1697 EXPECT_CALL(delegate_, OnUnrecoverableError(QUIC_UNENCRYPTED_FEC_DATA, _)); | 1668 EXPECT_CALL(delegate_, OnUnrecoverableError(QUIC_UNENCRYPTED_FEC_DATA, _)); |
1698 char seralized_fec_buffer[kMaxPacketSize]; | 1669 char seralized_fec_buffer[kMaxPacketSize]; |
1699 EXPECT_DFATAL(QuicPacketCreatorPeer::SerializeFec( | 1670 EXPECT_DFATAL(QuicPacketCreatorPeer::SerializeFec( |
1700 &creator_, seralized_fec_buffer, kMaxPacketSize), | 1671 &creator_, seralized_fec_buffer, kMaxPacketSize), |
1701 "SerializeFEC must be called with encryption."); | 1672 "SerializeFEC must be called with encryption."); |
1702 } | 1673 } |
1703 | 1674 |
1704 } // namespace | 1675 } // namespace |
1705 } // namespace test | 1676 } // namespace test |
1706 } // namespace net | 1677 } // namespace net |
OLD | NEW |