dvb-si 13.0.0

ETSI EN 300 468 DVB Service Information parser + builder. MPEG-2 PSI included.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Service Description Table — ETSI EN 300 468 §5.2.3.
//!
//! SDT lists the services available in a transport stream, with a
//! descriptor loop per service (service_descriptor etc.). The table is
//! split into two variants by table_id: `0x42` for the actual TS the
//! receiver is tuned to, `0x46` for services on other TSes.

use super::RunningStatus;
use crate::descriptors::DescriptorLoop;
use crate::error::{Error, Result};
use alloc::vec::Vec;
use broadcast_common::len::{need_in, need_out};
use broadcast_common::{Parse, Serialize};

/// table_id value for SDT describing services on the actual TS.
pub const TABLE_ID_ACTUAL: u8 = 0x42;
/// table_id value for SDT describing services on other TSes.
pub const TABLE_ID_OTHER: u8 = 0x46;
/// Well-known PID on which SDT is carried.
pub const PID: mpeg_ts::pid::Pid = mpeg_ts::pid::Pid::new(0x0011);

const MIN_HEADER_LEN: usize = 3;
const EXTENSION_HEADER_LEN: usize = 5;
/// Bytes after the extension header and before the first service entry:
/// 2 bytes of original_network_id + 1 reserved_future_use byte.
const POST_EXTENSION_LEN: usize = 3;
/// `EIT_schedule_flag` in a service's flags byte (EN 300 468 §5.2.3).
const EIT_SCHEDULE_FLAG: u8 = 0x02;
/// `EIT_present_following_flag` in a service's flags byte (§5.2.3).
const EIT_PRESENT_FOLLOWING_FLAG: u8 = 0x01;
use super::CRC_LEN;
const MIN_SECTION_LEN: usize = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN + CRC_LEN;
const SERVICE_HEADER_LEN: usize = 5;

/// SDT kind — distinguishes `0x42` (actual) from `0x46` (other).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[non_exhaustive]
pub enum SdtKind {
    /// Services on the transport stream the receiver is tuned to.
    Actual,
    /// Services on other transport streams (cross-TS SDT).
    Other,
}

/// One service entry in an SDT.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
#[non_exhaustive]
pub struct SdtService<'a> {
    /// service_id (matches `program_number` in the PAT).
    pub service_id: u16,
    /// EIT schedule flag — present-and-following events are in the EIT/schedule.
    pub eit_schedule_flag: bool,
    /// EIT P/F flag — present-and-following events are in the EIT present/following.
    pub eit_present_following_flag: bool,
    /// 3-bit running_status (EN 300 468 Table 6).
    pub running_status: RunningStatus,
    /// free_CA_mode: `true` = at least one elementary stream is scrambled.
    pub free_ca_mode: bool,
    /// Descriptor loop for this service (service_descriptor etc.). Serializes
    /// as the typed descriptor sequence; `.raw()` yields the wire bytes.
    pub descriptors: DescriptorLoop<'a>,
    /// `reserved_future_use(6)` before `EIT_schedule_flag` (EN 300 468
    /// §5.2.3, `111111`); preserved from the wire.
    #[cfg_attr(feature = "serde", serde(skip))]
    pub reserved_future_use: u8,
}

impl<'a> SdtService<'a> {
    /// Construct a service entry with the spec `reserved_future_use` fill.
    #[must_use]
    pub fn new(
        service_id: u16,
        eit_schedule_flag: bool,
        eit_present_following_flag: bool,
        running_status: RunningStatus,
        free_ca_mode: bool,
        descriptors: DescriptorLoop<'a>,
    ) -> Self {
        Self {
            service_id,
            eit_schedule_flag,
            eit_present_following_flag,
            running_status,
            free_ca_mode,
            descriptors,
            reserved_future_use: RESERVED_FUTURE_USE_6,
        }
    }
}

/// All-ones `reserved_future_use(6)` before the EIT flags.
const RESERVED_FUTURE_USE_6: u8 = super::RESERVED_6;
/// All-ones `reserved_future_use(8)` after `original_network_id`.
const RESERVED_FUTURE_USE_8: u8 = 0xFF;
/// Shift of `reserved_future_use(6)` in the EIT-flags byte.
const EIT_FLAGS_RESERVED_SHIFT: u32 = 2;

