OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/quic/quic_buffered_packet_store.h" |
| 6 |
| 7 #include <list> |
| 8 #include <string> |
| 9 |
| 10 #include "base/stl_util.h" |
| 11 #include "net/quic/test_tools/mock_clock.h" |
| 12 #include "net/quic/test_tools/quic_test_utils.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace net { |
| 17 |
| 18 typedef QuicBufferedPacketStore::BufferedPacket BufferedPacket; |
| 19 typedef QuicBufferedPacketStore::EnqueuePacketResult EnqueuePacketResult; |
| 20 |
| 21 static const size_t kDefaultMaxConnectionsInStore = 100; |
| 22 |
| 23 namespace test { |
| 24 class QuicBufferedPacketStorePeer { |
| 25 public: |
| 26 static QuicAlarm* expiration_alarm(QuicBufferedPacketStore* store) { |
| 27 return store->expiration_alarm_.get(); |
| 28 } |
| 29 }; |
| 30 |
| 31 namespace { |
| 32 |
| 33 typedef QuicBufferedPacketStore::BufferedPacket BufferedPacket; |
| 34 typedef QuicBufferedPacketStore::BufferedPacketList BufferedPacketList; |
| 35 |
| 36 class QuicBufferedPacketStoreVisitor |
| 37 : public QuicBufferedPacketStore::VisitorInterface { |
| 38 public: |
| 39 QuicBufferedPacketStoreVisitor() {} |
| 40 |
| 41 ~QuicBufferedPacketStoreVisitor() override {} |
| 42 |
| 43 void OnExpiredPackets(QuicConnectionId connection_id, |
| 44 BufferedPacketList early_arrived_packets) override { |
| 45 last_expired_packet_queue_ = std::move(early_arrived_packets); |
| 46 } |
| 47 |
| 48 // The packets queue for most recently expirect connection. |
| 49 BufferedPacketList last_expired_packet_queue_; |
| 50 }; |
| 51 |
| 52 class QuicBufferedPacketStoreTest : public ::testing::Test { |
| 53 public: |
| 54 QuicBufferedPacketStoreTest() |
| 55 : store_(&visitor_, &clock_, &alarm_factory_), |
| 56 server_address_(Loopback6(), 65535), |
| 57 client_address_(Loopback6(), 65535), |
| 58 data_packet_(packet_content_.data(), packet_content_.size()) {} |
| 59 |
| 60 protected: |
| 61 QuicBufferedPacketStoreVisitor visitor_; |
| 62 MockClock clock_; |
| 63 MockAlarmFactory alarm_factory_; |
| 64 QuicBufferedPacketStore store_; |
| 65 IPEndPoint server_address_; |
| 66 IPEndPoint client_address_; |
| 67 std::string packet_content_ = "some encrypted content"; |
| 68 QuicEncryptedPacket data_packet_; |
| 69 }; |
| 70 |
| 71 TEST_F(QuicBufferedPacketStoreTest, SimpleEnqueueAndDeliverPacket) { |
| 72 QuicConnectionId connection_id = 1; |
| 73 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 74 client_address_); |
| 75 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id); |
| 76 ASSERT_EQ(1u, queue.size()); |
| 77 // Check content of the only packet in the queue. |
| 78 EXPECT_EQ(packet_content_, queue.front().packet->AsStringPiece()); |
| 79 EXPECT_EQ(client_address_, queue.front().client_address); |
| 80 EXPECT_EQ(server_address_, queue.front().server_address); |
| 81 // No more packets on connection 1 should remain in the store. |
| 82 EXPECT_TRUE(store_.DeliverPackets(connection_id).empty()); |
| 83 } |
| 84 |
| 85 TEST_F(QuicBufferedPacketStoreTest, DifferentPacketAddressOnOneConnection) { |
| 86 IPEndPoint addr_with_new_port(Loopback4(), 256); |
| 87 QuicConnectionId connection_id = 1; |
| 88 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 89 client_address_); |
| 90 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 91 addr_with_new_port); |
| 92 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id); |
| 93 ASSERT_EQ(2u, queue.size()); |
| 94 // The address migration path should be preserved. |
| 95 EXPECT_EQ(client_address_, queue.front().client_address); |
| 96 EXPECT_EQ(addr_with_new_port, queue.back().client_address); |
| 97 } |
| 98 |
| 99 TEST_F(QuicBufferedPacketStoreTest, |
| 100 EnqueueAndDeliverMultiplePacketsOnMultipleConnections) { |
| 101 size_t num_connections = 10; |
| 102 for (QuicConnectionId connection_id = 1; connection_id <= num_connections; |
| 103 ++connection_id) { |
| 104 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 105 client_address_); |
| 106 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 107 client_address_); |
| 108 } |
| 109 |
| 110 // Deliver packets in reversed order. |
| 111 for (QuicConnectionId connection_id = num_connections; connection_id > 0; |
| 112 --connection_id) { |
| 113 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id); |
| 114 ASSERT_EQ(2u, queue.size()); |
| 115 } |
| 116 } |
| 117 |
| 118 TEST_F(QuicBufferedPacketStoreTest, |
| 119 FailToBufferTooManyPacketsOnExistingConnection) { |
| 120 // Tests that for one connection, only limited number of packets can be |
| 121 // buffered. |
| 122 size_t num_packets = kDefaultMaxUndecryptablePackets + 1; |
| 123 QuicConnectionId connection_id = 1; |
| 124 for (size_t i = 1; i <= num_packets; ++i) { |
| 125 // Only first |kDefaultMaxUndecryptablePackets packets| will be buffered. |
| 126 EnqueuePacketResult result = store_.EnqueuePacket( |
| 127 connection_id, data_packet_, server_address_, client_address_); |
| 128 if (i <= kDefaultMaxUndecryptablePackets) { |
| 129 EXPECT_EQ(EnqueuePacketResult::SUCCESS, result); |
| 130 } else { |
| 131 EXPECT_EQ(EnqueuePacketResult::TOO_MANY_PACKETS, result); |
| 132 } |
| 133 } |
| 134 |
| 135 // Only first |kDefaultMaxUndecryptablePackets| packets are kept in the store |
| 136 // and can be delivered. |
| 137 EXPECT_EQ(kDefaultMaxUndecryptablePackets, |
| 138 store_.DeliverPackets(connection_id).size()); |
| 139 } |
| 140 |
| 141 TEST_F(QuicBufferedPacketStoreTest, FailToBufferPacketsForTooManyConnections) { |
| 142 // Tests that store can only keep early arrived packets for limited number of |
| 143 // connections. |
| 144 size_t num_connections = kDefaultMaxConnectionsInStore + 1; |
| 145 for (size_t connection_id = 1; connection_id <= num_connections; |
| 146 ++connection_id) { |
| 147 EnqueuePacketResult result = store_.EnqueuePacket( |
| 148 connection_id, data_packet_, server_address_, client_address_); |
| 149 if (connection_id <= kDefaultMaxConnectionsInStore) { |
| 150 EXPECT_EQ(EnqueuePacketResult::SUCCESS, result); |
| 151 } else { |
| 152 EXPECT_EQ(EnqueuePacketResult::TOO_MANY_CONNECTIONS, result); |
| 153 } |
| 154 } |
| 155 // Store only keeps early arrived packets upto |kDefaultMaxConnectionsInStore| |
| 156 // connections. |
| 157 for (size_t connection_id = 1; connection_id <= num_connections; |
| 158 ++connection_id) { |
| 159 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id); |
| 160 if (connection_id <= kDefaultMaxConnectionsInStore) { |
| 161 EXPECT_EQ(1u, queue.size()); |
| 162 } else { |
| 163 EXPECT_EQ(0u, queue.size()); |
| 164 } |
| 165 } |
| 166 } |
| 167 |
| 168 TEST_F(QuicBufferedPacketStoreTest, PacketQueueExpiredBeforeDelivery) { |
| 169 QuicConnectionId connection_id = 1; |
| 170 store_.EnqueuePacket(connection_id, data_packet_, server_address_, |
| 171 client_address_); |
| 172 // Packet for another connection arrive 1ms later. |
| 173 clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(1)); |
| 174 QuicConnectionId connection_id2 = 2; |
| 175 // Use different client address to differetiate packets from different |
| 176 // connections. |
| 177 IPEndPoint another_client_address(Loopback4(), 255); |
| 178 store_.EnqueuePacket(connection_id2, data_packet_, server_address_, |
| 179 another_client_address); |
| 180 // Advance clock to the time when connection 1 expires. |
| 181 clock_.AdvanceTime(QuicBufferedPacketStorePeer::expiration_alarm(&store_) |
| 182 ->deadline() |
| 183 .Subtract(clock_.ApproximateNow())); |
| 184 ASSERT_GE(clock_.ApproximateNow(), |
| 185 QuicBufferedPacketStorePeer::expiration_alarm(&store_)->deadline()); |
| 186 // Fire alarm to remove long-staying connection 1 packets. |
| 187 alarm_factory_.FireAlarm( |
| 188 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); |
| 189 EXPECT_EQ(1u, visitor_.last_expired_packet_queue_.buffered_packets.size()); |
| 190 // Try to deliver packets, but packet queue has been removed so no |
| 191 // packets can be returned. |
| 192 ASSERT_EQ(0u, store_.DeliverPackets(connection_id).size()); |
| 193 |
| 194 // Deliver packets on connection 2. And the queue for connection 2 should be |
| 195 // returned. |
| 196 std::list<BufferedPacket> queue = store_.DeliverPackets(connection_id2); |
| 197 ASSERT_EQ(1u, queue.size()); |
| 198 // Packets in connection 2 should use another client address. |
| 199 EXPECT_EQ(another_client_address, queue.front().client_address); |
| 200 |
| 201 // Test the alarm is reset by enqueueing 2 packets for 3rd connection and wait |
| 202 // for them to expire. |
| 203 QuicConnectionId connection_id3 = 3; |
| 204 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, |
| 205 client_address_); |
| 206 store_.EnqueuePacket(connection_id3, data_packet_, server_address_, |
| 207 client_address_); |
| 208 clock_.AdvanceTime(QuicBufferedPacketStorePeer::expiration_alarm(&store_) |
| 209 ->deadline() |
| 210 .Subtract(clock_.ApproximateNow())); |
| 211 alarm_factory_.FireAlarm( |
| 212 QuicBufferedPacketStorePeer::expiration_alarm(&store_)); |
| 213 // |last_expired_packet_queue_| should be updated. |
| 214 EXPECT_EQ(2u, visitor_.last_expired_packet_queue_.buffered_packets.size()); |
| 215 } |
| 216 |
| 217 } // namespace |
| 218 } // namespace test |
| 219 } // namespace net |
OLD | NEW |