Set packet type in quic_connection_test according to encryption levels.
Also have some other minor test changes.

gfe-relnote: n/a (test only change)
PiperOrigin-RevId: 238230970
Change-Id: I35cc161dc9e42468c3d87f951c9af9b74ee599fa
diff --git a/quic/core/http/quic_spdy_client_session_test.cc b/quic/core/http/quic_spdy_client_session_test.cc
index 3192d8c..1f5b45d 100644
--- a/quic/core/http/quic_spdy_client_session_test.cc
+++ b/quic/core/http/quic_spdy_client_session_test.cc
@@ -413,6 +413,9 @@
 }
 
 TEST_P(QuicSpdyClientSessionTest, GoAwayReceived) {
+  if (connection_->transport_version() == QUIC_VERSION_99) {
+    return;
+  }
   CompleteCryptoHandshake();
 
   // After receiving a GoAway, I should no longer be able to create outgoing
diff --git a/quic/core/quic_connection_test.cc b/quic/core/quic_connection_test.cc
index 93ad4eb..b0e173b 100644
--- a/quic/core/quic_connection_test.cc
+++ b/quic/core/quic_connection_test.cc
@@ -89,6 +89,21 @@
   return QuicUtils::GetHeadersStreamId(version) + n * 2;
 }
 
+QuicLongHeaderType EncryptionlevelToLongHeaderType(EncryptionLevel level) {
+  switch (level) {
+    case ENCRYPTION_NONE:
+      return INITIAL;
+    case ENCRYPTION_ZERO_RTT:
+      return ZERO_RTT_PROTECTED;
+    case ENCRYPTION_FORWARD_SECURE:
+      DCHECK(false);
+      return INVALID_PACKET_TYPE;
+    default:
+      DCHECK(false);
+      return INVALID_PACKET_TYPE;
+  }
+}
+
 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message.
 class TaggingEncrypter : public QuicEncrypter {
  public:
@@ -1064,7 +1079,7 @@
                                   bool has_stop_waiting,
                                   EncryptionLevel level) {
     std::unique_ptr<QuicPacket> packet(
-        ConstructDataPacket(number, has_stop_waiting));
+        ConstructDataPacket(number, has_stop_waiting, level));
     char buffer[kMaxPacketSize];
     size_t encrypted_length = peer_framer_.EncryptPayload(
         level, QuicPacketNumber(number), *packet, buffer, kMaxPacketSize);
@@ -1173,8 +1188,22 @@
   }
 
   std::unique_ptr<QuicPacket> ConstructDataPacket(uint64_t number,
-                                                  bool has_stop_waiting) {
+                                                  bool has_stop_waiting,
+                                                  EncryptionLevel level) {
     QuicPacketHeader header;
+    if (peer_framer_.transport_version() > QUIC_VERSION_43 &&
+        level < ENCRYPTION_FORWARD_SECURE) {
+      // Set long header type accordingly.
+      header.version_flag = true;
+      header.long_packet_type = EncryptionlevelToLongHeaderType(level);
+      if (QuicVersionHasLongHeaderLengths(
+              peer_framer_.version().transport_version)) {
+        header.length_length = VARIABLE_LENGTH_INTEGER_LENGTH_2;
+        if (header.long_packet_type == INITIAL) {
+          header.retry_token_length_length = VARIABLE_LENGTH_INTEGER_LENGTH_1;
+        }
+      }
+    }
     // Set connection_id to peer's in memory representation as this data packet
     // is created by peer_framer.
     header.destination_connection_id = connection_id_;
@@ -1183,6 +1212,14 @@
     if (peer_framer_.transport_version() > QUIC_VERSION_43 &&
         peer_framer_.perspective() == Perspective::IS_SERVER) {
       header.destination_connection_id_included = CONNECTION_ID_ABSENT;
+      if (header.version_flag) {
+        header.source_connection_id = connection_id_;
+        header.source_connection_id_included = CONNECTION_ID_PRESENT;
+        if (GetParam().version.handshake_protocol == PROTOCOL_QUIC_CRYPTO &&
+            header.long_packet_type == ZERO_RTT_PROTECTED) {
+          header.nonce = &diversification_nonce_;
+        }
+      }
     }
     header.packet_number = QuicPacketNumber(number);
 
@@ -1351,6 +1388,7 @@
   QuicConnectionIdIncluded connection_id_included_;
 
   SimpleSessionNotifier notifier_;
+  DiversificationNonce diversification_nonce_;
 };
 
 // Run all end to end tests with all supported versions.
@@ -2396,7 +2434,9 @@
     ProcessDataPacket(i);
   }
   // Send a packet containing stream frame.
-  SendStreamDataToPeer(1, "bar", 0, NO_FIN, nullptr);
+  SendStreamDataToPeer(
+      QuicUtils::GetCryptoStreamId(connection_.version().transport_version),
+      "bar", 0, NO_FIN, nullptr);
 
   // Session will not be informed until receiving another 20 packets.
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _)).Times(19);
@@ -2717,7 +2757,9 @@
   EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1);
   ProcessDataPacket(1);
   QuicPacketNumber last_packet;
