Skip to content

Commit 3831e05

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

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
@@ -403,4 +403,46 @@ mod tests {
403403
assert_eq!(uefi_mac_addr.octets(), octets);
404404
}
405405
}
406+
407+
/// Tests the expected flow of types in a higher-level UEFI API.
408+
#[test]
409+
fn test_uefi_flow() {
410+
fn efi_retrieve_efi_ip_addr(addr: *mut IpAddress, is_ipv6: bool) {
411+
let addr = unsafe { addr.as_mut().unwrap() };
412+
// SAFETY: Alignment is guaranteed and memory is initialized.
413+
unsafe {
414+
addr.v4.0[0] = 42;
415+
addr.v4.0[1] = 42;
416+
addr.v4.0[2] = 42;
417+
addr.v4.0[3] = 42;
418+
}
419+
if is_ipv6 {
420+
unsafe {
421+
addr.v6.0[14] = 42;
422+
addr.v6.0[15] = 42;
423+
}
424+
}
425+
}
426+
427+
fn high_level_retrieve_ip(is_ipv6: bool) -> core::net::IpAddr {
428+
let mut efi_ip_addr = IpAddress::ZERO;
429+
efi_retrieve_efi_ip_addr(&mut efi_ip_addr, is_ipv6);
430+
unsafe { efi_ip_addr.into_core_ip_addr(is_ipv6) }
431+
}
432+
433+
let ipv4_addr = high_level_retrieve_ip(false);
434+
let ipv4_addr: core::net::Ipv4Addr = match ipv4_addr {
435+
core::net::IpAddr::V4(ipv4_addr) => ipv4_addr,
436+
core::net::IpAddr::V6(_) => panic!("should not happen"),
437+
};
438+
assert_eq!(ipv4_addr.octets(), [42, 42, 42, 42]);
439+
440+
let ipv6_addr = high_level_retrieve_ip(true);
441+
let ipv6_addr: core::net::Ipv6Addr = match ipv6_addr {
442+
core::net::IpAddr::V6(ipv6_addr) => ipv6_addr,
443+
core::net::IpAddr::V4(_) => panic!("should not happen"),
444+
};
445+
let expected = [42, 42, 42, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 42];
446+
assert_eq!(ipv6_addr.octets(), expected);
447+
}
406448
}

0 commit comments

Comments
 (0)