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/tools/quic/quic_client_session.h" | 5 #include "net/tools/quic/quic_simple_client_session.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "net/base/ip_endpoint.h" | 9 #include "net/base/ip_endpoint.h" |
10 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" | 10 #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" |
11 #include "net/quic/quic_flags.h" | 11 #include "net/quic/quic_flags.h" |
12 #include "net/quic/test_tools/crypto_test_utils.h" | 12 #include "net/quic/test_tools/crypto_test_utils.h" |
13 #include "net/quic/test_tools/quic_session_peer.h" | 13 #include "net/quic/test_tools/quic_session_peer.h" |
14 #include "net/quic/test_tools/quic_test_utils.h" | 14 #include "net/quic/test_tools/quic_test_utils.h" |
15 #include "net/tools/quic/quic_spdy_client_stream.h" | 15 #include "net/tools/quic/quic_simple_client_stream.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 using net::test::CryptoTestUtils; | 18 using net::test::CryptoTestUtils; |
19 using net::test::DefaultQuicConfig; | 19 using net::test::DefaultQuicConfig; |
20 using net::test::MockConnection; | 20 using net::test::MockConnection; |
21 using net::test::PacketSavingConnection; | 21 using net::test::PacketSavingConnection; |
22 using net::test::QuicSessionPeer; | 22 using net::test::QuicSessionPeer; |
23 using net::test::SupportedVersions; | 23 using net::test::SupportedVersions; |
24 using net::test::TestPeerIPAddress; | 24 using net::test::TestPeerIPAddress; |
25 using net::test::ValueRestore; | 25 using net::test::ValueRestore; |
26 using net::test::kTestPort; | 26 using net::test::kTestPort; |
27 using testing::Invoke; | 27 using testing::Invoke; |
28 using testing::_; | 28 using testing::_; |
29 | 29 |
30 namespace net { | 30 namespace net { |
31 namespace tools { | 31 namespace tools { |
32 namespace test { | 32 namespace test { |
33 namespace { | 33 namespace { |
34 | 34 |
35 const char kServerHostname[] = "www.example.org"; | 35 const char kServerHostname[] = "www.example.org"; |
36 const uint16 kPort = 80; | 36 const uint16 kPort = 80; |
37 | 37 |
38 class ToolsQuicClientSessionTest | 38 class QuicSimpleClientSessionTest |
39 : public ::testing::TestWithParam<QuicVersion> { | 39 : public ::testing::TestWithParam<QuicVersion> { |
40 protected: | 40 protected: |
41 ToolsQuicClientSessionTest() | 41 QuicSimpleClientSessionTest() |
42 : connection_(new PacketSavingConnection(Perspective::IS_CLIENT, | 42 : connection_(new PacketSavingConnection(Perspective::IS_CLIENT, |
43 SupportedVersions(GetParam()))) { | 43 SupportedVersions(GetParam()))) { |
44 session_.reset(new QuicClientSession(DefaultQuicConfig(), connection_)); | 44 session_.reset(new QuicSimpleClientSession(DefaultQuicConfig(), |
| 45 connection_)); |
45 session_->InitializeSession( | 46 session_->InitializeSession( |
46 QuicServerId(kServerHostname, kPort, false, PRIVACY_MODE_DISABLED), | 47 QuicServerId(kServerHostname, kPort, false, PRIVACY_MODE_DISABLED), |
47 &crypto_config_); | 48 &crypto_config_); |
48 // Advance the time, because timers do not like uninitialized times. | 49 // Advance the time, because timers do not like uninitialized times. |
49 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); | 50 connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
50 } | 51 } |
51 | 52 |
52 void CompleteCryptoHandshake() { | 53 void CompleteCryptoHandshake() { |
53 session_->CryptoConnect(); | 54 session_->CryptoConnect(); |
54 CryptoTestUtils::HandshakeWithFakeServer( | 55 CryptoTestUtils::HandshakeWithFakeServer( |
55 connection_, session_->GetCryptoStream()); | 56 connection_, session_->GetCryptoStream()); |
56 } | 57 } |
57 | 58 |
58 PacketSavingConnection* connection_; | 59 PacketSavingConnection* connection_; |
59 scoped_ptr<QuicClientSession> session_; | 60 scoped_ptr<QuicSimpleClientSession> session_; |
60 QuicCryptoClientConfig crypto_config_; | 61 QuicCryptoClientConfig crypto_config_; |
61 }; | 62 }; |
62 | 63 |
63 INSTANTIATE_TEST_CASE_P(Tests, ToolsQuicClientSessionTest, | 64 INSTANTIATE_TEST_CASE_P(Tests, QuicSimpleClientSessionTest, |
64 ::testing::ValuesIn(QuicSupportedVersions())); | 65 ::testing::ValuesIn(QuicSupportedVersions())); |
65 | 66 |
66 TEST_P(ToolsQuicClientSessionTest, CryptoConnect) { | 67 TEST_P(QuicSimpleClientSessionTest, CryptoConnect) { |
67 CompleteCryptoHandshake(); | 68 CompleteCryptoHandshake(); |
68 } | 69 } |
69 | 70 |
70 TEST_P(ToolsQuicClientSessionTest, MaxNumStreams) { | 71 TEST_P(QuicSimpleClientSessionTest, MaxNumStreams) { |
71 session_->config()->SetMaxStreamsPerConnection(1, 1); | 72 session_->config()->SetMaxStreamsPerConnection(1, 1); |
72 | 73 |
73 // Initialize crypto before the client session will create a stream. | 74 // Initialize crypto before the client session will create a stream. |
74 CompleteCryptoHandshake(); | 75 CompleteCryptoHandshake(); |
75 | 76 |
76 QuicSpdyClientStream* stream = session_->CreateOutgoingDataStream(); | 77 QuicSimpleClientStream* stream = session_->CreateOutgoingDataStream(); |
77 ASSERT_TRUE(stream); | 78 ASSERT_TRUE(stream); |
78 EXPECT_FALSE(session_->CreateOutgoingDataStream()); | 79 EXPECT_FALSE(session_->CreateOutgoingDataStream()); |
79 | 80 |
80 // Close a stream and ensure I can now open a new one. | 81 // Close a stream and ensure I can now open a new one. |
81 session_->CloseStream(stream->id()); | 82 session_->CloseStream(stream->id()); |
82 stream = session_->CreateOutgoingDataStream(); | 83 stream = session_->CreateOutgoingDataStream(); |
83 EXPECT_TRUE(stream); | 84 EXPECT_TRUE(stream); |
84 } | 85 } |
85 | 86 |
86 TEST_P(ToolsQuicClientSessionTest, GoAwayReceived) { | 87 TEST_P(QuicSimpleClientSessionTest, GoAwayReceived) { |
87 CompleteCryptoHandshake(); | 88 CompleteCryptoHandshake(); |
88 | 89 |
89 // After receiving a GoAway, I should no longer be able to create outgoing | 90 // After receiving a GoAway, I should no longer be able to create outgoing |
90 // streams. | 91 // streams. |
91 session_->OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away.")); | 92 session_->OnGoAway(QuicGoAwayFrame(QUIC_PEER_GOING_AWAY, 1u, "Going away.")); |
92 EXPECT_EQ(nullptr, session_->CreateOutgoingDataStream()); | 93 EXPECT_EQ(nullptr, session_->CreateOutgoingDataStream()); |
93 } | 94 } |
94 | 95 |
95 TEST_P(ToolsQuicClientSessionTest, SetFecProtectionFromConfig) { | 96 TEST_P(QuicSimpleClientSessionTest, SetFecProtectionFromConfig) { |
96 ValueRestore<bool> old_flag(&FLAGS_enable_quic_fec, true); | 97 ValueRestore<bool> old_flag(&FLAGS_enable_quic_fec, true); |
97 | 98 |
98 // Set FEC config in client's connection options. | 99 // Set FEC config in client's connection options. |
99 QuicTagVector copt; | 100 QuicTagVector copt; |
100 copt.push_back(kFHDR); | 101 copt.push_back(kFHDR); |
101 session_->config()->SetConnectionOptionsToSend(copt); | 102 session_->config()->SetConnectionOptionsToSend(copt); |
102 | 103 |
103 // Doing the handshake should set up FEC config correctly. | 104 // Doing the handshake should set up FEC config correctly. |
104 CompleteCryptoHandshake(); | 105 CompleteCryptoHandshake(); |
105 | 106 |
106 // Verify that headers stream is always protected and data streams are | 107 // Verify that headers stream is always protected and data streams are |
107 // optionally protected. | 108 // optionally protected. |
108 EXPECT_EQ(FEC_PROTECT_ALWAYS, | 109 EXPECT_EQ(FEC_PROTECT_ALWAYS, |
109 QuicSessionPeer::GetHeadersStream(session_.get())->fec_policy()); | 110 QuicSessionPeer::GetHeadersStream(session_.get())->fec_policy()); |
110 QuicSpdyClientStream* stream = session_->CreateOutgoingDataStream(); | 111 QuicSimpleClientStream* stream = session_->CreateOutgoingDataStream(); |
111 ASSERT_TRUE(stream); | 112 ASSERT_TRUE(stream); |
112 EXPECT_EQ(FEC_PROTECT_OPTIONAL, stream->fec_policy()); | 113 EXPECT_EQ(FEC_PROTECT_OPTIONAL, stream->fec_policy()); |
113 } | 114 } |
114 | 115 |
115 // Regression test for b/17206611. | 116 // Regression test for b/17206611. |
116 TEST_P(ToolsQuicClientSessionTest, InvalidPacketReceived) { | 117 TEST_P(QuicSimpleClientSessionTest, InvalidPacketReceived) { |
117 // Create Packet with 0 length. | 118 // Create Packet with 0 length. |
118 QuicEncryptedPacket invalid_packet(nullptr, 0, false); | 119 QuicEncryptedPacket invalid_packet(nullptr, 0, false); |
119 IPEndPoint server_address(TestPeerIPAddress(), kTestPort); | 120 IPEndPoint server_address(TestPeerIPAddress(), kTestPort); |
120 IPEndPoint client_address(TestPeerIPAddress(), kTestPort); | 121 IPEndPoint client_address(TestPeerIPAddress(), kTestPort); |
121 | 122 |
122 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session_->connection()), | 123 EXPECT_CALL(*reinterpret_cast<MockConnection*>(session_->connection()), |
123 ProcessUdpPacket(server_address, client_address, _)) | 124 ProcessUdpPacket(server_address, client_address, _)) |
124 .WillRepeatedly( | 125 .WillRepeatedly( |
125 Invoke(reinterpret_cast<MockConnection*>(session_->connection()), | 126 Invoke(reinterpret_cast<MockConnection*>(session_->connection()), |
126 &MockConnection::ReallyProcessUdpPacket)); | 127 &MockConnection::ReallyProcessUdpPacket)); |
127 | 128 |
128 // Validate that empty packets don't close the connection. | 129 // Validate that empty packets don't close the connection. |
129 EXPECT_CALL(*connection_, SendConnectionCloseWithDetails(_, _)).Times(0); | 130 EXPECT_CALL(*connection_, SendConnectionCloseWithDetails(_, _)).Times(0); |
130 session_->connection()->ProcessUdpPacket(client_address, server_address, | 131 session_->connection()->ProcessUdpPacket(client_address, server_address, |
131 invalid_packet); | 132 invalid_packet); |
132 | 133 |
133 // Verifiy that small, invalid packets don't close the connection. | 134 // Verifiy that small, invalid packets don't close the connection. |
134 char buf[2] = {0x00, 0x01}; | 135 char buf[2] = {0x00, 0x01}; |
135 QuicEncryptedPacket valid_packet(buf, 2, false); | 136 QuicEncryptedPacket valid_packet(buf, 2, false); |
136 // Close connection shouldn't be called. | 137 // Close connection shouldn't be called. |
137 EXPECT_CALL(*connection_, SendConnectionCloseWithDetails(_, _)).Times(0); | 138 EXPECT_CALL(*connection_, SendConnectionCloseWithDetails(_, _)).Times(0); |
138 session_->connection()->ProcessUdpPacket(client_address, server_address, | 139 session_->connection()->ProcessUdpPacket(client_address, server_address, |
139 valid_packet); | 140 valid_packet); |
140 } | 141 } |
141 | 142 |
142 } // namespace | 143 } // namespace |
143 } // namespace test | 144 } // namespace test |
144 } // namespace tools | 145 } // namespace tools |
145 } // namespace net | 146 } // namespace net |
OLD | NEW |