Skip to content

Commit 8716af6

Browse files
decathorpeThomasdezeeuw
authored andcommitted
Fix endianness issues in sys::unix::in_addr_convertion test
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.
1 parent 1896d2d commit 8716af6

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/sys/unix.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1449,13 +1449,13 @@ fn in_addr_convertion() {
14491449
let raw = to_in_addr(&ip);
14501450
// NOTE: `in_addr` is packed on NetBSD and it's unsafe to borrow.
14511451
let a = raw.s_addr;
1452-
assert_eq!(a, 127 | 1 << 24);
1452+
assert_eq!(a, u32::from_ne_bytes([127, 0, 0, 1]));
14531453
assert_eq!(from_in_addr(raw), ip);
14541454

14551455
let ip = Ipv4Addr::new(127, 34, 4, 12);
14561456
let raw = to_in_addr(&ip);
14571457
let a = raw.s_addr;
1458-
assert_eq!(a, 127 << 0 | 34 << 8 | 4 << 16 | 12 << 24);
1458+
assert_eq!(a, u32::from_ne_bytes([127, 34, 4, 12]));
14591459
assert_eq!(from_in_addr(raw), ip);
14601460
}
14611461

0 commit comments

Comments
 (0)