Skip to content

Commit

Permalink
Fix endianness issues in sys::unix::in_addr_convertion test
Browse files Browse the repository at this point in the history
Previously, the "expected" values were implicitly encoded in
little-endian byte order with bitwise operators to match the
native (little-endian) byte order of u32s on most architectures.
This test obviously breaks when the byte order is big-endian.

Instead, provide the expected value as an array of numbers
converted into a network-endian u32. This has the added benefit
that it's much clearer to read, and the numbers in the array
also match the ones that are used in the Ipv4Addr constructor, and
no longer have to be constructed manually with bitwise operators.
  • Loading branch information
decathorpe authored and Thomasdezeeuw committed Mar 27, 2021
1 parent 1896d2d commit 8716af6
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,13 +1449,13 @@ fn in_addr_convertion() {
let raw = to_in_addr(&ip);
// NOTE: `in_addr` is packed on NetBSD and it's unsafe to borrow.
let a = raw.s_addr;
assert_eq!(a, 127 | 1 << 24);
assert_eq!(a, u32::from_ne_bytes([127, 0, 0, 1]));
assert_eq!(from_in_addr(raw), ip);

let ip = Ipv4Addr::new(127, 34, 4, 12);
let raw = to_in_addr(&ip);
let a = raw.s_addr;
assert_eq!(a, 127 << 0 | 34 << 8 | 4 << 16 | 12 << 24);
assert_eq!(a, u32::from_ne_bytes([127, 34, 4, 12]));
assert_eq!(from_in_addr(raw), ip);
}

Expand Down

0 comments on commit 8716af6

Please sign in to comment.