| 1 |
import binascii |
| 2 |
from unittest import TestCase |
| 3 |
|
| 4 |
from aioquic.buffer import Buffer, BufferReadError |
| 5 |
from aioquic.quic import packet |
| 6 |
from aioquic.quic.packet import ( |
| 7 |
QuicPacketType, |
| 8 |
QuicPreferredAddress, |
| 9 |
QuicProtocolVersion, |
| 10 |
QuicTransportParameters, |
| 11 |
QuicVersionInformation, |
| 12 |
decode_packet_number, |
| 13 |
encode_quic_retry, |
| 14 |
encode_quic_version_negotiation, |
| 15 |
get_retry_integrity_tag, |
| 16 |
pull_quic_header, |
| 17 |
pull_quic_preferred_address, |
| 18 |
pull_quic_transport_parameters, |
| 19 |
push_quic_preferred_address, |
| 20 |
push_quic_transport_parameters, |
| 21 |
) |
| 22 |
|
| 23 |
from .test_crypto_v1 import LONG_CLIENT_ENCRYPTED_PACKET as CLIENT_INITIAL_V1 |
| 24 |
from .test_crypto_v1 import LONG_SERVER_ENCRYPTED_PACKET as SERVER_INITIAL_V1 |
| 25 |
from .test_crypto_v2 import LONG_CLIENT_ENCRYPTED_PACKET as CLIENT_INITIAL_V2 |
| 26 |
from .test_crypto_v2 import LONG_SERVER_ENCRYPTED_PACKET as SERVER_INITIAL_V2 |
| 27 |
|
| 28 |
|
| 29 |
class PacketTest(TestCase): |
| 30 |
def test_decode_packet_number(self): |
| 31 |
|
| 32 |
for i in range(0, 256): |
| 33 |
self.assertEqual(decode_packet_number(i, 8, expected=0), i) |
| 34 |
|
| 35 |
|
| 36 |
self.assertEqual(decode_packet_number(0, 8, expected=128), 256) |
| 37 |
for i in range(1, 256): |
| 38 |
self.assertEqual(decode_packet_number(i, 8, expected=128), i) |
| 39 |
|
| 40 |
|
| 41 |
self.assertEqual(decode_packet_number(0, 8, expected=129), 256) |
| 42 |
self.assertEqual(decode_packet_number(1, 8, expected=129), 257) |
| 43 |
for i in range(2, 256): |
| 44 |
self.assertEqual(decode_packet_number(i, 8, expected=129), i) |
| 45 |
|
| 46 |
|
| 47 |
for i in range(0, 128): |
| 48 |
self.assertEqual(decode_packet_number(i, 8, expected=256), 256 + i) |
| 49 |
for i in range(129, 256): |
| 50 |
self.assertEqual(decode_packet_number(i, 8, expected=256), i) |
| 51 |
|
| 52 |
def test_pull_empty(self): |
| 53 |
buf = Buffer(data=b"") |
| 54 |
with self.assertRaises(BufferReadError): |
| 55 |
pull_quic_header(buf, host_cid_length=8) |
| 56 |
|
| 57 |
def test_pull_initial_client_v1(self): |
| 58 |
buf = Buffer(data=CLIENT_INITIAL_V1) |
| 59 |
header = pull_quic_header(buf, host_cid_length=8) |
| 60 |
self.assertEqual(header.version, QuicProtocolVersion.VERSION_1) |
| 61 |
self.assertEqual(header.packet_type, QuicPacketType.INITIAL) |
| 62 |
self.assertEqual(header.packet_length, 1200) |
| 63 |
self.assertEqual(header.destination_cid, binascii.unhexlify("8394c8f03e515708")) |
| 64 |
self.assertEqual(header.source_cid, b"") |
| 65 |
self.assertEqual(header.token, b"") |
| 66 |
self.assertEqual(header.integrity_tag, b"") |
| 67 |
self.assertEqual(buf.tell(), 18) |
| 68 |
|
| 69 |
def test_pull_initial_client_v1_truncated(self): |
| 70 |
buf = Buffer(data=CLIENT_INITIAL_V1[0:100]) |
| 71 |
with self.assertRaises(ValueError) as cm: |
| 72 |
pull_quic_header(buf, host_cid_length=8) |
| 73 |
self.assertEqual(str(cm.exception), "Packet payload is truncated") |
| 74 |
|
| 75 |
def test_pull_initial_client_v2(self): |
| 76 |
buf = Buffer(data=CLIENT_INITIAL_V2) |
| 77 |
header = pull_quic_header(buf, host_cid_length=8) |
| 78 |
self.assertEqual(header.version, QuicProtocolVersion.VERSION_2) |
| 79 |
self.assertEqual(header.packet_type, QuicPacketType.INITIAL) |
| 80 |
self.assertEqual(header.packet_length, 1200) |
| 81 |
self.assertEqual(header.destination_cid, binascii.unhexlify("8394c8f03e515708")) |
| 82 |
self.assertEqual(header.source_cid, b"") |
| 83 |
self.assertEqual(header.token, b"") |
| 84 |
self.assertEqual(header.integrity_tag, b"") |
| 85 |
self.assertEqual(buf.tell(), 18) |
| 86 |
|
| 87 |
def test_pull_initial_server_v1(self): |
| 88 |
buf = Buffer(data=SERVER_INITIAL_V1) |
| 89 |
header = pull_quic_header(buf, host_cid_length=8) |
| 90 |
self.assertEqual(header.version, QuicProtocolVersion.VERSION_1) |
| 91 |
self.assertEqual(header.packet_type, QuicPacketType.INITIAL) |
| 92 |
self.assertEqual(header.packet_length, 135) |
| 93 |
self.assertEqual(header.destination_cid, b"") |
| 94 |
self.assertEqual(header.source_cid, binascii.unhexlify("f067a5502a4262b5")) |
| 95 |
self.assertEqual(header.token, b"") |
| 96 |
self.assertEqual(header.integrity_tag, b"") |
| 97 |
self.assertEqual(buf.tell(), 18) |
| 98 |
|
| 99 |
def test_pull_initial_server_v2(self): |
| 100 |
buf = Buffer(data=SERVER_INITIAL_V2) |
| 101 |
header = pull_quic_header(buf, host_cid_length=8) |
| 102 |
self.assertEqual(header.version, QuicProtocolVersion.VERSION_2) |
| 103 |
self.assertEqual(header.packet_type, QuicPacketType.INITIAL) |
| 104 |
self.assertEqual(header.packet_length, 135) |
| 105 |
self.assertEqual(header.destination_cid, b"") |
| 106 |
self.assertEqual(header.source_cid, binascii.unhexlify("f067a5502a4262b5")) |
| 107 |
self.assertEqual(header.token, b"") |
| 108 |
self.assertEqual(header.integrity_tag, b"") |
| 109 |
self.assertEqual(buf.tell(), 18) |
| 110 |
|
| 111 |
def test_pull_retry_v1(self): |
| 112 |
|
| 113 |
original_destination_cid = binascii.unhexlify("8394c8f03e515708") |
| 114 |
|
| 115 |
data = binascii.unhexlify( |
| 116 |
"ff000000010008f067a5502a4262b5746f6b656e04a265ba2eff4d829058fb3f0f2496ba" |
| 117 |
) |
| 118 |
buf = Buffer(data=data) |
| 119 |
header = pull_quic_header(buf) |
| 120 |
self.assertEqual(header.version, QuicProtocolVersion.VERSION_1) |
| 121 |
self.assertEqual(header.packet_type, QuicPacketType.RETRY) |
| 122 |
self.assertEqual(header.packet_length, 36) |
| 123 |
self.assertEqual(header.destination_cid, b"") |
| 124 |
self.assertEqual(header.source_cid, binascii.unhexlify("f067a5502a4262b5")) |
| 125 |
self.assertEqual(header.token, b"token") |
| 126 |
self.assertEqual( |
| 127 |
header.integrity_tag, binascii.unhexlify("04a265ba2eff4d829058fb3f0f2496ba") |
| 128 |
) |
| 129 |
self.assertEqual(buf.tell(), 36) |
| 130 |
|
| 131 |
|
| 132 |
self.assertEqual( |
| 133 |
get_retry_integrity_tag( |
| 134 |
buf.data_slice(0, 20), original_destination_cid, version=header.version |
| 135 |
), |
| 136 |
header.integrity_tag, |
| 137 |
) |
| 138 |
|
| 139 |
|
| 140 |
encoded = encode_quic_retry( |
| 141 |
version=header.version, |
| 142 |
source_cid=header.source_cid, |
| 143 |
destination_cid=header.destination_cid, |
| 144 |
original_destination_cid=original_destination_cid, |
| 145 |
retry_token=header.token, |
| 146 |
|
| 147 |
unused=0xF, |
| 148 |
) |
| 149 |
self.assertEqual(encoded, data) |
| 150 |
|
| 151 |
def test_pull_retry_v2(self): |
| 152 |
|
| 153 |
original_destination_cid = binascii.unhexlify("8394c8f03e515708") |
| 154 |
|
| 155 |
data = binascii.unhexlify( |
| 156 |
"cf6b3343cf0008f067a5502a4262b5746f6b656ec8646ce8bfe33952d955543665dcc7b6" |
| 157 |
) |
| 158 |
buf = Buffer(data=data) |
| 159 |
header = pull_quic_header(buf) |
| 160 |
self.assertEqual(header.version, QuicProtocolVersion.VERSION_2) |
| 161 |
self.assertEqual(header.packet_type, QuicPacketType.RETRY) |
| 162 |
self.assertEqual(header.packet_length, 36) |
| 163 |
self.assertEqual(header.destination_cid, b"") |
| 164 |
self.assertEqual(header.source_cid, binascii.unhexlify("f067a5502a4262b5")) |
| 165 |
self.assertEqual(header.token, b"token") |
| 166 |
self.assertEqual( |
| 167 |
header.integrity_tag, binascii.unhexlify("c8646ce8bfe33952d955543665dcc7b6") |
| 168 |
) |
| 169 |
self.assertEqual(buf.tell(), 36) |
| 170 |
|
| 171 |
|
| 172 |
self.assertEqual( |
| 173 |
get_retry_integrity_tag( |
| 174 |
buf.data_slice(0, 20), original_destination_cid, version=header.version |
| 175 |
), |
| 176 |
header.integrity_tag, |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
encoded = encode_quic_retry( |
| 181 |
version=header.version, |
| 182 |
source_cid=header.source_cid, |
| 183 |
destination_cid=header.destination_cid, |
| 184 |
original_destination_cid=original_destination_cid, |
| 185 |
retry_token=header.token, |
| 186 |
|
| 187 |
unused=0xF, |
| 188 |
) |
| 189 |
self.assertEqual(encoded, data) |
| 190 |
|
| 191 |
def test_pull_version_negotiation(self): |
| 192 |
data = binascii.unhexlify( |
| 193 |
"ea00000000089aac5a49ba87a84908f92f4336fa951ba14547471600000001" |
| 194 |
) |
| 195 |
|
| 196 |
buf = Buffer(data=data) |
| 197 |
header = pull_quic_header(buf, host_cid_length=8) |
| 198 |
self.assertEqual(header.version, QuicProtocolVersion.NEGOTIATION) |
| 199 |
self.assertEqual(header.packet_type, QuicPacketType.VERSION_NEGOTIATION) |
| 200 |
self.assertEqual(header.packet_length, 31) |
| 201 |
self.assertEqual(header.destination_cid, binascii.unhexlify("9aac5a49ba87a849")) |
| 202 |
self.assertEqual(header.source_cid, binascii.unhexlify("f92f4336fa951ba1")) |
| 203 |
self.assertEqual(header.token, b"") |
| 204 |
self.assertEqual(header.integrity_tag, b"") |
| 205 |
self.assertEqual( |
| 206 |
header.supported_versions, [0x45474716, QuicProtocolVersion.VERSION_1] |
| 207 |
) |
| 208 |
self.assertEqual(buf.tell(), 31) |
| 209 |
|
| 210 |
encoded = encode_quic_version_negotiation( |
| 211 |
destination_cid=header.destination_cid, |
| 212 |
source_cid=header.source_cid, |
| 213 |
supported_versions=header.supported_versions, |
| 214 |
) |
| 215 |
|
| 216 |
|
| 217 |
self.assertEqual(encoded[1:], data[1:]) |
| 218 |
|
| 219 |
def test_pull_long_header_dcid_too_long(self): |
| 220 |
buf = Buffer( |
| 221 |
data=binascii.unhexlify( |
| 222 |
"c6ff0000161500000000000000000000000000000000000000000000004" |
| 223 |
"01c514f99ec4bbf1f7a30f9b0c94fef717f1c1d07fec24c99a864da7ede" |
| 224 |
) |
| 225 |
) |
| 226 |
with self.assertRaises(ValueError) as cm: |
| 227 |
pull_quic_header(buf, host_cid_length=8) |
| 228 |
self.assertEqual(str(cm.exception), "Destination CID is too long (21 bytes)") |
| 229 |
|
| 230 |
def test_pull_long_header_scid_too_long(self): |
| 231 |
buf = Buffer( |
| 232 |
data=binascii.unhexlify( |
| 233 |
"c2ff0000160015000000000000000000000000000000000000000000004" |
| 234 |
"01cfcee99ec4bbf1f7a30f9b0c9417b8c263cdd8cc972a4439d68a46320" |
| 235 |
) |
| 236 |
) |
| 237 |
with self.assertRaises(ValueError) as cm: |
| 238 |
pull_quic_header(buf, host_cid_length=8) |
| 239 |
self.assertEqual(str(cm.exception), "Source CID is too long (21 bytes)") |
| 240 |
|
| 241 |
def test_pull_long_header_no_fixed_bit(self): |
| 242 |
buf = Buffer(data=b"\x80\xff\x00\x00\x11\x00\x00") |
| 243 |
with self.assertRaises(ValueError) as cm: |
| 244 |
pull_quic_header(buf, host_cid_length=8) |
| 245 |
self.assertEqual(str(cm.exception), "Packet fixed bit is zero") |
| 246 |
|
| 247 |
def test_pull_long_header_too_short(self): |
| 248 |
buf = Buffer(data=b"\xc0\x00") |
| 249 |
with self.assertRaises(BufferReadError): |
| 250 |
pull_quic_header(buf, host_cid_length=8) |
| 251 |
|
| 252 |
def test_pull_short_header(self): |
| 253 |
buf = Buffer( |
| 254 |
data=binascii.unhexlify("5df45aa7b59c0e1ad6e668f5304cd4fd1fb3799327") |
| 255 |
) |
| 256 |
header = pull_quic_header(buf, host_cid_length=8) |
| 257 |
self.assertEqual(header.version, None) |
| 258 |
self.assertEqual(header.packet_type, QuicPacketType.ONE_RTT) |
| 259 |
self.assertEqual(header.packet_length, 21) |
| 260 |
self.assertEqual(header.destination_cid, binascii.unhexlify("f45aa7b59c0e1ad6")) |
| 261 |
self.assertEqual(header.source_cid, b"") |
| 262 |
self.assertEqual(header.token, b"") |
| 263 |
self.assertEqual(header.integrity_tag, b"") |
| 264 |
self.assertEqual(buf.tell(), 9) |
| 265 |
|
| 266 |
def test_pull_short_header_no_fixed_bit(self): |
| 267 |
buf = Buffer(data=b"\x00") |
| 268 |
with self.assertRaises(ValueError) as cm: |
| 269 |
pull_quic_header(buf, host_cid_length=8) |
| 270 |
self.assertEqual(str(cm.exception), "Packet fixed bit is zero") |
| 271 |
|
| 272 |
|
| 273 |
class ParamsTest(TestCase): |
| 274 |
maxDiff = None |
| 275 |
|
| 276 |
def test_params(self): |
| 277 |
data = binascii.unhexlify( |
| 278 |
"010267100210cc2fd6e7d97a53ab5be85b28d75c8008030247e404048005fff" |
| 279 |
"a05048000ffff06048000ffff0801060a01030b0119" |
| 280 |
) |
| 281 |
|
| 282 |
|
| 283 |
buf = Buffer(data=data) |
| 284 |
params = pull_quic_transport_parameters(buf) |
| 285 |
self.assertEqual( |
| 286 |
params, |
| 287 |
QuicTransportParameters( |
| 288 |
max_idle_timeout=10000, |
| 289 |
stateless_reset_token=b"\xcc/\xd6\xe7\xd9zS\xab[\xe8[(\xd7\\\x80\x08", |
| 290 |
max_udp_payload_size=2020, |
| 291 |
initial_max_data=393210, |
| 292 |
initial_max_stream_data_bidi_local=65535, |
| 293 |
initial_max_stream_data_bidi_remote=65535, |
| 294 |
initial_max_stream_data_uni=None, |
| 295 |
initial_max_streams_bidi=6, |
| 296 |
initial_max_streams_uni=None, |
| 297 |
ack_delay_exponent=3, |
| 298 |
max_ack_delay=25, |
| 299 |
), |
| 300 |
) |
| 301 |
|
| 302 |
|
| 303 |
buf = Buffer(capacity=len(data)) |
| 304 |
push_quic_transport_parameters(buf, params) |
| 305 |
self.assertEqual(len(buf.data), len(data)) |
| 306 |
|
| 307 |
def test_params_disable_active_migration(self): |
| 308 |
data = binascii.unhexlify("0c00") |
| 309 |
|
| 310 |
|
| 311 |
buf = Buffer(data=data) |
| 312 |
params = pull_quic_transport_parameters(buf) |
| 313 |
self.assertEqual(params, QuicTransportParameters(disable_active_migration=True)) |
| 314 |
|
| 315 |
|
| 316 |
buf = Buffer(capacity=len(data)) |
| 317 |
push_quic_transport_parameters(buf, params) |
| 318 |
self.assertEqual(buf.data, data) |
| 319 |
|
| 320 |
def test_params_max_ack_delay(self): |
| 321 |
data = binascii.unhexlify("0b010a") |
| 322 |
|
| 323 |
|
| 324 |
buf = Buffer(data=data) |
| 325 |
params = pull_quic_transport_parameters(buf) |
| 326 |
self.assertEqual(params, QuicTransportParameters(max_ack_delay=10)) |
| 327 |
|
| 328 |
|
| 329 |
buf = Buffer(capacity=len(data)) |
| 330 |
push_quic_transport_parameters(buf, params) |
| 331 |
self.assertEqual(buf.data, data) |
| 332 |
|
| 333 |
def test_params_max_ack_delay_length_mismatch(self): |
| 334 |
buf = Buffer(data=binascii.unhexlify("0b020a")) |
| 335 |
with self.assertRaises(ValueError) as cm: |
| 336 |
pull_quic_transport_parameters(buf) |
| 337 |
self.assertEqual(str(cm.exception), "Transport parameter length does not match") |
| 338 |
|
| 339 |
def test_params_preferred_address(self): |
| 340 |
data = binascii.unhexlify( |
| 341 |
"0d3b8ba27b8611532400890200000000f03c91fffe69a45411531262c4518d6" |
| 342 |
"3013f0c287ed3573efa9095603746b2e02d45480ba6643e5c6e7d48ecb4" |
| 343 |
) |
| 344 |
|
| 345 |
|
| 346 |
buf = Buffer(data=data) |
| 347 |
params = pull_quic_transport_parameters(buf) |
| 348 |
self.assertEqual( |
| 349 |
params, |
| 350 |
QuicTransportParameters( |
| 351 |
preferred_address=QuicPreferredAddress( |
| 352 |
ipv4_address=("139.162.123.134", 4435), |
| 353 |
ipv6_address=("2400:8902::f03c:91ff:fe69:a454", 4435), |
| 354 |
connection_id=b"b\xc4Q\x8dc\x01?\x0c(~\xd3W>\xfa\x90\x95`7", |
| 355 |
stateless_reset_token=b"F\xb2\xe0-EH\x0b\xa6d>\\n}H\xec\xb4", |
| 356 |
), |
| 357 |
), |
| 358 |
) |
| 359 |
|
| 360 |
|
| 361 |
buf = Buffer(capacity=len(data)) |
| 362 |
push_quic_transport_parameters(buf, params) |
| 363 |
self.assertEqual(buf.data, data) |
| 364 |
|
| 365 |
def test_params_unknown(self): |
| 366 |
data = binascii.unhexlify("8000ff000100") |
| 367 |
|
| 368 |
|
| 369 |
buf = Buffer(data=data) |
| 370 |
params = pull_quic_transport_parameters(buf) |
| 371 |
self.assertEqual(params, QuicTransportParameters()) |
| 372 |
|
| 373 |
def test_params_version_information(self): |
| 374 |
data = binascii.unhexlify("110c00000001000000016b3343cf") |
| 375 |
|
| 376 |
|
| 377 |
buf = Buffer(data=data) |
| 378 |
params = pull_quic_transport_parameters(buf) |
| 379 |
self.assertEqual( |
| 380 |
params, |
| 381 |
QuicTransportParameters( |
| 382 |
version_information=QuicVersionInformation( |
| 383 |
chosen_version=QuicProtocolVersion.VERSION_1, |
| 384 |
available_versions=[ |
| 385 |
QuicProtocolVersion.VERSION_1, |
| 386 |
QuicProtocolVersion.VERSION_2, |
| 387 |
], |
| 388 |
), |
| 389 |
), |
| 390 |
) |
| 391 |
|
| 392 |
|
| 393 |
buf = Buffer(capacity=len(data)) |
| 394 |
push_quic_transport_parameters(buf, params) |
| 395 |
self.assertEqual(buf.data, data) |
| 396 |
|
| 397 |
def test_params_version_information_available_version_0(self): |
| 398 |
buf = Buffer(data=binascii.unhexlify("11080000000100000000")) |
| 399 |
with self.assertRaises(ValueError) as cm: |
| 400 |
pull_quic_transport_parameters(buf) |
| 401 |
self.assertEqual( |
| 402 |
str(cm.exception), "Version Information must not contain version 0" |
| 403 |
) |
| 404 |
|
| 405 |
def test_params_version_information_chosen_version_0(self): |
| 406 |
buf = Buffer(data=binascii.unhexlify("110400000000")) |
| 407 |
with self.assertRaises(ValueError) as cm: |
| 408 |
pull_quic_transport_parameters(buf) |
| 409 |
self.assertEqual( |
| 410 |
str(cm.exception), "Version Information must not contain version 0" |
| 411 |
) |
| 412 |
|
| 413 |
def test_params_version_information_length_not_divisible_by_four(self): |
| 414 |
buf = Buffer(data=binascii.unhexlify("11050000000100")) |
| 415 |
with self.assertRaises(ValueError) as cm: |
| 416 |
pull_quic_transport_parameters(buf) |
| 417 |
self.assertEqual(str(cm.exception), "Transport parameter length does not match") |
| 418 |
|
| 419 |
def test_params_version_information_truncated(self): |
| 420 |
buf = Buffer(data=binascii.unhexlify("110800000000")) |
| 421 |
with self.assertRaises(ValueError) as cm: |
| 422 |
pull_quic_transport_parameters(buf) |
| 423 |
self.assertEqual(str(cm.exception), "Read out of bounds") |
| 424 |
|
| 425 |
def test_preferred_address_ipv4_only(self): |
| 426 |
data = binascii.unhexlify( |
| 427 |
"8ba27b8611530000000000000000000000000000000000001262c4518d63013" |
| 428 |
"f0c287ed3573efa9095603746b2e02d45480ba6643e5c6e7d48ecb4" |
| 429 |
) |
| 430 |
|
| 431 |
|
| 432 |
buf = Buffer(data=data) |
| 433 |
preferred_address = pull_quic_preferred_address(buf) |
| 434 |
self.assertEqual( |
| 435 |
preferred_address, |
| 436 |
QuicPreferredAddress( |
| 437 |
ipv4_address=("139.162.123.134", 4435), |
| 438 |
ipv6_address=None, |
| 439 |
connection_id=b"b\xc4Q\x8dc\x01?\x0c(~\xd3W>\xfa\x90\x95`7", |
| 440 |
stateless_reset_token=b"F\xb2\xe0-EH\x0b\xa6d>\\n}H\xec\xb4", |
| 441 |
), |
| 442 |
) |
| 443 |
|
| 444 |
|
| 445 |
buf = Buffer(capacity=len(data)) |
| 446 |
push_quic_preferred_address(buf, preferred_address) |
| 447 |
self.assertEqual(buf.data, data) |
| 448 |
|
| 449 |
def test_preferred_address_ipv6_only(self): |
| 450 |
data = binascii.unhexlify( |
| 451 |
"0000000000002400890200000000f03c91fffe69a45411531262c4518d63013" |
| 452 |
"f0c287ed3573efa9095603746b2e02d45480ba6643e5c6e7d48ecb4" |
| 453 |
) |
| 454 |
|
| 455 |
|
| 456 |
buf = Buffer(data=data) |
| 457 |
preferred_address = pull_quic_preferred_address(buf) |
| 458 |
self.assertEqual( |
| 459 |
preferred_address, |
| 460 |
QuicPreferredAddress( |
| 461 |
ipv4_address=None, |
| 462 |
ipv6_address=("2400:8902::f03c:91ff:fe69:a454", 4435), |
| 463 |
connection_id=b"b\xc4Q\x8dc\x01?\x0c(~\xd3W>\xfa\x90\x95`7", |
| 464 |
stateless_reset_token=b"F\xb2\xe0-EH\x0b\xa6d>\\n}H\xec\xb4", |
| 465 |
), |
| 466 |
) |
| 467 |
|
| 468 |
|
| 469 |
buf = Buffer(capacity=len(data)) |
| 470 |
push_quic_preferred_address(buf, preferred_address) |
| 471 |
self.assertEqual(buf.data, data) |
| 472 |
|
| 473 |
|
| 474 |
class FrameTest(TestCase): |
| 475 |
def test_ack_frame(self): |
| 476 |
data = b"\x00\x02\x00\x00" |
| 477 |
|
| 478 |
|
| 479 |
buf = Buffer(data=data) |
| 480 |
rangeset, delay = packet.pull_ack_frame(buf) |
| 481 |
self.assertEqual(list(rangeset), [range(0, 1)]) |
| 482 |
self.assertEqual(delay, 2) |
| 483 |
|
| 484 |
|
| 485 |
buf = Buffer(capacity=len(data)) |
| 486 |
packet.push_ack_frame(buf, rangeset, delay) |
| 487 |
self.assertEqual(buf.data, data) |
| 488 |
|
| 489 |
def test_ack_frame_with_one_range(self): |
| 490 |
data = b"\x02\x02\x01\x00\x00\x00" |
| 491 |
|
| 492 |
|
| 493 |
buf = Buffer(data=data) |
| 494 |
rangeset, delay = packet.pull_ack_frame(buf) |
| 495 |
self.assertEqual(list(rangeset), [range(0, 1), range(2, 3)]) |
| 496 |
self.assertEqual(delay, 2) |
| 497 |
|
| 498 |
|
| 499 |
buf = Buffer(capacity=len(data)) |
| 500 |
packet.push_ack_frame(buf, rangeset, delay) |
| 501 |
self.assertEqual(buf.data, data) |
| 502 |
|
| 503 |
def test_ack_frame_with_one_range_2(self): |
| 504 |
data = b"\x05\x02\x01\x00\x00\x03" |
| 505 |
|
| 506 |
|
| 507 |
buf = Buffer(data=data) |
| 508 |
rangeset, delay = packet.pull_ack_frame(buf) |
| 509 |
self.assertEqual(list(rangeset), [range(0, 4), range(5, 6)]) |
| 510 |
self.assertEqual(delay, 2) |
| 511 |
|
| 512 |
|
| 513 |
buf = Buffer(capacity=len(data)) |
| 514 |
packet.push_ack_frame(buf, rangeset, delay) |
| 515 |
self.assertEqual(buf.data, data) |
| 516 |
|
| 517 |
def test_ack_frame_with_one_range_3(self): |
| 518 |
data = b"\x05\x02\x01\x00\x01\x02" |
| 519 |
|
| 520 |
|
| 521 |
buf = Buffer(data=data) |
| 522 |
rangeset, delay = packet.pull_ack_frame(buf) |
| 523 |
self.assertEqual(list(rangeset), [range(0, 3), range(5, 6)]) |
| 524 |
self.assertEqual(delay, 2) |
| 525 |
|
| 526 |
|
| 527 |
buf = Buffer(capacity=len(data)) |
| 528 |
packet.push_ack_frame(buf, rangeset, delay) |
| 529 |
self.assertEqual(buf.data, data) |
| 530 |
|
| 531 |
def test_ack_frame_with_two_ranges(self): |
| 532 |
data = b"\x04\x02\x02\x00\x00\x00\x00\x00" |
| 533 |
|
| 534 |
|
| 535 |
buf = Buffer(data=data) |
| 536 |
rangeset, delay = packet.pull_ack_frame(buf) |
| 537 |
self.assertEqual(list(rangeset), [range(0, 1), range(2, 3), range(4, 5)]) |
| 538 |
self.assertEqual(delay, 2) |
| 539 |
|
| 540 |
|
| 541 |
buf = Buffer(capacity=len(data)) |
| 542 |
packet.push_ack_frame(buf, rangeset, delay) |
| 543 |
self.assertEqual(buf.data, data) |