Fix some issues regarding quic TestPacketWriter changing its perspective.

In WritePacket() the decrypter's perspective should be associated with framer's perspective.

QuicConnectionTest::set_pespective() should also change peer_creator's and peer_framer's perspective.
QuicTestConnection::set_perspective() should also alter encrypters' perspective at different encryption levels.
PiperOrigin-RevId: 334481658
Change-Id: Icf627893e657967f9ff5e25ba3e8f3737e4ebe1c
diff --git a/quic/core/quic_connection_test.cc b/quic/core/quic_connection_test.cc
index 1b41e8e..0921d95 100644
--- a/quic/core/quic_connection_test.cc
+++ b/quic/core/quic_connection_test.cc
@@ -358,11 +358,26 @@
     writer()->SetSupportedVersions(versions);
   }
 
+  // This should be called before setting customized encrypters/decrypters for
+  // connection and peer creator.
   void set_perspective(Perspective perspective) {
     writer()->set_perspective(perspective);
     QuicConnectionPeer::SetPerspective(this, perspective);
     QuicSentPacketManagerPeer::SetPerspective(
         QuicConnectionPeer::GetSentPacketManager(this), perspective);
+    QuicConnectionPeer::GetFramer(this)->SetInitialObfuscators(
+        TestConnectionId());
+    for (EncryptionLevel level : {ENCRYPTION_ZERO_RTT, ENCRYPTION_HANDSHAKE,
+                                  ENCRYPTION_FORWARD_SECURE}) {
+      if (QuicConnectionPeer::GetFramer(this)->HasEncrypterOfEncryptionLevel(
+              level)) {
+        SetEncrypter(level, std::make_unique<NullEncrypter>(perspective));
+      }
+      if (QuicConnectionPeer::GetFramer(this)->HasDecrypterOfEncryptionLevel(
+              level)) {
+        InstallDecrypter(level, std::make_unique<NullDecrypter>(perspective));
+      }
+    }
   }
 
   // Enable path MTU discovery.  Assumes that the test is performed from the
@@ -575,7 +590,8 @@
         peer_creator_(connection_id_,
                       &peer_framer_,
                       /*delegate=*/nullptr),
-        writer_(new TestPacketWriter(version(), &clock_)),
+        writer_(
+            new TestPacketWriter(version(), &clock_, Perspective::IS_CLIENT)),
         connection_(connection_id_,
                     kPeerAddress,
                     helper_.get(),
@@ -1251,6 +1267,14 @@
     }
     QuicFramerPeer::SetPerspective(&peer_framer_,
                                    QuicUtils::InvertPerspective(perspective));
+    peer_framer_.SetInitialObfuscators(TestConnectionId());
+    for (EncryptionLevel level : {ENCRYPTION_ZERO_RTT, ENCRYPTION_HANDSHAKE,
+                                  ENCRYPTION_FORWARD_SECURE}) {
+      if (peer_framer_.HasEncrypterOfEncryptionLevel(level)) {
+        peer_creator_.SetEncrypter(
+            level, std::make_unique<NullEncrypter>(peer_framer_.perspective()));
+      }
+    }
   }
 
   void set_packets_between_probes_base(
@@ -6392,7 +6416,7 @@
 
 TEST_P(QuicConnectionTest, WriteBlockedAfterClientSendsConnectivityProbe) {
   PathProbeTestInit(Perspective::IS_CLIENT);
-  TestPacketWriter probing_writer(version(), &clock_);
+  TestPacketWriter probing_writer(version(), &clock_, Perspective::IS_CLIENT);
   // Block next write so that sending connectivity probe will encounter a
   // blocked write when send a connectivity probe to the peer.
   probing_writer.BlockOnNextWrite();
@@ -6424,7 +6448,7 @@
 
 TEST_P(QuicConnectionTest, WriterErrorWhenClientSendsConnectivityProbe) {
   PathProbeTestInit(Perspective::IS_CLIENT);
-  TestPacketWriter probing_writer(version(), &clock_);
+  TestPacketWriter probing_writer(version(), &clock_, Perspective::IS_CLIENT);
   probing_writer.SetShouldWriteFail();
 
   // Connection should not be closed if a connectivity probe is failed to be
@@ -8447,7 +8471,7 @@
 
   // Simulate path degrading handling by sending a probe on an alternet path.
   clock_.AdvanceTime(QuicTime::Delta::FromMilliseconds(5));
-  TestPacketWriter probing_writer(version(), &clock_);
+  TestPacketWriter probing_writer(version(), &clock_, Perspective::IS_CLIENT);
   connection_.SendConnectivityProbingPacket(&probing_writer,
                                             connection_.peer_address());
   // Verify that path degrading detection is not reset.