| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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_buffered_packet_store.h" | 5 #include "net/quic/quic_buffered_packet_store.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "net/quic/test_tools/mock_clock.h" | 11 #include "net/quic/test_tools/mock_clock.h" |
| 12 #include "net/quic/test_tools/quic_test_utils.h" | 12 #include "net/quic/test_tools/quic_test_utils.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using std::list; |
| 17 using std::string; |
| 18 |
| 16 namespace net { | 19 namespace net { |
| 17 | 20 |
| 18 typedef QuicBufferedPacketStore::BufferedPacket BufferedPacket; | 21 typedef QuicBufferedPacketStore::BufferedPacket BufferedPacket; |
| 19 typedef QuicBufferedPacketStore::EnqueuePacketResult EnqueuePacketResult; | 22 typedef QuicBufferedPacketStore::EnqueuePacketResult EnqueuePacketResult; |
| 20 | 23 |
| 21 static const size_t kDefaultMaxConnectionsInStore = 100; | 24 static const size_t kDefaultMaxConnectionsInStore = 100; |
| 22 | 25 |
| 23 namespace test { | 26 namespace test { |
| 24 class QuicBufferedPacketStorePeer { | 27 class QuicBufferedPacketStorePeer { |
| 25 public: | 28 public: |
| (...skipping 22 matching lines...) Expand all Loading... |
| 48 // The packets queue for most recently expirect connection. | 51 // The packets queue for most recently expirect connection. |
| 49 BufferedPacketList last_expired_packet_queue_; | 52 BufferedPacketList last_expired_packet_queue_; |
| 50 }; | 53 }; |
| 51 | 54 |
| 52 class QuicBufferedPacketStoreTest : public ::testing::Test { | 55 class QuicBufferedPacketStoreTest : public ::testing::Test { |
| 53 public: | 56 public: |
| 54 QuicBufferedPacketStoreTest() | 57 QuicBufferedPacketStoreTest() |
| 55 : store_(&visitor_, &clock_, &alarm_factory_), | 58 : store_(&visitor_, &clock_, &alarm_factory_), |
| 56 server_address_(Loopback6(), 65535), | 59 server_address_(Loopback6(), 65535), |
| 57 client_address_(Loopback6(), 65535), | 60 client_address_(Loopback6(), 65535), |
| 58 data_packet_(packet_content_.data(), packet_content_.size()) {} | 61 packet_content_("some encrypted content"), |
| 62 packet_time_( |
| 63 QuicTime::Zero().Add(QuicTime::Delta::FromMicroseconds(42))), |
| 64 data_packet_(packet_content_.data(), |
| 65 packet_content_.size(), |
| 66 packet_time_) {} |
| 59 | 67 |
| 60 protected: | 68 protected: |
| 61 QuicBufferedPacketStoreVisitor visitor_; | 69 QuicBufferedPacketStoreVisitor visitor_; |
| 62 MockClock clock_; | 70 MockClock clock_; |
| 63 MockAlarmFactory alarm_factory_; | 71 MockAlarmFactory alarm_factory_; |
| 64 QuicBufferedPacketStore store_; | 72 QuicBufferedPacketStore store_; |
| 65 IPEndPoint server_address_; | 73 IPEndPoint server_address_; |
| 66 IPEndPoint client_address_; | 74 IPEndPoint client_address_; |
| 67 std::string packet_content_ = "some encrypted content"; | 75 string packet_content_; |
| 68 QuicEncryptedPacket data_packet_; | 76 QuicTime packet_time_; |
| 77 QuicReceivedPacket data_packet_; |
| 69 }; | 78 }; |
| 70 | 79 |
| 71 TEST_F(QuicBufferedPacketStoreTest, SimpleEnqueueAndDeliverPacket) { | 80 TEST_F(QuicBufferedPacketStoreTest, SimpleEnqueueAndDeliverPacket) { |
| 72 QuicConnectionId connection_id = 1; | 81 QuicConnectionId connection_id = 1; |
| 73 store_.EnqueuePacket(connection_id, data_packet_, server_address_, | 82 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 74 client_address_); | 83 client_address_); |
| 75 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id); | 84 list<BufferedPacket> queue = store_.DeliverPackets(connection_id); |
| 85 EXPECT_TRUE(store_.HasBufferedPackets(connection_id)); |
| 76 ASSERT_EQ(1u, queue.size()); | 86 ASSERT_EQ(1u, queue.size()); |
| 77 // Check content of the only packet in the queue. | 87 // Check content of the only packet in the queue. |
| 78 EXPECT_EQ(packet_content_, queue.front().packet->AsStringPiece()); | 88 EXPECT_EQ(packet_content_, queue.front().packet->AsStringPiece()); |
| 89 EXPECT_EQ(packet_time_, queue.front().packet->receipt_time()); |
| 79 EXPECT_EQ(client_address_, queue.front().client_address); | 90 EXPECT_EQ(client_address_, queue.front().client_address); |
| 80 EXPECT_EQ(server_address_, queue.front().server_address); | 91 EXPECT_EQ(server_address_, queue.front().server_address); |
| 81 // No more packets on connection 1 should remain in the store. | 92 // No more packets on connection 1 should remain in the store. |
| 82 EXPECT_TRUE(store_.DeliverPackets(connection_id).empty()); | 93 EXPECT_TRUE(store_.DeliverPackets(connection_id).empty()); |
| 94 EXPECT_FALSE(store_.HasBufferedPackets(connection_id)); |
| 83 } | 95 } |
| 84 | 96 |
| 85 TEST_F(QuicBufferedPacketStoreTest, DifferentPacketAddressOnOneConnection) { | 97 TEST_F(QuicBufferedPacketStoreTest, DifferentPacketAddressOnOneConnection) { |
| 86 IPEndPoint addr_with_new_port(Loopback4(), 256); | 98 IPEndPoint addr_with_new_port(Loopback4(), 256); |
| 87 QuicConnectionId connection_id = 1; | 99 QuicConnectionId connection_id = 1; |
| 88 store_.EnqueuePacket(connection_id, data_packet_, server_address_, | 100 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 89 client_address_); | 101 client_address_); |
| 90 store_.EnqueuePacket(connection_id, data_packet_, server_address_, | 102 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 91 addr_with_new_port); | 103 addr_with_new_port); |
| 92 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id); | 104 list<BufferedPacket> queue = store_.DeliverPackets(connection_id); |
| 93 ASSERT_EQ(2u, queue.size()); | 105 ASSERT_EQ(2u, queue.size()); |
| 94 // The address migration path should be preserved. | 106 // The address migration path should be preserved. |
| 95 EXPECT_EQ(client_address_, queue.front().client_address); | 107 EXPECT_EQ(client_address_, queue.front().client_address); |
| 96 EXPECT_EQ(addr_with_new_port, queue.back().client_address); | 108 EXPECT_EQ(addr_with_new_port, queue.back().client_address); |
| 97 } | 109 } |
| 98 | 110 |
| 99 TEST_F(QuicBufferedPacketStoreTest, | 111 TEST_F(QuicBufferedPacketStoreTest, |
| 100 EnqueueAndDeliverMultiplePacketsOnMultipleConnections) { | 112 EnqueueAndDeliverMultiplePacketsOnMultipleConnections) { |
| 101 size_t num_connections = 10; | 113 size_t num_connections = 10; |
| 102 for (QuicConnectionId connection_id = 1; connection_id <= num_connections; | 114 for (QuicConnectionId connection_id = 1; connection_id <= num_connections; |
| 103 ++connection_id) { | 115 ++connection_id) { |
| 104 store_.EnqueuePacket(connection_id, data_packet_, server_address_, | 116 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 105 client_address_); | 117 client_address_); |
| 106 store_.EnqueuePacket(connection_id, data_packet_, server_address_, | 118 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 107 client_address_); | 119 client_address_); |
| 108 } | 120 } |
| 109 | 121 |
| 110 // Deliver packets in reversed order. | 122 // Deliver packets in reversed order. |
| 111 for (QuicConnectionId connection_id = num_connections; connection_id > 0; | 123 for (QuicConnectionId connection_id = num_connections; connection_id > 0; |
| 112 --connection_id) { | 124 --connection_id) { |
| 113 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id); | 125 list<BufferedPacket> queue = store_.DeliverPackets(connection_id); |
| 114 ASSERT_EQ(2u, queue.size()); | 126 ASSERT_EQ(2u, queue.size()); |
| 115 } | 127 } |
| 116 } | 128 } |
| 117 | 129 |
| 118 TEST_F(QuicBufferedPacketStoreTest, | 130 TEST_F(QuicBufferedPacketStoreTest, |
| 119 FailToBufferTooManyPacketsOnExistingConnection) { | 131 FailToBufferTooManyPacketsOnExistingConnection) { |
| 120 // Tests that for one connection, only limited number of packets can be | 132 // Tests that for one connection, only limited number of packets can be |
| 121 // buffered. | 133 // buffered. |
| 122 size_t num_packets = kDefaultMaxUndecryptablePackets + 1; | 134 size_t num_packets = kDefaultMaxUndecryptablePackets + 1; |
| 123 QuicConnectionId connection_id = 1; | 135 QuicConnectionId connection_id = 1; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 149 if (connection_id <= kDefaultMaxConnectionsInStore) { | 161 if (connection_id <= kDefaultMaxConnectionsInStore) { |
| 150 EXPECT_EQ(EnqueuePacketResult::SUCCESS, result); | 162 EXPECT_EQ(EnqueuePacketResult::SUCCESS, result); |
| 151 } else { | 163 } else { |
| 152 EXPECT_EQ(EnqueuePacketResult::TOO_MANY_CONNECTIONS, result); | 164 EXPECT_EQ(EnqueuePacketResult::TOO_MANY_CONNECTIONS, result); |
| 153 } | 165 } |
| 154 } | 166 } |
| 155 // Store only keeps early arrived packets upto |kDefaultMaxConnectionsInStore| | 167 // Store only keeps early arrived packets upto |kDefaultMaxConnectionsInStore| |
| 156 // connections. | 168 // connections. |
| 157 for (size_t connection_id = 1; connection_id <= num_connections; | 169 for (size_t connection_id = 1; connection_id <= num_connections; |
| 158 ++connection_id) { | 170 ++connection_id) { |
| 159 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id); | 171 list<BufferedPacket> queue = store_.DeliverPackets(connection_id); |
| 160 if (connection_id <= kDefaultMaxConnectionsInStore) { | 172 if (connection_id <= kDefaultMaxConnectionsInStore) { |
| 161 EXPECT_EQ(1u, queue.size()); | 173 EXPECT_EQ(1u, queue.size()); |
| 162 } else { | 174 } else { |
| 163 EXPECT_EQ(0u, queue.size()); | 175 EXPECT_EQ(0u, queue.size()); |
| 164 } | 176 } |
| 165 } | 177 } |
| 166 } | 178 } |
| 167 | 179 |
| 168 TEST_F(QuicBufferedPacketStoreTest, PacketQueueExpiredBeforeDelivery) { | 180 TEST_F(QuicBufferedPacketStoreTest, PacketQueueExpiredBeforeDelivery) { |
| 169 QuicConnectionId connection_id = 1; | 181 QuicConnectionId connection_id = 1; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 186 // Fire alarm to remove long-staying connection 1 packets. | 198 // Fire alarm to remove long-staying connection 1 packets. |
| 187 alarm_factory_.FireAlarm( | 199 alarm_factory_.FireAlarm( |
| 188 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); | 200 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); |
| 189 EXPECT_EQ(1u, visitor_.last_expired_packet_queue_.buffered_packets.size()); | 201 EXPECT_EQ(1u, visitor_.last_expired_packet_queue_.buffered_packets.size()); |
| 190 // Try to deliver packets, but packet queue has been removed so no | 202 // Try to deliver packets, but packet queue has been removed so no |
| 191 // packets can be returned. | 203 // packets can be returned. |
| 192 ASSERT_EQ(0u, store_.DeliverPackets(connection_id).size()); | 204 ASSERT_EQ(0u, store_.DeliverPackets(connection_id).size()); |
| 193 | 205 |
| 194 // Deliver packets on connection 2. And the queue for connection 2 should be | 206 // Deliver packets on connection 2. And the queue for connection 2 should be |
| 195 // returned. | 207 // returned. |
| 196 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id2); | 208 list<BufferedPacket> queue = store_.DeliverPackets(connection_id2); |
| 197 ASSERT_EQ(1u, queue.size()); | 209 ASSERT_EQ(1u, queue.size()); |
| 198 // Packets in connection 2 should use another client address. | 210 // Packets in connection 2 should use another client address. |
| 199 EXPECT_EQ(another_client_address, queue.front().client_address); | 211 EXPECT_EQ(another_client_address, queue.front().client_address); |
| 200 | 212 |
| 201 // Test the alarm is reset by enqueueing 2 packets for 3rd connection and wait | 213 // Test the alarm is reset by enqueueing 2 packets for 3rd connection and wait |
| 202 // for them to expire. | 214 // for them to expire. |
| 203 QuicConnectionId connection_id3 = 3; | 215 QuicConnectionId connection_id3 = 3; |
| 204 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, | 216 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, |
| 205 client_address_); | 217 client_address_); |
| 206 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, | 218 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, |
| 207 client_address_); | 219 client_address_); |
| 208 clock_.AdvanceTime(QuicBufferedPacketStorePeer::expiration_alarm(&store_) | 220 clock_.AdvanceTime(QuicBufferedPacketStorePeer::expiration_alarm(&store_) |
| 209 ->deadline() | 221 ->deadline() |
| 210 .Subtract(clock_.ApproximateNow())); | 222 .Subtract(clock_.ApproximateNow())); |
| 211 alarm_factory_.FireAlarm( | 223 alarm_factory_.FireAlarm( |
| 212 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); | 224 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); |
| 213 // |last_expired_packet_queue_| should be updated. | 225 // |last_expired_packet_queue_| should be updated. |
| 214 EXPECT_EQ(2u, visitor_.last_expired_packet_queue_.buffered_packets.size()); | 226 EXPECT_EQ(2u, visitor_.last_expired_packet_queue_.buffered_packets.size()); |
| 215 } | 227 } |
| 216 | 228 |
| 217 } // namespace | 229 } // namespace |
| 218 } // namespace test | 230 } // namespace test |
| 219 } // namespace net | 231 } // namespace net |
| OLD | NEW |