/// Service Description Table.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
#[non_exhaustive]
pub struct SdtSection<'a> {
    /// Variant discriminator (table_id 0x42 vs 0x46).
    pub kind: SdtKind,
    /// transport_stream_id of the described TS.
    pub transport_stream_id: u16,
    /// 5-bit version_number.
    pub version_number: u8,
    /// current_next_indicator bit.
    pub current_next_indicator: bool,
    /// section_number in the sub-table sequence.
    pub section_number: u8,
    /// last_section_number in the sub-table sequence.
    pub last_section_number: u8,
    /// original_network_id of the described TS.
    pub original_network_id: u16,
    /// Services in wire order.
    pub services: Vec<SdtService<'a>>,
    /// Header bit fields (`reserved_future_use`, `reserved(2)` x2; default
    /// [`SectionBits::dvb`](super::SectionBits::dvb)).
    #[cfg_attr(feature = "serde", serde(skip))]
    pub bits: super::SectionBits,
    /// `reserved_future_use(8)` after `original_network_id` (`0xFF`).
    #[cfg_attr(feature = "serde", serde(skip))]
    pub reserved_future_use: u8,
}

impl<'a> SdtSection<'a> {
    /// Construct an SDT section with the spec `reserved` fill.
    #[must_use]
    // The eight arguments mirror the table's wire fields (EN 300 468 §5.2.3); a builder would only rename them.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        kind: SdtKind,
        transport_stream_id: u16,
        version_number: u8,
        current_next_indicator: bool,
        section_number: u8,
        last_section_number: u8,
        original_network_id: u16,
        services: Vec<SdtService<'a>>,
    ) -> Self {
        Self {
            kind,
            transport_stream_id,
            version_number,
            current_next_indicator,
            section_number,
            last_section_number,
            original_network_id,
            services,
            bits: super::SectionBits::dvb(),
            reserved_future_use: RESERVED_FUTURE_USE_8,
        }
    }
}

impl<'a> Parse<'a> for SdtSection<'a> {
    type Error = crate::error::Error;
    fn parse(bytes: &'a [u8]) -> Result<Self> {
        let min_len = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN + CRC_LEN;
        need_in(bytes, min_len, "SdtSection")?;
        let kind = match bytes[0] {
            TABLE_ID_ACTUAL => SdtKind::Actual,
            TABLE_ID_OTHER => SdtKind::Other,
            other => {
                return Err(Error::UnexpectedTableId {
                    table_id: other,
                    what: "SdtSection",
                    expected: &[TABLE_ID_ACTUAL, TABLE_ID_OTHER],
                });
            }
        };

        let section_length = super::section_length_of(bytes);
        let total = super::check_section_length(
            bytes.len(),
            MIN_HEADER_LEN,
            section_length,
            MIN_SECTION_LEN,
        )?;

        let transport_stream_id = crate::wire::be_u16(bytes, 3, "sdt")?;
        let version_number = super::version_number_of(bytes[5]);
        let current_next_indicator = super::current_next_of(bytes[5]);
        let section_number = bytes[6];
        let last_section_number = bytes[7];
        let original_network_id = crate::wire::be_u16(bytes, 8, "sdt")?;

        let services_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN;
        let services_end = total - CRC_LEN;
        let mut services = Vec::new();
        let mut pos = services_start;
        while pos + SERVICE_HEADER_LEN <= services_end {
            let (b2, _) = bytes
                .get(pos..)
                .and_then(|s| s.split_first_chunk::<2>())
                .ok_or(Error::BufferTooShort(
                    broadcast_common::error::BufferTooShort {
                        need: pos + 2,
                        have: services_end,
                        what: "SdtSection service_id",
                    },
                ))?;
            let service_id = u16::from_be_bytes(*b2);
            let flags = bytes[pos + 2];
            let eit_schedule_flag = (flags & EIT_SCHEDULE_FLAG) != 0;
            let eit_present_following_flag = (flags & EIT_PRESENT_FOLLOWING_FLAG) != 0;
            let status_and_len_hi = bytes[pos + 3];
            let running_status =
                RunningStatus::from_u8(super::running_status_of(status_and_len_hi));
            let free_ca_mode = super::free_ca_mode_of(status_and_len_hi);
            let descriptors_loop_length = super::dll_from_status(status_and_len_hi, bytes[pos + 4]);
            let desc_start = pos + SERVICE_HEADER_LEN;
            let desc_end = desc_start + descriptors_loop_length;
            if desc_end > services_end {
                return Err(Error::SectionLengthOverflow {
                    declared: descriptors_loop_length,
                    available: services_end.saturating_sub(desc_start),
                });
            }
            services.push(SdtService {
                service_id,
                eit_schedule_flag,
                eit_present_following_flag,
                running_status,
                free_ca_mode,
                descriptors: DescriptorLoop::new(&bytes[desc_start..desc_end]),
                reserved_future_use: flags >> EIT_FLAGS_RESERVED_SHIFT,
            });
            pos = desc_end;
        }

        if pos != services_end {
            return Err(Error::BufferTooShort(
                broadcast_common::error::BufferTooShort {
                    need: services_end - pos,
                    have: 0,
                    what: "SdtSection trailing service bytes",
                },
            ));
        }

        Ok(SdtSection {
            kind,
            transport_stream_id,
            version_number,
            current_next_indicator,
            section_number,
            last_section_number,
            original_network_id,
            services,
            bits: super::SectionBits::parse(bytes[1], Some(bytes[5])),
            reserved_future_use: bytes[10],
        })
    }
}

