diff --git a/Cargo.lock b/Cargo.lock index 535f7ce..f984fd5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,6 +63,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "assert_matches" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" + [[package]] name = "atty" version = "0.2.14" @@ -867,6 +873,7 @@ name = "neolink_core" version = "0.4.0" dependencies = [ "aes", + "assert_matches", "cfb-mode", "cookie-factory", "env_logger", diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 2728304..d6a782c 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -20,5 +20,6 @@ yaserde_derive = "0.3.16" xml-rs = "0.8" [dev-dependencies] +assert_matches = "1.5.0" env_logger = "*" indoc = "0.3" diff --git a/crates/core/src/bc/de.rs b/crates/core/src/bc/de.rs index 6ce7d6f..d546f18 100644 --- a/crates/core/src/bc/de.rs +++ b/crates/core/src/bc/de.rs @@ -232,150 +232,289 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> { )) } -#[test] -fn test_bc_modern_login() { - let sample = include_bytes!("samples/model_sample_modern_login.bin"); +#[cfg(test)] +mod tests { + use super::*; + use crate::bc::xml::*; + use assert_matches::assert_matches; - let encryption_protocol = - std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); - let mut context = BcContext::new(encryption_protocol); + #[test] + fn test_bc_modern_login() { + let sample = include_bytes!("samples/model_sample_modern_login.bin"); - let (buf, header) = bc_header(&sample[..]).unwrap(); - let (_, body) = bc_body(&mut context, &header, buf).unwrap(); - assert_eq!(header.msg_id, 1); - assert_eq!(header.body_len, 145); - assert_eq!(header.channel_id, 0); - assert_eq!(header.stream_type, 0); - assert_eq!(header.payload_offset, None); - assert_eq!(header.response_code, 0xdd01); - assert_eq!(header.class, 0x6614); - match body { - BcBody::ModernMsg(ModernMsg { - payload: - Some(BcPayloads::BcXml(BcXml { - encryption: Some(encryption), - .. - })), - .. - }) => assert_eq!(encryption.nonce, "9E6D1FCB9E69846D"), - _ => panic!(), - } -} + let encryption_protocol = + std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); + let mut context = BcContext::new(encryption_protocol); -#[test] -fn test_bc_legacy_login() { - let sample = include_bytes!("samples/model_sample_legacy_login.bin"); - - let encryption_protocol = - std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); - let mut context = BcContext::new(encryption_protocol); - - let (buf, header) = bc_header(&sample[..]).unwrap(); - let (_, body) = bc_body(&mut context, &header, buf).unwrap(); - assert_eq!(header.msg_id, 1); - assert_eq!(header.body_len, 1836); - assert_eq!(header.channel_id, 0); - assert_eq!(header.stream_type, 0); - assert_eq!(header.payload_offset, None); - assert_eq!(header.response_code, 0xdc01); - assert_eq!(header.class, 0x6514); - match body { - BcBody::LegacyMsg(LegacyMsg::LoginMsg { username, password }) => { - assert_eq!(username, "21232F297A57A5A743894A0E4A801FC\0"); - assert_eq!(password, EMPTY_LEGACY_PASSWORD); + let (buf, header) = bc_header(&sample[..]).unwrap(); + let (_, body) = bc_body(&mut context, &header, buf).unwrap(); + assert_eq!(header.msg_id, 1); + assert_eq!(header.body_len, 145); + assert_eq!(header.channel_id, 0); + assert_eq!(header.stream_type, 0); + assert_eq!(header.payload_offset, None); + assert_eq!(header.response_code, 0xdd01); + assert_eq!(header.class, 0x6614); + match body { + BcBody::ModernMsg(ModernMsg { + payload: + Some(BcPayloads::BcXml(BcXml { + encryption: Some(encryption), + .. + })), + .. + }) => assert_eq!(encryption.nonce, "9E6D1FCB9E69846D"), + _ => panic!(), } - _ => panic!(), } -} -#[test] -fn test_bc_modern_login_failed() { - let sample = include_bytes!("samples/modern_login_failed.bin"); + #[test] + fn test_bc_legacy_login() { + let sample = include_bytes!("samples/model_sample_legacy_login.bin"); - let encryption_protocol = - std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); - let mut context = BcContext::new(encryption_protocol); + let encryption_protocol = + std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); + let mut context = BcContext::new(encryption_protocol); - let (buf, header) = bc_header(&sample[..]).unwrap(); - let (_, body) = bc_body(&mut context, &header, buf).unwrap(); - assert_eq!(header.msg_id, 1); - assert_eq!(header.body_len, 0); - assert_eq!(header.channel_id, 0); - assert_eq!(header.stream_type, 0); - assert_eq!(header.payload_offset, Some(0x0)); - assert_eq!(header.response_code, 0x190); // 400 - assert_eq!(header.class, 0x0000); - match body { - BcBody::ModernMsg(ModernMsg { - extension: None, - payload: None, - }) => {} - _ => panic!(), - } -} - -#[test] -fn test_bc_modern_login_success() { - let sample = include_bytes!("samples/modern_login_success.bin"); - - let encryption_protocol = - std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); - let mut context = BcContext::new(encryption_protocol); - - let (buf, header) = bc_header(&sample[..]).unwrap(); - let (_, body) = bc_body(&mut context, &header, buf).unwrap(); - assert_eq!(header.msg_id, 1); - assert_eq!(header.body_len, 2949); - assert_eq!(header.channel_id, 0); - assert_eq!(header.stream_type, 0); - assert_eq!(header.payload_offset, Some(0x0)); - assert_eq!(header.response_code, 0xc8); // 200 - assert_eq!(header.class, 0x0000); - - // Previously, we were not handling payload_offset == 0 (no bin offset) correctly. - // Test that we decoded XML and no binary. - match body { - BcBody::ModernMsg(ModernMsg { - extension: None, - payload: Some(_), - }) => {} - _ => panic!(), - } -} - -#[test] -fn test_bc_binary_mode() { - let sample1 = include_bytes!("samples/modern_video_start1.bin"); - let sample2 = include_bytes!("samples/modern_video_start2.bin"); - - let encryption_protocol = - std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); - let mut context = BcContext::new(encryption_protocol); - - let msg1 = Bc::deserialize(&mut context, &sample1[..]).unwrap(); - match msg1.body { - BcBody::ModernMsg(ModernMsg { - extension: - Some(Extension { - binary_data: Some(1), - .. - }), - payload: Some(BcPayloads::Binary(bin)), - }) => { - assert_eq!(bin.len(), 32); + let (buf, header) = bc_header(&sample[..]).unwrap(); + let (_, body) = bc_body(&mut context, &header, buf).unwrap(); + assert_eq!(header.msg_id, 1); + assert_eq!(header.body_len, 1836); + assert_eq!(header.channel_id, 0); + assert_eq!(header.stream_type, 0); + assert_eq!(header.payload_offset, None); + assert_eq!(header.response_code, 0xdc01); + assert_eq!(header.class, 0x6514); + match body { + BcBody::LegacyMsg(LegacyMsg::LoginMsg { username, password }) => { + assert_eq!(username, "21232F297A57A5A743894A0E4A801FC\0"); + assert_eq!(password, EMPTY_LEGACY_PASSWORD); + } + _ => panic!(), } - _ => panic!(), } - context.in_bin_mode.insert(msg1.meta.msg_num); - let msg2 = Bc::deserialize(&mut context, &sample2[..]).unwrap(); - match msg2.body { - BcBody::ModernMsg(ModernMsg { - extension: None, - payload: Some(BcPayloads::Binary(bin)), - }) => { - assert_eq!(bin.len(), 30344); + #[test] + fn test_bc_modern_login_failed() { + let sample = include_bytes!("samples/modern_login_failed.bin"); + + let encryption_protocol = + std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); + let mut context = BcContext::new(encryption_protocol); + + let (buf, header) = bc_header(&sample[..]).unwrap(); + let (_, body) = bc_body(&mut context, &header, buf).unwrap(); + assert_eq!(header.msg_id, 1); + assert_eq!(header.body_len, 0); + assert_eq!(header.channel_id, 0); + assert_eq!(header.stream_type, 0); + assert_eq!(header.payload_offset, Some(0x0)); + assert_eq!(header.response_code, 0x190); // 400 + assert_eq!(header.class, 0x0000); + match body { + BcBody::ModernMsg(ModernMsg { + extension: None, + payload: None, + }) => {} + _ => panic!(), } - _ => panic!(), + } + + #[test] + fn test_bc_modern_login_success() { + let sample = include_bytes!("samples/modern_login_success.bin"); + + let encryption_protocol = + std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); + let mut context = BcContext::new(encryption_protocol); + + let (buf, header) = bc_header(&sample[..]).unwrap(); + let (_, body) = bc_body(&mut context, &header, buf).unwrap(); + assert_eq!(header.msg_id, 1); + assert_eq!(header.body_len, 2949); + assert_eq!(header.channel_id, 0); + assert_eq!(header.stream_type, 0); + assert_eq!(header.payload_offset, Some(0x0)); + assert_eq!(header.response_code, 0xc8); // 200 + assert_eq!(header.class, 0x0000); + + // Previously, we were not handling payload_offset == 0 (no bin offset) correctly. + // Test that we decoded XML and no binary. + match body { + BcBody::ModernMsg(ModernMsg { + extension: None, + payload: Some(_), + }) => {} + _ => panic!(), + } + } + + #[test] + fn test_bc_binary_mode() { + let sample1 = include_bytes!("samples/modern_video_start1.bin"); + let sample2 = include_bytes!("samples/modern_video_start2.bin"); + + let encryption_protocol = + std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); + let mut context = BcContext::new(encryption_protocol); + + let msg1 = Bc::deserialize(&mut context, &sample1[..]).unwrap(); + match msg1.body { + BcBody::ModernMsg(ModernMsg { + extension: + Some(Extension { + binary_data: Some(1), + .. + }), + payload: Some(BcPayloads::Binary(bin)), + }) => { + assert_eq!(bin.len(), 32); + } + _ => panic!(), + } + + context.in_bin_mode.insert(msg1.meta.msg_num); + let msg2 = Bc::deserialize(&mut context, &sample2[..]).unwrap(); + match msg2.body { + BcBody::ModernMsg(ModernMsg { + extension: None, + payload: Some(BcPayloads::Binary(bin)), + }) => { + assert_eq!(bin.len(), 30344); + } + _ => panic!(), + } + } + + #[test] + // B800 seems to have a different header to the E1 and swann cameras + // the stream_type and message_num do not seem to set in the offical clients + // + // They also have extra streams + fn test_bc_b800_externstream() { + let sample = include_bytes!("samples/xml_externstream_b800.bin"); + + let encryption_protocol = + std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); + let mut context = BcContext::new(encryption_protocol); + + let e = Bc::deserialize(&mut context, &sample[..]); + assert_matches!( + e, + Ok(Bc { + meta: + BcMeta { + msg_id: 3, + channel_id: 0x8c, + stream_type: 0, + response_code: 0, + msg_num: 0, + class: 0x6414, + }, + body: + BcBody::ModernMsg(ModernMsg { + extension: None, + payload: + Some(BcPayloads::BcXml(BcXml { + preview: + Some(Preview { + version, + channel_id: 0, + handle: 1024, + stream_type, + }), + .. + })), + }), + }) if version == "1.1" && stream_type == "externStream" + ); + } + + #[test] + // B800 seems to have a different header to the E1 and swann cameras + // the stream_type and message_num do not seem to set in the offical clients + // + // They also have extra streams + fn test_bc_b800_substream() { + let sample = include_bytes!("samples/xml_substream_b800.bin"); + + let encryption_protocol = + std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); + let mut context = BcContext::new(encryption_protocol); + + let e = Bc::deserialize(&mut context, &sample[..]); + assert_matches!( + e, + Ok(Bc { + meta: + BcMeta { + msg_id: 3, + channel_id: 143, + stream_type: 0, + response_code: 0, + msg_num: 0, + class: 0x6414, + }, + body: + BcBody::ModernMsg(ModernMsg { + extension: None, + payload: + Some(BcPayloads::BcXml(BcXml { + preview: + Some(Preview { + version, + channel_id: 0, + handle: 256, + stream_type, + }), + .. + })), + }), + }) if version == "1.1" && stream_type == "subStream" + ); + } + + #[test] + // B800 seems to have a different header to the E1 and swann cameras + // the stream_type and message_num do not seem to set in the offical clients + // + // They also have extra streams + fn test_bc_b800_mainstream() { + let sample = include_bytes!("samples/xml_mainstream_b800.bin"); + + let encryption_protocol = + std::sync::Arc::new(std::sync::Mutex::new(EncryptionProtocol::BCEncrypt)); + let mut context = BcContext::new(encryption_protocol); + + let e = Bc::deserialize(&mut context, &sample[..]); + assert_matches!( + e, + Ok(Bc { + meta: + BcMeta { + msg_id: 3, + channel_id: 138, + stream_type: 0, + response_code: 0, + msg_num: 0, + class: 0x6414, + }, + body: + BcBody::ModernMsg(ModernMsg { + extension: None, + payload: + Some(BcPayloads::BcXml(BcXml { + preview: + Some(Preview { + version, + channel_id: 0, + handle: 0, + stream_type, + }), + .. + })), + }), + }) if version == "1.1" && stream_type == "mainStream" + ); } } diff --git a/crates/core/src/bc/samples/xml_externstream_b800.bin b/crates/core/src/bc/samples/xml_externstream_b800.bin new file mode 100644 index 0000000..2ce1bb9 Binary files /dev/null and b/crates/core/src/bc/samples/xml_externstream_b800.bin differ diff --git a/crates/core/src/bc/samples/xml_mainstream_b800.bin b/crates/core/src/bc/samples/xml_mainstream_b800.bin new file mode 100644 index 0000000..1a9cd3a Binary files /dev/null and b/crates/core/src/bc/samples/xml_mainstream_b800.bin differ diff --git a/crates/core/src/bc/samples/xml_substream_b800.bin b/crates/core/src/bc/samples/xml_substream_b800.bin new file mode 100644 index 0000000..86d3c43 Binary files /dev/null and b/crates/core/src/bc/samples/xml_substream_b800.bin differ diff --git a/crates/core/src/bc_protocol/stream.rs b/crates/core/src/bc_protocol/stream.rs index 6599272..227bd85 100644 --- a/crates/core/src/bc_protocol/stream.rs +++ b/crates/core/src/bc_protocol/stream.rs @@ -46,11 +46,7 @@ impl BcCamera { /// /// This will block forever or return an error when the camera connection is dropped /// - pub fn start_video( - &self, - data_outs: &mut Outputs, - stream_name: Stream, - ) -> Result + pub fn start_video(&self, data_outs: &mut Outputs, stream: Stream) -> Result where Outputs: StreamOutput, { @@ -60,21 +56,29 @@ impl BcCamera { .expect("Must be connected to start video"); let sub_video = connection.subscribe(MSG_ID_VIDEO)?; - let stream_num = match stream_name { + // On an E1: + // - mainStream always has a value of 0 + // - subStream always has a value of 1 + // - There is no externStram + // On a B800: + // - mainStream is 0 + // - subStream is 0 + // - externStream is 0 + let stream_code = match stream { Stream::Main => 0, Stream::Sub => 1, Stream::Extern => 0, }; // Theses are the numbers used with the offical client - // connecting to an E1. - let handle = match stream_name { + // connecting to an B800. + let handle = match stream { Stream::Main => 0, Stream::Sub => 256, Stream::Extern => 1024, }; - let stream_type = match stream_name { + let stream_name = match stream { Stream::Main => "mainStream", Stream::Sub => "subStream", Stream::Extern => "externStream", @@ -86,7 +90,7 @@ impl BcCamera { msg_id: MSG_ID_VIDEO, channel_id: self.channel_id, msg_num: self.new_message_num(), - stream_type: stream_num, + stream_type: stream_code, response_code: 0, class: 0x6414, // IDK why }, @@ -95,7 +99,7 @@ impl BcCamera { version: xml_ver(), channel_id: self.channel_id, handle, - stream_type, + stream_type: stream_name, }), ..Default::default() },