If there are no local credentials for a locked profile, show sign in button
[chromium-blink-merge.git] / net / quic / test_tools / crypto_test_utils.h
blobc72b904cefa96179cc50e5aaa3317413d3ae5893
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 // nullptr, CommunicateHandshakeMessagesAndRunCallbacks also runs callbacks
100 // from
101 // |callback_source| between processing messages.
102 static void CommunicateHandshakeMessagesAndRunCallbacks(
103 PacketSavingConnection* a_conn,
104 QuicCryptoStream* a,
105 PacketSavingConnection* b_conn,
106 QuicCryptoStream* b,
107 CallbackSource* callback_source);
109 // AdvanceHandshake attempts to moves messages from |a| to |b| and |b| to |a|.
110 // Returns the number of messages moved.
111 static std::pair<size_t, size_t> AdvanceHandshake(
112 PacketSavingConnection* a_conn,
113 QuicCryptoStream* a,
114 size_t a_i,
115 PacketSavingConnection* b_conn,
116 QuicCryptoStream* b,
117 size_t b_i);
119 // Returns the value for the tag |tag| in the tag value map of |message|.
120 static std::string GetValueForTag(const CryptoHandshakeMessage& message,
121 QuicTag tag);
123 // Returns a |ProofSource| that serves up test certificates.
124 static ProofSource* ProofSourceForTesting();
126 // Returns a |ProofVerifier| that uses the QUIC testing root CA.
127 static ProofVerifier* ProofVerifierForTesting();
129 // Returns a |ProofVerifyContext| that must be used with the verifier
130 // returned by |ProofVerifierForTesting|.
131 static ProofVerifyContext* ProofVerifyContextForTesting();
133 // These functions return a fake |ProofSource|, |ProofVerifier|, or
134 // |ProofVerifyContext| that works with each other. These are suitable for
135 // unit tests that aren't concerned with |ProofSource| and |ProofVerifier|.
136 // TODO(wtc): delete these when Chromium has a working
137 // ProofSourceForTesting().
138 static ProofSource* FakeProofSourceForTesting();
139 static ProofVerifier* FakeProofVerifierForTesting();
140 static ProofVerifyContext* FakeProofVerifyContextForTesting();
142 // MockCommonCertSets returns a CommonCertSets that contains a single set with
143 // hash |hash|, consisting of the certificate |cert| at index |index|.
144 static CommonCertSets* MockCommonCertSets(base::StringPiece cert,
145 uint64 hash,
146 uint32 index);
148 // ParseTag returns a QuicTag from parsing |tagstr|. |tagstr| may either be
149 // in the format "EXMP" (i.e. ASCII format), or "#11223344" (an explicit hex
150 // format). It CHECK fails if there's a parse error.
151 static QuicTag ParseTag(const char* tagstr);
153 // Message constructs a handshake message from a variable number of
154 // arguments. |message_tag| is passed to |ParseTag| and used as the tag of
155 // the resulting message. The arguments are taken in pairs and nullptr
156 // terminated. The first of each pair is the tag of a tag/value and is given
157 // as an argument to |ParseTag|. The second is the value of the tag/value
158 // pair and is either a hex dump, preceeded by a '#', or a raw value.
160 // Message(
161 // "CHLO",
162 // "NOCE", "#11223344",
163 // "SNI", "www.example.com",
164 // nullptr);
165 static CryptoHandshakeMessage Message(const char* message_tag, ...);
167 // BuildMessage is the same as |Message|, but takes the variable arguments
168 // explicitly. TODO(rtenneti): Investigate whether it'd be better for
169 // Message() and BuildMessage() to return a CryptoHandshakeMessage* pointer
170 // instead, to avoid copying the return value.
171 static CryptoHandshakeMessage BuildMessage(const char* message_tag,
172 va_list ap);
174 // ChannelIDSourceForTesting returns a ChannelIDSource that generates keys
175 // deterministically based on the hostname given in the GetChannelIDKey call.
176 // This ChannelIDSource works in synchronous mode, i.e., its GetChannelIDKey
177 // method never returns QUIC_PENDING.
178 static ChannelIDSource* ChannelIDSourceForTesting();
180 private:
181 static void CompareClientAndServerKeys(QuicCryptoClientStream* client,
182 QuicCryptoServerStream* server);
184 DISALLOW_COPY_AND_ASSIGN(CryptoTestUtils);
187 } // namespace test
189 } // namespace net
191 #endif // NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_