Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / tools / quic / quic_dispatcher_test.cc
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/tools/quic/quic_dispatcher.h"
6
7 #include <string>
8
9 #include "base/strings/string_piece.h"
10 #include "net/quic/crypto/crypto_handshake.h"
11 #include "net/quic/crypto/quic_crypto_server_config.h"
12 #include "net/quic/crypto/quic_random.h"
13 #include "net/quic/quic_crypto_stream.h"
14 #include "net/quic/quic_flags.h"
15 #include "net/quic/quic_utils.h"
16 #include "net/quic/test_tools/quic_test_utils.h"
17 #include "net/tools/epoll_server/epoll_server.h"
18 #include "net/tools/quic/quic_packet_writer_wrapper.h"
19 #include "net/tools/quic/quic_time_wait_list_manager.h"
20 #include "net/tools/quic/test_tools/quic_dispatcher_peer.h"
21 #include "net/tools/quic/test_tools/quic_test_utils.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 using base::StringPiece;
26 using net::EpollServer;
27 using net::test::ConstructEncryptedPacket;
28 using net::test::MockSession;
29 using net::test::ValueRestore;
30 using net::tools::test::MockConnection;
31 using std::make_pair;
32 using testing::DoAll;
33 using testing::InSequence;
34 using testing::Invoke;
35 using testing::WithoutArgs;
36 using testing::_;
37
38 namespace net {
39 namespace tools {
40 namespace test {
41 namespace {
42
43 class TestDispatcher : public QuicDispatcher {
44  public:
45   explicit TestDispatcher(const QuicConfig& config,
46                           const QuicCryptoServerConfig& crypto_config,
47                           EpollServer* eps)
48       : QuicDispatcher(config,
49                        crypto_config,
50                        QuicSupportedVersions(),
51                        new QuicDispatcher::DefaultPacketWriterFactory(),
52                        eps) {
53   }
54
55   MOCK_METHOD3(CreateQuicSession, QuicSession*(
56       QuicConnectionId connection_id,
57       const IPEndPoint& server_address,
58       const IPEndPoint& client_address));
59
60   using QuicDispatcher::current_server_address;
61   using QuicDispatcher::current_client_address;
62 };
63
64 // A Connection class which unregisters the session from the dispatcher
65 // when sending connection close.
66 // It'd be slightly more realistic to do this from the Session but it would
67 // involve a lot more mocking.
68 class MockServerConnection : public MockConnection {
69  public:
70   MockServerConnection(QuicConnectionId connection_id,
71                        QuicDispatcher* dispatcher)
72       : MockConnection(connection_id, true),
73         dispatcher_(dispatcher) {}
74
75   void UnregisterOnConnectionClosed() {
76     LOG(ERROR) << "Unregistering " << connection_id();
77     dispatcher_->OnConnectionClosed(connection_id(), QUIC_NO_ERROR);
78   }
79  private:
80   QuicDispatcher* dispatcher_;
81 };
82
83 QuicSession* CreateSession(QuicDispatcher* dispatcher,
84                            QuicConnectionId connection_id,
85                            const IPEndPoint& client_address,
86                            MockSession** session) {
87   MockServerConnection* connection =
88       new MockServerConnection(connection_id, dispatcher);
89   *session = new MockSession(connection);
90   ON_CALL(*connection, SendConnectionClose(_)).WillByDefault(
91       WithoutArgs(Invoke(
92           connection, &MockServerConnection::UnregisterOnConnectionClosed)));
93   EXPECT_CALL(*reinterpret_cast<MockConnection*>((*session)->connection()),
94               ProcessUdpPacket(_, client_address, _));
95
96   return *session;
97 }
98
99 class QuicDispatcherTest : public ::testing::Test {
100  public:
101   QuicDispatcherTest()
102       : crypto_config_(QuicCryptoServerConfig::TESTING,
103                        QuicRandom::GetInstance()),
104         dispatcher_(config_, crypto_config_, &eps_),
105         session1_(nullptr),
106         session2_(nullptr) {
107     dispatcher_.Initialize(1);
108   }
109
110   ~QuicDispatcherTest() override {}
111
112   MockConnection* connection1() {
113     return reinterpret_cast<MockConnection*>(session1_->connection());
114   }
115
116   MockConnection* connection2() {
117     return reinterpret_cast<MockConnection*>(session2_->connection());
118   }
119
120   void ProcessPacket(IPEndPoint client_address,
121                      QuicConnectionId connection_id,
122                      bool has_version_flag,
123                      const string& data) {
124     scoped_ptr<QuicEncryptedPacket> packet(ConstructEncryptedPacket(
125         connection_id, has_version_flag, false, 1, data));
126     data_ = string(packet->data(), packet->length());
127     dispatcher_.ProcessPacket(server_address_, client_address, *packet);
128   }
129
130   void ValidatePacket(const QuicEncryptedPacket& packet) {
131     EXPECT_EQ(data_.length(), packet.AsStringPiece().length());
132     EXPECT_EQ(data_, packet.AsStringPiece());
133   }
134
135   EpollServer eps_;
136   QuicConfig config_;
137   QuicCryptoServerConfig crypto_config_;
138   IPEndPoint server_address_;
139   TestDispatcher dispatcher_;
140   MockSession* session1_;
141   MockSession* session2_;
142   string data_;
143 };
144
145 TEST_F(QuicDispatcherTest, ProcessPackets) {
146   IPEndPoint client_address(net::test::Loopback4(), 1);
147   IPAddressNumber any4;
148   CHECK(net::ParseIPLiteralToNumber("0.0.0.0", &any4));
149   server_address_ = IPEndPoint(any4, 5);
150
151   EXPECT_CALL(dispatcher_, CreateQuicSession(1, _, client_address))
152       .WillOnce(testing::Return(CreateSession(
153           &dispatcher_, 1, client_address, &session1_)));
154   ProcessPacket(client_address, 1, true, "foo");
155   EXPECT_EQ(client_address, dispatcher_.current_client_address());
156   EXPECT_EQ(server_address_, dispatcher_.current_server_address());
157
158
159   EXPECT_CALL(dispatcher_, CreateQuicSession(2, _, client_address))
160       .WillOnce(testing::Return(CreateSession(
161                     &dispatcher_, 2, client_address, &session2_)));
162   ProcessPacket(client_address, 2, true, "bar");
163
164   EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()),
165               ProcessUdpPacket(_, _, _)).Times(1).
166       WillOnce(testing::WithArgs<2>(Invoke(
167           this, &QuicDispatcherTest::ValidatePacket)));
168   ProcessPacket(client_address, 1, false, "eep");
169 }
170
171 TEST_F(QuicDispatcherTest, Shutdown) {
172   IPEndPoint client_address(net::test::Loopback4(), 1);
173
174   EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address))
175       .WillOnce(testing::Return(CreateSession(
176                     &dispatcher_, 1, client_address, &session1_)));
177
178   ProcessPacket(client_address, 1, true, "foo");
179
180   EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()),
181               SendConnectionClose(QUIC_PEER_GOING_AWAY));
182
183   dispatcher_.Shutdown();
184 }
185
186 class MockTimeWaitListManager : public QuicTimeWaitListManager {
187  public:
188   MockTimeWaitListManager(QuicPacketWriter* writer,
189                           QuicServerSessionVisitor* visitor,
190                           EpollServer* eps)
191       : QuicTimeWaitListManager(writer, visitor, eps, QuicSupportedVersions()) {
192   }
193
194   MOCK_METHOD5(ProcessPacket, void(const IPEndPoint& server_address,
195                                    const IPEndPoint& client_address,
196                                    QuicConnectionId connection_id,
197                                    QuicPacketSequenceNumber sequence_number,
198                                    const QuicEncryptedPacket& packet));
199 };
200
201 TEST_F(QuicDispatcherTest, TimeWaitListManager) {
202   MockTimeWaitListManager* time_wait_list_manager =
203       new MockTimeWaitListManager(
204           QuicDispatcherPeer::GetWriter(&dispatcher_), &dispatcher_, &eps_);
205   // dispatcher takes the ownership of time_wait_list_manager.
206   QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_,
207                                              time_wait_list_manager);
208   // Create a new session.
209   IPEndPoint client_address(net::test::Loopback4(), 1);
210   QuicConnectionId connection_id = 1;
211   EXPECT_CALL(dispatcher_, CreateQuicSession(connection_id, _, client_address))
212       .WillOnce(testing::Return(CreateSession(
213                     &dispatcher_, connection_id, client_address, &session1_)));
214   ProcessPacket(client_address, connection_id, true, "foo");
215
216   // Close the connection by sending public reset packet.
217   QuicPublicResetPacket packet;
218   packet.public_header.connection_id = connection_id;
219   packet.public_header.reset_flag = true;
220   packet.public_header.version_flag = false;
221   packet.rejected_sequence_number = 19191;
222   packet.nonce_proof = 132232;
223   scoped_ptr<QuicEncryptedPacket> encrypted(
224       QuicFramer::BuildPublicResetPacket(packet));
225   EXPECT_CALL(*session1_, OnConnectionClosed(QUIC_PUBLIC_RESET, true)).Times(1)
226       .WillOnce(WithoutArgs(Invoke(
227           reinterpret_cast<MockServerConnection*>(session1_->connection()),
228           &MockServerConnection::UnregisterOnConnectionClosed)));
229   EXPECT_CALL(*reinterpret_cast<MockConnection*>(session1_->connection()),
230               ProcessUdpPacket(_, _, _))
231       .WillOnce(Invoke(
232           reinterpret_cast<MockConnection*>(session1_->connection()),
233           &MockConnection::ReallyProcessUdpPacket));
234   dispatcher_.ProcessPacket(IPEndPoint(), client_address, *encrypted);
235   EXPECT_TRUE(time_wait_list_manager->IsConnectionIdInTimeWait(connection_id));
236
237   // Dispatcher forwards subsequent packets for this connection_id to the time
238   // wait list manager.
239   EXPECT_CALL(*time_wait_list_manager,
240               ProcessPacket(_, _, connection_id, _, _)).Times(1);
241   ProcessPacket(client_address, connection_id, true, "foo");
242 }
243
244 TEST_F(QuicDispatcherTest, StrayPacketToTimeWaitListManager) {
245   MockTimeWaitListManager* time_wait_list_manager =
246       new MockTimeWaitListManager(
247           QuicDispatcherPeer::GetWriter(&dispatcher_), &dispatcher_, &eps_);
248   // dispatcher takes the ownership of time_wait_list_manager.
249   QuicDispatcherPeer::SetTimeWaitListManager(&dispatcher_,
250                                              time_wait_list_manager);
251
252   IPEndPoint client_address(net::test::Loopback4(), 1);
253   QuicConnectionId connection_id = 1;
254   // Dispatcher forwards all packets for this connection_id to the time wait
255   // list manager.
256   EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, _)).Times(0);
257   EXPECT_CALL(*time_wait_list_manager,
258               ProcessPacket(_, _, connection_id, _, _)).Times(1);
259   string data = "foo";
260   ProcessPacket(client_address, connection_id, false, "foo");
261 }
262
263 class BlockingWriter : public QuicPacketWriterWrapper {
264  public:
265   BlockingWriter() : write_blocked_(false) {}
266
267   bool IsWriteBlocked() const override { return write_blocked_; }
268   void SetWritable() override { write_blocked_ = false; }
269
270   WriteResult WritePacket(const char* buffer,
271                           size_t buf_len,
272                           const IPAddressNumber& self_client_address,
273                           const IPEndPoint& peer_client_address) override {
274     // It would be quite possible to actually implement this method here with
275     // the fake blocked status, but it would be significantly more work in
276     // Chromium, and since it's not called anyway, don't bother.
277     LOG(DFATAL) << "Not supported";
278     return WriteResult();
279   }
280
281   bool write_blocked_;
282 };
283
284 class QuicDispatcherWriteBlockedListTest : public QuicDispatcherTest {
285  public:
286   void SetUp() override {
287     writer_ = new BlockingWriter;
288     QuicDispatcherPeer::SetPacketWriterFactory(&dispatcher_,
289                                                new TestWriterFactory());
290     QuicDispatcherPeer::UseWriter(&dispatcher_, writer_);
291
292     IPEndPoint client_address(net::test::Loopback4(), 1);
293
294     EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address))
295         .WillOnce(testing::Return(CreateSession(
296                       &dispatcher_, 1, client_address, &session1_)));
297     ProcessPacket(client_address, 1, true, "foo");
298
299     EXPECT_CALL(dispatcher_, CreateQuicSession(_, _, client_address))
300         .WillOnce(testing::Return(CreateSession(
301                       &dispatcher_, 2, client_address, &session2_)));
302     ProcessPacket(client_address, 2, true, "bar");
303
304     blocked_list_ = QuicDispatcherPeer::GetWriteBlockedList(&dispatcher_);
305   }
306
307   void TearDown() override {
308     EXPECT_CALL(*connection1(), SendConnectionClose(QUIC_PEER_GOING_AWAY));
309     EXPECT_CALL(*connection2(), SendConnectionClose(QUIC_PEER_GOING_AWAY));
310     dispatcher_.Shutdown();
311   }
312
313   void SetBlocked() {
314     writer_->write_blocked_ = true;
315   }
316
317   void BlockConnection2() {
318     writer_->write_blocked_ = true;
319     dispatcher_.OnWriteBlocked(connection2());
320   }
321
322  protected:
323   BlockingWriter* writer_;
324   QuicDispatcher::WriteBlockedList* blocked_list_;
325 };
326
327 TEST_F(QuicDispatcherWriteBlockedListTest, BasicOnCanWrite) {
328   // No OnCanWrite calls because no connections are blocked.
329   dispatcher_.OnCanWrite();
330
331   // Register connection 1 for events, and make sure it's notified.
332   SetBlocked();
333   dispatcher_.OnWriteBlocked(connection1());
334   EXPECT_CALL(*connection1(), OnCanWrite());
335   dispatcher_.OnCanWrite();
336
337   // It should get only one notification.
338   EXPECT_CALL(*connection1(), OnCanWrite()).Times(0);
339   dispatcher_.OnCanWrite();
340   EXPECT_FALSE(dispatcher_.HasPendingWrites());
341 }
342
343 TEST_F(QuicDispatcherWriteBlockedListTest, OnCanWriteOrder) {
344   // Make sure we handle events in order.
345   InSequence s;
346   SetBlocked();
347   dispatcher_.OnWriteBlocked(connection1());
348   dispatcher_.OnWriteBlocked(connection2());
349   EXPECT_CALL(*connection1(), OnCanWrite());
350   EXPECT_CALL(*connection2(), OnCanWrite());
351   dispatcher_.OnCanWrite();
352
353   // Check the other ordering.
354   SetBlocked();
355   dispatcher_.OnWriteBlocked(connection2());
356   dispatcher_.OnWriteBlocked(connection1());
357   EXPECT_CALL(*connection2(), OnCanWrite());
358   EXPECT_CALL(*connection1(), OnCanWrite());
359   dispatcher_.OnCanWrite();
360 }
361
362 TEST_F(QuicDispatcherWriteBlockedListTest, OnCanWriteRemove) {
363   // Add and remove one connction.
364   SetBlocked();
365   dispatcher_.OnWriteBlocked(connection1());
366   blocked_list_->erase(connection1());
367   EXPECT_CALL(*connection1(), OnCanWrite()).Times(0);
368   dispatcher_.OnCanWrite();
369
370   // Add and remove one connction and make sure it doesn't affect others.
371   SetBlocked();
372   dispatcher_.OnWriteBlocked(connection1());
373   dispatcher_.OnWriteBlocked(connection2());
374   blocked_list_->erase(connection1());
375   EXPECT_CALL(*connection2(), OnCanWrite());
376   dispatcher_.OnCanWrite();
377
378   // Add it, remove it, and add it back and make sure things are OK.
379   SetBlocked();
380   dispatcher_.OnWriteBlocked(connection1());
381   blocked_list_->erase(connection1());
382   dispatcher_.OnWriteBlocked(connection1());
383   EXPECT_CALL(*connection1(), OnCanWrite()).Times(1);
384   dispatcher_.OnCanWrite();
385 }
386
387 TEST_F(QuicDispatcherWriteBlockedListTest, DoubleAdd) {
388   // Make sure a double add does not necessitate a double remove.
389   SetBlocked();
390   dispatcher_.OnWriteBlocked(connection1());
391   dispatcher_.OnWriteBlocked(connection1());
392   blocked_list_->erase(connection1());
393   EXPECT_CALL(*connection1(), OnCanWrite()).Times(0);
394   dispatcher_.OnCanWrite();
395
396   // Make sure a double add does not result in two OnCanWrite calls.
397   SetBlocked();
398   dispatcher_.OnWriteBlocked(connection1());
399   dispatcher_.OnWriteBlocked(connection1());
400   EXPECT_CALL(*connection1(), OnCanWrite()).Times(1);
401   dispatcher_.OnCanWrite();
402 }
403
404 TEST_F(QuicDispatcherWriteBlockedListTest, OnCanWriteHandleBlock) {
405   // Finally make sure if we write block on a write call, we stop calling.
406   InSequence s;
407   SetBlocked();
408   dispatcher_.OnWriteBlocked(connection1());
409   dispatcher_.OnWriteBlocked(connection2());
410   EXPECT_CALL(*connection1(), OnCanWrite()).WillOnce(
411       Invoke(this, &QuicDispatcherWriteBlockedListTest::SetBlocked));
412   EXPECT_CALL(*connection2(), OnCanWrite()).Times(0);
413   dispatcher_.OnCanWrite();
414
415   // And we'll resume where we left off when we get another call.
416   EXPECT_CALL(*connection2(), OnCanWrite());
417   dispatcher_.OnCanWrite();
418 }
419
420 TEST_F(QuicDispatcherWriteBlockedListTest, LimitedWrites) {
421   // Make sure we call both writers.  The first will register for more writing
422   // but should not be immediately called due to limits.
423   InSequence s;
424   SetBlocked();
425   dispatcher_.OnWriteBlocked(connection1());
426   dispatcher_.OnWriteBlocked(connection2());
427   EXPECT_CALL(*connection1(), OnCanWrite());
428   EXPECT_CALL(*connection2(), OnCanWrite()).WillOnce(
429       Invoke(this, &QuicDispatcherWriteBlockedListTest::BlockConnection2));
430   dispatcher_.OnCanWrite();
431   EXPECT_TRUE(dispatcher_.HasPendingWrites());
432
433   // Now call OnCanWrite again, and connection1 should get its second chance
434   EXPECT_CALL(*connection2(), OnCanWrite());
435   dispatcher_.OnCanWrite();
436   EXPECT_FALSE(dispatcher_.HasPendingWrites());
437 }
438
439 TEST_F(QuicDispatcherWriteBlockedListTest, TestWriteLimits) {
440   // Finally make sure if we write block on a write call, we stop calling.
441   InSequence s;
442   SetBlocked();
443   dispatcher_.OnWriteBlocked(connection1());
444   dispatcher_.OnWriteBlocked(connection2());
445   EXPECT_CALL(*connection1(), OnCanWrite()).WillOnce(
446       Invoke(this, &QuicDispatcherWriteBlockedListTest::SetBlocked));
447   EXPECT_CALL(*connection2(), OnCanWrite()).Times(0);
448   dispatcher_.OnCanWrite();
449   EXPECT_TRUE(dispatcher_.HasPendingWrites());
450
451   // And we'll resume where we left off when we get another call.
452   EXPECT_CALL(*connection2(), OnCanWrite());
453   dispatcher_.OnCanWrite();
454   EXPECT_FALSE(dispatcher_.HasPendingWrites());
455 }
456
457 }  // namespace
458 }  // namespace test
459 }  // namespace tools
460 }  // namespace net