@@ -478,6 +478,10 @@ pub struct RawHardwareAddress {
478
478
479
479
#[ cfg( any( feature = "medium-ethernet" , feature = "medium-ieee802154" ) ) ]
480
480
impl RawHardwareAddress {
481
+ /// Create a new `RawHardwareAddress` from a byte slice.
482
+ ///
483
+ /// # Panics
484
+ /// Panics if `addr.len() > MAX_HARDWARE_ADDRESS_LEN`.
481
485
pub fn from_bytes ( addr : & [ u8 ] ) -> Self {
482
486
let mut data = [ 0u8 ; MAX_HARDWARE_ADDRESS_LEN ] ;
483
487
data[ ..addr. len ( ) ] . copy_from_slice ( addr) ;
@@ -504,7 +508,7 @@ impl RawHardwareAddress {
504
508
match medium {
505
509
#[ cfg( feature = "medium-ethernet" ) ]
506
510
Medium :: Ethernet => {
507
- if self . len ( ) < 6 {
511
+ if self . len ( ) != 6 {
508
512
return Err ( Error ) ;
509
513
}
510
514
Ok ( HardwareAddress :: Ethernet ( EthernetAddress :: from_bytes (
@@ -513,7 +517,7 @@ impl RawHardwareAddress {
513
517
}
514
518
#[ cfg( feature = "medium-ieee802154" ) ]
515
519
Medium :: Ieee802154 => {
516
- if self . len ( ) < 8 {
520
+ if self . len ( ) != 8 {
517
521
return Err ( Error ) ;
518
522
}
519
523
Ok ( HardwareAddress :: Ieee802154 ( Ieee802154Address :: from_bytes (
@@ -559,3 +563,37 @@ impl From<HardwareAddress> for RawHardwareAddress {
559
563
Self :: from_bytes ( addr. as_bytes ( ) )
560
564
}
561
565
}
566
+
567
+ #[ cfg( test) ]
568
+ mod tests {
569
+ use super :: * ;
570
+ use rstest:: rstest;
571
+
572
+ #[ rstest]
573
+ #[ cfg( feature = "medium-ethernet" ) ]
574
+ #[ case( ( Medium :: Ethernet , & [ 0u8 ; 6 ] [ ..] ) , Ok ( HardwareAddress :: Ethernet ( EthernetAddress ( [ 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ) ) ]
575
+ #[ cfg( feature = "medium-ethernet" ) ]
576
+ #[ case( ( Medium :: Ethernet , & [ 1u8 ; 5 ] [ ..] ) , Err ( Error ) ) ]
577
+ #[ cfg( feature = "medium-ethernet" ) ]
578
+ #[ case( ( Medium :: Ethernet , & [ 1u8 ; 7 ] [ ..] ) , Err ( Error ) ) ]
579
+ #[ cfg( feature = "medium-ieee802154" ) ]
580
+ #[ case( ( Medium :: Ieee802154 , & [ 0u8 ; 8 ] [ ..] ) , Ok ( HardwareAddress :: Ieee802154 ( Ieee802154Address :: Extended ( [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ) ) ) ) ]
581
+ #[ cfg( feature = "medium-ieee802154" ) ]
582
+ #[ case( ( Medium :: Ieee802154 , & [ 1u8 ; 2 ] [ ..] ) , Err ( Error ) ) ]
583
+ #[ cfg( feature = "medium-ieee802154" ) ]
584
+ #[ case( ( Medium :: Ieee802154 , & [ 1u8 ; 1 ] [ ..] ) , Err ( Error ) ) ]
585
+ fn parse_hardware_address (
586
+ #[ case] input : ( Medium , & [ u8 ] ) ,
587
+ #[ case] expected : Result < HardwareAddress > ,
588
+ ) {
589
+ let ( medium, input) = input;
590
+
591
+ // NOTE: we check the length since `RawHardwareAddress::parse()` panics if the length is
592
+ // invalid. MAX_HARDWARE_ADDRESS_LEN is based on the medium, and depending on the feature
593
+ // flags, it can be different.
594
+ if input. len ( ) < MAX_HARDWARE_ADDRESS_LEN {
595
+ let raw = RawHardwareAddress :: from_bytes ( input) ;
596
+ assert_eq ! ( raw. parse( medium) , expected) ;
597
+ }
598
+ }
599
+ }
0 commit comments