dmidecode 1.0.1

Decode SMBIOS/DMI information into accessible data structures
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
//! System Enclosure or Chassis (Type 3)
//!
//! The information in this structure defines attributes of the system’s mechanical
//! enclosure(s). For example, if a system included a separate enclosure for its peripheral
//! devices, two structures would be returned: one for the main system enclosure and the second for
//! the peripheral device enclosure.

use core::fmt;
use core::hash::{Hash, Hasher};
use core::slice::Chunks;

use crate::{HeaderPacked, MalformedStructureError, RawStructure};

/// System Enclosure or Chassis structure
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Enclosure<'buffer> {
    /// Specifies the structure’s handle
    pub handle: u16,
    /// Manufacturer string is non-null
    pub manufacturer: &'buffer str,
    /// Chassis lock is present
    pub chassis_lock: bool,
    /// Type field identifies the type of enclosure. (Unknown is disallowed.)
    pub enclosure_type: EnclosureType,
    /// Version
    pub version: &'buffer str,
    /// Serial Number
    pub serial_number: &'buffer str,
    /// Asset Tag Number
    pub asset_tag_number: &'buffer str,
    /// State of the enclosure when it was last booted;
    pub boot_up_state: Option<State>,
    /// State of the enclosure’s power supply (or supplies) when last booted
    pub power_supply_state: Option<State>,
    /// Thermal state of the enclosure when last booted
    pub thermal_state: Option<State>,
    /// Physical security status of the enclosure when last booted
    pub security_status: Option<SecurityStatus>,
    /// OEM- or BIOS vendor-specific information
    pub oem_defined: Option<u32>,
    /// Height of the enclosure , in 'U's A U is a standard unit of measure for the height of a
    /// rack or rack-mountable component and is equal to 1.75 inches or 4.445 cm. A value of 00h
    /// indicates that the enclosure height is unspecified.
    pub height: Option<u8>,
    /// Number of power cords associated with the enclosure or chassis A value of 00h indicates
    /// that the number is unspecified.
    pub power_cords_number: Option<u8>,
    /// Each Contained Element record consists of sub-fields that further describe elements
    /// contained by the chassis
    pub contained_elements: Option<ContainedElements<'buffer>>,
    /// Number of null-terminated string describing the chassis or enclosure SKU number
    pub sku_number: Option<&'buffer str>,
}

/// System Enclosure or Chassis Type
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum EnclosureType {
    Other,
    Unknown,
    Desktop,
    LowProfileDesktop,
    PizzaBox,
    MiniTower,
    Tower,
    Portable,
    Laptop,
    Notebook,
    HandHeld,
    DockingStation,
    AllInOne,
    SubNotebook,
    SpaceSaving,
    LunchBox,
    MainServerChassis,
    ExpansionChassis,
    SubChassis,
    BusExpansionChassis,
    PeripheralChassis,
    RaidChassis,
    RackMountChassis,
    SealedCasePc,
    /// When this value is specified by an SMBIOS implementation, the physical chassis associated
    /// with this structure supports multiple, independently reporting physical systems—regardless
    /// of the chassis' current configuration. Systems in the same physical chassis are required to
    /// report the same value in this structure's Serial Number field.  For a chassis that may also
    /// be configured as either a single system or multiple physical systems, the Multi-system
    /// chassis value is reported even if the chassis is currently configured as a single system.
    /// This allows management applications to recognize the multisystem potential of the chassis.
    MultiSystemChassis,
    CompactPci,
    AdvancedTca,
    /// An SMBIOS implementation for a Blade would contain a Type 3 Chassis structure for the
    /// individual Blade system as well as one for the Blade Enclosure that completes the Blade
    /// system.
    Blade,
    /// A Blade Enclosure is a specialized chassis that contains a set of Blades. It provides much
    /// of the non-core computing infrastructure for a set of Blades (power, cooling, networking,
    /// etc.). A Blade Enclosure may itself reside inside a Rack or be a standalone chassis.
    BladeEnclosure,
    Tablet,
    Convertible,
    Detachable,
    IotGateway,
    EmbeddedPc,
    MiniPc,
    StickPc,
    Undefined(u8),
}

/// System Enclosure or Chassis States
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum State {
    Other,
    Unknown,
    Safe,
    Warning,
    Critical,
    NonRecoverable,
    Undefined(u8),
}

/// System Enclosure or Chassis Security Status
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum SecurityStatus {
    Other,
    Unknown,
    None,
    ExternalInterfaceLockedOut,
    ExternalInterfaceEnabled,
    Undefined(u8),
}

/// Elements, possibly defined by other SMBIOS structures, present in this chassis
#[derive(Clone, Debug)]
pub struct ContainedElements<'buffer> {
    chunks: Chunks<'buffer, u8>,
    count: u8,
    record_length: u8,
}

/// Each Contained Element record consists of sub-fields that further describe elements contained
/// by the chassis.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct ContainedElement {
    /// Specifies the type of element associated with this record
    type_: ContainedElementType,
    /// Specifies the minimum number of the element type that can be installed in the chassis for
    /// the chassis to properly operate, in the range 0 to 254. The value 255 (0FFh) is reserved
    /// for future definition.
    minimum: u8,
    /// Specifies the maximum number of the element type that can be installed in the chassis, in
    /// the range 1 to 255. The value 0 is reserved for future definition.
    maximum: u8,
}

/// Identifies whether the Type contains an SMBIOS Baseboard Type enumeration or an SMBIOS
/// structure type enumeration.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum ContainedElementType {
    BoardType(crate::baseboard::BoardType),
    InfoType(crate::InfoType),
}

impl<'buffer> Enclosure<'buffer> {
    pub(crate) fn try_from(structure: RawStructure<'buffer>) -> Result<Enclosure<'buffer>, MalformedStructureError> {
        #[repr(C)]
        #[repr(packed)]
        struct EnclosurePacked2_0 {
            manufacturer: u8,
            enclosure_type: u8,
            version: u8,
            serial_number: u8,
            asset_tag_number: u8,
        }

        // compile time assertion that our minimum enclosure structure
        // fits the minimum required by the BIOS spec
        const _: () = {
            assert!(core::mem::size_of::<EnclosurePacked2_0>() + core::mem::size_of::<HeaderPacked>() == 0x09);
        };

        struct RawEnclosureType(u8);
        impl RawEnclosureType {
            fn new(byte: u8) -> Self {
                RawEnclosureType(byte)
            }
            fn get_lock(&self) -> bool {
                (self.0 & 0b1000_0000) != 0
            }
            fn get_type(&self) -> EnclosureType {
                (self.0 & 0b0111_1111).into()
            }
        }

        if structure.data.len() < core::mem::size_of::<EnclosurePacked2_0>() {
            return Err(crate::MalformedStructureError::InvalidFormattedSectionLength(
                structure.info,
                structure.handle,
                "minimum of ",
                core::mem::size_of::<EnclosurePacked2_0>() as u8,
            ));
        }

        let (minimum, mut extra) = structure.data.split_at(core::mem::size_of::<EnclosurePacked2_0>());
        let_as_struct!(packed, EnclosurePacked2_0, minimum);
        let enclosure_type = RawEnclosureType::new(packed.enclosure_type);
        let mut enclosure = Enclosure {
            handle: structure.handle,
            manufacturer: structure.find_string(packed.manufacturer)?,
            chassis_lock: enclosure_type.get_lock(),
            enclosure_type: enclosure_type.get_type(),
            version: structure.find_string(packed.version)?,
            serial_number: structure.find_string(packed.serial_number)?,
            asset_tag_number: structure.find_string(packed.asset_tag_number)?,
            boot_up_state: None,
            power_supply_state: None,
            thermal_state: None,
            security_status: None,
            oem_defined: None,
            height: None,
            power_cords_number: None,
            contained_elements: None,
            sku_number: None,
        };
        let data = &mut extra;

        // Optional 2.1+ fields
        let sku_number = read_bytes(data)
            .and_then(|boot_up_state: u8| {
                enclosure.boot_up_state = Some(boot_up_state.into());
                read_bytes(data)
            })
            .and_then(|power_supply_state: u8| {
                enclosure.power_supply_state = Some(power_supply_state.into());
                read_bytes(data)
            })
            .and_then(|thermal_state: u8| {
                enclosure.thermal_state = Some(thermal_state.into());
                read_bytes(data)
            })
            .and_then(|security_status: u8| {
                enclosure.security_status = Some(security_status.into());
                read_bytes(data)
            })
            // Optional 2.3+ fields
            .and_then(|oem_defined: u32| {
                enclosure.oem_defined = Some(u32::from_le(oem_defined));
                read_bytes(data)
            })
            .and_then(|height: u8| {
                enclosure.height = Some(height);
                read_bytes(data)
            })
            .and_then(|power_cords_number: u8| {
                enclosure.power_cords_number = Some(power_cords_number);
                ContainedElements::new(data)
            })
            .and_then(|contained_elements| {
                enclosure.contained_elements = Some(contained_elements);
                read_bytes(data)
            });

        // Optional 2.7+ fields
        if let Some(sku_number) = sku_number {
            enclosure.sku_number = Some(structure.find_string(sku_number)?);
        }

        Ok(enclosure)
    }
}

impl From<u8> for EnclosureType {
    fn from(byte: u8) -> EnclosureType {
        match byte {
            0x01 => EnclosureType::Other,
            0x02 => EnclosureType::Unknown,
            0x03 => EnclosureType::Desktop,
            0x04 => EnclosureType::LowProfileDesktop,
            0x05 => EnclosureType::PizzaBox,
            0x06 => EnclosureType::MiniTower,
            0x07 => EnclosureType::Tower,
            0x08 => EnclosureType::Portable,
            0x09 => EnclosureType::Laptop,
            0x0A => EnclosureType::Notebook,
            0x0B => EnclosureType::HandHeld,
            0x0C => EnclosureType::DockingStation,
            0x0D => EnclosureType::AllInOne,
            0x0E => EnclosureType::SubNotebook,
            0x0F => EnclosureType::SpaceSaving,
            0x10 => EnclosureType::LunchBox,
            0x11 => EnclosureType::MainServerChassis,
            0x12 => EnclosureType::ExpansionChassis,
            0x13 => EnclosureType::SubChassis,
            0x14 => EnclosureType::BusExpansionChassis,
            0x15 => EnclosureType::PeripheralChassis,
            0x16 => EnclosureType::RaidChassis,
            0x17 => EnclosureType::RackMountChassis,
            0x18 => EnclosureType::SealedCasePc,
            0x19 => EnclosureType::MultiSystemChassis,
            0x1A => EnclosureType::CompactPci,
            0x1B => EnclosureType::AdvancedTca,
            0x1C => EnclosureType::Blade,
            0x1D => EnclosureType::BladeEnclosure,
            0x1E => EnclosureType::Tablet,
            0x1F => EnclosureType::Convertible,
            0x20 => EnclosureType::Detachable,
            0x21 => EnclosureType::IotGateway,
            0x22 => EnclosureType::EmbeddedPc,
            0x23 => EnclosureType::MiniPc,
            0x24 => EnclosureType::StickPc,
            v => Self::Undefined(v),
        }
    }
}
impl fmt::Display for EnclosureType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other => write!(f, "Other"),
            Self::Unknown => write!(f, "Unknown"),
            Self::Desktop => write!(f, "Desktop"),
            Self::LowProfileDesktop => write!(f, "Low Profile Desktop"),
            Self::PizzaBox => write!(f, "Pizza Box"),
            Self::MiniTower => write!(f, "Mini Tower"),
            Self::Tower => write!(f, "Tower"),
            Self::Portable => write!(f, "Portable"),
            Self::Laptop => write!(f, "Laptop"),
            Self::Notebook => write!(f, "Notebook"),
            Self::HandHeld => write!(f, "Hand Held"),
            Self::DockingStation => write!(f, "Docking Station"),
            Self::AllInOne => write!(f, "All in One"),
            Self::SubNotebook => write!(f, "Sub Notebook"),
            Self::SpaceSaving => write!(f, "Space-saving"),
            Self::LunchBox => write!(f, "Lunch Box"),
            Self::MainServerChassis => write!(f, "Main Server Chassis"),
            Self::ExpansionChassis => write!(f, "Expansion Chassis"),
            Self::SubChassis => write!(f, "SubChassis"),
            Self::BusExpansionChassis => write!(f, "Bus Expansion Chassis"),
            Self::PeripheralChassis => write!(f, "Peripheral Chassis"),
            Self::RaidChassis => write!(f, "RAID Chassis"),
            Self::RackMountChassis => write!(f, "Rack Mount Chassis"),
            Self::SealedCasePc => write!(f, "Sealed-case PC"),
            Self::MultiSystemChassis => write!(f, "Multi-system chassis"),
            Self::CompactPci => write!(f, "Compact PCI"),
            Self::AdvancedTca => write!(f, "Advanced TCA"),
            Self::Blade => write!(f, "Blade"),
            Self::BladeEnclosure => write!(f, "Blade Enclosure"),
            Self::Tablet => write!(f, "Tablet"),
            Self::Convertible => write!(f, "Convertible"),
            Self::Detachable => write!(f, "Detachable"),
            Self::IotGateway => write!(f, "IoT Gateway"),
            Self::EmbeddedPc => write!(f, "Embedded PC"),
            Self::MiniPc => write!(f, "Mini PC"),
            Self::StickPc => write!(f, "Stick PC"),
            Self::Undefined(v) => write!(f, "Undefined: {v}"),
        }
    }
}

impl From<u8> for State {
    fn from(byte: u8) -> State {
        match byte {
            0x01 => Self::Other,
            0x02 => Self::Unknown,
            0x03 => Self::Safe,
            0x04 => Self::Warning,
            0x05 => Self::Critical,
            0x06 => Self::NonRecoverable,
            v => Self::Undefined(v),
        }
    }
}
impl fmt::Display for State {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other => write!(f, "Other"),
            Self::Unknown => write!(f, "Unknown"),
            Self::Safe => write!(f, "Safe"),
            Self::Warning => write!(f, "Warning"),
            Self::Critical => write!(f, "Critical"),
            Self::NonRecoverable => write!(f, "Non-recoverable"),
            Self::Undefined(v) => write!(f, "Undefined: {v}"),
        }
    }
}

impl From<u8> for SecurityStatus {
    fn from(byte: u8) -> SecurityStatus {
        match byte {
            0x01 => Self::Other,
            0x02 => Self::Unknown,
            0x03 => Self::None,
            0x04 => Self::ExternalInterfaceLockedOut,
            0x05 => Self::ExternalInterfaceEnabled,
            v => Self::Undefined(v),
        }
    }
}
impl fmt::Display for SecurityStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Other => write!(f, "Other"),
            Self::Unknown => write!(f, "Unknown"),
            Self::None => write!(f, "None"),
            Self::ExternalInterfaceLockedOut => write!(f, "External interface locked out"),
            Self::ExternalInterfaceEnabled => write!(f, "External interface enabled"),
            Self::Undefined(v) => write!(f, "Undefined: {v}"),
        }
    }
}

