Fix QUICHE build on platforms using EXPECT_DEBUG_DEATH for EXPECT_QUIC_BUG

PiperOrigin-RevId: 452622101
diff --git a/quiche/quic/core/http/quic_spdy_session_test.cc b/quiche/quic/core/http/quic_spdy_session_test.cc
index f854763..4cfb5b7 100644
--- a/quiche/quic/core/http/quic_spdy_session_test.cc
+++ b/quiche/quic/core/http/quic_spdy_session_test.cc
@@ -2679,11 +2679,13 @@
       GetNthServerInitiatedUnidirectionalStreamId(transport_version(), 1);
   QuicStreamFrame data2(id2, false, 0, absl::string_view(type1, 1));
   EXPECT_CALL(debug_visitor, OnPeerControlStreamCreated(id2)).Times(0);
-  EXPECT_CALL(*connection_,
-              CloseConnection(QUIC_HTTP_DUPLICATE_UNIDIRECTIONAL_STREAM,
-                              "Control stream is received twice.", _));
   EXPECT_QUIC_PEER_BUG(
-      session_.OnStreamFrame(data2),
+      {
+        EXPECT_CALL(*connection_,
+                    CloseConnection(QUIC_HTTP_DUPLICATE_UNIDIRECTIONAL_STREAM,
+                                    "Control stream is received twice.", _));
+        session_.OnStreamFrame(data2);
+      },
       "Received a duplicate Control stream: Closing connection.");
 
   QuicStreamId id3 =
@@ -2698,11 +2700,14 @@
       GetNthServerInitiatedUnidirectionalStreamId(transport_version(), 3);
   QuicStreamFrame data4(id4, false, 0, absl::string_view(type2, 1));
   EXPECT_CALL(debug_visitor, OnPeerQpackEncoderStreamCreated(id4)).Times(0);
-  EXPECT_CALL(*connection_,
-              CloseConnection(QUIC_HTTP_DUPLICATE_UNIDIRECTIONAL_STREAM,
-                              "QPACK encoder stream is received twice.", _));
   EXPECT_QUIC_PEER_BUG(
-      session_.OnStreamFrame(data4),
+      {
+        EXPECT_CALL(
+            *connection_,
+            CloseConnection(QUIC_HTTP_DUPLICATE_UNIDIRECTIONAL_STREAM,
+                            "QPACK encoder stream is received twice.", _));
+        session_.OnStreamFrame(data4);
+      },
       "Received a duplicate QPACK encoder stream: Closing connection.");
 
   QuicStreamId id5 =
@@ -2717,11 +2722,14 @@
       GetNthServerInitiatedUnidirectionalStreamId(transport_version(), 5);
   QuicStreamFrame data6(id6, false, 0, absl::string_view(type3, 1));
   EXPECT_CALL(debug_visitor, OnPeerQpackDecoderStreamCreated(id6)).Times(0);
-  EXPECT_CALL(*connection_,
-              CloseConnection(QUIC_HTTP_DUPLICATE_UNIDIRECTIONAL_STREAM,
-                              "QPACK decoder stream is received twice.", _));
   EXPECT_QUIC_PEER_BUG(
-      session_.OnStreamFrame(data6),
+      {
+        EXPECT_CALL(
+            *connection_,
+            CloseConnection(QUIC_HTTP_DUPLICATE_UNIDIRECTIONAL_STREAM,
+                            "QPACK decoder stream is received twice.", _));
+        session_.OnStreamFrame(data6);
+      },
       "Received a duplicate QPACK decoder stream: Closing connection.");
 }
 
@@ -3790,10 +3798,12 @@
           ? GetNthClientInitiatedUnidirectionalStreamId(transport_version(), 3)
           : GetNthServerInitiatedUnidirectionalStreamId(transport_version(), 3);
   QuicStreamFrame frame(control_stream_id, /*fin=*/false, /*offset=*/0, data);
-  EXPECT_CALL(*connection_,
-              CloseConnection(QUIC_HTTP_INVALID_SETTING_VALUE, _, _));
   EXPECT_QUIC_PEER_BUG(
-      session_.OnStreamFrame(frame),
+      {
+        EXPECT_CALL(*connection_,
+                    CloseConnection(QUIC_HTTP_INVALID_SETTING_VALUE, _, _));
+        session_.OnStreamFrame(frame);
+      },
       "Received SETTINGS_ENABLE_CONNECT_PROTOCOL with invalid value");
 }
 
