Prepare crypto stream tests for reordering versions

This CL paves the way for having TLS before QUIC_CRYPTO.

gfe-relnote: n/a, test-only
PiperOrigin-RevId: 298385523
Change-Id: I9e94e2d4662586b62c35cfd77ec68761eee50d61
diff --git a/quic/core/quic_crypto_client_stream_test.cc b/quic/core/quic_crypto_client_stream_test.cc
index 0cfdbe9..bcd6a1f 100644
--- a/quic/core/quic_crypto_client_stream_test.cc
+++ b/quic/core/quic_crypto_client_stream_test.cc
@@ -46,13 +46,7 @@
     CreateConnection();
   }
 
-  void CreateConnection() {
-    connection_ =
-        new PacketSavingConnection(&client_helper_, &alarm_factory_,
-                                   Perspective::IS_CLIENT, supported_versions_);
-    // Advance the time, because timers do not like uninitialized times.
-    connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
-
+  void CreateSession() {
     session_ = std::make_unique<TestQuicSpdyClientSession>(
         connection_, DefaultQuicConfig(), supported_versions_, server_id_,
         &crypto_config_);
@@ -61,6 +55,15 @@
             {AlpnForVersion(connection_->version())})));
   }
 
+  void CreateConnection() {
+    connection_ =
+        new PacketSavingConnection(&client_helper_, &alarm_factory_,
+                                   Perspective::IS_CLIENT, supported_versions_);
+    // Advance the time, because timers do not like uninitialized times.
+    connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
+    CreateSession();
+  }
+
   void UseTlsHandshake() {
     supported_versions_.clear();
     for (ParsedQuicVersion version : AllSupportedVersions()) {
@@ -71,6 +74,16 @@
     }
   }
 
