Skip to content

Commit 8fc2097

Browse files
committed
uefi-raw: add unit test for typical high-level net API usage
1 parent a984cca commit 8fc2097

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

uefi-raw/src/net.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,4 +388,46 @@ mod tests {
388388
assert_eq!(uefi_mac_addr.octets(), octets);
389389
}
390390
}
391+
392+
/// Tests the expected flow of types in a higher-level UEFI API.
393+
#[test]
394+
fn test_uefi_flow() {
395+
fn efi_retrieve_efi_ip_addr(addr: *mut IpAddress, is_ipv6: bool) {
396+
let addr = unsafe { addr.as_mut().unwrap() };
397+
// SAFETY: Alignment is guaranteed and memory is initialized.
398+
unsafe {
399+
addr.v4.0[0] = 42;
400+
addr.v4.0[1] = 42;
401+
addr.v4.0[2] = 42;
402+
addr.v4.0[3] = 42;
403+
}
404+
if is_ipv6 {
405+
unsafe {
406+
addr.v6.0[14] = 42;
407+
addr.v6.0[15] = 42;
408+
}
409+
}
410+
}
411+
412+
fn high_level_retrieve_ip(is_ipv6: bool) -> core::net::IpAddr {
413+
let mut efi_ip_addr = IpAddress::ZERO;
414+
efi_retrieve_efi_ip_addr(&mut efi_ip_addr, is_ipv6);
415+
unsafe { efi_ip_addr.into_core_ip_addr(is_ipv6) }
416+
}
417+
418+
let ipv4_addr = high_level_retrieve_ip(false);
419+
let ipv4_addr: core::net::Ipv4Addr = match ipv4_addr {
420+
core::net::IpAddr::V4(ipv4_addr) => ipv4_addr,
421+
core::net::IpAddr::V6(_) => panic!("should not happen"),
422+
};
423+
assert_eq!(ipv4_addr.octets(), [42, 42, 42, 42]);
424+
425+
let ipv6_addr = high_level_retrieve_ip(true);
426+
let ipv6_addr: core::net::Ipv6Addr = match ipv6_addr {
427+
core::net::IpAddr::V6(ipv6_addr) => ipv6_addr,
428+
core::net::IpAddr::V4(_) => panic!("should not happen"),
429+
};
430+
let expected = [42, 42, 42, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 42];
431+
assert_eq!(ipv6_addr.octets(), expected);
432+
}
391433
}

0 commit comments

Comments
 (0)