Skip to main content

dvb_si/descriptors/
data_broadcast_id.rs

1//! Data Broadcast Id Descriptor — ETSI EN 300 468 §6.2.13 (tag 0x66).
2//!
3//! Table 32 (PDF p. 72). Identifies the data broadcast specification used by a
4//! data component, plus an `id_selector_byte` tail whose interpretation depends
5//! on the `data_broadcast_id` (see ETSI TS 101 162).
6//!
7//! The `id_selector` is decoded into [`IdSelector`]:
8//!
9//! - `data_broadcast_id = 0x000A` (SSU) → [`IdSelector::Ssu`] carrying a typed
10//!   [`SsuIdSelector`] (TS 102 006 §7.1 Table 4).
11//! - All other ids → [`IdSelector::Raw`] carrying the raw bytes, preserving
12//!   unknown-id behaviour for all non-SSU users.
13
14use super::descriptor_body;
15use crate::error::{Error, Result};
16use dvb_common::{Parse, Serialize};
17
18/// Descriptor tag for data_broadcast_id_descriptor.
19pub const TAG: u8 = 0x66;
20const HEADER_LEN: usize = 2;
21/// Fixed prefix length: the 16-bit data_broadcast_id (EN 300 468 Table 32).
22const ID_LEN: usize = 2;
23
24/// `data_broadcast_id` value for DVB System Software Update (TS 102 006).
25pub const DATA_BROADCAST_ID_SSU: u16 = 0x000A;
26
27// SSU id_selector constants (TS 102 006 Table 4).
28/// OUI_data_length field: 1 byte.
29const SSU_OUI_DATA_LENGTH_LEN: usize = 1;
30/// Per-OUI fixed fields: OUI(3) + combined-byte-1(1) + combined-byte-2(1) +
31/// selector_length(1) = 6 bytes before the selector bytes.
32const SSU_OUI_FIXED_LEN: usize = 6;
33const SSU_UPDATE_TYPE_MASK: u8 = 0x0F;
34const SSU_UPDATE_VERSIONING_FLAG_MASK: u8 = 0x20;
35const SSU_UPDATE_VERSION_MASK: u8 = 0x1F;
36
37/// Returns the well-known name for a `data_broadcast_id`, or `None` if the
38/// ID is not recognised.
39///
40/// Verified entries from the DVB Services registry.
41#[must_use]
42pub fn data_broadcast_id_name(id: u16) -> Option<&'static str> {
43    match id {
44        0x0005 => Some("Multiprotocol Encapsulation (MPE)"),
45        0x0006 => Some("Data Carousel"),
46        0x0007 => Some("Object Carousel"),
47        DATA_BROADCAST_ID_SSU => Some("System Software Update (SSU)"),
48        0x000B => Some("IP/MAC Notification (INT)"),
49        0x00F0 => Some("MHP Object Carousel"),
50        0x0123 => Some("HbbTV"),
51        _ => None,
52    }
53}
54
55/// One OUI entry in an SSU `id_selector` — TS 102 006 §7.1 Table 4.
56///
57/// Wire layout per entry (all in bits):
58///
59/// ```text
60/// OUI                       24  bslbf
61/// reserved                   4  bslbf
62/// update_type                4  uimsbf  (Table 5)
63/// reserved                   2  bslbf
64/// update_versioning_flag     1  uimsbf
65/// update_version             5  uimsbf
66/// selector_length            8  uimsbf
67/// selector_byte × N          8  uimsbf
68/// ```
69#[derive(Debug, Clone, PartialEq, Eq)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize))]
71pub struct SsuOuiEntry<'a> {
72    /// `OUI` — 24-bit IEEE Organizationally Unique Identifier.
73    pub oui: [u8; 3],
74    /// `update_type` `[3:0]` — TS 102 006 Table 5 coding.
75    /// `0x1` = standard update carousel, `0x2` = carousel with UNT, etc.
76    pub update_type: u8,
77    /// `update_versioning_flag` — when `1`, `update_version` is valid.
78    pub update_versioning_flag: bool,
79    /// `update_version` `[4:0]` — version counter; only meaningful when
80    /// `update_versioning_flag` is set.
81    pub update_version: u8,
82    /// `selector_byte` loop — additional targeting selector bytes.
83    #[cfg_attr(feature = "serde", serde(borrow))]
84    pub selector: &'a [u8],
85}
86
87/// Typed `id_selector` for `data_broadcast_id = 0x000A` (SSU) —
88/// TS 102 006 §7.1 Table 4 `system_software_update_info()`.
89///
90/// Wire layout:
91///
92/// ```text
93/// system_software_update_info() {
94///   OUI_data_length  8  uimsbf   (byte count of the OUI loop)
95///   for (i=0; i<N; i++) {
96///     OUI                       24
97///     reserved | update_type     8  (upper 4 = reserved, lower 4 = update_type)
98///     reserved | uvf | uversion  8  (upper 2 = reserved, bit5 = uvf, lower 5 = uversion)
99///     selector_length            8
100///     selector_byte × M          8
101///   }
102///   private_data_byte × P  8   (remainder after OUI loop)
103/// }
104/// ```
105#[derive(Debug, Clone, PartialEq, Eq)]
106#[cfg_attr(feature = "serde", derive(serde::Serialize))]
107pub struct SsuIdSelector<'a> {
108    /// OUI entries (the `OUI_data_length`-bounded loop).
109    pub oui_entries: Vec<SsuOuiEntry<'a>>,
110    /// `private_data_byte` tail — bytes after the OUI loop.
111    #[cfg_attr(feature = "serde", serde(borrow))]
112    pub private_data: &'a [u8],
113}
114
115/// Typed or raw `id_selector` — dispatch on `data_broadcast_id`.
116///
117/// Unknown ids fall through to `Raw`, preserving byte-exact content for
118/// non-SSU callers.
119#[derive(Debug, Clone, PartialEq, Eq)]
120#[cfg_attr(feature = "serde", derive(serde::Serialize))]
121pub enum IdSelector<'a> {
122    /// `data_broadcast_id = 0x000A` — TS 102 006 §7.1 Table 4 SSU selector.
123    Ssu(SsuIdSelector<'a>),
124    /// All other `data_broadcast_id` values — raw bytes, no interpretation.
125    #[cfg_attr(feature = "serde", serde(borrow))]
126    Raw(&'a [u8]),
127}
128
129impl<'a> IdSelector<'a> {
130    /// Parse bytes as the appropriate selector type for `data_broadcast_id`.
131    pub fn parse(data_broadcast_id: u16, bytes: &'a [u8]) -> Result<Self> {
132        if data_broadcast_id == DATA_BROADCAST_ID_SSU {
133            Ok(IdSelector::Ssu(SsuIdSelector::parse(bytes)?))
134        } else {
135            Ok(IdSelector::Raw(bytes))
136        }
137    }
138
139    /// Serialized byte length of this selector (not including the surrounding
140    /// descriptor tag/length or the 16-bit data_broadcast_id field).
141    pub fn serialized_len(&self) -> usize {
142        match self {
143            IdSelector::Ssu(s) => s.serialized_len(),
144            IdSelector::Raw(b) => b.len(),
145        }
146    }
147
148    /// Write the wire bytes into `buf` starting at offset `pos`.
149    pub fn serialize_into_at(&self, buf: &mut [u8], pos: usize) -> Result<usize> {
150        match self {
151            IdSelector::Ssu(s) => s.serialize_into(&mut buf[pos..]),
152            IdSelector::Raw(b) => {
153                buf[pos..pos + b.len()].copy_from_slice(b);
154                Ok(b.len())
155            }
156        }
157    }
158}
159
160impl<'a> Parse<'a> for SsuIdSelector<'a> {
161    type Error = crate::error::Error;
162
163    fn parse(bytes: &'a [u8]) -> Result<Self> {
164        if bytes.len() < SSU_OUI_DATA_LENGTH_LEN {
165            return Err(Error::InvalidDescriptor {
166                tag: TAG,
167                reason: "SSU id_selector too short for OUI_data_length",
168            });
169        }
170        let oui_data_length = bytes[0] as usize;
171        let oui_loop_end = SSU_OUI_DATA_LENGTH_LEN + oui_data_length;
172        if oui_loop_end > bytes.len() {
173            return Err(Error::InvalidDescriptor {
174                tag: TAG,
175                reason: "SSU id_selector OUI_data_length exceeds available bytes",
176            });
177        }
178        let mut pos = SSU_OUI_DATA_LENGTH_LEN;
179        let mut oui_entries = Vec::new();
180        while pos < oui_loop_end {
181            if pos + SSU_OUI_FIXED_LEN > oui_loop_end {
182                return Err(Error::InvalidDescriptor {
183                    tag: TAG,
184                    reason: "SSU id_selector OUI entry truncated",
185                });
186            }
187            let oui = [bytes[pos], bytes[pos + 1], bytes[pos + 2]];
188            let byte3 = bytes[pos + 3]; // reserved(4)|update_type(4)
189            let byte4 = bytes[pos + 4]; // reserved(2)|uvf(1)|uversion(5)
190            let selector_length = bytes[pos + 5] as usize;
191            pos += SSU_OUI_FIXED_LEN;
192            if pos + selector_length > oui_loop_end {
193                return Err(Error::InvalidDescriptor {
194                    tag: TAG,
195                    reason: "SSU id_selector OUI selector_length exceeds OUI loop",
196                });
197            }
198            let selector = &bytes[pos..pos + selector_length];
199            pos += selector_length;
200            oui_entries.push(SsuOuiEntry {
201                oui,
202                update_type: byte3 & SSU_UPDATE_TYPE_MASK,
203                update_versioning_flag: (byte4 & SSU_UPDATE_VERSIONING_FLAG_MASK) != 0,
204                update_version: byte4 & SSU_UPDATE_VERSION_MASK,
205                selector,
206            });
207        }
208        let private_data = &bytes[oui_loop_end..];
209        Ok(SsuIdSelector {
210            oui_entries,
211            private_data,
212        })
213    }
214}
215
216impl Serialize for SsuIdSelector<'_> {
217    type Error = crate::error::Error;
218
219    fn serialized_len(&self) -> usize {
220        let oui_body: usize = self
221            .oui_entries
222            .iter()
223            .map(|e| SSU_OUI_FIXED_LEN + e.selector.len())
224            .sum();
225        SSU_OUI_DATA_LENGTH_LEN + oui_body + self.private_data.len()
226    }
227
228    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
229        let len = self.serialized_len();
230        if buf.len() < len {
231            return Err(Error::OutputBufferTooSmall {
232                need: len,
233                have: buf.len(),
234            });
235        }
236        let oui_body: usize = self
237            .oui_entries
238            .iter()
239            .map(|e| SSU_OUI_FIXED_LEN + e.selector.len())
240            .sum();
241        if oui_body > u8::MAX as usize {
242            return Err(Error::InvalidDescriptor {
243                tag: TAG,
244                reason: "SSU OUI loop exceeds 255 bytes (OUI_data_length field overflow)",
245            });
246        }
247        buf[0] = oui_body as u8;
248        let mut pos = SSU_OUI_DATA_LENGTH_LEN;
249        for e in &self.oui_entries {
250            if e.selector.len() > u8::MAX as usize {
251                return Err(Error::InvalidDescriptor {
252                    tag: TAG,
253                    reason: "SSU OUI entry selector exceeds 255 bytes",
254                });
255            }
256            buf[pos..pos + 3].copy_from_slice(&e.oui);
257            buf[pos + 3] = e.update_type & SSU_UPDATE_TYPE_MASK; // reserved=0 | update_type
258            let uvf_bit: u8 = if e.update_versioning_flag {
259                SSU_UPDATE_VERSIONING_FLAG_MASK
260            } else {
261                0
262            };
263            buf[pos + 4] = uvf_bit | (e.update_version & SSU_UPDATE_VERSION_MASK);
264            buf[pos + 5] = e.selector.len() as u8;
265            pos += SSU_OUI_FIXED_LEN;
266            buf[pos..pos + e.selector.len()].copy_from_slice(e.selector);
267            pos += e.selector.len();
268        }
269        buf[pos..pos + self.private_data.len()].copy_from_slice(self.private_data);
270        Ok(len)
271    }
272}
273
274/// Data Broadcast Id Descriptor (tag 0x66).
275#[derive(Debug, Clone, PartialEq, Eq)]
276#[cfg_attr(feature = "serde", derive(serde::Serialize))]
277#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
278pub struct DataBroadcastIdDescriptor<'a> {
279    /// 16-bit data_broadcast_id (ETSI TS 101 162 registration).
280    pub data_broadcast_id: u16,
281    /// Raw `id_selector_byte` tail (wire bytes, unchanged on round-trip).
282    /// Decode it on demand with [`Self::id_selector_decoded`] — typed for
283    /// `data_broadcast_id = 0x000A` (SSU), raw for all other ids.
284    #[cfg_attr(feature = "serde", serde(borrow))]
285    pub id_selector: &'a [u8],
286}
287
288impl<'a> DataBroadcastIdDescriptor<'a> {
289    /// Decode the `id_selector` tail according to `data_broadcast_id`:
290    /// [`IdSelector::Ssu`] for `0x000A` (TS 102 006 §7.1 Table 4), else
291    /// [`IdSelector::Raw`]. Decode-on-demand; the raw bytes remain in
292    /// [`Self::id_selector`] and round-trip verbatim.
293    pub fn id_selector_decoded(&self) -> Result<IdSelector<'a>> {
294        IdSelector::parse(self.data_broadcast_id, self.id_selector)
295    }
296}
297
298impl<'a> Parse<'a> for DataBroadcastIdDescriptor<'a> {
299    type Error = crate::error::Error;
300    fn parse(bytes: &'a [u8]) -> Result<Self> {
301        let body = descriptor_body(
302            bytes,
303            TAG,
304            "DataBroadcastIdDescriptor",
305            "unexpected tag for data_broadcast_id_descriptor",
306        )?;
307        if body.len() < ID_LEN {
308            return Err(Error::InvalidDescriptor {
309                tag: TAG,
310                reason: "data_broadcast_id_descriptor body shorter than 2 bytes",
311            });
312        }
313        let data_broadcast_id = u16::from_be_bytes([body[0], body[1]]);
314        let id_selector = &body[ID_LEN..];
315        Ok(Self {
316            data_broadcast_id,
317            id_selector,
318        })
319    }
320}
321
322impl Serialize for DataBroadcastIdDescriptor<'_> {
323    type Error = crate::error::Error;
324    fn serialized_len(&self) -> usize {
325        HEADER_LEN + ID_LEN + self.id_selector.len()
326    }
327
328    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
329        let len = self.serialized_len();
330        let body = ID_LEN + self.id_selector.len();
331        if body > u8::MAX as usize {
332            return Err(Error::InvalidDescriptor {
333                tag: TAG,
334                reason: "data_broadcast_id_descriptor body exceeds 255 bytes",
335            });
336        }
337        if buf.len() < len {
338            return Err(Error::OutputBufferTooSmall {
339                need: len,
340                have: buf.len(),
341            });
342        }
343        buf[0] = TAG;
344        buf[1] = body as u8;
345        buf[HEADER_LEN..HEADER_LEN + ID_LEN].copy_from_slice(&self.data_broadcast_id.to_be_bytes());
346        buf[HEADER_LEN + ID_LEN..len].copy_from_slice(self.id_selector);
347        Ok(len)
348    }
349}
350impl<'a> crate::traits::DescriptorDef<'a> for DataBroadcastIdDescriptor<'a> {
351    const TAG: u8 = TAG;
352    const NAME: &'static str = "DATA_BROADCAST_ID";
353}
354
355#[cfg(test)]
356mod tests {
357    use super::*;
358
359    // ── DataBroadcastIdDescriptor (non-SSU raw path) ──────────────────────────
360
361    #[test]
362    fn parse_extracts_id_and_raw_selector() {
363        let bytes = [TAG, 0x05, 0x00, 0x0B, 0xAA, 0xBB, 0xCC];
364        let d = DataBroadcastIdDescriptor::parse(&bytes).unwrap();
365        assert_eq!(d.data_broadcast_id, 0x000B);
366        assert_eq!(d.id_selector, &[0xAA, 0xBB, 0xCC][..]);
367        assert_eq!(
368            d.id_selector_decoded().unwrap(),
369            IdSelector::Raw(&[0xAA, 0xBB, 0xCC])
370        );
371    }
372
373    #[test]
374    fn parse_accepts_empty_raw_selector() {
375        // Non-SSU id with no selector bytes.
376        let bytes = [TAG, 0x02, 0x00, 0x0B];
377        let d = DataBroadcastIdDescriptor::parse(&bytes).unwrap();
378        assert_eq!(d.data_broadcast_id, 0x000B);
379        assert!(d.id_selector.is_empty());
380        assert_eq!(d.id_selector_decoded().unwrap(), IdSelector::Raw(&[]));
381    }
382
383    #[test]
384    fn data_broadcast_id_name_verified() {
385        assert_eq!(
386            data_broadcast_id_name(0x0005),
387            Some("Multiprotocol Encapsulation (MPE)")
388        );
389        assert_eq!(data_broadcast_id_name(0x0006), Some("Data Carousel"));
390        assert_eq!(data_broadcast_id_name(0x0007), Some("Object Carousel"));
391        assert_eq!(
392            data_broadcast_id_name(DATA_BROADCAST_ID_SSU),
393            Some("System Software Update (SSU)")
394        );
395        assert_eq!(
396            data_broadcast_id_name(0x000B),
397            Some("IP/MAC Notification (INT)")
398        );
399        assert_eq!(data_broadcast_id_name(0x00F0), Some("MHP Object Carousel"));
400        assert_eq!(data_broadcast_id_name(0x0123), Some("HbbTV"));
401    }
402
403    #[test]
404    fn data_broadcast_id_name_removed_entries_return_none() {
405        assert_eq!(data_broadcast_id_name(0x00F1), None);
406        assert_eq!(data_broadcast_id_name(0x00F2), None);
407        assert_eq!(data_broadcast_id_name(0x00F3), None);
408        assert_eq!(data_broadcast_id_name(0x00F4), None);
409    }
410
411    #[test]
412    fn data_broadcast_id_name_unknown() {
413        assert_eq!(data_broadcast_id_name(0x0000), None);
414        assert_eq!(data_broadcast_id_name(0xFFFF), None);
415    }
416
417    #[test]
418    fn parse_rejects_wrong_tag() {
419        let err = DataBroadcastIdDescriptor::parse(&[0x65, 0x02, 0x00, 0x0A]).unwrap_err();
420        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x65, .. }));
421    }
422
423    #[test]
424    fn parse_rejects_short_buffer() {
425        let err = DataBroadcastIdDescriptor::parse(&[TAG]).unwrap_err();
426        assert!(matches!(err, Error::BufferTooShort { .. }));
427    }
428
429    #[test]
430    fn parse_rejects_body_too_short() {
431        // length=1: not even the 16-bit id fits.
432        let err = DataBroadcastIdDescriptor::parse(&[TAG, 0x01, 0x00]).unwrap_err();
433        assert!(matches!(err, Error::InvalidDescriptor { .. }));
434    }
435
436    #[test]
437    fn parse_rejects_length_overrun() {
438        // length=5 but only 3 payload bytes available.
439        let err = DataBroadcastIdDescriptor::parse(&[TAG, 0x05, 0x00, 0x0B, 0xAA]).unwrap_err();
440        assert!(matches!(err, Error::BufferTooShort { .. }));
441    }
442
443    #[test]
444    fn raw_round_trip() {
445        let d = DataBroadcastIdDescriptor {
446            data_broadcast_id: 0x0123,
447            id_selector: &[0xDE, 0xAD, 0xBE, 0xEF],
448        };
449        let mut buf = vec![0u8; d.serialized_len()];
450        d.serialize_into(&mut buf).unwrap();
451        let re = DataBroadcastIdDescriptor::parse(&buf).unwrap();
452        assert_eq!(d, re);
453    }
454
455    #[test]
456    fn serialize_rejects_too_small_buffer() {
457        let d = DataBroadcastIdDescriptor {
458            data_broadcast_id: 0x0001,
459            id_selector: &[0x01],
460        };
461        let mut tiny = [0u8; 2];
462        let err = d.serialize_into(&mut tiny).unwrap_err();
463        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
464    }
465
466    #[test]
467    fn serialize_rejects_over_range_body() {
468        // 254 selector bytes + 2 id bytes = 256 > 255.
469        let sel = vec![0u8; 254];
470        let d = DataBroadcastIdDescriptor {
471            data_broadcast_id: 0x0001,
472            id_selector: &sel,
473        };
474        let mut buf = vec![0u8; d.serialized_len()];
475        let err = d.serialize_into(&mut buf).unwrap_err();
476        assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
477    }
478
479    #[cfg(feature = "serde")]
480    #[test]
481    fn serde_serialize_is_stable() {
482        let d = DataBroadcastIdDescriptor {
483            data_broadcast_id: 0x000B,
484            id_selector: &[0x01, 0x02],
485        };
486        let json = serde_json::to_string(&d).unwrap();
487        assert!(json.contains("\"data_broadcast_id\""));
488        assert!(json.contains("\"id_selector\""));
489        assert!(json.contains("11"));
490    }
491
492    // ── SsuIdSelector (data_broadcast_id = 0x000A) ───────────────────────────
493
494    /// Hand-built wire bytes for an SSU id_selector with one OUI entry and
495    /// no private data.
496    ///
497    /// Wire layout (inside the descriptor body, after the 2-byte
498    /// data_broadcast_id field):
499    ///
500    /// ```text
501    /// [0]    OUI_data_length = 0x09  (9 bytes = 1 entry × 6 fixed + 3 selector)
502    /// [1..4] OUI = 00:15:0A
503    /// [4]    0x02 = reserved(4)|update_type(4=0x2, UNT carousel)
504    /// [5]    0x61 = reserved(2)|uvf(1=1)|uversion(5=0x01)
505    ///              i.e. 0b00_1_00001 = 0x21 ... wait:
506    ///              uvf bit5 = 0x20, uversion = 0x01 → 0x21
507    /// [6]    selector_length = 0x03
508    /// [7..10] selector bytes = AA BB CC
509    /// [10]   (OUI loop ends; private_data empty)
510    /// Total = 10 bytes
511    /// ```
512    fn sample_ssu_selector() -> SsuIdSelector<'static> {
513        SsuIdSelector {
514            oui_entries: vec![SsuOuiEntry {
515                oui: [0x00, 0x15, 0x0A],
516                update_type: 0x02, // UNT carousel
517                update_versioning_flag: true,
518                update_version: 0x01,
519                selector: &[0xAA, 0xBB, 0xCC],
520            }],
521            private_data: &[],
522        }
523    }
524
525    #[test]
526    fn ssu_selector_hand_built_byte_anchor() {
527        // OUI_data_length=9 (= 6 fixed + 3 selector), OUI=00:15:0A,
528        // byte3=0x02 (update_type=2), byte4=0x21 (uvf=1,uversion=1),
529        // selector_length=3, AA BB CC.
530        #[rustfmt::skip]
531        let expected: &[u8] = &[
532            0x09,                   // OUI_data_length = 9
533            0x00, 0x15, 0x0A,       // OUI
534            0x02,                   // reserved(0)|update_type(2)
535            0x21,                   // reserved(0)|uvf(1)|uversion(1)
536            0x03,                   // selector_length = 3
537            0xAA, 0xBB, 0xCC,       // selector bytes
538        ];
539        let s = sample_ssu_selector();
540        let mut buf = vec![0u8; s.serialized_len()];
541        s.serialize_into(&mut buf).unwrap();
542        assert_eq!(buf.as_slice(), expected);
543        let re = SsuIdSelector::parse(expected).unwrap();
544        assert_eq!(re, s);
545    }
546
547    #[test]
548    fn ssu_selector_round_trip() {
549        let s = sample_ssu_selector();
550        let mut buf = vec![0u8; s.serialized_len()];
551        s.serialize_into(&mut buf).unwrap();
552        let re = SsuIdSelector::parse(&buf).unwrap();
553        assert_eq!(re, s);
554        let mut buf2 = vec![0u8; re.serialized_len()];
555        re.serialize_into(&mut buf2).unwrap();
556        assert_eq!(buf, buf2, "SSU selector byte-exact re-serialize");
557    }
558
559    #[test]
560    fn ssu_selector_empty_oui_loop() {
561        let s = SsuIdSelector {
562            oui_entries: vec![],
563            private_data: &[0xDE, 0xAD],
564        };
565        let mut buf = vec![0u8; s.serialized_len()];
566        s.serialize_into(&mut buf).unwrap();
567        // OUI_data_length=0, then 2 private bytes.
568        assert_eq!(buf[0], 0x00);
569        assert_eq!(&buf[1..], &[0xDE, 0xAD]);
570        let re = SsuIdSelector::parse(&buf).unwrap();
571        assert_eq!(re, s);
572    }
573
574    #[test]
575    fn ssu_selector_no_versioning_flag() {
576        let s = SsuIdSelector {
577            oui_entries: vec![SsuOuiEntry {
578                oui: [0x00, 0x00, 0x00],
579                update_type: 0x01,
580                update_versioning_flag: false,
581                update_version: 0x00,
582                selector: &[],
583            }],
584            private_data: &[],
585        };
586        let mut buf = vec![0u8; s.serialized_len()];
587        s.serialize_into(&mut buf).unwrap();
588        // Wire layout: [0]=OUI_data_length, [1..4]=OUI, [4]=byte3(update_type),
589        // [5]=byte4(uvf|uversion), [6]=selector_length.
590        assert_eq!(buf[4], 0x01); // byte3: update_type = 1
591        assert_eq!(buf[5], 0x00); // byte4: uvf=0, uversion=0
592        let re = SsuIdSelector::parse(&buf).unwrap();
593        assert_eq!(re, s);
594    }
595
596    /// End-to-end: DataBroadcastIdDescriptor with data_broadcast_id=0x000A
597    /// dispatches to IdSelector::Ssu.
598    ///
599    /// Wire layout (complete descriptor):
600    ///
601    /// ```text
602    /// [0]    tag = 0x66
603    /// [1]    length = 2 + 10 = 12
604    ///           (2 for data_broadcast_id + 10 for SSU selector)
605    /// [2..4] data_broadcast_id = 0x00 0x0A
606    /// [4..14] SSU id_selector (OUI_data_length=9, …)
607    /// ```
608    #[test]
609    fn descriptor_ssu_round_trip() {
610        let sel = sample_ssu_selector();
611        let mut sel_bytes = vec![0u8; sel.serialized_len()];
612        sel.serialize_into(&mut sel_bytes).unwrap();
613        let d = DataBroadcastIdDescriptor {
614            data_broadcast_id: DATA_BROADCAST_ID_SSU,
615            id_selector: &sel_bytes,
616        };
617        let mut buf = vec![0u8; d.serialized_len()];
618        d.serialize_into(&mut buf).unwrap();
619        // Sanity-check descriptor framing.
620        assert_eq!(buf[0], TAG);
621        assert_eq!(buf[1] as usize, ID_LEN + sel.serialized_len());
622        assert_eq!(&buf[2..4], &[0x00, 0x0A]); // data_broadcast_id
623        let re = DataBroadcastIdDescriptor::parse(&buf).unwrap();
624        assert_eq!(re, d);
625        // The raw id_selector decodes to the typed SSU selector.
626        assert_eq!(
627            re.id_selector_decoded().unwrap(),
628            IdSelector::Ssu(sample_ssu_selector())
629        );
630        // Byte-identical re-serialize.
631        let mut buf2 = vec![0u8; re.serialized_len()];
632        re.serialize_into(&mut buf2).unwrap();
633        assert_eq!(buf, buf2, "SSU descriptor byte-exact re-serialize");
634    }
635
636    #[test]
637    fn ssu_selector_parse_rejects_truncated_oui_loop() {
638        // OUI_data_length=6 but only 2 bytes follow.
639        let bytes = &[0x06, 0x00, 0x15];
640        assert!(matches!(
641            SsuIdSelector::parse(bytes).unwrap_err(),
642            Error::InvalidDescriptor { .. }
643        ));
644    }
645
646    #[test]
647    fn ssu_selector_parse_rejects_selector_overflows_oui_loop() {
648        // OUI_data_length=6 (exactly 1 entry with selector_length=0),
649        // but we set selector_length=5 which would overflow the oui loop.
650        #[rustfmt::skip]
651        let bytes = &[
652            0x06,                   // OUI_data_length = 6
653            0x00, 0x15, 0x0A,       // OUI
654            0x02,                   // byte3
655            0x21,                   // byte4
656            0x05,                   // selector_length=5 — overflows OUI loop
657        ];
658        assert!(matches!(
659            SsuIdSelector::parse(bytes).unwrap_err(),
660            Error::InvalidDescriptor { .. }
661        ));
662    }
663
664    #[cfg(feature = "serde")]
665    #[test]
666    fn ssu_descriptor_serde_json() {
667        let sel = sample_ssu_selector();
668        let mut sel_bytes = vec![0u8; sel.serialized_len()];
669        sel.serialize_into(&mut sel_bytes).unwrap();
670        let d = DataBroadcastIdDescriptor {
671            data_broadcast_id: DATA_BROADCAST_ID_SSU,
672            id_selector: &sel_bytes,
673        };
674        let json = serde_json::to_string(&d).unwrap();
675        assert!(json.contains("\"data_broadcast_id\":10"));
676        // The typed SSU fields appear in the decoded selector's JSON.
677        let decoded = d.id_selector_decoded().unwrap();
678        let sj = serde_json::to_string(&decoded).unwrap();
679        assert!(sj.contains("\"update_type\":2"));
680        assert!(sj.contains("\"update_versioning_flag\":true"));
681        assert!(sj.contains("\"update_version\":1"));
682    }
683}