impl Serialize for SdtSection<'_> {
    type Error = crate::error::Error;
    fn serialized_len(&self) -> usize {
        let svc_bytes: usize = self
            .services
            .iter()
            .map(|s| SERVICE_HEADER_LEN + s.descriptors.len())
            .sum();
        MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN + svc_bytes + CRC_LEN
    }

    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
        let len = self.serialized_len();
        need_out(buf, len)?;
        buf[0] = match self.kind {
            SdtKind::Actual => TABLE_ID_ACTUAL,
            SdtKind::Other => TABLE_ID_OTHER,
        };
        buf[1] = self.bits.b1()?;
        super::write_section_length(buf, len - MIN_HEADER_LEN)?;
        buf[3..5].copy_from_slice(&self.transport_stream_id.to_be_bytes());
        buf[5] = self
            .bits
            .b5(self.version_number, self.current_next_indicator)?;
        buf[6] = self.section_number;
        buf[7] = self.last_section_number;
        buf[8..10].copy_from_slice(&self.original_network_id.to_be_bytes());
        buf[10] = self.reserved_future_use;

        let mut pos = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + POST_EXTENSION_LEN;
        for svc in &self.services {
            buf[pos..pos + 2].copy_from_slice(&svc.service_id.to_be_bytes());
            let flags = (broadcast_common::len::fit_mask(
                svc.reserved_future_use,
                RESERVED_FUTURE_USE_6,
                "reserved_future_use",
            )? << EIT_FLAGS_RESERVED_SHIFT)
                | (u8::from(svc.eit_schedule_flag) << 1)
                | u8::from(svc.eit_present_following_flag);
            buf[pos + 2] = flags;
            let dll = broadcast_common::len::fit_bits(
                svc.descriptors.len() as u64,
                12,
                "descriptors_loop_length",
            )?;
            buf[pos + 3] = super::status_byte(
                svc.running_status.to_u8(),
                svc.free_ca_mode,
                (dll >> 8) as u8,
            );
            buf[pos + 4] = dll as u8;
            let desc_start = pos + SERVICE_HEADER_LEN;
            buf[desc_start..desc_start + svc.descriptors.len()]
                .copy_from_slice(svc.descriptors.raw());
            pos = desc_start + svc.descriptors.len();
        }

        let crc_pos = len - CRC_LEN;
        super::write_crc(buf, crc_pos);
        Ok(len)
    }
}
impl<'a> crate::traits::TableDef<'a> for SdtSection<'a> {
    const TABLE_ID_RANGES: &'static [(u8, u8)] = &[
        (TABLE_ID_ACTUAL, TABLE_ID_ACTUAL),
        (TABLE_ID_OTHER, TABLE_ID_OTHER),
    ];
    const NAME: &'static str = "SERVICE_DESCRIPTION";
}

#[cfg(test)]
mod tests {
    use super::*;

    type TestService = (u16, bool, bool, u8, bool, Vec<u8>);

