| 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/test_tools/crypto_test_utils.h" | 5 #include "net/quic/test_tools/crypto_test_utils.h" |
| 6 | 6 |
| 7 #include "base/strings/string_piece.h" | 7 #include "base/strings/string_piece.h" |
| 8 #include "net/quic/crypto/crypto_handshake.h" | 8 #include "net/quic/crypto/crypto_handshake.h" |
| 9 #include "net/quic/crypto/crypto_server_config.h" |
| 9 #include "net/quic/crypto/quic_decrypter.h" | 10 #include "net/quic/crypto/quic_decrypter.h" |
| 10 #include "net/quic/crypto/quic_encrypter.h" | 11 #include "net/quic/crypto/quic_encrypter.h" |
| 11 #include "net/quic/crypto/quic_random.h" | 12 #include "net/quic/crypto/quic_random.h" |
| 12 #include "net/quic/quic_clock.h" | 13 #include "net/quic/quic_clock.h" |
| 13 #include "net/quic/quic_crypto_client_stream.h" | 14 #include "net/quic/quic_crypto_client_stream.h" |
| 14 #include "net/quic/quic_crypto_server_stream.h" | 15 #include "net/quic/quic_crypto_server_stream.h" |
| 15 #include "net/quic/quic_crypto_stream.h" | 16 #include "net/quic/quic_crypto_stream.h" |
| 16 #include "net/quic/test_tools/quic_test_utils.h" | 17 #include "net/quic/test_tools/quic_test_utils.h" |
| 17 #include "net/quic/test_tools/simple_quic_framer.h" | 18 #include "net/quic/test_tools/simple_quic_framer.h" |
| 18 | 19 |
| 19 using base::StringPiece; | 20 using base::StringPiece; |
| 20 using std::string; | 21 using std::string; |
| 22 using std::vector; |
| 21 | 23 |
| 22 namespace net { | 24 namespace net { |
| 23 namespace test { | 25 namespace test { |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 class TestSession : public QuicSession { | 29 class TestSession : public QuicSession { |
| 28 public: | 30 public: |
| 29 TestSession(QuicConnection* connection, bool is_server) | 31 TestSession(QuicConnection* connection, bool is_server) |
| 30 : QuicSession(connection, is_server) { | 32 : QuicSession(connection, is_server) { |
| 31 } | 33 } |
| 32 | 34 |
| 33 MOCK_METHOD1(CreateIncomingReliableStream, | 35 MOCK_METHOD1(CreateIncomingReliableStream, |
| 34 ReliableQuicStream*(QuicStreamId id)); | 36 ReliableQuicStream*(QuicStreamId id)); |
| 35 MOCK_METHOD0(GetCryptoStream, QuicCryptoStream*()); | 37 MOCK_METHOD0(GetCryptoStream, QuicCryptoStream*()); |
| 36 MOCK_METHOD0(CreateOutgoingReliableStream, ReliableQuicStream*()); | 38 MOCK_METHOD0(CreateOutgoingReliableStream, ReliableQuicStream*()); |
| 37 }; | 39 }; |
| 38 | 40 |
| 39 // CommunicateHandshakeMessages moves messages from |a| to |b| and back until | 41 // CryptoFramerVisitor is a framer visitor that records handshake messages. |
| 40 // |a|'s handshake has completed. | 42 class CryptoFramerVisitor : public CryptoFramerVisitorInterface { |
| 41 void CommunicateHandshakeMessages( | 43 public: |
| 42 PacketSavingConnection* a_conn, | 44 CryptoFramerVisitor() |
| 43 QuicCryptoStream* a, | 45 : error_(false) { |
| 44 PacketSavingConnection* b_conn, | 46 } |
| 45 QuicCryptoStream* b) { | |
| 46 scoped_ptr<SimpleQuicFramer> framer; | |
| 47 | 47 |
| 48 for (size_t i = 0; !a->handshake_complete(); i++) { | 48 void OnError(CryptoFramer* framer) { |
| 49 framer.reset(new SimpleQuicFramer); | 49 error_ = true; |
| 50 } |
| 50 | 51 |
| 51 ASSERT_LT(i, a_conn->packets_.size()); | 52 void OnHandshakeMessage(const CryptoHandshakeMessage& message) { |
| 52 ASSERT_TRUE(framer->ProcessPacket(*a_conn->packets_[i])); | 53 messages_.push_back(message); |
| 53 ASSERT_EQ(1u, framer->stream_frames().size()); | 54 } |
| 54 | 55 |
| 55 scoped_ptr<CryptoHandshakeMessage> a_msg(framer->HandshakeMessage(0)); | 56 bool error() const { |
| 56 b->OnHandshakeMessage(*(a_msg.get())); | 57 return error_; |
| 58 } |
| 57 | 59 |
| 58 framer.reset(new SimpleQuicFramer); | 60 const vector<CryptoHandshakeMessage>& messages() const { |
| 59 ASSERT_LT(i, b_conn->packets_.size()); | 61 return messages_; |
| 60 ASSERT_TRUE(framer->ProcessPacket(*b_conn->packets_[i])); | 62 } |
| 61 ASSERT_EQ(1u, framer->stream_frames().size()); | |
| 62 | 63 |
| 63 scoped_ptr<CryptoHandshakeMessage> b_msg(framer->HandshakeMessage(0)); | 64 private: |
| 64 a->OnHandshakeMessage(*(b_msg.get())); | 65 bool error_; |
| 66 vector<CryptoHandshakeMessage> messages_; |
| 67 }; |
| 68 |
| 69 // MovePackets parses crypto handshake messages from packet number |
| 70 // |*inout_packet_index| through to the last packet and has |dest_stream| |
| 71 // process them. |*inout_packet_index| is updated with an index one greater |
| 72 // than the last packet processed. |
| 73 void MovePackets(PacketSavingConnection* source_conn, |
| 74 size_t *inout_packet_index, |
| 75 QuicCryptoStream* dest_stream) { |
| 76 SimpleQuicFramer framer; |
| 77 CryptoFramer crypto_framer; |
| 78 CryptoFramerVisitor crypto_visitor; |
| 79 |
| 80 crypto_framer.set_visitor(&crypto_visitor); |
| 81 |
| 82 size_t index = *inout_packet_index; |
| 83 for (; index < source_conn->packets_.size(); index++) { |
| 84 ASSERT_TRUE(framer.ProcessPacket(*source_conn->packets_[index])); |
| 85 for (vector<QuicStreamFrame>::const_iterator |
| 86 i = framer.stream_frames().begin(); |
| 87 i != framer.stream_frames().end(); ++i) { |
| 88 ASSERT_TRUE(crypto_framer.ProcessInput(i->data)); |
| 89 ASSERT_FALSE(crypto_visitor.error()); |
| 90 } |
| 91 } |
| 92 *inout_packet_index = index; |
| 93 |
| 94 ASSERT_EQ(0u, crypto_framer.InputBytesRemaining()); |
| 95 |
| 96 for (vector<CryptoHandshakeMessage>::const_iterator |
| 97 i = crypto_visitor.messages().begin(); |
| 98 i != crypto_visitor.messages().end(); ++i) { |
| 99 dest_stream->OnHandshakeMessage(*i); |
| 65 } | 100 } |
| 66 } | 101 } |
| 67 | 102 |
| 68 } // anonymous namespace | 103 } // anonymous namespace |
| 69 | 104 |
| 70 // static | 105 // static |
| 71 void CryptoTestUtils::HandshakeWithFakeServer( | 106 void CryptoTestUtils::CommunicateHandshakeMessages( |
| 107 PacketSavingConnection* a_conn, |
| 108 QuicCryptoStream* a, |
| 109 PacketSavingConnection* b_conn, |
| 110 QuicCryptoStream* b) { |
| 111 size_t a_i = 0, b_i = 0; |
| 112 while (!a->handshake_complete()) { |
| 113 ASSERT_GT(a_conn->packets_.size(), a_i); |
| 114 LOG(INFO) << "Processing " << a_conn->packets_.size() - a_i |
| 115 << " packets a->b"; |
| 116 MovePackets(a_conn, &a_i, b); |
| 117 |
| 118 ASSERT_GT(b_conn->packets_.size(), b_i); |
| 119 LOG(INFO) << "Processing " << b_conn->packets_.size() - b_i |
| 120 << " packets b->a"; |
| 121 if (b_conn->packets_.size() - b_i == 2) { |
| 122 LOG(INFO) << "here"; |
| 123 } |
| 124 MovePackets(b_conn, &b_i, a); |
| 125 } |
| 126 } |
| 127 |
| 128 // static |
| 129 int CryptoTestUtils::HandshakeWithFakeServer( |
| 72 PacketSavingConnection* client_conn, | 130 PacketSavingConnection* client_conn, |
| 73 QuicCryptoClientStream* client) { | 131 QuicCryptoClientStream* client) { |
| 74 QuicGuid guid(1); | 132 QuicGuid guid(1); |
| 75 IPAddressNumber ip; | 133 IPAddressNumber ip; |
| 76 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); | 134 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
| 77 IPEndPoint addr = IPEndPoint(ip, 1); | 135 IPEndPoint addr = IPEndPoint(ip, 1); |
| 78 PacketSavingConnection* server_conn = | 136 PacketSavingConnection* server_conn = |
| 79 new PacketSavingConnection(guid, addr, true); | 137 new PacketSavingConnection(guid, addr, true); |
| 80 TestSession server_session(server_conn, true); | 138 TestSession server_session(server_conn, true); |
| 81 | 139 |
| 82 QuicConfig config; | 140 QuicConfig config; |
| 83 QuicCryptoServerConfig crypto_config(QuicCryptoServerConfig::TESTING); | 141 QuicCryptoServerConfig crypto_config(QuicCryptoServerConfig::TESTING); |
| 84 SetupCryptoServerConfigForTest( | 142 SetupCryptoServerConfigForTest( |
| 85 server_session.connection()->clock(), | 143 server_session.connection()->clock(), |
| 86 server_session.connection()->random_generator(), | 144 server_session.connection()->random_generator(), |
| 87 &config, &crypto_config); | 145 &config, &crypto_config); |
| 88 | 146 |
| 89 QuicCryptoServerStream server(config, crypto_config, &server_session); | 147 QuicCryptoServerStream server(config, crypto_config, &server_session); |
| 90 | 148 |
| 91 // The client's handshake must have been started already. | 149 // The client's handshake must have been started already. |
| 92 CHECK_NE(0u, client_conn->packets_.size()); | 150 CHECK_NE(0u, client_conn->packets_.size()); |
| 93 | 151 |
| 94 CommunicateHandshakeMessages(client_conn, client, server_conn, &server); | 152 CommunicateHandshakeMessages(client_conn, client, server_conn, &server); |
| 95 | 153 |
| 96 CompareClientAndServerKeys(client, &server); | 154 CompareClientAndServerKeys(client, &server); |
| 155 |
| 156 return client->num_sent_client_hellos(); |
| 97 } | 157 } |
| 98 | 158 |
| 99 // static | 159 // static |
| 100 void CryptoTestUtils::HandshakeWithFakeClient( | 160 int CryptoTestUtils::HandshakeWithFakeClient( |
| 101 PacketSavingConnection* server_conn, | 161 PacketSavingConnection* server_conn, |
| 102 QuicCryptoServerStream* server) { | 162 QuicCryptoServerStream* server) { |
| 103 QuicGuid guid(1); | 163 QuicGuid guid(1); |
| 104 IPAddressNumber ip; | 164 IPAddressNumber ip; |
| 105 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); | 165 CHECK(ParseIPLiteralToNumber("192.0.2.33", &ip)); |
| 106 IPEndPoint addr = IPEndPoint(ip, 1); | 166 IPEndPoint addr = IPEndPoint(ip, 1); |
| 107 PacketSavingConnection* client_conn = | 167 PacketSavingConnection* client_conn = |
| 108 new PacketSavingConnection(guid, addr, false); | 168 new PacketSavingConnection(guid, addr, false); |
| 109 TestSession client_session(client_conn, true); | 169 TestSession client_session(client_conn, true); |
| 110 QuicConfig config; | 170 QuicConfig config; |
| 111 QuicCryptoClientConfig crypto_config; | 171 QuicCryptoClientConfig crypto_config; |
| 112 | 172 |
| 113 config.SetDefaults(); | 173 config.SetDefaults(); |
| 114 crypto_config.SetDefaults(); | 174 crypto_config.SetDefaults(); |
| 115 QuicCryptoClientStream client("test.example.com", config, &client_session, | 175 QuicCryptoClientStream client("test.example.com", config, &client_session, |
| 116 &crypto_config); | 176 &crypto_config); |
| 117 | 177 |
| 118 CHECK(client.CryptoConnect()); | 178 CHECK(client.CryptoConnect()); |
| 119 CHECK_EQ(1u, client_conn->packets_.size()); | 179 CHECK_EQ(1u, client_conn->packets_.size()); |
| 120 | 180 |
| 121 CommunicateHandshakeMessages(client_conn, &client, server_conn, server); | 181 CommunicateHandshakeMessages(client_conn, &client, server_conn, server); |
| 122 | 182 |
| 123 CompareClientAndServerKeys(&client, server); | 183 CompareClientAndServerKeys(&client, server); |
| 184 |
| 185 return client.num_sent_client_hellos(); |
| 124 } | 186 } |
| 125 | 187 |
| 126 // static | 188 // static |
| 127 void CryptoTestUtils::SetupCryptoServerConfigForTest( | 189 void CryptoTestUtils::SetupCryptoServerConfigForTest( |
| 128 const QuicClock* clock, | 190 const QuicClock* clock, |
| 129 QuicRandom* rand, | 191 QuicRandom* rand, |
| 130 QuicConfig* config, | 192 QuicConfig* config, |
| 131 QuicCryptoServerConfig* crypto_config) { | 193 QuicCryptoServerConfig* crypto_config) { |
| 132 config->SetDefaults(); | 194 config->SetDefaults(); |
| 133 CryptoHandshakeMessage extra_tags; | 195 CryptoHandshakeMessage extra_tags; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 client_decrypter_key.data(), | 247 client_decrypter_key.data(), |
| 186 client_decrypter_key.length()); | 248 client_decrypter_key.length()); |
| 187 CompareCharArraysWithHexError("server write IV", | 249 CompareCharArraysWithHexError("server write IV", |
| 188 server_encrypter_iv.data(), | 250 server_encrypter_iv.data(), |
| 189 server_encrypter_iv.length(), | 251 server_encrypter_iv.length(), |
| 190 client_decrypter_iv.data(), | 252 client_decrypter_iv.data(), |
| 191 client_decrypter_iv.length()); | 253 client_decrypter_iv.length()); |
| 192 } | 254 } |
| 193 } // namespace test | 255 } // namespace test |
| 194 } // namespace net | 256 } // namespace net |
| OLD | NEW |