aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi-raw / src / net.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! UEFI network types. |
| 4 | //! |
| 5 | //! The main exports of this module are: |
| 6 | //! - [`MacAddress`] |
| 7 | //! - [`IpAddress`] |
| 8 | //! - [`Ipv4Address`] |
| 9 | //! - [`Ipv6Address`] |
| 10 | |
| 11 | use core::fmt::{self, Debug, Display, Formatter}; |
| 12 | use core::net as core_net; |
| 13 | |
| 14 | /// An IPv4 internet protocol address. |
| 15 | /// |
| 16 | /// # Conversions and Relation to [`core::net`] |
| 17 | /// |
| 18 | /// The following [`From`] implementations exist: |
| 19 | /// - `[u8; 4]` -> [`Ipv4Address`] |
| 20 | /// - [`core::net::Ipv4Addr`] -> [`Ipv4Address`] |
| 21 | /// - [`core::net::IpAddr`] -> [`Ipv4Address`] |
| 22 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 23 | #[repr(transparent)] |
| 24 | pub struct Ipv4Address(pub [u8; 4]); |
| 25 | |
| 26 | impl Ipv4Address { |
| 27 | /// Returns the octets of the IP address. |
| 28 | #[must_use] |
| 29 | pub const fn octets(self) -> [u8; 4] { |
| 30 | self.0 |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl From<core_net::Ipv4Addr> for Ipv4Address { |
| 35 | fn from(ip: core_net::Ipv4Addr) -> Self { |
| 36 | Self(ip.octets()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl From<Ipv4Address> for core_net::Ipv4Addr { |
| 41 | fn from(ip: Ipv4Address) -> Self { |
| 42 | Self::from(ip.0) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl From<[u8; 4]> for Ipv4Address { |
| 47 | fn from(octets: [u8; 4]) -> Self { |
| 48 | Self(octets) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | impl Display for Ipv4Address { |
| 53 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 54 | let ip = core_net::Ipv4Addr::from(*self); |
| 55 | write!(f, "{}", ip) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// An IPv6 internet protocol address. |
| 60 | /// |
| 61 | /// # Conversions and Relation to [`core::net`] |
| 62 | /// |
| 63 | /// The following [`From`] implementations exist: |
| 64 | /// - `[u8; 16]` -> [`Ipv6Address`] |
| 65 | /// - [`core::net::Ipv6Addr`] -> [`Ipv6Address`] |
| 66 | /// - [`core::net::IpAddr`] -> [`Ipv6Address`] |
| 67 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 68 | #[repr(transparent)] |
| 69 | pub struct Ipv6Address(pub [u8; 16]); |
| 70 | |
| 71 | impl Ipv6Address { |
| 72 | /// Returns the octets of the IP address. |
| 73 | #[must_use] |
| 74 | pub const fn octets(self) -> [u8; 16] { |
| 75 | self.0 |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | impl From<core_net::Ipv6Addr> for Ipv6Address { |
| 80 | fn from(ip: core_net::Ipv6Addr) -> Self { |
| 81 | Self(ip.octets()) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl From<Ipv6Address> for core_net::Ipv6Addr { |
| 86 | fn from(ip: Ipv6Address) -> Self { |
| 87 | Self::from(ip.0) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl From<[u8; 16]> for Ipv6Address { |
| 92 | fn from(octets: [u8; 16]) -> Self { |
| 93 | Self(octets) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | impl Display for Ipv6Address { |
| 98 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 99 | let ip = core_net::Ipv6Addr::from(*self); |
| 100 | write!(f, "{}", ip) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// An IPv4 or IPv6 internet protocol address that is ABI compatible with EFI. |
| 105 | /// |
| 106 | /// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This |
| 107 | /// type is defined in the same way as edk2 for compatibility with C code. Note |
| 108 | /// that this is an untagged union, so there's no way to tell which type of |
| 109 | /// address an `IpAddress` value contains without additional context. |
| 110 | /// |
| 111 | /// # Conversions and Relation to [`core::net`] |
| 112 | /// |
| 113 | /// The following [`From`] implementations exist: |
| 114 | /// - `[u8; 4]` -> [`IpAddress`] |
| 115 | /// - `[u8; 16]` -> [`IpAddress`] |
| 116 | /// - [`core::net::Ipv4Addr`] -> [`IpAddress`] |
| 117 | /// - [`core::net::Ipv6Addr`] -> [`IpAddress`] |
| 118 | /// - [`core::net::IpAddr`] -> [`IpAddress`] |
| 119 | #[derive(Clone, Copy)] |
| 120 | #[repr(C)] |
| 121 | pub union IpAddress { |
| 122 | /// This member serves to align the whole type to a 4 bytes as required by |
| 123 | /// the spec. Note that this is slightly different from `repr(align(4))`, |
| 124 | /// which would prevent placing this type in a packed structure. |
| 125 | pub addr: [u32; 4], |
| 126 | |
| 127 | /// An IPv4 internet protocol address. |
| 128 | pub v4: Ipv4Address, |
| 129 | |
| 130 | /// An IPv6 internet protocol address. |
| 131 | pub v6: Ipv6Address, |
| 132 | } |
| 133 | |
| 134 | // Ensure ABI guarantees for IpAddress. |
| 135 | const _: () = { |
| 136 | #[repr(C, packed)] |
| 137 | struct PackedHelper<T>(T); |
| 138 | |
| 139 | assert!(align_of::<IpAddress>() == 4); |
| 140 | assert!(size_of::<IpAddress>() == 16); |
| 141 | |
| 142 | // The type must be usable in a packed struct, even when it is normally |
| 143 | // 4 byte aligned. |
| 144 | assert!(align_of::<PackedHelper<IpAddress>>() == 1); |
| 145 | assert!(size_of::<PackedHelper<IpAddress>>() == 16); |
| 146 | }; |
| 147 | |
| 148 | impl IpAddress { |
| 149 | /// Zeroed variant where all bytes are guaranteed to be initialized to zero. |
| 150 | pub const ZERO: Self = Self { addr: [0; 4] }; |
| 151 | |
| 152 | /// Construct a new IPv4 address. |
| 153 | /// |
| 154 | /// The type won't know that it is an IPv4 address and additional context |
| 155 | /// is needed. The bytes not covered by the IPv4 address are zero. |
| 156 | #[must_use] |
| 157 | pub const fn new_v4(octets: [u8; 4]) -> Self { |
| 158 | // Fully initialize all bytes first. |
| 159 | let mut addr = Self::ZERO; |
| 160 | addr.v4 = Ipv4Address(octets); |
| 161 | addr |
| 162 | } |
| 163 | |
| 164 | /// Construct a new IPv6 address. |
| 165 | /// |
| 166 | /// The type won't know that it is an IPv6 address and additional context |
| 167 | /// is needed. |
| 168 | #[must_use] |
| 169 | pub const fn new_v6(octets: [u8; 16]) -> Self { |
| 170 | Self { |
| 171 | v6: Ipv6Address(octets), |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// Transforms this EFI type to the Rust standard library's type |
| 176 | /// [`core::net::IpAddr`]. |
| 177 | /// |
| 178 | /// # Arguments |
| 179 | /// - `is_ipv6`: Whether the internal data should be interpreted as IPv6 or |
| 180 | /// IPv4 address. |
| 181 | /// |
| 182 | /// # Safety |
| 183 | /// Callers must ensure that the `v4` field is valid if `is_ipv6` is false, |
| 184 | /// and that the `v6` field is valid if `is_ipv6` is true |
| 185 | #[must_use] |
| 186 | pub unsafe fn into_core_addr(self, is_ipv6: bool) -> core_net::IpAddr { |
| 187 | if is_ipv6 { |
| 188 | // SAFETY: Caller assumes that the underlying data is initialized. |
| 189 | core_net::IpAddr::V6(core_net::Ipv6Addr::from(unsafe { self.v6.octets() })) |
| 190 | } else { |
| 191 | // SAFETY: Caller assumes that the underlying data is initialized. |
| 192 | core_net::IpAddr::V4(core_net::Ipv4Addr::from(unsafe { self.v4.octets() })) |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | impl Debug for IpAddress { |
| 198 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 199 | // The type is an untagged union, so we don't know whether it contains |
| 200 | // an IPv4 or IPv6 address. It's also not safe to just print the whole |
| 201 | // 16 bytes, since they might not all be initialized. |
| 202 | f.debug_struct("IpAddress").finish() |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | impl Default for IpAddress { |
| 207 | fn default() -> Self { |
| 208 | Self::ZERO |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | impl From<core_net::IpAddr> for IpAddress { |
| 213 | fn from(t: core_net::IpAddr) -> Self { |
| 214 | match t { |
| 215 | core_net::IpAddr::V4(ip) => Self::new_v4(ip.octets()), |
| 216 | core_net::IpAddr::V6(ip) => Self::new_v6(ip.octets()), |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | impl From<core_net::Ipv4Addr> for IpAddress { |
| 222 | fn from(value: core_net::Ipv4Addr) -> Self { |
| 223 | Self::new_v4(value.octets()) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | impl From<core_net::Ipv6Addr> for IpAddress { |
| 228 | fn from(value: core_net::Ipv6Addr) -> Self { |
| 229 | Self::new_v6(value.octets()) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | impl From<[u8; 4]> for IpAddress { |
| 234 | fn from(octets: [u8; 4]) -> Self { |
| 235 | Self::new_v4(octets) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | impl From<[u8; 16]> for IpAddress { |
| 240 | fn from(octets: [u8; 16]) -> Self { |
| 241 | Self::new_v6(octets) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /// UEFI Media Access Control (MAC) address. |
| 246 | /// |
| 247 | /// UEFI supports multiple network protocols and hardware types, not just |
| 248 | /// Ethernet. Some of them may use MAC addresses longer than 6 bytes. To be |
| 249 | /// protocol-agnostic and future-proof, the UEFI spec chooses a maximum size |
| 250 | /// that can hold any supported media access control address. |
| 251 | /// |
| 252 | /// In most cases, this is just a typical `[u8; 6]` Ethernet style MAC |
| 253 | /// address with the rest of the bytes being zero. |
| 254 | /// |
| 255 | /// # Conversions and Relation to [`core::net`] |
| 256 | /// |
| 257 | /// There is no matching type in [`core::net`] but the following [`From`] |
| 258 | /// implementations exist: |
| 259 | /// - `[u8; 6]` <-> [`MacAddress`] |
| 260 | /// - `[u8; 32]` -> [`MacAddress`] |
| 261 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 262 | #[repr(transparent)] |
| 263 | pub struct MacAddress(pub [u8; 32]); |
| 264 | |
| 265 | impl MacAddress { |
| 266 | /// Returns the octets of the MAC address. |
| 267 | #[must_use] |
| 268 | pub const fn octets(self) -> [u8; 32] { |
| 269 | self.0 |
| 270 | } |
| 271 | |
| 272 | /// Interpret the MAC address as normal 6-byte MAC address, as used in |
| 273 | /// Ethernet. |
| 274 | #[must_use] |
| 275 | pub fn into_ethernet_addr(self) -> [u8; 6] { |
| 276 | let mut buffer = [0; 6]; |
| 277 | buffer.copy_from_slice(&self.octets()[..6]); |
| 278 | buffer |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Normal Ethernet MAC address. |
| 283 | impl From<[u8; 6]> for MacAddress { |
| 284 | fn from(octets: [u8; 6]) -> Self { |
| 285 | let mut buffer = [0; 32]; |
| 286 | buffer[..6].copy_from_slice(&octets); |
| 287 | Self(buffer) |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | // Normal Ethernet MAC address. |
| 292 | impl From<MacAddress> for [u8; 6] { |
| 293 | fn from(MacAddress(o): MacAddress) -> Self { |
| 294 | [o[0], o[1], o[2], o[3], o[4], o[5]] |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | // UEFI MAC addresses. |
| 299 | impl From<[u8; 32]> for MacAddress { |
| 300 | fn from(octets: [u8; 32]) -> Self { |
| 301 | Self(octets) |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | #[cfg(test)] |
| 306 | mod tests { |
| 307 | use super::*; |
| 308 | |
| 309 | const TEST_IPV4: [u8; 4] = [91, 92, 93, 94]; |
| 310 | const TEST_IPV6: [u8; 16] = [ |
| 311 | 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, |
| 312 | ]; |
| 313 | |
| 314 | /// Test round-trip conversion between [`Ipv4Address`] and [`core_net::Ipv4Addr`]. |
| 315 | #[test] |
| 316 | fn test_ip_addr4_conversion() { |
| 317 | let uefi_addr = Ipv4Address(TEST_IPV4); |
| 318 | let core_addr = core_net::Ipv4Addr::from(uefi_addr); |
| 319 | assert_eq!(uefi_addr, Ipv4Address::from(core_addr)); |
| 320 | } |
| 321 | |
| 322 | /// Test round-trip conversion between [`Ipv6Address`] and [`core_net::Ipv6Addr`]. |
| 323 | #[test] |
| 324 | fn test_ip_addr6_conversion() { |
| 325 | let uefi_addr = Ipv6Address(TEST_IPV6); |
| 326 | let core_addr = core_net::Ipv6Addr::from(uefi_addr); |
| 327 | assert_eq!(uefi_addr, Ipv6Address::from(core_addr)); |
| 328 | } |
| 329 | |
| 330 | /// Test conversion from [`core_net::IpAddr`] to [`IpvAddress`]. |
| 331 | /// |
| 332 | /// Note that conversion in the other direction is not possible. |
| 333 | #[test] |
| 334 | fn test_ip_addr_conversion() { |
| 335 | let core_addr = core_net::IpAddr::V4(core_net::Ipv4Addr::from(TEST_IPV4)); |
| 336 | let uefi_addr = IpAddress::from(core_addr); |
| 337 | // SAFETY: `IpAddress::from` constructed the IPv4 variant above. |
| 338 | let octets = unsafe { uefi_addr.v4.0 }; |
| 339 | assert_eq!(octets, TEST_IPV4); |
| 340 | |
| 341 | let core_addr = core_net::IpAddr::V6(core_net::Ipv6Addr::from(TEST_IPV6)); |
| 342 | let uefi_addr = IpAddress::from(core_addr); |
| 343 | // SAFETY: `IpAddress::from` constructed the IPv6 variant above. |
| 344 | let octets = unsafe { uefi_addr.v6.0 }; |
| 345 | assert_eq!(octets, TEST_IPV6); |
| 346 | } |
| 347 | |
| 348 | /// Test conversions between `MacAddress` and octet arrays. |
| 349 | #[test] |
| 350 | fn test_mac_addr_conversion() { |
| 351 | let ethernet_octets = [0, 1, 2, 3, 4, 5]; |
| 352 | let mut padded_octets = [0; 32]; |
| 353 | padded_octets[..6].copy_from_slice(ðernet_octets); |
| 354 | |
| 355 | let efi_mac_addr = MacAddress::from(ethernet_octets); |
| 356 | assert_eq!(efi_mac_addr.octets(), padded_octets); |
| 357 | assert_eq!(<[u8; 6]>::from(efi_mac_addr), ethernet_octets); |
| 358 | assert_eq!(efi_mac_addr.into_ethernet_addr(), ethernet_octets); |
| 359 | |
| 360 | let efi_octets = [ |
| 361 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
| 362 | 24, 25, 26, 27, 28, 29, 30, 31, |
| 363 | ]; |
| 364 | |
| 365 | let efi_mac_addr = MacAddress::from(efi_octets); |
| 366 | assert_eq!(efi_mac_addr.octets(), efi_octets); |
| 367 | assert_eq!(<[u8; 6]>::from(efi_mac_addr), ethernet_octets); |
| 368 | assert_eq!(efi_mac_addr.into_ethernet_addr(), ethernet_octets); |
| 369 | } |
| 370 | |
| 371 | /// Tests the From-impls from the documentation. |
| 372 | #[test] |
| 373 | fn test_promised_from_impls() { |
| 374 | // octets -> Ipv4Address |
| 375 | { |
| 376 | let octets = [0_u8, 1, 2, 3]; |
| 377 | assert_eq!(Ipv4Address::from(octets), Ipv4Address(octets)); |
| 378 | let uefi_addr = IpAddress::from(octets); |
| 379 | // SAFETY: `from([u8; 4])` constructs the IPv4 variant. |
| 380 | assert_eq!(&octets, &unsafe { uefi_addr.v4.octets() }); |
| 381 | } |
| 382 | // octets -> Ipv6Address |
| 383 | { |
| 384 | let octets = [0_u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; |
| 385 | assert_eq!(Ipv6Address::from(octets), Ipv6Address(octets)); |
| 386 | let uefi_addr = IpAddress::from(octets); |
| 387 | // SAFETY: `from([u8; 16])` constructs the IPv6 variant. |
| 388 | assert_eq!(&octets, &unsafe { uefi_addr.v6.octets() }); |
| 389 | } |
| 390 | // StdIpv4Addr -> Ipv4Address |
| 391 | { |
| 392 | let octets = [7, 5, 3, 1]; |
| 393 | let core_ipv4_addr = core_net::Ipv4Addr::from(octets); |
| 394 | assert_eq!(Ipv4Address::from(core_ipv4_addr).octets(), octets); |
| 395 | // SAFETY: The `Ipv4Addr` conversion selects the IPv4 variant. |
| 396 | let converted = unsafe { IpAddress::from(core_ipv4_addr).v4.octets() }; |
| 397 | assert_eq!(converted, octets); |
| 398 | } |
| 399 | // StdIpv6Addr -> Ipv6Address |
| 400 | { |
| 401 | let octets = [7, 5, 3, 1, 6, 3, 8, 5, 2, 5, 2, 7, 3, 5, 2, 6]; |
| 402 | let core_ipv6_addr = core_net::Ipv6Addr::from(octets); |
| 403 | assert_eq!(Ipv6Address::from(core_ipv6_addr).octets(), octets); |
| 404 | // SAFETY: The `Ipv6Addr` conversion selects the IPv6 variant. |
| 405 | let converted = unsafe { IpAddress::from(core_ipv6_addr).v6.octets() }; |
| 406 | assert_eq!(converted, octets); |
| 407 | } |
| 408 | // StdIpAddr -> IpAddress |
| 409 | { |
| 410 | let octets = [8, 8, 2, 6]; |
| 411 | let core_ip_addr = core_net::IpAddr::from(octets); |
| 412 | // SAFETY: This `IpAddr` is built from four octets, so it is IPv4. |
| 413 | let converted = unsafe { IpAddress::from(core_ip_addr).v4.octets() }; |
| 414 | assert_eq!(converted, octets); |
| 415 | } |
| 416 | // octets <-> MacAddress |
| 417 | { |
| 418 | let octets = [8, 8, 2, 6, 6, 7]; |
| 419 | let uefi_mac_addr = MacAddress::from(octets); |
| 420 | assert_eq!(uefi_mac_addr.octets()[0..6], octets); |
| 421 | let octets2: [u8; 6] = uefi_mac_addr.into(); |
| 422 | assert_eq!(octets2, octets) |
| 423 | } |
| 424 | // octets -> MacAddress |
| 425 | { |
| 426 | let octets = [ |
| 427 | 8_u8, 8, 2, 6, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 7, 0, 0, 0, |
| 428 | 0, 0, 0, 0, 42, |
| 429 | ]; |
| 430 | let uefi_mac_addr = MacAddress::from(octets); |
| 431 | assert_eq!(uefi_mac_addr.octets(), octets); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /// Tests the intended usage of net types in high-level APIs and how they |
| 436 | /// map to lower level UEFI types. |
| 437 | /// |
| 438 | /// TL;DR: High-level interfaces use core::net types whereas lower-level |
| 439 | /// interfaces use types from this module. |
| 440 | #[test] |
| 441 | fn test_uefi_flow() { |
| 442 | fn efi_retrieve_efi_ip_addr(addr: *mut IpAddress, is_ipv6: bool) { |
| 443 | // SAFETY: The caller passes a valid, writable `IpAddress` pointer. |
| 444 | let addr = unsafe { addr.as_mut().unwrap() }; |
| 445 | // SAFETY: The selected variant is the one initialized by the caller. |
| 446 | unsafe { |
| 447 | addr.v4.0[0] = 42; |
| 448 | addr.v4.0[1] = 42; |
| 449 | addr.v4.0[2] = 42; |
| 450 | addr.v4.0[3] = 42; |
| 451 | } |
| 452 | if is_ipv6 { |
| 453 | // SAFETY: The IPv6 branch writes the bytes that the caller will read back. |
| 454 | unsafe { |
| 455 | addr.v6.0[14] = 42; |
| 456 | addr.v6.0[15] = 42; |
| 457 | } |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | fn high_level_retrieve_ip(is_ipv6: bool) -> core_net::IpAddr { |
| 462 | let mut efi_ip_addr = IpAddress::ZERO; |
| 463 | efi_retrieve_efi_ip_addr(&mut efi_ip_addr, is_ipv6); |
| 464 | // SAFETY: The helper initialized the variant selected by `is_ipv6`. |
| 465 | unsafe { efi_ip_addr.into_core_addr(is_ipv6) } |
| 466 | } |
| 467 | |
| 468 | let ipv4_addr = high_level_retrieve_ip(false); |
| 469 | let ipv4_addr: core_net::Ipv4Addr = match ipv4_addr { |
| 470 | core_net::IpAddr::V4(ipv4_addr) => ipv4_addr, |
| 471 | core_net::IpAddr::V6(_) => panic!("should not happen"), |
| 472 | }; |
| 473 | assert_eq!(ipv4_addr.octets(), [42, 42, 42, 42]); |
| 474 | |
| 475 | let ipv6_addr = high_level_retrieve_ip(true); |
| 476 | let ipv6_addr: core_net::Ipv6Addr = match ipv6_addr { |
| 477 | core_net::IpAddr::V6(ipv6_addr) => ipv6_addr, |
| 478 | core_net::IpAddr::V4(_) => panic!("should not happen"), |
| 479 | }; |
| 480 | let expected = [42, 42, 42, 42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42, 42]; |
| 481 | assert_eq!(ipv6_addr.octets(), expected); |
| 482 | } |
| 483 | |
| 484 | /// `new_v4` must initialize all 16 bytes so that the value can be |
| 485 | /// byte-copied as a whole, e.g. into a device path node. |
| 486 | #[test] |
| 487 | fn test_new_v4_initializes_all_bytes() { |
| 488 | let uefi_addr = IpAddress::new_v4(TEST_IPV4); |
| 489 | // SAFETY: `new_v4` initializes the whole union. |
| 490 | let words = unsafe { uefi_addr.addr }; |
| 491 | assert_eq!(words[0], u32::from_ne_bytes(TEST_IPV4)); |
| 492 | assert_eq!(words[1..], [0; 3]); |
| 493 | } |
| 494 | } |