Skip to content

Commit 0d4fe50

Browse files
committed
uefi-raw: add unit test for typical high-level net API usage
1 parent 9b81506 commit 0d4fe50

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

uefi-raw/src/net.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,45 @@ mod tests {
397397
assert_eq!(uefi_mac_addr.octets(), octets);
398398
}
399399
}
400+
401+
/// Tests the expected flow of types in a higher-level UEFI API.
402+
#[test]
403+
fn test_uefi_flow() {
404+
fn efi_retrieve_efi_ip_addr(addr: &mut IpAddress, is_ipv6: bool) {
405+
// SAFETY: Alignment is guaranteed and memory is initialized.
406+
unsafe {
407+
addr.v4.0[0] = 42;
408+
addr.v4.0[1] = 42;
409+
addr.v4.0[2] = 42;
410+
addr.v4.0[3] = 42;
411+
}
412+
if is_ipv6 {
413+
unsafe {
414+
addr.v6.0[14] = 42;
415+
addr.v6.0[15] = 42;
416+
}
417+
}
418+
}
419+
420+
fn high_level_retrieve_ip(is_ipv6: bool) -> StdIpAddr {
421+
let mut efi_ip_addr = IpAddress::new_zeroed();
422+
efi_retrieve_efi_ip_addr(&mut efi_ip_addr, is_ipv6);
423+
unsafe { efi_ip_addr.into_std_ip_addr(is_ipv6) }
424+
}
425+
426+
let ipv4_addr = high_level_retrieve_ip(false);
427+
let ipv4_addr: StdIpv4Addr = match ipv4_addr {
428+
StdIpAddr::V4(ipv4_addr) => ipv4_addr,
429+
StdIpAddr::V6(_) => panic!("should not happen"),
430+
};
431+
assert_eq!(ipv4_addr.octets(), [42, 42, 42, 42]);
432+
433+
let ipv6_addr = high_level_retrieve_ip(true);
434+
let ipv6_addr: StdIpv6Addr = match ipv6_addr {
435+
StdIpAddr::V6(ipv6_addr) => ipv6_addr,
436+
StdIpAddr::V4(_) => panic!("should not happen"),
437+
};
438+
let expected = [42, 42, 42, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 42];
439+
assert_eq!(ipv6_addr.octets(), expected);
440+
}
400441
}

0 commit comments

Comments
 (0)