Skip to content

Commit 859cfc3

Browse files
committed
uefi: tests: improve readability
1 parent 69c5174 commit 859cfc3

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

uefi/src/proto/device_path/build.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,6 @@ mod tests {
247247
use crate::proto::device_path::messaging::{
248248
Ipv4AddressOrigin, IscsiLoginOptions, IscsiProtocol, RestServiceAccessMode, RestServiceType,
249249
};
250-
use core::slice;
251-
252-
const fn path_to_bytes(path: &DevicePath) -> &[u8] {
253-
unsafe { slice::from_raw_parts(path.as_ffi_ptr().cast::<u8>(), size_of_val(path)) }
254-
}
255250

256251
/// Test building an ACPI ADR node.
257252
#[test]
@@ -268,10 +263,8 @@ mod tests {
268263
let node: &crate::proto::device_path::acpi::Adr =
269264
path.node_iter().next().unwrap().try_into().unwrap();
270265
assert_eq!(node.adr().iter().collect::<Vec<_>>(), [1, 2]);
271-
272-
let bytes = path_to_bytes(path);
273266
#[rustfmt::skip]
274-
assert_eq!(bytes, [
267+
assert_eq!(path.as_bytes(), [
275268
// ACPI ADR node
276269
0x02, 0x03, 0x0c, 0x00,
277270
// Values
@@ -309,9 +302,8 @@ mod tests {
309302
assert_eq!(node.uid_str(), b"bc\0");
310303
assert_eq!(node.cid_str(), b"def\0");
311304

312-
let bytes = path_to_bytes(path);
313305
#[rustfmt::skip]
314-
assert_eq!(bytes, [
306+
assert_eq!(path.as_bytes(), [
315307
// ACPI Expanded node
316308
0x02, 0x02, 0x19, 0x00,
317309
// HID
@@ -366,9 +358,8 @@ mod tests {
366358
assert_eq!(node.vendor_guid_and_data().unwrap().0, vendor_guid);
367359
assert_eq!(node.vendor_guid_and_data().unwrap().1, &[1, 2, 3, 4, 5]);
368360

369-
let bytes = path_to_bytes(path);
370361
#[rustfmt::skip]
371-
assert_eq!(bytes, [
362+
assert_eq!(path.as_bytes(), [
372363
// Messaging REST Service node.
373364
0x03, 0x21, 0x06, 0x00,
374365
// Type and access mode
@@ -429,27 +420,37 @@ mod tests {
429420
/// from the UEFI Specification.
430421
#[test]
431422
fn test_fibre_channel_ex_device_path_example() -> Result<(), BuildError> {
432-
// Arbitrarily choose this test to use a statically-sized
433-
// buffer, just to make sure that code path is tested.
434-
let mut buf = [MaybeUninit::uninit(); 256];
435-
let path = DevicePathBuilder::with_buf(&mut buf)
436-
.push(&acpi::Acpi {
423+
let nodes: &[&dyn BuildNode] = &[
424+
&acpi::Acpi {
437425
hid: 0x41d0_0a03,
438426
uid: 0x0000_0000,
439-
})?
440-
.push(&hardware::Pci {
427+
},
428+
&hardware::Pci {
441429
function: 0x00,
442430
device: 0x1f,
443-
})?
444-
.push(&messaging::FibreChannelEx {
431+
},
432+
&messaging::FibreChannelEx {
445433
world_wide_name: [0, 1, 2, 3, 4, 5, 6, 7],
446434
logical_unit_number: [0, 1, 2, 3, 4, 5, 6, 7],
447-
})?
435+
},
436+
];
437+
438+
// Arbitrarily choose this test to use a statically-sized
439+
// buffer, just to make sure that code path is tested.
440+
let mut buf = [MaybeUninit::uninit(); 256];
441+
let path1 = DevicePathBuilder::with_buf(&mut buf)
442+
.push(nodes[0])?
443+
.push(nodes[1])?
444+
.push(nodes[2])?
445+
.finalize()?;
446+
let path2 = OwnedDevicePathBuilder::new()
447+
.push(nodes[0])?
448+
.push(nodes[1])?
449+
.push(nodes[2])?
448450
.finalize()?;
449451

450-
let bytes = path_to_bytes(path);
451452
#[rustfmt::skip]
452-
assert_eq!(bytes, [
453+
const EXPECTED: [u8; 46] = [
453454
// ACPI node
454455
0x02, 0x01, 0x0c, 0x00,
455456
// HID
@@ -478,7 +479,10 @@ mod tests {
478479

479480
// End-entire node
480481
0x7f, 0xff, 0x04, 0x00,
481-
]);
482+
];
483+
484+
assert_eq!(path1.as_bytes(), EXPECTED);
485+
assert_eq!(path2.as_bytes(), EXPECTED);
482486

483487
Ok(())
484488
}
@@ -533,9 +537,8 @@ mod tests {
533537
})?
534538
.finalize()?;
535539

536-
let bytes = path_to_bytes(path);
537540
#[rustfmt::skip]
538-
assert_eq!(bytes, [
541+
assert_eq!(path.as_bytes(), [
539542
// ACPI node
540543
0x02, 0x01, 0x0c, 0x00,
541544
// HID

0 commit comments

Comments
 (0)