├── .gitignore ├── AUTHORS ├── ChangeLog ├── LICENSE ├── Makefile ├── README ├── docs ├── QUICDesignDocumentandSpecificationRational.odt ├── QUICDesignDocumentandSpecificationRational.pdf ├── QUICFAQforGeeks.odt ├── QUICFAQforGeeks.pdf └── slides-88-tsvarea-10.pdf └── src ├── TODO ├── net ├── congestion_control │ ├── available_channel_estimator.cc │ ├── available_channel_estimator.h │ ├── available_channel_estimator_test.cc │ ├── channel_estimator.cc │ ├── channel_estimator.h │ ├── channel_estimator_test.cc │ ├── cube_root.cc │ ├── cube_root.h │ ├── cube_root_test.cc │ ├── cubic.cc │ ├── cubic.h │ ├── cubic_test.cc │ ├── fix_rate_receiver.cc │ ├── fix_rate_receiver.h │ ├── fix_rate_sender.cc │ ├── fix_rate_sender.h │ ├── fix_rate_test.cc │ ├── hybrid_slow_start.cc │ ├── hybrid_slow_start.h │ ├── hybrid_slow_start_test.cc │ ├── inter_arrival_bitrate_ramp_up.cc │ ├── inter_arrival_bitrate_ramp_up.h │ ├── inter_arrival_bitrate_ramp_up_test.cc │ ├── inter_arrival_overuse_detector.cc │ ├── inter_arrival_overuse_detector.h │ ├── inter_arrival_overuse_detector_test.cc │ ├── inter_arrival_probe.cc │ ├── inter_arrival_probe.h │ ├── inter_arrival_probe_test.cc │ ├── inter_arrival_receiver.cc │ ├── inter_arrival_receiver.h │ ├── inter_arrival_receiver_test.cc │ ├── inter_arrival_sender.cc │ ├── inter_arrival_sender.h │ ├── inter_arrival_sender_test.cc │ ├── inter_arrival_state_machine.cc │ ├── inter_arrival_state_machine.h │ ├── inter_arrival_state_machine_test.cc │ ├── leaky_bucket.cc │ ├── leaky_bucket.h │ ├── leaky_bucket_test.cc │ ├── paced_sender.cc │ ├── paced_sender.h │ ├── paced_sender_test.cc │ ├── pacing_sender.cc │ ├── pacing_sender.h │ ├── pacing_sender_test.cc │ ├── quic_max_sized_map.h │ ├── quic_max_sized_map_test.cc │ ├── receive_algorithm_interface.cc │ ├── receive_algorithm_interface.h │ ├── send_algorithm_interface.cc │ ├── send_algorithm_interface.h │ ├── tcp_cubic_sender.cc │ ├── tcp_cubic_sender.h │ ├── tcp_cubic_sender_test.cc │ ├── tcp_receiver.cc │ ├── tcp_receiver.h │ └── tcp_receiver_test.cc ├── crypto │ ├── aes_128_gcm_12_decrypter.h │ ├── aes_128_gcm_12_decrypter_nss.cc │ ├── aes_128_gcm_12_decrypter_openssl.cc │ ├── aes_128_gcm_12_decrypter_test.cc │ ├── aes_128_gcm_12_encrypter.h │ ├── aes_128_gcm_12_encrypter_nss.cc │ ├── aes_128_gcm_12_encrypter_openssl.cc │ ├── aes_128_gcm_12_encrypter_test.cc │ ├── cert_compressor.cc │ ├── cert_compressor.h │ ├── cert_compressor_test.cc │ ├── channel_id.cc │ ├── channel_id.h │ ├── channel_id_nss.cc │ ├── channel_id_openssl.cc │ ├── channel_id_test.cc │ ├── common_cert_set.cc │ ├── common_cert_set.h │ ├── common_cert_set_0.c │ ├── common_cert_set_1_50.inc │ ├── common_cert_set_51_100.inc │ ├── common_cert_set_test.cc │ ├── crypto_framer.cc │ ├── crypto_framer.h │ ├── crypto_framer_test.cc │ ├── crypto_handshake.cc │ ├── crypto_handshake.h │ ├── crypto_handshake_message.cc │ ├── crypto_handshake_message.h │ ├── crypto_protocol.h │ ├── crypto_secret_boxer.cc │ ├── crypto_secret_boxer.h │ ├── crypto_secret_boxer_test.cc │ ├── crypto_server_config_protobuf.cc │ ├── crypto_server_config_protobuf.h │ ├── crypto_server_test.cc │ ├── crypto_utils.cc │ ├── crypto_utils.h │ ├── crypto_utils_test.cc │ ├── curve25519_key_exchange.cc │ ├── curve25519_key_exchange.h │ ├── curve25519_key_exchange_test.cc │ ├── ephemeral_key_source.h │ ├── key_exchange.h │ ├── local_strike_register_client.cc │ ├── local_strike_register_client.h │ ├── local_strike_register_client_test.cc │ ├── null_decrypter.cc │ ├── null_decrypter.h │ ├── null_decrypter_test.cc │ ├── null_encrypter.cc │ ├── null_encrypter.h │ ├── null_encrypter_test.cc │ ├── p256_key_exchange.h │ ├── p256_key_exchange_nss.cc │ ├── p256_key_exchange_openssl.cc │ ├── p256_key_exchange_test.cc │ ├── proof_source.h │ ├── proof_source_chromium.cc │ ├── proof_source_chromium.h │ ├── proof_test.cc │ ├── proof_verifier.cc │ ├── proof_verifier.h │ ├── proof_verifier_chromium.cc │ ├── proof_verifier_chromium.h │ ├── quic_crypto_client_config.cc │ ├── quic_crypto_client_config.h │ ├── quic_crypto_client_config_test.cc │ ├── quic_crypto_server_config.cc │ ├── quic_crypto_server_config.h │ ├── quic_crypto_server_config_test.cc │ ├── quic_decrypter.cc │ ├── quic_decrypter.h │ ├── quic_encrypter.cc │ ├── quic_encrypter.h │ ├── quic_random.cc │ ├── quic_random.h │ ├── quic_random_test.cc │ ├── quic_server_info.cc │ ├── quic_server_info.h │ ├── scoped_evp_aead_ctx.cc │ ├── scoped_evp_aead_ctx.h │ ├── source_address_token.cc │ ├── source_address_token.h │ ├── strike_register.cc │ ├── strike_register.h │ ├── strike_register_client.h │ └── strike_register_test.cc ├── iovector.cc ├── iovector.h ├── iovector_test.cc ├── port_suggester.cc ├── port_suggester.h ├── port_suggester_unittest.cc ├── quic_ack_notifier.cc ├── quic_ack_notifier.h ├── quic_ack_notifier_manager.cc ├── quic_ack_notifier_manager.h ├── quic_ack_notifier_test.cc ├── quic_address_mismatch.cc ├── quic_address_mismatch.h ├── quic_address_mismatch_test.cc ├── quic_alarm.cc ├── quic_alarm.h ├── quic_alarm_test.cc ├── quic_bandwidth.cc ├── quic_bandwidth.h ├── quic_bandwidth_test.cc ├── quic_blocked_writer_interface.h ├── quic_client_session.cc ├── quic_client_session.h ├── quic_client_session_test.cc ├── quic_clock.cc ├── quic_clock.h ├── quic_clock_test.cc ├── quic_config.cc ├── quic_config.h ├── quic_config_test.cc ├── quic_connection.cc ├── quic_connection.h ├── quic_connection_helper.cc ├── quic_connection_helper.h ├── quic_connection_helper_test.cc ├── quic_connection_logger.cc ├── quic_connection_logger.h ├── quic_connection_stats.cc ├── quic_connection_stats.h ├── quic_connection_test.cc ├── quic_crypto_client_stream.cc ├── quic_crypto_client_stream.h ├── quic_crypto_client_stream_factory.h ├── quic_crypto_client_stream_test.cc ├── quic_crypto_server_stream.cc ├── quic_crypto_server_stream.h ├── quic_crypto_server_stream_test.cc ├── quic_crypto_stream.cc ├── quic_crypto_stream.h ├── quic_crypto_stream_test.cc ├── quic_data_reader.cc ├── quic_data_reader.h ├── quic_data_stream.cc ├── quic_data_stream.h ├── quic_data_stream_test.cc ├── quic_data_writer.cc ├── quic_data_writer.h ├── quic_data_writer_test.cc ├── quic_default_packet_writer.cc ├── quic_default_packet_writer.h ├── quic_end_to_end_unittest.cc ├── quic_fec_group.cc ├── quic_fec_group.h ├── quic_fec_group_test.cc ├── quic_framer.cc ├── quic_framer.h ├── quic_framer_test.cc ├── quic_headers_stream.cc ├── quic_headers_stream.h ├── quic_headers_stream_test.cc ├── quic_http_stream.cc ├── quic_http_stream.h ├── quic_http_stream_test.cc ├── quic_http_utils.cc ├── quic_http_utils.h ├── quic_http_utils_test.cc ├── quic_network_transaction_unittest.cc ├── quic_packet_creator.cc ├── quic_packet_creator.h ├── quic_packet_creator_test.cc ├── quic_packet_generator.cc ├── quic_packet_generator.h ├── quic_packet_generator_test.cc ├── quic_packet_writer.h ├── quic_protocol.cc ├── quic_protocol.h ├── quic_protocol_test.cc ├── quic_received_packet_manager.cc ├── quic_received_packet_manager.h ├── quic_received_packet_manager_test.cc ├── quic_reliable_client_stream.cc ├── quic_reliable_client_stream.h ├── quic_reliable_client_stream_test.cc ├── quic_sent_entropy_manager.cc ├── quic_sent_entropy_manager.h ├── quic_sent_entropy_manager_test.cc ├── quic_sent_packet_manager.cc ├── quic_sent_packet_manager.h ├── quic_sent_packet_manager_test.cc ├── quic_session.cc ├── quic_session.h ├── quic_session_test.cc ├── quic_socket_address_coder.cc ├── quic_socket_address_coder.h ├── quic_socket_address_coder_test.cc ├── quic_spdy_compressor.cc ├── quic_spdy_compressor.h ├── quic_spdy_compressor_test.cc ├── quic_spdy_decompressor.cc ├── quic_spdy_decompressor.h ├── quic_spdy_decompressor_test.cc ├── quic_stream_factory.cc ├── quic_stream_factory.h ├── quic_stream_factory_test.cc ├── quic_stream_sequencer.cc ├── quic_stream_sequencer.h ├── quic_stream_sequencer_test.cc ├── quic_time.cc ├── quic_time.h ├── quic_time_test.cc ├── quic_unacked_packet_map.cc ├── quic_unacked_packet_map.h ├── quic_utils.cc ├── quic_utils.h ├── quic_utils_chromium.h ├── quic_utils_chromium_test.cc ├── quic_utils_test.cc ├── quic_write_blocked_list.cc ├── quic_write_blocked_list.h ├── quic_write_blocked_list_test.cc ├── reliable_quic_stream.cc ├── reliable_quic_stream.h ├── reliable_quic_stream_test.cc ├── spdy_utils.cc ├── spdy_utils.h └── test_tools │ ├── crypto_test_utils.cc │ ├── crypto_test_utils.h │ ├── crypto_test_utils_chromium.cc │ ├── crypto_test_utils_nss.cc │ ├── crypto_test_utils_openssl.cc │ ├── delayed_verify_strike_register_client.cc │ ├── delayed_verify_strike_register_client.h │ ├── mock_clock.cc │ ├── mock_clock.h │ ├── mock_crypto_client_stream.cc │ ├── mock_crypto_client_stream.h │ ├── mock_crypto_client_stream_factory.cc │ ├── mock_crypto_client_stream_factory.h │ ├── mock_random.cc │ ├── mock_random.h │ ├── quic_client_session_peer.cc │ ├── quic_client_session_peer.h │ ├── quic_connection_peer.cc │ ├── quic_connection_peer.h │ ├── quic_data_stream_peer.cc │ ├── quic_data_stream_peer.h │ ├── quic_framer_peer.cc │ ├── quic_framer_peer.h │ ├── quic_packet_creator_peer.cc │ ├── quic_packet_creator_peer.h │ ├── quic_received_packet_manager_peer.cc │ ├── quic_received_packet_manager_peer.h │ ├── quic_sent_packet_manager_peer.cc │ ├── quic_sent_packet_manager_peer.h │ ├── quic_session_peer.cc │ ├── quic_session_peer.h │ ├── quic_test_packet_maker.cc │ ├── quic_test_packet_maker.h │ ├── quic_test_utils.cc │ ├── quic_test_utils.h │ ├── reliable_quic_stream_peer.cc │ ├── reliable_quic_stream_peer.h │ ├── simple_quic_framer.cc │ ├── simple_quic_framer.h │ ├── test_task_runner.cc │ └── test_task_runner.h └── tools ├── benchmark ├── run_client.py └── test_urls.json ├── end_to_end_test.cc ├── quic_client.cc ├── quic_client.h ├── quic_client_bin.cc ├── quic_client_session.cc ├── quic_client_session.h ├── quic_client_session_test.cc ├── quic_default_packet_writer.cc ├── quic_default_packet_writer.h ├── quic_dispatcher.cc ├── quic_dispatcher.h ├── quic_dispatcher_test.cc ├── quic_epoll_clock.cc ├── quic_epoll_clock.h ├── quic_epoll_clock_test.cc ├── quic_epoll_connection_helper.cc ├── quic_epoll_connection_helper.h ├── quic_epoll_connection_helper_test.cc ├── quic_in_memory_cache.cc ├── quic_in_memory_cache.h ├── quic_in_memory_cache_test.cc ├── quic_packet_writer_wrapper.cc ├── quic_packet_writer_wrapper.h ├── quic_server.cc ├── quic_server.h ├── quic_server_bin.cc ├── quic_server_session.cc ├── quic_server_session.h ├── quic_server_session_test.cc ├── quic_server_test.cc ├── quic_socket_utils.cc ├── quic_socket_utils.h ├── quic_spdy_client_stream.cc ├── quic_spdy_client_stream.h ├── quic_spdy_client_stream_test.cc ├── quic_spdy_server_stream.cc ├── quic_spdy_server_stream.h ├── quic_spdy_server_stream_test.cc ├── quic_time_wait_list_manager.cc ├── quic_time_wait_list_manager.h ├── quic_time_wait_list_manager_test.cc ├── spdy_utils.cc ├── spdy_utils.h └── test_tools ├── http_message_test_utils.cc ├── http_message_test_utils.h ├── mock_epoll_server.cc ├── mock_epoll_server.h ├── mock_quic_dispatcher.cc ├── mock_quic_dispatcher.h ├── packet_dropping_test_writer.cc ├── packet_dropping_test_writer.h ├── quic_client_peer.cc ├── quic_client_peer.h ├── quic_dispatcher_peer.cc ├── quic_dispatcher_peer.h ├── quic_in_memory_cache_peer.cc ├── quic_in_memory_cache_peer.h ├── quic_server_peer.cc ├── quic_server_peer.h ├── quic_test_client.cc ├── quic_test_client.h ├── quic_test_utils.cc ├── quic_test_utils.h ├── run_all_unittests.cc ├── server_thread.cc └── server_thread.h /ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/conght/quic/e490015d310d2db5f4686905e85be61240556c18/ChangeLog -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | // Copyright 2014 The Chromium Authors. All rights reserved. 2 | // 3 | // Redistribution and use in source and binary forms, with or without 4 | // modification, are permitted provided that the following conditions are 5 | // met: 6 | // 7 | // * Redistributions of source code must retain the above copyright 8 | // notice, this list of conditions and the following disclaimer. 9 | // * Redistributions in binary form must reproduce the above 10 | // copyright notice, this list of conditions and the following disclaimer 11 | // in the documentation and/or other materials provided with the 12 | // distribution. 13 | // * Neither the name of Google Inc. nor the names of its 14 | // contributors may be used to endorse or promote products derived from 15 | // this software without specific prior written permission. 16 | // 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/conght/quic/e490015d310d2db5f4686905e85be61240556c18/Makefile -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | QUIC (Quick UDP Internet Connections) 2 | 3 | QUIC is an early-stage network protocol [Jim Roskind ] 4 | is experimenting with that runs a stream multiplexing protocol over a 5 | new flavor of Transport Layer Security (TLS) on top of UDP instead of 6 | TCP. QUIC combines a carefully selected collection of techniques to 7 | reduce the number of round trips we need as we surf the Internet. You 8 | can learn more in the design document, but here are some of the 9 | highlights: 10 | 11 | * High security similar to TLS 12 | * Fast (often 0-RTT) connectivity similar to TLS Snapstart combined 13 | with TCP Fast Open 14 | * Packet pacing to reduce packet loss 15 | * Packet error correction to reduce retransmission latency 16 | * UDP transport to avoid TCP head-of-line blocking 17 | * A connection identifier to reduce reconnections for mobile clients 18 | * A pluggable congestion control mechanism 19 | 20 | This is "libquic", a user-space library that re-uses/packages/purposes 21 | the QUIC code from the Chromium browser project so that other software 22 | can use this new protocol for Internet layer-4 communications. QUIC 23 | is a general purpose protocol built primarily with HTTP in mind, but 24 | there is no reason not to use it as a replacement for TCP in other 25 | non-HTTP based connections. 26 | 27 | QUIC Discussions: 28 | proto-quic@chromium.org 29 | https://groups.google.com/a/chromium.org/d/forum/proto-quic 30 | 31 | 32 | [1] http://blog.chromium.org/2013/06/experimenting-with-quic.html 33 | [2] https://docs.google.com/document/d/1RNHkx_VvKWyWg6Lr8SZ-saqsQx7rFV-ev2jRFUoVD34/preview?sle=true 34 | [3] https://docs.google.com/document/d/1lmL9EF6qKrk7gbazY8bIdvq3Pno2Xj_l_YShP40GLQE/edit#heading=h.h3jsxme7rovm 35 | -------------------------------------------------------------------------------- /docs/QUICDesignDocumentandSpecificationRational.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/conght/quic/e490015d310d2db5f4686905e85be61240556c18/docs/QUICDesignDocumentandSpecificationRational.odt -------------------------------------------------------------------------------- /docs/QUICDesignDocumentandSpecificationRational.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/conght/quic/e490015d310d2db5f4686905e85be61240556c18/docs/QUICDesignDocumentandSpecificationRational.pdf -------------------------------------------------------------------------------- /docs/QUICFAQforGeeks.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/conght/quic/e490015d310d2db5f4686905e85be61240556c18/docs/QUICFAQforGeeks.odt -------------------------------------------------------------------------------- /docs/QUICFAQforGeeks.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/conght/quic/e490015d310d2db5f4686905e85be61240556c18/docs/QUICFAQforGeeks.pdf -------------------------------------------------------------------------------- /docs/slides-88-tsvarea-10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/conght/quic/e490015d310d2db5f4686905e85be61240556c18/docs/slides-88-tsvarea-10.pdf -------------------------------------------------------------------------------- /src/TODO: -------------------------------------------------------------------------------- 1 | 2 | 3 | * integrate missing files: 4 | grep -s "#include \"" */* | grep -v "net/quic" | grep -v "net/tools/quic" | awk '{print $2}' | sort -u 5 | "base/at_exit.h" 6 | "base/basictypes.h" 7 | "base/command_line.h" 8 | "base/compiler_specific.h" 9 | "base/containers/hash_tables.h" 10 | "base/debug/stack_trace.h" 11 | "base/file_util.h" 12 | "base/files/file_enumerator.h" 13 | "base/files/file_path.h" 14 | "base/logging.h" 15 | "base/memory/ref_counted.h" 16 | "base/memory/scoped_ptr.h" 17 | "base/memory/singleton.h" 18 | "base/path_service.h" 19 | "base/sha1.h" 20 | "base/stl_util.h" 21 | "base/strings/string_number_conversions.h" 22 | "base/strings/string_piece.h" 23 | "base/strings/string_util.h" 24 | "base/synchronization/waitable_event.h" 25 | "net/base/host_port_pair.h" 26 | "net/base/io_buffer.h" 27 | "net/base/iovec.h" 28 | "net/base/ip_endpoint.h" 29 | "net/base/linked_hash_map.h" 30 | "net/base/net_export.h" 31 | "net/spdy/spdy_frame_builder.h" 32 | "net/spdy/spdy_framer.h" 33 | "net/spdy/spdy_header_block.h" 34 | "net/spdy/spdy_protocol.h" 35 | "net/test/gtest_util.h" 36 | "net/tools/balsa/balsa_frame.h" 37 | "net/tools/balsa/balsa_headers.h" 38 | "net/tools/balsa/noop_balsa_visitor.h" 39 | "net/tools/epoll_server/epoll_server.h" 40 | "testing/gmock/include/gmock/gmock.h" 41 | "testing/gtest/include/gtest/gtest.h" 42 | "url/gurl.h" 43 | * autoconf/automake 44 | * test 45 | -------------------------------------------------------------------------------- /src/net/congestion_control/channel_estimator.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | // Based on the inter arrival time of the received packets relative to the time 6 | // those packets where sent we can estimate the max capacity of the channel. 7 | // We can only use packet pair that are sent out faster than the acctual 8 | // channel capacity. 9 | #ifndef NET_QUIC_CONGESTION_CONTROL_CHANNEL_ESTIMATOR_H_ 10 | #define NET_QUIC_CONGESTION_CONTROL_CHANNEL_ESTIMATOR_H_ 11 | 12 | #include 13 | #include 14 | 15 | #include "base/basictypes.h" 16 | #include "net/base/net_export.h" 17 | #include "net/quic/congestion_control/quic_max_sized_map.h" 18 | #include "net/quic/quic_bandwidth.h" 19 | #include "net/quic/quic_protocol.h" 20 | #include "net/quic/quic_time.h" 21 | 22 | namespace net { 23 | 24 | enum ChannelEstimateState { 25 | kChannelEstimateUnknown = 0, 26 | kChannelEstimateUncertain = 1, 27 | kChannelEstimateGood = 2 28 | }; 29 | 30 | class NET_EXPORT_PRIVATE ChannelEstimator { 31 | public: 32 | ChannelEstimator(); 33 | ~ChannelEstimator(); 34 | 35 | // This method should be called each time we acquire a receive time for a 36 | // packet we previously sent. It calculates deltas between consecutive 37 | // receive times, and may use that to update the channel bandwidth estimate. 38 | void OnAcknowledgedPacket(QuicPacketSequenceNumber sequence_number, 39 | QuicByteCount packet_size, 40 | QuicTime send_time, 41 | QuicTime receive_time); 42 | 43 | // Get the current estimated state and channel capacity. 44 | // Note: estimate will not be valid when kChannelEstimateUnknown is returned. 45 | ChannelEstimateState GetChannelEstimate(QuicBandwidth* estimate) const; 46 | 47 | private: 48 | void UpdateFilter(QuicTime::Delta received_delta, QuicByteCount size_delta, 49 | QuicPacketSequenceNumber sequence_number); 50 | 51 | QuicPacketSequenceNumber last_sequence_number_; 52 | QuicTime last_send_time_; 53 | QuicTime last_receive_time_; 54 | QuicMaxSizedMap 55 | sorted_bitrate_estimates_; 56 | 57 | DISALLOW_COPY_AND_ASSIGN(ChannelEstimator); 58 | }; 59 | 60 | } // namespace net 61 | #endif // NET_QUIC_CONGESTION_CONTROL_CHANNEL_ESTIMATOR_H_ 62 | -------------------------------------------------------------------------------- /src/net/congestion_control/cube_root.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CONGESTION_CONTROL_CUBE_ROOT_H_ 6 | #define NET_QUIC_CONGESTION_CONTROL_CUBE_ROOT_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "net/base/net_export.h" 10 | 11 | namespace net { 12 | 13 | class NET_EXPORT_PRIVATE CubeRoot { 14 | public: 15 | // Calculates the cube root using a table lookup followed by one Newton- 16 | // Raphson iteration. 17 | static uint32 Root(uint64 a); 18 | }; 19 | 20 | } // namespace net 21 | #endif // NET_QUIC_CONGESTION_CONTROL_CUBE_ROOT_H_ 22 | -------------------------------------------------------------------------------- /src/net/congestion_control/cube_root_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 "base/basictypes.h" 6 | #include "net/quic/congestion_control/cube_root.h" 7 | #include "testing/gtest/include/gtest/gtest.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | class CubeRootTest : public ::testing::Test { 13 | protected: 14 | CubeRootTest() { 15 | } 16 | }; 17 | 18 | TEST_F(CubeRootTest, LowRoot) { 19 | for (uint32 i = 1; i < 256; ++i) { 20 | uint64 cube = i * i * i; 21 | uint8 cube_root = CubeRoot::Root(cube); 22 | EXPECT_EQ(i, cube_root); 23 | } 24 | } 25 | 26 | TEST_F(CubeRootTest, HighRoot) { 27 | // Test the range we will opperate in, 1300 to 130 000. 28 | // We expect some loss in accuracy, accepting +-0.2%. 29 | for (uint64 i = 1300; i < 20000; i += 100) { 30 | uint64 cube = i * i * i; 31 | uint32 cube_root = CubeRoot::Root(cube); 32 | uint32 margin = cube_root >> 9; // Calculate 0.2% roughly by 33 | // dividing by 512. 34 | EXPECT_LE(i - margin, cube_root); 35 | EXPECT_GE(i + margin, cube_root); 36 | } 37 | for (uint64 i = 20000; i < 130000; i *= 2) { 38 | uint64 cube = i * i * i; 39 | uint32 cube_root = CubeRoot::Root(cube); 40 | uint32 margin = cube_root >> 9; 41 | EXPECT_LE(i - margin, cube_root); 42 | EXPECT_GE(i + margin, cube_root); 43 | } 44 | } 45 | 46 | } // namespace test 47 | } // namespace net 48 | -------------------------------------------------------------------------------- /src/net/congestion_control/fix_rate_receiver.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/quic/congestion_control/fix_rate_receiver.h" 6 | 7 | #include "base/basictypes.h" 8 | #include "net/quic/congestion_control/receive_algorithm_interface.h" 9 | 10 | namespace { 11 | static const int kInitialBitrate = 100000; // In bytes per second. 12 | } 13 | 14 | namespace net { 15 | 16 | FixRateReceiver::FixRateReceiver() 17 | : configured_rate_(QuicBandwidth::FromBytesPerSecond(kInitialBitrate)) { 18 | } 19 | 20 | bool FixRateReceiver::GenerateCongestionFeedback( 21 | QuicCongestionFeedbackFrame* feedback) { 22 | feedback->type = kFixRate; 23 | feedback->fix_rate.bitrate = configured_rate_; 24 | return true; 25 | } 26 | 27 | void FixRateReceiver::RecordIncomingPacket( 28 | QuicByteCount /*bytes*/, 29 | QuicPacketSequenceNumber /*sequence_number*/, 30 | QuicTime /*timestamp*/) { 31 | // Nothing to do for this simple implementation. 32 | } 33 | 34 | } // namespace net 35 | -------------------------------------------------------------------------------- /src/net/congestion_control/fix_rate_receiver.h: -------------------------------------------------------------------------------- 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 | // Fix rate receive side congestion control, used for initial testing. 6 | 7 | #ifndef NET_QUIC_CONGESTION_CONTROL_FIX_RATE_RECEIVER_H_ 8 | #define NET_QUIC_CONGESTION_CONTROL_FIX_RATE_RECEIVER_H_ 9 | 10 | #include "base/basictypes.h" 11 | #include "base/compiler_specific.h" 12 | #include "net/base/net_export.h" 13 | #include "net/quic/congestion_control/receive_algorithm_interface.h" 14 | #include "net/quic/quic_bandwidth.h" 15 | 16 | namespace net { 17 | 18 | namespace test { 19 | class FixRateReceiverPeer; 20 | } // namespace test 21 | 22 | class NET_EXPORT_PRIVATE FixRateReceiver : public ReceiveAlgorithmInterface { 23 | public: 24 | FixRateReceiver(); 25 | 26 | // Implements ReceiveAlgorithmInterface. 27 | virtual bool GenerateCongestionFeedback( 28 | QuicCongestionFeedbackFrame* feedback) OVERRIDE; 29 | 30 | // Implements ReceiveAlgorithmInterface. 31 | virtual void RecordIncomingPacket(QuicByteCount bytes, 32 | QuicPacketSequenceNumber sequence_number, 33 | QuicTime timestamp) OVERRIDE; 34 | private: 35 | friend class test::FixRateReceiverPeer; 36 | 37 | QuicBandwidth configured_rate_; 38 | 39 | DISALLOW_COPY_AND_ASSIGN(FixRateReceiver); 40 | }; 41 | 42 | } // namespace net 43 | #endif // NET_QUIC_CONGESTION_CONTROL_FIX_RATE_RECEIVER_H_ 44 | -------------------------------------------------------------------------------- /src/net/congestion_control/inter_arrival_receiver.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/congestion_control/inter_arrival_receiver.h" 6 | 7 | #include "base/basictypes.h" 8 | 9 | namespace net { 10 | 11 | InterArrivalReceiver::InterArrivalReceiver() { 12 | } 13 | 14 | InterArrivalReceiver::~InterArrivalReceiver() { 15 | } 16 | 17 | bool InterArrivalReceiver::GenerateCongestionFeedback( 18 | QuicCongestionFeedbackFrame* feedback) { 19 | if (received_packet_times_.size() <= 1) { 20 | // Don't waste resources by sending a feedback frame for only one packet. 21 | return false; 22 | } 23 | feedback->type = kInterArrival; 24 | 25 | // Copy our current receive set to our feedback message, we will not resend 26 | // this data if it is lost. 27 | feedback->inter_arrival.received_packet_times = received_packet_times_; 28 | 29 | // Prepare for the next set of arriving packets by clearing our current set. 30 | received_packet_times_.clear(); 31 | return true; 32 | } 33 | 34 | void InterArrivalReceiver::RecordIncomingPacket( 35 | QuicByteCount /*bytes*/, 36 | QuicPacketSequenceNumber sequence_number, 37 | QuicTime timestamp) { 38 | received_packet_times_.insert(std::make_pair(sequence_number, timestamp)); 39 | } 40 | 41 | } // namespace net 42 | -------------------------------------------------------------------------------- /src/net/congestion_control/inter_arrival_receiver.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CONGESTION_CONTROL_INTER_ARRIVAL_RECEIVER_H_ 6 | #define NET_QUIC_CONGESTION_CONTROL_INTER_ARRIVAL_RECEIVER_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "base/compiler_specific.h" 10 | #include "net/base/net_export.h" 11 | #include "net/quic/congestion_control/receive_algorithm_interface.h" 12 | #include "net/quic/quic_clock.h" 13 | #include "net/quic/quic_protocol.h" 14 | 15 | namespace net { 16 | 17 | class NET_EXPORT_PRIVATE InterArrivalReceiver 18 | : public ReceiveAlgorithmInterface { 19 | public: 20 | InterArrivalReceiver(); 21 | virtual ~InterArrivalReceiver(); 22 | 23 | // Start implementation of ReceiveAlgorithmInterface. 24 | virtual bool GenerateCongestionFeedback( 25 | QuicCongestionFeedbackFrame* feedback) OVERRIDE; 26 | 27 | virtual void RecordIncomingPacket(QuicByteCount bytes, 28 | QuicPacketSequenceNumber sequence_number, 29 | QuicTime timestamp) OVERRIDE; 30 | // End implementation of ReceiveAlgorithmInterface. 31 | 32 | private: 33 | // The set of received packets since the last feedback was sent, along with 34 | // their arrival times. 35 | TimeMap received_packet_times_; 36 | 37 | DISALLOW_COPY_AND_ASSIGN(InterArrivalReceiver); 38 | }; 39 | 40 | } // namespace net 41 | 42 | #endif // NET_QUIC_CONGESTION_CONTROL_INTER_ARRIVAL_RECEIVER_H_ 43 | -------------------------------------------------------------------------------- /src/net/congestion_control/inter_arrival_receiver_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 "base/logging.h" 6 | #include "net/quic/congestion_control/inter_arrival_receiver.h" 7 | #include "net/quic/test_tools/mock_clock.h" 8 | #include "testing/gtest/include/gtest/gtest.h" 9 | 10 | namespace net { 11 | namespace test { 12 | 13 | class InterArrivalReceiverTest : public ::testing::Test { 14 | protected: 15 | InterArrivalReceiver receiver_; 16 | MockClock clock_; 17 | }; 18 | 19 | TEST_F(InterArrivalReceiverTest, SimpleReceiver) { 20 | QuicTime start = clock_.ApproximateNow(); 21 | QuicTime::Delta received_delta = QuicTime::Delta::FromMilliseconds(10); 22 | clock_.AdvanceTime(received_delta); 23 | QuicTime receive_timestamp = clock_.ApproximateNow(); 24 | receiver_.RecordIncomingPacket(1, 1, receive_timestamp); 25 | 26 | QuicCongestionFeedbackFrame feedback; 27 | ASSERT_FALSE(receiver_.GenerateCongestionFeedback(&feedback)); 28 | 29 | clock_.AdvanceTime(received_delta); 30 | receive_timestamp = clock_.ApproximateNow(); 31 | // Packet not received; but rather revived by FEC. 32 | receiver_.RecordIncomingPacket(1, 2, receive_timestamp); 33 | clock_.AdvanceTime(received_delta); 34 | receive_timestamp = clock_.ApproximateNow(); 35 | receiver_.RecordIncomingPacket(1, 3, receive_timestamp); 36 | 37 | ASSERT_TRUE(receiver_.GenerateCongestionFeedback(&feedback)); 38 | 39 | EXPECT_EQ(kInterArrival, feedback.type); 40 | EXPECT_EQ(3u, feedback.inter_arrival.received_packet_times.size()); 41 | TimeMap::iterator it = feedback.inter_arrival.received_packet_times.begin(); 42 | EXPECT_EQ(1u, it->first); 43 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(10), it->second.Subtract(start)); 44 | it = feedback.inter_arrival.received_packet_times.begin(); 45 | it++; 46 | EXPECT_EQ(2u, it->first); 47 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(20), it->second.Subtract(start)); 48 | it++; 49 | EXPECT_EQ(3u, it->first); 50 | EXPECT_EQ(QuicTime::Delta::FromMilliseconds(30), it->second.Subtract(start)); 51 | } 52 | 53 | } // namespace test 54 | } // namespace net 55 | -------------------------------------------------------------------------------- /src/net/congestion_control/leaky_bucket.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/quic/congestion_control/leaky_bucket.h" 6 | 7 | #include "base/time/time.h" 8 | 9 | namespace net { 10 | 11 | LeakyBucket::LeakyBucket(QuicBandwidth draining_rate) 12 | : bytes_(0), 13 | time_last_updated_(QuicTime::Zero()), 14 | draining_rate_(draining_rate) { 15 | } 16 | 17 | void LeakyBucket::SetDrainingRate(QuicTime now, QuicBandwidth draining_rate) { 18 | Update(now); 19 | draining_rate_ = draining_rate; 20 | } 21 | 22 | void LeakyBucket::Add(QuicTime now, QuicByteCount bytes) { 23 | Update(now); 24 | bytes_ += bytes; 25 | } 26 | 27 | QuicTime::Delta LeakyBucket::TimeRemaining(QuicTime now) { 28 | Update(now); 29 | return QuicTime::Delta::FromMicroseconds( 30 | (bytes_ * base::Time::kMicrosecondsPerSecond) / 31 | draining_rate_.ToBytesPerSecond()); 32 | } 33 | 34 | QuicByteCount LeakyBucket::BytesPending(QuicTime now) { 35 | Update(now); 36 | return bytes_; 37 | } 38 | 39 | void LeakyBucket::Update(QuicTime now) { 40 | QuicTime::Delta elapsed_time = now.Subtract(time_last_updated_); 41 | QuicByteCount bytes_cleared = draining_rate_.ToBytesPerPeriod(elapsed_time); 42 | if (bytes_cleared >= bytes_) { 43 | bytes_ = 0; 44 | } else { 45 | bytes_ -= bytes_cleared; 46 | } 47 | time_last_updated_ = now; 48 | } 49 | 50 | } // namespace net 51 | -------------------------------------------------------------------------------- /src/net/congestion_control/leaky_bucket.h: -------------------------------------------------------------------------------- 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 | // Helper class to track the rate data can leave the buffer for pacing. 6 | // A leaky bucket drains the data at a constant rate regardless of fullness of 7 | // the buffer. 8 | // See http://en.wikipedia.org/wiki/Leaky_bucket for more details. 9 | 10 | #ifndef NET_QUIC_CONGESTION_CONTROL_LEAKY_BUCKET_H_ 11 | #define NET_QUIC_CONGESTION_CONTROL_LEAKY_BUCKET_H_ 12 | 13 | #include "base/basictypes.h" 14 | #include "net/base/net_export.h" 15 | #include "net/quic/quic_bandwidth.h" 16 | #include "net/quic/quic_protocol.h" 17 | #include "net/quic/quic_time.h" 18 | 19 | namespace net { 20 | 21 | class NET_EXPORT_PRIVATE LeakyBucket { 22 | public: 23 | explicit LeakyBucket(QuicBandwidth draining_rate); 24 | 25 | // Set the rate at which the bytes leave the buffer. 26 | void SetDrainingRate(QuicTime now, QuicBandwidth draining_rate); 27 | 28 | // Add data to the buffer. 29 | void Add(QuicTime now, QuicByteCount bytes); 30 | 31 | // Time until the buffer is empty. 32 | QuicTime::Delta TimeRemaining(QuicTime now); 33 | 34 | // Number of bytes in the buffer. 35 | QuicByteCount BytesPending(QuicTime now); 36 | 37 | private: 38 | void Update(QuicTime now); 39 | 40 | QuicByteCount bytes_; 41 | QuicTime time_last_updated_; 42 | QuicBandwidth draining_rate_; 43 | 44 | DISALLOW_COPY_AND_ASSIGN(LeakyBucket); 45 | }; 46 | 47 | } // namespace net 48 | 49 | #endif // NET_QUIC_CONGESTION_CONTROL_LEAKY_BUCKET_H_ 50 | -------------------------------------------------------------------------------- /src/net/congestion_control/paced_sender.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/quic/congestion_control/paced_sender.h" 6 | 7 | #include 8 | 9 | #include "net/quic/quic_protocol.h" 10 | 11 | namespace net { 12 | 13 | // To prevent too aggressive pacing we allow the following packet burst size. 14 | const int64 kMinPacketBurstSize = 2; 15 | // Max estimated time between calls to TimeUntilSend and 16 | // AvailableCongestionWindow. 17 | const int64 kMaxSchedulingDelayUs = 2000; 18 | 19 | PacedSender::PacedSender(QuicBandwidth estimate, QuicByteCount max_segment_size) 20 | : leaky_bucket_(estimate), 21 | pace_(estimate), 22 | max_segment_size_(kDefaultMaxPacketSize) { 23 | } 24 | 25 | void PacedSender::set_max_segment_size(QuicByteCount max_segment_size) { 26 | max_segment_size_ = max_segment_size; 27 | } 28 | 29 | void PacedSender::UpdateBandwidthEstimate(QuicTime now, 30 | QuicBandwidth estimate) { 31 | leaky_bucket_.SetDrainingRate(now, estimate); 32 | pace_ = estimate; 33 | } 34 | 35 | void PacedSender::OnPacketSent(QuicTime now, QuicByteCount bytes) { 36 | leaky_bucket_.Add(now, bytes); 37 | } 38 | 39 | QuicTime::Delta PacedSender::TimeUntilSend(QuicTime now, 40 | QuicTime::Delta time_until_send) { 41 | if (time_until_send.ToMicroseconds() >= kMaxSchedulingDelayUs) { 42 | return time_until_send; 43 | } 44 | // Pace the data. 45 | QuicByteCount pacing_window = pace_.ToBytesPerPeriod( 46 | QuicTime::Delta::FromMicroseconds(kMaxSchedulingDelayUs)); 47 | QuicByteCount min_window_size = kMinPacketBurstSize * max_segment_size_; 48 | pacing_window = std::max(pacing_window, min_window_size); 49 | 50 | if (pacing_window > leaky_bucket_.BytesPending(now)) { 51 | // We have not filled our pacing window yet. 52 | return time_until_send; 53 | } 54 | return leaky_bucket_.TimeRemaining(now); 55 | } 56 | 57 | } // namespace net 58 | -------------------------------------------------------------------------------- /src/net/congestion_control/paced_sender.h: -------------------------------------------------------------------------------- 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 | // Helper class that limits the congestion window to pace the packets. 6 | 7 | #ifndef NET_QUIC_CONGESTION_CONTROL_PACED_SENDER_H_ 8 | #define NET_QUIC_CONGESTION_CONTROL_PACED_SENDER_H_ 9 | 10 | #include "base/basictypes.h" 11 | #include "net/base/net_export.h" 12 | #include "net/quic/congestion_control/leaky_bucket.h" 13 | #include "net/quic/quic_bandwidth.h" 14 | #include "net/quic/quic_time.h" 15 | 16 | namespace net { 17 | 18 | class NET_EXPORT_PRIVATE PacedSender { 19 | public: 20 | PacedSender(QuicBandwidth bandwidth_estimate, QuicByteCount max_segment_size); 21 | 22 | void set_max_segment_size(QuicByteCount max_segment_size); 23 | 24 | // The estimated bandidth from the congestion algorithm changed. 25 | void UpdateBandwidthEstimate(QuicTime now, QuicBandwidth bandwidth_estimate); 26 | 27 | // A packet of size bytes was sent. 28 | void OnPacketSent(QuicTime now, QuicByteCount bytes); 29 | 30 | // Return time until we can send based on the pacing. 31 | QuicTime::Delta TimeUntilSend(QuicTime now, QuicTime::Delta time_until_send); 32 | 33 | private: 34 | // Helper object to track the rate data can leave the buffer for pacing. 35 | LeakyBucket leaky_bucket_; 36 | QuicBandwidth pace_; 37 | QuicByteCount max_segment_size_; 38 | 39 | DISALLOW_COPY_AND_ASSIGN(PacedSender); 40 | }; 41 | 42 | } // namespace net 43 | 44 | #endif // NET_QUIC_CONGESTION_CONTROL_PACED_SENDER_H_ 45 | -------------------------------------------------------------------------------- /src/net/congestion_control/quic_max_sized_map.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | // Simple max sized map. Automatically deletes the oldest element when the 6 | // max limit is reached. 7 | // Note: the ConstIterator will NOT be valid after an Insert or RemoveAll. 8 | #ifndef NET_QUIC_CONGESTION_CONTROL_QUIC_MAX_SIZED_MAP_H_ 9 | #define NET_QUIC_CONGESTION_CONTROL_QUIC_MAX_SIZED_MAP_H_ 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | #include "base/basictypes.h" 17 | 18 | namespace net { 19 | 20 | template 21 | class QuicMaxSizedMap { 22 | public: 23 | typedef typename std::multimap::const_iterator ConstIterator; 24 | 25 | explicit QuicMaxSizedMap(size_t max_numer_of_items) 26 | : max_numer_of_items_(max_numer_of_items) { 27 | } 28 | 29 | size_t MaxSize() const { 30 | return max_numer_of_items_; 31 | } 32 | 33 | size_t Size() const { 34 | return table_.size(); 35 | } 36 | 37 | void Insert(const Key& k, const Value& value) { 38 | if (Size() == MaxSize()) { 39 | ListIterator list_it = insert_order_.begin(); 40 | table_.erase(*list_it); 41 | insert_order_.pop_front(); 42 | } 43 | TableIterator it = table_.insert(std::pair(k, value)); 44 | insert_order_.push_back(it); 45 | } 46 | 47 | void RemoveAll() { 48 | table_.clear(); 49 | insert_order_.clear(); 50 | } 51 | 52 | // STL style const_iterator support. 53 | ConstIterator Find(const Key& k) const { 54 | return table_.find(k); 55 | } 56 | 57 | ConstIterator Begin() const { 58 | return ConstIterator(table_.begin()); 59 | } 60 | 61 | ConstIterator End() const { 62 | return ConstIterator(table_.end()); 63 | } 64 | 65 | private: 66 | typedef typename std::multimap::iterator TableIterator; 67 | typedef typename std::list::iterator ListIterator; 68 | 69 | const size_t max_numer_of_items_; 70 | std::multimap table_; 71 | std::list insert_order_; 72 | 73 | DISALLOW_COPY_AND_ASSIGN(QuicMaxSizedMap); 74 | }; 75 | 76 | } // namespace net 77 | #endif // NET_QUIC_CONGESTION_CONTROL_QUIC_MAX_SIZED_MAP_H_ 78 | -------------------------------------------------------------------------------- /src/net/congestion_control/quic_max_sized_map_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 "base/logging.h" 6 | #include "net/quic/congestion_control/quic_max_sized_map.h" 7 | #include "testing/gtest/include/gtest/gtest.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | class QuicMaxSizedMapTest : public ::testing::Test { 13 | }; 14 | 15 | TEST_F(QuicMaxSizedMapTest, Basic) { 16 | QuicMaxSizedMap test_map(100); 17 | EXPECT_EQ(100u, test_map.MaxSize()); 18 | EXPECT_EQ(0u, test_map.Size()); 19 | test_map.Insert(1, 2); 20 | test_map.Insert(1, 3); 21 | EXPECT_EQ(100u, test_map.MaxSize()); 22 | EXPECT_EQ(2u, test_map.Size()); 23 | test_map.RemoveAll(); 24 | EXPECT_EQ(100u, test_map.MaxSize()); 25 | EXPECT_EQ(0u, test_map.Size()); 26 | } 27 | 28 | TEST_F(QuicMaxSizedMapTest, Find) { 29 | QuicMaxSizedMap test_map(100); 30 | test_map.Insert(1, 2); 31 | test_map.Insert(1, 3); 32 | test_map.Insert(2, 4); 33 | test_map.Insert(3, 5); 34 | QuicMaxSizedMap::ConstIterator it = test_map.Find(2); 35 | EXPECT_TRUE(it != test_map.End()); 36 | EXPECT_EQ(4, it->second); 37 | it = test_map.Find(1); 38 | EXPECT_TRUE(it != test_map.End()); 39 | EXPECT_EQ(2, it->second); 40 | ++it; 41 | EXPECT_TRUE(it != test_map.End()); 42 | EXPECT_EQ(3, it->second); 43 | } 44 | 45 | TEST_F(QuicMaxSizedMapTest, Sort) { 46 | QuicMaxSizedMap test_map(100); 47 | test_map.Insert(9, 9); 48 | test_map.Insert(8, 8); 49 | test_map.Insert(7, 7); 50 | test_map.Insert(6, 6); 51 | test_map.Insert(2, 2); 52 | test_map.Insert(4, 4); 53 | test_map.Insert(5, 5); 54 | test_map.Insert(3, 3); 55 | test_map.Insert(0, 0); 56 | test_map.Insert(1, 1); 57 | QuicMaxSizedMap::ConstIterator it = test_map.Begin(); 58 | for (int i = 0; i < 10; ++i, ++it) { 59 | EXPECT_TRUE(it != test_map.End()); 60 | EXPECT_EQ(i, it->first); 61 | EXPECT_EQ(i, it->second); 62 | } 63 | } 64 | 65 | } // namespace test 66 | } // namespace net 67 | -------------------------------------------------------------------------------- /src/net/congestion_control/receive_algorithm_interface.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/quic/congestion_control/receive_algorithm_interface.h" 6 | 7 | #include "net/quic/congestion_control/fix_rate_receiver.h" 8 | #include "net/quic/congestion_control/inter_arrival_receiver.h" 9 | #include "net/quic/congestion_control/tcp_receiver.h" 10 | 11 | namespace net { 12 | 13 | // Factory for receive side congestion control algorithm. 14 | ReceiveAlgorithmInterface* ReceiveAlgorithmInterface::Create( 15 | CongestionFeedbackType type) { 16 | switch (type) { 17 | case kTCP: 18 | return new TcpReceiver(); 19 | case kInterArrival: 20 | return new InterArrivalReceiver(); 21 | case kFixRate: 22 | return new FixRateReceiver(); 23 | } 24 | return NULL; 25 | } 26 | 27 | } // namespace net 28 | -------------------------------------------------------------------------------- /src/net/congestion_control/receive_algorithm_interface.h: -------------------------------------------------------------------------------- 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 | // The pure virtual class for receive side congestion algorithm. 6 | 7 | #ifndef NET_QUIC_CONGESTION_CONTROL_RECEIVE_ALGORITHM_INTERFACE_H_ 8 | #define NET_QUIC_CONGESTION_CONTROL_RECEIVE_ALGORITHM_INTERFACE_H_ 9 | 10 | #include "base/basictypes.h" 11 | #include "net/base/net_export.h" 12 | #include "net/quic/quic_clock.h" 13 | #include "net/quic/quic_protocol.h" 14 | #include "net/quic/quic_time.h" 15 | 16 | namespace net { 17 | 18 | class NET_EXPORT_PRIVATE ReceiveAlgorithmInterface { 19 | public: 20 | static ReceiveAlgorithmInterface* Create(CongestionFeedbackType type); 21 | 22 | virtual ~ReceiveAlgorithmInterface() {} 23 | 24 | // Returns false if no QuicCongestionFeedbackFrame block is needed. 25 | // Otherwise fills in feedback and return true. 26 | virtual bool GenerateCongestionFeedback( 27 | QuicCongestionFeedbackFrame* feedback) = 0; 28 | 29 | // Should be called for each incoming packet. 30 | // bytes: is the packet size in bytes including IP headers. 31 | // sequence_number: is the unique sequence number from the QUIC packet header. 32 | // timestamp: is the sent timestamp from the QUIC packet header. 33 | virtual void RecordIncomingPacket(QuicByteCount bytes, 34 | QuicPacketSequenceNumber sequence_number, 35 | QuicTime timestamp) = 0; 36 | }; 37 | 38 | } // namespace net 39 | 40 | #endif // NET_QUIC_CONGESTION_CONTROL_RECEIVE_ALGORITHM_INTERFACE_H_ 41 | -------------------------------------------------------------------------------- /src/net/congestion_control/send_algorithm_interface.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/quic/congestion_control/send_algorithm_interface.h" 6 | 7 | #include "net/quic/congestion_control/fix_rate_sender.h" 8 | #include "net/quic/congestion_control/inter_arrival_sender.h" 9 | #include "net/quic/congestion_control/tcp_cubic_sender.h" 10 | #include "net/quic/quic_protocol.h" 11 | 12 | namespace net { 13 | 14 | const bool kUseReno = false; 15 | 16 | // Factory for send side congestion control algorithm. 17 | SendAlgorithmInterface* SendAlgorithmInterface::Create( 18 | const QuicClock* clock, 19 | CongestionFeedbackType type) { 20 | switch (type) { 21 | case kTCP: 22 | return new TcpCubicSender(clock, kUseReno, kMaxTcpCongestionWindow); 23 | case kInterArrival: 24 | return new InterArrivalSender(clock); 25 | case kFixRate: 26 | return new FixRateSender(clock); 27 | } 28 | return NULL; 29 | } 30 | 31 | } // namespace net 32 | -------------------------------------------------------------------------------- /src/net/congestion_control/tcp_receiver.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 "base/basictypes.h" 6 | #include "net/quic/congestion_control/tcp_receiver.h" 7 | 8 | namespace net { 9 | 10 | // Originally 64K bytes, but increased it to 256K to support higher bitrates. 11 | // static 12 | const QuicByteCount TcpReceiver::kReceiveWindowTCP = 256000; 13 | 14 | TcpReceiver::TcpReceiver() 15 | : receive_window_(kReceiveWindowTCP) { 16 | } 17 | 18 | bool TcpReceiver::GenerateCongestionFeedback( 19 | QuicCongestionFeedbackFrame* feedback) { 20 | feedback->type = kTCP; 21 | feedback->tcp.receive_window = receive_window_; 22 | return true; 23 | } 24 | 25 | void TcpReceiver::RecordIncomingPacket(QuicByteCount bytes, 26 | QuicPacketSequenceNumber sequence_number, 27 | QuicTime timestamp) { 28 | } 29 | 30 | } // namespace net 31 | -------------------------------------------------------------------------------- /src/net/congestion_control/tcp_receiver.h: -------------------------------------------------------------------------------- 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 | // TCP receiver side congestion algorithm, emulates the behaviour of TCP. 6 | 7 | #ifndef NET_QUIC_CONGESTION_CONTROL_TCP_RECEIVER_H_ 8 | #define NET_QUIC_CONGESTION_CONTROL_TCP_RECEIVER_H_ 9 | 10 | #include "base/basictypes.h" 11 | #include "base/compiler_specific.h" 12 | #include "net/base/net_export.h" 13 | #include "net/quic/congestion_control/receive_algorithm_interface.h" 14 | #include "net/quic/quic_clock.h" 15 | #include "net/quic/quic_protocol.h" 16 | 17 | namespace net { 18 | 19 | class NET_EXPORT_PRIVATE TcpReceiver : public ReceiveAlgorithmInterface { 20 | public: 21 | TcpReceiver(); 22 | 23 | // Size of the (currently fixed) receive window. 24 | static const QuicByteCount kReceiveWindowTCP; 25 | 26 | // Start implementation of SendAlgorithmInterface. 27 | virtual bool GenerateCongestionFeedback( 28 | QuicCongestionFeedbackFrame* feedback) OVERRIDE; 29 | 30 | virtual void RecordIncomingPacket(QuicByteCount bytes, 31 | QuicPacketSequenceNumber sequence_number, 32 | QuicTime timestamp) OVERRIDE; 33 | 34 | private: 35 | QuicByteCount receive_window_; 36 | 37 | DISALLOW_COPY_AND_ASSIGN(TcpReceiver); 38 | }; 39 | 40 | } // namespace net 41 | #endif // NET_QUIC_CONGESTION_CONTROL_TCP_RECEIVER_H_ 42 | -------------------------------------------------------------------------------- /src/net/congestion_control/tcp_receiver_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 "base/logging.h" 6 | #include "base/memory/scoped_ptr.h" 7 | #include "net/quic/congestion_control/tcp_receiver.h" 8 | #include "net/quic/test_tools/mock_clock.h" 9 | #include "testing/gtest/include/gtest/gtest.h" 10 | 11 | namespace net { 12 | namespace test { 13 | 14 | class QuicTcpReceiverTest : public ::testing::Test { 15 | protected: 16 | virtual void SetUp() { 17 | receiver_.reset(new TcpReceiver()); 18 | } 19 | scoped_ptr receiver_; 20 | }; 21 | 22 | TEST_F(QuicTcpReceiverTest, SimpleReceiver) { 23 | QuicCongestionFeedbackFrame feedback; 24 | QuicTime timestamp(QuicTime::Zero()); 25 | receiver_->RecordIncomingPacket(1, 1, timestamp); 26 | ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback)); 27 | EXPECT_EQ(kTCP, feedback.type); 28 | EXPECT_EQ(256000u, feedback.tcp.receive_window); 29 | receiver_->RecordIncomingPacket(1, 2, timestamp); 30 | ASSERT_TRUE(receiver_->GenerateCongestionFeedback(&feedback)); 31 | EXPECT_EQ(kTCP, feedback.type); 32 | EXPECT_EQ(256000u, feedback.tcp.receive_window); 33 | } 34 | 35 | } // namespace test 36 | } // namespace net 37 | -------------------------------------------------------------------------------- /src/net/crypto/aes_128_gcm_12_decrypter.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CRYPTO_AES_128_GCM_12_DECRYPTER_H_ 6 | #define NET_QUIC_CRYPTO_AES_128_GCM_12_DECRYPTER_H_ 7 | 8 | #include 9 | 10 | #include "base/compiler_specific.h" 11 | #include "net/quic/crypto/quic_decrypter.h" 12 | 13 | #if defined(USE_OPENSSL) 14 | #include "net/quic/crypto/scoped_evp_aead_ctx.h" 15 | #endif 16 | 17 | namespace net { 18 | 19 | namespace test { 20 | class Aes128Gcm12DecrypterPeer; 21 | } // namespace test 22 | 23 | // An Aes128Gcm12Decrypter is a QuicDecrypter that implements the 24 | // AEAD_AES_128_GCM_12 algorithm specified in RFC 5282. Create an instance by 25 | // calling QuicDecrypter::Create(kAESG). 26 | // 27 | // It uses an authentication tag of 12 bytes (96 bits). The fixed prefix 28 | // of the nonce is four bytes. 29 | class NET_EXPORT_PRIVATE Aes128Gcm12Decrypter : public QuicDecrypter { 30 | public: 31 | enum { 32 | // Authentication tags are truncated to 96 bits. 33 | kAuthTagSize = 12, 34 | }; 35 | 36 | Aes128Gcm12Decrypter(); 37 | virtual ~Aes128Gcm12Decrypter(); 38 | 39 | // Returns true if the underlying crypto library supports AES GCM. 40 | static bool IsSupported(); 41 | 42 | // QuicDecrypter implementation 43 | virtual bool SetKey(base::StringPiece key) OVERRIDE; 44 | virtual bool SetNoncePrefix(base::StringPiece nonce_prefix) OVERRIDE; 45 | virtual bool Decrypt(base::StringPiece nonce, 46 | base::StringPiece associated_data, 47 | base::StringPiece ciphertext, 48 | unsigned char* output, 49 | size_t* output_length) OVERRIDE; 50 | virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, 51 | base::StringPiece associated_data, 52 | base::StringPiece ciphertext) OVERRIDE; 53 | virtual base::StringPiece GetKey() const OVERRIDE; 54 | virtual base::StringPiece GetNoncePrefix() const OVERRIDE; 55 | 56 | private: 57 | // The 128-bit AES key. 58 | unsigned char key_[16]; 59 | // The nonce prefix. 60 | unsigned char nonce_prefix_[4]; 61 | 62 | #if defined(USE_OPENSSL) 63 | ScopedEVPAEADCtx ctx_; 64 | #endif 65 | }; 66 | 67 | } // namespace net 68 | 69 | #endif // NET_QUIC_CRYPTO_AES_128_GCM_12_DECRYPTER_H_ 70 | -------------------------------------------------------------------------------- /src/net/crypto/channel_id.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/crypto/channel_id.h" 6 | 7 | namespace net { 8 | 9 | // static 10 | const char ChannelIDVerifier::kContextStr[] = "QUIC ChannelID"; 11 | // static 12 | const char ChannelIDVerifier::kClientToServerStr[] = "client -> server"; 13 | 14 | } // namespace net 15 | -------------------------------------------------------------------------------- /src/net/crypto/common_cert_set.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CRYPTO_COMMON_CERT_SET_H_ 6 | #define NET_QUIC_CRYPTO_COMMON_CERT_SET_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "base/compiler_specific.h" 10 | #include "base/strings/string_piece.h" 11 | #include "net/base/net_export.h" 12 | #include "net/quic/crypto/crypto_protocol.h" 13 | 14 | namespace net { 15 | 16 | // CommonCertSets is an interface to an object that contains a number of common 17 | // certificate sets and can match against them. 18 | class NET_EXPORT_PRIVATE CommonCertSets { 19 | public: 20 | virtual ~CommonCertSets(); 21 | 22 | // GetInstanceQUIC returns the standard QUIC common certificate sets. 23 | static const CommonCertSets* GetInstanceQUIC(); 24 | 25 | // GetCommonHashes returns a StringPiece containing the hashes of common sets 26 | // supported by this object. The 64-bit hashes are concatenated in the 27 | // StringPiece. 28 | virtual base::StringPiece GetCommonHashes() const = 0; 29 | 30 | // GetCert returns a specific certificate (at index |index|) in the common 31 | // set identified by |hash|. If no such certificate is known, an empty 32 | // StringPiece is returned. 33 | virtual base::StringPiece GetCert(uint64 hash, uint32 index) const = 0; 34 | 35 | // MatchCert tries to find |cert| in one of the common certificate sets 36 | // identified by |common_set_hashes|. On success it puts the hash of the 37 | // set in |out_hash|, the index of |cert| in the set in |out_index| and 38 | // returns true. Otherwise it returns false. 39 | virtual bool MatchCert(base::StringPiece cert, 40 | base::StringPiece common_set_hashes, 41 | uint64* out_hash, 42 | uint32* out_index) const = 0; 43 | }; 44 | 45 | } // namespace net 46 | 47 | #endif // NET_QUIC_CRYPTO_COMMON_CERT_SET_H_ 48 | -------------------------------------------------------------------------------- /src/net/crypto/crypto_handshake.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/crypto/crypto_handshake.h" 6 | 7 | #include "net/quic/crypto/common_cert_set.h" 8 | #include "net/quic/crypto/key_exchange.h" 9 | #include "net/quic/crypto/quic_decrypter.h" 10 | #include "net/quic/crypto/quic_encrypter.h" 11 | 12 | namespace net { 13 | 14 | QuicCryptoNegotiatedParameters::QuicCryptoNegotiatedParameters() 15 | : key_exchange(0), 16 | aead(0) { 17 | } 18 | 19 | QuicCryptoNegotiatedParameters::~QuicCryptoNegotiatedParameters() {} 20 | 21 | CrypterPair::CrypterPair() {} 22 | 23 | CrypterPair::~CrypterPair() {} 24 | 25 | // static 26 | const char QuicCryptoConfig::kInitialLabel[] = "QUIC key expansion"; 27 | 28 | // static 29 | const char QuicCryptoConfig::kCETVLabel[] = "QUIC CETV block"; 30 | 31 | // static 32 | const char QuicCryptoConfig::kForwardSecureLabel[] = 33 | "QUIC forward secure key expansion"; 34 | 35 | QuicCryptoConfig::QuicCryptoConfig() 36 | : common_cert_sets(CommonCertSets::GetInstanceQUIC()) { 37 | } 38 | 39 | QuicCryptoConfig::~QuicCryptoConfig() {} 40 | 41 | } // namespace net 42 | -------------------------------------------------------------------------------- /src/net/crypto/crypto_secret_boxer.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ 6 | #define NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ 7 | 8 | #include 9 | 10 | #include "base/strings/string_piece.h" 11 | #include "net/base/net_export.h" 12 | 13 | namespace net { 14 | 15 | class QuicRandom; 16 | 17 | // CryptoSecretBoxer encrypts small chunks of plaintext (called 'boxing') and 18 | // then, later, can authenticate+decrypt the resulting boxes. This object is 19 | // thread-safe. 20 | class NET_EXPORT_PRIVATE CryptoSecretBoxer { 21 | public: 22 | // GetKeySize returns the number of bytes in a key. 23 | static size_t GetKeySize(); 24 | 25 | // SetKey sets the key for this object. This must be done before |Box| or 26 | // |Unbox| are called. |key| must be |GetKeySize()| bytes long. 27 | void SetKey(base::StringPiece key); 28 | 29 | // Box encrypts |plaintext| using a random nonce generated from |rand| and 30 | // returns the resulting ciphertext. Since an authenticator and nonce are 31 | // included, the result will be slightly larger than |plaintext|. 32 | std::string Box(QuicRandom* rand, base::StringPiece plaintext) const; 33 | 34 | // Unbox takes the result of a previous call to |Box| in |ciphertext| and 35 | // authenticates+decrypts it. If |ciphertext| is not authentic then it 36 | // returns false. Otherwise, |out_storage| is used to store the result and 37 | // |out| is set to point into |out_storage| and contains the original 38 | // plaintext. 39 | bool Unbox(base::StringPiece ciphertext, 40 | std::string* out_storage, 41 | base::StringPiece* out) const; 42 | 43 | private: 44 | std::string key_; 45 | }; 46 | 47 | } // namespace net 48 | 49 | #endif // NET_QUIC_CRYPTO_CRYPTO_SECRET_BOXER_H_ 50 | -------------------------------------------------------------------------------- /src/net/crypto/crypto_secret_boxer_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/crypto/crypto_secret_boxer.h" 6 | 7 | #include "base/memory/scoped_ptr.h" 8 | #include "net/quic/crypto/quic_random.h" 9 | #include "testing/gtest/include/gtest/gtest.h" 10 | 11 | using base::StringPiece; 12 | using std::string; 13 | 14 | namespace net { 15 | namespace test { 16 | 17 | TEST(CryptoSecretBoxerTest, BoxAndUnbox) { 18 | StringPiece message("hello world"); 19 | const size_t key_size = CryptoSecretBoxer::GetKeySize(); 20 | scoped_ptr key(new uint8[key_size]); 21 | memset(key.get(), 0x11, key_size); 22 | 23 | CryptoSecretBoxer boxer; 24 | boxer.SetKey(StringPiece(reinterpret_cast(key.get()), key_size)); 25 | 26 | const string box = boxer.Box(QuicRandom::GetInstance(), message); 27 | 28 | string storage; 29 | StringPiece result; 30 | EXPECT_TRUE(boxer.Unbox(box, &storage, &result)); 31 | EXPECT_EQ(result, message); 32 | 33 | EXPECT_FALSE(boxer.Unbox(string(1, 'X') + box, &storage, &result)); 34 | EXPECT_FALSE(boxer.Unbox(box.substr(1, string::npos), &storage, &result)); 35 | EXPECT_FALSE(boxer.Unbox(string(), &storage, &result)); 36 | EXPECT_FALSE(boxer.Unbox(string(1, box[0]^0x80) + box.substr(1, string::npos), 37 | &storage, &result)); 38 | } 39 | 40 | } // namespace test 41 | } // namespace net 42 | -------------------------------------------------------------------------------- /src/net/crypto/crypto_server_config_protobuf.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/crypto/crypto_server_config_protobuf.h" 6 | 7 | #include "base/stl_util.h" 8 | #include "net/quic/quic_time.h" 9 | 10 | namespace net { 11 | 12 | QuicServerConfigProtobuf::QuicServerConfigProtobuf() 13 | : primary_time_(QuicWallTime::Zero().ToUNIXSeconds()), 14 | priority_(0) { 15 | } 16 | 17 | QuicServerConfigProtobuf::~QuicServerConfigProtobuf() { 18 | STLDeleteElements(&keys_); 19 | } 20 | 21 | } // namespace net 22 | -------------------------------------------------------------------------------- /src/net/crypto/crypto_utils_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/crypto/crypto_utils.h" 6 | 7 | #include "testing/gtest/include/gtest/gtest.h" 8 | 9 | namespace net { 10 | namespace test { 11 | namespace { 12 | 13 | TEST(CryptoUtilsTest, IsValidSNI) { 14 | // IP as SNI. 15 | EXPECT_FALSE(CryptoUtils::IsValidSNI("192.168.0.1")); 16 | // SNI without any dot. 17 | EXPECT_FALSE(CryptoUtils::IsValidSNI("somedomain")); 18 | // Invalid RFC2396 hostname 19 | // TODO(rtenneti): Support RFC2396 hostname. 20 | // EXPECT_FALSE(CryptoUtils::IsValidSNI("some_domain.com")); 21 | // An empty string must be invalid otherwise the QUIC client will try sending 22 | // it. 23 | EXPECT_FALSE(CryptoUtils::IsValidSNI("")); 24 | 25 | // Valid SNI 26 | EXPECT_TRUE(CryptoUtils::IsValidSNI("test.google.com")); 27 | } 28 | 29 | TEST(CryptoUtilsTest, NormalizeHostname) { 30 | struct { 31 | const char *input, *expected; 32 | } tests[] = { 33 | { "www.google.com", "www.google.com", }, 34 | { "WWW.GOOGLE.COM", "www.google.com", }, 35 | { "www.google.com.", "www.google.com", }, 36 | { "www.google.COM.", "www.google.com", }, 37 | { "www.google.com..", "www.google.com", }, 38 | { "www.google.com........", "www.google.com", }, 39 | }; 40 | 41 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 42 | EXPECT_EQ(std::string(tests[i].expected), 43 | CryptoUtils::NormalizeHostname(tests[i].input)); 44 | } 45 | } 46 | 47 | } // namespace 48 | } // namespace test 49 | } // namespace net 50 | -------------------------------------------------------------------------------- /src/net/crypto/curve25519_key_exchange.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CRYPTO_CURVE25519_KEY_EXCHANGE_H_ 6 | #define NET_QUIC_CRYPTO_CURVE25519_KEY_EXCHANGE_H_ 7 | 8 | #include 9 | 10 | #include "base/compiler_specific.h" 11 | #include "base/strings/string_piece.h" 12 | #include "net/base/net_export.h" 13 | #include "net/quic/crypto/key_exchange.h" 14 | 15 | namespace net { 16 | 17 | class QuicRandom; 18 | 19 | // Curve25519KeyExchange implements a KeyExchange using elliptic-curve 20 | // Diffie-Hellman on curve25519. See http://cr.yp.to/ecdh.html 21 | class NET_EXPORT_PRIVATE Curve25519KeyExchange : public KeyExchange { 22 | public: 23 | virtual ~Curve25519KeyExchange(); 24 | 25 | // New creates a new object from a private key. If the private key is 26 | // invalid, NULL is returned. 27 | static Curve25519KeyExchange* New(const base::StringPiece& private_key); 28 | 29 | // NewPrivateKey returns a private key, generated from |rand|, suitable for 30 | // passing to |New|. 31 | static std::string NewPrivateKey(QuicRandom* rand); 32 | 33 | // KeyExchange interface. 34 | virtual KeyExchange* NewKeyPair(QuicRandom* rand) const OVERRIDE; 35 | virtual bool CalculateSharedKey(const base::StringPiece& peer_public_value, 36 | std::string* shared_key) const OVERRIDE; 37 | virtual base::StringPiece public_value() const OVERRIDE; 38 | virtual QuicTag tag() const OVERRIDE; 39 | 40 | private: 41 | Curve25519KeyExchange(); 42 | 43 | uint8 private_key_[32]; 44 | uint8 public_key_[32]; 45 | }; 46 | 47 | } // namespace net 48 | 49 | #endif // NET_QUIC_CRYPTO_CURVE25519_KEY_EXCHANGE_H_ 50 | -------------------------------------------------------------------------------- /src/net/crypto/curve25519_key_exchange_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/crypto/curve25519_key_exchange.h" 6 | 7 | #include "base/memory/scoped_ptr.h" 8 | #include "base/strings/string_piece.h" 9 | #include "net/quic/crypto/quic_random.h" 10 | #include "testing/gtest/include/gtest/gtest.h" 11 | 12 | using base::StringPiece; 13 | using std::string; 14 | 15 | namespace net { 16 | namespace test { 17 | 18 | // SharedKey just tests that the basic key exchange identity holds: that both 19 | // parties end up with the same key. 20 | TEST(Curve25519KeyExchange, SharedKey) { 21 | QuicRandom* const rand = QuicRandom::GetInstance(); 22 | 23 | for (int i = 0; i < 5; i++) { 24 | const string alice_key(Curve25519KeyExchange::NewPrivateKey(rand)); 25 | const string bob_key(Curve25519KeyExchange::NewPrivateKey(rand)); 26 | 27 | scoped_ptr alice( 28 | Curve25519KeyExchange::New(alice_key)); 29 | scoped_ptr bob(Curve25519KeyExchange::New(bob_key)); 30 | 31 | const StringPiece alice_public(alice->public_value()); 32 | const StringPiece bob_public(bob->public_value()); 33 | 34 | string alice_shared, bob_shared; 35 | ASSERT_TRUE(alice->CalculateSharedKey(bob_public, &alice_shared)); 36 | ASSERT_TRUE(bob->CalculateSharedKey(alice_public, &bob_shared)); 37 | ASSERT_EQ(alice_shared, bob_shared); 38 | } 39 | } 40 | 41 | } // namespace test 42 | } // namespace net 43 | -------------------------------------------------------------------------------- /src/net/crypto/ephemeral_key_source.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CRYPTO_EPHEMERAL_KEY_SOURCE_H_ 6 | #define NET_QUIC_CRYPTO_EPHEMERAL_KEY_SOURCE_H_ 7 | 8 | #include 9 | 10 | #include "base/strings/string_piece.h" 11 | #include "net/base/net_export.h" 12 | #include "net/quic/quic_time.h" 13 | 14 | namespace net { 15 | 16 | class KeyExchange; 17 | class QuicRandom; 18 | 19 | // EphemeralKeySource manages and rotates ephemeral keys as they can be reused 20 | // for several connections in a short space of time. Since the implementation 21 | // of this may involve locking or thread-local data, this interface abstracts 22 | // that away. 23 | class NET_EXPORT_PRIVATE EphemeralKeySource { 24 | public: 25 | virtual ~EphemeralKeySource() {} 26 | 27 | // CalculateForwardSecureKey generates an ephemeral public/private key pair 28 | // using the algorithm |key_exchange|, sets |*public_value| to the public key 29 | // and returns the shared key between |peer_public_value| and the private 30 | // key. |*public_value| will be sent to the peer to be used with the peer's 31 | // private key. 32 | virtual std::string CalculateForwardSecureKey( 33 | const KeyExchange* key_exchange, 34 | QuicRandom* rand, 35 | QuicTime now, 36 | base::StringPiece peer_public_value, 37 | std::string* public_value) = 0; 38 | }; 39 | 40 | } // namespace net 41 | 42 | #endif // NET_QUIC_CRYPTO_EPHEMERAL_KEY_SOURCE_H_ 43 | -------------------------------------------------------------------------------- /src/net/crypto/key_exchange.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CRYPTO_KEY_EXCHANGE_H_ 6 | #define NET_QUIC_CRYPTO_KEY_EXCHANGE_H_ 7 | 8 | #include 9 | 10 | #include "base/strings/string_piece.h" 11 | #include "net/base/net_export.h" 12 | #include "net/quic/crypto/crypto_protocol.h" 13 | 14 | namespace net { 15 | 16 | class QuicRandom; 17 | 18 | // KeyExchange is an abstract class that provides an interface to a 19 | // key-exchange primitive. 20 | class NET_EXPORT_PRIVATE KeyExchange { 21 | public: 22 | virtual ~KeyExchange() {} 23 | 24 | // NewKeyPair generates a new public, private key pair. The caller takes 25 | // ownership of the return value. (This is intended for servers that need to 26 | // generate forward-secure keys.) 27 | virtual KeyExchange* NewKeyPair(QuicRandom* rand) const = 0; 28 | 29 | // CalculateSharedKey computes the shared key between the local private key 30 | // (which is implicitly known by a KeyExchange object) and a public value 31 | // from the peer. 32 | virtual bool CalculateSharedKey(const base::StringPiece& peer_public_value, 33 | std::string* shared_key) const = 0; 34 | 35 | // public_value returns the local public key which can be sent to a peer in 36 | // order to complete a key exchange. The returned StringPiece is a reference 37 | // to a member of the KeyExchange and is only valid for as long as the 38 | // KeyExchange exists. 39 | virtual base::StringPiece public_value() const = 0; 40 | 41 | // tag returns the tag value that identifies this key exchange function. 42 | virtual QuicTag tag() const = 0; 43 | }; 44 | 45 | } // namespace net 46 | 47 | #endif // NET_QUIC_CRYPTO_KEY_EXCHANGE_H_ 48 | -------------------------------------------------------------------------------- /src/net/crypto/local_strike_register_client.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/crypto/local_strike_register_client.h" 6 | 7 | #include "net/quic/crypto/crypto_protocol.h" 8 | 9 | using base::StringPiece; 10 | using std::string; 11 | 12 | namespace net { 13 | 14 | LocalStrikeRegisterClient::LocalStrikeRegisterClient( 15 | unsigned max_entries, 16 | uint32 current_time_external, 17 | uint32 window_secs, 18 | const uint8 orbit[8], 19 | StrikeRegister::StartupType startup) 20 | : strike_register_(max_entries, current_time_external, window_secs, 21 | orbit, startup) { 22 | } 23 | 24 | bool LocalStrikeRegisterClient::IsKnownOrbit(StringPiece orbit) const { 25 | base::AutoLock lock(m_); 26 | if (orbit.length() != kOrbitSize) { 27 | return false; 28 | } 29 | return memcmp(orbit.data(), strike_register_.orbit(), kOrbitSize) == 0; 30 | } 31 | 32 | void LocalStrikeRegisterClient::VerifyNonceIsValidAndUnique( 33 | StringPiece nonce, QuicWallTime now, ResultCallback* cb) { 34 | bool nonce_is_valid_and_unique; 35 | if (nonce.length() != kNonceSize) { 36 | nonce_is_valid_and_unique = false; 37 | } else { 38 | base::AutoLock lock(m_); 39 | nonce_is_valid_and_unique = strike_register_.Insert( 40 | reinterpret_cast(nonce.data()), 41 | static_cast(now.ToUNIXSeconds())); 42 | } 43 | 44 | // m_ must not be held when the ResultCallback runs. 45 | cb->Run(nonce_is_valid_and_unique); 46 | } 47 | 48 | } // namespace net 49 | -------------------------------------------------------------------------------- /src/net/crypto/local_strike_register_client.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_CRYPTO_LOCAL_STRIKE_REGISTER_CLIENT_H_ 6 | #define NET_QUIC_CRYPTO_LOCAL_STRIKE_REGISTER_CLIENT_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "base/strings/string_piece.h" 10 | #include "base/synchronization/lock.h" 11 | #include "net/base/net_export.h" 12 | #include "net/quic/crypto/strike_register.h" 13 | #include "net/quic/crypto/strike_register_client.h" 14 | #include "net/quic/quic_time.h" 15 | 16 | namespace net { 17 | 18 | // StrikeRegisterClient implementation that wraps a local in-memory 19 | // strike register. 20 | class NET_EXPORT_PRIVATE LocalStrikeRegisterClient 21 | : public StrikeRegisterClient { 22 | public: 23 | LocalStrikeRegisterClient(unsigned max_entries, 24 | uint32 current_time_external, 25 | uint32 window_secs, 26 | const uint8 orbit[8], 27 | StrikeRegister::StartupType startup); 28 | 29 | virtual bool IsKnownOrbit(base::StringPiece orbit) const OVERRIDE; 30 | virtual void VerifyNonceIsValidAndUnique(base::StringPiece nonce, 31 | QuicWallTime now, 32 | ResultCallback* cb) OVERRIDE; 33 | 34 | private: 35 | mutable base::Lock m_; 36 | StrikeRegister strike_register_; 37 | 38 | DISALLOW_COPY_AND_ASSIGN(LocalStrikeRegisterClient); 39 | }; 40 | 41 | } // namespace net 42 | 43 | #endif // NET_QUIC_CRYPTO_LOCAL_STRIKE_REGISTER_CLIENT_H_ 44 | -------------------------------------------------------------------------------- /src/net/crypto/null_decrypter.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_CRYPTO_NULL_DECRYPTER_H_ 6 | #define NET_QUIC_CRYPTO_NULL_DECRYPTER_H_ 7 | 8 | #include "base/compiler_specific.h" 9 | #include "net/base/net_export.h" 10 | #include "net/quic/crypto/quic_decrypter.h" 11 | 12 | namespace net { 13 | 14 | class QuicDataReader; 15 | 16 | // A NullDecrypter is a QuicDecrypter used before a crypto negotiation 17 | // has occurred. It does not actually decrypt the payload, but does 18 | // verify a hash (fnv128) over both the payload and associated data. 19 | class NET_EXPORT_PRIVATE NullDecrypter : public QuicDecrypter { 20 | public: 21 | NullDecrypter(); 22 | virtual ~NullDecrypter() {} 23 | 24 | // QuicDecrypter implementation 25 | virtual bool SetKey(base::StringPiece key) OVERRIDE; 26 | virtual bool SetNoncePrefix(base::StringPiece nonce_prefix) OVERRIDE; 27 | virtual bool Decrypt(base::StringPiece nonce, 28 | base::StringPiece associated_data, 29 | base::StringPiece ciphertext, 30 | unsigned char* output, 31 | size_t* output_length) OVERRIDE; 32 | virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, 33 | base::StringPiece associated_data, 34 | base::StringPiece ciphertext) OVERRIDE; 35 | virtual base::StringPiece GetKey() const OVERRIDE; 36 | virtual base::StringPiece GetNoncePrefix() const OVERRIDE; 37 | 38 | private: 39 | bool ReadHash(QuicDataReader* reader, uint128* hash); 40 | uint128 ComputeHash(const std::string& data) const; 41 | }; 42 | 43 | } // namespace net 44 | 45 | #endif // NET_QUIC_CRYPTO_NULL_DECRYPTER_H_ 46 | -------------------------------------------------------------------------------- /src/net/crypto/null_decrypter_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/quic/crypto/null_decrypter.h" 6 | #include "net/quic/test_tools/quic_test_utils.h" 7 | 8 | using base::StringPiece; 9 | 10 | namespace net { 11 | namespace test { 12 | 13 | class NullDecrypterTest : public ::testing::TestWithParam { 14 | }; 15 | 16 | TEST_F(NullDecrypterTest, Decrypt) { 17 | unsigned char expected[] = { 18 | // fnv hash 19 | 0xa0, 0x6f, 0x44, 0x8a, 20 | 0x44, 0xf8, 0x18, 0x3b, 21 | 0x47, 0x91, 0xb2, 0x13, 22 | // payload 23 | 'g', 'o', 'o', 'd', 24 | 'b', 'y', 'e', '!', 25 | }; 26 | const char* data = reinterpret_cast(expected); 27 | size_t len = arraysize(expected); 28 | NullDecrypter decrypter; 29 | scoped_ptr decrypted( 30 | decrypter.DecryptPacket(0, "hello world!", StringPiece(data, len))); 31 | ASSERT_TRUE(decrypted.get()); 32 | EXPECT_EQ("goodbye!", decrypted->AsStringPiece()); 33 | } 34 | 35 | TEST_F(NullDecrypterTest, BadHash) { 36 | unsigned char expected[] = { 37 | // fnv hash 38 | 0x46, 0x11, 0xea, 0x5f, 39 | 0xcf, 0x1d, 0x66, 0x5b, 40 | 0xba, 0xf0, 0xbc, 0xfd, 41 | // payload 42 | 'g', 'o', 'o', 'd', 43 | 'b', 'y', 'e', '!', 44 | }; 45 | const char* data = reinterpret_cast(expected); 46 | size_t len = arraysize(expected); 47 | NullDecrypter decrypter; 48 | scoped_ptr decrypted( 49 | decrypter.DecryptPacket(0, "hello world!", StringPiece(data, len))); 50 | ASSERT_FALSE(decrypted.get()); 51 | } 52 | 53 | TEST_F(NullDecrypterTest, ShortInput) { 54 | unsigned char expected[] = { 55 | // fnv hash (truncated) 56 | 0x46, 0x11, 0xea, 0x5f, 57 | 0xcf, 0x1d, 0x66, 0x5b, 58 | 0xba, 0xf0, 0xbc, 59 | }; 60 | const char* data = reinterpret_cast(expected); 61 | size_t len = arraysize(expected); 62 | NullDecrypter decrypter; 63 | scoped_ptr decrypted( 64 | decrypter.DecryptPacket(0, "hello world!", StringPiece(data, len))); 65 | ASSERT_FALSE(decrypted.get()); 66 | } 67 | 68 | } // namespace test 69 | } // namespace net 70 | -------------------------------------------------------------------------------- /src/net/crypto/null_encrypter.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/quic/crypto/null_encrypter.h" 6 | #include "net/quic/quic_data_writer.h" 7 | #include "net/quic/quic_utils.h" 8 | 9 | using base::StringPiece; 10 | using std::string; 11 | 12 | namespace net { 13 | 14 | const size_t kHashSizeShort = 12; // size of uint128 serialized short 15 | 16 | NullEncrypter::NullEncrypter() {} 17 | 18 | bool NullEncrypter::SetKey(StringPiece key) { return key.empty(); } 19 | 20 | bool NullEncrypter::SetNoncePrefix(StringPiece nonce_prefix) { 21 | return nonce_prefix.empty(); 22 | } 23 | 24 | bool NullEncrypter::Encrypt( 25 | StringPiece /*nonce*/, 26 | StringPiece associated_data, 27 | StringPiece plaintext, 28 | unsigned char* output) { 29 | string buffer = associated_data.as_string(); 30 | plaintext.AppendToString(&buffer); 31 | uint128 hash = QuicUtils::FNV1a_128_Hash(buffer.data(), buffer.length()); 32 | QuicUtils::SerializeUint128Short(hash, output); 33 | memcpy(output + GetHashLength(), plaintext.data(), plaintext.size()); 34 | return true; 35 | } 36 | 37 | QuicData* NullEncrypter::EncryptPacket( 38 | QuicPacketSequenceNumber /*sequence_number*/, 39 | StringPiece associated_data, 40 | StringPiece plaintext) { 41 | const size_t len = plaintext.size() + GetHashLength(); 42 | uint8* buffer = new uint8[len]; 43 | Encrypt(StringPiece(), associated_data, plaintext, buffer); 44 | return new QuicData(reinterpret_cast(buffer), len, true); 45 | } 46 | 47 | size_t NullEncrypter::GetKeySize() const { return 0; } 48 | 49 | size_t NullEncrypter::GetNoncePrefixSize() const { return 0; } 50 | 51 | size_t NullEncrypter::GetMaxPlaintextSize(size_t ciphertext_size) const { 52 | return ciphertext_size - GetHashLength(); 53 | } 54 | 55 | size_t NullEncrypter::GetCiphertextSize(size_t plaintext_size) const { 56 | return plaintext_size + GetHashLength(); 57 | } 58 | 59 | StringPiece NullEncrypter::GetKey() const { return StringPiece(); } 60 | 61 | StringPiece NullEncrypter::GetNoncePrefix() const { return StringPiece(); } 62 | 63 | size_t NullEncrypter::GetHashLength() const { 64 | return kHashSizeShort; 65 | } 66 | 67 | } // namespace net 68 | -------------------------------------------------------------------------------- /src/net/crypto/null_encrypter.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_CRYPTO_NULL_ENCRYPTER_H_ 6 | #define NET_QUIC_CRYPTO_NULL_ENCRYPTER_H_ 7 | 8 | #include "base/compiler_specific.h" 9 | #include "net/base/net_export.h" 10 | #include "net/quic/crypto/quic_encrypter.h" 11 | 12 | namespace net { 13 | 14 | // A NullEncrypter is a QuicEncrypter used before a crypto negotiation 15 | // has occurred. It does not actually encrypt the payload, but does 16 | // generate a MAC (fnv128) over both the payload and associated data. 17 | class NET_EXPORT_PRIVATE NullEncrypter : public QuicEncrypter { 18 | public: 19 | NullEncrypter(); 20 | virtual ~NullEncrypter() {} 21 | 22 | // QuicEncrypter implementation 23 | virtual bool SetKey(base::StringPiece key) OVERRIDE; 24 | virtual bool SetNoncePrefix(base::StringPiece nonce_prefix) OVERRIDE; 25 | virtual bool Encrypt(base::StringPiece nonce, 26 | base::StringPiece associated_data, 27 | base::StringPiece plaintext, 28 | unsigned char* output) OVERRIDE; 29 | virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, 30 | base::StringPiece associated_data, 31 | base::StringPiece plaintext) OVERRIDE; 32 | virtual size_t GetKeySize() const OVERRIDE; 33 | virtual size_t GetNoncePrefixSize() const OVERRIDE; 34 | virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const OVERRIDE; 35 | virtual size_t GetCiphertextSize(size_t plaintext_size) const OVERRIDE; 36 | virtual base::StringPiece GetKey() const OVERRIDE; 37 | virtual base::StringPiece GetNoncePrefix() const OVERRIDE; 38 | 39 | private: 40 | size_t GetHashLength() const; 41 | }; 42 | 43 | } // namespace net 44 | 45 | #endif // NET_QUIC_CRYPTO_NULL_ENCRYPTER_H_ 46 | -------------------------------------------------------------------------------- /src/net/crypto/null_encrypter_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/quic/crypto/null_encrypter.h" 6 | #include "net/quic/test_tools/quic_test_utils.h" 7 | 8 | using base::StringPiece; 9 | 10 | namespace net { 11 | namespace test { 12 | 13 | class NullEncrypterTest : public ::testing::TestWithParam { 14 | }; 15 | 16 | TEST_F(NullEncrypterTest, Encrypt) { 17 | unsigned char expected[] = { 18 | // fnv hash 19 | 0xa0, 0x6f, 0x44, 0x8a, 20 | 0x44, 0xf8, 0x18, 0x3b, 21 | 0x47, 0x91, 0xb2, 0x13, 22 | // payload 23 | 'g', 'o', 'o', 'd', 24 | 'b', 'y', 'e', '!', 25 | }; 26 | NullEncrypter encrypter; 27 | scoped_ptr encrypted( 28 | encrypter.EncryptPacket(0, "hello world!", "goodbye!")); 29 | ASSERT_TRUE(encrypted.get()); 30 | test::CompareCharArraysWithHexError( 31 | "encrypted data", encrypted->data(), encrypted->length(), 32 | reinterpret_cast(expected), 33 | arraysize(expected)); 34 | } 35 | 36 | TEST_F(NullEncrypterTest, GetMaxPlaintextSize) { 37 | NullEncrypter encrypter; 38 | EXPECT_EQ(1000u, encrypter.GetMaxPlaintextSize(1012)); 39 | EXPECT_EQ(100u, encrypter.GetMaxPlaintextSize(112)); 40 | EXPECT_EQ(10u, encrypter.GetMaxPlaintextSize(22)); 41 | } 42 | 43 | TEST_F(NullEncrypterTest, GetCiphertextSize) { 44 | NullEncrypter encrypter; 45 | EXPECT_EQ(1012u, encrypter.GetCiphertextSize(1000)); 46 | EXPECT_EQ(112u, encrypter.GetCiphertextSize(100)); 47 | EXPECT_EQ(22u, encrypter.GetCiphertextSize(10)); 48 | } 49 | 50 | } // namespace test 51 | } // namespace net 52 | -------------------------------------------------------------------------------- /src/net/crypto/p256_key_exchange_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/crypto/p256_key_exchange.h" 6 | 7 | #include "base/logging.h" 8 | #include "testing/gtest/include/gtest/gtest.h" 9 | 10 | using std::string; 11 | 12 | namespace net { 13 | namespace test { 14 | 15 | // SharedKey just tests that the basic key exchange identity holds: that both 16 | // parties end up with the same key. 17 | TEST(P256KeyExchange, SharedKey) { 18 | for (int i = 0; i < 5; i++) { 19 | string alice_private(P256KeyExchange::NewPrivateKey()); 20 | string bob_private(P256KeyExchange::NewPrivateKey()); 21 | 22 | ASSERT_FALSE(alice_private.empty()); 23 | ASSERT_FALSE(bob_private.empty()); 24 | ASSERT_NE(alice_private, bob_private); 25 | 26 | scoped_ptr alice(P256KeyExchange::New(alice_private)); 27 | scoped_ptr bob(P256KeyExchange::New(bob_private)); 28 | 29 | ASSERT_TRUE(alice.get() != NULL); 30 | ASSERT_TRUE(bob.get() != NULL); 31 | 32 | const base::StringPiece alice_public(alice->public_value()); 33 | const base::StringPiece bob_public(bob->public_value()); 34 | 35 | std::string alice_shared, bob_shared; 36 | ASSERT_TRUE(alice->CalculateSharedKey(bob_public, &alice_shared)); 37 | ASSERT_TRUE(bob->CalculateSharedKey(alice_public, &bob_shared)); 38 | ASSERT_EQ(alice_shared, bob_shared); 39 | } 40 | } 41 | 42 | } // namespace test 43 | } // namespace net 44 | 45 | -------------------------------------------------------------------------------- /src/net/crypto/proof_source.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CRYPTO_PROOF_SOURCE_H_ 6 | #define NET_QUIC_CRYPTO_PROOF_SOURCE_H_ 7 | 8 | #include 9 | #include 10 | 11 | #include "net/base/net_export.h" 12 | 13 | namespace net { 14 | 15 | // ProofSource is an interface by which a QUIC server can obtain certificate 16 | // chains and signatures that prove its identity. 17 | class NET_EXPORT_PRIVATE ProofSource { 18 | public: 19 | virtual ~ProofSource() {} 20 | 21 | // GetProof finds a certificate chain for |hostname|, sets |out_certs| to 22 | // point to it (in leaf-first order), calculates a signature of 23 | // |server_config| using that chain and puts the result in |out_signature|. 24 | // 25 | // The signature uses SHA-256 as the hash function and PSS padding when the 26 | // key is RSA. 27 | // 28 | // The signature uses SHA-256 as the hash function when the key is ECDSA. 29 | // 30 | // If |ecdsa_ok| is true, the signature may use an ECDSA key. Otherwise, the 31 | // signature must use an RSA key. 32 | // 33 | // |out_certs| is a pointer to a pointer, not a pointer to an array. 34 | // 35 | // The number of certificate chains is expected to be small and fixed thus 36 | // the ProofSource retains ownership of the contents of |out_certs|. The 37 | // expectation is that they will be cached forever. 38 | // 39 | // The signature values should be cached because |server_config| will be 40 | // somewhat static. However, since they aren't bounded, the ProofSource may 41 | // wish to evicit entries from that cache, thus the caller takes ownership of 42 | // |*out_signature|. 43 | // 44 | // |hostname| may be empty to signify that a default certificate should be 45 | // used. 46 | // 47 | // This function may be called concurrently. 48 | virtual bool GetProof(const std::string& hostname, 49 | const std::string& server_config, 50 | bool ecdsa_ok, 51 | const std::vector** out_certs, 52 | std::string* out_signature) = 0; 53 | }; 54 | 55 | } // namespace net 56 | 57 | #endif // NET_QUIC_CRYPTO_PROOF_SOURCE_H_ 58 | -------------------------------------------------------------------------------- /src/net/crypto/proof_source_chromium.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/crypto/proof_source_chromium.h" 6 | 7 | using std::string; 8 | using std::vector; 9 | 10 | namespace net { 11 | 12 | ProofSourceChromium::ProofSourceChromium() { 13 | } 14 | 15 | bool ProofSourceChromium::GetProof(const string& hostname, 16 | const string& server_config, 17 | bool ecdsa_ok, 18 | const vector** out_certs, 19 | string* out_signature) { 20 | return false; 21 | } 22 | 23 | } // namespace net 24 | -------------------------------------------------------------------------------- /src/net/crypto/proof_source_chromium.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_CRYPTO_PROOF_SOURCE_CHROMIUM_H_ 6 | #define NET_QUIC_CRYPTO_PROOF_SOURCE_CHROMIUM_H_ 7 | 8 | #include 9 | #include 10 | 11 | #include "base/basictypes.h" 12 | #include "base/compiler_specific.h" 13 | #include "net/base/net_export.h" 14 | #include "net/quic/crypto/proof_source.h" 15 | 16 | namespace net { 17 | 18 | // ProofSourceChromium implements the QUIC ProofSource interface. 19 | // TODO(rtenneti): implement details of this class. 20 | class NET_EXPORT_PRIVATE ProofSourceChromium : public ProofSource { 21 | public: 22 | ProofSourceChromium(); 23 | virtual ~ProofSourceChromium() {} 24 | 25 | // ProofSource interface 26 | virtual bool GetProof(const std::string& hostname, 27 | const std::string& server_config, 28 | bool ecdsa_ok, 29 | const std::vector** out_certs, 30 | std::string* out_signature) OVERRIDE; 31 | 32 | private: 33 | DISALLOW_COPY_AND_ASSIGN(ProofSourceChromium); 34 | }; 35 | 36 | } // namespace net 37 | 38 | #endif // NET_QUIC_CRYPTO_PROOF_SOURCE_CHROMIUM_H_ 39 | -------------------------------------------------------------------------------- /src/net/crypto/proof_verifier.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/crypto/proof_verifier.h" 6 | 7 | namespace net { 8 | 9 | ProofVerifyDetails::~ProofVerifyDetails() {} 10 | 11 | ProofVerifierCallback::~ProofVerifierCallback() {} 12 | 13 | ProofVerifier::~ProofVerifier() {} 14 | 15 | } // namespace net 16 | -------------------------------------------------------------------------------- /src/net/crypto/quic_crypto_client_config_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/crypto/quic_crypto_client_config.h" 6 | 7 | #include "net/quic/test_tools/quic_test_utils.h" 8 | #include "testing/gtest/include/gtest/gtest.h" 9 | 10 | using std::string; 11 | 12 | namespace net { 13 | namespace test { 14 | 15 | TEST(QuicCryptoClientConfigTest, InchoateChlo) { 16 | QuicCryptoClientConfig::CachedState state; 17 | QuicCryptoClientConfig config; 18 | QuicCryptoNegotiatedParameters params; 19 | CryptoHandshakeMessage msg; 20 | config.FillInchoateClientHello("www.google.com", QuicVersionMax(), &state, 21 | ¶ms, &msg); 22 | 23 | QuicTag cver; 24 | EXPECT_EQ(QUIC_NO_ERROR, msg.GetUint32(kVER, &cver)); 25 | EXPECT_EQ(QuicVersionToQuicTag(QuicVersionMax()), cver); 26 | 27 | // TODO(rch): Remove once we remove QUIC_VERSION_12. 28 | uint16 vers; 29 | EXPECT_EQ(QUIC_NO_ERROR, msg.GetUint16(kVERS, &vers)); 30 | EXPECT_EQ(0u, vers); 31 | } 32 | 33 | TEST(QuicCryptoClientConfigTest, ProcessServerDowngradeAttack) { 34 | QuicVersionVector supported_versions = QuicSupportedVersions(); 35 | if (supported_versions.size() == 1) { 36 | // No downgrade attack is possible if the client only supports one version. 37 | return; 38 | } 39 | QuicTagVector supported_version_tags; 40 | for (size_t i = supported_versions.size(); i > 0; --i) { 41 | supported_version_tags.push_back( 42 | QuicVersionToQuicTag(supported_versions[i - 1])); 43 | } 44 | CryptoHandshakeMessage msg; 45 | msg.set_tag(kSHLO); 46 | msg.SetVector(kVER, supported_version_tags); 47 | 48 | QuicCryptoClientConfig::CachedState cached; 49 | QuicCryptoNegotiatedParameters out_params; 50 | string error; 51 | QuicCryptoClientConfig config; 52 | EXPECT_EQ(QUIC_VERSION_NEGOTIATION_MISMATCH, 53 | config.ProcessServerHello(msg, 0, supported_versions, 54 | &cached, &out_params, &error)); 55 | EXPECT_EQ("Downgrade attack detected", error); 56 | } 57 | 58 | } // namespace test 59 | } // namespace net 60 | -------------------------------------------------------------------------------- /src/net/crypto/quic_decrypter.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/quic/crypto/quic_decrypter.h" 6 | 7 | #include "net/quic/crypto/aes_128_gcm_12_decrypter.h" 8 | #include "net/quic/crypto/null_decrypter.h" 9 | 10 | namespace net { 11 | 12 | // static 13 | QuicDecrypter* QuicDecrypter::Create(QuicTag algorithm) { 14 | switch (algorithm) { 15 | case kAESG: 16 | return new Aes128Gcm12Decrypter(); 17 | case kNULL: 18 | return new NullDecrypter(); 19 | default: 20 | LOG(FATAL) << "Unsupported algorithm: " << algorithm; 21 | return NULL; 22 | } 23 | } 24 | 25 | } // namespace net 26 | -------------------------------------------------------------------------------- /src/net/crypto/quic_encrypter.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/quic/crypto/quic_encrypter.h" 6 | 7 | #include "net/quic/crypto/aes_128_gcm_12_encrypter.h" 8 | #include "net/quic/crypto/null_encrypter.h" 9 | 10 | namespace net { 11 | 12 | // static 13 | QuicEncrypter* QuicEncrypter::Create(QuicTag algorithm) { 14 | switch (algorithm) { 15 | case kAESG: 16 | return new Aes128Gcm12Encrypter(); 17 | case kNULL: 18 | return new NullEncrypter(); 19 | default: 20 | LOG(FATAL) << "Unsupported algorithm: " << algorithm; 21 | return NULL; 22 | } 23 | } 24 | 25 | } // namespace net 26 | -------------------------------------------------------------------------------- /src/net/crypto/quic_random.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/quic/crypto/quic_random.h" 6 | 7 | #include "base/logging.h" 8 | #include "base/memory/singleton.h" 9 | #include "crypto/random.h" 10 | 11 | namespace net { 12 | 13 | namespace { 14 | 15 | class DefaultRandom : public QuicRandom { 16 | public: 17 | static DefaultRandom* GetInstance(); 18 | 19 | // QuicRandom implementation 20 | virtual void RandBytes(void* data, size_t len) OVERRIDE; 21 | virtual uint64 RandUint64() OVERRIDE; 22 | virtual void Reseed(const void* additional_entropy, 23 | size_t entropy_len) OVERRIDE; 24 | 25 | private: 26 | DefaultRandom(); 27 | virtual ~DefaultRandom() {} 28 | 29 | friend struct DefaultSingletonTraits; 30 | DISALLOW_COPY_AND_ASSIGN(DefaultRandom); 31 | }; 32 | 33 | DefaultRandom* DefaultRandom::GetInstance() { 34 | return Singleton::get(); 35 | } 36 | 37 | void DefaultRandom::RandBytes(void* data, size_t len) { 38 | crypto::RandBytes(data, len); 39 | } 40 | 41 | uint64 DefaultRandom::RandUint64() { 42 | uint64 value; 43 | RandBytes(&value, sizeof(value)); 44 | return value; 45 | } 46 | 47 | void DefaultRandom::Reseed(const void* additional_entropy, size_t entropy_len) { 48 | // No such function exists in crypto/random.h. 49 | } 50 | 51 | DefaultRandom::DefaultRandom() { 52 | } 53 | 54 | } // namespace 55 | 56 | // static 57 | QuicRandom* QuicRandom::GetInstance() { return DefaultRandom::GetInstance(); } 58 | 59 | } // namespace net 60 | -------------------------------------------------------------------------------- /src/net/crypto/quic_random.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_CRYPTO_QUIC_RANDOM_H_ 6 | #define NET_QUIC_CRYPTO_QUIC_RANDOM_H_ 7 | 8 | #include 9 | 10 | #include "base/basictypes.h" 11 | #include "net/base/net_export.h" 12 | 13 | namespace net { 14 | 15 | // The interface for a random number generator. 16 | class NET_EXPORT_PRIVATE QuicRandom { 17 | public: 18 | virtual ~QuicRandom() {} 19 | 20 | // Returns the default random number generator, which is cryptographically 21 | // secure and thread-safe. 22 | static QuicRandom* GetInstance(); 23 | 24 | // Generates |len| random bytes in the |data| buffer. 25 | virtual void RandBytes(void* data, size_t len) = 0; 26 | 27 | // Returns a random number in the range [0, kuint64max]. 28 | virtual uint64 RandUint64() = 0; 29 | 30 | // Reseeds the random number generator with additional entropy input. 31 | // NOTE: the constructor of a QuicRandom object is responsible for seeding 32 | // itself with enough entropy input. 33 | virtual void Reseed(const void* additional_entropy, size_t entropy_len) = 0; 34 | }; 35 | 36 | } // namespace net 37 | 38 | #endif // NET_QUIC_CRYPTO_QUIC_RANDOM_H_ 39 | -------------------------------------------------------------------------------- /src/net/crypto/quic_random_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/quic/crypto/quic_random.h" 6 | 7 | #include "testing/gtest/include/gtest/gtest.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | TEST(QuicRandomTest, RandBytes) { 13 | unsigned char buf1[16]; 14 | unsigned char buf2[16]; 15 | memset(buf1, 0xaf, sizeof(buf1)); 16 | memset(buf2, 0xaf, sizeof(buf2)); 17 | ASSERT_EQ(0, memcmp(buf1, buf2, sizeof(buf1))); 18 | 19 | QuicRandom* rng = QuicRandom::GetInstance(); 20 | rng->RandBytes(buf1, sizeof(buf1)); 21 | EXPECT_NE(0, memcmp(buf1, buf2, sizeof(buf1))); 22 | } 23 | 24 | TEST(QuicRandomTest, RandUint64) { 25 | QuicRandom* rng = QuicRandom::GetInstance(); 26 | uint64 value1 = rng->RandUint64(); 27 | uint64 value2 = rng->RandUint64(); 28 | EXPECT_NE(value1, value2); 29 | } 30 | 31 | TEST(QuicRandomTest, Reseed) { 32 | char buf[1024]; 33 | memset(buf, 0xaf, sizeof(buf)); 34 | 35 | QuicRandom* rng = QuicRandom::GetInstance(); 36 | rng->Reseed(buf, sizeof(buf)); 37 | } 38 | 39 | } // namespace test 40 | } // namespace net 41 | -------------------------------------------------------------------------------- /src/net/crypto/scoped_evp_aead_ctx.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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/crypto/scoped_evp_aead_ctx.h" 6 | 7 | namespace net { 8 | 9 | ScopedEVPAEADCtx::ScopedEVPAEADCtx() { 10 | ctx_.aead = NULL; 11 | } 12 | 13 | ScopedEVPAEADCtx::~ScopedEVPAEADCtx() { 14 | if (ctx_.aead != NULL) { 15 | EVP_AEAD_CTX_cleanup(&ctx_); 16 | } 17 | } 18 | 19 | EVP_AEAD_CTX* ScopedEVPAEADCtx::get() { 20 | return &ctx_; 21 | } 22 | 23 | } // namespace net 24 | -------------------------------------------------------------------------------- /src/net/crypto/scoped_evp_aead_ctx.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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 | #ifndef NET_QUIC_CRYPTO_SCOPED_EVP_AEAD_CTX_H_ 6 | #define NET_QUIC_CRYPTO_SCOPED_EVP_AEAD_CTX_H_ 7 | 8 | #include 9 | 10 | namespace net { 11 | 12 | // ScopedEVPAEADCtx manages an EVP_AEAD_CTX object and calls the needed cleanup 13 | // functions when it goes out of scope. 14 | class ScopedEVPAEADCtx { 15 | public: 16 | ScopedEVPAEADCtx(); 17 | ~ScopedEVPAEADCtx(); 18 | 19 | EVP_AEAD_CTX* get(); 20 | 21 | private: 22 | EVP_AEAD_CTX ctx_; 23 | }; 24 | 25 | } // namespace net 26 | 27 | #endif // NET_QUIC_CRYPTO_SCOPED_EVP_AEAD_CTX_H_ 28 | -------------------------------------------------------------------------------- /src/net/crypto/source_address_token.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/crypto/source_address_token.h" 6 | 7 | #include 8 | 9 | #include "base/strings/string_number_conversions.h" 10 | #include "base/strings/string_split.h" 11 | 12 | using std::string; 13 | using std::vector; 14 | 15 | namespace net { 16 | 17 | SourceAddressToken::SourceAddressToken() { 18 | } 19 | 20 | SourceAddressToken::~SourceAddressToken() { 21 | } 22 | 23 | string SourceAddressToken::SerializeAsString() const { 24 | string out; 25 | out.push_back(ip_.size()); 26 | out.append(ip_); 27 | string time_str = base::Int64ToString(timestamp_); 28 | out.push_back(time_str.size()); 29 | out.append(time_str); 30 | return out; 31 | } 32 | 33 | bool SourceAddressToken::ParseFromArray(const char* plaintext, 34 | size_t plaintext_length) { 35 | if (plaintext_length == 0) { 36 | return false; 37 | } 38 | size_t ip_len = plaintext[0]; 39 | if (plaintext_length <= 1 + ip_len) { 40 | return false; 41 | } 42 | size_t time_len = plaintext[1 + ip_len]; 43 | if (plaintext_length != 1 + ip_len + 1 + time_len) { 44 | return false; 45 | } 46 | 47 | string time_str(&plaintext[1 + ip_len + 1], time_len); 48 | int64 timestamp; 49 | if (!base::StringToInt64(time_str, ×tamp)) { 50 | return false; 51 | } 52 | 53 | ip_.assign(&plaintext[1], ip_len); 54 | timestamp_ = timestamp; 55 | return true; 56 | } 57 | 58 | } // namespace net 59 | -------------------------------------------------------------------------------- /src/net/crypto/source_address_token.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_CRYPTO_SOURCE_ADDRESS_TOKEN_H_ 6 | #define NET_QUIC_CRYPTO_SOURCE_ADDRESS_TOKEN_H_ 7 | 8 | #include 9 | 10 | #include "base/basictypes.h" 11 | #include "base/strings/string_piece.h" 12 | 13 | namespace net { 14 | 15 | // TODO(rtenneti): sync with server more rationally. 16 | class SourceAddressToken { 17 | public: 18 | SourceAddressToken(); 19 | ~SourceAddressToken(); 20 | 21 | std::string SerializeAsString() const; 22 | 23 | bool ParseFromArray(const char* plaintext, size_t plaintext_length); 24 | 25 | std::string ip() const { 26 | return ip_; 27 | } 28 | 29 | int64 timestamp() const { 30 | return timestamp_; 31 | } 32 | 33 | void set_ip(base::StringPiece ip) { 34 | ip_ = ip.as_string(); 35 | } 36 | 37 | void set_timestamp(int64 timestamp) { 38 | timestamp_ = timestamp; 39 | } 40 | 41 | private: 42 | std::string ip_; 43 | int64 timestamp_; 44 | }; 45 | 46 | } // namespace net 47 | 48 | #endif // NET_QUIC_CRYPTO_SOURCE_ADDRESS_TOKEN_H_ 49 | -------------------------------------------------------------------------------- /src/net/crypto/strike_register_client.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_CRYPTO_STRIKE_REGISTER_CLIENT_H_ 6 | #define NET_QUIC_CRYPTO_STRIKE_REGISTER_CLIENT_H_ 7 | 8 | #include 9 | 10 | #include "base/basictypes.h" 11 | #include "base/strings/string_piece.h" 12 | #include "net/base/net_export.h" 13 | #include "net/quic/quic_time.h" 14 | 15 | namespace net { 16 | 17 | // Interface implemented by clients that talk to strike registers 18 | // implemented as local or remote services. 19 | class NET_EXPORT_PRIVATE StrikeRegisterClient { 20 | public: 21 | // Single use callback that will be invoked once the validation 22 | // operation is complete. 23 | class NET_EXPORT_PRIVATE ResultCallback { 24 | public: 25 | ResultCallback() {} 26 | virtual ~ResultCallback() {} 27 | void Run(bool nonce_is_valid_and_unique) { 28 | RunImpl(nonce_is_valid_and_unique); 29 | delete this; 30 | } 31 | 32 | protected: 33 | virtual void RunImpl(bool nonce_is_valid_and_unique) = 0; 34 | 35 | private: 36 | DISALLOW_COPY_AND_ASSIGN(ResultCallback); 37 | }; 38 | 39 | StrikeRegisterClient() {} 40 | virtual ~StrikeRegisterClient() {} 41 | 42 | // Returns true iff the strike register knows about the given orbit. 43 | virtual bool IsKnownOrbit(base::StringPiece orbit) const = 0; 44 | // Validate a nonce for freshness and uniqueness. 45 | // Will invoke cb->Run(ValidateResponse::nonce_is_valid_and_unique()) 46 | // once the asynchronous operation is complete. 47 | virtual void VerifyNonceIsValidAndUnique( 48 | base::StringPiece nonce, 49 | QuicWallTime now, 50 | ResultCallback* cb) = 0; 51 | 52 | private: 53 | DISALLOW_COPY_AND_ASSIGN(StrikeRegisterClient); 54 | }; 55 | 56 | } // namespace net 57 | 58 | #endif // NET_QUIC_CRYPTO_STRIKE_REGISTER_CLIENT_H_ 59 | -------------------------------------------------------------------------------- /src/net/iovector.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/iovector.h" 6 | 7 | namespace net { 8 | 9 | IOVector::IOVector() {} 10 | 11 | IOVector::~IOVector() {} 12 | 13 | } // namespace net 14 | -------------------------------------------------------------------------------- /src/net/port_suggester.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/port_suggester.h" 6 | 7 | #include "base/logging.h" 8 | #include "net/base/host_port_pair.h" 9 | 10 | namespace net { 11 | 12 | PortSuggester::PortSuggester(const HostPortPair& server, uint64 seed) 13 | : call_count_(0), 14 | previous_suggestion_(-1) { 15 | unsigned char hash_bytes[base::kSHA1Length]; 16 | base::SHA1HashBytes( 17 | reinterpret_cast(server.host().data()), 18 | server.host().length(), hash_bytes); 19 | COMPILE_ASSERT(sizeof(seed_) < sizeof(hash_bytes), seed_larger_than_hash); 20 | memcpy(&seed_, hash_bytes, sizeof(seed_)); 21 | seed_ ^= seed ^ server.port(); 22 | } 23 | 24 | int PortSuggester::SuggestPort(int min, int max) { 25 | // Sometimes our suggestion can't be used, so we ensure that if additional 26 | // calls are made, then each call (probably) provides a new suggestion. 27 | if (++call_count_ > 1) { 28 | // Evolve the seed. 29 | unsigned char hash_bytes[base::kSHA1Length]; 30 | base::SHA1HashBytes(reinterpret_cast(&seed_), 31 | sizeof(seed_), hash_bytes); 32 | memcpy(&seed_, hash_bytes, sizeof(seed_)); 33 | } 34 | DCHECK_LE(min, max); 35 | DCHECK_GT(min, 0); 36 | int range = max - min + 1; 37 | // Ports (and hence the extent of the |range|) are generally under 2^16, so 38 | // the tiny non-uniformity in the pseudo-random distribution is not 39 | // significant. 40 | previous_suggestion_ = static_cast(seed_ % range) + min; 41 | return previous_suggestion_; 42 | } 43 | 44 | int PortSuggester::previous_suggestion() const { 45 | DCHECK_LT(0u, call_count_); 46 | return previous_suggestion_; 47 | } 48 | 49 | } // namespace net 50 | -------------------------------------------------------------------------------- /src/net/port_suggester.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_PORT_SUGGESTER_H_ 6 | #define NET_QUIC_PORT_SUGGESTER_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "base/memory/ref_counted.h" 10 | #include "base/sha1.h" 11 | #include "net/base/net_export.h" 12 | 13 | namespace net { 14 | 15 | class HostPortPair; 16 | 17 | // We provide a pseudo-random number generator that is always seeded the same 18 | // way for a given destination host-port pair. The generator is used to 19 | // consistently suggest (for that host-port pair) an ephemeral source port, 20 | // and hence increase the likelihood that a server's load balancer will direct 21 | // a repeated connection to the same server (with QUIC, further increasing the 22 | // chance of connection establishment with 0-RTT). 23 | class NET_EXPORT_PRIVATE PortSuggester 24 | : public base::RefCounted { 25 | public: 26 | PortSuggester(const HostPortPair& server, uint64 seed); 27 | 28 | // Generate a pseudo-random int in the inclusive range from |min| to |max|. 29 | // Will (probably) return different numbers when called repeatedly. 30 | int SuggestPort(int min, int max); 31 | 32 | uint32 call_count() const { return call_count_; } 33 | int previous_suggestion() const; 34 | 35 | private: 36 | friend class base::RefCounted; 37 | 38 | virtual ~PortSuggester() {} 39 | 40 | // We maintain the first 8 bytes of a hash as our seed_ state. 41 | uint64 seed_; 42 | uint32 call_count_; // Number of suggestions made. 43 | int previous_suggestion_; 44 | 45 | DISALLOW_COPY_AND_ASSIGN(PortSuggester); 46 | }; 47 | 48 | } // namespace net 49 | 50 | #endif // NET_QUIC_PORT_SUGGESTER_H_ 51 | -------------------------------------------------------------------------------- /src/net/quic_ack_notifier.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_ack_notifier.h" 6 | 7 | #include 8 | 9 | #include "base/logging.h" 10 | #include "base/stl_util.h" 11 | 12 | namespace net { 13 | 14 | QuicAckNotifier::DelegateInterface::DelegateInterface() {} 15 | 16 | QuicAckNotifier::DelegateInterface::~DelegateInterface() {} 17 | 18 | QuicAckNotifier::QuicAckNotifier(DelegateInterface* delegate) 19 | : delegate_(delegate) { 20 | DCHECK(delegate); 21 | } 22 | 23 | QuicAckNotifier::~QuicAckNotifier() { 24 | } 25 | 26 | void QuicAckNotifier::AddSequenceNumber( 27 | const QuicPacketSequenceNumber& sequence_number) { 28 | sequence_numbers_.insert(sequence_number); 29 | } 30 | 31 | void QuicAckNotifier::AddSequenceNumbers( 32 | const SequenceNumberSet& sequence_numbers) { 33 | for (SequenceNumberSet::const_iterator it = sequence_numbers.begin(); 34 | it != sequence_numbers.end(); ++it) { 35 | AddSequenceNumber(*it); 36 | } 37 | } 38 | 39 | bool QuicAckNotifier::OnAck(QuicPacketSequenceNumber sequence_number) { 40 | DCHECK(ContainsKey(sequence_numbers_, sequence_number)); 41 | sequence_numbers_.erase(sequence_number); 42 | if (IsEmpty()) { 43 | // We have seen all the sequence numbers we were waiting for, trigger 44 | // callback notification. 45 | delegate_->OnAckNotification(); 46 | return true; 47 | } 48 | return false; 49 | } 50 | 51 | void QuicAckNotifier::UpdateSequenceNumber( 52 | QuicPacketSequenceNumber old_sequence_number, 53 | QuicPacketSequenceNumber new_sequence_number) { 54 | sequence_numbers_.erase(old_sequence_number); 55 | sequence_numbers_.insert(new_sequence_number); 56 | } 57 | 58 | }; // namespace net 59 | -------------------------------------------------------------------------------- /src/net/quic_ack_notifier_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_ack_notifier.h" 6 | 7 | #include "net/quic/test_tools/quic_test_utils.h" 8 | #include "testing/gmock/include/gmock/gmock.h" 9 | #include "testing/gtest/include/gtest/gtest.h" 10 | 11 | namespace net { 12 | namespace test { 13 | namespace { 14 | 15 | class QuicAckNotifierTest : public ::testing::Test { 16 | protected: 17 | virtual void SetUp() { 18 | delegate_ = new MockAckNotifierDelegate; 19 | notifier_.reset(new QuicAckNotifier(delegate_)); 20 | 21 | sequence_numbers_.insert(26); 22 | sequence_numbers_.insert(99); 23 | sequence_numbers_.insert(1234); 24 | notifier_->AddSequenceNumbers(sequence_numbers_); 25 | } 26 | 27 | SequenceNumberSet sequence_numbers_; 28 | MockAckNotifierDelegate* delegate_; 29 | scoped_ptr notifier_; 30 | }; 31 | 32 | // Should trigger callback when we receive acks for all the registered seqnums. 33 | TEST_F(QuicAckNotifierTest, TriggerCallback) { 34 | EXPECT_CALL(*delegate_, OnAckNotification()).Times(1); 35 | EXPECT_FALSE(notifier_->OnAck(26)); 36 | EXPECT_FALSE(notifier_->OnAck(99)); 37 | EXPECT_TRUE(notifier_->OnAck(1234)); 38 | } 39 | 40 | // Should not trigger callback if we never provide all the seqnums. 41 | TEST_F(QuicAckNotifierTest, DoesNotTrigger) { 42 | // Should not trigger callback as not all packets have been seen. 43 | EXPECT_CALL(*delegate_, OnAckNotification()).Times(0); 44 | EXPECT_FALSE(notifier_->OnAck(26)); 45 | EXPECT_FALSE(notifier_->OnAck(99)); 46 | } 47 | 48 | // Should trigger even after updating sequence numbers and receiving ACKs for 49 | // new sequeunce numbers. 50 | TEST_F(QuicAckNotifierTest, UpdateSeqNums) { 51 | // Update a couple of the sequence numbers (i.e. retransmitted packets) 52 | notifier_->UpdateSequenceNumber(99, 3000); 53 | notifier_->UpdateSequenceNumber(1234, 3001); 54 | 55 | EXPECT_CALL(*delegate_, OnAckNotification()).Times(1); 56 | EXPECT_FALSE(notifier_->OnAck(26)); // original 57 | EXPECT_FALSE(notifier_->OnAck(3000)); // updated 58 | EXPECT_TRUE(notifier_->OnAck(3001)); // updated 59 | } 60 | 61 | } // namespace 62 | } // namespace test 63 | } // namespace net 64 | -------------------------------------------------------------------------------- /src/net/quic_address_mismatch.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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_address_mismatch.h" 6 | 7 | #include "net/base/ip_endpoint.h" 8 | 9 | namespace net { 10 | 11 | int GetAddressMismatch(const IPEndPoint& first_address, 12 | const IPEndPoint& second_address) { 13 | if (first_address.address().empty() || second_address.address().empty()) { 14 | return -1; 15 | } 16 | 17 | int sample; 18 | if (first_address.address() != second_address.address()) { 19 | sample = QUIC_ADDRESS_MISMATCH_BASE; 20 | } else if (first_address.port() != second_address.port()) { 21 | sample = QUIC_PORT_MISMATCH_BASE; 22 | } else { 23 | sample = QUIC_ADDRESS_AND_PORT_MATCH_BASE; 24 | } 25 | 26 | // Add an offset to |sample|: 27 | // V4_V4: add 0 28 | // V6_V6: add 1 29 | // V4_V6: add 2 30 | // V6_V4: add 3 31 | bool first_ipv4 = (first_address.address().size() == kIPv4AddressSize); 32 | bool second_ipv4 = (second_address.address().size() == kIPv4AddressSize); 33 | if (first_ipv4 != second_ipv4) { 34 | CHECK_EQ(sample, QUIC_ADDRESS_MISMATCH_BASE); 35 | sample += 2; 36 | } 37 | if (!first_ipv4) { 38 | sample += 1; 39 | } 40 | return sample; 41 | } 42 | 43 | } // namespace net 44 | -------------------------------------------------------------------------------- /src/net/quic_address_mismatch.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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 | #ifndef NET_QUIC_QUIC_ADDRESS_MISMATCH_H_ 6 | #define NET_QUIC_QUIC_ADDRESS_MISMATCH_H_ 7 | 8 | #include "net/base/net_export.h" 9 | 10 | namespace net { 11 | 12 | class IPEndPoint; 13 | 14 | enum QuicAddressMismatch { 15 | // The addresses don't match. 16 | QUIC_ADDRESS_MISMATCH_BASE = 0, 17 | QUIC_ADDRESS_MISMATCH_V4_V4 = 0, 18 | QUIC_ADDRESS_MISMATCH_V6_V6 = 1, 19 | QUIC_ADDRESS_MISMATCH_V4_V6 = 2, 20 | QUIC_ADDRESS_MISMATCH_V6_V4 = 3, 21 | 22 | // The addresses match, but the ports don't match. 23 | QUIC_PORT_MISMATCH_BASE = 4, 24 | QUIC_PORT_MISMATCH_V4_V4 = 4, 25 | QUIC_PORT_MISMATCH_V6_V6 = 5, 26 | 27 | QUIC_ADDRESS_AND_PORT_MATCH_BASE = 6, 28 | QUIC_ADDRESS_AND_PORT_MATCH_V4_V4 = 6, 29 | QUIC_ADDRESS_AND_PORT_MATCH_V6_V6 = 7, 30 | 31 | QUIC_ADDRESS_MISMATCH_MAX, 32 | }; 33 | 34 | // Returns a value of the QuicAddressMismatch enum type that indicates how 35 | // |first_address| differs from |second_address|. Returns -1 if either address 36 | // is empty. 37 | // 38 | // Only used by the Net.QuicSession.PublicResetAddressMismatch histogram. 39 | NET_EXPORT_PRIVATE int GetAddressMismatch(const IPEndPoint& first_address, 40 | const IPEndPoint& second_address); 41 | 42 | } // namespace net 43 | 44 | #endif // NET_QUIC_QUIC_ADDRESS_MISMATCH_H_ 45 | -------------------------------------------------------------------------------- /src/net/quic_alarm.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_alarm.h" 6 | 7 | #include "base/logging.h" 8 | 9 | namespace net { 10 | 11 | QuicAlarm::QuicAlarm(Delegate* delegate) 12 | : delegate_(delegate), 13 | deadline_(QuicTime::Zero()) { 14 | } 15 | 16 | QuicAlarm::~QuicAlarm() {} 17 | 18 | void QuicAlarm::Set(QuicTime deadline) { 19 | DCHECK(!IsSet()); 20 | DCHECK(deadline.IsInitialized()); 21 | deadline_ = deadline; 22 | SetImpl(); 23 | } 24 | 25 | void QuicAlarm::Cancel() { 26 | deadline_ = QuicTime::Zero(); 27 | CancelImpl(); 28 | } 29 | 30 | bool QuicAlarm::IsSet() const { 31 | return deadline_.IsInitialized(); 32 | } 33 | 34 | void QuicAlarm::Fire() { 35 | if (!deadline_.IsInitialized()) { 36 | return; 37 | } 38 | 39 | deadline_ = QuicTime::Zero(); 40 | QuicTime deadline = delegate_->OnAlarm(); 41 | // delegate_->OnAlarm() might call Set(), in which case deadline_ will 42 | // already contain the new value, so don't overwrite it. 43 | if (!deadline_.IsInitialized() && deadline.IsInitialized()) { 44 | Set(deadline); 45 | } 46 | } 47 | 48 | } // namespace net 49 | -------------------------------------------------------------------------------- /src/net/quic_blocked_writer_interface.h: -------------------------------------------------------------------------------- 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 | // This is an interface for all objects that want to be notified that 6 | // the underlying UDP socket is available for writing (not write blocked 7 | // anymore). 8 | 9 | #ifndef NET_QUIC_QUIC_BLOCKED_WRITER_INTERFACE_H_ 10 | #define NET_QUIC_QUIC_BLOCKED_WRITER_INTERFACE_H_ 11 | 12 | #include "net/base/net_export.h" 13 | 14 | namespace net { 15 | 16 | class NET_EXPORT_PRIVATE QuicBlockedWriterInterface { 17 | public: 18 | virtual ~QuicBlockedWriterInterface() {} 19 | 20 | // Called by the PacketWriter when the underlying socket becomes writable 21 | // so that the BlockedWriter can go ahead and try writing. 22 | virtual void OnCanWrite() = 0; 23 | }; 24 | 25 | } // namespace net 26 | 27 | #endif // NET_QUIC_QUIC_BLOCKED_WRITER_INTERFACE_H_ 28 | -------------------------------------------------------------------------------- /src/net/quic_clock.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/quic/quic_clock.h" 6 | 7 | #include "base/time/time.h" 8 | 9 | namespace net { 10 | 11 | QuicClock::QuicClock() { 12 | } 13 | 14 | QuicClock::~QuicClock() {} 15 | 16 | QuicTime QuicClock::ApproximateNow() const { 17 | // Chrome does not have a distinct notion of ApproximateNow(). 18 | return Now(); 19 | } 20 | 21 | QuicTime QuicClock::Now() const { 22 | return QuicTime(base::TimeTicks::Now()); 23 | } 24 | 25 | QuicWallTime QuicClock::WallNow() const { 26 | return QuicWallTime::FromUNIXSeconds(base::Time::Now().ToTimeT()); 27 | } 28 | 29 | } // namespace net 30 | -------------------------------------------------------------------------------- /src/net/quic_clock.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_QUIC_CLOCK_H_ 6 | #define NET_QUIC_QUIC_CLOCK_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "net/base/net_export.h" 10 | #include "net/quic/quic_time.h" 11 | 12 | namespace net { 13 | 14 | typedef double WallTime; 15 | 16 | // Clock to efficiently retrieve an approximately accurate time from an 17 | // EpollServer. 18 | class NET_EXPORT_PRIVATE QuicClock { 19 | public: 20 | QuicClock(); 21 | virtual ~QuicClock(); 22 | 23 | // Returns the approximate current time as a QuicTime object. 24 | virtual QuicTime ApproximateNow() const; 25 | 26 | // Returns the current time as a QuicTime object. 27 | // Note: this use significant resources please use only if needed. 28 | virtual QuicTime Now() const; 29 | 30 | // WallNow returns the current wall-time - a time that is consistent across 31 | // different clocks. 32 | virtual QuicWallTime WallNow() const; 33 | }; 34 | 35 | } // namespace net 36 | 37 | #endif // NET_QUIC_QUIC_CLOCK_H_ 38 | -------------------------------------------------------------------------------- /src/net/quic_clock_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/quic/quic_clock.h" 6 | 7 | #include "testing/gtest/include/gtest/gtest.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | TEST(QuicClockTest, Now) { 13 | QuicClock clock; 14 | 15 | QuicTime start(base::TimeTicks::Now()); 16 | QuicTime now = clock.ApproximateNow(); 17 | QuicTime end(base::TimeTicks::Now()); 18 | 19 | EXPECT_LE(start, now); 20 | EXPECT_LE(now, end); 21 | } 22 | 23 | TEST(QuicClockTest, WallNow) { 24 | QuicClock clock; 25 | 26 | base::Time start = base::Time::Now(); 27 | QuicWallTime now = clock.WallNow(); 28 | base::Time end = base::Time::Now(); 29 | 30 | // If end > start, then we can check now is between start and end. 31 | if (end > start) { 32 | EXPECT_LE(static_cast(start.ToTimeT()), now.ToUNIXSeconds()); 33 | EXPECT_LE(now.ToUNIXSeconds(), static_cast(end.ToTimeT())); 34 | } 35 | } 36 | 37 | } // namespace test 38 | } // namespace net 39 | -------------------------------------------------------------------------------- /src/net/quic_connection_helper.h: -------------------------------------------------------------------------------- 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 | // The Chrome-specific helper for QuicConnection which uses 6 | // a TaskRunner for alarms, and uses a DatagramClientSocket for writing data. 7 | 8 | #ifndef NET_QUIC_QUIC_CONNECTION_HELPER_H_ 9 | #define NET_QUIC_QUIC_CONNECTION_HELPER_H_ 10 | 11 | #include "net/quic/quic_connection.h" 12 | 13 | #include 14 | 15 | #include "base/basictypes.h" 16 | #include "base/memory/weak_ptr.h" 17 | #include "net/base/ip_endpoint.h" 18 | #include "net/quic/quic_protocol.h" 19 | #include "net/quic/quic_time.h" 20 | #include "net/udp/datagram_client_socket.h" 21 | 22 | namespace base { 23 | class TaskRunner; 24 | } // namespace base 25 | 26 | namespace net { 27 | 28 | class QuicClock; 29 | class QuicRandom; 30 | 31 | class NET_EXPORT_PRIVATE QuicConnectionHelper 32 | : public QuicConnectionHelperInterface { 33 | public: 34 | QuicConnectionHelper(base::TaskRunner* task_runner, 35 | const QuicClock* clock, 36 | QuicRandom* random_generator); 37 | virtual ~QuicConnectionHelper(); 38 | 39 | // QuicConnectionHelperInterface 40 | virtual const QuicClock* GetClock() const OVERRIDE; 41 | virtual QuicRandom* GetRandomGenerator() OVERRIDE; 42 | virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) OVERRIDE; 43 | 44 | private: 45 | base::WeakPtrFactory weak_factory_; 46 | base::TaskRunner* task_runner_; 47 | const QuicClock* clock_; 48 | QuicRandom* random_generator_; 49 | 50 | DISALLOW_COPY_AND_ASSIGN(QuicConnectionHelper); 51 | }; 52 | 53 | } // namespace net 54 | 55 | #endif // NET_QUIC_QUIC_CONNECTION_HELPER_H_ 56 | -------------------------------------------------------------------------------- /src/net/quic_connection_stats.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_connection_stats.h" 6 | 7 | using std::ostream; 8 | 9 | namespace net { 10 | 11 | QuicConnectionStats::QuicConnectionStats() 12 | : bytes_sent(0), 13 | packets_sent(0), 14 | stream_bytes_sent(0), 15 | bytes_received(0), 16 | packets_received(0), 17 | stream_bytes_received(0), 18 | bytes_retransmitted(0), 19 | packets_retransmitted(0), 20 | packets_spuriously_retransmitted(0), 21 | packets_lost(0), 22 | packets_revived(0), 23 | packets_dropped(0), 24 | crypto_retransmit_count(0), 25 | tlp_count(0), 26 | rto_count(0), 27 | rtt(0), 28 | estimated_bandwidth(0) { 29 | } 30 | 31 | QuicConnectionStats::~QuicConnectionStats() {} 32 | 33 | ostream& operator<<(ostream& os, const QuicConnectionStats& s) { 34 | os << "{ bytes sent: " << s.bytes_sent 35 | << ", packets sent:" << s.packets_sent 36 | << ", stream bytes sent: " << s.stream_bytes_sent 37 | << ", bytes received: " << s.bytes_received 38 | << ", packets received: " << s.packets_received 39 | << ", stream bytes received: " << s.stream_bytes_received 40 | << ", bytes retransmitted: " << s.bytes_retransmitted 41 | << ", packets retransmitted: " << s.packets_retransmitted 42 | << ", packets_spuriously_retransmitted: " 43 | << s.packets_spuriously_retransmitted 44 | << ", packets lost: " << s.packets_lost 45 | << ", packets revived: " << s.packets_revived 46 | << ", packets dropped:" << s.packets_dropped 47 | << ", crypto retransmit count: " << s.crypto_retransmit_count 48 | << ", rto count: " << s.rto_count 49 | << ", tlp count: " << s.tlp_count 50 | << ", rtt(us): " << s.rtt 51 | << ", estimated_bandwidth: " << s.estimated_bandwidth 52 | << "}\n"; 53 | return os; 54 | } 55 | 56 | } // namespace net 57 | -------------------------------------------------------------------------------- /src/net/quic_connection_stats.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_QUIC_CONNECTION_STATS_H_ 6 | #define NET_QUIC_QUIC_CONNECTION_STATS_H_ 7 | 8 | #include 9 | 10 | #include "base/basictypes.h" 11 | #include "net/base/net_export.h" 12 | 13 | namespace net { 14 | // TODO(satyamshekhar): Add more interesting stats: 15 | // 1. (C/S)HLO retransmission count. 16 | // 2. SHLO received to first stream packet processed time. 17 | // 3. CHLO sent to SHLO received time. 18 | // 4. Number of migrations. 19 | // 5. Number of out of order packets. 20 | // 6. Number of connections that require more that 1-RTT. 21 | // 7. Avg number of streams / session. 22 | // 8. Number of duplicates received. 23 | // 9. Fraction of data transferred that was padding. 24 | 25 | // Structure to hold stats for a QuicConnection. 26 | struct NET_EXPORT_PRIVATE QuicConnectionStats { 27 | QuicConnectionStats(); 28 | ~QuicConnectionStats(); 29 | 30 | NET_EXPORT_PRIVATE friend std::ostream& operator<<( 31 | std::ostream& os, const QuicConnectionStats& s); 32 | 33 | uint64 bytes_sent; // includes retransmissions, fec. 34 | uint32 packets_sent; 35 | uint64 stream_bytes_sent; // non-retransmitted bytes sent in a stream frame. 36 | 37 | uint64 bytes_received; // includes duplicate data for a stream, fec. 38 | uint32 packets_received; // includes dropped packets 39 | uint64 stream_bytes_received; // bytes received in a stream frame. 40 | 41 | uint64 bytes_retransmitted; 42 | uint32 packets_retransmitted; 43 | 44 | uint32 packets_spuriously_retransmitted; 45 | uint32 packets_lost; 46 | 47 | uint32 packets_revived; 48 | uint32 packets_dropped; // duplicate or less than least unacked. 49 | uint32 crypto_retransmit_count; 50 | uint32 tlp_count; 51 | uint32 rto_count; 52 | 53 | uint32 rtt; // In microseconds 54 | uint64 estimated_bandwidth; 55 | // TODO(satyamshekhar): Add window_size, mss and mtu. 56 | }; 57 | 58 | } // namespace net 59 | 60 | #endif // NET_QUIC_QUIC_CONNECTION_STATS_H_ 61 | -------------------------------------------------------------------------------- /src/net/quic_crypto_client_stream_factory.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_QUIC_CRYPTO_CLIENT_STREAM_FACTORY_H_ 6 | #define NET_QUIC_QUIC_CRYPTO_CLIENT_STREAM_FACTORY_H_ 7 | 8 | #include 9 | 10 | #include "net/base/net_export.h" 11 | 12 | namespace net { 13 | 14 | class QuicCryptoClientStream; 15 | class QuicSession; 16 | 17 | // An interface used to instantiate QuicCryptoClientStream objects. Used to 18 | // facilitate testing code with mock implementations. 19 | class NET_EXPORT QuicCryptoClientStreamFactory { 20 | public: 21 | virtual ~QuicCryptoClientStreamFactory() {} 22 | 23 | virtual QuicCryptoClientStream* CreateQuicCryptoClientStream( 24 | const string& server_hostname, 25 | QuicSession* session, 26 | QuicCryptoClientConfig* crypto_config) = 0; 27 | }; 28 | 29 | } // namespace net 30 | 31 | #endif // NET_QUIC_QUIC_CRYPTO_CLIENT_STREAM_FACTORY_H_ 32 | -------------------------------------------------------------------------------- /src/net/quic_default_packet_writer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_default_packet_writer.h" 6 | 7 | #include "base/location.h" 8 | #include "base/logging.h" 9 | #include "base/metrics/sparse_histogram.h" 10 | #include "net/base/io_buffer.h" 11 | #include "net/base/net_errors.h" 12 | 13 | namespace net { 14 | 15 | QuicDefaultPacketWriter::QuicDefaultPacketWriter() : weak_factory_(this) { 16 | } 17 | 18 | QuicDefaultPacketWriter::QuicDefaultPacketWriter(DatagramClientSocket* socket) 19 | : weak_factory_(this), 20 | socket_(socket), 21 | write_blocked_(false) { 22 | } 23 | 24 | QuicDefaultPacketWriter::~QuicDefaultPacketWriter() {} 25 | 26 | WriteResult QuicDefaultPacketWriter::WritePacket( 27 | const char* buffer, size_t buf_len, 28 | const net::IPAddressNumber& self_address, 29 | const net::IPEndPoint& peer_address) { 30 | scoped_refptr buf( 31 | new StringIOBuffer(std::string(buffer, buf_len))); 32 | DCHECK(!IsWriteBlocked()); 33 | int rv = socket_->Write(buf.get(), 34 | buf_len, 35 | base::Bind(&QuicDefaultPacketWriter::OnWriteComplete, 36 | weak_factory_.GetWeakPtr())); 37 | WriteStatus status = WRITE_STATUS_OK; 38 | if (rv < 0) { 39 | if (rv != ERR_IO_PENDING) { 40 | UMA_HISTOGRAM_SPARSE_SLOWLY("Net.QuicSession.WriteError", -rv); 41 | status = WRITE_STATUS_ERROR; 42 | } else { 43 | status = WRITE_STATUS_BLOCKED; 44 | write_blocked_ = true; 45 | } 46 | } 47 | 48 | return WriteResult(status, rv); 49 | } 50 | 51 | bool QuicDefaultPacketWriter::IsWriteBlockedDataBuffered() const { 52 | // Chrome sockets' Write() methods buffer the data until the Write is 53 | // permitted. 54 | return true; 55 | } 56 | 57 | bool QuicDefaultPacketWriter::IsWriteBlocked() const { 58 | return write_blocked_; 59 | } 60 | 61 | void QuicDefaultPacketWriter::SetWritable() { 62 | write_blocked_ = false; 63 | } 64 | 65 | void QuicDefaultPacketWriter::OnWriteComplete(int rv) { 66 | DCHECK_NE(rv, ERR_IO_PENDING); 67 | write_blocked_ = false; 68 | WriteResult result(rv < 0 ? WRITE_STATUS_ERROR : WRITE_STATUS_OK, rv); 69 | connection_->OnPacketSent(result); 70 | connection_->OnCanWrite(); 71 | } 72 | 73 | } // namespace net 74 | -------------------------------------------------------------------------------- /src/net/quic_default_packet_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_QUIC_DEFAULT_PACKET_WRITER_H_ 6 | #define NET_QUIC_QUIC_DEFAULT_PACKET_WRITER_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "base/memory/weak_ptr.h" 10 | #include "net/base/ip_endpoint.h" 11 | #include "net/quic/quic_connection.h" 12 | #include "net/quic/quic_packet_writer.h" 13 | #include "net/quic/quic_protocol.h" 14 | #include "net/udp/datagram_client_socket.h" 15 | 16 | namespace net { 17 | 18 | struct WriteResult; 19 | 20 | // Chrome specific packet writer which uses a DatagramClientSocket for writing 21 | // data. 22 | class NET_EXPORT_PRIVATE QuicDefaultPacketWriter : public QuicPacketWriter { 23 | public: 24 | QuicDefaultPacketWriter(); 25 | explicit QuicDefaultPacketWriter(DatagramClientSocket* socket); 26 | virtual ~QuicDefaultPacketWriter(); 27 | 28 | // QuicPacketWriter 29 | virtual WriteResult WritePacket( 30 | const char* buffer, size_t buf_len, 31 | const net::IPAddressNumber& self_address, 32 | const net::IPEndPoint& peer_address) OVERRIDE; 33 | virtual bool IsWriteBlockedDataBuffered() const OVERRIDE; 34 | virtual bool IsWriteBlocked() const OVERRIDE; 35 | virtual void SetWritable() OVERRIDE; 36 | 37 | void OnWriteComplete(int rv); 38 | void SetConnection(QuicConnection* connection) { 39 | connection_ = connection; 40 | } 41 | 42 | private: 43 | base::WeakPtrFactory weak_factory_; 44 | DatagramClientSocket* socket_; 45 | QuicConnection* connection_; 46 | bool write_blocked_; 47 | }; 48 | 49 | } // namespace net 50 | 51 | #endif // NET_QUIC_QUIC_DEFAULT_PACKET_WRITER_H_ 52 | -------------------------------------------------------------------------------- /src/net/quic_http_utils.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_http_utils.h" 6 | 7 | namespace net { 8 | 9 | QuicPriority ConvertRequestPriorityToQuicPriority( 10 | const RequestPriority priority) { 11 | DCHECK_GE(priority, MINIMUM_PRIORITY); 12 | DCHECK_LE(priority, MAXIMUM_PRIORITY); 13 | return static_cast(HIGHEST - priority); 14 | } 15 | 16 | NET_EXPORT_PRIVATE RequestPriority ConvertQuicPriorityToRequestPriority( 17 | QuicPriority priority) { 18 | // Handle invalid values gracefully. 19 | return (priority >= 5) ? 20 | IDLE : static_cast(HIGHEST - priority); 21 | } 22 | 23 | base::Value* QuicRequestNetLogCallback( 24 | const SpdyHeaderBlock* headers, 25 | QuicPriority priority, 26 | NetLog::LogLevel log_level) { 27 | base::DictionaryValue* dict = static_cast( 28 | SpdyHeaderBlockNetLogCallback(headers, log_level)); 29 | dict->SetInteger("quic_priority", static_cast(priority)); 30 | return dict; 31 | } 32 | 33 | } // namespace net 34 | -------------------------------------------------------------------------------- /src/net/quic_http_utils.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_QUIC_HTTP_UTILS_H_ 6 | #define NET_QUIC_QUIC_HTTP_UTILS_H_ 7 | 8 | #include "base/values.h" 9 | #include "net/base/net_export.h" 10 | #include "net/base/request_priority.h" 11 | #include "net/quic/quic_protocol.h" 12 | #include "net/spdy/spdy_header_block.h" 13 | 14 | namespace net { 15 | 16 | NET_EXPORT_PRIVATE QuicPriority ConvertRequestPriorityToQuicPriority( 17 | RequestPriority priority); 18 | 19 | NET_EXPORT_PRIVATE RequestPriority ConvertQuicPriorityToRequestPriority( 20 | QuicPriority priority); 21 | 22 | // Converts a SpdyHeaderBlock and priority into NetLog event parameters. Caller 23 | // takes ownership of returned value. 24 | NET_EXPORT base::Value* QuicRequestNetLogCallback( 25 | const SpdyHeaderBlock* headers, 26 | QuicPriority priority, 27 | NetLog::LogLevel log_level); 28 | 29 | } // namespace net 30 | 31 | #endif // NET_QUIC_QUIC_HTTP_UTILS_H_ 32 | -------------------------------------------------------------------------------- /src/net/quic_http_utils_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_http_utils.h" 6 | 7 | #include "testing/gtest/include/gtest/gtest.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | TEST(QuicHttpUtilsTest, ConvertRequestPriorityToQuicPriority) { 13 | EXPECT_EQ(0u, ConvertRequestPriorityToQuicPriority(HIGHEST)); 14 | EXPECT_EQ(1u, ConvertRequestPriorityToQuicPriority(MEDIUM)); 15 | EXPECT_EQ(2u, ConvertRequestPriorityToQuicPriority(LOW)); 16 | EXPECT_EQ(3u, ConvertRequestPriorityToQuicPriority(LOWEST)); 17 | EXPECT_EQ(4u, ConvertRequestPriorityToQuicPriority(IDLE)); 18 | } 19 | 20 | TEST(QuicHttpUtilsTest, ConvertQuicPriorityToRequestPriority) { 21 | EXPECT_EQ(HIGHEST, ConvertQuicPriorityToRequestPriority(0)); 22 | EXPECT_EQ(MEDIUM, ConvertQuicPriorityToRequestPriority(1)); 23 | EXPECT_EQ(LOW, ConvertQuicPriorityToRequestPriority(2)); 24 | EXPECT_EQ(LOWEST, ConvertQuicPriorityToRequestPriority(3)); 25 | EXPECT_EQ(IDLE, ConvertQuicPriorityToRequestPriority(4)); 26 | // These are invalid values, but we should still handle them 27 | // gracefully. TODO(rtenneti): should we test for all possible values of 28 | // uint32? 29 | for (int i = 5; i < kuint8max; ++i) { 30 | EXPECT_EQ(IDLE, ConvertQuicPriorityToRequestPriority(i)); 31 | } 32 | } 33 | 34 | } // namespace test 35 | } // namespace net 36 | -------------------------------------------------------------------------------- /src/net/quic_packet_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_QUIC_PACKET_WRITER_H_ 6 | #define NET_QUIC_QUIC_PACKET_WRITER_H_ 7 | 8 | #include "net/base/ip_endpoint.h" 9 | #include "net/quic/quic_protocol.h" 10 | 11 | namespace net { 12 | 13 | struct WriteResult; 14 | 15 | // An interface between writers and the entity managing the 16 | // socket (in our case the QuicDispatcher). This allows the Dispatcher to 17 | // control writes, and manage any writers who end up write blocked. 18 | class NET_EXPORT_PRIVATE QuicPacketWriter { 19 | public: 20 | virtual ~QuicPacketWriter() {} 21 | 22 | // Sends the packet out to the peer. If the write succeeded, the result's 23 | // status is WRITE_STATUS_OK and bytes_written is populated. If the write 24 | // failed, the result's status is WRITE_STATUS_BLOCKED or WRITE_STATUS_ERROR 25 | // and error_code is populated. 26 | virtual WriteResult WritePacket( 27 | const char* buffer, size_t buf_len, 28 | const IPAddressNumber& self_address, 29 | const IPEndPoint& peer_address) = 0; 30 | 31 | // Returns true if the writer buffers and subsequently rewrites data 32 | // when an attempt to write results in the underlying socket becoming 33 | // write blocked. 34 | virtual bool IsWriteBlockedDataBuffered() const = 0; 35 | 36 | // Returns true if the network socket is not writable. 37 | virtual bool IsWriteBlocked() const = 0; 38 | 39 | // Records that the socket has become writable, for example when an EPOLLOUT 40 | // is received or an asynchronous write completes. 41 | virtual void SetWritable() = 0; 42 | }; 43 | 44 | } // namespace net 45 | 46 | #endif // NET_QUIC_QUIC_PACKET_WRITER_H_ 47 | -------------------------------------------------------------------------------- /src/net/quic_sent_entropy_manager.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | // Manages the packet entropy calculation for both sent and received packets 6 | // for a connection. 7 | 8 | #ifndef NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_ 9 | #define NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_ 10 | 11 | #include "net/base/linked_hash_map.h" 12 | #include "net/quic/quic_framer.h" 13 | #include "net/quic/quic_protocol.h" 14 | 15 | namespace net { 16 | 17 | // Records all sent packets by a connection to track the cumulative entropy of 18 | // sent packets. It is used by the connection to validate an ack 19 | // frame sent by the peer as a preventive measure against the optimistic ack 20 | // attack. 21 | class NET_EXPORT_PRIVATE QuicSentEntropyManager { 22 | public: 23 | QuicSentEntropyManager(); 24 | virtual ~QuicSentEntropyManager(); 25 | 26 | // Record |entropy_hash| for sent packet corresponding to |sequence_number|. 27 | void RecordPacketEntropyHash(QuicPacketSequenceNumber sequence_number, 28 | QuicPacketEntropyHash entropy_hash); 29 | 30 | QuicPacketEntropyHash EntropyHash( 31 | QuicPacketSequenceNumber sequence_number) const; 32 | 33 | // Returns true if |entropy_hash| matches the expected sent entropy hash 34 | // up to |sequence_number| removing sequence numbers from |missing_packets|. 35 | bool IsValidEntropy(QuicPacketSequenceNumber sequence_number, 36 | const SequenceNumberSet& missing_packets, 37 | QuicPacketEntropyHash entropy_hash) const; 38 | 39 | // Removes not required entries from |packets_entropy_| before 40 | // |sequence_number|. 41 | void ClearEntropyBefore(QuicPacketSequenceNumber sequence_number); 42 | 43 | private: 44 | typedef linked_hash_map > SentEntropyMap; 47 | 48 | // Linked hash map from sequence numbers to the sent entropy hash up to the 49 | // sequence number in the key. 50 | SentEntropyMap packets_entropy_; 51 | 52 | // Cumulative hash of entropy of all sent packets. 53 | QuicPacketEntropyHash packets_entropy_hash_; 54 | }; 55 | 56 | } // namespace net 57 | 58 | #endif // NET_QUIC_QUIC_SENT_ENTROPY_MANAGER_H_ 59 | -------------------------------------------------------------------------------- /src/net/quic_sent_entropy_manager_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_sent_entropy_manager.h" 6 | 7 | #include 8 | #include 9 | 10 | #include "testing/gmock/include/gmock/gmock.h" 11 | #include "testing/gtest/include/gtest/gtest.h" 12 | 13 | using std::make_pair; 14 | using std::pair; 15 | using std::vector; 16 | 17 | namespace net { 18 | namespace test { 19 | namespace { 20 | 21 | class QuicSentEntropyManagerTest : public ::testing::Test { 22 | protected: 23 | QuicSentEntropyManager entropy_manager_; 24 | }; 25 | 26 | TEST_F(QuicSentEntropyManagerTest, SentEntropyHash) { 27 | EXPECT_EQ(0, entropy_manager_.EntropyHash(0)); 28 | 29 | vector > entropies; 30 | entropies.push_back(make_pair(1, 12)); 31 | entropies.push_back(make_pair(2, 1)); 32 | entropies.push_back(make_pair(3, 33)); 33 | entropies.push_back(make_pair(4, 3)); 34 | 35 | for (size_t i = 0; i < entropies.size(); ++i) { 36 | entropy_manager_.RecordPacketEntropyHash(entropies[i].first, 37 | entropies[i].second); 38 | } 39 | 40 | QuicPacketEntropyHash hash = 0; 41 | for (size_t i = 0; i < entropies.size(); ++i) { 42 | hash ^= entropies[i].second; 43 | EXPECT_EQ(hash, entropy_manager_.EntropyHash(i + 1)); 44 | } 45 | } 46 | 47 | TEST_F(QuicSentEntropyManagerTest, IsValidEntropy) { 48 | QuicPacketEntropyHash entropies[10] = 49 | {12, 1, 33, 3, 32, 100, 28, 42, 22, 255}; 50 | for (size_t i = 0; i < 10; ++i) { 51 | entropy_manager_.RecordPacketEntropyHash(i + 1, entropies[i]); 52 | } 53 | 54 | SequenceNumberSet missing_packets; 55 | missing_packets.insert(1); 56 | missing_packets.insert(4); 57 | missing_packets.insert(7); 58 | missing_packets.insert(8); 59 | 60 | QuicPacketEntropyHash entropy_hash = 0; 61 | for (size_t i = 0; i < 10; ++i) { 62 | if (missing_packets.find(i + 1) == missing_packets.end()) { 63 | entropy_hash ^= entropies[i]; 64 | } 65 | } 66 | 67 | EXPECT_TRUE(entropy_manager_.IsValidEntropy(10, missing_packets, 68 | entropy_hash)); 69 | } 70 | 71 | } // namespace 72 | } // namespace test 73 | } // namespace net 74 | -------------------------------------------------------------------------------- /src/net/quic_socket_address_coder.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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 | #ifndef NET_QUIC_QUIC_SOCKET_ADDRESS_CODER_H_ 6 | #define NET_QUIC_QUIC_SOCKET_ADDRESS_CODER_H_ 7 | 8 | #include 9 | 10 | #include "base/basictypes.h" 11 | #include "net/base/ip_endpoint.h" 12 | #include "net/base/net_export.h" 13 | 14 | namespace net { 15 | 16 | // Serializes and parses a socket address (IP address and port), to be used in 17 | // the kCADR tag in the ServerHello handshake message and the Public Reset 18 | // packet. 19 | class NET_EXPORT_PRIVATE QuicSocketAddressCoder { 20 | public: 21 | QuicSocketAddressCoder(); 22 | explicit QuicSocketAddressCoder(const IPEndPoint& address); 23 | ~QuicSocketAddressCoder(); 24 | 25 | std::string Encode() const; 26 | 27 | bool Decode(const char* data, size_t length); 28 | 29 | IPAddressNumber ip() const { 30 | return address_.address(); 31 | } 32 | 33 | uint16 port() const { 34 | return address_.port(); 35 | } 36 | 37 | private: 38 | IPEndPoint address_; 39 | DISALLOW_COPY_AND_ASSIGN(QuicSocketAddressCoder); 40 | }; 41 | 42 | } // namespace net 43 | 44 | #endif // NET_QUIC_QUIC_SOCKET_ADDRESS_CODER_H_ 45 | -------------------------------------------------------------------------------- /src/net/quic_spdy_compressor.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_QUIC_SPDY_COMPRESSOR_H_ 6 | #define NET_QUIC_QUIC_SPDY_COMPRESSOR_H_ 7 | 8 | #include 9 | 10 | #include "net/base/net_export.h" 11 | #include "net/quic/quic_protocol.h" 12 | #include "net/spdy/spdy_framer.h" 13 | 14 | namespace net { 15 | 16 | // Handles the compression of request/response headers blocks. The 17 | // serialized format is: 18 | // uint32 - Priority 19 | // uint32 - Header ID 20 | // uint32 - Compressed header length 21 | // ... - Compressed data 22 | // 23 | class NET_EXPORT_PRIVATE QuicSpdyCompressor { 24 | public: 25 | QuicSpdyCompressor(); 26 | ~QuicSpdyCompressor(); 27 | 28 | // Returns a string comprised of [header_sequence_id, compressed_headers]. 29 | std::string CompressHeaders(const SpdyHeaderBlock& headers); 30 | 31 | // Returns a string comprised of 32 | // [priority, header_sequence_id, compressed_headers] 33 | std::string CompressHeadersWithPriority(QuicPriority priority, 34 | const SpdyHeaderBlock& headers); 35 | 36 | private: 37 | std::string CompressHeadersInternal(QuicPriority priority, 38 | const SpdyHeaderBlock& headers, 39 | bool write_priority); 40 | 41 | SpdyFramer spdy_framer_; 42 | QuicHeaderId header_sequence_id_; 43 | 44 | DISALLOW_COPY_AND_ASSIGN(QuicSpdyCompressor); 45 | }; 46 | 47 | } // namespace net 48 | 49 | #endif // NET_QUIC_QUIC_SPDY_COMPRESSOR_H_ 50 | -------------------------------------------------------------------------------- /src/net/quic_spdy_compressor_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 6 | 7 | #include "net/quic/quic_spdy_compressor.h" 8 | #include "net/quic/quic_spdy_decompressor.h" 9 | #include "net/quic/spdy_utils.h" 10 | #include "net/quic/test_tools/quic_test_utils.h" 11 | #include "testing/gtest/include/gtest/gtest.h" 12 | 13 | using std::string; 14 | 15 | namespace net { 16 | namespace test { 17 | namespace { 18 | 19 | class QuicSpdyCompressorTest : public ::testing::Test { 20 | protected: 21 | TestDecompressorVisitor visitor_; 22 | }; 23 | 24 | TEST_F(QuicSpdyCompressorTest, Compress) { 25 | QuicSpdyCompressor compressor; 26 | QuicSpdyDecompressor decompressor; 27 | 28 | SpdyHeaderBlock headers; 29 | headers[":host"] = "www.google.com"; 30 | headers[":path"] = "/index.hml"; 31 | headers[":scheme"] = "https"; 32 | 33 | string compressed_headers = compressor.CompressHeaders(headers); 34 | EXPECT_EQ('\1', compressed_headers[0]); 35 | EXPECT_EQ('\0', compressed_headers[1]); 36 | EXPECT_EQ('\0', compressed_headers[2]); 37 | EXPECT_EQ('\0', compressed_headers[3]); 38 | string compressed_data = compressed_headers.substr(4); 39 | decompressor.DecompressData(compressed_data, &visitor_); 40 | EXPECT_EQ(SpdyUtils::SerializeUncompressedHeaders(headers), 41 | visitor_.data()); 42 | } 43 | 44 | } // namespace 45 | } // namespace test 46 | } // namespace net 47 | -------------------------------------------------------------------------------- /src/net/quic_spdy_decompressor.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_QUIC_SPDY_DECOMPRESSOR_H_ 6 | #define NET_QUIC_QUIC_SPDY_DECOMPRESSOR_H_ 7 | 8 | #include 9 | 10 | #include "base/basictypes.h" 11 | #include "base/memory/scoped_ptr.h" 12 | #include "base/strings/string_piece.h" 13 | #include "net/base/net_export.h" 14 | #include "net/quic/quic_protocol.h" 15 | #include "net/spdy/spdy_framer.h" 16 | 17 | namespace net { 18 | 19 | class SpdyFramerVisitor; 20 | 21 | // Handles the compression of request/response headers blocks. 22 | class NET_EXPORT_PRIVATE QuicSpdyDecompressor { 23 | public: 24 | // Interface that receives callbacks with decompressed data as it 25 | // becomes available. 26 | class NET_EXPORT_PRIVATE Visitor { 27 | public: 28 | virtual ~Visitor() {} 29 | virtual bool OnDecompressedData(base::StringPiece data) = 0; 30 | virtual void OnDecompressionError() = 0; 31 | }; 32 | 33 | QuicSpdyDecompressor(); 34 | ~QuicSpdyDecompressor(); 35 | 36 | // Decompresses the data in |data| and invokes |OnDecompressedData|, 37 | // possibly multiple times, on |visitor|. Returns number of bytes 38 | // consumed from |data|. 39 | size_t DecompressData(base::StringPiece data, Visitor* visitor); 40 | 41 | QuicHeaderId current_header_id() { return current_header_id_; } 42 | 43 | private: 44 | void ResetForNextHeaders(); 45 | 46 | SpdyFramer spdy_framer_; 47 | scoped_ptr spdy_visitor_; 48 | // ID of the header currently being parsed. 49 | QuicHeaderId current_header_id_; 50 | // True when the size of the headers has been parsed. 51 | bool has_current_compressed_size_; 52 | // Size of the headers being parsed. 53 | uint32 current_compressed_size_; 54 | // Buffer into which the partial compressed size is written until 55 | // it is fully parsed. 56 | std::string compressed_size_buffer_; 57 | // Number of compressed bytes consumed, out of the total in 58 | // |current_compressed_size_|. 59 | uint32 compressed_bytes_consumed_; 60 | DISALLOW_COPY_AND_ASSIGN(QuicSpdyDecompressor); 61 | }; 62 | 63 | } // namespace net 64 | 65 | #endif // NET_QUIC_QUIC_SPDY_DECOMPRESSOR_H_ 66 | -------------------------------------------------------------------------------- /src/net/quic_utils_chromium_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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_utils_chromium.h" 6 | 7 | #include 8 | 9 | #include "testing/gtest/include/gtest/gtest.h" 10 | 11 | using std::map; 12 | 13 | namespace net { 14 | namespace test { 15 | namespace { 16 | 17 | TEST(QuicUtilsChromiumTest, FindOrNullTest) { 18 | map m; 19 | m[0] = 2; 20 | 21 | // Check FindOrNull 22 | int* p1 = FindOrNull(m, 0); 23 | CHECK_EQ(*p1, 2); 24 | ++(*p1); 25 | const map& const_m = m; 26 | const int* p2 = FindOrNull(const_m, 0); 27 | CHECK_EQ(*p2, 3); 28 | CHECK(FindOrNull(m, 1) == NULL); 29 | } 30 | 31 | TEST(QuicUtilsChromiumTest, FindOrDieTest) { 32 | std::map m; 33 | m[10] = 15; 34 | EXPECT_EQ(15, FindOrDie(m, 10)); 35 | // TODO(rtenneti): Use the latest DEATH macros after merging with latest rch's 36 | // changes. 37 | // ASSERT_DEATH(FindOrDie(m, 8), "Map key not found: 8"); 38 | 39 | // Make sure the non-const reference returning version works. 40 | FindOrDie(m, 10) = 20; 41 | EXPECT_EQ(20, FindOrDie(m, 10)); 42 | 43 | // Make sure we can lookup values in a const map. 44 | const map& const_m = m; 45 | EXPECT_EQ(20, FindOrDie(const_m, 10)); 46 | } 47 | 48 | } // namespace 49 | } // namespace test 50 | } // namespace net 51 | -------------------------------------------------------------------------------- /src/net/quic_write_blocked_list.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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_write_blocked_list.h" 6 | 7 | namespace net { 8 | 9 | const QuicPriority QuicWriteBlockedList::kHighestPriority = 10 | static_cast(net::kHighestPriority); 11 | const QuicPriority QuicWriteBlockedList::kLowestPriority = 12 | static_cast(net::kLowestPriority); 13 | 14 | QuicWriteBlockedList::QuicWriteBlockedList() 15 | : crypto_stream_blocked_(false), 16 | headers_stream_blocked_(false) {} 17 | 18 | QuicWriteBlockedList::~QuicWriteBlockedList() {} 19 | 20 | } // namespace net 21 | -------------------------------------------------------------------------------- /src/net/spdy_utils.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/spdy_utils.h" 6 | 7 | #include "base/memory/scoped_ptr.h" 8 | #include "net/spdy/spdy_frame_builder.h" 9 | #include "net/spdy/spdy_framer.h" 10 | #include "net/spdy/spdy_protocol.h" 11 | 12 | using std::string; 13 | 14 | namespace net { 15 | 16 | // static 17 | string SpdyUtils::SerializeUncompressedHeaders(const SpdyHeaderBlock& headers) { 18 | int length = SpdyFramer::GetSerializedLength(SPDY3, &headers); 19 | SpdyFrameBuilder builder(length); 20 | SpdyFramer::WriteHeaderBlock(&builder, SPDY3, &headers); 21 | scoped_ptr block(builder.take()); 22 | return string(block->data(), length); 23 | } 24 | 25 | } // namespace net 26 | -------------------------------------------------------------------------------- /src/net/spdy_utils.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_SPDY_UTILS_H_ 6 | #define NET_QUIC_SPDY_UTILS_H_ 7 | 8 | #include 9 | 10 | #include "net/base/net_export.h" 11 | #include "net/spdy/spdy_framer.h" 12 | 13 | namespace net { 14 | 15 | class NET_EXPORT_PRIVATE SpdyUtils { 16 | public: 17 | static std::string SerializeUncompressedHeaders( 18 | const SpdyHeaderBlock& headers); 19 | }; 20 | 21 | } // namespace net 22 | 23 | #endif // NET_QUIC_SPDY_UTILS_H_ 24 | -------------------------------------------------------------------------------- /src/net/test_tools/crypto_test_utils_chromium.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/test_tools/crypto_test_utils.h" 6 | 7 | #include "base/memory/ref_counted.h" 8 | #include "base/memory/scoped_ptr.h" 9 | #include "net/base/test_data_directory.h" 10 | #include "net/cert/cert_verifier.h" 11 | #include "net/cert/test_root_certs.h" 12 | #include "net/cert/x509_certificate.h" 13 | #include "net/quic/crypto/proof_source_chromium.h" 14 | #include "net/quic/crypto/proof_verifier_chromium.h" 15 | #include "net/test/cert_test_util.h" 16 | 17 | namespace net { 18 | 19 | namespace test { 20 | 21 | class TestProofVerifierChromium : public ProofVerifierChromium { 22 | public: 23 | TestProofVerifierChromium(CertVerifier* cert_verifier, 24 | const std::string& cert_file) 25 | : ProofVerifierChromium(cert_verifier, BoundNetLog()), 26 | cert_verifier_(cert_verifier) { 27 | // Load and install the root for the validated chain. 28 | scoped_refptr root_cert = 29 | ImportCertFromFile(GetTestCertsDirectory(), cert_file); 30 | scoped_root_.Reset(root_cert.get()); 31 | } 32 | virtual ~TestProofVerifierChromium() { } 33 | 34 | private: 35 | ScopedTestRoot scoped_root_; 36 | scoped_ptr cert_verifier_; 37 | }; 38 | 39 | // static 40 | ProofSource* CryptoTestUtils::ProofSourceForTesting() { 41 | return new ProofSourceChromium(); 42 | } 43 | 44 | // static 45 | ProofVerifier* CryptoTestUtils::ProofVerifierForTesting() { 46 | TestProofVerifierChromium* proof_verifier = new TestProofVerifierChromium( 47 | CertVerifier::CreateDefault(), "quic_root.crt"); 48 | return proof_verifier; 49 | } 50 | 51 | } // namespace test 52 | 53 | } // namespace net 54 | -------------------------------------------------------------------------------- /src/net/test_tools/delayed_verify_strike_register_client.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/test_tools/delayed_verify_strike_register_client.h" 6 | 7 | using base::StringPiece; 8 | using std::string; 9 | using std::vector; 10 | 11 | namespace net { 12 | namespace test { 13 | 14 | DelayedVerifyStrikeRegisterClient::DelayedVerifyStrikeRegisterClient( 15 | unsigned max_entries, 16 | uint32 current_time_external, 17 | uint32 window_secs, 18 | const uint8 orbit[8], 19 | StrikeRegister::StartupType startup) 20 | : LocalStrikeRegisterClient(max_entries, current_time_external, 21 | window_secs, orbit, startup), 22 | delay_verifications_(false) { 23 | } 24 | 25 | DelayedVerifyStrikeRegisterClient::~DelayedVerifyStrikeRegisterClient() {} 26 | 27 | void DelayedVerifyStrikeRegisterClient::VerifyNonceIsValidAndUnique( 28 | StringPiece nonce, 29 | QuicWallTime now, 30 | ResultCallback* cb) { 31 | if (delay_verifications_) { 32 | pending_verifications_.push_back(VerifyArgs(nonce.as_string(), now, cb)); 33 | } else { 34 | LocalStrikeRegisterClient::VerifyNonceIsValidAndUnique(nonce, now, cb); 35 | } 36 | } 37 | 38 | int DelayedVerifyStrikeRegisterClient::PendingVerifications() const { 39 | return pending_verifications_.size(); 40 | } 41 | 42 | void DelayedVerifyStrikeRegisterClient::RunPendingVerifications() { 43 | vector pending; 44 | pending_verifications_.swap(pending); 45 | for (vector::const_iterator it = pending.begin(), 46 | end = pending.end(); it != end; ++it) { 47 | LocalStrikeRegisterClient::VerifyNonceIsValidAndUnique( 48 | it->nonce, it->now, it->cb); 49 | } 50 | } 51 | 52 | } // namespace test 53 | } // namespace net 54 | -------------------------------------------------------------------------------- /src/net/test_tools/delayed_verify_strike_register_client.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_TEST_TOOLS_DELAYED_VERIFY_STRIKE_REGISTER_CLIENT_H_ 6 | #define NET_QUIC_TEST_TOOLS_DELAYED_VERIFY_STRIKE_REGISTER_CLIENT_H_ 7 | 8 | #include 9 | #include 10 | 11 | #include "base/strings/string_piece.h" 12 | #include "net/quic/crypto/local_strike_register_client.h" 13 | 14 | namespace net { 15 | namespace test { 16 | 17 | // Test helper that allows delaying execution of nonce verification 18 | // callbacks until a later time. 19 | class DelayedVerifyStrikeRegisterClient : public LocalStrikeRegisterClient { 20 | public: 21 | DelayedVerifyStrikeRegisterClient(unsigned max_entries, 22 | uint32 current_time_external, 23 | uint32 window_secs, 24 | const uint8 orbit[8], 25 | StrikeRegister::StartupType startup); 26 | virtual ~DelayedVerifyStrikeRegisterClient(); 27 | 28 | virtual void VerifyNonceIsValidAndUnique(base::StringPiece nonce, 29 | QuicWallTime now, 30 | ResultCallback* cb) OVERRIDE; 31 | 32 | // Start queueing verifications instead of executing them immediately. 33 | void StartDelayingVerification() { 34 | delay_verifications_ = true; 35 | } 36 | // Number of verifications that are queued. 37 | int PendingVerifications() const; 38 | // Run all pending verifications. 39 | void RunPendingVerifications(); 40 | 41 | private: 42 | struct VerifyArgs { 43 | VerifyArgs(base::StringPiece in_nonce, 44 | QuicWallTime in_now, 45 | ResultCallback* in_cb) 46 | : nonce(in_nonce.as_string()), 47 | now(in_now), 48 | cb(in_cb) { 49 | } 50 | 51 | std::string nonce; 52 | QuicWallTime now; 53 | ResultCallback* cb; 54 | }; 55 | 56 | bool delay_verifications_; 57 | std::vector pending_verifications_; 58 | 59 | DISALLOW_COPY_AND_ASSIGN(DelayedVerifyStrikeRegisterClient); 60 | }; 61 | 62 | } // namespace test 63 | } // namespace net 64 | 65 | #endif // NET_QUIC_TEST_TOOLS_DELAYED_VERIFY_STRIKE_REGISTER_CLIENT_H_ 66 | -------------------------------------------------------------------------------- /src/net/test_tools/mock_clock.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/quic/test_tools/mock_clock.h" 6 | 7 | namespace net { 8 | 9 | MockClock::MockClock() : now_(QuicTime::Zero()) { 10 | } 11 | 12 | MockClock::~MockClock() { 13 | } 14 | 15 | void MockClock::AdvanceTime(QuicTime::Delta delta) { 16 | now_ = now_.Add(delta); 17 | } 18 | 19 | QuicTime MockClock::Now() const { 20 | return now_; 21 | } 22 | 23 | QuicTime MockClock::ApproximateNow() const { 24 | return now_; 25 | } 26 | 27 | QuicWallTime MockClock::WallNow() const { 28 | return QuicWallTime::FromUNIXSeconds( 29 | now_.Subtract(QuicTime::Zero()).ToSeconds()); 30 | } 31 | 32 | base::TimeTicks MockClock::NowInTicks() const { 33 | base::TimeTicks ticks; 34 | return ticks + base::TimeDelta::FromMicroseconds( 35 | now_.Subtract(QuicTime::Zero()).ToMicroseconds()); 36 | } 37 | 38 | } // namespace net 39 | -------------------------------------------------------------------------------- /src/net/test_tools/mock_clock.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_TEST_TOOLS_MOCK_CLOCK_H_ 6 | #define NET_QUIC_TEST_TOOLS_MOCK_CLOCK_H_ 7 | 8 | #include "net/quic/quic_clock.h" 9 | 10 | #include "base/compiler_specific.h" 11 | #include "base/logging.h" 12 | #include "base/time/time.h" 13 | 14 | namespace net { 15 | 16 | class MockClock : public QuicClock { 17 | public: 18 | MockClock(); 19 | virtual ~MockClock(); 20 | 21 | void AdvanceTime(QuicTime::Delta delta); 22 | 23 | virtual QuicTime Now() const OVERRIDE; 24 | 25 | virtual QuicTime ApproximateNow() const OVERRIDE; 26 | 27 | virtual QuicWallTime WallNow() const OVERRIDE; 28 | 29 | base::TimeTicks NowInTicks() const; 30 | 31 | private: 32 | QuicTime now_; 33 | }; 34 | 35 | } // namespace net 36 | 37 | #endif // NET_QUIC_TEST_TOOLS_MOCK_CLOCK_H_ 38 | -------------------------------------------------------------------------------- /src/net/test_tools/mock_crypto_client_stream.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_TEST_TOOLS_MOCK_CRYPTO_CLIENT_STREAM_H_ 6 | #define NET_QUIC_TEST_TOOLS_MOCK_CRYPTO_CLIENT_STREAM_H_ 7 | 8 | #include 9 | 10 | #include "net/quic/crypto/crypto_handshake.h" 11 | #include "net/quic/crypto/crypto_protocol.h" 12 | #include "net/quic/quic_crypto_client_stream.h" 13 | #include "net/quic/quic_session.h" 14 | 15 | namespace net { 16 | 17 | class MockCryptoClientStream : public QuicCryptoClientStream { 18 | public: 19 | // HandshakeMode enumerates the handshake mode MockCryptoClientStream should 20 | // mock in CryptoConnect. 21 | enum HandshakeMode { 22 | // CONFIRM_HANDSHAKE indicates that CryptoConnect will immediately confirm 23 | // the handshake and establish encryption. This behavior will never happen 24 | // in the field, but is convenient for higher level tests. 25 | CONFIRM_HANDSHAKE, 26 | 27 | // ZERO_RTT indicates that CryptoConnect will establish encryption but will 28 | // not confirm the handshake. 29 | ZERO_RTT, 30 | 31 | // COLD_START indicates that CryptoConnect will neither establish encryption 32 | // nor confirm the handshake 33 | COLD_START, 34 | }; 35 | 36 | MockCryptoClientStream( 37 | const string& server_hostname, 38 | QuicSession* session, 39 | QuicCryptoClientConfig* crypto_config, 40 | HandshakeMode handshake_mode, 41 | const SSLInfo* ssl_info); 42 | virtual ~MockCryptoClientStream(); 43 | 44 | // CryptoFramerVisitorInterface implementation. 45 | virtual void OnHandshakeMessage( 46 | const CryptoHandshakeMessage& message) OVERRIDE; 47 | 48 | // QuicCryptoClientStream implementation. 49 | virtual bool CryptoConnect() OVERRIDE; 50 | 51 | virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE; 52 | 53 | // Invokes the sessions's CryptoHandshakeEvent method with the specified 54 | // event. 55 | void SendOnCryptoHandshakeEvent(QuicSession::CryptoHandshakeEvent event); 56 | 57 | HandshakeMode handshake_mode_; 58 | 59 | private: 60 | void SetConfigNegotiated(); 61 | 62 | const SSLInfo* ssl_info_; 63 | }; 64 | 65 | } // namespace net 66 | 67 | #endif // NET_QUIC_TEST_TOOLS_MOCK_CRYPTO_CLIENT_STREAM_H_ 68 | -------------------------------------------------------------------------------- /src/net/test_tools/mock_crypto_client_stream_factory.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/test_tools/mock_crypto_client_stream_factory.h" 6 | 7 | #include "base/lazy_instance.h" 8 | #include "net/quic/quic_crypto_client_stream.h" 9 | 10 | using std::string; 11 | 12 | namespace net { 13 | 14 | MockCryptoClientStreamFactory::MockCryptoClientStreamFactory() 15 | : handshake_mode_(MockCryptoClientStream::CONFIRM_HANDSHAKE), 16 | last_stream_(NULL), 17 | ssl_info_(NULL) { 18 | } 19 | 20 | QuicCryptoClientStream* 21 | MockCryptoClientStreamFactory::CreateQuicCryptoClientStream( 22 | const string& server_hostname, 23 | QuicSession* session, 24 | QuicCryptoClientConfig* crypto_config) { 25 | last_stream_ = new MockCryptoClientStream( 26 | server_hostname, session, crypto_config, handshake_mode_, ssl_info_); 27 | return last_stream_; 28 | } 29 | 30 | } // namespace net 31 | -------------------------------------------------------------------------------- /src/net/test_tools/mock_crypto_client_stream_factory.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_TEST_TOOLS_MOCK_CRYPTO_CLIENT_STREAM_FACTORY_H_ 6 | #define NET_QUIC_TEST_TOOLS_MOCK_CRYPTO_CLIENT_STREAM_FACTORY_H_ 7 | 8 | #include 9 | 10 | #include "net/quic/quic_crypto_client_stream.h" 11 | #include "net/quic/quic_crypto_client_stream_factory.h" 12 | #include "net/quic/test_tools/mock_crypto_client_stream.h" 13 | 14 | namespace net { 15 | 16 | class MockCryptoClientStreamFactory : public QuicCryptoClientStreamFactory { 17 | public: 18 | MockCryptoClientStreamFactory(); 19 | virtual ~MockCryptoClientStreamFactory() {} 20 | 21 | virtual QuicCryptoClientStream* CreateQuicCryptoClientStream( 22 | const string& server_hostname, 23 | QuicSession* session, 24 | QuicCryptoClientConfig* crypto_config) OVERRIDE; 25 | 26 | void set_handshake_mode( 27 | MockCryptoClientStream::HandshakeMode handshake_mode) { 28 | handshake_mode_ = handshake_mode; 29 | } 30 | 31 | void set_ssl_info(const SSLInfo* ssl_info) { 32 | ssl_info_ = ssl_info; 33 | } 34 | 35 | MockCryptoClientStream* last_stream() const { 36 | return last_stream_; 37 | } 38 | 39 | private: 40 | MockCryptoClientStream::HandshakeMode handshake_mode_; 41 | MockCryptoClientStream* last_stream_; 42 | const SSLInfo* ssl_info_; 43 | }; 44 | 45 | } // namespace net 46 | 47 | #endif // NET_QUIC_TEST_TOOLS_MOCK_CRYPTO_CLIENT_STREAM_FACTORY_H_ 48 | -------------------------------------------------------------------------------- /src/net/test_tools/mock_random.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/quic/test_tools/mock_random.h" 6 | 7 | namespace net { 8 | 9 | MockRandom::MockRandom() 10 | : base_(0xDEADBEEF), 11 | increment_(0) { 12 | } 13 | 14 | MockRandom::MockRandom(uint32 base) 15 | : base_(base), 16 | increment_(0) { 17 | } 18 | 19 | void MockRandom::RandBytes(void* data, size_t len) { 20 | memset(data, 'r' + increment_, len); 21 | } 22 | 23 | uint64 MockRandom::RandUint64() { 24 | return base_ + increment_; 25 | } 26 | 27 | void MockRandom::Reseed(const void* additional_entropy, size_t entropy_len) { 28 | } 29 | 30 | void MockRandom::ChangeValue() { 31 | increment_++; 32 | } 33 | 34 | } // namespace net 35 | -------------------------------------------------------------------------------- /src/net/test_tools/mock_random.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_TEST_TOOLS_MOCK_RANDOM_H_ 6 | #define NET_QUIC_TEST_TOOLS_MOCK_RANDOM_H_ 7 | 8 | #include "base/compiler_specific.h" 9 | #include "net/quic/crypto/quic_random.h" 10 | 11 | namespace net { 12 | 13 | class MockRandom : public QuicRandom { 14 | public: 15 | // Initializes base_ to 0xDEADBEEF. 16 | MockRandom(); 17 | explicit MockRandom(uint32 base); 18 | 19 | // QuicRandom: 20 | // Fills the |data| buffer with a repeating byte, initially 'r'. 21 | virtual void RandBytes(void* data, size_t len) OVERRIDE; 22 | // Returns base + the current increment. 23 | virtual uint64 RandUint64() OVERRIDE; 24 | // Does nothing. 25 | virtual void Reseed(const void* additional_entropy, 26 | size_t entropy_len) OVERRIDE; 27 | 28 | // ChangeValue increments |increment_|. This causes the value returned by 29 | // |RandUint64| and the byte that |RandBytes| fills with, to change. 30 | void ChangeValue(); 31 | 32 | private: 33 | uint32 base_; 34 | uint8 increment_; 35 | }; 36 | 37 | } // namespace net 38 | 39 | #endif // NET_QUIC_TEST_TOOLS_MOCK_RANDOM_H_ 40 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_client_session_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/test_tools/quic_client_session_peer.h" 6 | 7 | #include "net/quic/quic_client_session.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | // static 13 | void QuicClientSessionPeer::SetMaxOpenStreams(QuicClientSession* session, 14 | size_t max_streams, 15 | size_t default_streams) { 16 | session->config()->set_max_streams_per_connection(max_streams, 17 | default_streams); 18 | } 19 | 20 | } // namespace test 21 | } // namespace net 22 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_client_session_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_TEST_TOOLS_QUIC_CLIENT_SESSION_PEER_H_ 6 | #define NET_QUIC_TEST_TOOLS_QUIC_CLIENT_SESSION_PEER_H_ 7 | 8 | #include "net/quic/quic_protocol.h" 9 | 10 | namespace net { 11 | 12 | class QuicClientSession; 13 | 14 | namespace test { 15 | 16 | class QuicClientSessionPeer { 17 | public: 18 | static void SetMaxOpenStreams(QuicClientSession* session, 19 | size_t max_streams, 20 | size_t default_streams); 21 | 22 | private: 23 | DISALLOW_COPY_AND_ASSIGN(QuicClientSessionPeer); 24 | }; 25 | 26 | } // namespace test 27 | } // namespace net 28 | 29 | #endif // NET_QUIC_TEST_TOOLS_QUIC_CLIENT_SESSION_PEER_H_ 30 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_data_stream_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/test_tools/quic_data_stream_peer.h" 6 | 7 | #include "net/quic/quic_data_stream.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | // static 13 | void QuicDataStreamPeer::SetHeadersDecompressed(QuicDataStream* stream, 14 | bool headers_decompressed) { 15 | stream->headers_decompressed_ = headers_decompressed; 16 | } 17 | 18 | } // namespace test 19 | } // namespace net 20 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_data_stream_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_TEST_TOOLS_QUIC_DATA_STREAM_PEER_H_ 6 | #define NET_QUIC_TEST_TOOLS_QUIC_DATA_STREAM_PEER_H_ 7 | 8 | #include "base/basictypes.h" 9 | 10 | namespace net { 11 | 12 | class QuicDataStream; 13 | 14 | namespace test { 15 | 16 | class QuicDataStreamPeer { 17 | public: 18 | static void SetHeadersDecompressed(QuicDataStream* stream, 19 | bool headers_decompressed); 20 | 21 | private: 22 | DISALLOW_COPY_AND_ASSIGN(QuicDataStreamPeer); 23 | }; 24 | 25 | } // namespace test 26 | 27 | } // namespace net 28 | 29 | #endif // NET_QUIC_TEST_TOOLS_QUIC_DATA_STREAM_PEER_H_ 30 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_framer_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/test_tools/quic_framer_peer.h" 6 | 7 | #include "net/quic/quic_framer.h" 8 | #include "net/quic/quic_protocol.h" 9 | 10 | namespace net { 11 | namespace test { 12 | 13 | // static 14 | QuicPacketSequenceNumber QuicFramerPeer::CalculatePacketSequenceNumberFromWire( 15 | QuicFramer* framer, 16 | QuicSequenceNumberLength sequence_number_length, 17 | QuicPacketSequenceNumber packet_sequence_number) { 18 | return framer->CalculatePacketSequenceNumberFromWire(sequence_number_length, 19 | packet_sequence_number); 20 | } 21 | 22 | // static 23 | void QuicFramerPeer::SetLastSerializedGuid(QuicFramer* framer, QuicGuid guid) { 24 | framer->last_serialized_guid_ = guid; 25 | } 26 | 27 | void QuicFramerPeer::SetLastSequenceNumber( 28 | QuicFramer* framer, 29 | QuicPacketSequenceNumber packet_sequence_number) { 30 | framer->last_sequence_number_ = packet_sequence_number; 31 | } 32 | 33 | void QuicFramerPeer::SetIsServer(QuicFramer* framer, bool is_server) { 34 | framer->is_server_ = is_server; 35 | } 36 | 37 | } // namespace test 38 | } // namespace net 39 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_framer_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_TEST_TOOLS_QUIC_FRAMER_PEER_H_ 6 | #define NET_QUIC_TEST_TOOLS_QUIC_FRAMER_PEER_H_ 7 | 8 | #include "net/quic/quic_protocol.h" 9 | 10 | namespace net { 11 | 12 | class QuicFramer; 13 | 14 | namespace test { 15 | 16 | class QuicFramerPeer { 17 | public: 18 | static QuicPacketSequenceNumber CalculatePacketSequenceNumberFromWire( 19 | QuicFramer* framer, 20 | QuicSequenceNumberLength sequence_number_length, 21 | QuicPacketSequenceNumber packet_sequence_number); 22 | static void SetLastSerializedGuid(QuicFramer* framer, QuicGuid guid); 23 | static void SetLastSequenceNumber( 24 | QuicFramer* framer, 25 | QuicPacketSequenceNumber packet_sequence_number); 26 | static void SetIsServer(QuicFramer* framer, bool is_server); 27 | 28 | private: 29 | DISALLOW_COPY_AND_ASSIGN(QuicFramerPeer); 30 | }; 31 | 32 | } // namespace test 33 | 34 | } // namespace net 35 | 36 | #endif // NET_QUIC_TEST_TOOLS_QUIC_FRAMER_PEER_H_ 37 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_packet_creator_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/test_tools/quic_packet_creator_peer.h" 6 | 7 | #include "net/quic/quic_packet_creator.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | // static 13 | bool QuicPacketCreatorPeer::SendVersionInPacket(QuicPacketCreator* creator) { 14 | return creator->send_version_in_packet_; 15 | } 16 | 17 | // static 18 | void QuicPacketCreatorPeer::SetSendVersionInPacket( 19 | QuicPacketCreator* creator, bool send_version_in_packet) { 20 | creator->send_version_in_packet_ = send_version_in_packet; 21 | } 22 | 23 | // static 24 | void QuicPacketCreatorPeer::SetSequenceNumberLength( 25 | QuicPacketCreator* creator, 26 | QuicSequenceNumberLength sequence_number_length) { 27 | creator->sequence_number_length_ = sequence_number_length; 28 | } 29 | 30 | // static 31 | QuicSequenceNumberLength QuicPacketCreatorPeer::GetSequenceNumberLength( 32 | QuicPacketCreator* creator) { 33 | return creator->sequence_number_length_; 34 | } 35 | 36 | // static 37 | void QuicPacketCreatorPeer::SetIsServer(QuicPacketCreator* creator, 38 | bool is_server) { 39 | creator->is_server_ = is_server; 40 | } 41 | 42 | } // namespace test 43 | } // namespace net 44 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_packet_creator_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_QUIC_TEST_TOOLS_QUIC_PACKET_CREATOR_PEER_H_ 6 | #define NET_QUIC_TEST_TOOLS_QUIC_PACKET_CREATOR_PEER_H_ 7 | 8 | #include "net/quic/quic_protocol.h" 9 | 10 | namespace net { 11 | class QuicPacketCreator; 12 | 13 | namespace test { 14 | 15 | class QuicPacketCreatorPeer { 16 | public: 17 | static bool SendVersionInPacket(QuicPacketCreator* creator); 18 | 19 | static void SetSendVersionInPacket(QuicPacketCreator* creator, 20 | bool send_version_in_packet); 21 | static void SetSequenceNumberLength( 22 | QuicPacketCreator* creator, 23 | QuicSequenceNumberLength sequence_number_length); 24 | static QuicSequenceNumberLength GetSequenceNumberLength( 25 | QuicPacketCreator* creator); 26 | 27 | static void SetIsServer(QuicPacketCreator* creator, bool is_server); 28 | 29 | private: 30 | DISALLOW_COPY_AND_ASSIGN(QuicPacketCreatorPeer); 31 | }; 32 | 33 | } // namespace test 34 | 35 | } // namespace net 36 | 37 | #endif // NET_QUIC_TEST_TOOLS_QUIC_PACKET_CREATOR_PEER_H_ 38 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_received_packet_manager_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/test_tools/quic_received_packet_manager_peer.h" 6 | 7 | #include "net/quic/quic_protocol.h" 8 | #include "net/quic/quic_received_packet_manager.h" 9 | 10 | namespace net { 11 | namespace test { 12 | 13 | // static 14 | void QuicReceivedPacketManagerPeer::RecalculateEntropyHash( 15 | QuicReceivedPacketManager* received_packet_manager, 16 | QuicPacketSequenceNumber peer_least_unacked, 17 | QuicPacketEntropyHash entropy_hash) { 18 | received_packet_manager->RecalculateEntropyHash(peer_least_unacked, 19 | entropy_hash); 20 | } 21 | 22 | // static 23 | bool QuicReceivedPacketManagerPeer::DontWaitForPacketsBefore( 24 | QuicReceivedPacketManager* received_packet_manager, 25 | QuicPacketSequenceNumber least_unacked) { 26 | return received_packet_manager->DontWaitForPacketsBefore(least_unacked); 27 | } 28 | 29 | } // namespace test 30 | } // namespace net 31 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_received_packet_manager_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_QUIC_TEST_TOOLS_QUIC_RECEIVED_PACKET_MANAGER_PEER_H_ 6 | #define NET_QUIC_TEST_TOOLS_QUIC_RECEIVED_PACKET_MANAGER_PEER_H_ 7 | 8 | #include "net/quic/quic_protocol.h" 9 | 10 | namespace net { 11 | 12 | class QuicReceivedPacketManager; 13 | 14 | namespace test { 15 | 16 | class QuicReceivedPacketManagerPeer { 17 | public: 18 | static void RecalculateEntropyHash( 19 | QuicReceivedPacketManager* received_packet_manager, 20 | QuicPacketSequenceNumber peer_least_unacked, 21 | QuicPacketEntropyHash entropy_hash); 22 | 23 | static bool DontWaitForPacketsBefore( 24 | QuicReceivedPacketManager* received_packet_manager, 25 | QuicPacketSequenceNumber least_unacked); 26 | 27 | private: 28 | DISALLOW_COPY_AND_ASSIGN(QuicReceivedPacketManagerPeer); 29 | }; 30 | 31 | } // namespace test 32 | 33 | } // namespace net 34 | 35 | #endif // NET_QUIC_TEST_TOOLS_QUIC_RECEIVED_PACKET_MANAGER_PEER_H_ 36 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_session_peer.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/quic/test_tools/quic_session_peer.h" 6 | 7 | #include "net/quic/quic_session.h" 8 | #include "net/quic/reliable_quic_stream.h" 9 | 10 | namespace net { 11 | namespace test { 12 | 13 | // static 14 | void QuicSessionPeer::SetNextStreamId(QuicSession* session, QuicStreamId id) { 15 | session->next_stream_id_ = id; 16 | } 17 | 18 | // static 19 | void QuicSessionPeer::SetMaxOpenStreams(QuicSession* session, 20 | uint32 max_streams) { 21 | session->max_open_streams_ = max_streams; 22 | } 23 | 24 | // static 25 | QuicHeadersStream* QuicSessionPeer::GetHeadersStream(QuicSession* session) { 26 | return session->headers_stream_.get(); 27 | } 28 | 29 | // static 30 | QuicWriteBlockedList* QuicSessionPeer::GetWriteblockedStreams( 31 | QuicSession* session) { 32 | return &session->write_blocked_streams_; 33 | } 34 | 35 | // static 36 | QuicDataStream* QuicSessionPeer::GetIncomingDataStream( 37 | QuicSession* session, 38 | QuicStreamId stream_id) { 39 | return session->GetIncomingDataStream(stream_id); 40 | } 41 | 42 | } // namespace test 43 | } // namespace net 44 | -------------------------------------------------------------------------------- /src/net/test_tools/quic_session_peer.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_TEST_TOOLS_QUIC_SESSION_PEER_H_ 6 | #define NET_QUIC_TEST_TOOLS_QUIC_SESSION_PEER_H_ 7 | 8 | #include "net/quic/quic_protocol.h" 9 | #include "net/quic/quic_write_blocked_list.h" 10 | 11 | namespace net { 12 | 13 | class QuicDataStream; 14 | class QuicHeadersStream; 15 | class QuicSession; 16 | 17 | namespace test { 18 | 19 | class QuicSessionPeer { 20 | public: 21 | static void SetNextStreamId(QuicSession* session, QuicStreamId id); 22 | static void SetMaxOpenStreams(QuicSession* session, uint32 max_streams); 23 | static QuicHeadersStream* GetHeadersStream(QuicSession* session); 24 | static QuicWriteBlockedList* GetWriteblockedStreams(QuicSession* session); 25 | static QuicDataStream* GetIncomingDataStream(QuicSession* session, 26 | QuicStreamId stream_id); 27 | 28 | private: 29 | DISALLOW_COPY_AND_ASSIGN(QuicSessionPeer); 30 | }; 31 | 32 | } // namespace test 33 | } // namespace net 34 | 35 | #endif // NET_QUIC_TEST_TOOLS_QUIC_SESSION_PEER_H_ 36 | -------------------------------------------------------------------------------- /src/net/test_tools/reliable_quic_stream_peer.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/quic/test_tools/reliable_quic_stream_peer.h" 6 | 7 | #include "net/quic/reliable_quic_stream.h" 8 | 9 | namespace net { 10 | namespace test { 11 | 12 | // static 13 | void ReliableQuicStreamPeer::SetWriteSideClosed(bool value, 14 | ReliableQuicStream* stream) { 15 | stream->write_side_closed_ = value; 16 | } 17 | 18 | // static 19 | void ReliableQuicStreamPeer::SetStreamBytesWritten( 20 | QuicStreamOffset stream_bytes_written, 21 | ReliableQuicStream* stream) { 22 | stream->stream_bytes_written_ = stream_bytes_written; 23 | } 24 | 25 | // static 26 | void ReliableQuicStreamPeer::CloseReadSide(ReliableQuicStream* stream) { 27 | stream->CloseReadSide(); 28 | } 29 | 30 | // static 31 | bool ReliableQuicStreamPeer::FinSent(ReliableQuicStream* stream) { 32 | return stream->fin_sent_; 33 | } 34 | 35 | // static 36 | bool ReliableQuicStreamPeer::RstSent(ReliableQuicStream* stream) { 37 | return stream->rst_sent_; 38 | } 39 | 40 | } // namespace test 41 | } // namespace net 42 | -------------------------------------------------------------------------------- /src/net/test_tools/reliable_quic_stream_peer.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_TEST_TOOLS_RELIABLE_QUIC_STREAM_PEER_H_ 6 | #define NET_QUIC_TEST_TOOLS_RELIABLE_QUIC_STREAM_PEER_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "net/quic/quic_protocol.h" 10 | 11 | namespace net { 12 | 13 | class ReliableQuicStream; 14 | 15 | namespace test { 16 | 17 | class ReliableQuicStreamPeer { 18 | public: 19 | static void SetWriteSideClosed(bool value, ReliableQuicStream* stream); 20 | static void SetStreamBytesWritten(QuicStreamOffset stream_bytes_written, 21 | ReliableQuicStream* stream); 22 | static void CloseReadSide(ReliableQuicStream* stream); 23 | 24 | static bool FinSent(ReliableQuicStream* stream); 25 | static bool RstSent(ReliableQuicStream* stream); 26 | 27 | private: 28 | DISALLOW_COPY_AND_ASSIGN(ReliableQuicStreamPeer); 29 | }; 30 | 31 | } // namespace test 32 | } // namespace net 33 | 34 | #endif // NET_QUIC_TEST_TOOLS_RELIABLE_QUIC_STREAM_PEER_H_ 35 | -------------------------------------------------------------------------------- /src/net/test_tools/simple_quic_framer.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_QUIC_TEST_TOOLS_SIMPLE_QUIC_FRAMER_H_ 6 | #define NET_QUIC_TEST_TOOLS_SIMPLE_QUIC_FRAMER_H_ 7 | 8 | #include 9 | 10 | #include "base/basictypes.h" 11 | #include "base/memory/scoped_ptr.h" 12 | #include "net/quic/quic_framer.h" 13 | #include "net/quic/quic_protocol.h" 14 | 15 | namespace net { 16 | 17 | class CryptoHandshakeMessage; 18 | struct QuicAckFrame; 19 | class QuicConnection; 20 | class QuicConnectionVisitorInterface; 21 | class QuicPacketCreator; 22 | class ReceiveAlgorithmInterface; 23 | class SendAlgorithmInterface; 24 | 25 | namespace test { 26 | 27 | class SimpleFramerVisitor; 28 | 29 | // Peer to make public a number of otherwise private QuicFramer methods. 30 | class SimpleQuicFramer { 31 | public: 32 | SimpleQuicFramer(); 33 | explicit SimpleQuicFramer(const QuicVersionVector& supported_versions); 34 | ~SimpleQuicFramer(); 35 | 36 | bool ProcessPacket(const QuicEncryptedPacket& packet); 37 | bool ProcessPacket(const QuicPacket& packet); 38 | 39 | const QuicPacketHeader& header() const; 40 | size_t num_frames() const; 41 | const std::vector& ack_frames() const; 42 | const std::vector& connection_close_frames() const; 43 | const std::vector& feedback_frames() const; 44 | const std::vector& goaway_frames() const; 45 | const std::vector& rst_stream_frames() const; 46 | const std::vector& stream_frames() const; 47 | const QuicFecData& fec_data() const; 48 | QuicFramer* framer(); 49 | 50 | private: 51 | QuicFramer framer_; 52 | scoped_ptr visitor_; 53 | DISALLOW_COPY_AND_ASSIGN(SimpleQuicFramer); 54 | }; 55 | 56 | } // namespace test 57 | 58 | } // namespace net 59 | 60 | #endif // NET_QUIC_TEST_TOOLS_SIMPLE_QUIC_FRAMER_H_ 61 | -------------------------------------------------------------------------------- /src/net/test_tools/test_task_runner.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/quic/test_tools/test_task_runner.h" 6 | 7 | #include 8 | 9 | #include "net/quic/test_tools/mock_clock.h" 10 | #include "testing/gtest/include/gtest/gtest.h" 11 | 12 | namespace net { 13 | namespace test { 14 | 15 | TestTaskRunner::TestTaskRunner(MockClock* clock) 16 | : clock_(clock) { 17 | } 18 | 19 | TestTaskRunner::~TestTaskRunner() { 20 | } 21 | 22 | bool TestTaskRunner::PostDelayedTask(const tracked_objects::Location& from_here, 23 | const base::Closure& task, 24 | base::TimeDelta delay) { 25 | EXPECT_GE(delay, base::TimeDelta()); 26 | tasks_.push_back( 27 | PostedTask(from_here, task, clock_->NowInTicks(), delay, 28 | base::TestPendingTask::NESTABLE)); 29 | return false; 30 | } 31 | 32 | bool TestTaskRunner::RunsTasksOnCurrentThread() const { 33 | return true; 34 | } 35 | 36 | const std::vector& TestTaskRunner::GetPostedTasks() const { 37 | return tasks_; 38 | } 39 | 40 | void TestTaskRunner::RunNextTask() { 41 | // Find the next task to run, advance the time to the correct time 42 | // and then run the task. 43 | std::vector::iterator next = FindNextTask(); 44 | DCHECK(next != tasks_.end()); 45 | clock_->AdvanceTime(QuicTime::Delta::FromMicroseconds( 46 | (next->GetTimeToRun() - clock_->NowInTicks()).InMicroseconds())); 47 | PostedTask task = *next; 48 | tasks_.erase(next); 49 | task.task.Run(); 50 | } 51 | 52 | namespace { 53 | 54 | struct ShouldRunBeforeLessThan { 55 | bool operator()(const PostedTask& task1, const PostedTask& task2) const { 56 | return task1.ShouldRunBefore(task2); 57 | } 58 | }; 59 | 60 | } // namespace 61 | 62 | std::vector::iterator TestTaskRunner::FindNextTask() { 63 | return std::min_element( 64 | tasks_.begin(), tasks_.end(), ShouldRunBeforeLessThan()); 65 | } 66 | 67 | } // namespace test 68 | } // namespace net 69 | -------------------------------------------------------------------------------- /src/net/test_tools/test_task_runner.h: -------------------------------------------------------------------------------- 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 | // Common utilities for Quic tests 6 | 7 | #ifndef NET_QUIC_TEST_TOOLS_TEST_TASK_RUNNER_H_ 8 | #define NET_QUIC_TEST_TOOLS_TEST_TASK_RUNNER_H_ 9 | 10 | #include 11 | 12 | #include "base/basictypes.h" 13 | #include "base/task_runner.h" 14 | #include "base/test/test_pending_task.h" 15 | 16 | namespace net { 17 | 18 | class MockClock; 19 | 20 | namespace test { 21 | 22 | typedef base::TestPendingTask PostedTask; 23 | 24 | class TestTaskRunner : public base::TaskRunner { 25 | public: 26 | explicit TestTaskRunner(MockClock* clock); 27 | 28 | // base::TaskRunner implementation. 29 | virtual bool PostDelayedTask(const tracked_objects::Location& from_here, 30 | const base::Closure& task, 31 | base::TimeDelta delay) OVERRIDE; 32 | virtual bool RunsTasksOnCurrentThread() const OVERRIDE; 33 | 34 | const std::vector& GetPostedTasks() const; 35 | 36 | void RunNextTask(); 37 | 38 | protected: 39 | virtual ~TestTaskRunner(); 40 | 41 | private: 42 | std::vector::iterator FindNextTask(); 43 | 44 | MockClock* const clock_; 45 | std::vector tasks_; 46 | 47 | DISALLOW_COPY_AND_ASSIGN(TestTaskRunner); 48 | }; 49 | 50 | } // namespace test 51 | 52 | } // namespace net 53 | 54 | #endif // NET_QUIC_TEST_TOOLS_TEST_TASK_RUNNER_H_ 55 | -------------------------------------------------------------------------------- /src/tools/benchmark/test_urls.json: -------------------------------------------------------------------------------- 1 | { 2 | "description": "List of pages (URLs) for measuring quic performance using standalone quic client and server. These URLs are for testing only and are provided without guarantee.", 3 | "pages": [ 4 | { 5 | "url": "http://dev1.mdw.la/test/warmup.html", 6 | "why": "A warmup page." 7 | }, 8 | { 9 | "url": "http://dev1.mdw.la/test/test_1KB.jpg", 10 | "why": "A tiny page, about 1K Bytes." 11 | }, 12 | { 13 | "url": "http://dev1.mdw.la/test/test_10KB.jpg", 14 | "why": "A small page, about 10K Bytes." 15 | }, 16 | { 17 | "url": "http://dev1.mdw.la/test/test_100KB.jpg", 18 | "why": "A medium page, about 100K Bytes." 19 | }, 20 | { 21 | "url": "http://dev1.mdw.la/test/test_1MB.jpg", 22 | "why": "A large page, about 1M Bytes." 23 | }, 24 | { 25 | "url": "http://dev1.mdw.la/test/ten_img.html", 26 | "why": "A large page, with 1 html and 10 images totaling about 1M Bytes.", 27 | "resources": [ 28 | "imgs/test_100KB_0.jpg", 29 | "imgs/test_100KB_1.jpg", 30 | "imgs/test_100KB_2.jpg", 31 | "imgs/test_100KB_3.jpg", 32 | "imgs/test_100KB_4.jpg", 33 | "imgs/test_100KB_5.jpg", 34 | "imgs/test_100KB_6.jpg", 35 | "imgs/test_100KB_7.jpg", 36 | "imgs/test_100KB_8.jpg", 37 | "imgs/test_100KB_9.jpg" 38 | ] 39 | } 40 | ] 41 | } 42 | -------------------------------------------------------------------------------- /src/tools/quic_client_session.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_client_session.h" 6 | 7 | #include "base/logging.h" 8 | #include "net/quic/crypto/crypto_protocol.h" 9 | #include "net/tools/quic/quic_spdy_client_stream.h" 10 | 11 | using std::string; 12 | 13 | namespace net { 14 | namespace tools { 15 | 16 | QuicClientSession::QuicClientSession( 17 | const string& server_hostname, 18 | const QuicConfig& config, 19 | QuicConnection* connection, 20 | QuicCryptoClientConfig* crypto_config) 21 | : QuicSession(connection, config), 22 | crypto_stream_(server_hostname, this, crypto_config) { 23 | } 24 | 25 | QuicClientSession::~QuicClientSession() { 26 | } 27 | 28 | QuicSpdyClientStream* QuicClientSession::CreateOutgoingDataStream() { 29 | if (!crypto_stream_.encryption_established()) { 30 | DVLOG(1) << "Encryption not active so no outgoing stream created."; 31 | return NULL; 32 | } 33 | if (GetNumOpenStreams() >= get_max_open_streams()) { 34 | DVLOG(1) << "Failed to create a new outgoing stream. " 35 | << "Already " << GetNumOpenStreams() << " open."; 36 | return NULL; 37 | } 38 | if (goaway_received()) { 39 | DVLOG(1) << "Failed to create a new outgoing stream. " 40 | << "Already received goaway."; 41 | return NULL; 42 | } 43 | QuicSpdyClientStream* stream 44 | = new QuicSpdyClientStream(GetNextStreamId(), this); 45 | ActivateStream(stream); 46 | return stream; 47 | } 48 | 49 | QuicCryptoClientStream* QuicClientSession::GetCryptoStream() { 50 | return &crypto_stream_; 51 | } 52 | 53 | bool QuicClientSession::CryptoConnect() { 54 | return crypto_stream_.CryptoConnect(); 55 | } 56 | 57 | int QuicClientSession::GetNumSentClientHellos() const { 58 | return crypto_stream_.num_sent_client_hellos(); 59 | } 60 | 61 | QuicDataStream* QuicClientSession::CreateIncomingDataStream( 62 | QuicStreamId id) { 63 | DLOG(ERROR) << "Server push not supported"; 64 | return NULL; 65 | } 66 | 67 | } // namespace tools 68 | } // namespace net 69 | -------------------------------------------------------------------------------- /src/tools/quic_client_session.h: -------------------------------------------------------------------------------- 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 | // A client specific QuicSession subclass. 6 | 7 | #ifndef NET_TOOLS_QUIC_QUIC_CLIENT_SESSION_H_ 8 | #define NET_TOOLS_QUIC_QUIC_CLIENT_SESSION_H_ 9 | 10 | #include 11 | 12 | #include "base/basictypes.h" 13 | #include "net/quic/quic_crypto_client_stream.h" 14 | #include "net/quic/quic_protocol.h" 15 | #include "net/quic/quic_session.h" 16 | #include "net/tools/quic/quic_spdy_client_stream.h" 17 | 18 | namespace net { 19 | 20 | class QuicConnection; 21 | class ReliableQuicStream; 22 | 23 | namespace tools { 24 | 25 | class QuicClientSession : public QuicSession { 26 | public: 27 | QuicClientSession(const std::string& server_hostname, 28 | const QuicConfig& config, 29 | QuicConnection* connection, 30 | QuicCryptoClientConfig* crypto_config); 31 | virtual ~QuicClientSession(); 32 | 33 | // QuicSession methods: 34 | virtual QuicSpdyClientStream* CreateOutgoingDataStream() OVERRIDE; 35 | virtual QuicCryptoClientStream* GetCryptoStream() OVERRIDE; 36 | 37 | // Performs a crypto handshake with the server. Returns true if the crypto 38 | // handshake is started successfully. 39 | bool CryptoConnect(); 40 | 41 | // Returns the number of client hello messages that have been sent on the 42 | // crypto stream. If the handshake has completed then this is one greater 43 | // than the number of round-trips needed for the handshake. 44 | int GetNumSentClientHellos() const; 45 | 46 | protected: 47 | // QuicSession methods: 48 | virtual QuicDataStream* CreateIncomingDataStream(QuicStreamId id) OVERRIDE; 49 | 50 | private: 51 | QuicCryptoClientStream crypto_stream_; 52 | 53 | DISALLOW_COPY_AND_ASSIGN(QuicClientSession); 54 | }; 55 | 56 | } // namespace tools 57 | } // namespace net 58 | 59 | #endif // NET_TOOLS_QUIC_QUIC_CLIENT_SESSION_H_ 60 | -------------------------------------------------------------------------------- /src/tools/quic_default_packet_writer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_default_packet_writer.h" 6 | 7 | #include "net/tools/quic/quic_socket_utils.h" 8 | 9 | namespace net { 10 | namespace tools { 11 | 12 | QuicDefaultPacketWriter::QuicDefaultPacketWriter(int fd) 13 | : fd_(fd), 14 | write_blocked_(false) {} 15 | 16 | QuicDefaultPacketWriter::~QuicDefaultPacketWriter() {} 17 | 18 | WriteResult QuicDefaultPacketWriter::WritePacket( 19 | const char* buffer, size_t buf_len, 20 | const net::IPAddressNumber& self_address, 21 | const net::IPEndPoint& peer_address) { 22 | DCHECK(!IsWriteBlocked()); 23 | WriteResult result = QuicSocketUtils::WritePacket( 24 | fd_, buffer, buf_len, self_address, peer_address); 25 | if (result.status == WRITE_STATUS_BLOCKED) { 26 | write_blocked_ = true; 27 | } 28 | return result; 29 | } 30 | 31 | bool QuicDefaultPacketWriter::IsWriteBlockedDataBuffered() const { 32 | return false; 33 | } 34 | 35 | bool QuicDefaultPacketWriter::IsWriteBlocked() const { 36 | return write_blocked_; 37 | } 38 | 39 | void QuicDefaultPacketWriter::SetWritable() { 40 | write_blocked_ = false; 41 | } 42 | 43 | } // namespace tools 44 | } // namespace net 45 | -------------------------------------------------------------------------------- /src/tools/quic_default_packet_writer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_TOOLS_QUIC_QUIC_DEFAULT_PACKET_WRITER_H_ 6 | #define NET_TOOLS_QUIC_QUIC_DEFAULT_PACKET_WRITER_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "net/base/ip_endpoint.h" 10 | #include "net/quic/quic_packet_writer.h" 11 | 12 | namespace net { 13 | 14 | struct WriteResult; 15 | 16 | namespace tools { 17 | 18 | // Default packet writer which wraps QuicSocketUtils WritePacket. 19 | class QuicDefaultPacketWriter : public QuicPacketWriter { 20 | public: 21 | explicit QuicDefaultPacketWriter(int fd); 22 | virtual ~QuicDefaultPacketWriter(); 23 | 24 | // QuicPacketWriter 25 | virtual WriteResult WritePacket( 26 | const char* buffer, size_t buf_len, 27 | const net::IPAddressNumber& self_address, 28 | const net::IPEndPoint& peer_address) OVERRIDE; 29 | virtual bool IsWriteBlockedDataBuffered() const OVERRIDE; 30 | virtual bool IsWriteBlocked() const OVERRIDE; 31 | virtual void SetWritable() OVERRIDE; 32 | 33 | private: 34 | int fd_; 35 | bool write_blocked_; 36 | }; 37 | 38 | } // namespace tools 39 | } // namespace net 40 | 41 | #endif // NET_TOOLS_QUIC_QUIC_DEFAULT_PACKET_WRITER_H_ 42 | -------------------------------------------------------------------------------- /src/tools/quic_epoll_clock.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_epoll_clock.h" 6 | 7 | #include "net/tools/epoll_server/epoll_server.h" 8 | 9 | namespace net { 10 | namespace tools { 11 | 12 | QuicEpollClock::QuicEpollClock(EpollServer* epoll_server) 13 | : epoll_server_(epoll_server) { 14 | } 15 | 16 | QuicEpollClock::~QuicEpollClock() {} 17 | 18 | QuicTime QuicEpollClock::ApproximateNow() const { 19 | return QuicTime::Zero().Add( 20 | QuicTime::Delta::FromMicroseconds(epoll_server_->ApproximateNowInUsec())); 21 | } 22 | 23 | QuicTime QuicEpollClock::Now() const { 24 | return QuicTime::Zero().Add( 25 | QuicTime::Delta::FromMicroseconds(epoll_server_->NowInUsec())); 26 | } 27 | 28 | } // namespace tools 29 | } // namespace net 30 | -------------------------------------------------------------------------------- /src/tools/quic_epoll_clock.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_TOOLS_QUIC_QUIC_EPOLL_CLOCK_H_ 6 | #define NET_TOOLS_QUIC_QUIC_EPOLL_CLOCK_H_ 7 | 8 | #include "base/basictypes.h" 9 | #include "base/compiler_specific.h" 10 | #include "net/quic/quic_clock.h" 11 | #include "net/quic/quic_time.h" 12 | 13 | namespace net { 14 | 15 | class EpollServer; 16 | 17 | namespace tools { 18 | 19 | // Clock to efficiently retrieve an approximately accurate time from an 20 | // EpollServer. 21 | class QuicEpollClock : public QuicClock { 22 | public: 23 | explicit QuicEpollClock(EpollServer* epoll_server); 24 | virtual ~QuicEpollClock(); 25 | 26 | // Returns the approximate current time as a QuicTime object. 27 | virtual QuicTime ApproximateNow() const OVERRIDE; 28 | 29 | // Returns the current time as a QuicTime object. 30 | // Note: this use significant resources please use only if needed. 31 | virtual QuicTime Now() const OVERRIDE; 32 | 33 | protected: 34 | EpollServer* epoll_server_; 35 | }; 36 | 37 | } // namespace tools 38 | } // namespace net 39 | 40 | #endif // NET_TOOLS_QUIC_QUIC_EPOLL_CLOCK_H_ 41 | -------------------------------------------------------------------------------- /src/tools/quic_epoll_clock_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_epoll_clock.h" 6 | 7 | #include "net/tools/quic/test_tools/mock_epoll_server.h" 8 | #include "testing/gmock/include/gmock/gmock.h" 9 | 10 | namespace net { 11 | namespace tools { 12 | namespace test { 13 | 14 | TEST(QuicEpollClockTest, ApproximateNowInUsec) { 15 | MockEpollServer epoll_server; 16 | QuicEpollClock clock(&epoll_server); 17 | 18 | epoll_server.set_now_in_usec(1000000); 19 | EXPECT_EQ(1000000, 20 | clock.ApproximateNow().Subtract(QuicTime::Zero()).ToMicroseconds()); 21 | 22 | epoll_server.AdvanceBy(5); 23 | EXPECT_EQ(1000005, 24 | clock.ApproximateNow().Subtract(QuicTime::Zero()).ToMicroseconds()); 25 | } 26 | 27 | TEST(QuicEpollClockTest, NowInUsec) { 28 | MockEpollServer epoll_server; 29 | QuicEpollClock clock(&epoll_server); 30 | 31 | epoll_server.set_now_in_usec(1000000); 32 | EXPECT_EQ(1000000, 33 | clock.Now().Subtract(QuicTime::Zero()).ToMicroseconds()); 34 | 35 | epoll_server.AdvanceBy(5); 36 | EXPECT_EQ(1000005, 37 | clock.Now().Subtract(QuicTime::Zero()).ToMicroseconds()); 38 | } 39 | 40 | TEST(QuicEpollClockTest, WallNow) { 41 | MockEpollServer epoll_server; 42 | QuicEpollClock clock(&epoll_server); 43 | 44 | base::Time start = base::Time::Now(); 45 | QuicWallTime now = clock.WallNow(); 46 | base::Time end = base::Time::Now(); 47 | 48 | // If end > start, then we can check now is between start and end. 49 | if (end > start) { 50 | EXPECT_LE(static_cast(start.ToTimeT()), now.ToUNIXSeconds()); 51 | EXPECT_LE(now.ToUNIXSeconds(), static_cast(end.ToTimeT())); 52 | } 53 | } 54 | 55 | } // namespace test 56 | } // namespace tools 57 | } // namespace net 58 | -------------------------------------------------------------------------------- /src/tools/quic_epoll_connection_helper.h: -------------------------------------------------------------------------------- 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 | // The Google-specific helper for QuicConnection which uses 6 | // EpollAlarm for alarms, and used an int fd_ for writing data. 7 | 8 | #ifndef NET_TOOLS_QUIC_QUIC_EPOLL_CONNECTION_HELPER_H_ 9 | #define NET_TOOLS_QUIC_QUIC_EPOLL_CONNECTION_HELPER_H_ 10 | 11 | #include 12 | #include 13 | 14 | #include "net/quic/quic_connection.h" 15 | #include "net/quic/quic_packet_writer.h" 16 | #include "net/quic/quic_protocol.h" 17 | #include "net/quic/quic_time.h" 18 | #include "net/tools/quic/quic_default_packet_writer.h" 19 | #include "net/tools/quic/quic_epoll_clock.h" 20 | 21 | namespace net { 22 | 23 | class EpollServer; 24 | class QuicRandom; 25 | 26 | namespace tools { 27 | 28 | class AckAlarm; 29 | class RetransmissionAlarm; 30 | class SendAlarm; 31 | class TimeoutAlarm; 32 | 33 | class QuicEpollConnectionHelper : public QuicConnectionHelperInterface { 34 | public: 35 | explicit QuicEpollConnectionHelper(EpollServer* eps); 36 | virtual ~QuicEpollConnectionHelper(); 37 | 38 | // QuicEpollConnectionHelperInterface 39 | virtual const QuicClock* GetClock() const OVERRIDE; 40 | virtual QuicRandom* GetRandomGenerator() OVERRIDE; 41 | virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) OVERRIDE; 42 | 43 | EpollServer* epoll_server() { return epoll_server_; } 44 | 45 | private: 46 | friend class QuicConnectionPeer; 47 | 48 | EpollServer* epoll_server_; // Not owned. 49 | 50 | const QuicEpollClock clock_; 51 | QuicRandom* random_generator_; 52 | 53 | DISALLOW_COPY_AND_ASSIGN(QuicEpollConnectionHelper); 54 | }; 55 | 56 | } // namespace tools 57 | } // namespace net 58 | 59 | #endif // NET_TOOLS_QUIC_QUIC_EPOLL_CONNECTION_HELPER_H_ 60 | -------------------------------------------------------------------------------- /src/tools/quic_packet_writer_wrapper.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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_packet_writer_wrapper.h" 6 | 7 | #include "net/quic/quic_protocol.h" 8 | 9 | namespace net { 10 | namespace tools { 11 | 12 | QuicPacketWriterWrapper::QuicPacketWriterWrapper() {} 13 | 14 | QuicPacketWriterWrapper::QuicPacketWriterWrapper(QuicPacketWriter* writer) 15 | : writer_(writer) {} 16 | 17 | QuicPacketWriterWrapper::~QuicPacketWriterWrapper() {} 18 | 19 | WriteResult QuicPacketWriterWrapper::WritePacket( 20 | const char* buffer, 21 | size_t buf_len, 22 | const net::IPAddressNumber& self_address, 23 | const net::IPEndPoint& peer_address) { 24 | return writer_->WritePacket(buffer, buf_len, self_address, peer_address); 25 | } 26 | 27 | bool QuicPacketWriterWrapper::IsWriteBlockedDataBuffered() const { 28 | return writer_->IsWriteBlockedDataBuffered(); 29 | } 30 | 31 | bool QuicPacketWriterWrapper::IsWriteBlocked() const { 32 | return writer_->IsWriteBlocked(); 33 | } 34 | 35 | void QuicPacketWriterWrapper::SetWritable() { 36 | writer_->SetWritable(); 37 | } 38 | 39 | void QuicPacketWriterWrapper::set_writer(QuicPacketWriter* writer) { 40 | writer_.reset(writer); 41 | } 42 | 43 | QuicPacketWriter* QuicPacketWriterWrapper::release_writer() { 44 | return writer_.release(); 45 | } 46 | 47 | } // namespace tools 48 | } // namespace net 49 | -------------------------------------------------------------------------------- /src/tools/quic_packet_writer_wrapper.h: -------------------------------------------------------------------------------- 1 | // Copyright 2014 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 | #ifndef NET_TOOLS_QUIC_QUIC_PACKET_WRITER_WRAPPER_H_ 6 | #define NET_TOOLS_QUIC_QUIC_PACKET_WRITER_WRAPPER_H_ 7 | 8 | #include "base/memory/scoped_ptr.h" 9 | #include "net/quic/quic_packet_writer.h" 10 | 11 | namespace net { 12 | 13 | namespace tools { 14 | 15 | // Wraps a writer object to allow dynamically extending functionality. Use 16 | // cases: replace writer while dispatcher and connections hold on to the 17 | // wrapper; mix in monitoring in internal server; mix in mocks in unit tests. 18 | class QuicPacketWriterWrapper : public net::QuicPacketWriter { 19 | public: 20 | QuicPacketWriterWrapper(); 21 | explicit QuicPacketWriterWrapper(QuicPacketWriter* writer); 22 | virtual ~QuicPacketWriterWrapper(); 23 | 24 | // Default implementation of the QuicPacketWriter interface. Passes everything 25 | // to |writer_|. 26 | virtual WriteResult WritePacket( 27 | const char* buffer, 28 | size_t buf_len, 29 | const IPAddressNumber& self_address, 30 | const IPEndPoint& peer_address) OVERRIDE; 31 | virtual bool IsWriteBlockedDataBuffered() const OVERRIDE; 32 | virtual bool IsWriteBlocked() const OVERRIDE; 33 | virtual void SetWritable() OVERRIDE; 34 | 35 | // Takes ownership of |writer|. 36 | void set_writer(QuicPacketWriter* writer); 37 | 38 | // Releases ownership of |writer_|. 39 | QuicPacketWriter* release_writer(); 40 | 41 | private: 42 | scoped_ptr writer_; 43 | 44 | DISALLOW_COPY_AND_ASSIGN(QuicPacketWriterWrapper); 45 | }; 46 | 47 | } // namespace tools 48 | } // namespace net 49 | 50 | #endif // NET_TOOLS_QUIC_QUIC_PACKET_WRITER_WRAPPER_H_ 51 | -------------------------------------------------------------------------------- /src/tools/quic_server_bin.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 | // A binary wrapper for QuicServer. It listens forever on --port 6 | // (default 6121) until it's killed or ctrl-cd to death. 7 | 8 | #include 9 | 10 | #include "base/at_exit.h" 11 | #include "base/basictypes.h" 12 | #include "base/command_line.h" 13 | #include "base/logging.h" 14 | #include "base/strings/string_number_conversions.h" 15 | #include "net/base/ip_endpoint.h" 16 | #include "net/tools/quic/quic_in_memory_cache.h" 17 | #include "net/tools/quic/quic_server.h" 18 | 19 | // The port the quic server will listen on. 20 | 21 | int32 FLAGS_port = 6121; 22 | 23 | int main(int argc, char *argv[]) { 24 | CommandLine::Init(argc, argv); 25 | CommandLine* line = CommandLine::ForCurrentProcess(); 26 | 27 | logging::LoggingSettings settings; 28 | settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG; 29 | CHECK(logging::InitLogging(settings)); 30 | 31 | if (line->HasSwitch("h") || line->HasSwitch("help")) { 32 | const char* help_str = 33 | "Usage: quic_server [options]\n" 34 | "\n" 35 | "Options:\n" 36 | "-h, --help show this help message and exit\n" 37 | "--port= specify the port to listen on\n" 38 | "--quic_in_memory_cache_dir directory containing response data\n" 39 | " to load\n"; 40 | std::cout << help_str; 41 | exit(0); 42 | } 43 | 44 | if (line->HasSwitch("quic_in_memory_cache_dir")) { 45 | net::tools::FLAGS_quic_in_memory_cache_dir = 46 | line->GetSwitchValueASCII("quic_in_memory_cache_dir"); 47 | } 48 | 49 | if (line->HasSwitch("port")) { 50 | int port; 51 | if (base::StringToInt(line->GetSwitchValueASCII("port"), &port)) { 52 | FLAGS_port = port; 53 | } 54 | } 55 | 56 | base::AtExitManager exit_manager; 57 | 58 | net::IPAddressNumber ip; 59 | CHECK(net::ParseIPLiteralToNumber("::", &ip)); 60 | 61 | net::tools::QuicServer server; 62 | 63 | if (!server.Listen(net::IPEndPoint(ip, FLAGS_port))) { 64 | return 1; 65 | } 66 | 67 | while (1) { 68 | server.WaitForEvents(); 69 | } 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /src/tools/quic_server_test.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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_server.h" 6 | 7 | #include "net/quic/crypto/quic_random.h" 8 | #include "net/quic/quic_utils.h" 9 | #include "net/tools/quic/test_tools/mock_quic_dispatcher.h" 10 | #include "testing/gtest/include/gtest/gtest.h" 11 | 12 | using ::testing::_; 13 | 14 | namespace net { 15 | namespace tools { 16 | namespace test { 17 | 18 | namespace { 19 | 20 | class QuicServerDispatchPacketTest : public ::testing::Test { 21 | public: 22 | QuicServerDispatchPacketTest() 23 | : crypto_config_("blah", QuicRandom::GetInstance()), 24 | dispatcher_(config_, crypto_config_, &eps_) { 25 | dispatcher_.Initialize(1234); 26 | } 27 | 28 | void DispatchPacket(const QuicEncryptedPacket& packet) { 29 | IPEndPoint client_addr, server_addr; 30 | dispatcher_.ProcessPacket(server_addr, client_addr, packet); 31 | } 32 | 33 | protected: 34 | QuicConfig config_; 35 | QuicCryptoServerConfig crypto_config_; 36 | EpollServer eps_; 37 | MockQuicDispatcher dispatcher_; 38 | }; 39 | 40 | TEST_F(QuicServerDispatchPacketTest, DispatchPacket) { 41 | unsigned char valid_packet[] = { 42 | // public flags (8 byte guid) 43 | 0x3C, 44 | // guid 45 | 0x10, 0x32, 0x54, 0x76, 46 | 0x98, 0xBA, 0xDC, 0xFE, 47 | // packet sequence number 48 | 0xBC, 0x9A, 0x78, 0x56, 49 | 0x34, 0x12, 50 | // private flags 51 | 0x00 }; 52 | QuicEncryptedPacket encrypted_valid_packet(QuicUtils::AsChars(valid_packet), 53 | arraysize(valid_packet), false); 54 | 55 | EXPECT_CALL(dispatcher_, ProcessPacket(_, _, _)).Times(1); 56 | DispatchPacket(encrypted_valid_packet); 57 | } 58 | 59 | } // namespace 60 | } // namespace test 61 | } // namespace tools 62 | } // namespace net 63 | -------------------------------------------------------------------------------- /src/tools/quic_spdy_server_stream.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_TOOLS_QUIC_QUIC_SPDY_SERVER_STREAM_H_ 6 | #define NET_TOOLS_QUIC_QUIC_SPDY_SERVER_STREAM_H_ 7 | 8 | #include 9 | 10 | #include "base/basictypes.h" 11 | #include "net/base/io_buffer.h" 12 | #include "net/quic/quic_data_stream.h" 13 | #include "net/quic/quic_protocol.h" 14 | #include "net/tools/balsa/balsa_headers.h" 15 | 16 | namespace net { 17 | 18 | class QuicSession; 19 | 20 | namespace tools { 21 | 22 | namespace test { 23 | class QuicSpdyServerStreamPeer; 24 | } // namespace test 25 | 26 | // All this does right now is aggregate data, and on fin, send an HTTP 27 | // response. 28 | class QuicSpdyServerStream : public QuicDataStream { 29 | public: 30 | QuicSpdyServerStream(QuicStreamId id, QuicSession* session); 31 | virtual ~QuicSpdyServerStream(); 32 | 33 | // ReliableQuicStream implementation called by the session when there's 34 | // data for us. 35 | virtual uint32 ProcessData(const char* data, uint32 data_len) OVERRIDE; 36 | virtual void OnFinRead() OVERRIDE; 37 | 38 | int ParseRequestHeaders(); 39 | 40 | private: 41 | friend class test::QuicSpdyServerStreamPeer; 42 | 43 | // Sends a basic 200 response using SendHeaders for the headers and WriteData 44 | // for the body. 45 | void SendResponse(); 46 | 47 | // Sends a basic 500 response using SendHeaders for the headers and WriteData 48 | // for the body 49 | void SendErrorResponse(); 50 | 51 | void SendHeadersAndBody(const BalsaHeaders& response_headers, 52 | base::StringPiece body); 53 | 54 | BalsaHeaders headers_; 55 | string body_; 56 | 57 | // Buffer into which response header data is read. 58 | scoped_refptr read_buf_; 59 | bool request_headers_received_; 60 | }; 61 | 62 | } // namespace tools 63 | } // namespace net 64 | 65 | #endif // NET_TOOLS_QUIC_QUIC_SPDY_SERVER_STREAM_H_ 66 | -------------------------------------------------------------------------------- /src/tools/spdy_utils.h: -------------------------------------------------------------------------------- 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 | #ifndef NET_TOOLS_QUIC_SPDY_UTILS_H_ 6 | #define NET_TOOLS_QUIC_SPDY_UTILS_H_ 7 | 8 | #include 9 | 10 | #include "net/spdy/spdy_framer.h" 11 | #include "net/spdy/spdy_header_block.h" 12 | #include "net/spdy/spdy_protocol.h" 13 | #include "net/tools/balsa/balsa_headers.h" 14 | 15 | namespace net { 16 | namespace tools { 17 | 18 | class SpdyUtils { 19 | public: 20 | static std::string SerializeRequestHeaders( 21 | const BalsaHeaders& request_headers); 22 | 23 | static std::string SerializeResponseHeaders( 24 | const BalsaHeaders& response_headers); 25 | 26 | static bool FillBalsaRequestHeaders(const SpdyHeaderBlock& header_block, 27 | BalsaHeaders* request_headers); 28 | 29 | static bool FillBalsaResponseHeaders(const SpdyHeaderBlock& header_block, 30 | BalsaHeaders* response_headers); 31 | 32 | static SpdyHeaderBlock RequestHeadersToSpdyHeaders( 33 | const BalsaHeaders& request_headers); 34 | 35 | static SpdyHeaderBlock ResponseHeadersToSpdyHeaders( 36 | const BalsaHeaders& response_headers); 37 | 38 | static std::string SerializeUncompressedHeaders( 39 | const SpdyHeaderBlock& headers); 40 | }; 41 | 42 | } // namespace tools 43 | } // namespace net 44 | 45 | #endif // NET_TOOLS_QUIC_SPDY_UTILS_H_ 46 | -------------------------------------------------------------------------------- /src/tools/test_tools/mock_epoll_server.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/test_tools/mock_epoll_server.h" 6 | 7 | namespace net { 8 | namespace tools { 9 | namespace test { 10 | 11 | FakeTimeEpollServer::FakeTimeEpollServer(): now_in_usec_(0) { 12 | } 13 | 14 | FakeTimeEpollServer::~FakeTimeEpollServer() { 15 | } 16 | 17 | int64 FakeTimeEpollServer::NowInUsec() const { 18 | return now_in_usec_; 19 | } 20 | 21 | MockEpollServer::MockEpollServer() : until_in_usec_(-1) { 22 | } 23 | 24 | MockEpollServer::~MockEpollServer() { 25 | } 26 | 27 | int MockEpollServer::epoll_wait_impl(int epfd, 28 | struct epoll_event* events, 29 | int max_events, 30 | int timeout_in_ms) { 31 | int num_events = 0; 32 | while (!event_queue_.empty() && 33 | num_events < max_events && 34 | event_queue_.begin()->first <= NowInUsec() && 35 | ((until_in_usec_ == -1) || 36 | (event_queue_.begin()->first < until_in_usec_)) 37 | ) { 38 | int64 event_time_in_usec = event_queue_.begin()->first; 39 | events[num_events] = event_queue_.begin()->second; 40 | if (event_time_in_usec > NowInUsec()) { 41 | set_now_in_usec(event_time_in_usec); 42 | } 43 | event_queue_.erase(event_queue_.begin()); 44 | ++num_events; 45 | } 46 | if (num_events == 0) { // then we'd have waited 'till the timeout. 47 | if (until_in_usec_ < 0) { // then we don't care what the final time is. 48 | if (timeout_in_ms > 0) { 49 | AdvanceBy(timeout_in_ms * 1000); 50 | } 51 | } else { // except we assume that we don't wait for the timeout 52 | // period if until_in_usec_ is a positive number. 53 | set_now_in_usec(until_in_usec_); 54 | // And reset until_in_usec_ to signal no waiting (as 55 | // the AdvanceByExactly* stuff is meant to be one-shot, 56 | // as are all similar EpollServer functions) 57 | until_in_usec_ = -1; 58 | } 59 | } 60 | if (until_in_usec_ >= 0) { 61 | CHECK(until_in_usec_ >= NowInUsec()); 62 | } 63 | return num_events; 64 | } 65 | 66 | } // namespace test 67 | } // namespace tools 68 | } // namespace net 69 | -------------------------------------------------------------------------------- /src/tools/test_tools/mock_quic_dispatcher.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/test_tools/mock_quic_dispatcher.h" 6 | 7 | namespace net { 8 | namespace tools { 9 | namespace test { 10 | 11 | MockQuicDispatcher::MockQuicDispatcher( 12 | const QuicConfig& config, 13 | const QuicCryptoServerConfig& crypto_config, 14 | EpollServer* eps) 15 | : QuicDispatcher(config, crypto_config, QuicSupportedVersions(), eps) {} 16 | 17 | MockQuicDispatcher::~MockQuicDispatcher() {} 18 | 19 | } // namespace test 20 | } // namespace tools 21 | } // namespace net 22 | -------------------------------------------------------------------------------- /src/tools/test_tools/mock_quic_dispatcher.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_TOOLS_QUIC_TEST_TOOLS_MOCK_QUIC_DISPATCHER_H_ 6 | #define NET_TOOLS_QUIC_TEST_TOOLS_MOCK_QUIC_DISPATCHER_H_ 7 | 8 | #include "net/base/ip_endpoint.h" 9 | #include "net/quic/crypto/quic_crypto_server_config.h" 10 | #include "net/quic/quic_config.h" 11 | #include "net/quic/quic_protocol.h" 12 | #include "net/tools/epoll_server/epoll_server.h" 13 | #include "net/tools/quic/quic_dispatcher.h" 14 | #include "testing/gmock/include/gmock/gmock.h" 15 | 16 | namespace net { 17 | namespace tools { 18 | namespace test { 19 | 20 | class MockQuicDispatcher : public QuicDispatcher { 21 | public: 22 | MockQuicDispatcher(const QuicConfig& config, 23 | const QuicCryptoServerConfig& crypto_config, 24 | EpollServer* eps); 25 | 26 | virtual ~MockQuicDispatcher(); 27 | 28 | MOCK_METHOD3(ProcessPacket, void(const IPEndPoint& server_address, 29 | const IPEndPoint& client_address, 30 | const QuicEncryptedPacket& packet)); 31 | }; 32 | 33 | } // namespace test 34 | } // namespace tools 35 | } // namespace net 36 | 37 | #endif // NET_TOOLS_QUIC_TEST_TOOLS_MOCK_QUIC_DISPATCHER_H_ 38 | -------------------------------------------------------------------------------- /src/tools/test_tools/quic_client_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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/test_tools/quic_client_peer.h" 6 | 7 | #include "net/tools/quic/quic_client.h" 8 | 9 | namespace net { 10 | namespace tools { 11 | namespace test { 12 | 13 | // static 14 | int QuicClientPeer::GetFd(QuicClient* client) { 15 | return client->fd_; 16 | } 17 | 18 | // static 19 | QuicCryptoClientConfig* QuicClientPeer::GetCryptoConfig(QuicClient* client) { 20 | return &client->crypto_config_; 21 | } 22 | 23 | } // namespace test 24 | } // namespace tools 25 | } // namespace net 26 | -------------------------------------------------------------------------------- /src/tools/test_tools/quic_client_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 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 | #ifndef NET_TOOLS_QUIC_TEST_TOOLS_QUIC_CLIENT_PEER_H_ 6 | #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_CLIENT_PEER_H_ 7 | 8 | namespace net { 9 | 10 | class QuicCryptoClientConfig; 11 | 12 | namespace tools { 13 | 14 | class QuicClient; 15 | 16 | namespace test { 17 | 18 | class QuicClientPeer { 19 | public: 20 | static int GetFd(QuicClient* client); 21 | static QuicCryptoClientConfig* GetCryptoConfig(QuicClient* client); 22 | }; 23 | 24 | } // namespace test 25 | } // namespace tools 26 | } // namespace net 27 | 28 | #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_CLIENT_PEER_H_ 29 | -------------------------------------------------------------------------------- /src/tools/test_tools/quic_dispatcher_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/test_tools/quic_dispatcher_peer.h" 6 | 7 | #include "net/tools/quic/quic_dispatcher.h" 8 | #include "net/tools/quic/quic_packet_writer_wrapper.h" 9 | 10 | namespace net { 11 | namespace tools { 12 | namespace test { 13 | 14 | // static 15 | void QuicDispatcherPeer::SetTimeWaitListManager( 16 | QuicDispatcher* dispatcher, 17 | QuicTimeWaitListManager* time_wait_list_manager) { 18 | dispatcher->time_wait_list_manager_.reset(time_wait_list_manager); 19 | } 20 | 21 | // static 22 | void QuicDispatcherPeer::UseWriter(QuicDispatcher* dispatcher, 23 | QuicPacketWriterWrapper* writer) { 24 | writer->set_writer(dispatcher->writer_->release_writer()); 25 | dispatcher->writer_->set_writer(writer); 26 | } 27 | 28 | // static 29 | QuicPacketWriterWrapper* QuicDispatcherPeer::GetWriter( 30 | QuicDispatcher* dispatcher) { 31 | return dispatcher->writer_.get(); 32 | } 33 | 34 | // static 35 | QuicEpollConnectionHelper* QuicDispatcherPeer::GetHelper( 36 | QuicDispatcher* dispatcher) { 37 | return dispatcher->helper_.get(); 38 | } 39 | 40 | } // namespace test 41 | } // namespace tools 42 | } // namespace net 43 | -------------------------------------------------------------------------------- /src/tools/test_tools/quic_dispatcher_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_TOOLS_QUIC_TEST_TOOLS_QUIC_DISPATCHER_PEER_H_ 6 | #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_DISPATCHER_PEER_H_ 7 | 8 | #include "net/tools/quic/quic_dispatcher.h" 9 | 10 | namespace net { 11 | namespace tools { 12 | 13 | class QuicPacketWriterWrapper; 14 | 15 | namespace test { 16 | 17 | class QuicDispatcherPeer { 18 | public: 19 | static void SetTimeWaitListManager( 20 | QuicDispatcher* dispatcher, 21 | QuicTimeWaitListManager* time_wait_list_manager); 22 | 23 | // Injects |writer| into |dispatcher| beneath the top-level wrapper (to avoid 24 | // messing up existing connections). 25 | static void UseWriter(QuicDispatcher* dispatcher, 26 | QuicPacketWriterWrapper* writer); 27 | 28 | static QuicPacketWriterWrapper* GetWriter(QuicDispatcher* dispatcher); 29 | 30 | static QuicEpollConnectionHelper* GetHelper(QuicDispatcher* dispatcher); 31 | }; 32 | 33 | } // namespace test 34 | } // namespace tools 35 | } // namespace net 36 | 37 | #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_DISPATCHER_PEER_H_ 38 | -------------------------------------------------------------------------------- /src/tools/test_tools/quic_in_memory_cache_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/test_tools/quic_in_memory_cache_peer.h" 6 | 7 | #include "net/tools/quic/quic_in_memory_cache.h" 8 | 9 | namespace net { 10 | namespace tools { 11 | namespace test { 12 | 13 | // static 14 | void QuicInMemoryCachePeer::ResetForTests() { 15 | QuicInMemoryCache::GetInstance()->ResetForTests(); 16 | } 17 | 18 | } // namespace test 19 | } // namespace tools 20 | } // namespace net 21 | -------------------------------------------------------------------------------- /src/tools/test_tools/quic_in_memory_cache_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_TOOLS_QUIC_TEST_TOOLS_QUIC_IN_MEMORY_CACHE_PEER_H_ 6 | #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_IN_MEMORY_CACHE_PEER_H_ 7 | 8 | #include "net/tools/quic/quic_in_memory_cache.h" 9 | 10 | namespace net { 11 | namespace tools { 12 | namespace test { 13 | 14 | class QuicInMemoryCachePeer { 15 | public: 16 | // Resets the singleton QuicInMemoryCache to a fresh state. 17 | static void ResetForTests(); 18 | 19 | private: 20 | DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCachePeer); 21 | }; 22 | 23 | } // namespace test 24 | } // namespace tools 25 | } // namespace net 26 | 27 | #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_IN_MEMORY_CACHE_PEER_H_ 28 | -------------------------------------------------------------------------------- /src/tools/test_tools/quic_server_peer.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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/test_tools/quic_server_peer.h" 6 | 7 | #include "net/tools/quic/quic_dispatcher.h" 8 | #include "net/tools/quic/quic_server.h" 9 | 10 | namespace net { 11 | namespace tools { 12 | namespace test { 13 | 14 | // static 15 | bool QuicServerPeer::SetSmallSocket(QuicServer* server) { 16 | int size = 1024 * 10; 17 | return setsockopt( 18 | server->fd_, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) != -1; 19 | } 20 | 21 | // static 22 | void QuicServerPeer::DisableRecvmmsg(QuicServer* server) { 23 | server->use_recvmmsg_ = false; 24 | } 25 | 26 | // static 27 | QuicDispatcher* QuicServerPeer::GetDispatcher(QuicServer* server) { 28 | return server->dispatcher_.get(); 29 | } 30 | 31 | // static 32 | int QuicServerPeer::GetFD(QuicServer* server) { 33 | return server->fd_; 34 | } 35 | 36 | } // namespace test 37 | } // namespace tools 38 | } // namespace net 39 | -------------------------------------------------------------------------------- /src/tools/test_tools/quic_server_peer.h: -------------------------------------------------------------------------------- 1 | // Copyright 2013 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 | #ifndef NET_TOOLS_QUIC_TEST_TOOLS_QUIC_SERVER_PEER_H_ 6 | #define NET_TOOLS_QUIC_TEST_TOOLS_QUIC_SERVER_PEER_H_ 7 | 8 | namespace net { 9 | 10 | namespace tools { 11 | 12 | class QuicDispatcher; 13 | class QuicServer; 14 | 15 | namespace test { 16 | 17 | class QuicServerPeer { 18 | public: 19 | static bool SetSmallSocket(QuicServer* server); 20 | static void DisableRecvmmsg(QuicServer* server); 21 | static QuicDispatcher* GetDispatcher(QuicServer* server); 22 | static int GetFD(QuicServer* server); 23 | }; 24 | 25 | } // namespace test 26 | } // namespace tools 27 | } // namespace net 28 | 29 | #endif // NET_TOOLS_QUIC_TEST_TOOLS_QUIC_SERVER_PEER_H_ 30 | -------------------------------------------------------------------------------- /src/tools/test_tools/run_all_unittests.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 "base/test/test_suite.h" 6 | 7 | int main(int argc, char** argv) { 8 | base::TestSuite test_suite(argc, argv); 9 | 10 | return test_suite.Run(); 11 | } 12 | --------------------------------------------------------------------------------