| 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/core/quic_packet_creator.h" | 5 #include "net/quic/core/quic_packet_creator.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <ostream> | 9 #include <ostream> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "net/quic/core/crypto/null_encrypter.h" | 13 #include "net/quic/core/crypto/null_encrypter.h" |
| 14 #include "net/quic/core/crypto/quic_decrypter.h" | 14 #include "net/quic/core/crypto/quic_decrypter.h" |
| 15 #include "net/quic/core/crypto/quic_encrypter.h" | 15 #include "net/quic/core/crypto/quic_encrypter.h" |
| 16 #include "net/quic/core/quic_data_writer.h" |
| 16 #include "net/quic/core/quic_pending_retransmission.h" | 17 #include "net/quic/core/quic_pending_retransmission.h" |
| 17 #include "net/quic/core/quic_simple_buffer_allocator.h" | 18 #include "net/quic/core/quic_simple_buffer_allocator.h" |
| 18 #include "net/quic/core/quic_utils.h" | 19 #include "net/quic/core/quic_utils.h" |
| 19 #include "net/quic/platform/api/quic_socket_address.h" | 20 #include "net/quic/platform/api/quic_socket_address.h" |
| 20 #include "net/quic/platform/api/quic_string_piece.h" | 21 #include "net/quic/platform/api/quic_string_piece.h" |
| 21 #include "net/quic/platform/api/quic_test.h" | 22 #include "net/quic/platform/api/quic_test.h" |
| 22 #include "net/quic/test_tools/quic_framer_peer.h" | 23 #include "net/quic/test_tools/quic_framer_peer.h" |
| 23 #include "net/quic/test_tools/quic_packet_creator_peer.h" | 24 #include "net/quic/test_tools/quic_packet_creator_peer.h" |
| 24 #include "net/quic/test_tools/quic_test_utils.h" | 25 #include "net/quic/test_tools/quic_test_utils.h" |
| 26 #include "net/quic/test_tools/simple_data_producer.h" |
| 25 | 27 |
| 26 using std::string; | 28 using std::string; |
| 27 using testing::DoAll; | 29 using testing::DoAll; |
| 28 using testing::InSequence; | 30 using testing::InSequence; |
| 29 using testing::Return; | 31 using testing::Return; |
| 30 using testing::SaveArg; | 32 using testing::SaveArg; |
| 31 using testing::StrictMock; | 33 using testing::StrictMock; |
| 32 using testing::_; | 34 using testing::_; |
| 33 | 35 |
| 34 namespace net { | 36 namespace net { |
| 35 namespace test { | 37 namespace test { |
| 36 namespace { | 38 namespace { |
| 37 | 39 |
| 38 const QuicStreamId kGetNthClientInitiatedStreamId1 = kHeadersStreamId + 2; | 40 const QuicStreamId kGetNthClientInitiatedStreamId1 = kHeadersStreamId + 2; |
| 39 | 41 |
| 40 // Run tests with combinations of {QuicVersion, ToggleVersionSerialization}. | 42 // Run tests with combinations of {QuicVersion, ToggleVersionSerialization}. |
| 41 struct TestParams { | 43 struct TestParams { |
| 42 TestParams(QuicVersion version, | 44 TestParams(QuicVersion version, |
| 43 bool version_serialization, | 45 bool version_serialization, |
| 44 QuicConnectionIdLength length) | 46 QuicConnectionIdLength length, |
| 47 bool delegate_saves_data) |
| 45 : version(version), | 48 : version(version), |
| 46 connection_id_length(length), | 49 connection_id_length(length), |
| 47 version_serialization(version_serialization) {} | 50 version_serialization(version_serialization), |
| 51 delegate_saves_data(delegate_saves_data) {} |
| 48 | 52 |
| 49 friend std::ostream& operator<<(std::ostream& os, const TestParams& p) { | 53 friend std::ostream& operator<<(std::ostream& os, const TestParams& p) { |
| 50 os << "{ client_version: " << QuicVersionToString(p.version) | 54 os << "{ client_version: " << QuicVersionToString(p.version) |
| 51 << " connection id length: " << p.connection_id_length | 55 << " connection id length: " << p.connection_id_length |
| 52 << " include version: " << p.version_serialization << " }"; | 56 << " include version: " << p.version_serialization |
| 57 << " delegate_saves_data: " << p.delegate_saves_data << " }"; |
| 53 return os; | 58 return os; |
| 54 } | 59 } |
| 55 | 60 |
| 56 QuicVersion version; | 61 QuicVersion version; |
| 57 QuicConnectionIdLength connection_id_length; | 62 QuicConnectionIdLength connection_id_length; |
| 58 bool version_serialization; | 63 bool version_serialization; |
| 64 bool delegate_saves_data; |
| 59 }; | 65 }; |
| 60 | 66 |
| 61 // Constructs various test permutations. | 67 // Constructs various test permutations. |
| 62 std::vector<TestParams> GetTestParams() { | 68 std::vector<TestParams> GetTestParams() { |
| 63 std::vector<TestParams> params; | 69 std::vector<TestParams> params; |
| 64 constexpr QuicConnectionIdLength kMax = PACKET_8BYTE_CONNECTION_ID; | 70 constexpr QuicConnectionIdLength kMax = PACKET_8BYTE_CONNECTION_ID; |
| 65 QuicVersionVector all_supported_versions = AllSupportedVersions(); | 71 QuicVersionVector all_supported_versions = AllSupportedVersions(); |
| 66 for (size_t i = 0; i < all_supported_versions.size(); ++i) { | 72 for (bool delegate_saves_data : {true, false}) { |
| 67 params.push_back(TestParams(all_supported_versions[i], true, kMax)); | 73 for (size_t i = 0; i < all_supported_versions.size(); ++i) { |
| 68 params.push_back(TestParams(all_supported_versions[i], false, kMax)); | 74 params.push_back(TestParams(all_supported_versions[i], true, kMax, |
| 75 delegate_saves_data)); |
| 76 params.push_back(TestParams(all_supported_versions[i], false, kMax, |
| 77 delegate_saves_data)); |
| 78 } |
| 79 params.push_back(TestParams(all_supported_versions[0], true, |
| 80 PACKET_0BYTE_CONNECTION_ID, |
| 81 delegate_saves_data)); |
| 82 params.push_back( |
| 83 TestParams(all_supported_versions[0], true, kMax, delegate_saves_data)); |
| 69 } | 84 } |
| 70 params.push_back( | |
| 71 TestParams(all_supported_versions[0], true, PACKET_0BYTE_CONNECTION_ID)); | |
| 72 params.push_back(TestParams(all_supported_versions[0], true, kMax)); | |
| 73 return params; | 85 return params; |
| 74 } | 86 } |
| 75 | 87 |
| 76 class MockDelegate : public QuicPacketCreator::DelegateInterface { | |
| 77 public: | |
| 78 MockDelegate() {} | |
| 79 ~MockDelegate() override {} | |
| 80 | |
| 81 MOCK_METHOD1(OnSerializedPacket, void(SerializedPacket* packet)); | |
| 82 MOCK_METHOD3(OnUnrecoverableError, | |
| 83 void(QuicErrorCode, | |
| 84 const string&, | |
| 85 ConnectionCloseSource source)); | |
| 86 | |
| 87 private: | |
| 88 DISALLOW_COPY_AND_ASSIGN(MockDelegate); | |
| 89 }; | |
| 90 | |
| 91 class QuicPacketCreatorTest : public QuicTestWithParam<TestParams> { | 88 class QuicPacketCreatorTest : public QuicTestWithParam<TestParams> { |
| 92 public: | 89 public: |
| 93 void ClearSerializedPacketForTests(SerializedPacket* serialized_packet) { | 90 void ClearSerializedPacketForTests(SerializedPacket* serialized_packet) { |
| 94 if (serialized_packet == nullptr) { | 91 if (serialized_packet == nullptr) { |
| 95 return; | 92 return; |
| 96 } | 93 } |
| 97 ClearSerializedPacket(serialized_packet); | 94 ClearSerializedPacket(serialized_packet); |
| 98 } | 95 } |
| 99 | 96 |
| 100 void SaveSerializedPacket(SerializedPacket* serialized_packet) { | 97 void SaveSerializedPacket(SerializedPacket* serialized_packet) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 129 &delegate_), | 126 &delegate_), |
| 130 serialized_packet_(creator_.NoPacket()) { | 127 serialized_packet_(creator_.NoPacket()) { |
| 131 creator_.set_connection_id_length(GetParam().connection_id_length); | 128 creator_.set_connection_id_length(GetParam().connection_id_length); |
| 132 | 129 |
| 133 creator_.SetEncrypter(ENCRYPTION_INITIAL, | 130 creator_.SetEncrypter(ENCRYPTION_INITIAL, |
| 134 new NullEncrypter(Perspective::IS_CLIENT)); | 131 new NullEncrypter(Perspective::IS_CLIENT)); |
| 135 creator_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, | 132 creator_.SetEncrypter(ENCRYPTION_FORWARD_SECURE, |
| 136 new NullEncrypter(Perspective::IS_CLIENT)); | 133 new NullEncrypter(Perspective::IS_CLIENT)); |
| 137 client_framer_.set_visitor(&framer_visitor_); | 134 client_framer_.set_visitor(&framer_visitor_); |
| 138 server_framer_.set_visitor(&framer_visitor_); | 135 server_framer_.set_visitor(&framer_visitor_); |
| 136 if (GetParam().delegate_saves_data) { |
| 137 creator_.set_delegate_saves_data(true); |
| 138 } |
| 139 } | 139 } |
| 140 | 140 |
| 141 ~QuicPacketCreatorTest() override { | 141 ~QuicPacketCreatorTest() override { |
| 142 delete[] serialized_packet_.encrypted_buffer; | 142 delete[] serialized_packet_.encrypted_buffer; |
| 143 ClearSerializedPacket(&serialized_packet_); | 143 ClearSerializedPacket(&serialized_packet_); |
| 144 } | 144 } |
| 145 | 145 |
| 146 SerializedPacket SerializeAllFrames(const QuicFrames& frames) { | 146 SerializedPacket SerializeAllFrames(const QuicFrames& frames) { |
| 147 SerializedPacket packet = QuicPacketCreatorPeer::SerializeAllFrames( | 147 SerializedPacket packet = QuicPacketCreatorPeer::SerializeAllFrames( |
| 148 &creator_, frames, buffer_, kMaxPacketSize); | 148 &creator_, frames, buffer_, kMaxPacketSize); |
| 149 EXPECT_EQ(QuicPacketCreatorPeer::GetEncryptionLevel(&creator_), | 149 EXPECT_EQ(QuicPacketCreatorPeer::GetEncryptionLevel(&creator_), |
| 150 packet.encryption_level); | 150 packet.encryption_level); |
| 151 return packet; | 151 return packet; |
| 152 } | 152 } |
| 153 | 153 |
| 154 void ProcessPacket(const SerializedPacket& packet) { | 154 void ProcessPacket(const SerializedPacket& packet) { |
| 155 QuicEncryptedPacket encrypted_packet(packet.encrypted_buffer, | 155 QuicEncryptedPacket encrypted_packet(packet.encrypted_buffer, |
| 156 packet.encrypted_length); | 156 packet.encrypted_length); |
| 157 server_framer_.ProcessPacket(encrypted_packet); | 157 server_framer_.ProcessPacket(encrypted_packet); |
| 158 } | 158 } |
| 159 | 159 |
| 160 void CheckStreamFrame(const QuicFrame& frame, | 160 void CheckStreamFrame(const QuicFrame& frame, |
| 161 QuicStreamId stream_id, | 161 QuicStreamId stream_id, |
| 162 const string& data, | 162 const string& data, |
| 163 QuicStreamOffset offset, | 163 QuicStreamOffset offset, |
| 164 bool fin) { | 164 bool fin) { |
| 165 EXPECT_EQ(STREAM_FRAME, frame.type); | 165 EXPECT_EQ(STREAM_FRAME, frame.type); |
| 166 ASSERT_TRUE(frame.stream_frame); | 166 ASSERT_TRUE(frame.stream_frame); |
| 167 EXPECT_EQ(stream_id, frame.stream_frame->stream_id); | 167 EXPECT_EQ(stream_id, frame.stream_frame->stream_id); |
| 168 EXPECT_EQ(data, QuicStringPiece(frame.stream_frame->data_buffer, | 168 if (GetParam().delegate_saves_data) { |
| 169 frame.stream_frame->data_length)); | 169 char buf[kMaxPacketSize]; |
| 170 QuicDataWriter writer(kMaxPacketSize, buf, Perspective::IS_CLIENT, |
| 171 HOST_BYTE_ORDER); |
| 172 if (frame.stream_frame->data_length > 0) { |
| 173 delegate_.WriteStreamData(stream_id, frame.stream_frame->offset, |
| 174 frame.stream_frame->data_length, &writer); |
| 175 } |
| 176 EXPECT_EQ(data, QuicStringPiece(buf, frame.stream_frame->data_length)); |
| 177 } else { |
| 178 EXPECT_EQ(data, QuicStringPiece(frame.stream_frame->data_buffer, |
| 179 frame.stream_frame->data_length)); |
| 180 } |
| 170 EXPECT_EQ(offset, frame.stream_frame->offset); | 181 EXPECT_EQ(offset, frame.stream_frame->offset); |
| 171 EXPECT_EQ(fin, frame.stream_frame->fin); | 182 EXPECT_EQ(fin, frame.stream_frame->fin); |
| 172 } | 183 } |
| 173 | 184 |
| 174 // Returns the number of bytes consumed by the header of packet, including | 185 // Returns the number of bytes consumed by the header of packet, including |
| 175 // the version. | 186 // the version. |
| 176 size_t GetPacketHeaderOverhead(QuicVersion version) { | 187 size_t GetPacketHeaderOverhead(QuicVersion version) { |
| 177 return GetPacketHeaderSize( | 188 return GetPacketHeaderSize( |
| 178 version, creator_.connection_id_length(), kIncludeVersion, | 189 version, creator_.connection_id_length(), kIncludeVersion, |
| 179 !kIncludeDiversificationNonce, | 190 !kIncludeDiversificationNonce, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 209 num_padding_bytes, encryption_level, packet_number_length); | 220 num_padding_bytes, encryption_level, packet_number_length); |
| 210 } | 221 } |
| 211 | 222 |
| 212 static const QuicStreamOffset kOffset = 1u; | 223 static const QuicStreamOffset kOffset = 1u; |
| 213 | 224 |
| 214 char buffer_[kMaxPacketSize]; | 225 char buffer_[kMaxPacketSize]; |
| 215 QuicFrames frames_; | 226 QuicFrames frames_; |
| 216 QuicFramer server_framer_; | 227 QuicFramer server_framer_; |
| 217 QuicFramer client_framer_; | 228 QuicFramer client_framer_; |
| 218 StrictMock<MockFramerVisitor> framer_visitor_; | 229 StrictMock<MockFramerVisitor> framer_visitor_; |
| 219 StrictMock<MockDelegate> delegate_; | 230 StrictMock<MockPacketCreatorDelegate> delegate_; |
| 220 QuicConnectionId connection_id_; | 231 QuicConnectionId connection_id_; |
| 221 string data_; | 232 string data_; |
| 222 struct iovec iov_; | 233 struct iovec iov_; |
| 223 SimpleBufferAllocator buffer_allocator_; | 234 SimpleBufferAllocator buffer_allocator_; |
| 224 QuicPacketCreator creator_; | 235 QuicPacketCreator creator_; |
| 225 SerializedPacket serialized_packet_; | 236 SerializedPacket serialized_packet_; |
| 237 SimpleDataProducer producer_; |
| 226 }; | 238 }; |
| 227 | 239 |
| 228 // Run all packet creator tests with all supported versions of QUIC, and with | 240 // Run all packet creator tests with all supported versions of QUIC, and with |
| 229 // and without version in the packet header, as well as doing a run for each | 241 // and without version in the packet header, as well as doing a run for each |
| 230 // length of truncated connection id. | 242 // length of truncated connection id. |
| 231 INSTANTIATE_TEST_CASE_P(QuicPacketCreatorTests, | 243 INSTANTIATE_TEST_CASE_P(QuicPacketCreatorTests, |
| 232 QuicPacketCreatorTest, | 244 QuicPacketCreatorTest, |
| 233 ::testing::ValuesIn(GetTestParams())); | 245 ::testing::ValuesIn(GetTestParams())); |
| 234 | 246 |
| 235 TEST_P(QuicPacketCreatorTest, SerializeFrames) { | 247 TEST_P(QuicPacketCreatorTest, SerializeFrames) { |
| (...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1026 // Flush all paddings. | 1038 // Flush all paddings. |
| 1027 while (creator_.pending_padding_bytes() > 0) { | 1039 while (creator_.pending_padding_bytes() > 0) { |
| 1028 creator_.Flush(); | 1040 creator_.Flush(); |
| 1029 } | 1041 } |
| 1030 EXPECT_EQ(0u, creator_.pending_padding_bytes()); | 1042 EXPECT_EQ(0u, creator_.pending_padding_bytes()); |
| 1031 } | 1043 } |
| 1032 | 1044 |
| 1033 } // namespace | 1045 } // namespace |
| 1034 } // namespace test | 1046 } // namespace test |
| 1035 } // namespace net | 1047 } // namespace net |
| OLD | NEW |