diff --git a/quiche/quic/core/quic_connection_test.cc b/quiche/quic/core/quic_connection_test.cc
index 682f867..ff84538 100644
--- a/quiche/quic/core/quic_connection_test.cc
+++ b/quiche/quic/core/quic_connection_test.cc
@@ -6957,14 +6957,19 @@
     return;
   }
 
-  EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF))
-      .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame));
-  EXPECT_QUIC_BUG(connection_.SaveAndSendStreamData(3, {}, 0, FIN),
-                  "Cannot send stream data with level: ENCRYPTION_INITIAL");
-  EXPECT_FALSE(connection_.connected());
-  EXPECT_EQ(1, connection_close_frame_count_);
-  EXPECT_THAT(saved_connection_close_frame_.quic_error_code,
-              IsError(QUIC_ATTEMPT_TO_SEND_UNENCRYPTED_STREAM_DATA));
+  EXPECT_QUIC_BUG(
+      {
+        EXPECT_CALL(visitor_,
+                    OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF))
+            .WillOnce(
+                Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame));
+        connection_.SaveAndSendStreamData(3, {}, 0, FIN);
+        EXPECT_FALSE(connection_.connected());
+        EXPECT_EQ(1, connection_close_frame_count_);
+        EXPECT_THAT(saved_connection_close_frame_.quic_error_code,
+                    IsError(QUIC_ATTEMPT_TO_SEND_UNENCRYPTED_STREAM_DATA));
+      },
+      "Cannot send stream data with level: ENCRYPTION_INITIAL");
 }
 
 TEST_P(QuicConnectionTest, SetRetransmissionAlarmForCryptoPacket) {
@@ -9776,60 +9781,65 @@
   set_perspective(Perspective::IS_SERVER);
   use_tagging_decrypter();
 
-  EXPECT_CALL(visitor_, OnHandshakePacketSent());
-
-  EXPECT_CALL(visitor_, OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF))
-      .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame));
-
-  ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_INITIAL);
   auto test_body = [&] {
-    QuicConnection::ScopedPacketFlusher flusher(&connection_);
-    connection_.SetEncrypter(ENCRYPTION_INITIAL,
-                             std::make_unique<TaggingEncrypter>(0x01));
-    connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
-    connection_.SendCryptoDataWithString("foo", 0);
-    // Verify this packet is on hold.
-    EXPECT_EQ(0u, writer_->packets_write_attempts());
+    EXPECT_CALL(visitor_, OnHandshakePacketSent());
 
-    connection_.SetEncrypter(ENCRYPTION_HANDSHAKE,
-                             std::make_unique<TaggingEncrypter>(0x02));
-    connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE);
-    connection_.SendCryptoDataWithString("bar", 3);
-    EXPECT_EQ(0u, writer_->packets_write_attempts());
+    EXPECT_CALL(visitor_,
+                OnConnectionClosed(_, ConnectionCloseSource::FROM_SELF))
+        .WillOnce(Invoke(this, &QuicConnectionTest::SaveConnectionCloseFrame));
 
-    connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE,
-                             std::make_unique<TaggingEncrypter>(0x03));
-    connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE);
-    SendStreamDataToPeer(2, "baz", 3, NO_FIN, nullptr);
+    ProcessDataPacketAtLevel(1, !kHasStopWaiting, ENCRYPTION_INITIAL);
 
-    creator_->Flush();
+    {
+      QuicConnection::ScopedPacketFlusher flusher(&connection_);
+      connection_.SetEncrypter(ENCRYPTION_INITIAL,
+                               std::make_unique<TaggingEncrypter>(0x01));
+      connection_.SetDefaultEncryptionLevel(ENCRYPTION_INITIAL);
+      connection_.SendCryptoDataWithString("foo", 0);
+      // Verify this packet is on hold.
+      EXPECT_EQ(0u, writer_->packets_write_attempts());
 
-    auto& coalesced_packet =
-        QuicConnectionPeer::GetCoalescedPacket(&connection_);
-    QuicPacketLength coalesced_packet_max_length =
-        coalesced_packet.max_packet_length();
-    QuicCoalescedPacketPeer::SetMaxPacketLength(coalesced_packet,
-                                                coalesced_packet.length());
+      connection_.SetEncrypter(ENCRYPTION_HANDSHAKE,
+                               std::make_unique<TaggingEncrypter>(0x02));
+      connection_.SetDefaultEncryptionLevel(ENCRYPTION_HANDSHAKE);
+      connection_.SendCryptoDataWithString("bar", 3);
+      EXPECT_EQ(0u, writer_->packets_write_attempts());
 
-    // Make the coalescer's FORWARD_SECURE packet longer.
-    *QuicCoalescedPacketPeer::GetMutableEncryptedBuffer(
-        coalesced_packet, ENCRYPTION_FORWARD_SECURE) += "!!! TEST !!!";
+      connection_.SetEncrypter(ENCRYPTION_FORWARD_SECURE,
+                               std::make_unique<TaggingEncrypter>(0x03));
+      connection_.SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE);
+      SendStreamDataToPeer(2, "baz", 3, NO_FIN, nullptr);
 
