add into_inner (#5)

* add into_inner

* remove redundant super::*
This commit is contained in:
Hubert 2022-06-24 13:49:10 +02:00 committed by GitHub
parent f3dc6b118d
commit 317b145bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,6 +83,11 @@ impl WolPacket {
Ok(()) Ok(())
} }
/// Returns the underlying WoL packet bytes
pub fn into_inner(self) -> Packet {
self.packet
}
/// Converts string representation of MAC address (e.x. 00:01:02:03:04:05) to raw bytes. /// Converts string representation of MAC address (e.x. 00:01:02:03:04:05) to raw bytes.
/// # Panic /// # Panic
/// Panics when input MAC is invalid (i.e. contains non-byte characters) /// Panics when input MAC is invalid (i.e. contains non-byte characters)
@ -125,37 +130,36 @@ impl WolPacket {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*;
#[test] #[test]
fn extend_mac_test() { fn extend_mac_test() {
let mac = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06]; let mac = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06];
let extended_mac = super::WolPacket::extend_mac(&mac); let extended_mac = WolPacket::extend_mac(&mac);
assert_eq!(extended_mac.len(), super::MAC_PER_MAGIC * super::MAC_SIZE); assert_eq!(extended_mac.len(), MAC_PER_MAGIC * MAC_SIZE);
assert_eq!( assert_eq!(&extended_mac[(MAC_PER_MAGIC - 1) * MAC_SIZE..], &mac[..]);
&extended_mac[(super::MAC_PER_MAGIC - 1) * super::MAC_SIZE..],
&mac[..]
);
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn extend_mac_mac_too_long_test() { fn extend_mac_mac_too_long_test() {
let mac = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]; let mac = vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07];
super::WolPacket::extend_mac(&mac); WolPacket::extend_mac(&mac);
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn extend_mac_mac_too_short_test() { fn extend_mac_mac_too_short_test() {
let mac = vec![0x01, 0x02, 0x03, 0x04, 0x05]; let mac = vec![0x01, 0x02, 0x03, 0x04, 0x05];
super::WolPacket::extend_mac(&mac); WolPacket::extend_mac(&mac);
} }
#[test] #[test]
fn mac_to_byte_test() { fn mac_to_byte_test() {
let mac = "01:02:03:04:05:06"; let mac = "01:02:03:04:05:06";
let result = super::WolPacket::mac_to_byte(mac, ':'); let result = WolPacket::mac_to_byte(mac, ':');
assert_eq!( assert_eq!(
result.into_inner().unwrap(), result.into_inner().unwrap(),
@ -167,38 +171,51 @@ mod tests {
#[should_panic] #[should_panic]
fn mac_to_byte_invalid_chars_test() { fn mac_to_byte_invalid_chars_test() {
let mac = "ZZ:02:03:04:05:06"; let mac = "ZZ:02:03:04:05:06";
super::WolPacket::mac_to_byte(mac, ':'); WolPacket::mac_to_byte(mac, ':');
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn mac_to_byte_invalid_separator_test() { fn mac_to_byte_invalid_separator_test() {
let mac = "01002:03:04:05:06"; let mac = "01002:03:04:05:06";
super::WolPacket::mac_to_byte(mac, ':'); WolPacket::mac_to_byte(mac, ':');
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn mac_to_byte_mac_too_long_test() { fn mac_to_byte_mac_too_long_test() {
let mac = "01:02:03:04:05:06:07"; let mac = "01:02:03:04:05:06:07";
super::WolPacket::mac_to_byte(mac, ':'); WolPacket::mac_to_byte(mac, ':');
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn mac_to_byte_mac_too_short_test() { fn mac_to_byte_mac_too_short_test() {
let mac = "01:02:03:04:05"; let mac = "01:02:03:04:05";
super::WolPacket::mac_to_byte(mac, ':'); WolPacket::mac_to_byte(mac, ':');
} }
#[test] #[test]
fn create_packet_bytes_test() { fn create_packet_bytes_test() {
let bytes = super::WolPacket::create_packet_bytes(&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]); let bytes = WolPacket::create_packet_bytes(&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
assert_eq!( assert_eq!(bytes.len(), MAC_SIZE * MAC_PER_MAGIC + HEADER.len());
bytes.len(),
super::MAC_SIZE * super::MAC_PER_MAGIC + super::HEADER.len()
);
assert!(bytes.iter().all(|&x| x == 0xFF)); assert!(bytes.iter().all(|&x| x == 0xFF));
} }
#[test]
fn create_wol_packet() {
let mac = vec![0x00, 0x01, 0x02, 0x03, 0x04, 0x05];
let wol = WolPacket::from_bytes(&mac);
let packet = wol.into_inner();
assert_eq!(packet.len(), PACKET_LEN);
assert_eq!(
[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
&packet[0..HEADER.len()]
);
for offset in (HEADER.len()..PACKET_LEN).step_by(MAC_SIZE) {
assert_eq!(&mac, &packet[offset..offset + MAC_SIZE]);
}
}
} }