-  SendStreamDataToPeer(1, "foo", 0, NO_FIN, &last_packet);
+  SendStreamDataToPeer(
+      QuicUtils::GetCryptoStreamId(connection_.version().transport_version),
+      "foo", 0, NO_FIN, &last_packet);
   // Verify ack is bundled with outging packet.
   EXPECT_FALSE(writer_->ack_frames().empty());
 
@@ -2731,7 +2773,11 @@
 
   // Process a data packet to cause the visitor's OnCanWrite to be invoked.
   EXPECT_CALL(visitor_, OnStreamFrame(_)).Times(1);
-  ProcessDataPacket(2);
+  peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE,
+                            QuicMakeUnique<TaggingEncrypter>(0x01));
+  connection_.SetDecrypter(ENCRYPTION_FORWARD_SECURE,
+                           QuicMakeUnique<StrictTaggingDecrypter>(0x01));
+  ProcessDataPacketAtLevel(2, false, ENCRYPTION_FORWARD_SECURE);
 
   EXPECT_EQ(0u, connection_.NumQueuedPackets());
   EXPECT_FALSE(connection_.HasQueuedData());
@@ -3341,8 +3387,8 @@
   const uint64_t received_packet_num = 1;
   const bool has_stop_waiting = false;
   const EncryptionLevel level = ENCRYPTION_NONE;
-  std::unique_ptr<QuicPacket> packet(
-      ConstructDataPacket(received_packet_num, has_stop_waiting));
+  std::unique_ptr<QuicPacket> packet(ConstructDataPacket(
+      received_packet_num, has_stop_waiting, ENCRYPTION_NONE));
   char buffer[kMaxPacketSize];
   size_t encrypted_length =
       peer_framer_.EncryptPayload(level, QuicPacketNumber(received_packet_num),
@@ -4900,7 +4946,8 @@
 TEST_P(QuicConnectionTest, SendScheduler) {
   // Test that if we send a packet without delay, it is not queued.
   QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT);
-  std::unique_ptr<QuicPacket> packet = ConstructDataPacket(1, !kHasStopWaiting);
+  std::unique_ptr<QuicPacket> packet =
+      ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_NONE);
   QuicPacketCreatorPeer::SetPacketNumber(creator_, 1);
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, _, _, _));
   connection_.SendPacket(ENCRYPTION_NONE, 1, std::move(packet),
@@ -4913,7 +4960,8 @@
   // packet at which point self_address_ might be uninitialized.
   QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT);
   EXPECT_CALL(visitor_, OnConnectionClosed(_, _, _)).Times(1);
-  std::unique_ptr<QuicPacket> packet = ConstructDataPacket(1, !kHasStopWaiting);
+  std::unique_ptr<QuicPacket> packet =
+      ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_NONE);
   QuicPacketCreatorPeer::SetPacketNumber(creator_, 1);
   writer_->SetShouldWriteFail();
   connection_.SendPacket(ENCRYPTION_NONE, 1, std::move(packet),
@@ -4922,7 +4970,8 @@
 
 TEST_P(QuicConnectionTest, SendSchedulerEAGAIN) {
   QuicFramerPeer::SetPerspective(&peer_framer_, Perspective::IS_CLIENT);
-  std::unique_ptr<QuicPacket> packet = ConstructDataPacket(1, !kHasStopWaiting);
+  std::unique_ptr<QuicPacket> packet =
+      ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_NONE);
   QuicPacketCreatorPeer::SetPacketNumber(creator_, 1);
   BlockOnNextWrite();
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(2u), _, _))
@@ -5076,6 +5125,7 @@
   EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
   EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline());
   // Simulate delayed ack alarm firing.
+  clock_.AdvanceTime(DefaultDelayedAckTime());
   connection_.GetAckAlarm()->Fire();
   // Check that ack is sent and that delayed ack alarm is reset.
   if (GetParam().no_stop_waiting) {
@@ -5114,6 +5164,7 @@
   EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
   EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline());
   // Simulate delayed ack alarm firing.
+  clock_.AdvanceTime(DefaultDelayedAckTime());
   connection_.GetAckAlarm()->Fire();
   // Check that ack is sent and that delayed ack alarm is reset.
   if (GetParam().no_stop_waiting) {
@@ -5136,6 +5187,7 @@
   EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
   EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline());
   // Simulate delayed ack alarm firing.
+  clock_.AdvanceTime(DefaultDelayedAckTime());
   connection_.GetAckAlarm()->Fire();
   // Check that ack is sent and that delayed ack alarm is reset.
   if (GetParam().no_stop_waiting) {
@@ -5249,6 +5301,7 @@
   EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
   EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline());
   // Simulate delayed ack alarm firing.
