diff --git a/rust/sbp2json/tests/test_round_trips.rs b/rust/sbp2json/tests/test_round_trips.rs index 0675405e5e..2c5f0fc09e 100644 --- a/rust/sbp2json/tests/test_round_trips.rs +++ b/rust/sbp2json/tests/test_round_trips.rs @@ -14,6 +14,61 @@ use common::{ use crate::common::run_jsonfields2sbp; use serde_json::ser::CompactFormatter; +use sbp::json::HaskellishFloatFormatter; +use serde_json::json; + +#[test] +fn test_invalid_message_round_trip() { + // Properly framed data but the payload isn't right given the message type + let data: Vec = vec![0x55, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x65, 0x8D]; + let mut sbp2json_out: Vec = Vec::new(); + + let mut expected_json = json!({"msg_name": "INVALID", "payload": "VQEBAQEBAWWN"}); + let expected_json = expected_json.as_object_mut().expect("is a map"); + let input = Cursor::new(data.clone()); + let error_handler_opt = converters::ErrorHandlerOptions::CoerceErrorsToInvalidMsg; + converters::sbp2json( + input, + &mut sbp2json_out, + HaskellishFloatFormatter {}, + true, + error_handler_opt, + ) + .expect("can run sbp2json"); + + let mut out_as_json: serde_json::Value = + serde_json::from_str(std::str::from_utf8(&sbp2json_out).expect("is valid utf8")) + .expect("is valid JSON"); + + for key in &["msg_name".to_string(), "payload".to_string()] { + assert!(expected_json.get(key).is_some()); + assert_eq!( + out_as_json.as_object_mut().and_then(|m| m.remove(key)), + expected_json.clone().remove(key), + "{key} values must be equal" + ); + } + assert!( + out_as_json + .as_object() + .map(|m| m.is_empty()) + .unwrap_or(false), + "out object should contain no additional keys" + ); + + let mut json2sbp_out: Vec = Vec::new(); + + converters::json2sbp( + Cursor::new(sbp2json_out), + &mut json2sbp_out, + true, + error_handler_opt, + ) + .expect("can run json2sbp"); + + assert_eq!(data, json2sbp_out, "completed round trip"); +} + #[test] fn test_stop_on_error() { let root = find_project_root().unwrap();