use weir_core::{Durability, Envelope, Header, MessageType, WIRE_VERSION};
const REFERENCE_PUSH_HELLO_SYNC: &[u8; 25] = &[
0x57, 0x45, 0x49, 0x52, 0x01, 0x01, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00, 0x66, 0xad, 0x7d, 0x3c, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x86, 0xa6, 0x10, 0x36, ];
#[test]
fn push_hello_sync_encodes_to_reference_bytes() {
let header = Header::new(MessageType::Push, Durability::Sync, 0);
assert_eq!(
header.version(),
WIRE_VERSION,
"test assumes WIRE_VERSION = 1"
);
let envelope = Envelope::new(header, b"hello".to_vec());
let encoded = envelope.encode();
assert_eq!(
encoded.as_slice(),
REFERENCE_PUSH_HELLO_SYNC.as_slice(),
"encoder drifted from docs/wire_protocol.md Worked Examples → \
Push of 5-byte payload. Either restore byte-compatibility or \
update the doc and this constant."
);
}
const REFERENCE_ACK: &[u8; 20] = &[
0x57, 0x45, 0x49, 0x52, 0x01, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc9, 0x47, 0x4b, 0x3a, 0x00, 0x00, 0x00, 0x00, ];
#[test]
fn ack_encodes_to_reference_bytes() {
let header = Header::new(MessageType::Ack, Durability::Sync, 0);
let envelope = Envelope::new(header, Vec::new());
let encoded = envelope.encode();
assert_eq!(
encoded.as_slice(),
REFERENCE_ACK.as_slice(),
"encoder drifted from docs/wire_protocol.md Worked Examples → \
Ack response. Either restore byte-compatibility or update the \
doc and this constant."
);
}
const REFERENCE_NACK_PAYLOAD_TOO_LARGE: &[u8; 21] = &[
0x57, 0x45, 0x49, 0x52, 0x01, 0x03, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x2b, 0x80, 0x24, 0x04, 0x94, 0x2b, 0x6f, 0xd5, ];
#[test]
fn nack_payload_too_large_encodes_to_reference_bytes() {
use weir_core::NackReason;
let header = Header::new(MessageType::Nack, Durability::Sync, 0);
let envelope = Envelope::new(header, vec![NackReason::PayloadTooLarge as u8]);
let encoded = envelope.encode();
assert_eq!(
encoded.as_slice(),
REFERENCE_NACK_PAYLOAD_TOO_LARGE.as_slice(),
"encoder drifted from docs/wire_protocol.md Worked Examples → \
Nack(PayloadTooLarge). Either restore byte-compatibility or \
update the doc and this constant."
);
}
const REFERENCE_NACK_VERSION_MISMATCH: &[u8; 22] = &[
0x57,
0x45,
0x49,
0x52, 0x01, 0x03, 0x01, 0x00, 0x02,
0x00,
0x00,
0x00, 0xf6,
0x84,
0x35,
0x36, 0x02,
WIRE_VERSION, 0xeb,
0x40,
0xe8,
0x04, ];
#[test]
fn nack_version_mismatch_encodes_to_reference_bytes() {
use weir_core::NackReason;
let header = Header::new(MessageType::Nack, Durability::Sync, 0);
let envelope = Envelope::new(
header,
vec![NackReason::VersionMismatch as u8, WIRE_VERSION],
);
let encoded = envelope.encode();
assert_eq!(
encoded.as_slice(),
REFERENCE_NACK_VERSION_MISMATCH.as_slice(),
"encoder drifted from docs/wire_protocol.md → \
Nack(VersionMismatch). Either restore byte-compatibility or \
update the doc and this constant."
);
}
const REFERENCE_NACK_BAD_HEADER_CRC: &[u8; 21] = &[
0x57, 0x45, 0x49, 0x52, 0x01, 0x03, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x18, 0x2b, 0x80, 0x24, 0x03, 0x37, 0xbe, 0x0b, 0x4b, ];
#[test]
fn nack_bad_header_crc_encodes_to_reference_bytes() {
use weir_core::NackReason;
let header = Header::new(MessageType::Nack, Durability::Sync, 0);
let envelope = Envelope::new(header, vec![NackReason::BadHeaderCrc as u8]);
let encoded = envelope.encode();
assert_eq!(
encoded.as_slice(),
REFERENCE_NACK_BAD_HEADER_CRC.as_slice(),
"encoder drifted from docs/wire_protocol.md → \
Nack(BadHeaderCrc). Either restore byte-compatibility or \
update the doc and this constant."
);
}
const REFERENCE_HEALTHCHECK: &[u8; 20] = &[
0x57, 0x45, 0x49, 0x52, 0x01, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf3, 0x72, 0x9b, 0x59, 0x00, 0x00, 0x00, 0x00, ];
#[test]
fn healthcheck_encodes_to_reference_bytes() {
let header = Header::new(MessageType::HealthCheck, Durability::Sync, 0);
let envelope = Envelope::new(header, Vec::new());
let encoded = envelope.encode();
assert_eq!(
encoded.as_slice(),
REFERENCE_HEALTHCHECK.as_slice(),
"encoder drifted from docs/wire_protocol.md → \
HealthCheck request. Either restore byte-compatibility or \
update the doc and this constant."
);
}
const REFERENCE_HEALTHCHECK_RESPONSE: &[u8; 20] = &[
0x57, 0x45, 0x49, 0x52, 0x01, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x79, 0xec, 0xff, 0x00, 0x00, 0x00, 0x00, ];
#[test]
fn healthcheck_response_encodes_to_reference_bytes() {
let header = Header::new(MessageType::HealthCheckResponse, Durability::Sync, 0);
let envelope = Envelope::new(header, Vec::new());
let encoded = envelope.encode();
assert_eq!(
encoded.as_slice(),
REFERENCE_HEALTHCHECK_RESPONSE.as_slice(),
"encoder drifted from docs/wire_protocol.md → \
HealthCheckResponse. Either restore byte-compatibility or \
update the doc and this constant."
);
}
#[test]
fn crc_algorithm_is_ieee_not_castagnoli() {
let crc = crc32fast::hash(b"hello");
assert_eq!(
crc, 0x3610_a686,
"crc32fast::hash(b\"hello\") should be 0x3610a686 under the IEEE \
polynomial; if this fails the dep has switched algorithms and \
every wire frame is now incompatible"
);
}
#[test]
fn empty_payload_crc_is_zero() {
assert_eq!(crc32fast::hash(&[]), 0);
}