-    QUIC_LOG(INFO) << "Reduced coalesced_packet_max_length from "
-                   << coalesced_packet_max_length << " to "
-                   << coalesced_packet.max_packet_length()
-                   << ", coalesced_packet.length:" << coalesced_packet.length()
-                   << ", coalesced_packet.packet_lengths:"
-                   << absl::StrJoin(coalesced_packet.packet_lengths(), ":");
+      creator_->Flush();
+
+      auto& coalesced_packet =
+          QuicConnectionPeer::GetCoalescedPacket(&connection_);
+      QuicPacketLength coalesced_packet_max_length =
+          coalesced_packet.max_packet_length();
+      QuicCoalescedPacketPeer::SetMaxPacketLength(coalesced_packet,
+                                                  coalesced_packet.length());
+
+      // Make the coalescer's FORWARD_SECURE packet longer.
+      *QuicCoalescedPacketPeer::GetMutableEncryptedBuffer(
+          coalesced_packet, ENCRYPTION_FORWARD_SECURE) += "!!! TEST !!!";
+
+      QUIC_LOG(INFO) << "Reduced coalesced_packet_max_length from "
+                     << coalesced_packet_max_length << " to "
+                     << coalesced_packet.max_packet_length()
+                     << ", coalesced_packet.length:"
+                     << coalesced_packet.length()
+                     << ", coalesced_packet.packet_lengths:"
+                     << absl::StrJoin(coalesced_packet.packet_lengths(), ":");
+    }
+
+    EXPECT_FALSE(connection_.connected());
+    EXPECT_THAT(saved_connection_close_frame_.quic_error_code,
+                IsError(QUIC_FAILED_TO_SERIALIZE_PACKET));
+    EXPECT_EQ(saved_connection_close_frame_.error_details,
+              "Failed to serialize coalesced packet.");
   };
 
   EXPECT_QUIC_BUG(test_body(), "SerializeCoalescedPacket failed.");
