Skip to content

Commit 684c89f

Browse files
committed
uefi-raw: add unit test for typical high-level net API usage
1 parent b430c82 commit 684c89f

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

0 commit comments

Comments
 (0)