|
1 | 1 | use super::alloc::*;
|
2 | 2 | use super::time::*;
|
| 3 | +use crate::io::{IoSlice, IoSliceMut}; |
3 | 4 | use crate::time::Duration;
|
4 | 5 |
|
5 | 6 | #[test]
|
@@ -39,3 +40,92 @@ fn epoch() {
|
39 | 40 | };
|
40 | 41 | assert_eq!(system_time_internal::uefi_time_to_duration(t), Duration::new(0, 0));
|
41 | 42 | }
|
| 43 | + |
| 44 | +// UEFI IoSlice and IoSliceMut Tests |
| 45 | +// |
| 46 | +// Strictly speaking, vectored read/write types for UDP4, UDP6, TCP4, TCP6 are defined |
| 47 | +// separately in the UEFI Spec. However, they have the same signature. These tests just ensure |
| 48 | +// that `IoSlice` and `IoSliceMut` are compatible with the vectored types for all the |
| 49 | +// networking protocols. |
| 50 | + |
| 51 | +fn to_slice<T>(val: &T) -> &[u8] { |
| 52 | + let len = size_of_val(val); |
| 53 | + unsafe { crate::slice::from_raw_parts(crate::ptr::from_ref(val).cast(), len) } |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +fn io_slice_single() { |
| 58 | + let mut data = [0, 1, 2, 3, 4]; |
| 59 | + |
| 60 | + let tcp4_frag = r_efi::protocols::tcp4::FragmentData { |
| 61 | + fragment_length: data.len().try_into().unwrap(), |
| 62 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 63 | + }; |
| 64 | + let tcp6_frag = r_efi::protocols::tcp6::FragmentData { |
| 65 | + fragment_length: data.len().try_into().unwrap(), |
| 66 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 67 | + }; |
| 68 | + let udp4_frag = r_efi::protocols::udp4::FragmentData { |
| 69 | + fragment_length: data.len().try_into().unwrap(), |
| 70 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 71 | + }; |
| 72 | + let udp6_frag = r_efi::protocols::udp6::FragmentData { |
| 73 | + fragment_length: data.len().try_into().unwrap(), |
| 74 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 75 | + }; |
| 76 | + let io_slice = IoSlice::new(&data); |
| 77 | + |
| 78 | + assert_eq!(to_slice(&io_slice), to_slice(&tcp4_frag)); |
| 79 | + assert_eq!(to_slice(&io_slice), to_slice(&tcp6_frag)); |
| 80 | + assert_eq!(to_slice(&io_slice), to_slice(&udp4_frag)); |
| 81 | + assert_eq!(to_slice(&io_slice), to_slice(&udp6_frag)); |
| 82 | +} |
| 83 | + |
| 84 | +#[test] |
| 85 | +fn io_slice_mut_single() { |
| 86 | + let mut data = [0, 1, 2, 3, 4]; |
| 87 | + |
| 88 | + let tcp4_frag = r_efi::protocols::tcp4::FragmentData { |
| 89 | + fragment_length: data.len().try_into().unwrap(), |
| 90 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 91 | + }; |
| 92 | + let tcp6_frag = r_efi::protocols::tcp6::FragmentData { |
| 93 | + fragment_length: data.len().try_into().unwrap(), |
| 94 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 95 | + }; |
| 96 | + let udp4_frag = r_efi::protocols::udp4::FragmentData { |
| 97 | + fragment_length: data.len().try_into().unwrap(), |
| 98 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 99 | + }; |
| 100 | + let udp6_frag = r_efi::protocols::udp6::FragmentData { |
| 101 | + fragment_length: data.len().try_into().unwrap(), |
| 102 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 103 | + }; |
| 104 | + let io_slice_mut = IoSliceMut::new(&mut data); |
| 105 | + |
| 106 | + assert_eq!(to_slice(&io_slice_mut), to_slice(&tcp4_frag)); |
| 107 | + assert_eq!(to_slice(&io_slice_mut), to_slice(&tcp6_frag)); |
| 108 | + assert_eq!(to_slice(&io_slice_mut), to_slice(&udp4_frag)); |
| 109 | + assert_eq!(to_slice(&io_slice_mut), to_slice(&udp6_frag)); |
| 110 | +} |
| 111 | + |
| 112 | +#[test] |
| 113 | +fn io_slice_multi() { |
| 114 | + let mut data = [0, 1, 2, 3, 4]; |
| 115 | + |
| 116 | + let tcp4_frag = r_efi::protocols::tcp4::FragmentData { |
| 117 | + fragment_length: data.len().try_into().unwrap(), |
| 118 | + fragment_buffer: data.as_mut_ptr().cast(), |
| 119 | + }; |
| 120 | + let rhs = |
| 121 | + [tcp4_frag.clone(), tcp4_frag.clone(), tcp4_frag.clone(), tcp4_frag.clone(), tcp4_frag]; |
| 122 | + let lhs = [ |
| 123 | + IoSlice::new(&data), |
| 124 | + IoSlice::new(&data), |
| 125 | + IoSlice::new(&data), |
| 126 | + IoSlice::new(&data), |
| 127 | + IoSlice::new(&data), |
| 128 | + ]; |
| 129 | + |
| 130 | + assert_eq!(to_slice(&lhs), to_slice(&rhs)); |
| 131 | +} |
0 commit comments