+  void UseQuicCryptoHandshake() {
+    supported_versions_.clear();
+    for (ParsedQuicVersion version : AllSupportedVersions()) {
+      if (version.handshake_protocol != PROTOCOL_QUIC_CRYPTO) {
+        continue;
+      }
+      supported_versions_.push_back(version);
+    }
+  }
+
   void CompleteCryptoHandshake() {
     int proof_verify_details_calls = 1;
     if (stream()->handshake_protocol() != PROTOCOL_TLS1_3) {
@@ -165,6 +178,8 @@
 }
 
 TEST_F(QuicCryptoClientStreamTest, MessageAfterHandshake) {
+  UseQuicCryptoHandshake();
+  CreateConnection();
   CompleteCryptoHandshake();
 
   EXPECT_CALL(
@@ -176,6 +191,8 @@
 }
 
 TEST_F(QuicCryptoClientStreamTest, BadMessageType) {
+  UseQuicCryptoHandshake();
+  CreateConnection();
   stream()->CryptoConnect();
 
   message_.set_tag(kCHLO);
@@ -187,6 +204,8 @@
 }
 
 TEST_F(QuicCryptoClientStreamTest, NegotiatedParameters) {
+  UseQuicCryptoHandshake();
+  CreateConnection();
   CompleteCryptoHandshake();
 
   const QuicConfig* config = session_->config();
@@ -199,6 +218,8 @@
 }
 
 TEST_F(QuicCryptoClientStreamTest, ExpiredServerConfig) {
+  UseQuicCryptoHandshake();
+  CreateConnection();
   // Seed the config with a cached server config.
   CompleteCryptoHandshake();
 
@@ -257,6 +278,8 @@
 TEST_F(QuicCryptoClientStreamTest, ServerConfigUpdate) {
   // Test that the crypto client stream can receive server config updates after
   // the connection has been established.
+  UseQuicCryptoHandshake();
+  CreateConnection();
   CompleteCryptoHandshake();
 
   QuicCryptoClientConfig::CachedState* state =
@@ -308,6 +331,8 @@
 TEST_F(QuicCryptoClientStreamTest, ServerConfigUpdateWithCert) {
   // Test that the crypto client stream can receive and use server config
   // updates with certificates after the connection has been established.
+  UseQuicCryptoHandshake();
+  CreateConnection();
   CompleteCryptoHandshake();
 
   // Build a server config update message with certificates
@@ -363,6 +388,8 @@
 }
 
 TEST_F(QuicCryptoClientStreamTest, ServerConfigUpdateBeforeHandshake) {
+  UseQuicCryptoHandshake();
+  CreateConnection();
   EXPECT_CALL(
       *connection_,
       CloseConnection(QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE, _, _));
@@ -375,14 +402,13 @@
 TEST_F(QuicCryptoClientStreamTest, PreferredVersion) {
   // This mimics the case where client receives version negotiation packet, such
   // that, the preferred version is different from the packets' version.
+  UseQuicCryptoHandshake();
   connection_ = new PacketSavingConnection(
       &client_helper_, &alarm_factory_, Perspective::IS_CLIENT,
       ParsedVersionOfIndex(supported_versions_, 1));
   connection_->AdvanceTime(QuicTime::Delta::FromSeconds(1));
 
-  session_ = std::make_unique<TestQuicSpdyClientSession>(
-      connection_, DefaultQuicConfig(), supported_versions_, server_id_,
-      &crypto_config_);
+  CreateSession();
   CompleteCryptoHandshake();
   // 2 CHLOs are sent.
   ASSERT_EQ(2u, session_->sent_crypto_handshake_messages().size());
diff --git a/quic/core/quic_crypto_server_stream_test.cc b/quic/core/quic_crypto_server_stream_test.cc
index 86ad6df..60882b8 100644
--- a/quic/core/quic_crypto_server_stream_test.cc
+++ b/quic/core/quic_crypto_server_stream_test.cc
@@ -152,6 +152,28 @@
                                         server_connection_, server_stream(), 0);
   }
 
+  void UseTlsHandshake() {
+    client_options_.only_tls_versions = true;
+    supported_versions_.clear();
+    for (ParsedQuicVersion version : AllSupportedVersions()) {
+      if (version.handshake_protocol != PROTOCOL_TLS1_3) {
+        continue;
+      }
+      supported_versions_.push_back(version);
+    }
+  }
+
+  void UseQuicCryptoHandshake() {
+    client_options_.only_quic_crypto_versions = true;
+    supported_versions_.clear();
+    for (ParsedQuicVersion version : AllSupportedVersions()) {
+      if (version.handshake_protocol != PROTOCOL_QUIC_CRYPTO) {
+        continue;
+      }
+      supported_versions_.push_back(version);
+    }
+  }
+
  protected:
   // Every connection gets its own MockQuicConnectionHelper and
   // MockAlarmFactory, tracked separately from the server and client state so
@@ -194,6 +216,7 @@
   // test should send:
   //   * One to get a source-address token and certificates.
   //   * One to complete the handshake.
+  UseQuicCryptoHandshake();
   Initialize();
   EXPECT_EQ(2, CompleteCryptoHandshake());
   EXPECT_TRUE(server_stream()->encryption_established());
@@ -201,14 +224,7 @@
 }
 
 TEST_P(QuicCryptoServerStreamTest, ConnectedAfterTlsHandshake) {
-  client_options_.only_tls_versions = true;
-  supported_versions_.clear();
-  for (ParsedQuicVersion version : AllSupportedVersions()) {
-    if (version.handshake_protocol != PROTOCOL_TLS1_3) {
-      continue;
-    }
-    supported_versions_.push_back(version);
-  }
+  UseTlsHandshake();
   Initialize();
   CompleteCryptoHandshake();
   EXPECT_EQ(PROTOCOL_TLS1_3, server_stream()->handshake_protocol());
@@ -217,6 +233,7 @@
 }
 
 TEST_P(QuicCryptoServerStreamTest, ForwardSecureAfterCHLO) {
+  UseQuicCryptoHandshake();
   Initialize();
   InitializeFakeClient();
 
@@ -238,6 +255,7 @@
 }
 
 TEST_P(QuicCryptoServerStreamTest, ZeroRTT) {
+  UseQuicCryptoHandshake();
   Initialize();
   InitializeFakeClient();
 
@@ -269,6 +287,7 @@
 }
 
 TEST_P(QuicCryptoServerStreamTest, FailByPolicy) {
+  UseQuicCryptoHandshake();
   Initialize();
   InitializeFakeClient();
 
@@ -281,6 +300,7 @@
 }
 
 TEST_P(QuicCryptoServerStreamTest, MessageAfterHandshake) {
+  UseQuicCryptoHandshake();
   Initialize();
   CompleteCryptoHandshake();
   EXPECT_CALL(
@@ -292,6 +312,7 @@
 }
 
 TEST_P(QuicCryptoServerStreamTest, BadMessageType) {
+  UseQuicCryptoHandshake();
   Initialize();
 
   message_.set_tag(kSHLO);
@@ -310,6 +331,7 @@
 }
 
 TEST_P(QuicCryptoServerStreamTest, SendSCUPAfterHandshakeComplete) {
+  UseQuicCryptoHandshake();
   Initialize();
 
   InitializeFakeClient();
@@ -347,6 +369,7 @@
                          ::testing::PrintToStringParamName());
 
 TEST_P(QuicCryptoServerStreamTestWithFailingProofSource, Test) {
+  UseQuicCryptoHandshake();
   Initialize();
   InitializeFakeClient();
 
@@ -385,6 +408,7 @@
 // connection in close succession could cause a crash, especially when the use
 // of Mentat signing meant that it took a while for each CHLO to be processed.
 TEST_P(QuicCryptoServerStreamTestWithFakeProofSource, MultipleChlo) {
+  UseQuicCryptoHandshake();
   Initialize();
   GetFakeProofSource()->Activate();
   EXPECT_CALL(*server_session_->helper(), CanAcceptClientHello(_, _, _, _, _))
diff --git a/quic/test_tools/crypto_test_utils.cc b/quic/test_tools/crypto_test_utils.cc
index 1379944..9537ec7 100644
--- a/quic/test_tools/crypto_test_utils.cc
+++ b/quic/test_tools/crypto_test_utils.cc
@@ -280,6 +280,15 @@
       }
       supported_versions.push_back(version);
     }
+    CHECK(!options.only_quic_crypto_versions);
+  } else if (options.only_quic_crypto_versions) {
+    supported_versions.clear();
+    for (ParsedQuicVersion version : AllSupportedVersions()) {
+      if (version.handshake_protocol != PROTOCOL_QUIC_CRYPTO) {
+        continue;
+      }
+      supported_versions.push_back(version);
+    }
   }
   PacketSavingConnection* client_conn = new PacketSavingConnection(
       helper, alarm_factory, Perspective::IS_CLIENT, supported_versions);
diff --git a/quic/test_tools/crypto_test_utils.h b/quic/test_tools/crypto_test_utils.h
index d80210a..3d58837 100644
--- a/quic/test_tools/crypto_test_utils.h
+++ b/quic/test_tools/crypto_test_utils.h
@@ -62,6 +62,10 @@
   // If only_tls_versions is set, then the client will only use TLS for the
   // crypto handshake.
   bool only_tls_versions = false;
+
+  // If only_quic_crypto_versions is set, then the client will only use
+  // PROTOCOL_QUIC_CRYPTO for the crypto handshake.
+  bool only_quic_crypto_versions = false;
 };
 
 // Returns a QuicCryptoServerConfig that is in a reasonable configuration to