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