Skip to content

Commit cb2c728

Browse files
committed
Move tests from src/socket.rs into tests/socket.rs
Also removes the tempdir dependency.
1 parent c794b21 commit cb2c728

File tree

3 files changed

+85
-110
lines changed

3 files changed

+85
-110
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ libc = "0.2.81"
3838
[target."cfg(windows)".dependencies]
3939
winapi = { version = "0.3.9", features = ["handleapi", "ws2ipdef", "ws2tcpip"] }
4040

41-
[dev-dependencies]
42-
tempdir = "0.3"
43-
4441
[features]
4542
# Enable all API, even ones not available on all OSes.
4643
all = []

src/socket.rs

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,109 +1315,3 @@ impl Drop for Socket {
13151315
sys::close(self.inner);
13161316
}
13171317
}
1318-
1319-
#[cfg(test)]
1320-
mod test {
1321-
use std::net::SocketAddr;
1322-
1323-
use super::*;
1324-
1325-
#[test]
1326-
#[cfg(all(feature = "all", unix))]
1327-
fn pair() {
1328-
let (mut a, mut b) = Socket::pair(Domain::UNIX, Type::STREAM, None).unwrap();
1329-
a.write_all(b"hello world").unwrap();
1330-
let mut buf = [0; 11];
1331-
b.read_exact(&mut buf).unwrap();
1332-
assert_eq!(buf, &b"hello world"[..]);
1333-
}
1334-
1335-
#[test]
1336-
#[cfg(all(feature = "all", unix))]
1337-
fn unix() {
1338-
use tempdir::TempDir;
1339-
1340-
let dir = TempDir::new("unix").unwrap();
1341-
let addr = SockAddr::unix(dir.path().join("sock")).unwrap();
1342-
1343-
let listener = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
1344-
listener.bind(&addr).unwrap();
1345-
listener.listen(10).unwrap();
1346-
1347-
let mut a = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
1348-
a.connect(&addr).unwrap();
1349-
1350-
let mut b = listener.accept().unwrap().0;
1351-
1352-
a.write_all(b"hello world").unwrap();
1353-
let mut buf = [0; 11];
1354-
b.read_exact(&mut buf).unwrap();
1355-
assert_eq!(buf, &b"hello world"[..]);
1356-
}
1357-
1358-
#[test]
1359-
fn nodelay() {
1360-
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
1361-
1362-
assert!(socket.set_nodelay(true).is_ok());
1363-
1364-
let result = socket.nodelay();
1365-
1366-
assert!(result.is_ok());
1367-
assert!(result.unwrap());
1368-
}
1369-
1370-
#[test]
1371-
#[cfg(feature = "all")]
1372-
fn out_of_band_inline() {
1373-
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
1374-
1375-
assert_eq!(socket.out_of_band_inline().unwrap(), false);
1376-
1377-
socket.set_out_of_band_inline(true).unwrap();
1378-
assert_eq!(socket.out_of_band_inline().unwrap(), true);
1379-
}
1380-
1381-
#[test]
1382-
#[cfg(all(feature = "all", any(windows, target_os = "linux")))]
1383-
fn out_of_band_send_recv() {
1384-
let s1 = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
1385-
s1.bind(&"127.0.0.1:0".parse::<SocketAddr>().unwrap().into())
1386-
.unwrap();
1387-
let s1_addr = s1.local_addr().unwrap();
1388-
s1.listen(1).unwrap();
1389-
1390-
let s2 = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
1391-
s2.connect(&s1_addr).unwrap();
1392-
1393-
let (s3, _) = s1.accept().unwrap();
1394-
1395-
let mut buf = [0; 10];
1396-
// send some plain inband data
1397-
s2.send(&mut buf).unwrap();
1398-
// send a single out of band byte
1399-
assert_eq!(s2.send_out_of_band(&mut [b"!"[0]]).unwrap(), 1);
1400-
// recv the OOB data first
1401-
assert_eq!(s3.recv_out_of_band(&mut buf).unwrap(), 1);
1402-
assert_eq!(buf[0], b"!"[0]);
1403-
assert_eq!(s3.recv(&mut buf).unwrap(), 10);
1404-
}
1405-
1406-
#[test]
1407-
fn tcp() {
1408-
let s1 = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
1409-
s1.bind(&"127.0.0.1:0".parse::<SocketAddr>().unwrap().into())
1410-
.unwrap();
1411-
let s1_addr = s1.local_addr().unwrap();
1412-
s1.listen(1).unwrap();
1413-
1414-
let s2 = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
1415-
s2.connect(&s1_addr).unwrap();
1416-
1417-
let (s3, _) = s1.accept().unwrap();
1418-
1419-
let mut buf = [0; 11];
1420-
assert_eq!(s2.send(&mut buf).unwrap(), 11);
1421-
assert_eq!(s3.recv(&mut buf).unwrap(), 11);
1422-
}
1423-
}

