• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2018-2019, Cloudflare, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright notice,
9 //       this list of conditions and the following disclaimer.
10 //
11 //     * Redistributions in binary form must reproduce the above copyright
12 //       notice, this list of conditions and the following disclaimer in the
13 //       documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 use crate::Error;
28 use crate::Result;
29 
30 use crate::octets;
31 use crate::packet;
32 use crate::ranges;
33 use crate::stream;
34 
35 pub const MAX_CRYPTO_OVERHEAD: usize = 8;
36 pub const MAX_DGRAM_OVERHEAD: usize = 2;
37 pub const MAX_STREAM_OVERHEAD: usize = 12;
38 pub const MAX_STREAM_SIZE: u64 = 1 << 62;
39 
40 #[derive(Clone, PartialEq)]
41 pub enum Frame {
42     Padding {
43         len: usize,
44     },
45 
46     Ping,
47 
48     ACK {
49         ack_delay: u64,
50         ranges: ranges::RangeSet,
51     },
52 
53     ResetStream {
54         stream_id: u64,
55         error_code: u64,
56         final_size: u64,
57     },
58 
59     StopSending {
60         stream_id: u64,
61         error_code: u64,
62     },
63 
64     Crypto {
65         data: stream::RangeBuf,
66     },
67 
68     NewToken {
69         token: Vec<u8>,
70     },
71 
72     Stream {
73         stream_id: u64,
74         data: stream::RangeBuf,
75     },
76 
77     MaxData {
78         max: u64,
79     },
80 
81     MaxStreamData {
82         stream_id: u64,
83         max: u64,
84     },
85 
86     MaxStreamsBidi {
87         max: u64,
88     },
89 
90     MaxStreamsUni {
91         max: u64,
92     },
93 
94     DataBlocked {
95         limit: u64,
96     },
97 
98     StreamDataBlocked {
99         stream_id: u64,
100         limit: u64,
101     },
102 
103     StreamsBlockedBidi {
104         limit: u64,
105     },
106 
107     StreamsBlockedUni {
108         limit: u64,
109     },
110 
111     NewConnectionId {
112         seq_num: u64,
113         retire_prior_to: u64,
114         conn_id: Vec<u8>,
115         reset_token: Vec<u8>,
116     },
117 
118     RetireConnectionId {
119         seq_num: u64,
120     },
121 
122     PathChallenge {
123         data: Vec<u8>,
124     },
125 
126     PathResponse {
127         data: Vec<u8>,
128     },
129 
130     ConnectionClose {
131         error_code: u64,
132         frame_type: u64,
133         reason: Vec<u8>,
134     },
135 
136     ApplicationClose {
137         error_code: u64,
138         reason: Vec<u8>,
139     },
140 
141     HandshakeDone,
142 
143     Datagram {
144         data: Vec<u8>,
145     },
146 }
147 
148 impl Frame {
from_bytes( b: &mut octets::Octets, pkt: packet::Type, ) -> Result<Frame>149     pub fn from_bytes(
150         b: &mut octets::Octets, pkt: packet::Type,
151     ) -> Result<Frame> {
152         let frame_type = b.get_varint()?;
153 
154         // println!("GOT FRAME {:x}", frame_type);
155 
156         let frame = match frame_type {
157             0x00 => {
158                 let mut len = 1;
159 
160                 while b.peek_u8() == Ok(0x00) {
161                     b.get_u8()?;
162 
163                     len += 1;
164                 }
165 
166                 Frame::Padding { len }
167             },
168 
169             0x01 => Frame::Ping,
170 
171             0x02 => parse_ack_frame(frame_type, b)?,
172 
173             0x04 => Frame::ResetStream {
174                 stream_id: b.get_varint()?,
175                 error_code: b.get_varint()?,
176                 final_size: b.get_varint()?,
177             },
178 
179             0x05 => Frame::StopSending {
180                 stream_id: b.get_varint()?,
181                 error_code: b.get_varint()?,
182             },
183 
184             0x06 => {
185                 let offset = b.get_varint()?;
186                 let data = b.get_bytes_with_varint_length()?;
187                 let data = stream::RangeBuf::from(data.as_ref(), offset, false);
188 
189                 Frame::Crypto { data }
190             },
191 
192             0x07 => Frame::NewToken {
193                 token: b.get_bytes_with_varint_length()?.to_vec(),
194             },
195 
196             0x08..=0x0f => parse_stream_frame(frame_type, b)?,
197 
198             0x10 => Frame::MaxData {
199                 max: b.get_varint()?,
200             },
201 
202             0x11 => Frame::MaxStreamData {
203                 stream_id: b.get_varint()?,
204                 max: b.get_varint()?,
205             },
206 
207             0x12 => Frame::MaxStreamsBidi {
208                 max: b.get_varint()?,
209             },
210 
211             0x13 => Frame::MaxStreamsUni {
212                 max: b.get_varint()?,
213             },
214 
215             0x14 => Frame::DataBlocked {
216                 limit: b.get_varint()?,
217             },
218 
219             0x15 => Frame::StreamDataBlocked {
220                 stream_id: b.get_varint()?,
221                 limit: b.get_varint()?,
222             },
223 
224             0x16 => Frame::StreamsBlockedBidi {
225                 limit: b.get_varint()?,
226             },
227 
228             0x17 => Frame::StreamsBlockedUni {
229                 limit: b.get_varint()?,
230             },
231 
232             0x18 => Frame::NewConnectionId {
233                 seq_num: b.get_varint()?,
234                 retire_prior_to: b.get_varint()?,
235                 conn_id: b.get_bytes_with_u8_length()?.to_vec(),
236                 reset_token: b.get_bytes(16)?.to_vec(),
237             },
238 
239             0x19 => Frame::RetireConnectionId {
240                 seq_num: b.get_varint()?,
241             },
242 
243             0x1a => Frame::PathChallenge {
244                 data: b.get_bytes(8)?.to_vec(),
245             },
246 
247             0x1b => Frame::PathResponse {
248                 data: b.get_bytes(8)?.to_vec(),
249             },
250 
251             0x1c => Frame::ConnectionClose {
252                 error_code: b.get_varint()?,
253                 frame_type: b.get_varint()?,
254                 reason: b.get_bytes_with_varint_length()?.to_vec(),
255             },
256 
257             0x1d => Frame::ApplicationClose {
258                 error_code: b.get_varint()?,
259                 reason: b.get_bytes_with_varint_length()?.to_vec(),
260             },
261 
262             0x1e => Frame::HandshakeDone,
263 
264             0x30 | 0x31 => parse_datagram_frame(frame_type, b)?,
265 
266             _ => return Err(Error::InvalidFrame),
267         };
268 
269         let allowed = match (pkt, &frame) {
270             // PADDING and PING are allowed on all packet types.
271             (_, Frame::Padding { .. }) | (_, Frame::Ping { .. }) => true,
272 
273             // ACK, CRYPTO, HANDSHAKE_DONE, NEW_TOKEN, PATH_RESPONSE, and
274             // RETIRE_CONNECTION_ID can't be sent on 0-RTT packets.
275             (packet::Type::ZeroRTT, Frame::ACK { .. }) => false,
276             (packet::Type::ZeroRTT, Frame::Crypto { .. }) => false,
277             (packet::Type::ZeroRTT, Frame::HandshakeDone) => false,
278             (packet::Type::ZeroRTT, Frame::NewToken { .. }) => false,
279             (packet::Type::ZeroRTT, Frame::PathResponse { .. }) => false,
280             (packet::Type::ZeroRTT, Frame::RetireConnectionId { .. }) => false,
281             (packet::Type::ZeroRTT, Frame::ConnectionClose { .. }) => false,
282 
283             // ACK, CRYPTO and CONNECTION_CLOSE can be sent on all other packet
284             // types.
285             (_, Frame::ACK { .. }) => true,
286             (_, Frame::Crypto { .. }) => true,
287             (_, Frame::ConnectionClose { .. }) => true,
288 
289             // All frames are allowed on 0-RTT and 1-RTT packets.
290             (packet::Type::Short, _) => true,
291             (packet::Type::ZeroRTT, _) => true,
292 
293             // All other cases are forbidden.
294             (..) => false,
295         };
296 
297         if !allowed {
298             return Err(Error::InvalidPacket);
299         }
300 
301         Ok(frame)
302     }
303 
to_bytes(&self, b: &mut octets::OctetsMut) -> Result<usize>304     pub fn to_bytes(&self, b: &mut octets::OctetsMut) -> Result<usize> {
305         let before = b.cap();
306 
307         match self {
308             Frame::Padding { len } => {
309                 let mut left = *len;
310 
311                 while left > 0 {
312                     b.put_varint(0x00)?;
313 
314                     left -= 1;
315                 }
316             },
317 
318             Frame::Ping => {
319                 b.put_varint(0x01)?;
320             },
321 
322             Frame::ACK { ack_delay, ranges } => {
323                 b.put_varint(0x02)?;
324 
325                 let mut it = ranges.iter().rev();
326 
327                 let first = it.next().unwrap();
328                 let ack_block = (first.end - 1) - first.start;
329 
330                 b.put_varint(first.end - 1)?;
331                 b.put_varint(*ack_delay)?;
332                 b.put_varint(it.len() as u64)?;
333                 b.put_varint(ack_block)?;
334 
335                 let mut smallest_ack = first.start;
336 
337                 for block in it {
338                     let gap = smallest_ack - block.end - 1;
339                     let ack_block = (block.end - 1) - block.start;
340 
341                     b.put_varint(gap)?;
342                     b.put_varint(ack_block)?;
343 
344                     smallest_ack = block.start;
345                 }
346             },
347 
348             Frame::ResetStream {
349                 stream_id,
350                 error_code,
351                 final_size,
352             } => {
353                 b.put_varint(0x04)?;
354 
355                 b.put_varint(*stream_id)?;
356                 b.put_varint(*error_code)?;
357                 b.put_varint(*final_size)?;
358             },
359 
360             Frame::StopSending {
361                 stream_id,
362                 error_code,
363             } => {
364                 b.put_varint(0x05)?;
365 
366                 b.put_varint(*stream_id)?;
367                 b.put_varint(*error_code)?;
368             },
369 
370             Frame::Crypto { data } => {
371                 b.put_varint(0x06)?;
372 
373                 b.put_varint(data.off() as u64)?;
374                 b.put_varint(data.len() as u64)?;
375                 b.put_bytes(&data)?;
376             },
377 
378             Frame::NewToken { token } => {
379                 b.put_varint(0x07)?;
380 
381                 b.put_varint(token.len() as u64)?;
382                 b.put_bytes(&token)?;
383             },
384 
385             Frame::Stream { stream_id, data } => {
386                 let mut ty: u8 = 0x08;
387 
388                 // Always encode offset
389                 ty |= 0x04;
390 
391                 // Always encode length
392                 ty |= 0x02;
393 
394                 if data.fin() {
395                     ty |= 0x01;
396                 }
397 
398                 b.put_varint(u64::from(ty))?;
399 
400                 b.put_varint(*stream_id)?;
401                 b.put_varint(data.off() as u64)?;
402                 b.put_varint(data.len() as u64)?;
403                 b.put_bytes(data.as_ref())?;
404             },
405 
406             Frame::MaxData { max } => {
407                 b.put_varint(0x10)?;
408 
409                 b.put_varint(*max)?;
410             },
411 
412             Frame::MaxStreamData { stream_id, max } => {
413                 b.put_varint(0x11)?;
414 
415                 b.put_varint(*stream_id)?;
416                 b.put_varint(*max)?;
417             },
418 
419             Frame::MaxStreamsBidi { max } => {
420                 b.put_varint(0x12)?;
421 
422                 b.put_varint(*max)?;
423             },
424 
425             Frame::MaxStreamsUni { max } => {
426                 b.put_varint(0x13)?;
427 
428                 b.put_varint(*max)?;
429             },
430 
431             Frame::DataBlocked { limit } => {
432                 b.put_varint(0x14)?;
433 
434                 b.put_varint(*limit)?;
435             },
436 
437             Frame::StreamDataBlocked { stream_id, limit } => {
438                 b.put_varint(0x15)?;
439 
440                 b.put_varint(*stream_id)?;
441                 b.put_varint(*limit)?;
442             },
443 
444             Frame::StreamsBlockedBidi { limit } => {
445                 b.put_varint(0x16)?;
446 
447                 b.put_varint(*limit)?;
448             },
449 
450             Frame::StreamsBlockedUni { limit } => {
451                 b.put_varint(0x17)?;
452 
453                 b.put_varint(*limit)?;
454             },
455 
456             Frame::NewConnectionId {
457                 seq_num,
458                 retire_prior_to,
459                 conn_id,
460                 reset_token,
461             } => {
462                 b.put_varint(0x18)?;
463 
464                 b.put_varint(*seq_num)?;
465                 b.put_varint(*retire_prior_to)?;
466                 b.put_u8(conn_id.len() as u8)?;
467                 b.put_bytes(conn_id.as_ref())?;
468                 b.put_bytes(reset_token.as_ref())?;
469             },
470 
471             Frame::RetireConnectionId { seq_num } => {
472                 b.put_varint(0x19)?;
473 
474                 b.put_varint(*seq_num)?;
475             },
476 
477             Frame::PathChallenge { data } => {
478                 b.put_varint(0x1a)?;
479 
480                 b.put_bytes(data.as_ref())?;
481             },
482 
483             Frame::PathResponse { data } => {
484                 b.put_varint(0x1b)?;
485 
486                 b.put_bytes(data.as_ref())?;
487             },
488 
489             Frame::ConnectionClose {
490                 error_code,
491                 frame_type,
492                 reason,
493             } => {
494                 b.put_varint(0x1c)?;
495 
496                 b.put_varint(*error_code)?;
497                 b.put_varint(*frame_type)?;
498                 b.put_varint(reason.len() as u64)?;
499                 b.put_bytes(reason.as_ref())?;
500             },
501 
502             Frame::ApplicationClose { error_code, reason } => {
503                 b.put_varint(0x1d)?;
504 
505                 b.put_varint(*error_code)?;
506                 b.put_varint(reason.len() as u64)?;
507                 b.put_bytes(reason.as_ref())?;
508             },
509 
510             Frame::HandshakeDone => {
511                 b.put_varint(0x1e)?;
512             },
513 
514             Frame::Datagram { data } => {
515                 let mut ty: u8 = 0x30;
516 
517                 // Always encode length
518                 ty |= 0x01;
519 
520                 b.put_varint(u64::from(ty))?;
521 
522                 b.put_varint(data.len() as u64)?;
523                 b.put_bytes(data.as_ref())?;
524             },
525         }
526 
527         Ok(before - b.cap())
528     }
529 
wire_len(&self) -> usize530     pub fn wire_len(&self) -> usize {
531         match self {
532             Frame::Padding { len } => *len,
533 
534             Frame::Ping => 1,
535 
536             Frame::ACK { ack_delay, ranges } => {
537                 let mut it = ranges.iter().rev();
538 
539                 let first = it.next().unwrap();
540                 let ack_block = (first.end - 1) - first.start;
541 
542                 let mut len = 1 + // frame type
543                     octets::varint_len(first.end - 1) + // largest_ack
544                     octets::varint_len(*ack_delay) + // ack_delay
545                     octets::varint_len(it.len() as u64) + // block_count
546                     octets::varint_len(ack_block); // first_block
547 
548                 let mut smallest_ack = first.start;
549 
550                 for block in it {
551                     let gap = smallest_ack - block.end - 1;
552                     let ack_block = (block.end - 1) - block.start;
553 
554                     len += octets::varint_len(gap) + // gap
555                            octets::varint_len(ack_block); // ack_block
556 
557                     smallest_ack = block.start;
558                 }
559 
560                 len
561             },
562 
563             Frame::ResetStream {
564                 stream_id,
565                 error_code,
566                 final_size,
567             } => {
568                 1 + // frame type
569                 octets::varint_len(*stream_id) + // stream_id
570                 octets::varint_len(*error_code) + // error_code
571                 octets::varint_len(*final_size) // final_size
572             },
573 
574             Frame::StopSending {
575                 stream_id,
576                 error_code,
577             } => {
578                 1 + // frame type
579                 octets::varint_len(*stream_id) + // stream_id
580                 octets::varint_len(*error_code) // error_code
581             },
582 
583             Frame::Crypto { data } => {
584                 1 + // frame type
585                 octets::varint_len(data.off() as u64) + // offset
586                 octets::varint_len(data.len() as u64) + // length
587                 data.len() // data
588             },
589 
590             Frame::NewToken { token } => {
591                 1 + // frame type
592                 octets::varint_len(token.len() as u64) + // token length
593                 token.len() // token
594             },
595 
596             Frame::Stream { stream_id, data } => {
597                 1 + // frame type
598                 octets::varint_len(*stream_id) + // stream_id
599                 octets::varint_len(data.off() as u64) + // offset
600                 octets::varint_len(data.len() as u64) + // length
601                 data.len() // data
602             },
603 
604             Frame::MaxData { max } => {
605                 1 + // frame type
606                 octets::varint_len(*max) // max
607             },
608 
609             Frame::MaxStreamData { stream_id, max } => {
610                 1 + // frame type
611                 octets::varint_len(*stream_id) + // stream_id
612                 octets::varint_len(*max) // max
613             },
614 
615             Frame::MaxStreamsBidi { max } => {
616                 1 + // frame type
617                 octets::varint_len(*max) // max
618             },
619 
620             Frame::MaxStreamsUni { max } => {
621                 1 + // frame type
622                 octets::varint_len(*max) // max
623             },
624 
625             Frame::DataBlocked { limit } => {
626                 1 + // frame type
627                 octets::varint_len(*limit) // limit
628             },
629 
630             Frame::StreamDataBlocked { stream_id, limit } => {
631                 1 + // frame type
632                 octets::varint_len(*stream_id) + // stream_id
633                 octets::varint_len(*limit) // limit
634             },
635 
636             Frame::StreamsBlockedBidi { limit } => {
637                 1 + // frame type
638                 octets::varint_len(*limit) // limit
639             },
640 
641             Frame::StreamsBlockedUni { limit } => {
642                 1 + // frame type
643                 octets::varint_len(*limit) // limit
644             },
645 
646             Frame::NewConnectionId {
647                 seq_num,
648                 retire_prior_to,
649                 conn_id,
650                 reset_token,
651             } => {
652                 1 + // frame type
653                 octets::varint_len(*seq_num) + // seq_num
654                 octets::varint_len(*retire_prior_to) + // retire_prior_to
655                 1 + // conn_id length
656                 conn_id.len() + // conn_id
657                 reset_token.len() // reset_token
658             },
659 
660             Frame::RetireConnectionId { seq_num } => {
661                 1 + // frame type
662                 octets::varint_len(*seq_num) // seq_num
663             },
664 
665             Frame::PathChallenge { .. } => {
666                 1 + // frame type
667                 8 // data
668             },
669 
670             Frame::PathResponse { .. } => {
671                 1 + // frame type
672                 8 // data
673             },
674 
675             Frame::ConnectionClose {
676                 frame_type,
677                 error_code,
678                 reason,
679                 ..
680             } => {
681                 1 + // frame type
682                 octets::varint_len(*error_code) + // error_code
683                 octets::varint_len(*frame_type) + // frame_type
684                 octets::varint_len(reason.len() as u64) + // reason_len
685                 reason.len() // reason
686             },
687 
688             Frame::ApplicationClose { reason, error_code } => {
689                 1 + // frame type
690                 octets::varint_len(*error_code) + // error_code
691                 octets::varint_len(reason.len() as u64) + // reason_len
692                 reason.len() // reason
693             },
694 
695             Frame::HandshakeDone => {
696                 1 // frame type
697             },
698 
699             Frame::Datagram { data } => {
700                 1 + // frame type
701                 octets::varint_len(data.len() as u64) + // length
702                 data.len() // data
703             },
704         }
705     }
706 
ack_eliciting(&self) -> bool707     pub fn ack_eliciting(&self) -> bool {
708         // Any other frame is ack-eliciting (note the `!`).
709         !matches!(self, Frame::Padding { .. } |
710             Frame::ACK { .. } |
711             Frame::ApplicationClose { .. } |
712             Frame::ConnectionClose { .. })
713     }
714 
shrink_for_retransmission(&mut self)715     pub fn shrink_for_retransmission(&mut self) {
716         if let Frame::Datagram { data } = self {
717             *data = Vec::new();
718         }
719     }
720 
721     #[cfg(feature = "qlog")]
to_qlog(&self) -> qlog::QuicFrame722     pub fn to_qlog(&self) -> qlog::QuicFrame {
723         match self {
724             Frame::Padding { .. } => qlog::QuicFrame::padding(),
725 
726             Frame::Ping { .. } => qlog::QuicFrame::ping(),
727 
728             Frame::ACK { ack_delay, ranges } => {
729                 let ack_ranges =
730                     ranges.iter().map(|r| (r.start, r.end - 1)).collect();
731                 qlog::QuicFrame::ack(
732                     Some(ack_delay.to_string()),
733                     Some(ack_ranges),
734                     None,
735                     None,
736                     None,
737                 )
738             },
739 
740             Frame::ResetStream {
741                 stream_id,
742                 error_code,
743                 final_size,
744             } => qlog::QuicFrame::reset_stream(
745                 stream_id.to_string(),
746                 *error_code,
747                 final_size.to_string(),
748             ),
749 
750             Frame::StopSending {
751                 stream_id,
752                 error_code,
753             } =>
754                 qlog::QuicFrame::stop_sending(stream_id.to_string(), *error_code),
755 
756             Frame::Crypto { data } => qlog::QuicFrame::crypto(
757                 data.off().to_string(),
758                 data.len().to_string(),
759             ),
760 
761             Frame::NewToken { token } => qlog::QuicFrame::new_token(
762                 token.len().to_string(),
763                 "TODO: https://github.com/quiclog/internet-drafts/issues/36"
764                     .to_string(),
765             ),
766 
767             Frame::Stream { stream_id, data } => qlog::QuicFrame::stream(
768                 stream_id.to_string(),
769                 data.off().to_string(),
770                 data.len().to_string(),
771                 data.fin(),
772                 None,
773             ),
774 
775             Frame::MaxData { max } => qlog::QuicFrame::max_data(max.to_string()),
776 
777             Frame::MaxStreamData { stream_id, max } =>
778                 qlog::QuicFrame::max_stream_data(
779                     stream_id.to_string(),
780                     max.to_string(),
781                 ),
782 
783             Frame::MaxStreamsBidi { max } => qlog::QuicFrame::max_streams(
784                 qlog::StreamType::Bidirectional,
785                 max.to_string(),
786             ),
787 
788             Frame::MaxStreamsUni { max } => qlog::QuicFrame::max_streams(
789                 qlog::StreamType::Unidirectional,
790                 max.to_string(),
791             ),
792 
793             Frame::DataBlocked { limit } =>
794                 qlog::QuicFrame::data_blocked(limit.to_string()),
795 
796             Frame::StreamDataBlocked { stream_id, limit } =>
797                 qlog::QuicFrame::stream_data_blocked(
798                     stream_id.to_string(),
799                     limit.to_string(),
800                 ),
801 
802             Frame::StreamsBlockedBidi { limit } =>
803                 qlog::QuicFrame::streams_blocked(
804                     qlog::StreamType::Bidirectional,
805                     limit.to_string(),
806                 ),
807 
808             Frame::StreamsBlockedUni { limit } =>
809                 qlog::QuicFrame::streams_blocked(
810                     qlog::StreamType::Unidirectional,
811                     limit.to_string(),
812                 ),
813 
814             Frame::NewConnectionId {
815                 seq_num,
816                 retire_prior_to,
817                 conn_id,
818                 ..
819             } => qlog::QuicFrame::new_connection_id(
820                 seq_num.to_string(),
821                 retire_prior_to.to_string(),
822                 conn_id.len() as u64,
823                 "TODO: https://github.com/quiclog/internet-drafts/issues/36"
824                     .to_string(),
825                 "TODO: https://github.com/quiclog/internet-drafts/issues/36"
826                     .to_string(),
827             ),
828 
829             Frame::RetireConnectionId { seq_num } =>
830                 qlog::QuicFrame::retire_connection_id(seq_num.to_string()),
831 
832             Frame::PathChallenge { .. } => qlog::QuicFrame::path_challenge(Some(
833                 "TODO: https://github.com/quiclog/internet-drafts/issues/36"
834                     .to_string(),
835             )),
836 
837             Frame::PathResponse { .. } => qlog::QuicFrame::path_response(Some(
838                 "TODO: https://github.com/quiclog/internet-drafts/issues/36"
839                     .to_string(),
840             )),
841 
842             Frame::ConnectionClose {
843                 error_code, reason, ..
844             } => qlog::QuicFrame::connection_close(
845                 qlog::ErrorSpace::TransportError,
846                 *error_code,
847                 *error_code,
848                 String::from_utf8(reason.clone()).unwrap(),
849                 Some(
850                     "TODO: https://github.com/quiclog/internet-drafts/issues/36"
851                         .to_string(),
852                 ),
853             ),
854 
855             Frame::ApplicationClose { error_code, reason } =>
856                 qlog::QuicFrame::connection_close(
857                     qlog::ErrorSpace::ApplicationError,
858                     *error_code,
859                     *error_code,
860                     String::from_utf8(reason.clone()).unwrap(),
861                     None, /* Application variant of the frame has no trigger
862                            * frame type */
863                 ),
864 
865             Frame::HandshakeDone => qlog::QuicFrame::handshake_done(),
866 
867             Frame::Datagram { .. } => qlog::QuicFrame::unknown(0x30),
868         }
869     }
870 }
871 
872 impl std::fmt::Debug for Frame {
fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result873     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
874         match self {
875             Frame::Padding { len } => {
876                 write!(f, "PADDING len={}", len)?;
877             },
878 
879             Frame::Ping => {
880                 write!(f, "PING")?;
881             },
882 
883             Frame::ACK { ack_delay, ranges } => {
884                 write!(f, "ACK delay={} blocks={:?}", ack_delay, ranges)?;
885             },
886 
887             Frame::ResetStream {
888                 stream_id,
889                 error_code,
890                 final_size,
891             } => {
892                 write!(
893                     f,
894                     "RESET_STREAM stream={} err={:x} size={}",
895                     stream_id, error_code, final_size
896                 )?;
897             },
898 
899             Frame::StopSending {
900                 stream_id,
901                 error_code,
902             } => {
903                 write!(
904                     f,
905                     "STOP_SENDING stream={} err={:x}",
906                     stream_id, error_code
907                 )?;
908             },
909 
910             Frame::Crypto { data } => {
911                 write!(f, "CRYPTO off={} len={}", data.off(), data.len())?;
912             },
913 
914             Frame::NewToken { .. } => {
915                 write!(f, "NEW_TOKEN (TODO)")?;
916             },
917 
918             Frame::Stream { stream_id, data } => {
919                 write!(
920                     f,
921                     "STREAM id={} off={} len={} fin={}",
922                     stream_id,
923                     data.off(),
924                     data.len(),
925                     data.fin()
926                 )?;
927             },
928 
929             Frame::MaxData { max } => {
930                 write!(f, "MAX_DATA max={}", max)?;
931             },
932 
933             Frame::MaxStreamData { stream_id, max } => {
934                 write!(f, "MAX_STREAM_DATA stream={} max={}", stream_id, max)?;
935             },
936 
937             Frame::MaxStreamsBidi { max } => {
938                 write!(f, "MAX_STREAMS type=bidi max={}", max)?;
939             },
940 
941             Frame::MaxStreamsUni { max } => {
942                 write!(f, "MAX_STREAMS type=uni max={}", max)?;
943             },
944 
945             Frame::DataBlocked { limit } => {
946                 write!(f, "DATA_BLOCKED limit={}", limit)?;
947             },
948 
949             Frame::StreamDataBlocked { stream_id, limit } => {
950                 write!(
951                     f,
952                     "STREAM_DATA_BLOCKED stream={} limit={}",
953                     stream_id, limit
954                 )?;
955             },
956 
957             Frame::StreamsBlockedBidi { limit } => {
958                 write!(f, "STREAMS_BLOCKED type=bidi limit={}", limit)?;
959             },
960 
961             Frame::StreamsBlockedUni { limit } => {
962                 write!(f, "STREAMS_BLOCKED type=uni limit={}", limit)?;
963             },
964 
965             Frame::NewConnectionId { .. } => {
966                 write!(f, "NEW_CONNECTION_ID (TODO)")?;
967             },
968 
969             Frame::RetireConnectionId { .. } => {
970                 write!(f, "RETIRE_CONNECTION_ID (TODO)")?;
971             },
972 
973             Frame::PathChallenge { data } => {
974                 write!(f, "PATH_CHALLENGE data={:02x?}", data)?;
975             },
976 
977             Frame::PathResponse { data } => {
978                 write!(f, "PATH_RESPONSE data={:02x?}", data)?;
979             },
980 
981             Frame::ConnectionClose {
982                 error_code,
983                 frame_type,
984                 reason,
985             } => {
986                 write!(
987                     f,
988                     "CONNECTION_CLOSE err={:x} frame={:x} reason={:x?}",
989                     error_code, frame_type, reason
990                 )?;
991             },
992 
993             Frame::ApplicationClose { error_code, reason } => {
994                 write!(
995                     f,
996                     "APPLICATION_CLOSE err={:x} reason={:x?}",
997                     error_code, reason
998                 )?;
999             },
1000 
1001             Frame::HandshakeDone => {
1002                 write!(f, "HANDSHAKE_DONE")?;
1003             },
1004 
1005             Frame::Datagram { data } => {
1006                 write!(f, "DATAGRAM len={}", data.len(),)?;
1007             },
1008         }
1009 
1010         Ok(())
1011     }
1012 }
1013 
parse_ack_frame(_ty: u64, b: &mut octets::Octets) -> Result<Frame>1014 fn parse_ack_frame(_ty: u64, b: &mut octets::Octets) -> Result<Frame> {
1015     let largest_ack = b.get_varint()?;
1016     let ack_delay = b.get_varint()?;
1017     let block_count = b.get_varint()?;
1018     let ack_block = b.get_varint()?;
1019 
1020     if largest_ack < ack_block {
1021         return Err(Error::InvalidFrame);
1022     }
1023 
1024     let mut smallest_ack = largest_ack - ack_block;
1025 
1026     let mut ranges = ranges::RangeSet::default();
1027 
1028     #[allow(clippy::range_plus_one)]
1029     ranges.insert(smallest_ack..largest_ack + 1);
1030 
1031     for _i in 0..block_count {
1032         let gap = b.get_varint()?;
1033 
1034         if smallest_ack < 2 + gap {
1035             return Err(Error::InvalidFrame);
1036         }
1037 
1038         let largest_ack = (smallest_ack - gap) - 2;
1039         let ack_block = b.get_varint()?;
1040 
1041         if largest_ack < ack_block {
1042             return Err(Error::InvalidFrame);
1043         }
1044 
1045         smallest_ack = largest_ack - ack_block;
1046 
1047         #[allow(clippy::range_plus_one)]
1048         ranges.insert(smallest_ack..largest_ack + 1);
1049     }
1050 
1051     Ok(Frame::ACK { ack_delay, ranges })
1052 }
1053 
parse_stream_frame(ty: u64, b: &mut octets::Octets) -> Result<Frame>1054 fn parse_stream_frame(ty: u64, b: &mut octets::Octets) -> Result<Frame> {
1055     let first = ty as u8;
1056 
1057     let stream_id = b.get_varint()?;
1058 
1059     let offset = if first & 0x04 != 0 {
1060         b.get_varint()?
1061     } else {
1062         0
1063     };
1064 
1065     let len = if first & 0x02 != 0 {
1066         b.get_varint()? as usize
1067     } else {
1068         b.cap()
1069     };
1070 
1071     if offset + len as u64 >= MAX_STREAM_SIZE {
1072         return Err(Error::InvalidFrame);
1073     }
1074 
1075     let fin = first & 0x01 != 0;
1076 
1077     let data = b.get_bytes(len)?;
1078     let data = stream::RangeBuf::from(data.as_ref(), offset, fin);
1079 
1080     Ok(Frame::Stream { stream_id, data })
1081 }
1082 
parse_datagram_frame(ty: u64, b: &mut octets::Octets) -> Result<Frame>1083 fn parse_datagram_frame(ty: u64, b: &mut octets::Octets) -> Result<Frame> {
1084     let first = ty as u8;
1085 
1086     let len = if first & 0x01 != 0 {
1087         b.get_varint()? as usize
1088     } else {
1089         b.cap()
1090     };
1091 
1092     let data = b.get_bytes(len)?;
1093 
1094     Ok(Frame::Datagram {
1095         data: Vec::from(data.buf()),
1096     })
1097 }
1098 
1099 #[cfg(test)]
1100 mod tests {
1101     use super::*;
1102 
1103     #[test]
padding()1104     fn padding() {
1105         let mut d = [42; 128];
1106 
1107         let frame = Frame::Padding { len: 128 };
1108 
1109         let wire_len = {
1110             let mut b = octets::OctetsMut::with_slice(&mut d);
1111             frame.to_bytes(&mut b).unwrap()
1112         };
1113 
1114         assert_eq!(wire_len, 128);
1115 
1116         let mut b = octets::Octets::with_slice(&d);
1117         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1118 
1119         let mut b = octets::Octets::with_slice(&d);
1120         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1121 
1122         let mut b = octets::Octets::with_slice(&d);
1123         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1124 
1125         let mut b = octets::Octets::with_slice(&d);
1126         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1127     }
1128 
1129     #[test]
ping()1130     fn ping() {
1131         let mut d = [42; 128];
1132 
1133         let frame = Frame::Ping;
1134 
1135         let wire_len = {
1136             let mut b = octets::OctetsMut::with_slice(&mut d);
1137             frame.to_bytes(&mut b).unwrap()
1138         };
1139 
1140         assert_eq!(wire_len, 1);
1141         assert_eq!(&d[..wire_len], [0x01 as u8]);
1142 
1143         let mut b = octets::Octets::with_slice(&d);
1144         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1145 
1146         let mut b = octets::Octets::with_slice(&d);
1147         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1148 
1149         let mut b = octets::Octets::with_slice(&d);
1150         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1151 
1152         let mut b = octets::Octets::with_slice(&d);
1153         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1154     }
1155 
1156     #[test]
ack()1157     fn ack() {
1158         let mut d = [42; 128];
1159 
1160         let mut ranges = ranges::RangeSet::default();
1161         ranges.insert(4..7);
1162         ranges.insert(9..12);
1163         ranges.insert(15..19);
1164         ranges.insert(3000..5000);
1165 
1166         let frame = Frame::ACK {
1167             ack_delay: 874_656_534,
1168             ranges,
1169         };
1170 
1171         let wire_len = {
1172             let mut b = octets::OctetsMut::with_slice(&mut d);
1173             frame.to_bytes(&mut b).unwrap()
1174         };
1175 
1176         assert_eq!(wire_len, 17);
1177 
1178         let mut b = octets::Octets::with_slice(&d);
1179         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1180 
1181         let mut b = octets::Octets::with_slice(&d);
1182         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1183 
1184         let mut b = octets::Octets::with_slice(&d);
1185         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1186 
1187         let mut b = octets::Octets::with_slice(&d);
1188         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1189     }
1190 
1191     #[test]
reset_stream()1192     fn reset_stream() {
1193         let mut d = [42; 128];
1194 
1195         let frame = Frame::ResetStream {
1196             stream_id: 123_213,
1197             error_code: 21_123_767,
1198             final_size: 21_123_767,
1199         };
1200 
1201         let wire_len = {
1202             let mut b = octets::OctetsMut::with_slice(&mut d);
1203             frame.to_bytes(&mut b).unwrap()
1204         };
1205 
1206         assert_eq!(wire_len, 13);
1207 
1208         let mut b = octets::Octets::with_slice(&d);
1209         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1210 
1211         let mut b = octets::Octets::with_slice(&d);
1212         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1213 
1214         let mut b = octets::Octets::with_slice(&d);
1215         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1216 
1217         let mut b = octets::Octets::with_slice(&d);
1218         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1219     }
1220 
1221     #[test]
stop_sending()1222     fn stop_sending() {
1223         let mut d = [42; 128];
1224 
1225         let frame = Frame::StopSending {
1226             stream_id: 123_213,
1227             error_code: 15_352,
1228         };
1229 
1230         let wire_len = {
1231             let mut b = octets::OctetsMut::with_slice(&mut d);
1232             frame.to_bytes(&mut b).unwrap()
1233         };
1234 
1235         assert_eq!(wire_len, 7);
1236 
1237         let mut b = octets::Octets::with_slice(&d);
1238         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1239 
1240         let mut b = octets::Octets::with_slice(&d);
1241         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1242 
1243         let mut b = octets::Octets::with_slice(&d);
1244         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1245 
1246         let mut b = octets::Octets::with_slice(&d);
1247         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1248     }
1249 
1250     #[test]
crypto()1251     fn crypto() {
1252         let mut d = [42; 128];
1253 
1254         let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1255 
1256         let frame = Frame::Crypto {
1257             data: stream::RangeBuf::from(&data, 1230976, false),
1258         };
1259 
1260         let wire_len = {
1261             let mut b = octets::OctetsMut::with_slice(&mut d);
1262             frame.to_bytes(&mut b).unwrap()
1263         };
1264 
1265         assert_eq!(wire_len, 18);
1266 
1267         let mut b = octets::Octets::with_slice(&d);
1268         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1269 
1270         let mut b = octets::Octets::with_slice(&d);
1271         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1272 
1273         let mut b = octets::Octets::with_slice(&d);
1274         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1275 
1276         let mut b = octets::Octets::with_slice(&d);
1277         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1278     }
1279 
1280     #[test]
new_token()1281     fn new_token() {
1282         let mut d = [42; 128];
1283 
1284         let frame = Frame::NewToken {
1285             token: Vec::from("this is a token"),
1286         };
1287 
1288         let wire_len = {
1289             let mut b = octets::OctetsMut::with_slice(&mut d);
1290             frame.to_bytes(&mut b).unwrap()
1291         };
1292 
1293         assert_eq!(wire_len, 17);
1294 
1295         let mut b = octets::Octets::with_slice(&d);
1296         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1297 
1298         let mut b = octets::Octets::with_slice(&d);
1299         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1300 
1301         let mut b = octets::Octets::with_slice(&d);
1302         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1303 
1304         let mut b = octets::Octets::with_slice(&d);
1305         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1306     }
1307 
1308     #[test]
stream()1309     fn stream() {
1310         let mut d = [42; 128];
1311 
1312         let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1313 
1314         let frame = Frame::Stream {
1315             stream_id: 32,
1316             data: stream::RangeBuf::from(&data, 1230976, true),
1317         };
1318 
1319         let wire_len = {
1320             let mut b = octets::OctetsMut::with_slice(&mut d);
1321             frame.to_bytes(&mut b).unwrap()
1322         };
1323 
1324         assert_eq!(wire_len, 19);
1325 
1326         let mut b = octets::Octets::with_slice(&d);
1327         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1328 
1329         let mut b = octets::Octets::with_slice(&d);
1330         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1331 
1332         let mut b = octets::Octets::with_slice(&d);
1333         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1334 
1335         let mut b = octets::Octets::with_slice(&d);
1336         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1337     }
1338 
1339     #[test]
stream_too_big()1340     fn stream_too_big() {
1341         let mut d = [42; 128];
1342 
1343         let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1344 
1345         let frame = Frame::Stream {
1346             stream_id: 32,
1347             data: stream::RangeBuf::from(&data, MAX_STREAM_SIZE - 11, true),
1348         };
1349 
1350         let wire_len = {
1351             let mut b = octets::OctetsMut::with_slice(&mut d);
1352             frame.to_bytes(&mut b).unwrap()
1353         };
1354 
1355         assert_eq!(wire_len, 23);
1356 
1357         let mut b = octets::Octets::with_slice(&d);
1358         assert_eq!(
1359             Frame::from_bytes(&mut b, packet::Type::Short),
1360             Err(Error::InvalidFrame)
1361         );
1362     }
1363 
1364     #[test]
max_data()1365     fn max_data() {
1366         let mut d = [42; 128];
1367 
1368         let frame = Frame::MaxData { max: 128_318_273 };
1369 
1370         let wire_len = {
1371             let mut b = octets::OctetsMut::with_slice(&mut d);
1372             frame.to_bytes(&mut b).unwrap()
1373         };
1374 
1375         assert_eq!(wire_len, 5);
1376 
1377         let mut b = octets::Octets::with_slice(&d);
1378         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1379 
1380         let mut b = octets::Octets::with_slice(&d);
1381         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1382 
1383         let mut b = octets::Octets::with_slice(&d);
1384         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1385 
1386         let mut b = octets::Octets::with_slice(&d);
1387         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1388     }
1389 
1390     #[test]
max_stream_data()1391     fn max_stream_data() {
1392         let mut d = [42; 128];
1393 
1394         let frame = Frame::MaxStreamData {
1395             stream_id: 12_321,
1396             max: 128_318_273,
1397         };
1398 
1399         let wire_len = {
1400             let mut b = octets::OctetsMut::with_slice(&mut d);
1401             frame.to_bytes(&mut b).unwrap()
1402         };
1403 
1404         assert_eq!(wire_len, 7);
1405 
1406         let mut b = octets::Octets::with_slice(&d);
1407         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1408 
1409         let mut b = octets::Octets::with_slice(&d);
1410         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1411 
1412         let mut b = octets::Octets::with_slice(&d);
1413         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1414 
1415         let mut b = octets::Octets::with_slice(&d);
1416         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1417     }
1418 
1419     #[test]
max_streams_bidi()1420     fn max_streams_bidi() {
1421         let mut d = [42; 128];
1422 
1423         let frame = Frame::MaxStreamsBidi { max: 128_318_273 };
1424 
1425         let wire_len = {
1426             let mut b = octets::OctetsMut::with_slice(&mut d);
1427             frame.to_bytes(&mut b).unwrap()
1428         };
1429 
1430         assert_eq!(wire_len, 5);
1431 
1432         let mut b = octets::Octets::with_slice(&d);
1433         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1434 
1435         let mut b = octets::Octets::with_slice(&d);
1436         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1437 
1438         let mut b = octets::Octets::with_slice(&d);
1439         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1440 
1441         let mut b = octets::Octets::with_slice(&d);
1442         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1443     }
1444 
1445     #[test]
max_streams_uni()1446     fn max_streams_uni() {
1447         let mut d = [42; 128];
1448 
1449         let frame = Frame::MaxStreamsUni { max: 128_318_273 };
1450 
1451         let wire_len = {
1452             let mut b = octets::OctetsMut::with_slice(&mut d);
1453             frame.to_bytes(&mut b).unwrap()
1454         };
1455 
1456         assert_eq!(wire_len, 5);
1457 
1458         let mut b = octets::Octets::with_slice(&d);
1459         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1460 
1461         let mut b = octets::Octets::with_slice(&d);
1462         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1463 
1464         let mut b = octets::Octets::with_slice(&d);
1465         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1466 
1467         let mut b = octets::Octets::with_slice(&d);
1468         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1469     }
1470 
1471     #[test]
data_blocked()1472     fn data_blocked() {
1473         let mut d = [42; 128];
1474 
1475         let frame = Frame::DataBlocked { limit: 128_318_273 };
1476 
1477         let wire_len = {
1478             let mut b = octets::OctetsMut::with_slice(&mut d);
1479             frame.to_bytes(&mut b).unwrap()
1480         };
1481 
1482         assert_eq!(wire_len, 5);
1483 
1484         let mut b = octets::Octets::with_slice(&d);
1485         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1486 
1487         let mut b = octets::Octets::with_slice(&d);
1488         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1489 
1490         let mut b = octets::Octets::with_slice(&d);
1491         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1492 
1493         let mut b = octets::Octets::with_slice(&d);
1494         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1495     }
1496 
1497     #[test]
stream_data_blocked()1498     fn stream_data_blocked() {
1499         let mut d = [42; 128];
1500 
1501         let frame = Frame::StreamDataBlocked {
1502             stream_id: 12_321,
1503             limit: 128_318_273,
1504         };
1505 
1506         let wire_len = {
1507             let mut b = octets::OctetsMut::with_slice(&mut d);
1508             frame.to_bytes(&mut b).unwrap()
1509         };
1510 
1511         assert_eq!(wire_len, 7);
1512 
1513         let mut b = octets::Octets::with_slice(&d);
1514         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1515 
1516         let mut b = octets::Octets::with_slice(&d);
1517         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1518 
1519         let mut b = octets::Octets::with_slice(&d);
1520         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1521 
1522         let mut b = octets::Octets::with_slice(&d);
1523         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1524     }
1525 
1526     #[test]
streams_blocked_bidi()1527     fn streams_blocked_bidi() {
1528         let mut d = [42; 128];
1529 
1530         let frame = Frame::StreamsBlockedBidi { limit: 128_318_273 };
1531 
1532         let wire_len = {
1533             let mut b = octets::OctetsMut::with_slice(&mut d);
1534             frame.to_bytes(&mut b).unwrap()
1535         };
1536 
1537         assert_eq!(wire_len, 5);
1538 
1539         let mut b = octets::Octets::with_slice(&d);
1540         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1541 
1542         let mut b = octets::Octets::with_slice(&d);
1543         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1544 
1545         let mut b = octets::Octets::with_slice(&d);
1546         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1547 
1548         let mut b = octets::Octets::with_slice(&d);
1549         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1550     }
1551 
1552     #[test]
streams_blocked_uni()1553     fn streams_blocked_uni() {
1554         let mut d = [42; 128];
1555 
1556         let frame = Frame::StreamsBlockedUni { limit: 128_318_273 };
1557 
1558         let wire_len = {
1559             let mut b = octets::OctetsMut::with_slice(&mut d);
1560             frame.to_bytes(&mut b).unwrap()
1561         };
1562 
1563         assert_eq!(wire_len, 5);
1564 
1565         let mut b = octets::Octets::with_slice(&d);
1566         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1567 
1568         let mut b = octets::Octets::with_slice(&d);
1569         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1570 
1571         let mut b = octets::Octets::with_slice(&d);
1572         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1573 
1574         let mut b = octets::Octets::with_slice(&d);
1575         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1576     }
1577 
1578     #[test]
new_connection_id()1579     fn new_connection_id() {
1580         let mut d = [42; 128];
1581 
1582         let frame = Frame::NewConnectionId {
1583             seq_num: 123_213,
1584             retire_prior_to: 122_211,
1585             conn_id: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
1586             reset_token: vec![0x42; 16],
1587         };
1588 
1589         let wire_len = {
1590             let mut b = octets::OctetsMut::with_slice(&mut d);
1591             frame.to_bytes(&mut b).unwrap()
1592         };
1593 
1594         assert_eq!(wire_len, 41);
1595 
1596         let mut b = octets::Octets::with_slice(&d);
1597         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1598 
1599         let mut b = octets::Octets::with_slice(&d);
1600         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1601 
1602         let mut b = octets::Octets::with_slice(&d);
1603         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1604 
1605         let mut b = octets::Octets::with_slice(&d);
1606         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1607     }
1608 
1609     #[test]
retire_connection_id()1610     fn retire_connection_id() {
1611         let mut d = [42; 128];
1612 
1613         let frame = Frame::RetireConnectionId { seq_num: 123_213 };
1614 
1615         let wire_len = {
1616             let mut b = octets::OctetsMut::with_slice(&mut d);
1617             frame.to_bytes(&mut b).unwrap()
1618         };
1619 
1620         assert_eq!(wire_len, 5);
1621 
1622         let mut b = octets::Octets::with_slice(&d);
1623         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1624 
1625         let mut b = octets::Octets::with_slice(&d);
1626         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1627 
1628         let mut b = octets::Octets::with_slice(&d);
1629         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1630 
1631         let mut b = octets::Octets::with_slice(&d);
1632         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1633     }
1634 
1635     #[test]
path_challenge()1636     fn path_challenge() {
1637         let mut d = [42; 128];
1638 
1639         let frame = Frame::PathChallenge {
1640             data: vec![1, 2, 3, 4, 5, 6, 7, 8],
1641         };
1642 
1643         let wire_len = {
1644             let mut b = octets::OctetsMut::with_slice(&mut d);
1645             frame.to_bytes(&mut b).unwrap()
1646         };
1647 
1648         assert_eq!(wire_len, 9);
1649 
1650         let mut b = octets::Octets::with_slice(&d);
1651         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1652 
1653         let mut b = octets::Octets::with_slice(&d);
1654         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1655 
1656         let mut b = octets::Octets::with_slice(&d);
1657         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1658 
1659         let mut b = octets::Octets::with_slice(&d);
1660         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1661     }
1662 
1663     #[test]
path_response()1664     fn path_response() {
1665         let mut d = [42; 128];
1666 
1667         let frame = Frame::PathResponse {
1668             data: vec![1, 2, 3, 4, 5, 6, 7, 8],
1669         };
1670 
1671         let wire_len = {
1672             let mut b = octets::OctetsMut::with_slice(&mut d);
1673             frame.to_bytes(&mut b).unwrap()
1674         };
1675 
1676         assert_eq!(wire_len, 9);
1677 
1678         let mut b = octets::Octets::with_slice(&d);
1679         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1680 
1681         let mut b = octets::Octets::with_slice(&d);
1682         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1683 
1684         let mut b = octets::Octets::with_slice(&d);
1685         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1686 
1687         let mut b = octets::Octets::with_slice(&d);
1688         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1689     }
1690 
1691     #[test]
connection_close()1692     fn connection_close() {
1693         let mut d = [42; 128];
1694 
1695         let frame = Frame::ConnectionClose {
1696             error_code: 0xbeef,
1697             frame_type: 523_423,
1698             reason: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
1699         };
1700 
1701         let wire_len = {
1702             let mut b = octets::OctetsMut::with_slice(&mut d);
1703             frame.to_bytes(&mut b).unwrap()
1704         };
1705 
1706         assert_eq!(wire_len, 22);
1707 
1708         let mut b = octets::Octets::with_slice(&d);
1709         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1710 
1711         let mut b = octets::Octets::with_slice(&d);
1712         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_ok());
1713 
1714         let mut b = octets::Octets::with_slice(&d);
1715         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1716 
1717         let mut b = octets::Octets::with_slice(&d);
1718         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_ok());
1719     }
1720 
1721     #[test]
application_close()1722     fn application_close() {
1723         let mut d = [42; 128];
1724 
1725         let frame = Frame::ApplicationClose {
1726             error_code: 0xbeef,
1727             reason: vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
1728         };
1729 
1730         let wire_len = {
1731             let mut b = octets::OctetsMut::with_slice(&mut d);
1732             frame.to_bytes(&mut b).unwrap()
1733         };
1734 
1735         assert_eq!(wire_len, 18);
1736 
1737         let mut b = octets::Octets::with_slice(&d);
1738         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1739 
1740         let mut b = octets::Octets::with_slice(&d);
1741         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1742 
1743         let mut b = octets::Octets::with_slice(&d);
1744         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1745 
1746         let mut b = octets::Octets::with_slice(&d);
1747         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1748     }
1749 
1750     #[test]
handshake_done()1751     fn handshake_done() {
1752         let mut d = [42; 128];
1753 
1754         let frame = Frame::HandshakeDone;
1755 
1756         let wire_len = {
1757             let mut b = octets::OctetsMut::with_slice(&mut d);
1758             frame.to_bytes(&mut b).unwrap()
1759         };
1760 
1761         assert_eq!(wire_len, 1);
1762 
1763         let mut b = octets::Octets::with_slice(&d);
1764         assert_eq!(Frame::from_bytes(&mut b, packet::Type::Short), Ok(frame));
1765 
1766         let mut b = octets::Octets::with_slice(&d);
1767         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1768 
1769         let mut b = octets::Octets::with_slice(&d);
1770         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_err());
1771 
1772         let mut b = octets::Octets::with_slice(&d);
1773         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1774     }
1775 
1776     #[test]
datagram()1777     fn datagram() {
1778         let mut d = [42; 128];
1779 
1780         let data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
1781 
1782         let mut frame = Frame::Datagram { data: data.clone() };
1783 
1784         let wire_len = {
1785             let mut b = octets::OctetsMut::with_slice(&mut d);
1786             frame.to_bytes(&mut b).unwrap()
1787         };
1788 
1789         assert_eq!(wire_len, 14);
1790 
1791         let mut b = octets::Octets::with_slice(&mut d);
1792         assert_eq!(
1793             Frame::from_bytes(&mut b, packet::Type::Short),
1794             Ok(frame.clone())
1795         );
1796 
1797         let mut b = octets::Octets::with_slice(&mut d);
1798         assert!(Frame::from_bytes(&mut b, packet::Type::Initial).is_err());
1799 
1800         let mut b = octets::Octets::with_slice(&mut d);
1801         assert!(Frame::from_bytes(&mut b, packet::Type::ZeroRTT).is_ok());
1802 
1803         let mut b = octets::Octets::with_slice(&mut d);
1804         assert!(Frame::from_bytes(&mut b, packet::Type::Handshake).is_err());
1805 
1806         let frame_data = match &frame {
1807             Frame::Datagram { data } => data.clone(),
1808 
1809             _ => unreachable!(),
1810         };
1811 
1812         assert_eq!(frame_data, data);
1813 
1814         frame.shrink_for_retransmission();
1815 
1816         let frame_data = match &frame {
1817             Frame::Datagram { data } => data.clone(),
1818 
1819             _ => unreachable!(),
1820         };
1821 
1822         assert_eq!(frame_data.len(), 0);
1823     }
1824 }
1825