Skip to content

Commit 5de4792

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

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/net.rs

Lines changed: 14 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, TcpListener, TcpStream, UdpSocket};
974+
use std::slice;
974975
use std::thread;
975976

976977
use socket2::{Domain, Socket, Type};
@@ -1239,4 +1240,16 @@ mod tests {
12391240
assert_eq!(addrs.remote(), Some(remote));
12401241
})
12411242
}
1243+
1244+
#[test]
1245+
fn sockaddr_convert() {
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+
let addr2 = unsafe { super::ptrs_to_socket_addr(raw_addr, addr_len).unwrap() };
1253+
assert_eq!(addr, addr2);
1254+
}
12421255
}

0 commit comments

Comments
 (0)