tests/socket.rs

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@
22
use std::ffi::CStr;
33
#[cfg(any(windows, target_vendor = "apple"))]
44
use std::io;
5+
#[cfg(all(unix, feature = "all"))]
6+
use std::io::Read;
57
use std::io::Write;
68
#[cfg(not(target_os = "redox"))]
79
use std::io::{IoSlice, IoSliceMut};
10+
#[cfg(feature = "all")]
11+
use std::net::{Ipv4Addr, SocketAddrV4};
812
use std::net::{Ipv6Addr, SocketAddr, SocketAddrV6};
913
#[cfg(unix)]
1014
use std::os::unix::io::AsRawFd;
1115
#[cfg(windows)]
1216
use std::os::windows::io::AsRawSocket;
1317
use std::str;
18+
#[cfg(feature = "all")]
19+
use std::thread;
1420
use std::time::Duration;
21+
#[cfg(all(unix, feature = "all"))]
22+
use std::{env, fs};
1523

1624
#[cfg(windows)]
1725
use winapi::shared::minwindef::DWORD;
@@ -20,7 +28,7 @@ use winapi::um::handleapi::GetHandleInformation;
2028
#[cfg(windows)]
2129
use winapi::um::winbase::HANDLE_FLAG_INHERIT;
2230

23-
#[cfg(all(unix, feature = "all"))]
31+
#[cfg(feature = "all")]
2432
use socket2::SockAddr;
2533
use socket2::{Domain, Protocol, Socket, TcpKeepalive, Type};
2634

@@ -297,6 +305,77 @@ where
297305
assert_eq!(flags, want as _, "non-blocking option");
298306
}
299307

308+
#[cfg(feature = "all")]
309+
const DATA: &[u8] = b"hello world";
310+
311+
#[test]
312+
#[cfg(all(feature = "all", unix))]
313+
fn pair() {
314+
let (mut a, mut b) = Socket::pair(Domain::UNIX, Type::STREAM, None).unwrap();
315+
a.write(DATA).unwrap();
316+
let mut buf = [0; DATA.len() + 1];
317+
let n = b.read(&mut buf).unwrap();
318+
assert_eq!(n, DATA.len());
319+
assert_eq!(&buf[..n], DATA);
320+
}
321+
322+
#[test]
323+
#[cfg(all(feature = "all", unix))]
324+
fn unix() {
325+
let mut path = env::temp_dir();
326+
path.push("socket2");
327+
let _ = fs::remove_dir_all(&path);
328+
fs::create_dir_all(&path).unwrap();
329+
path.push("unix");
330+
331+
let addr = SockAddr::unix(path).unwrap();
332+
333+
let listener = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
334+
listener.bind(&addr).unwrap();
335+
listener.listen(10).unwrap();
336+
337+
let mut a = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
338+
a.connect(&addr).unwrap();
339+
let mut b = listener.accept().unwrap().0;
340+
341+
a.write(DATA).unwrap();
342+
let mut buf = [0; DATA.len() + 1];
343+
let n = b.read(&mut buf).unwrap();
344+
assert_eq!(n, DATA.len());
345+
assert_eq!(&buf[..n], DATA);
346+
}
347+
348+
#[test]
349+
#[cfg(feature = "all")]
350+
fn out_of_band() {
351+
let listener = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
352+
listener.bind(&any_ipv4()).unwrap();
353+
listener.listen(1).unwrap();
354+
355+
let sender = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
356+
sender.bind(&any_ipv4()).unwrap();
357+
sender.connect(&listener.local_addr().unwrap()).unwrap();
358+
359+
let (receiver, _) = listener.accept().unwrap();
360+
361+
sender.send(&DATA).unwrap();
362+
363+
const FIRST: &[u8] = b"!";
364+
assert_eq!(sender.send_out_of_band(FIRST).unwrap(), FIRST.len());
365+
// On macOS if no `MSG_OOB` is available it will return `EINVAL`, to prevent
366+
// this from happening we'll sleep to ensure the data is present.
367+
thread::sleep(Duration::from_millis(10));
368+
369+
let mut buf = [1; DATA.len() + 1];
370+
let n = receiver.recv_out_of_band(&mut buf).unwrap();
371+
assert_eq!(n, FIRST.len());
372+
assert_eq!(&buf[..n], FIRST);
373+
374+
let n = receiver.recv(&mut buf).unwrap();
375+
assert_eq!(n, DATA.len());
376+
assert_eq!(&buf[..n], DATA);
377+
}
378+
300379
#[test]
301380
#[cfg(not(target_os = "redox"))]
302381
fn send_recv_vectored() {
@@ -567,6 +646,11 @@ fn device() {
567646
}
568647
}
569648

649+
#[cfg(feature = "all")]
650+
fn any_ipv4() -> SockAddr {
651+
SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0).into()
652+
}
653+
570654
/// Macro to create a simple test to set and get a socket option.
571655
macro_rules! test {
572656
// Test using the `arg`ument as expected return value.

0 commit comments

Comments
 (0)