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,
},
)
}
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(())
}
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)
}
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(())
}