Skip to content

Commit 63700a8

Browse files
committed
Add tests for SockAddr Display.
1 parent 36c42fa commit 63700a8

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

library/std/src/net/addr/socket/tests.rs

+69
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,75 @@ fn to_socket_addr_string() {
5151
// s has been moved into the tsa call
5252
}
5353

54+
#[test]
55+
fn ipv4_socket_addr_to_string() {
56+
// Shortest possible IPv4 length.
57+
assert_eq!(SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0).to_string(), "0.0.0.0:0");
58+
59+
// Longest possible IPv4 length.
60+
assert_eq!(
61+
SocketAddrV4::new(Ipv4Addr::new(255, 255, 255, 255), u16::MAX).to_string(),
62+
"255.255.255.255:65535"
63+
);
64+
65+
// Test padding.
66+
assert_eq!(
67+
&format!("{:16}", SocketAddrV4::new(Ipv4Addr::new(1, 1, 1, 1), 53)),
68+
"1.1.1.1:53 "
69+
);
70+
assert_eq!(
71+
&format!("{:>16}", SocketAddrV4::new(Ipv4Addr::new(1, 1, 1, 1), 53)),
72+
" 1.1.1.1:53"
73+
);
74+
}
75+
76+
#[test]
77+
fn ipv6_socket_addr_to_string() {
78+
// IPv4-mapped address.
79+
assert_eq!(
80+
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0xc000, 0x280), 8080, 0, 0)
81+
.to_string(),
82+
"[::ffff:192.0.2.128]:8080"
83+
);
84+
85+
// IPv4-compatible address.
86+
assert_eq!(
87+
SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0xc000, 0x280), 8080, 0, 0).to_string(),
88+
"[::192.0.2.128]:8080"
89+
);
90+
91+
// IPv6 address with no zero segments.
92+
assert_eq!(
93+
SocketAddrV6::new(Ipv6Addr::new(8, 9, 10, 11, 12, 13, 14, 15), 80, 0, 0).to_string(),
94+
"[8:9:a:b:c:d:e:f]:80"
95+
);
96+
97+
// Shortest possible IPv6 length.
98+
assert_eq!(SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, 0, 0, 0).to_string(), "[::]:0");
99+
100+
// Longest possible IPv6 length.
101+
assert_eq!(
102+
SocketAddrV6::new(
103+
Ipv6Addr::new(0x1111, 0x2222, 0x3333, 0x4444, 0x5555, 0x6666, 0x7777, 0x8888),
104+
u16::MAX,
105+
u32::MAX,
106+
u32::MAX,
107+
)
108+
.to_string(),
109+
"[1111:2222:3333:4444:5555:6666:7777:8888%4294967295]:65535"
110+
);
111+
112+
// Test padding.
113+
assert_eq!(
114+
&format!("{:22}", SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9, 0, 0)),
115+
"[1:2:3:4:5:6:7:8]:9 "
116+
);
117+
assert_eq!(
118+
&format!("{:>22}", SocketAddrV6::new(Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8), 9, 0, 0)),
119+
" [1:2:3:4:5:6:7:8]:9"
120+
);
121+
}
122+
54123
#[test]
55124
fn bind_udp_socket_bad() {
56125
// rust-lang/rust#53957: This is a regression test for a parsing problem

0 commit comments

Comments
 (0)