Debian Codegraph @

third_party/rust/neqo-qpack/src/decoder.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 #![allow(
8 clippy::module_name_repetitions,
9 reason = "<https://github.com/mozilla/neqo/issues/2284#issuecomment-2782711813>"
10 )]
11
12 use std::fmt::{self, Display, Formatter};
13
14 use neqo_common::{qdebug, Header};
15 use neqo_transport::{Connection, StreamId};
16
17 use crate::{
18 decoder_instructions::DecoderInstruction,
19 encoder_instructions::{DecodedEncoderInstruction, EncoderInstructionReader},
20 header_block::{HeaderDecoder, HeaderDecoderResult},
21 qpack_send_buf::QpackData,
22 reader::ReceiverConnWrapper,
23 stats::Stats,
24 table::HeaderTable,
25 Error, QpackSettings, Res,
26 };
27
28 pub const QPACK_UNI_STREAM_TYPE_DECODER: u64 = 0x3;
29
30 #[derive(Debug)]
31 pub struct QPackDecoder {
32 instruction_reader: EncoderInstructionReader,
33 table: HeaderTable,
34 acked_inserts: u64,
35 max_entries: u64,
36 send_buf: QpackData,
37 local_stream_id: Option<StreamId>,
38 max_table_size: u64,
39 max_blocked_streams: usize,
40 blocked_streams: Vec<(StreamId, u64)>, // stream_id and requested inserts count.
41 stats: Stats,
42 }
43
44 impl QPackDecoder {
45 /// # Panics
46 ///
47 /// If settings include invalid values.
48 #[must_use]
49 pub fn new(qpack_settings: &QpackSettings) -> Self {
50 qdebug!("Decoder: creating a new qpack decoder");
51 let mut send_buf = QpackData::default();
52 send_buf.encode_varint(QPACK_UNI_STREAM_TYPE_DECODER);
53 Self {
54 instruction_reader: EncoderInstructionReader::new(),
55 table: HeaderTable::new(false),
56 acked_inserts: 0,
57 max_entries: qpack_settings.max_table_size_decoder >> 5,
58 send_buf,
59 local_stream_id: None,
60 max_table_size: qpack_settings.max_table_size_decoder,
61 max_blocked_streams: usize::from(qpack_settings.max_blocked_streams),
62 blocked_streams: Vec::new(),
63 stats: Stats::default(),
64 }
65 }
66
67 #[must_use]
68 const fn capacity(&self) -> u64 {
69 self.table.capacity()
70 }
71
72 #[must_use]
73 pub const fn get_max_table_size(&self) -> u64 {
74 self.max_table_size
75 }
76
77 #[must_use]
78 pub const fn get_blocked_streams(&self) -> usize {
79 self.max_blocked_streams
80 }
81
82 /// returns a list of unblocked streams
83 ///
84 /// # Errors
85 ///
86 /// May return: `ClosedCriticalStream` if stream has been closed or `EncoderStream`
87 /// in case of any other transport error.
88 pub fn receive(&mut self, conn: &mut Connection, stream_id: StreamId) -> Res<Vec<StreamId>> {
89 let base_old = self.table.base();
90 self.read_instructions(conn, stream_id)
91 .map_err(|e| map_error(&e))?;
92 let base_new = self.table.base();
93 if base_old == base_new {
94 return Ok(Vec::new());
95 }
96
97 let r = self
98 .blocked_streams
99 .iter()
100 .filter_map(|(id, req)| (*req <= base_new).then_some(*id))
101 .collect::<Vec<_>>();
102 self.blocked_streams.retain(|(_, req)| *req > base_new);
103 Ok(r)
104 }
105
106 fn read_instructions(&mut self, conn: &mut Connection, stream_id: StreamId) -> Res<()> {
107 let mut recv = ReceiverConnWrapper::new(conn, stream_id);
108 loop {
109 match self.instruction_reader.read_instructions(&mut recv) {
110 Ok(instruction) => self.execute_instruction(instruction)?,
111 Err(Error::NeedMoreData) => break Ok(()),
112 Err(e) => break Err(e),
113 }
114 }
115 }
116
117 fn execute_instruction(&mut self, instruction: DecodedEncoderInstruction) -> Res<()> {
118 match instruction {
119 DecodedEncoderInstruction::Capacity { value } => self.set_capacity(value)?,
120 DecodedEncoderInstruction::InsertWithNameRefStatic { index, value } => {
121 Error::map_error(
122 self.table.insert_with_name_ref(true, index, &value),
123 Error::EncoderStream,
124 )?;
125 self.stats.dynamic_table_inserts += 1;
126 }
127 DecodedEncoderInstruction::InsertWithNameRefDynamic { index, value } => {
128 Error::map_error(
129 self.table.insert_with_name_ref(false, index, &value),
130 Error::EncoderStream,
131 )?;
132 self.stats.dynamic_table_inserts += 1;
133 }
134 DecodedEncoderInstruction::InsertWithNameLiteral { name, value } => {
135 Error::map_error(
136 self.table.insert(&name, &value).map(|_| ()),
137 Error::EncoderStream,
138 )?;
139 self.stats.dynamic_table_inserts += 1;
140 }
141 DecodedEncoderInstruction::Duplicate { index } => {
142 Error::map_error(self.table.duplicate(index), Error::EncoderStream)?;
143 self.stats.dynamic_table_inserts += 1;
144 }
145 DecodedEncoderInstruction::NoInstruction => {
146 unreachable!("This can be call only with an instruction");
147 }
148 }
149 Ok(())
150 }
151
152 fn set_capacity(&mut self, cap: u64) -> Res<()> {
153 qdebug!("[{self}] received instruction capacity cap={cap}");
154 if cap > self.max_table_size {
155 return Err(Error::EncoderStream);
156 }
157 self.table.set_capacity(cap)
158 }
159
160 fn header_ack(&mut self, stream_id: StreamId, required_inserts: u64) {
161 DecoderInstruction::HeaderAck { stream_id }.marshal(&mut self.send_buf);
162 if required_inserts > self.acked_inserts {
163 self.acked_inserts = required_inserts;
164 }
165 }
166
167 pub fn cancel_stream(&mut self, stream_id: StreamId) {
168 if self.table.capacity() > 0 {
169 self.blocked_streams.retain(|(id, _)| *id != stream_id);
170 DecoderInstruction::StreamCancellation { stream_id }.marshal(&mut self.send_buf);
171 }
172 }
173
174 /// # Errors
175 ///
176 /// May return an error in case of any transport error. TODO: define transport errors.
177 ///
178 /// # Panics
179 ///
180 /// Never, but rust doesn't know that.
181 pub fn send(&mut self, conn: &mut Connection) -> Res<()> {
182 // Encode increment instruction if needed.
183 let increment = self.table.base() - self.acked_inserts;
184 if increment > 0 {
185 DecoderInstruction::InsertCountIncrement { increment }.marshal(&mut self.send_buf);
186 self.acked_inserts = self.table.base();
187 }
188 if !self.send_buf.is_empty() && self.local_stream_id.is_some() {
189 let r = conn
190 .stream_send(
191 self.local_stream_id.ok_or(Error::Internal)?,
192 &self.send_buf[..],
193 )
194 .map_err(|_| Error::DecoderStream)?;
195 qdebug!("[{self}] {r} bytes sent");
196 self.send_buf.read(r);
197 }
198 Ok(())
199 }
200
201 /// # Errors
202 ///
203 /// May return `DecompressionFailed` if header block is incorrect or incomplete.
204 pub fn refers_dynamic_table(&self, buf: &[u8]) -> Res<bool> {
205 HeaderDecoder::new(buf).refers_dynamic_table(self.max_entries, self.table.base())
206 }
207
208 /// This function returns None if the stream is blocked waiting for table insertions.
209 /// 'buf' must contain the complete header block.
210 ///
211 /// # Errors
212 ///
213 /// May return `DecompressionFailed` if header block is incorrect or incomplete.
214 ///
215 /// # Panics
216 ///
217 /// When there is a programming error.
218 pub fn decode_header_block(
219 &mut self,
220 buf: &[u8],
221 stream_id: StreamId,
222 ) -> Res<Option<Vec<Header>>> {
223 qdebug!("[{self}] decode header block");
224 let mut decoder = HeaderDecoder::new(buf);
225
226 match decoder.decode_header_block(&self.table, self.max_entries, self.table.base()) {
227 Ok(HeaderDecoderResult::Blocked(req_insert_cnt)) => {
228 if self.blocked_streams.len() > self.max_blocked_streams {
229 Err(Error::DecompressionFailed)
230 } else {
231 let r = self
232 .blocked_streams
233 .iter()
234 .filter_map(|(id, req)| (*id == stream_id).then_some(*req))
235 .collect::<Vec<_>>();
236 if !r.is_empty() {
237 debug_assert!(r.len() == 1);
238 debug_assert!(r[0] == req_insert_cnt);
239 return Ok(None);
240 }
241 self.blocked_streams.push((stream_id, req_insert_cnt));
242 Ok(None)
243 }
244 }
245 Ok(HeaderDecoderResult::Headers(h)) => {
246 if decoder.get_req_insert_cnt() != 0 {
247 self.header_ack(stream_id, decoder.get_req_insert_cnt());
248 self.stats.dynamic_table_references += 1;
249 }
250 Ok(Some(h))
251 }
252 Err(_) => Err(Error::DecompressionFailed),
253 }
254 }
255
256 /// # Panics
257 ///
258 /// When a stream has already been added.
259 pub fn add_send_stream(&mut self, stream_id: StreamId) {
260 assert!(
261 self.local_stream_id.is_none(),
262 "Adding multiple local streams"
263 );
264 self.local_stream_id = Some(stream_id);
265 }
266
267 #[must_use]
268 pub const fn local_stream_id(&self) -> Option<StreamId> {
269 self.local_stream_id
270 }
271
272 #[must_use]
273 pub fn stats(&self) -> Stats {
274 self.stats.clone()
275 }
276 }
277
278 impl Display for QPackDecoder {
279 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
280 write!(f, "QPackDecoder {}", self.capacity())
281 }
282 }
283
284 fn map_error(err: &Error) -> Error {
285 if *err == Error::ClosedCriticalStream {
286 Error::ClosedCriticalStream
287 } else {
288 Error::EncoderStream
289 }
290 }
291
292 #[cfg(test)]
293 mod tests {
294 use neqo_common::Header;
295 use neqo_transport::{StreamId, StreamType};
296 use test_fixture::now;
297
298 use super::{Connection, Error, QPackDecoder, Res};
299 use crate::QpackSettings;
300
301 const STREAM_0: StreamId = StreamId::new(0);
302
303 struct TestDecoder {
304 decoder: QPackDecoder,
305 send_stream_id: StreamId,
306 recv_stream_id: StreamId,
307 conn: Connection,
308 peer_conn: Connection,
309 }
310
311 fn connect() -> TestDecoder {
312 let (mut conn, mut peer_conn) = test_fixture::connect();
313
314 // create a stream
315 let recv_stream_id = peer_conn.stream_create(StreamType::UniDi).unwrap();
316 let send_stream_id = conn.stream_create(StreamType::UniDi).unwrap();
317
318 // create a decoder
319 let mut decoder = QPackDecoder::new(&QpackSettings {
320 max_table_size_encoder: 0,
321 max_table_size_decoder: 300,
322 max_blocked_streams: 100,
323 });
324 decoder.add_send_stream(send_stream_id);
325
326 TestDecoder {
327 decoder,
328 send_stream_id,
329 recv_stream_id,
330 conn,
331 peer_conn,
332 }
333 }
334
335 fn recv_instruction(decoder: &mut TestDecoder, encoder_instruction: &[u8], res: &Res<()>) {
336 _ = decoder
337 .peer_conn
338 .stream_send(decoder.recv_stream_id, encoder_instruction)
339 .unwrap();
340 let out = decoder.peer_conn.process_output(now());
341 drop(decoder.conn.process(out.dgram(), now()));
342 assert_eq!(
343 decoder
344 .decoder
345 .read_instructions(&mut decoder.conn, decoder.recv_stream_id),
346 *res
347 );
348 }
349
350 fn send_instructions_and_check(decoder: &mut TestDecoder, decoder_instruction: &[u8]) {
351 decoder.decoder.send(&mut decoder.conn).unwrap();
352 let out = decoder.conn.process_output(now());
353 drop(decoder.peer_conn.process(out.dgram(), now()));
354 let mut buf = [0_u8; 100];
355 let (amount, fin) = decoder
356 .peer_conn
357 .stream_recv(decoder.send_stream_id, &mut buf)
358 .unwrap();
359 assert!(!fin);
360 assert_eq!(&buf[..amount], decoder_instruction);
361 }
362
363 fn decode_headers(
364 decoder: &mut TestDecoder,
365 header_block: &[u8],
366 headers: &[Header],
367 stream_id: StreamId,
368 ) {
369 let decoded_headers = decoder
370 .decoder
371 .decode_header_block(header_block, stream_id)
372 .unwrap();
373 let h = decoded_headers.unwrap();
374 assert_eq!(h, headers);
375 }
376
377 fn test_instruction(
378 capacity: u64,
379 instruction: &[u8],
380 res: &Res<()>,
381 decoder_instruction: &[u8],
382 check_capacity: u64,
383 ) {
384 let mut decoder = connect();
385
386 if capacity > 0 {
387 assert!(decoder.decoder.set_capacity(capacity).is_ok());
388 }
389
390 // recv an instruction
391 recv_instruction(&mut decoder, instruction, res);
392
393 // send decoder instruction and check that is what we expect.
394 send_instructions_and_check(&mut decoder, decoder_instruction);
395
396 if check_capacity > 0 {
397 assert_eq!(decoder.decoder.capacity(), check_capacity);
398 }
399 }
400
401 // test insert_with_name_ref which fails because there is not enough space in the table
402 #[test]
403 fn recv_insert_with_name_ref_1() {
404 test_instruction(
405 0,
406 &[0xc4, 0x04, 0x31, 0x32, 0x33, 0x34],
407 &Err(Error::EncoderStream),
408 &[0x03],
409 0,
410 );
411 }
412
413 // test insert_name_ref that succeeds
414 #[test]
415 fn recv_insert_with_name_ref_2() {
416 test_instruction(
417 100,
418 &[0xc4, 0x04, 0x31, 0x32, 0x33, 0x34],
419 &Ok(()),
420 &[0x03, 0x01],
421 0,
422 );
423 }
424
425 // test insert with name literal - succeeds
426 #[test]
427 fn recv_insert_with_name_litarel_2() {
428 test_instruction(
429 200,
430 &[
431 0x4e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67, 0x74,
432 0x68, 0x04, 0x31, 0x32, 0x33, 0x34,
433 ],
434 &Ok(()),
435 &[0x03, 0x01],
436 0,
437 );
438 }
439
440 #[test]
441 fn recv_change_capacity() {
442 test_instruction(0, &[0x3f, 0xa9, 0x01], &Ok(()), &[0x03], 200);
443 }
444
445 #[test]
446 fn recv_change_capacity_too_big() {
447 test_instruction(
448 0,
449 &[0x3f, 0xf1, 0x02],
450 &Err(Error::EncoderStream),
451 &[0x03],
452 0,
453 );
454 }
455
456 // this test tests header decoding, the header acks command and the insert count increment
457 // command.
458 #[test]
459 fn duplicate() {
460 let mut decoder = connect();
461
462 assert!(decoder.decoder.set_capacity(100).is_ok());
463
464 // receive an instruction
465 recv_instruction(
466 &mut decoder,
467 &[
468 0x4e, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67, 0x74,
469 0x68, 0x04, 0x31, 0x32, 0x33, 0x34,
470 ],
471 &Ok(()),
472 );
473
474 // receive the second instruction, a duplicate instruction.
475 recv_instruction(&mut decoder, &[0x00], &Ok(()));
476
477 send_instructions_and_check(&mut decoder, &[0x03, 0x02]);
478 }
479
480 struct TestElement {
481 pub headers: Vec<Header>,
482 pub header_block: &'static [u8],
483 pub encoder_inst: &'static [u8],
484 }
485
486 #[test]
487 fn encode_incr_encode_header_ack_some() {
488 // 1. Decoder receives an instruction (header and value both as literal)
489 // 2. Decoder process the instruction and sends an increment instruction.
490 // 3. Decoder receives another two instruction (header and value both as literal) and a
491 // header block.
492 // 4. Now it sends only a header ack and an increment instruction with increment==1.
493 let headers = vec![
494 Header::new("my-headera", "my-valuea"),
495 Header::new("my-headerb", "my-valueb"),
496 ];
497 let header_block = &[0x03, 0x81, 0x10, 0x11];
498 let first_encoder_inst = &[
499 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x61, 0x09, 0x6d, 0x79,
500 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x61,
501 ];
502 let second_encoder_inst = &[
503 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x62, 0x09, 0x6d, 0x79,
504 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x62, 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61,
505 0x64, 0x65, 0x72, 0x63, 0x09, 0x6d, 0x79, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x63,
506 ];
507
508 let mut decoder = connect();
509
510 assert!(decoder.decoder.set_capacity(200).is_ok());
511
512 recv_instruction(&mut decoder, first_encoder_inst, &Ok(()));
513
514 send_instructions_and_check(&mut decoder, &[0x03, 0x1]);
515
516 recv_instruction(&mut decoder, second_encoder_inst, &Ok(()));
517
518 decode_headers(&mut decoder, header_block, &headers, STREAM_0);
519
520 send_instructions_and_check(&mut decoder, &[0x80, 0x1]);
521 }
522
523 #[test]
524 fn encode_incr_encode_header_ack_all() {
525 // 1. Decoder receives an instruction (header and value both as literal)
526 // 2. Decoder process the instruction and sends an increment instruction.
527 // 3. Decoder receives another instruction (header and value both as literal) and a header
528 // block.
529 // 4. Now it sends only a header ack.
530 let headers = vec![
531 Header::new("my-headera", "my-valuea"),
532 Header::new("my-headerb", "my-valueb"),
533 ];
534 let header_block = &[0x03, 0x81, 0x10, 0x11];
535 let first_encoder_inst = &[
536 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x61, 0x09, 0x6d, 0x79,
537 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x61,
538 ];
539 let second_encoder_inst = &[
540 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x62, 0x09, 0x6d, 0x79,
541 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x62,
542 ];
543
544 let mut decoder = connect();
545
546 assert!(decoder.decoder.set_capacity(200).is_ok());
547
548 recv_instruction(&mut decoder, first_encoder_inst, &Ok(()));
549
550 send_instructions_and_check(&mut decoder, &[0x03, 0x1]);
551
552 recv_instruction(&mut decoder, second_encoder_inst, &Ok(()));
553
554 decode_headers(&mut decoder, header_block, &headers, STREAM_0);
555
556 send_instructions_and_check(&mut decoder, &[0x80]);
557 }
558
559 #[test]
560 fn header_ack_all() {
561 // Send two instructions to insert values into the dynamic table and then send a header
562 // that references them both. The result should be only a header acknowledgement.
563 let headers = vec![
564 Header::new("my-headera", "my-valuea"),
565 Header::new("my-headerb", "my-valueb"),
566 ];
567 let header_block = &[0x03, 0x81, 0x10, 0x11];
568 let encoder_inst = &[
569 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x61, 0x09, 0x6d, 0x79,
570 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x61, 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61,
571 0x64, 0x65, 0x72, 0x62, 0x09, 0x6d, 0x79, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x62,
572 ];
573
574 let mut decoder = connect();
575
576 assert!(decoder.decoder.set_capacity(200).is_ok());
577
578 recv_instruction(&mut decoder, encoder_inst, &Ok(()));
579
580 decode_headers(&mut decoder, header_block, &headers, STREAM_0);
581
582 send_instructions_and_check(&mut decoder, &[0x03, 0x80]);
583 }
584
585 #[test]
586 fn header_ack_and_incr_instruction() {
587 // Send two instructions to insert values into the dynamic table and then send a header
588 // that references only the first. The result should be a header acknowledgement and a
589 // increment instruction.
590 let headers = vec![Header::new("my-headera", "my-valuea")];
591 let header_block = &[0x02, 0x80, 0x10];
592 let encoder_inst = &[
593 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x61, 0x09, 0x6d, 0x79,
594 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x61, 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61,
595 0x64, 0x65, 0x72, 0x62, 0x09, 0x6d, 0x79, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x62,
596 ];
597
598 let mut decoder = connect();
599
600 assert!(decoder.decoder.set_capacity(200).is_ok());
601
602 recv_instruction(&mut decoder, encoder_inst, &Ok(()));
603
604 decode_headers(&mut decoder, header_block, &headers, STREAM_0);
605
606 send_instructions_and_check(&mut decoder, &[0x03, 0x80, 0x01]);
607 }
608
609 #[test]
610 fn header_block_decoder() {
611 let test_cases: [TestElement; 6] = [
612 // test a header with ref to static - encode_indexed
613 TestElement {
614 headers: vec![Header::new(":method", "GET")],
615 header_block: &[0x00, 0x00, 0xd1],
616 encoder_inst: &[],
617 },
618 // test encode_literal_with_name_ref
619 TestElement {
620 headers: vec![Header::new(":path", "/somewhere")],
621 header_block: &[
622 0x00, 0x00, 0x51, 0x0a, 0x2f, 0x73, 0x6f, 0x6d, 0x65, 0x77, 0x68, 0x65, 0x72,
623 0x65,
624 ],
625 encoder_inst: &[],
626 },
627 // test adding a new header and encode_post_base_index, also test
628 // fix_header_block_prefix
629 TestElement {
630 headers: vec![Header::new("my-header", "my-value")],
631 header_block: &[0x02, 0x80, 0x10],
632 encoder_inst: &[
633 0x49, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x08, 0x6d, 0x79,
634 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65,
635 ],
636 },
637 // test encode_indexed with a ref to dynamic table.
638 TestElement {
639 headers: vec![Header::new("my-header", "my-value")],
640 header_block: &[0x02, 0x00, 0x80],
641 encoder_inst: &[],
642 },
643 // test encode_literal_with_name_ref.
644 TestElement {
645 headers: vec![Header::new("my-header", "my-value2")],
646 header_block: &[
647 0x02, 0x00, 0x40, 0x09, 0x6d, 0x79, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x32,
648 ],
649 encoder_inst: &[],
650 },
651 // test multiple headers
652 TestElement {
653 headers: vec![
654 Header::new(":method", "GET"),
655 Header::new(":path", "/somewhere"),
656 Header::new(":authority", "example.com"),
657 Header::new(":scheme", "https"),
658 ],
659 header_block: &[
660 0x00, 0x01, 0xd1, 0x51, 0x0a, 0x2f, 0x73, 0x6f, 0x6d, 0x65, 0x77, 0x68, 0x65,
661 0x72, 0x65, 0x50, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
662 0x6f, 0x6d, 0xd7,
663 ],
664 encoder_inst: &[],
665 },
666 ];
667
668 let mut decoder = connect();
669
670 assert!(decoder.decoder.set_capacity(200).is_ok());
671
672 for (i, t) in test_cases.iter().enumerate() {
673 // receive an instruction
674 if !t.encoder_inst.is_empty() {
675 recv_instruction(&mut decoder, t.encoder_inst, &Ok(()));
676 }
677
678 decode_headers(
679 &mut decoder,
680 t.header_block,
681 &t.headers,
682 StreamId::from(u64::try_from(i).unwrap()),
683 );
684 }
685
686 // test header acks and the insert count increment command
687 send_instructions_and_check(&mut decoder, &[0x03, 0x82, 0x83, 0x84]);
688 }
689
690 #[test]
691 fn header_block_decoder_huffman() {
692 let test_cases: [TestElement; 6] = [
693 // test a header with ref to static - encode_indexed
694 TestElement {
695 headers: vec![Header::new(":method", "GET")],
696 header_block: &[0x00, 0x00, 0xd1],
697 encoder_inst: &[],
698 },
699 // test encode_literal_with_name_ref
700 TestElement {
701 headers: vec![Header::new(":path", "/somewhere")],
702 header_block: &[
703 0x00, 0x00, 0x51, 0x87, 0x61, 0x07, 0xa4, 0xbe, 0x27, 0x2d, 0x85,
704 ],
705 encoder_inst: &[],
706 },
707 // test adding a new header and encode_post_base_index, also test
708 // fix_header_block_prefix
709 TestElement {
710 headers: vec![Header::new("my-header", "my-value")],
711 header_block: &[0x02, 0x80, 0x10],
712 encoder_inst: &[
713 0x67, 0xa7, 0xd2, 0xd3, 0x94, 0x72, 0x16, 0xcf, 0x86, 0xa7, 0xd2, 0xdd, 0xc7,
714 0x45, 0xa5,
715 ],
716 },
717 // test encode_indexed with a ref to dynamic table.
718 TestElement {
719 headers: vec![Header::new("my-header", "my-value")],
720 header_block: &[0x02, 0x00, 0x80],
721 encoder_inst: &[],
722 },
723 // test encode_literal_with_name_ref.
724 TestElement {
725 headers: vec![Header::new("my-header", "my-value2")],
726 header_block: &[
727 0x02, 0x00, 0x40, 0x87, 0xa7, 0xd2, 0xdd, 0xc7, 0x45, 0xa5, 0x17,
728 ],
729 encoder_inst: &[],
730 },
731 // test multiple headers
732 TestElement {
733 headers: vec![
734 Header::new(":method", "GET"),
735 Header::new(":path", "/somewhere"),
736 Header::new(":authority", "example.com"),
737 Header::new(":scheme", "https"),
738 ],
739 header_block: &[
740 0x00, 0x01, 0xd1, 0x51, 0x87, 0x61, 0x07, 0xa4, 0xbe, 0x27, 0x2d, 0x85, 0x50,
741 0x88, 0x2f, 0x91, 0xd3, 0x5d, 0x05, 0x5c, 0x87, 0xa7, 0xd7,
742 ],
743 encoder_inst: &[],
744 },
745 ];
746
747 let mut decoder = connect();
748
749 assert!(decoder.decoder.set_capacity(200).is_ok());
750
751 for (i, t) in test_cases.iter().enumerate() {
752 // receive an instruction.
753 if !t.encoder_inst.is_empty() {
754 recv_instruction(&mut decoder, t.encoder_inst, &Ok(()));
755 }
756
757 decode_headers(
758 &mut decoder,
759 t.header_block,
760 &t.headers,
761 StreamId::from(u64::try_from(i).unwrap()),
762 );
763 }
764
765 // test header acks and the insert count increment command
766 send_instructions_and_check(&mut decoder, &[0x03, 0x82, 0x83, 0x84]);
767 }
768
769 #[test]
770 fn subtract_overflow_in_header_ack() {
771 const HEADER_BLOCK_1: &[u8] = &[0x03, 0x81, 0x10, 0x11];
772 const ENCODER_INST: &[u8] = &[
773 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x61, 0x09, 0x6d, 0x79,
774 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x61, 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61,
775 0x64, 0x65, 0x72, 0x62, 0x09, 0x6d, 0x79, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x62,
776 ];
777 const HEADER_BLOCK_2: &[u8] = &[0x02, 0x80, 0x10];
778 // Test for issue https://github.com/mozilla/neqo/issues/475
779 // Send two instructions to insert values into the dynamic table and send a header
780 // that references them both. This will increase number of acked inserts in the table
781 // to 2. Then send a header that references only one of them which shouldn't increase
782 // number of acked inserts.
783 let headers = vec![
784 Header::new("my-headera", "my-valuea"),
785 Header::new("my-headerb", "my-valueb"),
786 ];
787
788 let mut decoder = connect();
789
790 assert!(decoder.decoder.set_capacity(200).is_ok());
791
792 recv_instruction(&mut decoder, ENCODER_INST, &Ok(()));
793
794 decode_headers(&mut decoder, HEADER_BLOCK_1, &headers, STREAM_0);
795
796 let headers = vec![Header::new("my-headera", "my-valuea")];
797
798 decode_headers(&mut decoder, HEADER_BLOCK_2, &headers, STREAM_0);
799 }
800
801 #[test]
802 fn base_larger_than_entry_count() {
803 // Test for issue https://github.com/mozilla/neqo/issues/533
804 // Send instruction that inserts 2 fields into the dynamic table and send a header that
805 // uses base larger than 2.
806 const ENCODER_INST: &[u8] = &[
807 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x61, 0x09, 0x6d, 0x79,
808 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x61, 0x4a, 0x6d, 0x79, 0x2d, 0x68, 0x65, 0x61,
809 0x64, 0x65, 0x72, 0x62, 0x09, 0x6d, 0x79, 0x2d, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x62,
810 ];
811
812 const HEADER_BLOCK: &[u8] = &[0x03, 0x03, 0x83, 0x84];
813
814 let headers = vec![
815 Header::new("my-headerb", "my-valueb"),
816 Header::new("my-headera", "my-valuea"),
817 ];
818
819 let mut decoder = connect();
820
821 assert!(decoder.decoder.set_capacity(200).is_ok());
822
823 recv_instruction(&mut decoder, ENCODER_INST, &Ok(()));
824
825 decode_headers(&mut decoder, HEADER_BLOCK, &headers, STREAM_0);
826 }
827 }