Add new methods to QuicFramer for controlling decrypters

This CL is a roll forward of cl/243273832. David had to make test-only changes to fix the broken test //third_party/quic/core:tls_handshaker_test. And the chromium patch is ready.

gfe-relnote: Protected behind QUIC_VERSION_99 and quic_supports_tls_handshake
PiperOrigin-RevId: 243344023
Change-Id: Ia845325b55557d4d8811d6641ae5b50bdf2aed45
diff --git a/quic/test_tools/crypto_test_utils.cc b/quic/test_tools/crypto_test_utils.cc
index af33d4e..63154b9 100644
--- a/quic/test_tools/crypto_test_utils.cc
+++ b/quic/test_tools/crypto_test_utils.cc
@@ -749,6 +749,11 @@
 void CompareCrypters(const QuicEncrypter* encrypter,
                      const QuicDecrypter* decrypter,
                      std::string label) {
+  if (encrypter == nullptr || decrypter == nullptr) {
+    ADD_FAILURE() << "Expected non-null crypters; have " << encrypter << " and "
+                  << decrypter;
+    return;
+  }
   QuicStringPiece encrypter_key = encrypter->GetKey();
   QuicStringPiece encrypter_iv = encrypter->GetNoncePrefix();
   QuicStringPiece decrypter_key = decrypter->GetKey();
diff --git a/quic/test_tools/quic_test_utils.cc b/quic/test_tools/quic_test_utils.cc
index cd54eb8..2de1491 100644
--- a/quic/test_tools/quic_test_utils.cc
+++ b/quic/test_tools/quic_test_utils.cc
@@ -499,7 +499,7 @@
     : QuicSession(connection,
                   nullptr,
                   DefaultQuicConfig(),
-                  CurrentSupportedVersions()) {
+                  connection->supported_versions()) {
   if (create_mock_crypto_stream) {
     crypto_stream_ = QuicMakeUnique<MockQuicCryptoStream>(this);
   }
@@ -923,6 +923,11 @@
   header.reset_flag = reset_flag;
   header.packet_number_length = packet_number_length;
   header.packet_number = QuicPacketNumber(packet_number);
+  if (QuicVersionHasLongHeaderLengths((*versions)[0].transport_version) &&
+      version_flag) {
+    header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
+    header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
+  }
   QuicFrame frame(QuicStreamFrame(1, false, 0, QuicStringPiece(data)));
   QuicFrames frames;
   frames.push_back(frame);
@@ -941,8 +946,7 @@
       GetIncludedDestinationConnectionIdLength(header),
       GetIncludedSourceConnectionIdLength(header), version_flag,
       false /* no diversification nonce */, packet_number_length,
-      VARIABLE_LENGTH_INTEGER_LENGTH_0, 0, VARIABLE_LENGTH_INTEGER_LENGTH_0)] =
-      0x1F;
+      header.retry_token_length_length, 0, header.length_length)] = 0x1F;
 
   char* buffer = new char[kMaxOutgoingPacketSize];
   size_t encrypted_length =
diff --git a/quic/test_tools/simulator/quic_endpoint.cc b/quic/test_tools/simulator/quic_endpoint.cc
index 1072724..9e5c3fa 100644
--- a/quic/test_tools/simulator/quic_endpoint.cc
+++ b/quic/test_tools/simulator/quic_endpoint.cc
@@ -86,8 +86,14 @@
   connection_.set_visitor(this);
   connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE,
                            QuicMakeUnique<NullEncrypter>(perspective));
-  connection_.SetDecrypter(ENCRYPTION_FORWARD_SECURE,
-                           QuicMakeUnique<NullDecrypter>(perspective));
+  if (connection_.version().KnowsWhichDecrypterToUse()) {
+    connection_.InstallDecrypter(ENCRYPTION_FORWARD_SECURE,
+                                 QuicMakeUnique<NullDecrypter>(perspective));
+    connection_.RemoveDecrypter(ENCRYPTION_INITIAL);
+  } else {
+    connection_.SetDecrypter(ENCRYPTION_FORWARD_SECURE,
+                             QuicMakeUnique<NullDecrypter>(perspective));
+  }
   connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE);
   if (perspective == Perspective::IS_SERVER) {
     // Skip version negotiation.