Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / quic / test_tools / crypto_test_utils.h
blob643550e87ccfef8e8568ad7281648e09c459c485
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.
5 #ifndef NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_
6 #define NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_
8 #include <stdarg.h>
10 #include <utility>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/logging.h"
15 #include "base/strings/string_piece.h"
16 #include "net/quic/crypto/crypto_framer.h"
17 #include "net/quic/quic_framer.h"
18 #include "net/quic/quic_protocol.h"
20 namespace net {
22 class ChannelIDSource;
23 class CommonCertSets;
24 class ProofSource;
25 class ProofVerifier;
26 class ProofVerifyContext;
27 class QuicClock;
28 class QuicConfig;
29 class QuicCryptoClientStream;
30 class QuicCryptoServerConfig;
31 class QuicCryptoServerStream;
32 class QuicCryptoStream;
33 class QuicRandom;
35 namespace test {
37 class PacketSavingConnection;
39 class CryptoTestUtils {
40 public:
41 // An interface for a source of callbacks. This is used for invoking
42 // callbacks asynchronously.
44 // Call the RunPendingCallbacks method regularly to run the callbacks from
45 // this source.
46 class CallbackSource {
47 public:
48 virtual ~CallbackSource() {}
50 // Runs pending callbacks from this source. If there is no pending
51 // callback, does nothing.
52 virtual void RunPendingCallbacks() = 0;
55 // FakeClientOptions bundles together a number of options for configuring
56 // HandshakeWithFakeClient.
57 struct FakeClientOptions {
58 FakeClientOptions();
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;
64 // If channel_id_enabled is true then the client will attempt to send a
65 // ChannelID.
66 bool channel_id_enabled;
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;
73 // returns: the number of client hellos that the client sent.
74 static int HandshakeWithFakeServer(PacketSavingConnection* client_conn,
75 QuicCryptoClientStream* client);
77 // returns: the number of client hellos that the client sent.
78 static int HandshakeWithFakeClient(PacketSavingConnection* server_conn,
79 QuicCryptoServerStream* server,
80 const FakeClientOptions& options);
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);
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);
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(
102 PacketSavingConnection* a_conn,
103 QuicCryptoStream* a,
104 PacketSavingConnection* b_conn,
105 QuicCryptoStream* b,
106 CallbackSource* callback_source);
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);
118 // Returns the value for the tag |tag| in the tag value map of |message|.
119 static std::string GetValueForTag(const CryptoHandshakeMessage& message,
120 QuicTag tag);
122 // Returns a |ProofSource| that serves up test certificates.
123 static ProofSource* ProofSourceForTesting();
125 // Returns a |ProofVerifier| that uses the QUIC testing root CA.
126 static ProofVerifier* ProofVerifierForTesting();
128 // Returns a |ProofVerifyContext| that must be used with the verifier
129 // returned by |ProofVerifierForTesting|.
130 static ProofVerifyContext* ProofVerifyContextForTesting();
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();
141 // MockCommonCertSets returns a CommonCertSets that contains a single set with
142 // hash |hash|, consisting of the certificate |cert| at index |index|.
143 static CommonCertSets* MockCommonCertSets(base::StringPiece cert,
144 uint64 hash,
145 uint32 index);
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);
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.
159 // Message(
160 // "CHLO",
161 // "NOCE", "#11223344",
162 // "SNI", "www.example.com",
163 // NULL);
164 static CryptoHandshakeMessage Message(const char* message_tag, ...);
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);
173 // ChannelIDSourceForTesting returns a ChannelIDSource that generates keys
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.
177 static ChannelIDSource* ChannelIDSourceForTesting();
179 private:
180 static void CompareClientAndServerKeys(QuicCryptoClientStream* client,
181 QuicCryptoServerStream* server);
183 DISALLOW_COPY_AND_ASSIGN(CryptoTestUtils);
186 } // namespace test
188 } // namespace net
190 #endif // NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_