impl<'buffer> ContainedElements<'buffer> {
    fn new(data: &mut &'buffer [u8]) -> Option<Self> {
        let count: u8 = read_bytes(data)?;
        let record_length: u8 = read_bytes(data)?;

        if count == 0 || record_length == 0 {
            return Some(Self {
                chunks: [].chunks(usize::MAX),
                count,
                record_length,
            });
        }

        let length = (count * record_length) as usize;
        let chunks = data.get(0..length)?.chunks(record_length as usize);
        *data = &data[length..];
        Some(Self {
            chunks,
            count,
            record_length,
        })
    }

    pub fn count(&self) -> u8 {
        self.count
    }
}

impl PartialEq for ContainedElements<'_> {
    fn eq(&self, other: &Self) -> bool {
        self.chunks.clone().eq(other.chunks.clone())
            && self.count == other.count
            && self.record_length == other.record_length
    }
}
impl Eq for ContainedElements<'_> {}
impl Hash for ContainedElements<'_> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.chunks.clone().for_each(|c| c.hash(state));
        self.count.hash(state);
        self.record_length.hash(state);
    }
}
impl Iterator for ContainedElements<'_> {
    type Item = ContainedElement;

    fn next(&mut self) -> Option<Self::Item> {
        self.chunks.next().map(|a| a.into())
    }
}

impl From<&[u8]> for ContainedElement {
    fn from(data: &[u8]) -> ContainedElement {
        #[repr(C)]
        #[repr(packed)]
        struct ContainedElement2_3 {
            type_: u8,
            minimum: u8,
            maximum: u8,
        }
        let_as_struct!(packed, ContainedElement2_3, data);
        ContainedElement {
            type_: packed.type_.into(),
            minimum: packed.minimum,
            maximum: packed.maximum,
        }
    }
}
impl fmt::Display for ContainedElement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} ({}-{})", self.type_, self.minimum, self.maximum)
    }
}

impl From<u8> for ContainedElementType {
    fn from(byte: u8) -> ContainedElementType {
        let val = byte & 0b0111_1111;
        if byte & 0b1000_0000 == 0 {
            Self::BoardType(val.into())
        } else {
            Self::InfoType(val.into())
        }
    }
}
impl fmt::Display for ContainedElementType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::BoardType(board) => write!(f, "Baseboard type: {board}"),
            Self::InfoType(info) => write!(f, "Structure type: {info}"),
        }
    }
}

