src/net/quic/quic_network_transaction_unittest.cc
// Test that two requests to the same origin over QUIC tunnels use different
// QUIC sessions if their NetworkIsolationKeys don't match, and
// kPartitionConnectionsByNetworkIsolationKey is enabled.
Test that two requests to the same origin over QUIC tunnels use different
QUIC sessions if their NetworkIsolationKeys don't match, and
kPartitionConnectionsByNetworkIsolationKey is enabled.
Test that two requests to the same origin over QUIC tunnels use different QUIC sessions if their NetworkIsolationKeys don ' t match and kPartitionConnectionsByNetworkIsolationKey is enabled .
TEST_P(QuicNetworkTransactionTest, NetworkIsolationTunnel) {
  base::test::ScopedFeatureList feature_list;
  feature_list.InitAndEnableFeature(
      features::kPartitionConnectionsByNetworkIsolationKey);
  session_params_.enable_quic = true;
  session_params_.enable_quic_proxies_for_https_urls = true;
  proxy_resolution_service_ =
      ConfiguredProxyResolutionService::CreateFixedFromPacResult(
          "QUIC proxy.example.org:70", TRAFFIC_ANNOTATION_FOR_TESTS);
  const char kGetRequest[] =
      "GET / HTTP/1.1\r\n"
      "Host: mail.example.org\r\n"
      "Connection: keep-alive\r\n\r\n";
  const char kGetResponse[] =
      "HTTP/1.1 200 OK\r\n"
      "Content-Length: 10\r\n\r\n";
  std::unique_ptr<MockQuicData> mock_quic_data[2] = {
      std::make_unique<MockQuicData>(version_),
      std::make_unique<MockQuicData>(version_)};
  for (int index : {0, 1}) {
    QuicTestPacketMaker client_maker(
        version_,
        quic::QuicUtils::CreateRandomConnectionId(context_.random_generator()),
        context_.clock(), kDefaultServerHostName, quic::Perspective::IS_CLIENT,
        client_headers_include_h2_stream_dependency_);
    QuicTestPacketMaker server_maker(
        version_,
        quic::QuicUtils::CreateRandomConnectionId(context_.random_generator()),
        context_.clock(), kDefaultServerHostName, quic::Perspective::IS_SERVER,
        false);
    int packet_num = 1;
    if (VersionUsesHttp3(version_.transport_version)) {
      mock_quic_data[index]->AddWrite(
          SYNCHRONOUS, client_maker.MakeInitialSettingsPacket(packet_num++));
      mock_quic_data[index]->AddWrite(
          SYNCHRONOUS, client_maker.MakePriorityPacket(
                           packet_num++, true,
                           GetNthClientInitiatedBidirectionalStreamId(0), 0,
                           ConvertRequestPriorityToQuicPriority(
                               HttpProxyConnectJob::kH2QuicTunnelPriority)));
    }
    mock_quic_data[index]->AddWrite(
        SYNCHRONOUS,
        client_maker.MakeRequestHeadersPacket(
            packet_num++, GetNthClientInitiatedBidirectionalStreamId(0), true,
            false,
            VersionUsesHttp3(version_.transport_version)
                ? 1
                : ConvertRequestPriorityToQuicPriority(
                      HttpProxyConnectJob::kH2QuicTunnelPriority),
            ConnectRequestHeaders("mail.example.org:443"), 0, nullptr));
    mock_quic_data[index]->AddRead(
        ASYNC, server_maker.MakeResponseHeadersPacket(
                   1, GetNthClientInitiatedBidirectionalStreamId(0), false,
                   false, GetResponseHeaders("200 OK"), nullptr));
    std::string header = ConstructDataHeader(strlen(kGetRequest));
    if (!version_.HasIetfQuicFrames()) {
      mock_quic_data[index]->AddWrite(
          SYNCHRONOUS, client_maker.MakeAckAndDataPacket(
                           packet_num++, false,
                           GetNthClientInitiatedBidirectionalStreamId(0), 1, 1,
                           1, false, quiche::QuicheStringPiece(kGetRequest)));
    } else {
      mock_quic_data[index]->AddWrite(
          SYNCHRONOUS, client_maker.MakeAckAndDataPacket(
                           packet_num++, false,
                           GetNthClientInitiatedBidirectionalStreamId(0), 1, 1,
                           1, false, {header + std::string(kGetRequest)}));
    }
    std::string header2 = ConstructDataHeader(strlen(kGetResponse));
    mock_quic_data[index]->AddRead(
        ASYNC, server_maker.MakeDataPacket(
                   2, GetNthClientInitiatedBidirectionalStreamId(0), false,
                   false, header2 + std::string(kGetResponse)));
    mock_quic_data[index]->AddRead(
        SYNCHRONOUS,
        server_maker.MakeDataPacket(
            3, GetNthClientInitiatedBidirectionalStreamId(0), false, false,
            ConstructDataHeader(10) + std::string("0123456789")));
    mock_quic_data[index]->AddWrite(
        SYNCHRONOUS, client_maker.MakeAckPacket(packet_num++, 3, 2, 1, true));
    mock_quic_data[index]->AddRead(SYNCHRONOUS,
                                   ERR_IO_PENDING);  
    mock_quic_data[index]->AddSocketDataToFactory(&socket_factory_);
  }
  socket_factory_.AddSSLSocketDataProvider(&ssl_data_);
  SSLSocketDataProvider ssl_data2(ASYNC, OK);
  socket_factory_.AddSSLSocketDataProvider(&ssl_data2);
  CreateSession();
  request_.url = GURL("https://mail.example.org/");
  AddQuicAlternateProtocolMapping(MockCryptoClientStream::CONFIRM_HANDSHAKE);
  HttpNetworkTransaction trans(DEFAULT_PRIORITY, session_.get());
  RunTransaction(&trans);
  CheckResponseData(&trans, "0123456789");
  HttpRequestInfo request2;
  const auto kOrigin1 = url::Origin::Create(GURL("http://origin1/"));
  request_.network_isolation_key = NetworkIsolationKey(kOrigin1, kOrigin1);
  HttpNetworkTransaction trans2(DEFAULT_PRIORITY, session_.get());
  RunTransaction(&trans2);
  CheckResponseData(&trans2, "0123456789");
  EXPECT_TRUE(mock_quic_data[0]->AllReadDataConsumed());
  EXPECT_TRUE(mock_quic_data[0]->AllWriteDataConsumed());
  EXPECT_TRUE(mock_quic_data[1]->AllReadDataConsumed());
  EXPECT_TRUE(mock_quic_data[1]->AllWriteDataConsumed());
}
TEST_P ( QuicNetworkTransactionTest , NetworkIsolationTunnel ) { base :: test :: ScopedFeatureList feature_list ; feature_list . InitAndEnableFeature ( features :: kPartitionConnectionsByNetworkIsolationKey ) ; session_params_ . enable_quic = true ; session_params_ . enable_quic_proxies_for_https_urls = true ; proxy_resolution_service_ = ConfiguredProxyResolutionService :: CreateFixedFromPacResult ( " " , TRAFFIC_ANNOTATION_FOR_TESTS ) ; const char kGetRequest [ ] = " \r \n " " \r \n " " \r \n \r \n " ; const char kGetResponse [ ] = " \r \n " " \r \n \r \n " ; std :: unique_ptr < MockQuicData > mock_quic_data [ 2 ] = { std :: make_unique < MockQuicData > ( version_ ) , std :: make_unique < MockQuicData > ( version_ ) } ; for ( int index : { 0 , 1 } ) { QuicTestPacketMaker client_maker ( version_ , quic :: QuicUtils :: CreateRandomConnectionId ( context_ . random_generator ( ) ) , context_ . clock ( ) , kDefaultServerHostName , quic :: Perspective :: IS_CLIENT , client_headers_include_h2_stream_dependency_ ) ; QuicTestPacketMaker server_maker ( version_ , quic :: QuicUtils :: CreateRandomConnectionId ( context_ . random_generator ( ) ) , context_ . clock ( ) , kDefaultServerHostName , quic :: Perspective :: IS_SERVER , false ) ; int packet_num = 1 ; if ( VersionUsesHttp3 ( version_ . transport_version ) ) { mock_quic_data [ index ] -> AddWrite ( SYNCHRONOUS , client_maker . MakeInitialSettingsPacket ( packet_num ++ ) ) ; mock_quic_data [ index ] -> AddWrite ( SYNCHRONOUS , client_maker . MakePriorityPacket ( packet_num ++ , true , GetNthClientInitiatedBidirectionalStreamId ( 0 ) , 0 , ConvertRequestPriorityToQuicPriority ( HttpProxyConnectJob :: kH2QuicTunnelPriority ) ) ) ; } mock_quic_data [ index ] -> AddWrite ( SYNCHRONOUS , client_maker . MakeRequestHeadersPacket ( packet_num ++ , GetNthClientInitiatedBidirectionalStreamId ( 0 ) , true , false , VersionUsesHttp3 ( version_ . transport_version ) ? 1 : ConvertRequestPriorityToQuicPriority ( HttpProxyConnectJob :: kH2QuicTunnelPriority ) , ConnectRequestHeaders ( " " ) , 0 , nullptr ) ) ; mock_quic_data [ index ] -> AddRead ( ASYNC , server_maker . MakeResponseHeadersPacket ( 1 , GetNthClientInitiatedBidirectionalStreamId ( 0 ) , false , false , GetResponseHeaders ( " " ) , nullptr ) ) ; std :: string header = ConstructDataHeader ( strlen ( kGetRequest ) ) ; if ( ! version_ . HasIetfQuicFrames ( ) ) { mock_quic_data [ index ] -> AddWrite ( SYNCHRONOUS , client_maker . MakeAckAndDataPacket ( packet_num ++ , false , GetNthClientInitiatedBidirectionalStreamId ( 0 ) , 1 , 1 , 1 , false , quiche :: QuicheStringPiece ( kGetRequest ) ) ) ; } else { mock_quic_data [ index ] -> AddWrite ( SYNCHRONOUS , client_maker . MakeAckAndDataPacket ( packet_num ++ , false , GetNthClientInitiatedBidirectionalStreamId ( 0 ) , 1 , 1 , 1 , false , { header + std :: string ( kGetRequest ) } ) ) ; } std :: string header2 = ConstructDataHeader ( strlen ( kGetResponse ) ) ; mock_quic_data [ index ] -> AddRead ( ASYNC , server_maker . MakeDataPacket ( 2 , GetNthClientInitiatedBidirectionalStreamId ( 0 ) , false , false , header2 + std :: string ( kGetResponse ) ) ) ; mock_quic_data [ index ] -> AddRead ( SYNCHRONOUS , server_maker . MakeDataPacket ( 3 , GetNthClientInitiatedBidirectionalStreamId ( 0 ) , false , false , ConstructDataHeader ( 10 ) + std :: string ( " " ) ) ) ; mock_quic_data [ index ] -> AddWrite ( SYNCHRONOUS , client_maker . MakeAckPacket ( packet_num ++ , 3 , 2 , 1 , true ) ) ; mock_quic_data [ index ] -> AddRead ( SYNCHRONOUS , ERR_IO_PENDING ) ; mock_quic_data [ index ] -> AddSocketDataToFactory ( & socket_factory_ ) ; } socket_factory_ . AddSSLSocketDataProvider ( & ssl_data_ ) ; SSLSocketDataProvider ssl_data2 ( ASYNC , OK ) ; socket_factory_ . AddSSLSocketDataProvider ( & ssl_data2 ) ; CreateSession ( ) ; request_ . url = GURL ( " " ) ; AddQuicAlternateProtocolMapping ( MockCryptoClientStream :: CONFIRM_HANDSHAKE ) ; HttpNetworkTransaction trans ( DEFAULT_PRIORITY , session_ . get ( ) ) ; RunTransaction ( & trans ) ; CheckResponseData ( & trans , " " ) ; HttpRequestInfo request2 ; const auto kOrigin1 = url :: Origin :: Create ( GURL ( " " ) ) ; request_ . network_isolation_key = NetworkIsolationKey ( kOrigin1 , kOrigin1 ) ; HttpNetworkTransaction trans2 ( DEFAULT_PRIORITY , session_ . get ( ) ) ; RunTransaction ( & trans2 ) ; CheckResponseData ( & trans2 , " " ) ; EXPECT_TRUE ( mock_quic_data [ 0 ] -> AllReadDataConsumed ( ) ) ; EXPECT_TRUE ( mock_quic_data [ 0 ] -> AllWriteDataConsumed ( ) ) ; EXPECT_TRUE ( mock_quic_data [ 1 ] -> AllReadDataConsumed ( ) ) ; EXPECT_TRUE ( mock_quic_data [ 1 ] -> AllWriteDataConsumed ( ) ) ; }
Test that two requests to the same origin over QUIC tunnels use different
QUIC sessions if their NetworkIsolationKeys don't match, and
kPartitionConnectionsByNetworkIsolationKey is enabled.
Test that two requests to the same origin over QUIC tunnels use different QUIC sessions if their NetworkIsolationKeys don ' t match and kPartitionConnectionsByNetworkIsolationKey is enabled .
// No more data to read