diff --git a/onvif/src/discovery/mod.rs b/onvif/src/discovery/mod.rs index d983887..5ec3992 100644 --- a/onvif/src/discovery/mod.rs +++ b/onvif/src/discovery/mod.rs @@ -1,7 +1,9 @@ mod network_enumeration; +use crate::discovery::network_enumeration::enumerate_network_v4; use futures_core::stream::Stream; use schema::ws_discovery::{probe, probe_matches}; +use std::iter::Iterator; use std::{ collections::HashSet, fmt::{Debug, Formatter}, @@ -155,7 +157,7 @@ impl DiscoveryBuilder { futures::future::join_all(unicast_requests.iter().map( |(sock, addr, device_sender, payload, message_id)| async move { sock.send_to(payload, addr).await.ok()?; - let (xml, _) = recv_string(&sock).await.ok()?; + let (xml, _) = recv_string(sock).await.ok()?; debug!("Probe match XML: {}", xml); @@ -390,34 +392,35 @@ fn build_probe() -> probe::Envelope { } } -use crate::discovery::network_enumeration::enumerate_network_v4; -use std::iter::Iterator; -use std::net::Ipv6Addr; -use tokio_stream::StreamExt; - -#[tokio::test] -async fn test_unicast() { - let devices = DiscoveryBuilder::default() - .discovery_mode(DiscoveryMode::Unicast { - network: Ipv4Addr::new(192, 168, 1, 0), - network_mask: Ipv4Addr::new(255, 255, 255, 0), - }) - .run() - .await - .unwrap() - .collect::>() - .await; - - println!("Devices found: {:?}", devices); -} +#[cfg(test)] +mod tests { + use super::*; + use tokio_stream::StreamExt; + + /// This test serves more as an example of how the unicast discovery works. + #[tokio::test] + async fn test_unicast() { + let devices = DiscoveryBuilder::default() + .discovery_mode(DiscoveryMode::Unicast { + network: Ipv4Addr::new(192, 168, 1, 0), + network_mask: Ipv4Addr::new(255, 255, 255, 0), + }) + .run() + .await + .unwrap() + .collect::>() + .await; -#[test] -fn test_xaddrs_extraction() { - const DEVICE_ADDRESS: &str = "an address"; + println!("Devices found: {:?}", devices); + } - let make_xml = |relates_to: &str, xaddrs: &str| -> String { - format!( - r#" + #[test] + fn test_xaddrs_extraction() { + const DEVICE_ADDRESS: &str = "an address"; + + let make_xml = |relates_to: &str, xaddrs: &str| -> String { + format!( + r#" "#, - relates_to = relates_to, - xaddrs = xaddrs, - device_address = DEVICE_ADDRESS - ) - }; - - let our_uuid = "uuid:84ede3de-7dec-11d0-c360-F01234567890"; - let bad_uuid = "uuid:84ede3de-7dec-11d0-c360-F00000000000"; - - let input = vec![ - make_xml(our_uuid, "http://addr_20 http://addr_21 http://addr_22"), - make_xml(bad_uuid, "http://addr_30 http://addr_31"), - ]; + relates_to = relates_to, + xaddrs = xaddrs, + device_address = DEVICE_ADDRESS + ) + }; - let actual = input - .iter() - .filter_map(|xml| yaserde::de::from_str::(xml).ok()) - .filter(|envelope| envelope.header.relates_to == our_uuid) - .filter_map(device_from_envelope) - .collect::>(); - - assert_eq!(actual.len(), 1); - - // OK: message UUID matches and addr responds - assert_eq!( - actual, - &[Device { - urls: vec![ - Url::parse("http://addr_20").unwrap(), - Url::parse("http://addr_21").unwrap(), - Url::parse("http://addr_22").unwrap(), - ], - name: Some("MyCamera2000".to_string()), - hardware: None, - address: DEVICE_ADDRESS.to_string(), - types: vec![], - }] - ); + let our_uuid = "uuid:84ede3de-7dec-11d0-c360-F01234567890"; + let bad_uuid = "uuid:84ede3de-7dec-11d0-c360-F00000000000"; + + let input = vec![ + make_xml(our_uuid, "http://addr_20 http://addr_21 http://addr_22"), + make_xml(bad_uuid, "http://addr_30 http://addr_31"), + ]; + + let actual = input + .iter() + .filter_map(|xml| yaserde::de::from_str::(xml).ok()) + .filter(|envelope| envelope.header.relates_to == our_uuid) + .filter_map(device_from_envelope) + .collect::>(); + + assert_eq!(actual.len(), 1); + + // OK: message UUID matches and addr responds + assert_eq!( + actual, + &[Device { + urls: vec![ + Url::parse("http://addr_20").unwrap(), + Url::parse("http://addr_21").unwrap(), + Url::parse("http://addr_22").unwrap(), + ], + name: Some("MyCamera2000".to_string()), + hardware: None, + address: DEVICE_ADDRESS.to_string(), + types: vec![], + }] + ); + } } diff --git a/onvif/src/discovery/network_enumeration.rs b/onvif/src/discovery/network_enumeration.rs index 5ef01dd..a97f52e 100644 --- a/onvif/src/discovery/network_enumeration.rs +++ b/onvif/src/discovery/network_enumeration.rs @@ -4,7 +4,7 @@ use std::net::Ipv4Addr; fn octets_to_u32(octets: [u8; 4]) -> u32 { (octets[0] as u32) << (3 * 8) | (octets[1] as u32) << (2 * 8) - | (octets[2] as u32) << (1 * 8) + | (octets[2] as u32) << 8 | (octets[3] as u32) }