fn read_bytes<T: Copy>(data: &mut &[u8]) -> Option<T> {
    if data.len() < core::mem::size_of::<T>() {
        return None;
    }

    let value = unsafe { core::ptr::read_unaligned((*data).as_ptr() as *const T) };
    *data = &data[core::mem::size_of::<T>()..];
    Some(value)
}

#[cfg(test)]
mod tests {
    use std::prelude::v1::*;
    #[test]
    fn enclosure_type() {
        use super::EnclosureType::*;
        for i in 1..=0x24 {
            let (e, s) = match i {
                0x01 => (Other, "Other".into()),
                0x09 => (Laptop, "Laptop".into()),
                0x18 => (SealedCasePc, "Sealed-case PC".into()),
                0x22 => (EmbeddedPc, "Embedded PC".into()),
                v @ 0xF0..=0xFF => (Undefined(v), format!("Undefined: {v}")),
                _ => continue,
            };
            assert_eq!(e, i.into(), "{i:#x}");
            assert_eq!(s, format!("{e}"));
        }
    }

    #[test]
    fn state() {
        use super::State::*;
        for i in 0..=0xFF {
            let (e, s) = match i {
                0x01 => (Other, "Other".into()),
                0x04 => (Warning, "Warning".into()),
                0x06 => (NonRecoverable, "Non-recoverable".into()),
                v @ 0xF0..=0xFF => (Undefined(v), format!("Undefined: {v}")),
                _ => continue,
            };
            assert_eq!(e, i.into(), "{i:#x}");
            assert_eq!(s, format!("{e}"));
        }
    }

    #[test]
    fn security_status() {
        use super::SecurityStatus::*;
        for i in 0..=0xFF {
            let (e, s) = match i {
                0x01 => (Other, "Other".into()),
                0x03 => (None, "None".into()),
                0x05 => (ExternalInterfaceEnabled, "External interface enabled".into()),
                v @ 0xF0..=0xFF => (Undefined(v), format!("Undefined: {v}")),
                _ => continue,
            };
            assert_eq!(e, i.into(), "{i:#x}");
            assert_eq!(s, format!("{e}"));
        }
    }

    #[test]
    fn contained_element() {
        use super::{ContainedElement, ContainedElementType};
        let data = &[
            // Type contains an SMBIOS structure type
            (
                [0b1000_1001, 1, 2],
                ContainedElement {
                    type_: ContainedElementType::InfoType(crate::InfoType::SystemSlots),
                    minimum: 1,
                    maximum: 2,
                },
                "Structure type: System Slots (1-2)",
            ),
            // Type contains an SMBIOS Baseboard Type
            (
                [0b0000_0100, 1, 2],
                ContainedElement {
                    type_: ContainedElementType::BoardType(crate::baseboard::BoardType::ConnectivitySwitch),
                    minimum: 1,
                    maximum: 2,
                },
                "Baseboard type: Connectivity Switch (1-2)",
            ),
        ];
        for (array, contained_element, display) in data {
            let v = &ContainedElement::from(&array[..]);
            assert_eq!(contained_element, v);
            assert_eq!(format!("{display}"), format!("{}", v));
        }
    }

    #[test]
    fn contained_elements() {
        use super::{ContainedElement, ContainedElementType, ContainedElements};
        let structure_data = [
            0x02, // count = 2
            0x03, // length = 3
            0x91, 0x01, 0x02, 0x07, 0x03, 0x04, // 6 bytes of elements
            0x03, // remaining
        ];
        let mut data: &[u8] = &structure_data;
        let mut contained_elements = ContainedElements::new(&mut data).expect("should not be empty");
        if let Some(el) = contained_elements.next() {
            assert_eq!(
                ContainedElement {
                    type_: ContainedElementType::InfoType(crate::InfoType::MemoryDevice),
                    minimum: 1,
                    maximum: 2
                },
                el
            );
        }
        if let Some(el) = contained_elements.next() {
            assert_eq!(
                ContainedElement {
                    type_: ContainedElementType::BoardType(crate::baseboard::BoardType::IoModule),
                    minimum: 3,
                    maximum: 4
                },
                el
            );
        }
        assert_eq!(contained_elements.next(), None);
        // the data cursor was updated to the remaining bytes
        assert_eq!(data, &structure_data[8..]);
    }

