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 8 #include <stdarg.h> 9 10 #include <utility> 11 #include <vector> 12 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" 19 20 namespace net { 21 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; 34 35 namespace test { 36 37 class PacketSavingConnection; 38 39 class CryptoTestUtils { 40 public: 41 // FakeClientOptions bundles together a number of options for configuring 42 // HandshakeWithFakeClient. 43 struct FakeClientOptions { 44 FakeClientOptions(); 45 46 // If dont_verify_certs is true then no ProofVerifier is set on the client. 47 // Thus no certificates will be requested or checked. 48 bool dont_verify_certs; 49 50 // If channel_id_enabled is true then the client will attempt to send a 51 // ChannelID. 52 bool channel_id_enabled; 53 }; 54 55 // returns: the number of client hellos that the client sent. 56 static int HandshakeWithFakeServer(PacketSavingConnection* client_conn, 57 QuicCryptoClientStream* client); 58 59 // returns: the number of client hellos that the client sent. 60 static int HandshakeWithFakeClient(PacketSavingConnection* server_conn, 61 QuicCryptoServerStream* server, 62 const FakeClientOptions& options); 63 64 // SetupCryptoServerConfigForTest configures |config| and |crypto_config| 65 // with sensible defaults for testing. 66 static void SetupCryptoServerConfigForTest( 67 const QuicClock* clock, 68 QuicRandom* rand, 69 QuicConfig* config, 70 QuicCryptoServerConfig* crypto_config); 71 72 // CommunicateHandshakeMessages moves messages from |a| to |b| and back until 73 // |a|'s handshake has completed. 74 static void CommunicateHandshakeMessages(PacketSavingConnection* a_conn, 75 QuicCryptoStream* a, 76 PacketSavingConnection* b_conn, 77 QuicCryptoStream* b); 78 79 // AdvanceHandshake attempts to moves messages from |a| to |b| and |b| to |a|. 80 // Returns the number of messages moved. 81 static std::pair<size_t, size_t> AdvanceHandshake( 82 PacketSavingConnection* a_conn, 83 QuicCryptoStream* a, 84 size_t a_i, 85 PacketSavingConnection* b_conn, 86 QuicCryptoStream* b, 87 size_t b_i); 88 89 // Returns the value for the tag |tag| in the tag value map of |message|. 90 static std::string GetValueForTag(const CryptoHandshakeMessage& message, 91 QuicTag tag); 92 93 // Returns a |ProofSource| that serves up test certificates. 94 static ProofSource* ProofSourceForTesting(); 95 96 // Returns a |ProofVerifier| that uses the QUIC testing root CA. 97 static ProofVerifier* ProofVerifierForTesting(); 98 99 // Returns a |ProofVerifyContext| that must be used with the verifier 100 // returned by |ProofVerifierForTesting|. 101 static ProofVerifyContext* ProofVerifyContextForTesting(); 102 103 // MockCommonCertSets returns a CommonCertSets that contains a single set with 104 // hash |hash|, consisting of the certificate |cert| at index |index|. 105 static CommonCertSets* MockCommonCertSets(base::StringPiece cert, 106 uint64 hash, 107 uint32 index); 108 109 // ParseTag returns a QuicTag from parsing |tagstr|. |tagstr| may either be 110 // in the format "EXMP" (i.e. ASCII format), or "#11223344" (an explicit hex 111 // format). It CHECK fails if there's a parse error. 112 static QuicTag ParseTag(const char* tagstr); 113 114 // Message constructs a handshake message from a variable number of 115 // arguments. |message_tag| is passed to |ParseTag| and used as the tag of 116 // the resulting message. The arguments are taken in pairs and NULL 117 // terminated. The first of each pair is the tag of a tag/value and is given 118 // as an argument to |ParseTag|. The second is the value of the tag/value 119 // pair and is either a hex dump, preceeded by a '#', or a raw value. 120 // 121 // Message( 122 // "CHLO", 123 // "NOCE", "#11223344", 124 // "SNI", "www.example.com", 125 // NULL); 126 static CryptoHandshakeMessage Message(const char* message_tag, ...); 127 128 // BuildMessage is the same as |Message|, but takes the variable arguments 129 // explicitly. TODO(rtenneti): Investigate whether it'd be better for 130 // Message() and BuildMessage() to return a CryptoHandshakeMessage* pointer 131 // instead, to avoid copying the return value. 132 static CryptoHandshakeMessage BuildMessage(const char* message_tag, 133 va_list ap); 134 135 // ChannelIDSourceForTesting returns a ChannelIDSource that generates keys 136 // deterministically based on the hostname given in the GetChannelIDKey call. 137 // This ChannelIDSource works in synchronous mode, i.e., its GetChannelIDKey 138 // method never returns QUIC_PENDING. 139 static ChannelIDSource* ChannelIDSourceForTesting(); 140 141 private: 142 static void CompareClientAndServerKeys(QuicCryptoClientStream* client, 143 QuicCryptoServerStream* server); 144 145 DISALLOW_COPY_AND_ASSIGN(CryptoTestUtils); 146 }; 147 148 } // namespace test 149 150 } // namespace net 151 152 #endif // NET_QUIC_TEST_TOOLS_CRYPTO_TEST_UTILS_H_ 153