use json_packer::{compress_to_bytes, CompressOptions};
use serde_json::json;
#[test]
fn bad_magic() {
let mut bytes = Vec::new();
bytes.extend_from_slice(b"BAD!");
bytes.push(0x01);
bytes.push(0x00); bytes.push(0x00); let err = json_packer::decompress_from_bytes(&bytes).unwrap_err();
assert!(matches!(err, json_packer::Error::BadMagic));
}
#[test]
fn bad_version() {
let mut bytes = Vec::new();
bytes.extend_from_slice(b"JCPR");
bytes.push(0xFF);
bytes.push(0x00); bytes.push(0x00); let err = json_packer::decompress_from_bytes(&bytes).unwrap_err();
assert!(matches!(err, json_packer::Error::BadVersion));
}
#[test]
fn truncated_data() {
let v = json!({"a": 1});
let mut bytes = compress_to_bytes(&v, &CompressOptions::default()).unwrap();
bytes.truncate(bytes.len().saturating_sub(3));
let err = json_packer::decompress_from_bytes(&bytes).unwrap_err();
matches!(err, json_packer::Error::BitstreamOutOfBounds | json_packer::Error::VarintError);
}
#[test]
fn utf8_error_in_string() {
let mut bytes = Vec::new();
bytes.extend_from_slice(b"JCPR");
bytes.push(0x01);
bytes.push(0x00);
bytes.push(0x00);
bytes.push(0b101);
bytes.push(0x01);
bytes.push(0xFF);
let err = json_packer::decompress_from_bytes(&bytes).unwrap_err();
matches!(err, json_packer::Error::Utf8(_));
}