    #[test]
    fn dmi_bin() {
        use super::*;
        const DMIDECODE_BIN: &[u8] = include_bytes!("../../tests/data/dmi.0.bin");
        let entry_point = crate::EntryPoint::search(DMIDECODE_BIN).unwrap();
        let enc = entry_point
            .structures(&DMIDECODE_BIN[(entry_point.smbios_address() as usize)..])
            .find_map(|s| {
                if let Ok(crate::Structure::Enclosure(enc)) = s {
                    Some(enc)
                } else {
                    None
                }
            })
            .unwrap();
        let sample = Enclosure {
            handle: 768,
            manufacturer: "Dell Inc.",
            chassis_lock: true,
            enclosure_type: EnclosureType::RackMountChassis,
            version: "",
            serial_number: "XXXXXXX",
            asset_tag_number: "",
            boot_up_state: Some(State::Safe),
            power_supply_state: Some(State::Safe),
            thermal_state: Some(State::Safe),
            security_status: Some(SecurityStatus::Unknown),
            oem_defined: Some(0x01010101),
            height: Some(2),
            power_cords_number: Some(2),
            contained_elements: Some(ContainedElements {
                chunks: [145, 1, 2, 3, 255, 0].chunks(3),
                count: 2,
                record_length: 3,
            }),
            sku_number: Some("SKU Number"),
        };

        assert_eq!(sample, enc);
        assert_eq!(format!("{}", enc.manufacturer), "Dell Inc.", "Manufacturer");
        assert_eq!(format!("{}", enc.enclosure_type), "Rack Mount Chassis", "Type");
        assert_eq!(format!("{}", enc.chassis_lock), "true", "Lock");
        assert_eq!(format!("{}", enc.version), "", "Version");
        assert_eq!(format!("{}", enc.serial_number), "XXXXXXX", "Serial Number");
        assert_eq!(format!("{}", enc.asset_tag_number), "", "Asset Tag");
        assert_eq!(
            enc.boot_up_state.map(|v| format!("{v}")),
            Some("Safe".into()),
            "Boot-up State"
        );
        assert_eq!(
            enc.power_supply_state.map(|v| format!("{v}")),
            Some("Safe".into()),
            "Power Supply State"
        );
        assert_eq!(
            enc.thermal_state.map(|v| format!("{v}")),
            Some("Safe".into()),
            "Thermal State"
        );
        assert_eq!(
            enc.security_status.map(|v| format!("{v}")),
            Some("Unknown".into()),
            "Security Status"
        );
        assert_eq!(
            enc.oem_defined.map(|v| format!("{v:#010X}")),
            Some("0x01010101".into()),
            "OEM Information"
        );
        assert_eq!(enc.height, Some(2), "Height");
        assert_eq!(enc.power_cords_number, Some(2), "Number Of Power Cords");
        assert_eq!(
            enc.contained_elements
                .clone()
                .and_then(|mut ce| ce.next().map(|s| format!("{s}"))),
            Some("Structure type: Memory Device (1-2)".into()),
            "Number Of Power Cords"
        );
        assert_eq!(
            enc.contained_elements
                .clone()
                .and_then(|mut ce| ce.nth(1).map(|s| format!("{s}"))),
            Some("Baseboard type: Server Blade (255-0)".into()),
            "Number Of Power Cords"
        );
        assert_eq!(
            enc.sku_number.map(|v| v.to_string()),
            Some("SKU Number".into()),
            "SKU Number"
        );
    }

    #[test]
    fn no_sku_on_3_14() {
        use super::*;

        let enclosure = Enclosure::try_from(RawStructure {
            version: crate::SmbiosVersion { major: 3, minor: 14 },
            info: crate::InfoType::Enclosure,
            length: 20,
            handle: 153,
            data: &[1, 1, 0, 0, 0, 3, 3, 3, 2, 0, 0, 0, 0, 0, 0, 0],
            strings: &[71, 111, 111, 103, 108, 101, 0, 0],
        })
        .expect("failed to create enclosure");

        assert_eq!(
            enclosure,
            Enclosure {
                handle: 153,
                manufacturer: "Google",
                chassis_lock: false,
                enclosure_type: EnclosureType::Other,
                version: "",
                serial_number: "",
                asset_tag_number: "",
                boot_up_state: Some(State::Safe),
                power_supply_state: Some(State::Safe),
                thermal_state: Some(State::Safe),
                security_status: Some(SecurityStatus::Unknown),
                oem_defined: Some(0),
                height: Some(0),
                power_cords_number: Some(0),
                contained_elements: None,
                sku_number: None
            }
        )
    }
}