-
-  EXPECT_FALSE(connection_.connected());
-  EXPECT_THAT(saved_connection_close_frame_.quic_error_code,
-              IsError(QUIC_FAILED_TO_SERIALIZE_PACKET));
-  EXPECT_EQ(saved_connection_close_frame_.error_details,
-            "Failed to serialize coalesced packet.");
 }
 
 TEST_P(QuicConnectionTest, LegacyVersionEncapsulation) {
@@ -14386,21 +14396,24 @@
   QuicAckFrame ack = InitAckFrame({{QuicPacketNumber(1), QuicPacketNumber(5)}});
   frames.push_back(QuicFrame(&ack));
 
-  EXPECT_CALL(visitor_, OnConnectionMigration(_)).Times(1);
-
   // Invoke OnCanWrite.
-  EXPECT_CALL(visitor_, OnStreamFrame(_))
-      .WillOnce(
-          InvokeWithoutArgs(&notifier_, &SimpleSessionNotifier::OnCanWrite));
   QuicIpAddress ip_address;
   ASSERT_TRUE(ip_address.FromString("127.0.52.223"));
-  EXPECT_QUIC_BUG(ProcessFramesPacketWithAddresses(
-                      frames, kSelfAddress, QuicSocketAddress(ip_address, 1000),
-                      ENCRYPTION_FORWARD_SECURE),
-                  "Try to write mid packet processing");
-  EXPECT_EQ(1u, writer_->path_challenge_frames().size());
-  // Verify stream frame will not be retransmitted.
-  EXPECT_TRUE(writer_->stream_frames().empty());
+  EXPECT_QUIC_BUG(
+      {
+        EXPECT_CALL(visitor_, OnConnectionMigration(_)).Times(1);
+        EXPECT_CALL(visitor_, OnStreamFrame(_))
+            .WillOnce(InvokeWithoutArgs(&notifier_,
+                                        &SimpleSessionNotifier::OnCanWrite));
+        ProcessFramesPacketWithAddresses(frames, kSelfAddress,
+                                         QuicSocketAddress(ip_address, 1000),
+                                         ENCRYPTION_FORWARD_SECURE);
+        EXPECT_EQ(1u, writer_->path_challenge_frames().size());
+
+        // Verify stream frame will not be retransmitted.
+        EXPECT_TRUE(writer_->stream_frames().empty());
+      },
+      "Try to write mid packet processing");
 }
 
 TEST_P(QuicConnectionTest, PtoSendStreamData) {
diff --git a/quiche/quic/core/quic_dispatcher_test.cc b/quiche/quic/core/quic_dispatcher_test.cc
index 3f65d3e..e5e6ea2 100644
--- a/quiche/quic/core/quic_dispatcher_test.cc
+++ b/quiche/quic/core/quic_dispatcher_test.cc
@@ -2162,20 +2162,23 @@
 
 TEST_P(QuicDispatcherWriteBlockedListTest,
        RemoveConnectionFromWriteBlockedListWhenDeletingSessions) {
-  dispatcher_->OnConnectionClosed(connection1()->connection_id(),
-                                  QUIC_PACKET_WRITE_ERROR, "Closed by test.",
-                                  ConnectionCloseSource::FROM_SELF);
+  EXPECT_QUIC_BUG(
+      {
+        dispatcher_->OnConnectionClosed(
+            connection1()->connection_id(), QUIC_PACKET_WRITE_ERROR,
+            "Closed by test.", ConnectionCloseSource::FROM_SELF);
 
-  SetBlocked();
+        SetBlocked();
 
-  ASSERT_FALSE(dispatcher_->HasPendingWrites());
-  SetBlocked();
-  dispatcher_->OnWriteBlocked(connection1());
-  ASSERT_TRUE(dispatcher_->HasPendingWrites());
+        ASSERT_FALSE(dispatcher_->HasPendingWrites());
+        SetBlocked();
+        dispatcher_->OnWriteBlocked(connection1());
+        ASSERT_TRUE(dispatcher_->HasPendingWrites());
 
-  EXPECT_QUIC_BUG(dispatcher_->DeleteSessions(),
-                  "QuicConnection was in WriteBlockedList before destruction");
-  MarkSession1Deleted();
+        dispatcher_->DeleteSessions();
+        MarkSession1Deleted();
+      },
+      "QuicConnection was in WriteBlockedList before destruction");
 }
 
 class QuicDispatcherSupportMultipleConnectionIdPerConnectionTest
diff --git a/quiche/quic/core/quic_flow_controller_test.cc b/quiche/quic/core/quic_flow_controller_test.cc
index a03cff0..567b120 100644
--- a/quiche/quic/core/quic_flow_controller_test.cc
+++ b/quiche/quic/core/quic_flow_controller_test.cc
@@ -95,13 +95,16 @@
   EXPECT_EQ(send_window_, flow_controller_->SendWindowSize());
 
   // Try to send more bytes, violating flow control.
-  EXPECT_CALL(*connection_,
-              CloseConnection(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA, _, _));
   EXPECT_QUIC_BUG(
-      flow_controller_->AddBytesSent(send_window_ * 10),
+      {
+        EXPECT_CALL(
+            *connection_,
+            CloseConnection(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA, _, _));
+        flow_controller_->AddBytesSent(send_window_ * 10);
+        EXPECT_TRUE(flow_controller_->IsBlocked());
+        EXPECT_EQ(0u, flow_controller_->SendWindowSize());
+      },
       absl::StrCat("Trying to send an extra ", send_window_ * 10, " bytes"));
-  EXPECT_TRUE(flow_controller_->IsBlocked());
-  EXPECT_EQ(0u, flow_controller_->SendWindowSize());
 }
 
 TEST_F(QuicFlowControllerTest, ReceivingBytes) {
diff --git a/quiche/quic/core/quic_framer_test.cc b/quiche/quic/core/quic_framer_test.cc
index 7751510..9b022a2 100644
--- a/quiche/quic/core/quic_framer_test.cc
+++ b/quiche/quic/core/quic_framer_test.cc
@@ -10446,11 +10446,14 @@
       /*retry_token_length=*/0, VARIABLE_LENGTH_INTEGER_LENGTH_0);
   char buffer[kMaxOutgoingPacketSize];
   size_t encrypted_length = 1;
