liminal-server 0.25.4

Standalone server for the liminal messaging bus
Documentation
//! ACCESS-010 R2: an exchange that gates nothing (no bearer, no pass
//! verifier, no revocation authority) admits whatever record a `Connect`
//! carries, a Lys pass record included, as an open-access connection with no
//! holder and no session, and its hot routes admit that connection with no
//! session; a gated exchange refuses the same records with today's words.

use liminal_protocol::credential::{LYS_PASS_V1_TAG, WIRE_PASS_V1_TAG};

use super::*;

fn connect(
    runtime: &ConnectionRuntime,
    state: &mut ConnectionProcessState,
    credential: Vec<u8>,
) -> FrameAction {
    let version = ProtocolVersion::new(1, 0);
    apply_frame(
        PID,
        runtime,
        state,
        Frame::Connect {
            flags: 0,
            min_version: version,
            max_version: version,
            auth_token: credential,
        },
    )
}

/// The words a refused connect answered on its error frame.
fn connect_refusal(action: FrameAction) -> Result<String, Failure> {
    match action {
        FrameAction::RespondThenClose(Frame::ConnectError {
            message: Some(message),
            ..
        }) => Ok(message),
        other => Err(format!("the connect was not refused: {other:?}").into()),
    }
}

fn admitted_open(
    runtime: &ConnectionRuntime,
    state: &mut ConnectionProcessState,
    credential: Vec<u8>,
) -> Result<(), Failure> {
    let action = connect(runtime, state, credential);
    if !matches!(action, FrameAction::Respond(Frame::ConnectAck { .. })) {
        return Err(format!("the record was not admitted: {action:?}").into());
    }
    if !state.authenticated || state.verified_holder.is_some() || state.lys_session.is_some() {
        return Err("an open-access connection holds a holder or a session".into());
    }
    if state.pass_principal.is_some() {
        return Err("an open-access connection holds a pass principal".into());
    }
    Ok(())
}

/// A runtime that gates nothing: no bearer, no verifier, no admitter.
fn open_runtime(door: &Arc<Door>) -> Result<ConnectionRuntime, Failure> {
    let runtime = ConnectionRuntime::for_tests(Arc::clone(door) as Arc<dyn ConnectionServices>);
    runtime.register_for_tests(PID)?;
    Ok(runtime)
}

/// A runtime gated by a bearer and nothing else: no admitter is installed.
fn gated_runtime(door: &Arc<Door>) -> Result<ConnectionRuntime, Failure> {
    let runtime = ConnectionRuntime::for_tests_with_auth_token(
        Arc::clone(door) as Arc<dyn ConnectionServices>,
        b"bearer-under-test".to_vec(),
    );
    runtime.register_for_tests(PID)?;
    Ok(runtime)
}

#[test]
fn an_open_exchange_admits_a_lys_record_as_an_open_access_connection() -> Result<(), Failure> {
    let door = Arc::new(Door::default());
    let runtime = open_runtime(&door)?;
    let mut state = ConnectionProcessState::default();
    admitted_open(
        &runtime,
        &mut state,
        WireCredential::lys_record(b"not-a-pass"),
    )
}

#[test]
fn an_open_exchange_admits_every_other_record_the_same_way() -> Result<(), Failure> {
    let door = Arc::new(Door::default());
    for credential in [
        Vec::new(),
        vec![LYS_PASS_V1_TAG],
        vec![WIRE_PASS_V1_TAG, 7, 8],
        vec![9, 9],
    ] {
        let runtime = open_runtime(&door)?;
        let mut state = ConnectionProcessState::default();
        admitted_open(&runtime, &mut state, credential)?;
    }
    Ok(())
}

#[test]
fn the_hot_routes_admit_an_open_access_connection_with_no_session() -> Result<(), Failure> {
    let door = Arc::new(Door::default());
    let runtime = open_runtime(&door)?;
    let mut state = ConnectionProcessState::default();
    admitted_open(
        &runtime,
        &mut state,
        WireCredential::lys_record(b"not-a-pass"),
    )?;
    assert!(matches!(
        open(&runtime, &mut state, "orders"),
        FrameAction::NoResponse
    ));
    assert_eq!(door.opens()?, vec![(CONVERSATION, "orders".to_owned())]);
    assert!(matches!(
        message(&runtime, &mut state),
        FrameAction::NoResponse
    ));
    assert_eq!(door.messages()?, 1);
    Ok(())
}

#[test]
fn a_gated_exchange_with_no_admitter_refuses_a_lys_record_by_name() -> Result<(), Failure> {
    let door = Arc::new(Door::default());
    let runtime = gated_runtime(&door)?;
    let mut state = ConnectionProcessState::default();
    let words = connect_refusal(connect(
        &runtime,
        &mut state,
        WireCredential::lys_record(b"not-a-pass"),
    ))?;
    assert_eq!(
        words,
        "lys pass admission refused: no lys admitter is installed"
    );
    assert!(!state.authenticated);
    Ok(())
}

#[test]
fn a_gated_exchange_refuses_an_unknown_record_as_today() -> Result<(), Failure> {
    let door = Arc::new(Door::default());
    let runtime = gated_runtime(&door)?;
    let mut state = ConnectionProcessState::default();
    let words = connect_refusal(connect(&runtime, &mut state, vec![9, 9]))?;
    assert_eq!(words, "connection authentication token rejected");
    assert!(!state.authenticated);
    Ok(())
}