    fn build_sdt(
        kind: SdtKind,
        tsid: u16,
        version: u8,
        original_network_id: u16,
        services: &[TestService],
    ) -> Vec<u8> {
        let svc_bytes: usize = services
            .iter()
            .map(|(_, _, _, _, _, d)| SERVICE_HEADER_LEN + d.len())
            .sum();
        let section_length: u16 =
            (EXTENSION_HEADER_LEN + POST_EXTENSION_LEN + svc_bytes + CRC_LEN) as u16;
        let mut v = Vec::new();
        v.push(match kind {
            SdtKind::Actual => TABLE_ID_ACTUAL,
            SdtKind::Other => TABLE_ID_OTHER,
        });
        crate::tables::push_section_header(
            &mut v,
            crate::tables::SECTION_B1_FLAGS_DVB,
            section_length as usize,
        );
        v.extend_from_slice(&tsid.to_be_bytes());
        v.push(crate::tables::version_byte(version, true));
        v.push(0);
        v.push(0);
        v.extend_from_slice(&original_network_id.to_be_bytes());
        v.push(0xFF);
        for (sid, eit_s, eit_pf, rs, fca, desc) in services {
            v.extend_from_slice(&sid.to_be_bytes());
            let flags = 0xFC | (u8::from(*eit_s) << 1) | u8::from(*eit_pf);
            v.push(flags);
            crate::tables::push_status_and_len(&mut v, *rs, *fca, desc.len());
            v.extend_from_slice(desc);
        }
        v.extend_from_slice(&[0, 0, 0, 0]);
        v
    }

    #[test]
    fn parse_actual_and_other_tables_distinguished_by_table_id() {
        let a = build_sdt(SdtKind::Actual, 1, 0, 0x20, &[]);
        let o = build_sdt(SdtKind::Other, 1, 0, 0x20, &[]);
        assert!(matches!(
            SdtSection::parse(&a).unwrap().kind,
            SdtKind::Actual
        ));
        assert!(matches!(
            SdtSection::parse(&o).unwrap().kind,
            SdtKind::Other
        ));
    }

    #[test]
    fn parse_services_with_descriptor_bytes() {
        let bytes = build_sdt(
            SdtKind::Actual,
            1,
            0,
            0x20,
            &[(
                100,
                true,
                true,
                4,
                false,
                vec![0x48, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05],
            )],
        );
        let sdt = SdtSection::parse(&bytes).unwrap();
        assert_eq!(sdt.services.len(), 1);
        assert_eq!(sdt.services[0].service_id, 100);
        assert!(sdt.services[0].eit_schedule_flag);
        assert!(sdt.services[0].eit_present_following_flag);
        assert_eq!(sdt.services[0].running_status, RunningStatus::Running);
        assert!(!sdt.services[0].free_ca_mode);
        assert_eq!(
            sdt.services[0].descriptors.raw(),
            &[0x48, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05][..]
        );
    }

    #[test]
    fn service_free_ca_mode_flag_extracted() {
        let bytes = build_sdt(
            SdtKind::Actual,
            1,
            0,
            0x20,
            &[(1, false, false, 0, true, vec![])],
        );
        let sdt = SdtSection::parse(&bytes).unwrap();
        assert!(sdt.services[0].free_ca_mode);
    }

    #[test]
    fn service_running_status_extracted() {
        let bytes = build_sdt(
            SdtKind::Actual,
            1,
            0,
            0x20,
            &[(1, false, false, 2, false, vec![])],
        );
        let sdt = SdtSection::parse(&bytes).unwrap();
        assert_eq!(
            sdt.services[0].running_status,
            RunningStatus::StartsInAFewSeconds
        );
    }

    #[test]
    fn parse_rejects_short_buffer() {
        let err = SdtSection::parse(&[0x42, 0x00]).unwrap_err();
        assert!(matches!(err, Error::BufferTooShort(..)));
    }

    #[test]
    fn parse_rejects_wrong_table_id() {
        let mut bytes = build_sdt(SdtKind::Actual, 1, 0, 0x20, &[]);
        bytes[0] = 0x00;
        let err = SdtSection::parse(&bytes).unwrap_err();
        assert!(matches!(
            err,
            Error::UnexpectedTableId { table_id: 0x00, .. }
        ));
    }

    #[test]
    fn serialize_round_trip() {
        let desc1: [u8; 4] = [0x48, 0x02, 0xAA, 0xBB];
        let sdt = SdtSection::new(
            SdtKind::Actual,
            0x1234,
            5,
            true,
            0,
            0,
            0x0020,
            vec![
                SdtService::new(
                    100,
                    true,
                    false,
                    RunningStatus::Running,
                    false,
                    DescriptorLoop::new(&desc1),
                ),
                SdtService::new(
                    101,
                    false,
                    true,
                    RunningStatus::StartsInAFewSeconds,
                    true,
                    DescriptorLoop::new(&[]),
                ),
            ],
        );
        let mut buf = vec![0u8; sdt.serialized_len()];
        sdt.serialize_into(&mut buf).unwrap();
        let re = SdtSection::parse(&buf).unwrap();
        assert_eq!(sdt, re);
    }

