Skip to content

Commit e762976

Browse files
Merge pull request #1781 from rust-osdev/core-net-2
uefi-raw: add unit test for typical high-level net API usage
2 parents a897b4a + 746082a commit e762976

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

uefi-raw/src/net.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,50 @@ mod tests {
403403
assert_eq!(uefi_mac_addr.octets(), octets);
404404
}
405405
}
406+
407+
/// Tests the intended usage of net types in high-level APIs and how they
408+
/// map to lower level UEFI types.
409+
///
410+
/// TL;DR: High-level interfaces use core::net types whereas lower-level
411+
/// interfaces use types from this module.
412+
#[test]
413+
fn test_uefi_flow() {
414+
fn efi_retrieve_efi_ip_addr(addr: *mut IpAddress, is_ipv6: bool) {
415+
let addr = unsafe { addr.as_mut().unwrap() };
416+
// SAFETY: Alignment is guaranteed and memory is initialized.
417+
unsafe {
418+
addr.v4.0[0] = 42;
419+
addr.v4.0[1] = 42;
420+
addr.v4.0[2] = 42;
421+
addr.v4.0[3] = 42;
422+
}
423+
if is_ipv6 {
424+
unsafe {
425+
addr.v6.0[14] = 42;
426+
addr.v6.0[15] = 42;
427+
}
428+
}
429+
}
430+
431+
fn high_level_retrieve_ip(is_ipv6: bool) -> core::net::IpAddr {
432+
let mut efi_ip_addr = IpAddress::ZERO;
433+
efi_retrieve_efi_ip_addr(&mut efi_ip_addr, is_ipv6);
434+
unsafe { efi_ip_addr.into_core_addr(is_ipv6) }
435+
}
436+
437+
let ipv4_addr = high_level_retrieve_ip(false);
438+
let ipv4_addr: core::net::Ipv4Addr = match ipv4_addr {
439+
core::net::IpAddr::V4(ipv4_addr) => ipv4_addr,
440+
core::net::IpAddr::V6(_) => panic!("should not happen"),
441+
};
442+
assert_eq!(ipv4_addr.octets(), [42, 42, 42, 42]);
443+
444+
let ipv6_addr = high_level_retrieve_ip(true);
445+
let ipv6_addr: core::net::Ipv6Addr = match ipv6_addr {
446+
core::net::IpAddr::V6(ipv6_addr) => ipv6_addr,
447+
core::net::IpAddr::V4(_) => panic!("should not happen"),
448+
};
449+
let expected = [42, 42, 42, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 42];
450+
assert_eq!(ipv6_addr.octets(), expected);
451+
}
406452
}

0 commit comments

Comments
 (0)