Skip to content

Commit d2a972e

Browse files
committed
Att socket conversion tests
1 parent 47d243c commit d2a972e

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/net.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,8 @@ impl WsaExtension {
970970
#[cfg(test)]
971971
mod tests {
972972
use std::io::prelude::*;
973-
use std::net::{SocketAddr, TcpListener, TcpStream, UdpSocket};
973+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV6, TcpListener, TcpStream, UdpSocket};
974+
use std::slice;
974975
use std::thread;
975976

976977
use socket2::{Domain, Socket, Type};
@@ -1239,4 +1240,35 @@ mod tests {
12391240
assert_eq!(addrs.remote(), Some(remote));
12401241
})
12411242
}
1243+
1244+
#[test]
1245+
fn sockaddr_convert_4() {
1246+
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(3, 4, 5, 6)), 0xabcd);
1247+
let (raw_addr, addr_len) = super::socket_addr_to_ptrs(&addr);
1248+
assert_eq!(addr_len, 16);
1249+
let addr_bytes = unsafe { slice::from_raw_parts(raw_addr as *const u8, addr_len as usize) };
1250+
assert_eq!(addr_bytes, &[2, 0, 0xab, 0xcd, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0]);
1251+
}
1252+
1253+
#[test]
1254+
fn sockaddr_convert_v6() {
1255+
let port = 0xabcd;
1256+
let flowinfo = 0x12345678;
1257+
let scope_id = 0x87654321;
1258+
let addr = SocketAddr::V6(SocketAddrV6::new(
1259+
Ipv6Addr::new(0x0102, 0x0304, 0x0506, 0x0708, 0x090a, 0x0b0c, 0x0d0e, 0x0f10),
1260+
port,
1261+
flowinfo,
1262+
scope_id
1263+
));
1264+
let (raw_addr, addr_len) = super::socket_addr_to_ptrs(&addr);
1265+
assert_eq!(addr_len, 28);
1266+
let addr_bytes = unsafe { slice::from_raw_parts(raw_addr as *const u8, addr_len as usize) };
1267+
assert_eq!(addr_bytes, &[
1268+
23, 0, // AF_INET6
1269+
0xab, 0xcd, // Port
1270+
0x78, 0x56, 0x34, 0x12, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, // IP
1271+
0x21, 0x43, 0x65, 0x87, // scope_id
1272+
]);
1273+
}
12421274
}

0 commit comments

Comments
 (0)