示例#1
0
def create_builder():
    return QuicPacketBuilder(
        host_cid=bytes(8),
        packet_number=0,
        peer_cid=bytes(8),
        peer_token=b"",
        spin_bit=False,
        version=QuicProtocolVersion.DRAFT_20,
    )
示例#2
0
    def test_receive_datagram_wrong_version(self):
        client = create_standalone_client(self)

        builder = QuicPacketBuilder(
            host_cid=client._peer_cid,
            peer_cid=client.host_cid,
            version=0xFF000011,  # DRAFT_16
        )
        crypto = CryptoPair()
        crypto.setup_initial(client.host_cid, is_client=False)
        builder.start_packet(PACKET_TYPE_INITIAL, crypto)
        builder.buffer.push_bytes(bytes(1200))
        builder.end_packet()

        for datagram in builder.flush()[0]:
            client.receive_datagram(datagram, SERVER_ADDR, now=time.time())
        self.assertEqual(drop(client), 0)
示例#3
0
    def test_short_header_padding(self):
        builder = QuicPacketBuilder(
            host_cid=bytes(8),
            packet_number=0,
            peer_cid=bytes(8),
            peer_token=b"",
            spin_bit=False,
            version=QuicProtocolVersion.DRAFT_20,
        )
        crypto = CryptoPair()
        crypto.setup_initial(bytes(8), is_client=True)

        # ONE_RTT, fully padded
        builder.start_packet(PACKET_TYPE_ONE_RTT, crypto)
        self.assertEqual(builder.remaining_space, 1253)
        builder.start_frame(QuicFrameType.CRYPTO)
        builder.buffer.push_bytes(bytes(builder.remaining_space))
        self.assertTrue(builder.end_packet())

        # check builder
        self.assertEqual(builder.buffer.tell(), 0)
        self.assertEqual(builder.packet_number, 1)

        # check datagrams
        datagrams, packets = builder.flush()
        self.assertEqual(len(datagrams), 1)
        self.assertEqual(len(datagrams[0]), 1280)
        self.assertEqual(
            packets,
            [
                QuicSentPacket(
                    epoch=Epoch.ONE_RTT,
                    in_flight=True,
                    is_ack_eliciting=True,
                    is_crypto_packet=True,
                    packet_number=0,
                    sent_bytes=1280,
                )
            ],
        )
示例#4
0
    def test_datagram_received_wrong_version(self):
        client, client_transport = create_standalone_client()
        self.assertEqual(client_transport.sent, 1)

        builder = QuicPacketBuilder(
            host_cid=client.peer_cid,
            peer_cid=client.host_cid,
            version=0xFF000011,  # DRAFT_16
        )
        crypto = CryptoPair()
        crypto.setup_initial(client.host_cid, is_client=False)
        builder.start_packet(PACKET_TYPE_INITIAL, crypto)
        push_bytes(builder.buffer, bytes(1200))
        builder.end_packet()

        for datagram in builder.flush()[0]:
            client.datagram_received(datagram, SERVER_ADDR)
        self.assertEqual(client_transport.sent, 1)
    def test_long_header_empty(self):
        builder = QuicPacketBuilder(
            host_cid=bytes(8),
            packet_number=0,
            peer_cid=bytes(8),
            peer_token=b"",
            spin_bit=False,
            version=QuicProtocolVersion.DRAFT_20,
        )
        crypto = CryptoPair()

        builder.start_packet(PACKET_TYPE_INITIAL, crypto)
        self.assertEqual(builder.remaining_space, 1237)

        # nothing to write

        self.assertFalse(builder.end_packet())
        self.assertEqual(builder.buffer.tell(), 0)
        self.assertEqual(builder.packet_number, 0)

        datagrams, packets = builder.flush()
        self.assertEqual(len(datagrams), 0)
        self.assertEqual(packets, [])
    def test_long_header_then_long_header(self):
        builder = QuicPacketBuilder(
            host_cid=bytes(8),
            packet_number=0,
            peer_cid=bytes(8),
            peer_token=b"",
            spin_bit=False,
            version=QuicProtocolVersion.DRAFT_20,
        )
        crypto = CryptoPair()
        crypto.setup_initial(bytes(8), is_client=True)

        # INITIAL
        builder.start_packet(PACKET_TYPE_INITIAL, crypto)
        self.assertEqual(builder.remaining_space, 1237)
        builder.start_frame(QuicFrameType.CRYPTO)
        push_bytes(builder.buffer, bytes(199))
        self.assertEqual(builder.buffer.tell(), 227)
        self.assertTrue(builder.end_packet())
        self.assertEqual(builder.buffer.tell(), 243)

        # HANDSHAKE
        builder.start_packet(PACKET_TYPE_HANDSHAKE, crypto)
        self.assertEqual(builder.buffer.tell(), 269)
        self.assertEqual(builder.remaining_space, 995)
        builder.start_frame(QuicFrameType.CRYPTO)
        push_bytes(builder.buffer, bytes(299))
        self.assertEqual(builder.buffer.tell(), 569)
        self.assertTrue(builder.end_packet())
        self.assertEqual(builder.buffer.tell(), 585)

        # ONE_RTT
        builder.start_packet(PACKET_TYPE_ONE_RTT, crypto)
        self.assertEqual(builder.remaining_space, 668)
        builder.start_frame(QuicFrameType.CRYPTO)
        push_bytes(builder.buffer, bytes(299))
        self.assertTrue(builder.end_packet())
        self.assertEqual(builder.buffer.tell(), 0)

        # check datagrams
        datagrams, packets = builder.flush()
        self.assertEqual(len(datagrams), 1)
        self.assertEqual(len(datagrams[0]), 912)
        self.assertEqual(
            packets,
            [
                QuicSentPacket(
                    epoch=Epoch.INITIAL,
                    is_ack_eliciting=True,
                    is_crypto_packet=True,
                    packet_number=0,
                    sent_bytes=243,
                ),
                QuicSentPacket(
                    epoch=Epoch.HANDSHAKE,
                    is_ack_eliciting=True,
                    is_crypto_packet=True,
                    packet_number=1,
                    sent_bytes=342,
                ),
                QuicSentPacket(
                    epoch=Epoch.ONE_RTT,
                    is_ack_eliciting=True,
                    is_crypto_packet=True,
                    packet_number=2,
                    sent_bytes=327,
                ),
            ],
        )