+  clock_.AdvanceTime(DefaultDelayedAckTime());
   connection_.GetAckAlarm()->Fire();
   // Check that ack is sent and that delayed ack alarm is reset.
   if (GetParam().no_stop_waiting) {
@@ -5271,6 +5324,7 @@
   EXPECT_TRUE(connection_.GetAckAlarm()->IsSet());
   EXPECT_EQ(ack_time, connection_.GetAckAlarm()->deadline());
   // Simulate delayed ack alarm firing.
+  clock_.AdvanceTime(DefaultDelayedAckTime());
   connection_.GetAckAlarm()->Fire();
   // Check that ack is sent and that delayed ack alarm is reset.
   if (GetParam().no_stop_waiting) {
@@ -5849,7 +5903,12 @@
 
 TEST_P(QuicConnectionTest, SendDelayedAckOnOutgoingPacket) {
   EXPECT_CALL(visitor_, OnSuccessfulVersionNegotiation(_));
-  ProcessPacket(1);
+  EXPECT_CALL(visitor_, OnStreamFrame(_));
+  peer_framer_.SetEncrypter(ENCRYPTION_FORWARD_SECURE,
+                            QuicMakeUnique<TaggingEncrypter>(0x01));
+  connection_.SetDecrypter(ENCRYPTION_FORWARD_SECURE,
+                           QuicMakeUnique<StrictTaggingDecrypter>(0x01));
+  ProcessDataPacketAtLevel(1, false, ENCRYPTION_FORWARD_SECURE);
   connection_.SendStreamDataWithString(
       GetNthClientInitiatedStreamId(1, connection_.transport_version()), "foo",
       0, NO_FIN);
@@ -6034,7 +6093,8 @@
                               ConnectionCloseBehavior::SILENT_CLOSE);
   EXPECT_FALSE(connection_.connected());
   EXPECT_FALSE(connection_.CanWriteStreamData());
-  std::unique_ptr<QuicPacket> packet = ConstructDataPacket(1, !kHasStopWaiting);
+  std::unique_ptr<QuicPacket> packet =
+      ConstructDataPacket(1, !kHasStopWaiting, ENCRYPTION_NONE);
   EXPECT_CALL(*send_algorithm_, OnPacketSent(_, _, QuicPacketNumber(1), _, _))
       .Times(0);
   connection_.SendPacket(ENCRYPTION_NONE, 1, std::move(packet),
@@ -7789,7 +7849,8 @@
   // Let connection process a Google QUIC packet.
   peer_framer_.set_version_for_tests(
       ParsedQuicVersion(PROTOCOL_QUIC_CRYPTO, QUIC_VERSION_43));
-  std::unique_ptr<QuicPacket> packet(ConstructDataPacket(2, !kHasStopWaiting));
+  std::unique_ptr<QuicPacket> packet(
+      ConstructDataPacket(2, !kHasStopWaiting, ENCRYPTION_NONE));
   char buffer[kMaxPacketSize];
   size_t encrypted_length = peer_framer_.EncryptPayload(
       ENCRYPTION_NONE, QuicPacketNumber(2), *packet, buffer, kMaxPacketSize);
diff --git a/quic/core/quic_session_test.cc b/quic/core/quic_session_test.cc
index a1b294b..915d31b 100644
--- a/quic/core/quic_session_test.cc
+++ b/quic/core/quic_session_test.cc
@@ -1139,6 +1139,8 @@
       .WillOnce(Invoke(
           connection_,
           &MockQuicConnection::ReallySendConnectivityProbingResponsePacket));
+  QuicConnectionPeer::SetLastHeaderFormat(connection_,
+                                          IETF_QUIC_SHORT_HEADER_PACKET);
   // Need to explicitly do this to emulate the reception of a PathChallenge,
   // which stores its payload for use in generating the response.
   connection_->OnPathChallengeFrame(
diff --git a/quic/test_tools/quic_connection_peer.cc b/quic/test_tools/quic_connection_peer.cc
index 100fe15..953e495 100644
--- a/quic/test_tools/quic_connection_peer.cc
+++ b/quic/test_tools/quic_connection_peer.cc
@@ -351,5 +351,11 @@
   return connection->current_packet_content_;
 }
 
+// static
+void QuicConnectionPeer::SetLastHeaderFormat(QuicConnection* connection,
+                                             PacketHeaderFormat format) {
+  connection->last_header_.form = format;
+}
+
 }  // namespace test
 }  // namespace quic
diff --git a/quic/test_tools/quic_connection_peer.h b/quic/test_tools/quic_connection_peer.h
index b82c1b3..ef7e630 100644
--- a/quic/test_tools/quic_connection_peer.h
+++ b/quic/test_tools/quic_connection_peer.h
@@ -138,6 +138,8 @@
   static bool SupportsReleaseTime(QuicConnection* connection);
   static QuicConnection::PacketContent GetCurrentPacketContent(
       QuicConnection* connection);
+  static void SetLastHeaderFormat(QuicConnection* connection,
+                                  PacketHeaderFormat format);
 };
 
 }  // namespace test