[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/quic/quic_packet_creator.h" |
| 6 | |
| 7 | #include "base/stl_util.h" |
| 8 | #include "net/quic/crypto/null_encrypter.h" |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 9 | #include "net/quic/crypto/quic_decrypter.h" |
| 10 | #include "net/quic/crypto/quic_encrypter.h" |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 11 | #include "net/quic/quic_utils.h" |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 12 | #include "net/quic/test_tools/mock_random.h" |
[email protected] | bbb1007 | 2014-06-13 07:41:59 | [diff] [blame] | 13 | #include "net/quic/test_tools/quic_framer_peer.h" |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 14 | #include "net/quic/test_tools/quic_packet_creator_peer.h" |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 15 | #include "net/quic/test_tools/quic_test_utils.h" |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 16 | #include "net/test/gtest_util.h" |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 17 | #include "testing/gmock/include/gmock/gmock.h" |
| 18 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 19 | using base::StringPiece; |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 20 | using std::ostream; |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 21 | using std::string; |
| 22 | using std::vector; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 23 | using testing::DoAll; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 24 | using testing::InSequence; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 25 | using testing::Return; |
| 26 | using testing::SaveArg; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 27 | using testing::_; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 28 | |
| 29 | namespace net { |
| 30 | namespace test { |
| 31 | namespace { |
| 32 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 33 | // Run tests with combinations of {QuicVersion, ToggleVersionSerialization}. |
| 34 | struct TestParams { |
| 35 | TestParams(QuicVersion version, |
| 36 | bool version_serialization) |
| 37 | : version(version), |
| 38 | version_serialization(version_serialization) { |
| 39 | } |
| 40 | |
| 41 | friend ostream& operator<<(ostream& os, const TestParams& p) { |
| 42 | os << "{ client_version: " << QuicVersionToString(p.version) |
| 43 | << " include version: " << p.version_serialization << " }"; |
| 44 | return os; |
| 45 | } |
| 46 | |
| 47 | QuicVersion version; |
| 48 | bool version_serialization; |
| 49 | }; |
| 50 | |
| 51 | // Constructs various test permutations. |
| 52 | vector<TestParams> GetTestParams() { |
| 53 | vector<TestParams> params; |
| 54 | QuicVersionVector all_supported_versions = QuicSupportedVersions(); |
| 55 | for (size_t i = 0; i < all_supported_versions.size(); ++i) { |
| 56 | params.push_back(TestParams(all_supported_versions[i], true)); |
| 57 | params.push_back(TestParams(all_supported_versions[i], false)); |
| 58 | } |
| 59 | return params; |
| 60 | } |
| 61 | |
| 62 | class QuicPacketCreatorTest : public ::testing::TestWithParam<TestParams> { |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 63 | protected: |
| 64 | QuicPacketCreatorTest() |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 65 | : server_framer_(SupportedVersions(GetParam().version), QuicTime::Zero(), |
| 66 | true), |
| 67 | client_framer_(SupportedVersions(GetParam().version), QuicTime::Zero(), |
| 68 | false), |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 69 | sequence_number_(0), |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 70 | connection_id_(2), |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 71 | data_("foo"), |
[email protected] | bbb1007 | 2014-06-13 07:41:59 | [diff] [blame] | 72 | creator_(connection_id_, &client_framer_, &mock_random_) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 73 | client_framer_.set_visitor(&framer_visitor_); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 74 | client_framer_.set_received_entropy_calculator(&entropy_calculator_); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 75 | server_framer_.set_visitor(&framer_visitor_); |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 76 | } |
[email protected] | d8bbef6 | 2014-06-12 08:02:50 | [diff] [blame] | 77 | |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 78 | virtual ~QuicPacketCreatorTest() override { |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 81 | void ProcessPacket(QuicPacket* packet) { |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 82 | scoped_ptr<QuicEncryptedPacket> encrypted( |
[email protected] | 8ba8121 | 2013-05-03 13:11:48 | [diff] [blame] | 83 | server_framer_.EncryptPacket(ENCRYPTION_NONE, sequence_number_, |
| 84 | *packet)); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 85 | server_framer_.ProcessPacket(*encrypted); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 86 | } |
| 87 | |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 88 | void CheckStreamFrame(const QuicFrame& frame, |
| 89 | QuicStreamId stream_id, |
| 90 | const string& data, |
| 91 | QuicStreamOffset offset, |
| 92 | bool fin) { |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 93 | EXPECT_EQ(STREAM_FRAME, frame.type); |
| 94 | ASSERT_TRUE(frame.stream_frame); |
| 95 | EXPECT_EQ(stream_id, frame.stream_frame->stream_id); |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 96 | scoped_ptr<string> frame_data(frame.stream_frame->GetDataAsString()); |
| 97 | EXPECT_EQ(data, *frame_data); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 98 | EXPECT_EQ(offset, frame.stream_frame->offset); |
| 99 | EXPECT_EQ(fin, frame.stream_frame->fin); |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 102 | // Returns the number of bytes consumed by the header of packet, including |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 103 | // the version. |
| 104 | size_t GetPacketHeaderOverhead(InFecGroup is_in_fec_group) { |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 105 | return GetPacketHeaderSize(creator_.connection_id_length(), |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 106 | kIncludeVersion, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 107 | creator_.next_sequence_number_length(), |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 108 | is_in_fec_group); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Returns the number of bytes of overhead that will be added to a packet |
| 112 | // of maximum length. |
| 113 | size_t GetEncryptionOverhead() { |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 114 | return creator_.max_packet_length() - client_framer_.GetMaxPlaintextSize( |
| 115 | creator_.max_packet_length()); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Returns the number of bytes consumed by the non-data fields of a stream |
| 119 | // frame, assuming it is the last frame in the packet |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 120 | size_t GetStreamFrameOverhead(InFecGroup is_in_fec_group) { |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 121 | return QuicFramer::GetMinStreamFrameSize(kClientDataStreamId1, kOffset, |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 122 | true, is_in_fec_group); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 123 | } |
[email protected] | d8bbef6 | 2014-06-12 08:02:50 | [diff] [blame] | 124 | |
| 125 | // Enables and turns on FEC protection. Returns true if FEC protection is on. |
| 126 | bool SwitchFecProtectionOn(size_t max_packets_per_fec_group) { |
| 127 | creator_.set_max_packets_per_fec_group(max_packets_per_fec_group); |
| 128 | creator_.StartFecProtectingPackets(); |
| 129 | return creator_.IsFecProtected(); |
| 130 | } |
| 131 | |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 132 | static const QuicStreamOffset kOffset = 1u; |
| 133 | |
[email protected] | 701bc89 | 2013-01-17 04:51:54 | [diff] [blame] | 134 | QuicFrames frames_; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 135 | QuicFramer server_framer_; |
| 136 | QuicFramer client_framer_; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 137 | testing::StrictMock<MockFramerVisitor> framer_visitor_; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 138 | QuicPacketSequenceNumber sequence_number_; |
[email protected] | 3aa9ca7 | 2014-02-27 19:39:43 | [diff] [blame] | 139 | QuicConnectionId connection_id_; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 140 | string data_; |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 141 | MockRandom mock_random_; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 142 | QuicPacketCreator creator_; |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 143 | MockEntropyCalculator entropy_calculator_; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 144 | }; |
| 145 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 146 | // Run all packet creator tests with all supported versions of QUIC, and with |
| 147 | // and without version in the packet header. |
| 148 | INSTANTIATE_TEST_CASE_P(QuicPacketCreatorTests, |
| 149 | QuicPacketCreatorTest, |
| 150 | ::testing::ValuesIn(GetTestParams())); |
| 151 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 152 | TEST_P(QuicPacketCreatorTest, SerializeFrames) { |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 153 | frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u)))); |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 154 | frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector()))); |
| 155 | frames_.push_back(QuicFrame(new QuicStreamFrame(0u, true, 0u, IOVector()))); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 156 | SerializedPacket serialized = creator_.SerializeAllFrames(frames_); |
| 157 | delete frames_[0].ack_frame; |
| 158 | delete frames_[1].stream_frame; |
| 159 | delete frames_[2].stream_frame; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 160 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 161 | { |
| 162 | InSequence s; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 163 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 164 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 165 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 166 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 167 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 168 | EXPECT_CALL(framer_visitor_, OnAckFrame(_)); |
| 169 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 170 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 171 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 172 | } |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 173 | ProcessPacket(serialized.packet); |
| 174 | delete serialized.packet; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 177 | TEST_P(QuicPacketCreatorTest, SerializeWithFEC) { |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 178 | // Enable FEC protection, and send FEC packet every 6 packets. |
[email protected] | d8bbef6 | 2014-06-12 08:02:50 | [diff] [blame] | 179 | EXPECT_TRUE(SwitchFecProtectionOn(6)); |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 180 | // Should return false since we do not have enough packets in the FEC group to |
| 181 | // trigger an FEC packet. |
| 182 | ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false)); |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 183 | |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 184 | frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector()))); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 185 | SerializedPacket serialized = creator_.SerializeAllFrames(frames_); |
| 186 | delete frames_[0].stream_frame; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 187 | |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 188 | { |
| 189 | InSequence s; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 190 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 191 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 192 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 193 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 194 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
[email protected] | 08da9adb | 2014-04-24 08:33:31 | [diff] [blame] | 195 | EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_)); |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 196 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 197 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 198 | } |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 199 | ProcessPacket(serialized.packet); |
| 200 | delete serialized.packet; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 201 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 202 | // Should return false since we do not have enough packets in the FEC group to |
| 203 | // trigger an FEC packet. |
| 204 | ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false)); |
[email protected] | 08da9adb | 2014-04-24 08:33:31 | [diff] [blame] | 205 | // Should return true since there are packets in the FEC group. |
| 206 | ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true)); |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 207 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 208 | serialized = creator_.SerializeFec(); |
| 209 | ASSERT_EQ(2u, serialized.sequence_number); |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 210 | { |
| 211 | InSequence s; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 212 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 213 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 214 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 215 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | c995c57 | 2013-01-18 05:43:20 | [diff] [blame] | 216 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 217 | EXPECT_CALL(framer_visitor_, OnFecData(_)); |
| 218 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 219 | } |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 220 | ProcessPacket(serialized.packet); |
| 221 | delete serialized.packet; |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 222 | } |
| 223 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 224 | TEST_P(QuicPacketCreatorTest, SerializeChangingSequenceNumberLength) { |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 225 | frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u)))); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 226 | creator_.AddSavedFrame(frames_[0]); |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 227 | creator_.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 228 | SerializedPacket serialized = creator_.SerializePacket(); |
| 229 | // The sequence number length will not change mid-packet. |
| 230 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
| 231 | |
| 232 | { |
| 233 | InSequence s; |
| 234 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 235 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 236 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 237 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 238 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 239 | EXPECT_CALL(framer_visitor_, OnAckFrame(_)); |
| 240 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 241 | } |
| 242 | ProcessPacket(serialized.packet); |
| 243 | delete serialized.packet; |
| 244 | |
| 245 | creator_.AddSavedFrame(frames_[0]); |
| 246 | serialized = creator_.SerializePacket(); |
| 247 | // Now the actual sequence number length should have changed. |
| 248 | EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
| 249 | delete frames_[0].ack_frame; |
| 250 | |
| 251 | { |
| 252 | InSequence s; |
| 253 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 254 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 255 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 256 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 257 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 258 | EXPECT_CALL(framer_visitor_, OnAckFrame(_)); |
| 259 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 260 | } |
| 261 | ProcessPacket(serialized.packet); |
| 262 | delete serialized.packet; |
| 263 | } |
| 264 | |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 265 | TEST_P(QuicPacketCreatorTest, ChangeSequenceNumberLengthMidPacket) { |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 266 | // Changing the sequence number length with queued frames in the creator |
| 267 | // should hold the change until after any currently queued frames are |
| 268 | // serialized. |
| 269 | |
| 270 | // Packet 1. |
| 271 | // Queue a frame in the creator. |
| 272 | EXPECT_FALSE(creator_.HasPendingFrames()); |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 273 | QuicFrame ack_frame = QuicFrame(new QuicAckFrame(MakeAckFrame(0u))); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 274 | creator_.AddSavedFrame(ack_frame); |
| 275 | |
| 276 | // Now change sequence number length. |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 277 | creator_.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 278 | |
| 279 | // Add a STOP_WAITING frame since it contains a packet sequence number, |
| 280 | // whose length should be 1. |
| 281 | QuicStopWaitingFrame stop_waiting_frame; |
| 282 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame))); |
| 283 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 284 | |
| 285 | // Ensure the packet is successfully created. |
| 286 | SerializedPacket serialized = creator_.SerializePacket(); |
| 287 | ASSERT_TRUE(serialized.packet); |
| 288 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
| 289 | |
| 290 | // Verify that header in transmitted packet has 1 byte sequence length. |
| 291 | QuicPacketHeader header; |
| 292 | { |
| 293 | InSequence s; |
| 294 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 295 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 296 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 297 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 298 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce( |
| 299 | DoAll(SaveArg<0>(&header), Return(true))); |
| 300 | EXPECT_CALL(framer_visitor_, OnAckFrame(_)); |
| 301 | EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_)); |
| 302 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 303 | } |
| 304 | ProcessPacket(serialized.packet); |
| 305 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, |
| 306 | header.public_header.sequence_number_length); |
| 307 | delete serialized.packet; |
| 308 | |
| 309 | // Packet 2. |
| 310 | EXPECT_FALSE(creator_.HasPendingFrames()); |
| 311 | // Generate Packet 2 with one frame -- sequence number length should now |
| 312 | // change to 4 bytes. |
| 313 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&stop_waiting_frame))); |
| 314 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 315 | |
| 316 | // Ensure the packet is successfully created. |
| 317 | serialized = creator_.SerializePacket(); |
| 318 | ASSERT_TRUE(serialized.packet); |
| 319 | EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
| 320 | |
| 321 | // Verify that header in transmitted packet has 4 byte sequence length. |
| 322 | { |
| 323 | InSequence s; |
| 324 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 325 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 326 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
| 327 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
| 328 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce( |
| 329 | DoAll(SaveArg<0>(&header), Return(true))); |
| 330 | EXPECT_CALL(framer_visitor_, OnStopWaitingFrame(_)); |
| 331 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 332 | } |
| 333 | ProcessPacket(serialized.packet); |
| 334 | EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, |
| 335 | header.public_header.sequence_number_length); |
| 336 | |
| 337 | delete serialized.packet; |
| 338 | delete ack_frame.ack_frame; |
| 339 | } |
| 340 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 341 | TEST_P(QuicPacketCreatorTest, SerializeWithFECChangingSequenceNumberLength) { |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 342 | // Test goal is to test the following sequence (P1 => generate Packet 1): |
| 343 | // P1 <change seq num length> P2 FEC, |
| 344 | // and we expect that sequence number length should not change until the end |
| 345 | // of the open FEC group. |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 346 | |
| 347 | // Enable FEC protection, and send FEC packet every 6 packets. |
[email protected] | d8bbef6 | 2014-06-12 08:02:50 | [diff] [blame] | 348 | EXPECT_TRUE(SwitchFecProtectionOn(6)); |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 349 | // Should return false since we do not have enough packets in the FEC group to |
| 350 | // trigger an FEC packet. |
| 351 | ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false)); |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 352 | frames_.push_back(QuicFrame(new QuicAckFrame(MakeAckFrame(0u)))); |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 353 | |
| 354 | // Generate Packet 1. |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 355 | creator_.AddSavedFrame(frames_[0]); |
| 356 | // Change the sequence number length mid-FEC group and it should not change. |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 357 | creator_.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 358 | SerializedPacket serialized = creator_.SerializePacket(); |
| 359 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
| 360 | |
| 361 | { |
| 362 | InSequence s; |
| 363 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 364 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 365 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 366 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 367 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 368 | EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_)); |
| 369 | EXPECT_CALL(framer_visitor_, OnAckFrame(_)); |
| 370 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 371 | } |
| 372 | ProcessPacket(serialized.packet); |
| 373 | delete serialized.packet; |
| 374 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 375 | // Generate Packet 2. |
| 376 | creator_.AddSavedFrame(frames_[0]); |
| 377 | serialized = creator_.SerializePacket(); |
| 378 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 379 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 380 | { |
| 381 | InSequence s; |
| 382 | EXPECT_CALL(framer_visitor_, OnPacket()); |
| 383 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
| 384 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 385 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 386 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 387 | EXPECT_CALL(framer_visitor_, OnFecProtectedPayload(_)); |
| 388 | EXPECT_CALL(framer_visitor_, OnAckFrame(_)); |
| 389 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 390 | } |
| 391 | ProcessPacket(serialized.packet); |
| 392 | delete serialized.packet; |
| 393 | |
| 394 | // Should return false since we do not have enough packets in the FEC group to |
| 395 | // trigger an FEC packet. |
| 396 | ASSERT_FALSE(creator_.ShouldSendFec(/*force_close=*/false)); |
| 397 | // Should return true since there are packets in the FEC group. |
| 398 | ASSERT_TRUE(creator_.ShouldSendFec(/*force_close=*/true)); |
| 399 | |
| 400 | // Force generation of FEC packet. |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 401 | serialized = creator_.SerializeFec(); |
| 402 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 403 | ASSERT_EQ(3u, serialized.sequence_number); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 404 | |
| 405 | { |
| 406 | InSequence s; |
| 407 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 408 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 409 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 410 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 411 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 412 | EXPECT_CALL(framer_visitor_, OnFecData(_)); |
| 413 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 414 | } |
| 415 | ProcessPacket(serialized.packet); |
| 416 | delete serialized.packet; |
| 417 | |
| 418 | // Ensure the next FEC group starts using the new sequence number length. |
| 419 | serialized = creator_.SerializeAllFrames(frames_); |
| 420 | EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 421 | delete frames_[0].ack_frame; |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 422 | delete serialized.packet; |
| 423 | } |
| 424 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 425 | TEST_P(QuicPacketCreatorTest, ReserializeFramesWithSequenceNumberLength) { |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 426 | // If the original packet sequence number length, the current sequence number |
| 427 | // length, and the configured send sequence number length are different, the |
| 428 | // retransmit must sent with the original length and the others do not change. |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 429 | creator_.set_next_sequence_number_length(PACKET_4BYTE_SEQUENCE_NUMBER); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 430 | QuicPacketCreatorPeer::SetSequenceNumberLength(&creator_, |
| 431 | PACKET_2BYTE_SEQUENCE_NUMBER); |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 432 | frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector()))); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 433 | SerializedPacket serialized = |
| 434 | creator_.ReserializeAllFrames(frames_, PACKET_1BYTE_SEQUENCE_NUMBER); |
| 435 | EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 436 | creator_.next_sequence_number_length()); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 437 | EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER, |
| 438 | QuicPacketCreatorPeer::GetSequenceNumberLength(&creator_)); |
| 439 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, serialized.sequence_number_length); |
| 440 | delete frames_[0].stream_frame; |
| 441 | |
| 442 | { |
| 443 | InSequence s; |
| 444 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 445 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 446 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 447 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 448 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 449 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 450 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 451 | } |
| 452 | ProcessPacket(serialized.packet); |
| 453 | delete serialized.packet; |
| 454 | } |
| 455 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 456 | TEST_P(QuicPacketCreatorTest, SerializeConnectionClose) { |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 457 | QuicConnectionCloseFrame frame; |
| 458 | frame.error_code = QUIC_NO_ERROR; |
| 459 | frame.error_details = "error"; |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 460 | |
| 461 | SerializedPacket serialized = creator_.SerializeConnectionClose(&frame); |
| 462 | ASSERT_EQ(1u, serialized.sequence_number); |
| 463 | ASSERT_EQ(1u, creator_.sequence_number()); |
| 464 | |
| 465 | InSequence s; |
| 466 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 467 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 468 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 469 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 470 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)); |
| 471 | EXPECT_CALL(framer_visitor_, OnConnectionCloseFrame(_)); |
| 472 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 473 | |
| 474 | ProcessPacket(serialized.packet); |
| 475 | delete serialized.packet; |
| 476 | } |
| 477 | |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 478 | TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithNoGroup) { |
| 479 | // Enable FEC protection. |
| 480 | creator_.set_max_packets_per_fec_group(6); |
| 481 | EXPECT_TRUE(creator_.IsFecEnabled()); |
| 482 | EXPECT_FALSE(creator_.IsFecProtected()); |
| 483 | |
| 484 | // Turn on FEC protection. |
| 485 | creator_.StartFecProtectingPackets(); |
| 486 | EXPECT_TRUE(creator_.IsFecProtected()); |
| 487 | // We have no packets in the FEC group, so no FEC packet can be created. |
| 488 | EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/true)); |
| 489 | // Since no packets are in FEC group yet, we should be able to turn FEC |
| 490 | // off with no trouble. |
| 491 | creator_.StopFecProtectingPackets(); |
| 492 | EXPECT_FALSE(creator_.IsFecProtected()); |
| 493 | } |
| 494 | |
| 495 | TEST_P(QuicPacketCreatorTest, SwitchFecOnOffWithGroupInProgress) { |
| 496 | // Enable FEC protection, and send FEC packet every 6 packets. |
[email protected] | d8bbef6 | 2014-06-12 08:02:50 | [diff] [blame] | 497 | EXPECT_TRUE(SwitchFecProtectionOn(6)); |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 498 | frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector()))); |
| 499 | SerializedPacket serialized = creator_.SerializeAllFrames(frames_); |
| 500 | delete frames_[0].stream_frame; |
| 501 | delete serialized.packet; |
| 502 | |
| 503 | EXPECT_TRUE(creator_.IsFecProtected()); |
| 504 | // We do not have enough packets in the FEC group to trigger an FEC packet. |
| 505 | EXPECT_FALSE(creator_.ShouldSendFec(/*force_close=*/false)); |
| 506 | // Should return true since there are packets in the FEC group. |
| 507 | EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true)); |
| 508 | |
| 509 | // Switching FEC off should not change creator state, since there is an |
| 510 | // FEC packet under construction. |
| 511 | EXPECT_DFATAL(creator_.StopFecProtectingPackets(), |
| 512 | "Cannot stop FEC protection with open FEC group."); |
| 513 | EXPECT_TRUE(creator_.IsFecProtected()); |
| 514 | // Confirm that FEC packet is still under construction. |
| 515 | EXPECT_TRUE(creator_.ShouldSendFec(/*force_close=*/true)); |
| 516 | |
| 517 | serialized = creator_.SerializeFec(); |
| 518 | delete serialized.packet; |
| 519 | |
| 520 | // Switching FEC on/off should work now. |
| 521 | creator_.StopFecProtectingPackets(); |
| 522 | EXPECT_FALSE(creator_.IsFecProtected()); |
| 523 | creator_.StartFecProtectingPackets(); |
| 524 | EXPECT_TRUE(creator_.IsFecProtected()); |
| 525 | } |
| 526 | |
| 527 | TEST_P(QuicPacketCreatorTest, SwitchFecOnWithStreamFrameQueued) { |
| 528 | // Add a stream frame to the creator. |
| 529 | QuicFrame frame; |
| 530 | size_t consumed = creator_.CreateStreamFrame( |
| 531 | 1u, MakeIOVector("test"), 0u, false, &frame); |
| 532 | EXPECT_EQ(4u, consumed); |
| 533 | ASSERT_TRUE(frame.stream_frame); |
| 534 | EXPECT_TRUE(creator_.AddSavedFrame(frame)); |
| 535 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 536 | |
[email protected] | cc1aa27 | 2014-06-30 19:48:22 | [diff] [blame] | 537 | // Enable FEC protection, and send FEC packet every 6 packets. |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 538 | creator_.set_max_packets_per_fec_group(6); |
| 539 | EXPECT_TRUE(creator_.IsFecEnabled()); |
| 540 | EXPECT_DFATAL(creator_.StartFecProtectingPackets(), |
| 541 | "Cannot start FEC protection with pending frames."); |
| 542 | EXPECT_FALSE(creator_.IsFecProtected()); |
| 543 | |
| 544 | // Serialize packet for transmission. |
| 545 | SerializedPacket serialized = creator_.SerializePacket(); |
| 546 | delete serialized.packet; |
| 547 | delete serialized.retransmittable_frames; |
| 548 | EXPECT_FALSE(creator_.HasPendingFrames()); |
| 549 | |
| 550 | // Since all pending frames have been serialized, turning FEC on should work. |
| 551 | creator_.StartFecProtectingPackets(); |
| 552 | EXPECT_TRUE(creator_.IsFecProtected()); |
| 553 | } |
| 554 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 555 | TEST_P(QuicPacketCreatorTest, CreateStreamFrame) { |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 556 | QuicFrame frame; |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 557 | size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 0u, |
| 558 | false, &frame); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 559 | EXPECT_EQ(4u, consumed); |
| 560 | CheckStreamFrame(frame, 1u, "test", 0u, false); |
| 561 | delete frame.stream_frame; |
| 562 | } |
| 563 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 564 | TEST_P(QuicPacketCreatorTest, CreateStreamFrameFin) { |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 565 | QuicFrame frame; |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 566 | size_t consumed = creator_.CreateStreamFrame(1u, MakeIOVector("test"), 10u, |
| 567 | true, &frame); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 568 | EXPECT_EQ(4u, consumed); |
| 569 | CheckStreamFrame(frame, 1u, "test", 10u, true); |
| 570 | delete frame.stream_frame; |
| 571 | } |
| 572 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 573 | TEST_P(QuicPacketCreatorTest, CreateStreamFrameFinOnly) { |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 574 | QuicFrame frame; |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 575 | size_t consumed = creator_.CreateStreamFrame(1u, IOVector(), 0u, true, |
| 576 | &frame); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 577 | EXPECT_EQ(0u, consumed); |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 578 | CheckStreamFrame(frame, 1u, string(), 0u, true); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 579 | delete frame.stream_frame; |
| 580 | } |
| 581 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 582 | TEST_P(QuicPacketCreatorTest, CreateAllFreeBytesForStreamFrames) { |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 583 | const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP) |
| 584 | + GetEncryptionOverhead(); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 585 | for (size_t i = overhead; i < overhead + 100; ++i) { |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 586 | creator_.set_max_packet_length(i); |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 587 | const bool should_have_room = i > overhead + GetStreamFrameOverhead( |
| 588 | NOT_IN_FEC_GROUP); |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 589 | ASSERT_EQ(should_have_room, creator_.HasRoomForStreamFrame( |
| 590 | kClientDataStreamId1, kOffset)); |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 591 | if (should_have_room) { |
| 592 | QuicFrame frame; |
| 593 | size_t bytes_consumed = creator_.CreateStreamFrame( |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 594 | kClientDataStreamId1, MakeIOVector("testdata"), kOffset, false, |
| 595 | &frame); |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 596 | EXPECT_LT(0u, bytes_consumed); |
| 597 | ASSERT_TRUE(creator_.AddSavedFrame(frame)); |
| 598 | SerializedPacket serialized_packet = creator_.SerializePacket(); |
| 599 | ASSERT_TRUE(serialized_packet.packet); |
| 600 | delete serialized_packet.packet; |
| 601 | delete serialized_packet.retransmittable_frames; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 606 | TEST_P(QuicPacketCreatorTest, StreamFrameConsumption) { |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 607 | // Compute the total overhead for a single frame in packet. |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 608 | const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP) |
| 609 | + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP); |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 610 | size_t capacity = kDefaultMaxPacketSize - overhead; |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 611 | // Now, test various sizes around this size. |
| 612 | for (int delta = -5; delta <= 5; ++delta) { |
| 613 | string data(capacity + delta, 'A'); |
| 614 | size_t bytes_free = delta > 0 ? 0 : 0 - delta; |
| 615 | QuicFrame frame; |
| 616 | size_t bytes_consumed = creator_.CreateStreamFrame( |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 617 | kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 618 | EXPECT_EQ(capacity - bytes_free, bytes_consumed); |
| 619 | |
| 620 | ASSERT_TRUE(creator_.AddSavedFrame(frame)); |
| 621 | // BytesFree() returns bytes available for the next frame, which will |
| 622 | // be two bytes smaller since the stream frame would need to be grown. |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 623 | EXPECT_EQ(2u, creator_.ExpansionOnNewFrame()); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 624 | size_t expected_bytes_free = bytes_free < 3 ? 0 : bytes_free - 2; |
| 625 | EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta; |
| 626 | SerializedPacket serialized_packet = creator_.SerializePacket(); |
| 627 | ASSERT_TRUE(serialized_packet.packet); |
| 628 | delete serialized_packet.packet; |
| 629 | delete serialized_packet.retransmittable_frames; |
| 630 | } |
| 631 | } |
| 632 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 633 | TEST_P(QuicPacketCreatorTest, StreamFrameConsumptionWithFec) { |
[email protected] | ce7bb141 | 2014-05-17 15:51:33 | [diff] [blame] | 634 | // Enable FEC protection, and send FEC packet every 6 packets. |
[email protected] | d8bbef6 | 2014-06-12 08:02:50 | [diff] [blame] | 635 | EXPECT_TRUE(SwitchFecProtectionOn(6)); |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 636 | // Compute the total overhead for a single frame in packet. |
| 637 | const size_t overhead = GetPacketHeaderOverhead(IN_FEC_GROUP) |
| 638 | + GetEncryptionOverhead() + GetStreamFrameOverhead(IN_FEC_GROUP); |
| 639 | size_t capacity = kDefaultMaxPacketSize - overhead; |
| 640 | // Now, test various sizes around this size. |
| 641 | for (int delta = -5; delta <= 5; ++delta) { |
| 642 | string data(capacity + delta, 'A'); |
| 643 | size_t bytes_free = delta > 0 ? 0 : 0 - delta; |
| 644 | QuicFrame frame; |
| 645 | size_t bytes_consumed = creator_.CreateStreamFrame( |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 646 | kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame); |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 647 | EXPECT_EQ(capacity - bytes_free, bytes_consumed); |
| 648 | |
| 649 | ASSERT_TRUE(creator_.AddSavedFrame(frame)); |
| 650 | // BytesFree() returns bytes available for the next frame. Since stream |
| 651 | // frame does not grow for FEC protected packets, this should be the same |
| 652 | // as bytes_free (bound by 0). |
| 653 | EXPECT_EQ(0u, creator_.ExpansionOnNewFrame()); |
| 654 | size_t expected_bytes_free = bytes_free > 0 ? bytes_free : 0; |
| 655 | EXPECT_EQ(expected_bytes_free, creator_.BytesFree()) << "delta: " << delta; |
| 656 | SerializedPacket serialized_packet = creator_.SerializePacket(); |
| 657 | ASSERT_TRUE(serialized_packet.packet); |
| 658 | delete serialized_packet.packet; |
| 659 | delete serialized_packet.retransmittable_frames; |
| 660 | } |
| 661 | } |
| 662 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 663 | TEST_P(QuicPacketCreatorTest, CryptoStreamFramePacketPadding) { |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 664 | // Compute the total overhead for a single frame in packet. |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 665 | const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP) |
| 666 | + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 667 | ASSERT_GT(kMaxPacketSize, overhead); |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 668 | size_t capacity = kDefaultMaxPacketSize - overhead; |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 669 | // Now, test various sizes around this size. |
| 670 | for (int delta = -5; delta <= 5; ++delta) { |
| 671 | string data(capacity + delta, 'A'); |
| 672 | size_t bytes_free = delta > 0 ? 0 : 0 - delta; |
| 673 | |
| 674 | QuicFrame frame; |
| 675 | size_t bytes_consumed = creator_.CreateStreamFrame( |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 676 | kCryptoStreamId, MakeIOVector(data), kOffset, false, &frame); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 677 | EXPECT_LT(0u, bytes_consumed); |
| 678 | ASSERT_TRUE(creator_.AddSavedFrame(frame)); |
| 679 | SerializedPacket serialized_packet = creator_.SerializePacket(); |
| 680 | ASSERT_TRUE(serialized_packet.packet); |
| 681 | // If there is not enough space in the packet to fit a padding frame |
| 682 | // (1 byte) and to expand the stream frame (another 2 bytes) the packet |
| 683 | // will not be padded. |
| 684 | if (bytes_free < 3) { |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 685 | EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize) |
| 686 | - bytes_free, serialized_packet.packet->length()); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 687 | } else { |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 688 | EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize), |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 689 | serialized_packet.packet->length()); |
| 690 | } |
| 691 | delete serialized_packet.packet; |
| 692 | delete serialized_packet.retransmittable_frames; |
| 693 | } |
| 694 | } |
| 695 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 696 | TEST_P(QuicPacketCreatorTest, NonCryptoStreamFramePacketNonPadding) { |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 697 | // Compute the total overhead for a single frame in packet. |
[email protected] | 98a9d125 | 2014-04-04 00:43:59 | [diff] [blame] | 698 | const size_t overhead = GetPacketHeaderOverhead(NOT_IN_FEC_GROUP) |
| 699 | + GetEncryptionOverhead() + GetStreamFrameOverhead(NOT_IN_FEC_GROUP); |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 700 | ASSERT_GT(kDefaultMaxPacketSize, overhead); |
| 701 | size_t capacity = kDefaultMaxPacketSize - overhead; |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 702 | // Now, test various sizes around this size. |
| 703 | for (int delta = -5; delta <= 5; ++delta) { |
| 704 | string data(capacity + delta, 'A'); |
| 705 | size_t bytes_free = delta > 0 ? 0 : 0 - delta; |
| 706 | |
| 707 | QuicFrame frame; |
| 708 | size_t bytes_consumed = creator_.CreateStreamFrame( |
[email protected] | 66ae596 | 2014-05-22 11:13:05 | [diff] [blame] | 709 | kClientDataStreamId1, MakeIOVector(data), kOffset, false, &frame); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 710 | EXPECT_LT(0u, bytes_consumed); |
| 711 | ASSERT_TRUE(creator_.AddSavedFrame(frame)); |
| 712 | SerializedPacket serialized_packet = creator_.SerializePacket(); |
| 713 | ASSERT_TRUE(serialized_packet.packet); |
| 714 | if (bytes_free > 0) { |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 715 | EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize) |
| 716 | - bytes_free, serialized_packet.packet->length()); |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 717 | } else { |
[email protected] | 8e01c06 | 2013-10-31 07:35:31 | [diff] [blame] | 718 | EXPECT_EQ(client_framer_.GetMaxPlaintextSize(kDefaultMaxPacketSize), |
[email protected] | 583bcbcf | 2013-10-28 01:51:15 | [diff] [blame] | 719 | serialized_packet.packet->length()); |
| 720 | } |
| 721 | delete serialized_packet.packet; |
| 722 | delete serialized_packet.retransmittable_frames; |
| 723 | } |
| 724 | } |
| 725 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 726 | TEST_P(QuicPacketCreatorTest, SerializeVersionNegotiationPacket) { |
[email protected] | bbb1007 | 2014-06-13 07:41:59 | [diff] [blame] | 727 | QuicFramerPeer::SetIsServer(&client_framer_, true); |
[email protected] | 4887809 | 2013-07-26 14:51:56 | [diff] [blame] | 728 | QuicVersionVector versions; |
[email protected] | b007e63 | 2013-10-28 08:39:25 | [diff] [blame] | 729 | versions.push_back(test::QuicVersionMax()); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 730 | scoped_ptr<QuicEncryptedPacket> encrypted( |
| 731 | creator_.SerializeVersionNegotiationPacket(versions)); |
| 732 | |
| 733 | { |
| 734 | InSequence s; |
| 735 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 736 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 737 | EXPECT_CALL(framer_visitor_, OnVersionNegotiationPacket(_)); |
| 738 | } |
[email protected] | bbb1007 | 2014-06-13 07:41:59 | [diff] [blame] | 739 | QuicFramerPeer::SetIsServer(&client_framer_, false); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 740 | client_framer_.ProcessPacket(*encrypted); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 741 | } |
| 742 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 743 | TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthLeastAwaiting) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 744 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 745 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 746 | |
[email protected] | a692ad9d | 2014-07-18 21:35:24 | [diff] [blame] | 747 | size_t max_packets_per_fec_group = 10; |
| 748 | creator_.set_max_packets_per_fec_group(max_packets_per_fec_group); |
| 749 | creator_.set_sequence_number(64 - max_packets_per_fec_group); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 750 | creator_.UpdateSequenceNumberLength(2, 10000); |
| 751 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 752 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 753 | |
[email protected] | a692ad9d | 2014-07-18 21:35:24 | [diff] [blame] | 754 | creator_.set_sequence_number(64 * 256 - max_packets_per_fec_group); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 755 | creator_.UpdateSequenceNumberLength(2, 10000); |
| 756 | EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 757 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 758 | |
[email protected] | a692ad9d | 2014-07-18 21:35:24 | [diff] [blame] | 759 | creator_.set_sequence_number(64 * 256 * 256 - max_packets_per_fec_group); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 760 | creator_.UpdateSequenceNumberLength(2, 10000); |
| 761 | EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 762 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 763 | |
[email protected] | a692ad9d | 2014-07-18 21:35:24 | [diff] [blame] | 764 | creator_.set_sequence_number( |
| 765 | GG_UINT64_C(64) * 256 * 256 * 256 * 256 - max_packets_per_fec_group); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 766 | creator_.UpdateSequenceNumberLength(2, 10000); |
| 767 | EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 768 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 769 | } |
| 770 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 771 | TEST_P(QuicPacketCreatorTest, UpdatePacketSequenceNumberLengthBandwidth) { |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 772 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 773 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 774 | |
| 775 | creator_.UpdateSequenceNumberLength(1, 10000); |
| 776 | EXPECT_EQ(PACKET_1BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 777 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 778 | |
| 779 | creator_.UpdateSequenceNumberLength(1, 10000 * 256); |
| 780 | EXPECT_EQ(PACKET_2BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 781 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 782 | |
| 783 | creator_.UpdateSequenceNumberLength(1, 10000 * 256 * 256); |
| 784 | EXPECT_EQ(PACKET_4BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 785 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 786 | |
| 787 | creator_.UpdateSequenceNumberLength( |
| 788 | 1, GG_UINT64_C(1000) * 256 * 256 * 256 * 256); |
| 789 | EXPECT_EQ(PACKET_6BYTE_SEQUENCE_NUMBER, |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 790 | creator_.next_sequence_number_length()); |
[email protected] | 24e5bc5 | 2013-09-18 15:36:58 | [diff] [blame] | 791 | } |
| 792 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 793 | TEST_P(QuicPacketCreatorTest, CreateStreamFrameWithNotifier) { |
[email protected] | e6ccc1a6 | 2013-12-02 07:02:12 | [diff] [blame] | 794 | // Ensure that if CreateStreamFrame does not consume any data (e.g. a FIN only |
| 795 | // frame) then any QuicAckNotifier that is passed in still gets attached to |
| 796 | // the frame. |
[email protected] | 14a9575 | 2014-01-08 19:01:52 | [diff] [blame] | 797 | scoped_refptr<MockAckNotifierDelegate> delegate(new MockAckNotifierDelegate); |
| 798 | QuicAckNotifier notifier(delegate.get()); |
[email protected] | e6ccc1a6 | 2013-12-02 07:02:12 | [diff] [blame] | 799 | QuicFrame frame; |
| 800 | IOVector empty_iovector; |
| 801 | bool fin = true; |
| 802 | size_t consumed_bytes = creator_.CreateStreamFrameWithNotifier( |
| 803 | 1u, empty_iovector, 0u, fin, ¬ifier, &frame); |
| 804 | EXPECT_EQ(0u, consumed_bytes); |
| 805 | EXPECT_EQ(¬ifier, frame.stream_frame->notifier); |
| 806 | delete frame.stream_frame; |
| 807 | } |
| 808 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 809 | TEST_P(QuicPacketCreatorTest, SerializeFrame) { |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 810 | if (!GetParam().version_serialization) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 811 | creator_.StopSendingVersion(); |
| 812 | } |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 813 | frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector()))); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 814 | SerializedPacket serialized = creator_.SerializeAllFrames(frames_); |
| 815 | delete frames_[0].stream_frame; |
| 816 | |
| 817 | QuicPacketHeader header; |
| 818 | { |
| 819 | InSequence s; |
| 820 | EXPECT_CALL(framer_visitor_, OnPacket()); |
[email protected] | 066d818 | 2014-01-04 02:02:45 | [diff] [blame] | 821 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedPublicHeader(_)); |
[email protected] | ec86d546 | 2013-11-17 16:04:49 | [diff] [blame] | 822 | EXPECT_CALL(framer_visitor_, OnUnauthenticatedHeader(_)); |
[email protected] | 42a3eba | 2014-04-30 10:52:55 | [diff] [blame] | 823 | EXPECT_CALL(framer_visitor_, OnDecryptedPacket(_)); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 824 | EXPECT_CALL(framer_visitor_, OnPacketHeader(_)).WillOnce( |
| 825 | DoAll(SaveArg<0>(&header), Return(true))); |
| 826 | EXPECT_CALL(framer_visitor_, OnStreamFrame(_)); |
| 827 | EXPECT_CALL(framer_visitor_, OnPacketComplete()); |
| 828 | } |
| 829 | ProcessPacket(serialized.packet); |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 830 | EXPECT_EQ(GetParam().version_serialization, |
| 831 | header.public_header.version_flag); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 832 | delete serialized.packet; |
| 833 | } |
| 834 | |
| 835 | TEST_P(QuicPacketCreatorTest, CreateStreamFrameTooLarge) { |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 836 | if (!GetParam().version_serialization) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 837 | creator_.StopSendingVersion(); |
| 838 | } |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 839 | // A string larger than fits into a frame. |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 840 | size_t payload_length; |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 841 | creator_.set_max_packet_length(GetPacketLengthForOneStream( |
[email protected] | 3e60db8 | 2013-08-05 19:43:06 | [diff] [blame] | 842 | client_framer_.version(), |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 843 | QuicPacketCreatorPeer::SendVersionInPacket(&creator_), |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 844 | PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP, &payload_length)); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 845 | QuicFrame frame; |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 846 | const string too_long_payload(payload_length * 2, 'a'); |
| 847 | size_t consumed = creator_.CreateStreamFrame( |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 848 | 1u, MakeIOVector(too_long_payload), 0u, true, &frame); |
[email protected] | f62262b | 2013-07-05 20:57:30 | [diff] [blame] | 849 | EXPECT_EQ(payload_length, consumed); |
| 850 | const string payload(payload_length, 'a'); |
| 851 | CheckStreamFrame(frame, 1u, payload, 0u, false); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 852 | delete frame.stream_frame; |
| 853 | } |
| 854 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 855 | TEST_P(QuicPacketCreatorTest, AddFrameAndSerialize) { |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 856 | if (!GetParam().version_serialization) { |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 857 | creator_.StopSendingVersion(); |
| 858 | } |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 859 | const size_t max_plaintext_size = |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 860 | client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 861 | EXPECT_FALSE(creator_.HasPendingFrames()); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 862 | EXPECT_EQ(max_plaintext_size - |
| 863 | GetPacketHeaderSize( |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 864 | creator_.connection_id_length(), |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 865 | QuicPacketCreatorPeer::SendVersionInPacket(&creator_), |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 866 | PACKET_1BYTE_SEQUENCE_NUMBER, NOT_IN_FEC_GROUP), |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 867 | creator_.BytesFree()); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 868 | |
| 869 | // Add a variety of frame types and then a padding frame. |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 870 | QuicAckFrame ack_frame(MakeAckFrame(0u)); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 871 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame))); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 872 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 873 | |
| 874 | QuicCongestionFeedbackFrame congestion_feedback; |
[email protected] | 310d37b | 2014-08-02 06:15:37 | [diff] [blame] | 875 | congestion_feedback.type = kTCP; |
| 876 | congestion_feedback.tcp.receive_window = 0x4030; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 877 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&congestion_feedback))); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 878 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 879 | |
| 880 | QuicFrame frame; |
[email protected] | 5dafdb6 | 2013-11-14 01:24:26 | [diff] [blame] | 881 | size_t consumed = creator_.CreateStreamFrame( |
| 882 | 1u, MakeIOVector("test"), 0u, false, &frame); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 883 | EXPECT_EQ(4u, consumed); |
| 884 | ASSERT_TRUE(frame.stream_frame); |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 885 | EXPECT_TRUE(creator_.AddSavedFrame(frame)); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 886 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 887 | |
| 888 | QuicPaddingFrame padding_frame; |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 889 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&padding_frame))); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 890 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 891 | EXPECT_EQ(0u, creator_.BytesFree()); |
| 892 | |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 893 | EXPECT_FALSE(creator_.AddSavedFrame(QuicFrame(&ack_frame))); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 894 | |
| 895 | // Ensure the packet is successfully created. |
[email protected] | 9db44391 | 2013-02-25 05:27:03 | [diff] [blame] | 896 | SerializedPacket serialized = creator_.SerializePacket(); |
| 897 | ASSERT_TRUE(serialized.packet); |
| 898 | delete serialized.packet; |
| 899 | ASSERT_TRUE(serialized.retransmittable_frames); |
| 900 | RetransmittableFrames* retransmittable = serialized.retransmittable_frames; |
| 901 | ASSERT_EQ(1u, retransmittable->frames().size()); |
| 902 | EXPECT_EQ(STREAM_FRAME, retransmittable->frames()[0].type); |
| 903 | ASSERT_TRUE(retransmittable->frames()[0].stream_frame); |
| 904 | delete serialized.retransmittable_frames; |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 905 | |
| 906 | EXPECT_FALSE(creator_.HasPendingFrames()); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 907 | EXPECT_EQ(max_plaintext_size - |
| 908 | GetPacketHeaderSize( |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 909 | creator_.connection_id_length(), |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 910 | QuicPacketCreatorPeer::SendVersionInPacket(&creator_), |
[email protected] | ea825e0 | 2013-08-21 18:12:45 | [diff] [blame] | 911 | PACKET_1BYTE_SEQUENCE_NUMBER, |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 912 | NOT_IN_FEC_GROUP), |
[email protected] | 5351cc4b | 2013-03-03 07:22:41 | [diff] [blame] | 913 | creator_.BytesFree()); |
[email protected] | fee17f7 | 2013-02-03 07:47:41 | [diff] [blame] | 914 | } |
| 915 | |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 916 | TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithLargePacketSize) { |
| 917 | if (!GetParam().version_serialization) { |
| 918 | creator_.StopSendingVersion(); |
| 919 | } |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 920 | creator_.set_max_packet_length(kMaxPacketSize); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 921 | const size_t max_plaintext_size = |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 922 | client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 923 | |
| 924 | // Serialized length of ack frame with 2000 nack ranges should be limited by |
| 925 | // the number of nack ranges that can be fit in an ack frame. |
| 926 | QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u); |
| 927 | size_t frame_len = client_framer_.GetSerializedFrameLength( |
| 928 | QuicFrame(&ack_frame), creator_.BytesFree(), true, true, |
| 929 | NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER); |
| 930 | EXPECT_GT(creator_.BytesFree(), frame_len); |
| 931 | EXPECT_GT(max_plaintext_size, creator_.PacketSize()); |
| 932 | |
| 933 | // Add ack frame to creator. |
| 934 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame))); |
| 935 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 936 | EXPECT_GT(max_plaintext_size, creator_.PacketSize()); |
| 937 | EXPECT_LT(0u, creator_.BytesFree()); |
| 938 | |
| 939 | // Make sure that an additional stream frame can be added to the packet. |
| 940 | QuicFrame stream_frame; |
| 941 | size_t consumed = creator_.CreateStreamFrame( |
| 942 | 2u, MakeIOVector("test"), 0u, false, &stream_frame); |
| 943 | EXPECT_EQ(4u, consumed); |
| 944 | ASSERT_TRUE(stream_frame.stream_frame); |
| 945 | EXPECT_TRUE(creator_.AddSavedFrame(stream_frame)); |
| 946 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 947 | |
| 948 | // Ensure the packet is successfully created, and the packet size estimate |
| 949 | // matches the serialized packet length. |
| 950 | EXPECT_CALL(entropy_calculator_, |
| 951 | EntropyHash(_)).WillOnce(testing::Return(0)); |
| 952 | size_t est_packet_size = creator_.PacketSize(); |
| 953 | SerializedPacket serialized = creator_.SerializePacket(); |
| 954 | ASSERT_TRUE(serialized.packet); |
| 955 | EXPECT_EQ(est_packet_size, serialized.packet->length()); |
| 956 | delete serialized.retransmittable_frames; |
| 957 | delete serialized.packet; |
| 958 | } |
| 959 | |
| 960 | TEST_P(QuicPacketCreatorTest, SerializeTruncatedAckFrameWithSmallPacketSize) { |
| 961 | if (!GetParam().version_serialization) { |
| 962 | creator_.StopSendingVersion(); |
| 963 | } |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 964 | creator_.set_max_packet_length(500u); |
| 965 | |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 966 | const size_t max_plaintext_size = |
[email protected] | 9cda5fd | 2014-06-03 10:20:28 | [diff] [blame] | 967 | client_framer_.GetMaxPlaintextSize(creator_.max_packet_length()); |
[email protected] | aa7e4ef | 2014-05-28 03:53:15 | [diff] [blame] | 968 | EXPECT_EQ(max_plaintext_size - creator_.PacketSize(), creator_.BytesFree()); |
| 969 | |
| 970 | // Serialized length of ack frame with 2000 nack ranges should be limited by |
| 971 | // the packet size. |
| 972 | QuicAckFrame ack_frame = MakeAckFrameWithNackRanges(2000u, 0u); |
| 973 | size_t frame_len = client_framer_.GetSerializedFrameLength( |
| 974 | QuicFrame(&ack_frame), creator_.BytesFree(), true, true, |
| 975 | NOT_IN_FEC_GROUP, PACKET_1BYTE_SEQUENCE_NUMBER); |
| 976 | EXPECT_EQ(creator_.BytesFree(), frame_len); |
| 977 | |
| 978 | // Add ack frame to creator. |
| 979 | EXPECT_TRUE(creator_.AddSavedFrame(QuicFrame(&ack_frame))); |
| 980 | EXPECT_TRUE(creator_.HasPendingFrames()); |
| 981 | EXPECT_EQ(max_plaintext_size, creator_.PacketSize()); |
| 982 | EXPECT_EQ(0u, creator_.BytesFree()); |
| 983 | |
| 984 | // Ensure the packet is successfully created, and the packet size estimate |
| 985 | // may not match the serialized packet length. |
| 986 | EXPECT_CALL(entropy_calculator_, |
| 987 | EntropyHash(_)).WillOnce(Return(0)); |
| 988 | size_t est_packet_size = creator_.PacketSize(); |
| 989 | SerializedPacket serialized = creator_.SerializePacket(); |
| 990 | ASSERT_TRUE(serialized.packet); |
| 991 | EXPECT_GE(est_packet_size, serialized.packet->length()); |
| 992 | delete serialized.packet; |
| 993 | } |
| 994 | |
| 995 | |
[email protected] | 51cc134 | 2014-04-18 23:44:37 | [diff] [blame] | 996 | TEST_P(QuicPacketCreatorTest, EntropyFlag) { |
[email protected] | 457d695 | 2013-12-13 09:24:58 | [diff] [blame] | 997 | frames_.push_back(QuicFrame(new QuicStreamFrame(0u, false, 0u, IOVector()))); |
| 998 | |
| 999 | for (int i = 0; i < 2; ++i) { |
| 1000 | for (int j = 0; j < 64; ++j) { |
| 1001 | SerializedPacket serialized = creator_.SerializeAllFrames(frames_); |
| 1002 | // Verify both BoolSource and hash algorithm. |
| 1003 | bool expected_rand_bool = |
| 1004 | (mock_random_.RandUint64() & (GG_UINT64_C(1) << j)) != 0; |
| 1005 | bool observed_rand_bool = |
| 1006 | (serialized.entropy_hash & (1 << ((j+1) % 8))) != 0; |
| 1007 | uint8 rest_of_hash = serialized.entropy_hash & ~(1 << ((j+1) % 8)); |
| 1008 | EXPECT_EQ(expected_rand_bool, observed_rand_bool); |
| 1009 | EXPECT_EQ(0, rest_of_hash); |
| 1010 | delete serialized.packet; |
| 1011 | } |
| 1012 | // After 64 calls, BoolSource will refresh the bucket - make sure it does. |
| 1013 | mock_random_.ChangeValue(); |
| 1014 | } |
| 1015 | |
| 1016 | delete frames_[0].stream_frame; |
| 1017 | } |
| 1018 | |
[email protected] | a506124 | 2012-10-23 23:29:37 | [diff] [blame] | 1019 | } // namespace |
| 1020 | } // namespace test |
| 1021 | } // namespace net |