[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 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_CRYPTO_TEST_UTILS_H_ |
| 6 | #define NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ |
| 7 | |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 8 | #include <stdarg.h> |
| 9 | |
[email protected] | 691f45a98 | 2013-11-19 10:52:04 | [diff] [blame] | 10 | #include <utility> |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | 9f0dcd4e | 2014-01-16 15:58:14 | [diff] [blame] | 13 | #include "base/basictypes.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 15 | #include "base/strings/string_piece.h" |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 16 | #include "net/quic/crypto/crypto_framer.h" |
| 17 | #include "net/quic/quic_framer.h" |
| 18 | #include "net/quic/quic_protocol.h" |
| 19 | |
| 20 | namespace net { |
| 21 | |
[email protected] | 03dd3253 | 2014-05-30 07:11:25 | [diff] [blame] | 22 | class ChannelIDSource; |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 23 | class CommonCertSets; |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 24 | class ProofSource; |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 25 | class ProofVerifier; |
[email protected] | c817c67 | 2014-03-21 22:25:34 | [diff] [blame] | 26 | class ProofVerifyContext; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 27 | class QuicClock; |
| 28 | class QuicConfig; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 29 | class QuicCryptoClientStream; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 30 | class QuicCryptoServerConfig; |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 31 | class QuicCryptoServerStream; |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 32 | class QuicCryptoStream; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 33 | class QuicRandom; |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 34 | |
| 35 | namespace test { |
| 36 | |
| 37 | class PacketSavingConnection; |
| 38 | |
| 39 | class CryptoTestUtils { |
| 40 | public: |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 41 | // An interface for a source of callbacks. This is used for invoking |
| 42 | // callbacks asynchronously. |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 43 | // |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 44 | // Call the RunPendingCallbacks method regularly to run the callbacks from |
| 45 | // this source. |
| 46 | class CallbackSource { |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 47 | public: |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 48 | virtual ~CallbackSource() {} |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 49 | |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 50 | // Runs pending callbacks from this source. If there is no pending |
| 51 | // callback, does nothing. |
| 52 | virtual void RunPendingCallbacks() = 0; |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 53 | }; |
| 54 | |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 55 | // FakeClientOptions bundles together a number of options for configuring |
| 56 | // HandshakeWithFakeClient. |
| 57 | struct FakeClientOptions { |
| 58 | FakeClientOptions(); |
| 59 | |
| 60 | // If dont_verify_certs is true then no ProofVerifier is set on the client. |
| 61 | // Thus no certificates will be requested or checked. |
| 62 | bool dont_verify_certs; |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 63 | |
| 64 | // If channel_id_enabled is true then the client will attempt to send a |
[email protected] | 03dd3253 | 2014-05-30 07:11:25 | [diff] [blame] | 65 | // ChannelID. |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 66 | bool channel_id_enabled; |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 67 | |
| 68 | // If channel_id_source_async is true then the client will use an async |
| 69 | // ChannelIDSource for testing. Ignored if channel_id_enabled is false. |
| 70 | bool channel_id_source_async; |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 71 | }; |
| 72 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 73 | // returns: the number of client hellos that the client sent. |
| 74 | static int HandshakeWithFakeServer(PacketSavingConnection* client_conn, |
| 75 | QuicCryptoClientStream* client); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 76 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 77 | // returns: the number of client hellos that the client sent. |
| 78 | static int HandshakeWithFakeClient(PacketSavingConnection* server_conn, |
[email protected] | 89995165 | 2013-05-16 12:52:39 | [diff] [blame] | 79 | QuicCryptoServerStream* server, |
| 80 | const FakeClientOptions& options); |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 81 | |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 82 | // SetupCryptoServerConfigForTest configures |config| and |crypto_config| |
| 83 | // with sensible defaults for testing. |
| 84 | static void SetupCryptoServerConfigForTest( |
| 85 | const QuicClock* clock, |
| 86 | QuicRandom* rand, |
| 87 | QuicConfig* config, |
| 88 | QuicCryptoServerConfig* crypto_config); |
| 89 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 90 | // CommunicateHandshakeMessages moves messages from |a| to |b| and back until |
| 91 | // |a|'s handshake has completed. |
| 92 | static void CommunicateHandshakeMessages(PacketSavingConnection* a_conn, |
| 93 | QuicCryptoStream* a, |
| 94 | PacketSavingConnection* b_conn, |
| 95 | QuicCryptoStream* b); |
| 96 | |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 97 | // CommunicateHandshakeMessagesAndRunCallbacks moves messages from |a| to |b| |
| 98 | // and back until |a|'s handshake has completed. If |callback_source| is not |
| 99 | // NULL, CommunicateHandshakeMessagesAndRunCallbacks also runs callbacks from |
| 100 | // |callback_source| between processing messages. |
| 101 | static void CommunicateHandshakeMessagesAndRunCallbacks( |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 102 | PacketSavingConnection* a_conn, |
| 103 | QuicCryptoStream* a, |
| 104 | PacketSavingConnection* b_conn, |
| 105 | QuicCryptoStream* b, |
[email protected] | dc6094a | 2014-07-23 01:50:04 | [diff] [blame] | 106 | CallbackSource* callback_source); |
[email protected] | 6fc79ea | 2014-07-10 04:30:23 | [diff] [blame] | 107 | |
[email protected] | 691f45a98 | 2013-11-19 10:52:04 | [diff] [blame] | 108 | // AdvanceHandshake attempts to moves messages from |a| to |b| and |b| to |a|. |
| 109 | // Returns the number of messages moved. |
| 110 | static std::pair<size_t, size_t> AdvanceHandshake( |
| 111 | PacketSavingConnection* a_conn, |
| 112 | QuicCryptoStream* a, |
| 113 | size_t a_i, |
| 114 | PacketSavingConnection* b_conn, |
| 115 | QuicCryptoStream* b, |
| 116 | size_t b_i); |
| 117 | |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 118 | // Returns the value for the tag |tag| in the tag value map of |message|. |
| 119 | static std::string GetValueForTag(const CryptoHandshakeMessage& message, |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 120 | QuicTag tag); |
[email protected] | ccc66e8a | 2013-03-26 08:26:14 | [diff] [blame] | 121 | |
[email protected] | fe053f9 | 2013-04-23 20:18:55 | [diff] [blame] | 122 | // Returns a |ProofSource| that serves up test certificates. |
| 123 | static ProofSource* ProofSourceForTesting(); |
| 124 | |
[email protected] | a57e027 | 2013-04-26 07:31:47 | [diff] [blame] | 125 | // Returns a |ProofVerifier| that uses the QUIC testing root CA. |
| 126 | static ProofVerifier* ProofVerifierForTesting(); |
| 127 | |
[email protected] | c817c67 | 2014-03-21 22:25:34 | [diff] [blame] | 128 | // Returns a |ProofVerifyContext| that must be used with the verifier |
[email protected] | 59c0bbd | 2014-03-22 04:08:12 | [diff] [blame] | 129 | // returned by |ProofVerifierForTesting|. |
[email protected] | c817c67 | 2014-03-21 22:25:34 | [diff] [blame] | 130 | static ProofVerifyContext* ProofVerifyContextForTesting(); |
| 131 | |
[email protected] | 0cceb92 | 2014-07-01 02:00:56 | [diff] [blame] | 132 | // These functions return a fake |ProofSource|, |ProofVerifier|, or |
| 133 | // |ProofVerifyContext| that works with each other. These are suitable for |
| 134 | // unit tests that aren't concerned with |ProofSource| and |ProofVerifier|. |
| 135 | // TODO(wtc): delete these when Chromium has a working |
| 136 | // ProofSourceForTesting(). |
| 137 | static ProofSource* FakeProofSourceForTesting(); |
| 138 | static ProofVerifier* FakeProofVerifierForTesting(); |
| 139 | static ProofVerifyContext* FakeProofVerifyContextForTesting(); |
| 140 | |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 141 | // MockCommonCertSets returns a CommonCertSets that contains a single set with |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 142 | // hash |hash|, consisting of the certificate |cert| at index |index|. |
[email protected] | 2532de1 | 2013-05-09 12:29:33 | [diff] [blame] | 143 | static CommonCertSets* MockCommonCertSets(base::StringPiece cert, |
| 144 | uint64 hash, |
| 145 | uint32 index); |
[email protected] | c244c5a1 | 2013-05-07 20:55:04 | [diff] [blame] | 146 | |
[email protected] | 0bbeb697 | 2013-05-23 04:10:21 | [diff] [blame] | 147 | // ParseTag returns a QuicTag from parsing |tagstr|. |tagstr| may either be |
| 148 | // in the format "EXMP" (i.e. ASCII format), or "#11223344" (an explicit hex |
| 149 | // format). It CHECK fails if there's a parse error. |
| 150 | static QuicTag ParseTag(const char* tagstr); |
| 151 | |
| 152 | // Message constructs a handshake message from a variable number of |
| 153 | // arguments. |message_tag| is passed to |ParseTag| and used as the tag of |
| 154 | // the resulting message. The arguments are taken in pairs and NULL |
| 155 | // terminated. The first of each pair is the tag of a tag/value and is given |
| 156 | // as an argument to |ParseTag|. The second is the value of the tag/value |
| 157 | // pair and is either a hex dump, preceeded by a '#', or a raw value. |
| 158 | // |
| 159 | // Message( |
| 160 | // "CHLO", |
| 161 | // "NOCE", "#11223344", |
| 162 | // "SNI", "www.example.com", |
| 163 | // NULL); |
| 164 | static CryptoHandshakeMessage Message(const char* message_tag, ...); |
| 165 | |
| 166 | // BuildMessage is the same as |Message|, but takes the variable arguments |
| 167 | // explicitly. TODO(rtenneti): Investigate whether it'd be better for |
| 168 | // Message() and BuildMessage() to return a CryptoHandshakeMessage* pointer |
| 169 | // instead, to avoid copying the return value. |
| 170 | static CryptoHandshakeMessage BuildMessage(const char* message_tag, |
| 171 | va_list ap); |
| 172 | |
[email protected] | 03dd3253 | 2014-05-30 07:11:25 | [diff] [blame] | 173 | // ChannelIDSourceForTesting returns a ChannelIDSource that generates keys |
[email protected] | 05bfc260f | 2014-06-07 06:31:25 | [diff] [blame] | 174 | // deterministically based on the hostname given in the GetChannelIDKey call. |
| 175 | // This ChannelIDSource works in synchronous mode, i.e., its GetChannelIDKey |
| 176 | // method never returns QUIC_PENDING. |
[email protected] | 03dd3253 | 2014-05-30 07:11:25 | [diff] [blame] | 177 | static ChannelIDSource* ChannelIDSourceForTesting(); |
[email protected] | b06431078 | 2013-05-30 21:12:17 | [diff] [blame] | 178 | |
[email protected] | 14e8106c | 2013-03-14 16:25:33 | [diff] [blame] | 179 | private: |
| 180 | static void CompareClientAndServerKeys(QuicCryptoClientStream* client, |
| 181 | QuicCryptoServerStream* server); |
[email protected] | b99c0fc | 2014-04-22 07:56:52 | [diff] [blame] | 182 | |
| 183 | DISALLOW_COPY_AND_ASSIGN(CryptoTestUtils); |
[email protected] | ed3fc15d | 2013-03-08 18:37:44 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | } // namespace test |
| 187 | |
| 188 | } // namespace net |
| 189 | |
| 190 | #endif // NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ |