Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test to round trip an invalid sbp message #1352

Merged
merged 2 commits into from
Jul 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions rust/sbp2json/tests/test_round_trips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = vec![0x55, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x65, 0x8D];
let mut sbp2json_out: Vec<u8> = 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<u8> = 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();
Expand Down
Loading