Skip to content

Commit 9f05697

Browse files
committed
Try multiple networking interface in device test
1 parent 6780944 commit 9f05697

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

tests/socket.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,27 @@ fn keepalive() {
277277
#[cfg(all(feature = "all", target_os = "linux"))]
278278
#[test]
279279
fn device() {
280-
const INTERFACE: &str = "lo0\0";
281-
let interface = CStr::from_bytes_with_nul(INTERFACE.as_bytes()).unwrap();
282-
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
280+
// Some common network interface on Linux.
281+
const INTERFACES: &[&str] = &["lo\0", "lo0\0", "eth0\0", "wlan0\0"];
283282

283+
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap();
284284
assert_eq!(socket.device().unwrap(), None);
285285

286-
socket.bind_device(Some(interface)).unwrap();
287-
assert_eq!(socket.device().unwrap().as_deref(), Some(interface));
288-
289-
socket.bind_device(None).unwrap();
290-
assert_eq!(socket.device().unwrap(), None);
286+
for interface in INTERFACES.iter() {
287+
let interface = CStr::from_bytes_with_nul(interface.as_bytes()).unwrap();
288+
if let Err(err) = socket.bind_device(Some(interface)) {
289+
// Network interface is not available try another.
290+
if let Some(libc::ENODEV) = err.raw_os_error() {
291+
continue;
292+
} else {
293+
panic!("unexpected error binding device: {}", err);
294+
}
295+
}
296+
assert_eq!(socket.device().unwrap().as_deref(), Some(interface));
297+
298+
socket.bind_device(None).unwrap();
299+
assert_eq!(socket.device().unwrap(), None);
300+
// Just need to do it with one interface.
301+
break;
302+
}
291303
}

0 commit comments

Comments
 (0)