    #[test]
    fn zero_services_is_valid() {
        let bytes = build_sdt(SdtKind::Actual, 1, 0, 0x20, &[]);
        let sdt = SdtSection::parse(&bytes).unwrap();
        assert_eq!(sdt.services.len(), 0);
    }

    #[test]
    fn parse_rejects_zero_section_length() {
        let mut buf = vec![0u8; 64];
        buf[0] = TABLE_ID_ACTUAL;
        buf[1] = 0xF0;
        buf[2] = 0x00;
        for b in &mut buf[3..] {
            *b = 0xFF;
        }
        assert!(matches!(
            SdtSection::parse(&buf).unwrap_err(),
            Error::SectionLengthOverflow { .. }
        ));
    }

    /// section_length is 12 bits (max 4095 = EXTENSION_HEADER_LEN(5) +
    /// POST_EXTENSION_LEN(3) + SERVICE_HEADER_LEN(5) + descriptors +
    /// CRC(4), so 4078 is the largest single-service descriptor loop that
    /// fits).
    #[test]
    fn serialize_rejects_service_descriptors_exceeding_section_length() {
        let desc = vec![0xAAu8; 4079];
        let sdt = SdtSection::new(
            SdtKind::Actual,
            1,
            0,
            true,
            0,
            0,
            0x20,
            vec![SdtService::new(
                1,
                false,
                false,
                RunningStatus::Undefined,
                false,
                DescriptorLoop::new(&desc),
            )],
        );
        let mut buf = vec![0u8; sdt.serialized_len()];
        assert!(matches!(
            sdt.serialize_into(&mut buf).unwrap_err(),
            Error::FieldOverflow(_)
        ));
    }

    #[test]
    fn serialize_accepts_service_descriptors_at_section_length_boundary() {
        let desc = vec![0xAAu8; 4078];
        let sdt = SdtSection::new(
            SdtKind::Actual,
            1,
            0,
            true,
            0,
            0,
            0x20,
            vec![SdtService::new(
                1,
                false,
                false,
                RunningStatus::Undefined,
                false,
                DescriptorLoop::new(&desc),
            )],
        );
        let mut buf = vec![0u8; sdt.serialized_len()];
        sdt.serialize_into(&mut buf).expect("boundary fits");
        let reparsed = SdtSection::parse(&buf).expect("reparse");
        assert_eq!(sdt, reparsed);
    }

    #[test]
    fn parse_rejects_trailing_slack_bytes() {
        // ETSI EN 300 468 §5.2.3: the service loop runs to services_end
        // exactly. A truncated final entry (fewer than SERVICE_HEADER_LEN
        // bytes of slack) must be rejected, not silently dropped (mirrors
        // EitSection's post-loop check).
        let mut bytes = build_sdt(
            SdtKind::Actual,
            1,
            0,
            0x20,
            &[(1, false, false, 0, false, vec![])],
        );
        let sl = (bytes.len() - MIN_HEADER_LEN) as u16 + 2;
        bytes[1] = (bytes[1] & 0xF0) | ((sl >> 8) as u8 & 0x0F);
        bytes[2] = (sl & 0xFF) as u8;
        let crc_pos = bytes.len() - CRC_LEN;
        bytes.splice(crc_pos..crc_pos, [0xFF, 0xFF]);
        let err = SdtSection::parse(&bytes).unwrap_err();
        assert!(matches!(
            err,
            Error::BufferTooShort(broadcast_common::error::BufferTooShort {
                what: "SdtSection trailing service bytes",
                ..
            })
        ));
    }

    #[test]
    fn parse_accepts_well_formed_service_loop_with_no_slack() {
        let bytes = build_sdt(
            SdtKind::Actual,
            1,
            0,
            0x20,
            &[
                (1, false, false, 0, false, vec![]),
                (2, true, true, 4, false, vec![0x48, 0x02, 0xAA, 0xBB]),
            ],
        );
        let sdt = SdtSection::parse(&bytes).unwrap();
        assert_eq!(sdt.services.len(), 2);
    }
}