blob: 643550e87ccfef8e8568ad7281648e09c459c485 [file] [log] [blame]
[email protected]ed3fc15d2013-03-08 18:37:441// 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]0bbeb6972013-05-23 04:10:218#include <stdarg.h>
9
[email protected]691f45a982013-11-19 10:52:0410#include <utility>
[email protected]ed3fc15d2013-03-08 18:37:4411#include <vector>
12
[email protected]9f0dcd4e2014-01-16 15:58:1413#include "base/basictypes.h"
[email protected]ed3fc15d2013-03-08 18:37:4414#include "base/logging.h"
[email protected]c244c5a12013-05-07 20:55:0415#include "base/strings/string_piece.h"
[email protected]ed3fc15d2013-03-08 18:37:4416#include "net/quic/crypto/crypto_framer.h"
17#include "net/quic/quic_framer.h"
18#include "net/quic/quic_protocol.h"
19
20namespace net {
21
[email protected]03dd32532014-05-30 07:11:2522class ChannelIDSource;
[email protected]2532de12013-05-09 12:29:3323class CommonCertSets;
[email protected]fe053f92013-04-23 20:18:5524class ProofSource;
[email protected]a57e0272013-04-26 07:31:4725class ProofVerifier;
[email protected]c817c672014-03-21 22:25:3426class ProofVerifyContext;
[email protected]ef95114d2013-04-17 17:57:0127class QuicClock;
28class QuicConfig;
[email protected]14e8106c2013-03-14 16:25:3329class QuicCryptoClientStream;
[email protected]ef95114d2013-04-17 17:57:0130class QuicCryptoServerConfig;
[email protected]14e8106c2013-03-14 16:25:3331class QuicCryptoServerStream;
[email protected]fe053f92013-04-23 20:18:5532class QuicCryptoStream;
[email protected]ef95114d2013-04-17 17:57:0133class QuicRandom;
[email protected]ed3fc15d2013-03-08 18:37:4434
35namespace test {
36
37class PacketSavingConnection;
38
39class CryptoTestUtils {
40 public:
[email protected]dc6094a2014-07-23 01:50:0441 // An interface for a source of callbacks. This is used for invoking
42 // callbacks asynchronously.
[email protected]6fc79ea2014-07-10 04:30:2343 //
[email protected]dc6094a2014-07-23 01:50:0444 // Call the RunPendingCallbacks method regularly to run the callbacks from
45 // this source.
46 class CallbackSource {
[email protected]6fc79ea2014-07-10 04:30:2347 public:
[email protected]dc6094a2014-07-23 01:50:0448 virtual ~CallbackSource() {}
[email protected]6fc79ea2014-07-10 04:30:2349
[email protected]dc6094a2014-07-23 01:50:0450 // Runs pending callbacks from this source. If there is no pending
51 // callback, does nothing.
52 virtual void RunPendingCallbacks() = 0;
[email protected]6fc79ea2014-07-10 04:30:2353 };
54
[email protected]899951652013-05-16 12:52:3955 // 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]b064310782013-05-30 21:12:1763
64 // If channel_id_enabled is true then the client will attempt to send a
[email protected]03dd32532014-05-30 07:11:2565 // ChannelID.
[email protected]b064310782013-05-30 21:12:1766 bool channel_id_enabled;
[email protected]6fc79ea2014-07-10 04:30:2367
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]899951652013-05-16 12:52:3971 };
72
[email protected]fe053f92013-04-23 20:18:5573 // returns: the number of client hellos that the client sent.
74 static int HandshakeWithFakeServer(PacketSavingConnection* client_conn,
75 QuicCryptoClientStream* client);
[email protected]ed3fc15d2013-03-08 18:37:4476
[email protected]fe053f92013-04-23 20:18:5577 // returns: the number of client hellos that the client sent.
78 static int HandshakeWithFakeClient(PacketSavingConnection* server_conn,
[email protected]899951652013-05-16 12:52:3979 QuicCryptoServerStream* server,
80 const FakeClientOptions& options);
[email protected]14e8106c2013-03-14 16:25:3381
[email protected]ef95114d2013-04-17 17:57:0182 // 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]fe053f92013-04-23 20:18:5590 // 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]dc6094a2014-07-23 01:50:0497 // 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]6fc79ea2014-07-10 04:30:23102 PacketSavingConnection* a_conn,
103 QuicCryptoStream* a,
104 PacketSavingConnection* b_conn,
105 QuicCryptoStream* b,
[email protected]dc6094a2014-07-23 01:50:04106 CallbackSource* callback_source);
[email protected]6fc79ea2014-07-10 04:30:23107
[email protected]691f45a982013-11-19 10:52:04108 // 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]ccc66e8a2013-03-26 08:26:14118 // Returns the value for the tag |tag| in the tag value map of |message|.
119 static std::string GetValueForTag(const CryptoHandshakeMessage& message,
[email protected]2532de12013-05-09 12:29:33120 QuicTag tag);
[email protected]ccc66e8a2013-03-26 08:26:14121
[email protected]fe053f92013-04-23 20:18:55122 // Returns a |ProofSource| that serves up test certificates.
123 static ProofSource* ProofSourceForTesting();
124
[email protected]a57e0272013-04-26 07:31:47125 // Returns a |ProofVerifier| that uses the QUIC testing root CA.
126 static ProofVerifier* ProofVerifierForTesting();
127
[email protected]c817c672014-03-21 22:25:34128 // Returns a |ProofVerifyContext| that must be used with the verifier
[email protected]59c0bbd2014-03-22 04:08:12129 // returned by |ProofVerifierForTesting|.
[email protected]c817c672014-03-21 22:25:34130 static ProofVerifyContext* ProofVerifyContextForTesting();
131
[email protected]0cceb922014-07-01 02:00:56132 // 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]2532de12013-05-09 12:29:33141 // MockCommonCertSets returns a CommonCertSets that contains a single set with
[email protected]c244c5a12013-05-07 20:55:04142 // hash |hash|, consisting of the certificate |cert| at index |index|.
[email protected]2532de12013-05-09 12:29:33143 static CommonCertSets* MockCommonCertSets(base::StringPiece cert,
144 uint64 hash,
145 uint32 index);
[email protected]c244c5a12013-05-07 20:55:04146
[email protected]0bbeb6972013-05-23 04:10:21147 // 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]03dd32532014-05-30 07:11:25173 // ChannelIDSourceForTesting returns a ChannelIDSource that generates keys
[email protected]05bfc260f2014-06-07 06:31:25174 // 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]03dd32532014-05-30 07:11:25177 static ChannelIDSource* ChannelIDSourceForTesting();
[email protected]b064310782013-05-30 21:12:17178
[email protected]14e8106c2013-03-14 16:25:33179 private:
180 static void CompareClientAndServerKeys(QuicCryptoClientStream* client,
181 QuicCryptoServerStream* server);
[email protected]b99c0fc2014-04-22 07:56:52182
183 DISALLOW_COPY_AND_ASSIGN(CryptoTestUtils);
[email protected]ed3fc15d2013-03-08 18:37:44184};
185
186} // namespace test
187
188} // namespace net
189
190#endif // NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_