mpeg_ts/section.rs
1//! Generic PSI/SI section framing — ITU-T H.222.0 §2.4 (= ISO/IEC 13818-1) and ETSI EN 300 468 §5.1.1.
2//!
3//! Every PSI and SI table is carried in one or more sections. This module
4//! parses the **section header** and exposes the payload + CRC for
5//! table-specific parsers to consume.
6//!
7//! # Section layout
8//!
9//! ```text
10//! byte 0: table_id (8 bits)
11//! byte 1 bit 7: section_syntax_indicator (1 bit)
12//! byte 1 bit 6: private_indicator (1 bit)
13//! byte 1 bits 5-4: reserved (2 bits — ignored)
14//! byte 1 bits 3-0 + byte 2: section_length (12 bits)
15//!
16//! Long-form (section_syntax_indicator == 1):
17//! byte 3-4: table_id_extension (16 bits)
18//! byte 5: reserved(2) | version_number(5) | current_next_indicator(1)
19//! byte 6: section_number (8 bits)
20//! byte 7: last_section_number (8 bits)
21//! byte 8..(total-4): payload
22//! last 4 bytes: CRC_32
23//!
24//! Short-form (section_syntax_indicator == 0, e.g. TDT):
25//! byte 3..(3+section_length): payload (no extension header, no CRC)
26//! ```
27//!
28//! NOTE the TOT exception: the TOT (0x73) also sets SSI=0 but DOES end with a
29//! CRC_32 (EN 300 468 §5.2.6). Parsing it through this generic short-form
30//! path folds the CRC into `payload` — use the table-specific `TotSection` parser instead.
31//!
32//! `section_length` counts bytes *after* the 3-byte section header, so the
33//! total section size is `section_length + 3`.
34
35use crate::error::{Error, Result};
36use broadcast_common::crc32_mpeg2 as crc;
37use broadcast_common::len::{need_in, need_out};
38use broadcast_common::{Parse, Serialize};
39
40const MIN_HEADER_LEN: usize = 3;
41
42// Long-form header adds: extension_id(2) + version/cni(1) + sec_num(1) +
43// last_sec_num(1) = 5 bytes.
44const LONG_FORM_EXTRA: usize = 5;
45
46// CRC occupies the last 4 bytes of every long-form section.
47const CRC_LEN: usize = 4;
48
49// W15 (#1074): named bit-field masks for the section header, used by both
50// `parse` and `serialize_into` — ISO/IEC 13818-1 §2.4.4.1.
51
52/// `section_syntax_indicator` — byte 1 bit 7.
53const SSI_MASK: u8 = 0x80;
54/// `private_indicator` (byte 1 bit 6). Meaning is table-specific; DVB SI uses
55/// this position for the DVB `private_indicator` (ETSI EN 300 468 §5.1.1).
56const PRIVATE_INDICATOR_MASK: u8 = 0x40;
57/// Byte 1 bits 5-4: reserved, convention is both set on serialize.
58const BYTE1_RESERVED_BITS: u8 = 0x30;
59/// `version_number` — 5 bits (byte 5 bits 5-1 of the long-form extension header).
60const VERSION_NUMBER_MASK: u8 = 0x1F;
61/// Byte 5 bits 7-6: reserved, convention is both set on serialize.
62const BYTE5_RESERVED_BITS: u8 = 0xC0;
63/// `current_next_indicator` — byte 5 bit 0 of the long-form extension header.
64const CURRENT_NEXT_MASK: u8 = 0x01;
65
66/// A parsed PSI/SI section header, borrowing the raw input buffer for payload.
67///
68/// Created via `Section::parse(bytes)`. Does **not** validate the CRC on
69/// construction — call [`Section::validate_crc`] explicitly.
70#[derive(Debug, Clone, PartialEq, Eq)]
71#[cfg_attr(feature = "serde", derive(serde::Serialize))]
72pub struct Section<'a> {
73 /// Table identifier.
74 pub table_id: u8,
75 /// When `true` the section uses the long-form syntax (has extension header
76 /// and CRC). When `false` only the 3-byte header is present (short form,
77 /// e.g. TDT — but see the module docs for the TOT exception: SSI=0 yet
78 /// CRC present; parse TOT via `tables::tot`, not this path).
79 pub section_syntax_indicator: bool,
80 /// Private indicator bit (meaning is table-specific).
81 pub private_indicator: bool,
82 /// Number of bytes following byte 2 of the section header.
83 pub section_length: u16,
84 /// Table ID extension (aka `table_id_extension`). Present only for
85 /// long-form sections; zero for short-form.
86 pub extension_id: u16,
87 /// Version number (5 bits). Present only for long-form sections.
88 pub version_number: u8,
89 /// `current_next_indicator` flag. Present only for long-form sections.
90 pub current_next_indicator: bool,
91 /// Section number within the table sub-table.
92 pub section_number: u8,
93 /// Number of the last section in the table sub-table.
94 pub last_section_number: u8,
95 /// Section payload: excludes the header bytes and the trailing CRC for
96 /// long-form sections. For short-form sections this is bytes
97 /// `3..(section_length + 3)`.
98 pub payload: &'a [u8],
99 /// Declared CRC value (last 4 bytes, big-endian). `None` for short-form
100 /// sections which carry no CRC.
101 pub crc32: Option<u32>,
102}
103
104impl<'a> Section<'a> {
105 /// Return the payload slice (same as the `payload` field — convenience
106 /// getter for code that has a `&Section` reference).
107 #[inline]
108 pub fn payload(&self) -> &'a [u8] {
109 self.payload
110 }
111
112 /// Validate the CRC-32 of the section against `raw` — the complete section
113 /// bytes (including header and CRC suffix).
114 ///
115 /// For short-form sections (`section_syntax_indicator == false`) this
116 /// returns `Ok(())` immediately because no CRC is present.
117 ///
118 /// # Errors
119 ///
120 /// Returns [`Error::CrcMismatch`] when the computed CRC over
121 /// `raw[..raw.len() - 4]` does not match the declared value at
122 /// `raw[raw.len()-4..]`.
123 pub fn validate_crc(&self, raw: &[u8]) -> Result<()> {
124 let expected = match self.crc32 {
125 None => return Ok(()), // short-form — no CRC
126 Some(v) => v,
127 };
128
129 // Guard: raw must be at least CRC_LEN bytes for a valid long-form section.
130 need_in(raw, CRC_LEN, "CRC suffix in validate_crc")?;
131
132 // The CRC covers everything up to (but not including) the 4 CRC bytes.
133 let covered = &raw[..raw.len() - CRC_LEN];
134 let computed = crc::compute(covered);
135
136 if computed != expected {
137 return Err(Error::CrcMismatch(broadcast_common::error::CrcMismatch {
138 what: "section",
139 computed,
140 expected,
141 }));
142 }
143 Ok(())
144 }
145}
146
147impl<'a> Parse<'a> for Section<'a> {
148 type Error = crate::error::Error;
149 /// Parse a complete section from `bytes`.
150 ///
151 /// # Errors
152 ///
153 /// - [`Error::BufferTooShort`] — fewer than 3 bytes supplied.
154 /// - [`Error::SectionLengthOverflow`] — `section_length` field declares
155 /// more data than `bytes` contains.
156 fn parse(bytes: &'a [u8]) -> Result<Self> {
157 // ── 3-byte common header ────────────────────────────────────────────
158 need_in(bytes, MIN_HEADER_LEN, "section header")?;
159
160 let table_id = bytes[0];
161 let section_syntax_indicator = (bytes[1] & SSI_MASK) != 0;
162 let private_indicator = (bytes[1] & PRIVATE_INDICATOR_MASK) != 0;
163
164 // Total section size is section_length + 3 (the 3-byte header itself
165 // is not counted by section_length). W14 (#1074): shared with the
166 // other three sites that used to decode this field separately.
167 let total = crate::ts::section_total_len(bytes)
168 .expect("bytes.len() >= MIN_HEADER_LEN checked above");
169 let section_length = (total - MIN_HEADER_LEN) as u16;
170
171 if bytes.len() < total {
172 return Err(Error::SectionLengthOverflow {
173 declared: total,
174 available: bytes.len(),
175 });
176 }
177
178 // Work only inside the declared section boundary.
179 let section_bytes = &bytes[..total];
180
181 if !section_syntax_indicator {
182 // ── Short-form section (e.g. TDT) ──────────────────────────────
183 // No extension header, no CRC. Payload is everything after the
184 // 3-byte header.
185 let payload = §ion_bytes[MIN_HEADER_LEN..];
186 return Ok(Section {
187 table_id,
188 section_syntax_indicator,
189 private_indicator,
190 section_length,
191 extension_id: 0,
192 version_number: 0,
193 current_next_indicator: false,
194 section_number: 0,
195 last_section_number: 0,
196 payload,
197 crc32: None,
198 });
199 }
200
201 // ── Long-form section ───────────────────────────────────────────────
202 // Minimum size for a valid long-form section:
203 // 3 (common header) + 5 (extension header) + 4 (CRC) = 12 bytes.
204 let min_long = MIN_HEADER_LEN + LONG_FORM_EXTRA + CRC_LEN;
205 need_in(
206 section_bytes,
207 min_long,
208 "long-form section extension header + CRC",
209 )?;
210
211 let extension_id = ((bytes[3] as u16) << 8) | (bytes[4] as u16);
212 let version_number = (bytes[5] >> 1) & VERSION_NUMBER_MASK;
213 let current_next_indicator = (bytes[5] & CURRENT_NEXT_MASK) != 0;
214 let section_number = bytes[6];
215 let last_section_number = bytes[7];
216
217 // Payload: bytes[8 .. total-4] (excludes 5-byte extension header
218 // counting from offset 3, and the 4-byte CRC at the end).
219 let payload_start = MIN_HEADER_LEN + LONG_FORM_EXTRA;
220 let payload_end = total - CRC_LEN;
221 let payload = §ion_bytes[payload_start..payload_end];
222
223 let crc_offset = total - CRC_LEN;
224 let crc32 = Some(
225 ((section_bytes[crc_offset] as u32) << 24)
226 | ((section_bytes[crc_offset + 1] as u32) << 16)
227 | ((section_bytes[crc_offset + 2] as u32) << 8)
228 | (section_bytes[crc_offset + 3] as u32),
229 );
230
231 Ok(Section {
232 table_id,
233 section_syntax_indicator,
234 private_indicator,
235 section_length,
236 extension_id,
237 version_number,
238 current_next_indicator,
239 section_number,
240 last_section_number,
241 payload,
242 crc32,
243 })
244 }
245}
246
247impl Serialize for Section<'_> {
248 type Error = crate::error::Error;
249 fn serialized_len(&self) -> usize {
250 usize::from(self.section_length) + MIN_HEADER_LEN
251 }
252
253 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
254 let need = self.serialized_len();
255 need_out(buf, need)?;
256
257 // W5 (#1074): `section_length` and `payload.len()` are two
258 // independently-settable public fields. If a caller edits one
259 // without the other, the old code indexed `buf` by
260 // `payload.len()` while `serialized_len()` (and thus `need`/`buf`)
261 // was sized from `section_length` — an over-long payload panicked
262 // on the `copy_from_slice` below, and an under-long one silently
263 // wrote a wrong CRC into stale trailing bytes. Validate the two
264 // agree before touching `buf`.
265 let overhead = if self.section_syntax_indicator {
266 LONG_FORM_EXTRA + CRC_LEN
267 } else {
268 0
269 };
270 let expected_payload_len = usize::from(self.section_length).checked_sub(overhead);
271 if expected_payload_len != Some(self.payload.len()) {
272 return Err(Error::SectionPayloadLengthMismatch {
273 section_length: self.section_length,
274 expected: expected_payload_len.unwrap_or(0),
275 actual: self.payload.len(),
276 });
277 }
278
279 // Byte 0: table_id
280 buf[0] = self.table_id;
281
282 // Byte 1: SSI | PI | 2-bit reserved (set high per spec) | length hi 4 bits
283 let length_hi = ((self.section_length >> 8) as u8) & crate::ts::SECTION_LENGTH_HI_MASK;
284 let ssi = u8::from(self.section_syntax_indicator) << 7;
285 let pi = u8::from(self.private_indicator) << 6;
286 // Reserved bits 5..4 are 'reserved' per §5.1.1 — convention is both set.
287 buf[1] = ssi | pi | BYTE1_RESERVED_BITS | length_hi;
288
289 // Byte 2: length low 8 bits
290 buf[2] = self.section_length.to_be_bytes()[1];
291
292 if self.section_syntax_indicator {
293 // Long form: 5 bytes of extension header, then payload, then CRC.
294 buf[3..5].copy_from_slice(&self.extension_id.to_be_bytes());
295 // Byte 5: 2-bit reserved (both high) | 5-bit version | 1-bit current_next
296 let version = (self.version_number & VERSION_NUMBER_MASK) << 1;
297 let cni = u8::from(self.current_next_indicator);
298 buf[5] = BYTE5_RESERVED_BITS | version | cni;
299 buf[6] = self.section_number;
300 buf[7] = self.last_section_number;
301
302 let payload_start = MIN_HEADER_LEN + LONG_FORM_EXTRA;
303 let payload_end = payload_start + self.payload.len();
304 buf[payload_start..payload_end].copy_from_slice(self.payload);
305
306 // Append CRC — use the declared value to preserve round-trip identity.
307 let crc = self
308 .crc32
309 .unwrap_or_else(|| crc::compute(&buf[..payload_end]));
310 let crc_start = payload_end;
311 buf[crc_start..crc_start + CRC_LEN].copy_from_slice(&crc.to_be_bytes());
312 } else {
313 // Short form: no extension header, no CRC.
314 let payload_end = MIN_HEADER_LEN + self.payload.len();
315 buf[MIN_HEADER_LEN..payload_end].copy_from_slice(self.payload);
316 }
317
318 Ok(need)
319 }
320}
321
322#[cfg(test)]
323mod tests {
324 use super::*;
325 use alloc::vec;
326 use alloc::vec::Vec;
327 use broadcast_common::crc32_mpeg2::compute as crc32;
328
329 // ── Helper: build a minimal long-form section with correct CRC ───────────
330
331 /// Build a syntactically valid long-form section byte vector.
332 ///
333 /// Layout: [table_id, flags+len_hi, len_lo, ext_hi, ext_lo,
334 /// ver_cni, sec_num, last_sec_num, ...payload..., crc(4)]
335 fn make_long_section(
336 table_id: u8,
337 extension_id: u16,
338 version: u8,
339 current_next: bool,
340 section_number: u8,
341 last_section_number: u8,
342 payload: &[u8],
343 ) -> Vec<u8> {
344 // section_length = 5 (extension header) + payload.len() + 4 (CRC)
345 let section_length: u16 = (5 + payload.len() + 4) as u16;
346
347 // reserved(2) | version(5) | current_next(1)
348 let ver_cni = 0xC0u8 | ((version & 0x1F) << 1) | (current_next as u8);
349 let mut buf: Vec<u8> = vec![
350 table_id,
351 // section_syntax_indicator=1, private_indicator=0, reserved=0b11, upper 4 bits of section_length
352 0x80 | 0x30 | ((section_length >> 8) as u8 & 0x0F),
353 (section_length & 0xFF) as u8,
354 (extension_id >> 8) as u8,
355 (extension_id & 0xFF) as u8,
356 ver_cni,
357 section_number,
358 last_section_number,
359 ];
360 buf.extend_from_slice(payload);
361
362 // Compute CRC over bytes so far, append as big-endian u32.
363 let crc = crc32(&buf);
364 buf.push((crc >> 24) as u8);
365 buf.push((crc >> 16) as u8);
366 buf.push((crc >> 8) as u8);
367 buf.push(crc as u8);
368
369 buf
370 }
371
372 // ── Test 1 ───────────────────────────────────────────────────────────────
373
374 #[test]
375 fn parse_rejects_buffer_shorter_than_3_bytes() {
376 for bad_len in [0usize, 1, 2] {
377 let buf = vec![0x00u8; bad_len];
378 let err = Section::parse(&buf).unwrap_err();
379 assert!(
380 matches!(err, Error::BufferTooShort(broadcast_common::error::BufferTooShort { need: 3, have, .. }) if have == bad_len),
381 "expected BufferTooShort for len={bad_len}, got {err:?}"
382 );
383 }
384 }
385
386 // ── Test 2 ───────────────────────────────────────────────────────────────
387
388 #[test]
389 fn parse_reads_table_id_syntax_indicator_and_length() {
390 // Construct a minimal 13-byte long-form section with no payload.
391 // section_length = 5 (extension header) + 0 (payload) + 4 (CRC) = 9
392 // BUT we need section_length >= min for a valid section: 9 → total = 12, which is 12 bytes, not 13.
393 // Let's use 1 byte of payload so section_length = 10, total = 13.
394 let raw = make_long_section(0x42, 0x1234, 3, true, 0, 0, &[0xAB]);
395 assert_eq!(raw.len(), 13);
396
397 let section = Section::parse(&raw).unwrap();
398 assert_eq!(section.table_id, 0x42);
399 assert!(section.section_syntax_indicator);
400 // section_length = 5 + 1 + 4 = 10
401 assert_eq!(section.section_length, 10);
402 }
403
404 // ── Test 3 ───────────────────────────────────────────────────────────────
405
406 #[test]
407 fn parse_rejects_when_section_length_exceeds_buffer() {
408 // Build a 3-byte header that claims section_length = 100 bytes of data.
409 // Buffer is only 3 bytes (just the header), so total = 103 > 3.
410 let buf = [
411 0x00u8, // table_id
412 0x80u8, // section_syntax_indicator=1, section_length upper nibble = 0
413 100u8, // section_length lower byte = 100 → total = 103
414 ];
415 let err = Section::parse(&buf).unwrap_err();
416 assert!(
417 matches!(
418 err,
419 Error::SectionLengthOverflow {
420 declared: 103,
421 available: 3
422 }
423 ),
424 "expected SectionLengthOverflow, got {err:?}"
425 );
426 }
427
428 // ── Test 4 ───────────────────────────────────────────────────────────────
429
430 #[test]
431 fn parse_reads_extension_id_version_current_next_section_numbers() {
432 let raw = make_long_section(
433 0x02, // PMT table_id
434 0xBEEF, // extension_id / program_number
435 7, // version_number
436 true, // current_next
437 2, // section_number
438 5, // last_section_number
439 &[0x00, 0x00], // dummy payload
440 );
441
442 let section = Section::parse(&raw).unwrap();
443 assert_eq!(section.extension_id, 0xBEEF);
444 assert_eq!(section.version_number, 7);
445 assert!(section.current_next_indicator);
446 assert_eq!(section.section_number, 2);
447 assert_eq!(section.last_section_number, 5);
448 }
449
450 #[test]
451 fn parse_reads_current_next_indicator_false() {
452 // Same as test 4 but with current_next = false (bit 0 of byte 5 cleared).
453 let raw = make_long_section(
454 0x02, // PMT table_id
455 0xBEEF, // extension_id
456 7, // version_number
457 false, // current_next — the field under test
458 2, // section_number
459 5, // last_section_number
460 &[0x00, 0x00],
461 );
462
463 let section = Section::parse(&raw).unwrap();
464 assert!(!section.current_next_indicator);
465 // Confirm the other fields are unaffected by flipping bit 0.
466 assert_eq!(section.extension_id, 0xBEEF);
467 assert_eq!(section.version_number, 7);
468 assert_eq!(section.section_number, 2);
469 assert_eq!(section.last_section_number, 5);
470 }
471
472 // ── Test 5 ───────────────────────────────────────────────────────────────
473
474 #[test]
475 fn payload_slice_excludes_header_and_crc() {
476 let inner_payload = &[0x01u8, 0x02, 0x03, 0x04, 0x05];
477 let raw = make_long_section(0x42, 0x0001, 0, true, 0, 0, inner_payload);
478
479 let section = Section::parse(&raw).unwrap();
480 assert_eq!(section.payload(), inner_payload);
481 }
482
483 // ── Test 6 ───────────────────────────────────────────────────────────────
484
485 #[test]
486 fn validate_crc_accepts_matching_crc32() {
487 let raw = make_long_section(0x00, 0x0001, 1, true, 0, 0, &[0xDE, 0xAD, 0xBE, 0xEF]);
488 let section = Section::parse(&raw).unwrap();
489 section.validate_crc(&raw).expect("CRC should match");
490 }
491
492 // ── Test 7 ───────────────────────────────────────────────────────────────
493
494 #[test]
495 fn validate_crc_rejects_flipped_bit() {
496 let mut raw = make_long_section(0x00, 0x0001, 1, true, 0, 0, &[0xDE, 0xAD, 0xBE, 0xEF]);
497 // Flip a bit inside the payload (byte 8 is the first payload byte) BEFORE
498 // parsing. The 4-byte CRC at the tail of `raw` was computed over the
499 // original (un-flipped) bytes and is NOT updated here, so after the flip:
500 // • `raw[..raw.len()-4]` contains corrupted data, and
501 // • `raw[raw.len()-4..]` still holds the CRC of the original data.
502 // Parsing after the flip captures that old CRC into `section.crc32`.
503 // `validate_crc` then recomputes over the corrupted bytes and detects the
504 // mismatch — which is exactly the invariant we are testing.
505 // (Parsing before the flip would immutably borrow `raw` for the lifetime
506 // of `section` — the compiler would then reject `raw[8] ^= 0x01` because
507 // a mutable borrow conflicts with any live shared borrow.)
508 raw[8] ^= 0x01;
509
510 let section = Section::parse(&raw).unwrap();
511 let err = section.validate_crc(&raw).unwrap_err();
512 assert!(
513 matches!(err, Error::CrcMismatch(..)),
514 "expected CrcMismatch, got {err:?}"
515 );
516 }
517
518 // ── Test (Fix 1 TDD) ─────────────────────────────────────────────────────
519
520 #[test]
521 fn validate_crc_rejects_raw_slice_shorter_than_crc_len() {
522 let raw = make_long_section(0x42, 0x0001, 0, true, 0, 0, &[0xDE, 0xAD]);
523 let section = Section::parse(&raw).unwrap();
524 // Pass an empty slice — shorter than CRC_LEN bytes.
525 let err = section.validate_crc(&[]).unwrap_err();
526 assert!(
527 matches!(
528 err,
529 Error::BufferTooShort(broadcast_common::error::BufferTooShort {
530 need: CRC_LEN,
531 ..
532 })
533 ),
534 "expected BufferTooShort(need=CRC_LEN), got {err:?}"
535 );
536 }
537
538 // ── Test 8 ───────────────────────────────────────────────────────────────
539
540 #[test]
541 fn short_form_section_has_no_crc() {
542 // TDT-style short-form section: section_syntax_indicator = 0.
543 // section_length = 5 (5 bytes of payload), total = 8 bytes.
544 let buf = [
545 0x70u8, // table_id (TDT)
546 0x70u8, // SSI=0, private=1, reserved=0b11, upper nibble of section_length=0
547 0x05u8, // section_length = 5
548 // 5 bytes of "UTC time" payload
549 0xE0, 0x00, 0x00, 0x00, 0x00,
550 ];
551
552 let section = Section::parse(&buf).unwrap();
553 assert!(!section.section_syntax_indicator);
554 assert!(section.crc32.is_none());
555 // validate_crc on short-form should return Ok(()) vacuously.
556 section
557 .validate_crc(&buf)
558 .expect("short-form: no CRC to validate");
559 // Payload is the 5 bytes after the 3-byte header.
560 assert_eq!(section.payload(), &buf[3..]);
561 }
562
563 // ── W5 (#1074): serialize rejects a hand-edited length/payload mismatch ──
564
565 #[test]
566 fn serialize_rejects_oversized_payload_for_declared_section_length() {
567 // A caller edited `payload` to be longer than `section_length`
568 // implies (all fields are `pub`). Pre-fix, `serialized_len()` sized
569 // `buf` from `section_length` while the copy indexed by
570 // `payload.len()`, so this panicked with an out-of-bounds slice
571 // index instead of erroring.
572 let raw = make_long_section(0x42, 0x0001, 0, true, 0, 0, &[0xAA, 0xBB]);
573 let mut section = Section::parse(&raw).unwrap();
574 let oversized = vec![0u8; section.payload.len() + 1];
575 section.payload = &oversized;
576
577 let mut buf = vec![0u8; section.serialized_len()];
578 let err = section.serialize_into(&mut buf).unwrap_err();
579 assert!(
580 matches!(err, Error::SectionPayloadLengthMismatch { actual, .. } if actual == oversized.len()),
581 "expected SectionPayloadLengthMismatch, got {err:?}"
582 );
583 }
584
585 #[test]
586 fn serialize_rejects_undersized_payload_for_declared_section_length() {
587 let raw = make_long_section(0x42, 0x0001, 0, true, 0, 0, &[0xAA, 0xBB, 0xCC]);
588 let mut section = Section::parse(&raw).unwrap();
589 let undersized = &[0xAAu8][..];
590 section.payload = undersized;
591
592 // Buffer sized generously so an undersized payload can't be caught by
593 // `OutputBufferTooSmall` first — it must be caught by the length
594 // consistency check.
595 let mut buf = vec![0u8; section.serialized_len() + 16];
596 let err = section.serialize_into(&mut buf).unwrap_err();
597 assert!(
598 matches!(err, Error::SectionPayloadLengthMismatch { actual, .. } if actual == undersized.len()),
599 "expected SectionPayloadLengthMismatch, got {err:?}"
600 );
601 }
602
603 #[test]
604 fn serialize_accepts_consistent_section_round_trip() {
605 // Sanity: a self-consistent Section (as produced by `parse`) still
606 // serializes byte-identically after the new validation.
607 let raw = make_long_section(0x42, 0x0001, 3, true, 1, 2, &[0x01, 0x02, 0x03]);
608 let section = Section::parse(&raw).unwrap();
609 let mut buf = vec![0u8; section.serialized_len()];
610 let n = section.serialize_into(&mut buf).unwrap();
611 assert_eq!(&buf[..n], raw.as_slice());
612 }
613}