liminal-rs 0.16.4

A conversation-based messaging bus built on beamr
Documentation
//! ACCESS-002 R1: the link route on the wire. An offer, each outcome and a
//! sealed crossing round-trip exactly; a bad end or status byte refuses to
//! decode; the link frames live on application streams.

use super::super::tests_support::{link_frames, round_trip};
use super::super::{decode, encode, encoded_len};
use crate::protocol::{Frame, FrameType, LinkEnd, LinkOffer, ProtocolError};

#[test]
fn link_frames_round_trip_exactly() -> Result<(), ProtocolError> {
    for frame in link_frames() {
        assert_eq!(round_trip(&frame)?, frame);
    }
    Ok(())
}

#[test]
fn link_offer_machine_key_is_fixed_length_and_raw() -> Result<(), ProtocolError> {
    let frame = Frame::LinkOpen {
        flags: 0,
        stream_id: 1,
        offer: LinkOffer {
            link_id: "l".to_owned(),
            end: LinkEnd::Export,
            machine: [0xEE; 32],
            handshake: vec![9],
        },
    };
    let mut buffer = vec![0_u8; encoded_len(&frame)?];
    let written = encode(&frame, &mut buffer)?;
    // header 10 + link_id (4 + 1) + end 1 + machine 32 + handshake (4 + 1)
    assert_eq!(written, 10 + 5 + 1 + 32 + 5);
    let machine = buffer.get(10 + 5 + 1..10 + 5 + 1 + 32);
    assert_eq!(machine, Some(&[0xEE_u8; 32][..]));
    Ok(())
}

#[test]
fn a_link_end_byte_outside_the_two_ends_refuses_to_decode() {
    // header: type, flags, stream 1, payload length 11; payload: link_id "l",
    // end byte 7, then nothing else (the refusal comes before the key read).
    let mut bytes = vec![u8::from(FrameType::LinkOpen), 0, 0, 0, 0, 1, 0, 0, 0, 6];
    bytes.extend_from_slice(&[0, 0, 0, 1, b'l', 7]);
    assert!(matches!(
        decode(&bytes),
        Err(ProtocolError::CodecError { .. })
    ));
}

#[test]
fn a_link_open_ack_status_outside_the_three_outcomes_refuses_to_decode() {
    let bytes = [
        u8::from(FrameType::LinkOpenAck),
        0,
        0,
        0,
        0,
        1,
        0,
        0,
        0,
        1,
        9,
    ];
    assert!(matches!(
        decode(&bytes),
        Err(ProtocolError::CodecError { .. })
    ));
}

#[test]
fn link_frames_live_on_application_streams() {
    for frame_type in [
        FrameType::LinkOpen,
        FrameType::LinkOpenAck,
        FrameType::LinkMessage,
    ] {
        let on_control_stream = [u8::from(frame_type), 0, 0, 0, 0, 0, 0, 0, 0, 0];
        assert!(matches!(
            decode(&on_control_stream),
            Err(ProtocolError::InvalidStream { .. })
        ));
    }
}