-  EXPECT_QUIC_BUG(encrypted_length = framer_.EncryptPayload(
-                      ENCRYPTION_INITIAL, kPacketNumber, *packet, buffer,
-                      kMaxOutgoingPacketSize),
-                  "packet is shorter than associated data length");
-  EXPECT_EQ(0u, encrypted_length);
+  EXPECT_QUIC_BUG(
+      {
+        encrypted_length =
+            framer_.EncryptPayload(ENCRYPTION_INITIAL, kPacketNumber, *packet,
+                                   buffer, kMaxOutgoingPacketSize);
+        EXPECT_EQ(0u, encrypted_length);
+      },
+      "packet is shorter than associated data length");
 }
 
 TEST_P(QuicFramerTest, EncryptPacketWithVersionFlag) {
diff --git a/quiche/quic/core/quic_stream_test.cc b/quiche/quic/core/quic_stream_test.cc
index e96a99d..51b25a5 100644
--- a/quiche/quic/core/quic_stream_test.cc
+++ b/quiche/quic/core/quic_stream_test.cc
@@ -480,9 +480,13 @@
       .WillOnce(Invoke(session_.get(), &MockQuicSession::ConsumeData));
   stream_->WriteOrBufferData(data, false, nullptr);
   EXPECT_TRUE(session_->HasUnackedStreamData());
-  EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
-  EXPECT_QUIC_BUG(stream_->WriteOrBufferData("a", false, nullptr),
-                  "Write too many data via stream");
+  EXPECT_QUIC_BUG(
+      {
+        EXPECT_CALL(*connection_,
+                    CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
+        stream_->WriteOrBufferData("a", false, nullptr);
+      },
+      "Write too many data via stream");
 }
 
 TEST_P(QuicStreamTest, ConnectionCloseAfterStreamClose) {
@@ -789,11 +793,14 @@
 
 TEST_P(QuicStreamTest, StreamTooLong) {
   Initialize();
-  EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _))
-      .Times(1);
   QuicStreamFrame stream_frame(stream_->id(), false, kMaxStreamLength, ".");
   EXPECT_QUIC_PEER_BUG(
-      stream_->OnStreamFrame(stream_frame),
+      {
+        EXPECT_CALL(*connection_,
+                    CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _))
+            .Times(1);
+        stream_->OnStreamFrame(stream_frame);
+      },
       absl::StrCat("Receive stream frame on stream ", stream_->id(),
                    " reaches max stream length"));
 }
@@ -1237,9 +1244,13 @@
   quiche::QuicheMemSliceStorage storage2(
       &iov2, 1,
       session_->connection()->helper()->GetStreamSendBufferAllocator(), 1024);
-  EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
-  EXPECT_QUIC_BUG(stream_->WriteMemSlices(storage2.ToSpan(), false),
-                  "Write too many data via stream");
+  EXPECT_QUIC_BUG(
+      {
+        EXPECT_CALL(*connection_,
+                    CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
+        stream_->WriteMemSlices(storage2.ToSpan(), false);
+      },
+      "Write too many data via stream");
 }
 
 TEST_P(QuicStreamTest, WriteMemSlices) {
@@ -1328,9 +1339,13 @@
   EXPECT_EQ(5u, consumed.bytes_consumed);
 
   quiche::QuicheMemSlice slice2 = MemSliceFromString("6");
-  EXPECT_CALL(*connection_, CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
-  EXPECT_QUIC_BUG(stream_->WriteMemSlice(std::move(slice2), false),
-                  "Write too many data via stream");
+  EXPECT_QUIC_BUG(
+      {
+        EXPECT_CALL(*connection_,
+                    CloseConnection(QUIC_STREAM_LENGTH_OVERFLOW, _, _));
+        stream_->WriteMemSlice(std::move(slice2), false);
+      },
+      "Write too many data via stream");
 }
 
 TEST_P(QuicStreamTest, StreamDataGetAckedMultipleTimes) {