Lines
98.47 %
Functions
100 %
Branches
//! The eval subsystem's wire rules and limits, over a real SSH connection.
use std::net::SocketAddr;
use rpc::MAX_FRAME_BYTES;
use russh::client::{self, Handle};
use russh::keys::PrivateKey;
use russh::{Channel, ChannelMsg, Disconnect};
use server::db::install_test_pool;
use sqlx::PgPool;
use sqlx::types::Uuid;
use supp_macro::local_db_sqlx_test;
use crate::eval_subsystem_tests::{
DEADLINE, Daemon, TrustingClient, connect, insert_verified_user, open_subsystem, random_key,
read_until_closed, register_key, response_lines, setup, start_daemon,
};
async fn daemon_with_user(pool: &PgPool) -> anyhow::Result<(Daemon, PrivateKey)> {
let user_id = Uuid::new_v4();
insert_verified_user(pool, user_id).await?;
let key = random_key()?;
register_key(user_id, &key).await?;
Ok((start_daemon().await?, key))
}
async fn connected(addr: SocketAddr, key: PrivateKey) -> anyhow::Result<Handle<TrustingClient>> {
connect(addr, key, client::Config::default().window_size).await
/// Sends `input`, half-closes and reads until the daemon ends the channel.
async fn exchange(
session: &Handle<TrustingClient>,
input: &[u8],
) -> anyhow::Result<crate::eval_subsystem_tests::Transcript> {
let mut channel = open_subsystem(session).await?;
channel.data(input).await?;
channel.eof().await?;
Ok(read_until_closed(&mut channel).await)
async fn first_data(channel: &mut Channel<client::Msg>) -> anyhow::Result<Vec<u8>> {
while let Some(msg) = channel.wait().await {
if let ChannelMsg::Data { data } = msg {
return Ok(data.to_vec());
anyhow::bail!("the channel ended without a reply")
async fn run_with_user<T>(
pool: &PgPool,
scenario: impl AsyncFnOnce(SocketAddr, PrivateKey) -> anyhow::Result<T>,
) -> anyhow::Result<T> {
let (daemon, key) = daemon_with_user(pool).await?;
let outcome = tokio::time::timeout(DEADLINE, scenario(daemon.addr, key)).await;
daemon.stop().await?;
outcome?
#[local_db_sqlx_test]
async fn a_third_eval_channel_is_refused_until_one_closes(pool: PgPool) -> anyhow::Result<()> {
run_with_user(&pool, async |addr, key| {
let session = connected(addr, key).await?;
let mut first = open_subsystem(&session).await?;
let mut second = open_subsystem(&session).await?;
let mut third = open_subsystem(&session).await?;
let refused = read_until_closed(&mut third).await;
assert!(refused.failed && refused.closed, "{refused:?}");
assert!(refused.output.is_empty(), "{refused:?}");
for channel in [&mut first, &mut second] {
channel
.data(&b"(:id 1 :form (rpc-protocol-version))\n"[..])
.await?;
assert_eq!(first_data(channel).await?, b"(:id 1 :value 1)\n");
first.eof().await?;
let ended = read_until_closed(&mut first).await;
assert!(ended.closed, "{ended:?}");
let reopened = exchange(&session, b"(:id 2 :form (rpc-protocol-version))\n").await?;
assert!(!reopened.failed, "{reopened:?}");
assert_eq!(response_lines(&reopened.output), ["(:id 2 :value 1)"]);
session
.disconnect(Disconnect::ByApplication, "done", "")
Ok(())
})
async fn a_stray_etx_byte_is_an_error_not_an_interrupt(pool: PgPool) -> anyhow::Result<()> {
let transcript = exchange(&session, b"\x03\n").await?;
let lines = response_lines(&transcript.output);
assert_eq!(lines.len(), 1, "{lines:?}");
assert!(
lines[0].starts_with("(:id 0 :error (:code parse"),
"{lines:?}"
);
assert_eq!(transcript.exit_status, Some(0));
async fn eof_answers_both_streams_in_full_before_exit_status_zero(
pool: PgPool,
) -> anyhow::Result<()> {
let forms: String = (1..=20)
.map(|id| match id % 2 {
0 => format!("(:id {id} :stream 1 :form (rpc-protocol-version))\n"),
_ => format!("(:id {id} :form (rpc-protocol-version))\n"),
.collect();
let transcript = exchange(&session, forms.as_bytes()).await?;
let mut ids: Vec<usize> = lines
.iter()
.filter_map(|line| line.strip_prefix("(:id ")?.split_once(' ')?.0.parse().ok())
ids.sort_unstable();
assert_eq!(ids, (1..=20).collect::<Vec<_>>(), "{lines:?}");
assert!(!transcript.data_after_eof, "{transcript:?}");
async fn a_frame_over_one_mebibyte_gets_a_parse_reply_then_the_channel_ends(
let mut channel = open_subsystem(&session).await?;
let oversized = format!("({}", "a".repeat(MAX_FRAME_BYTES));
channel.data(oversized.as_bytes()).await?;
let transcript = read_until_closed(&mut channel).await;
assert!(transcript.closed, "{transcript:?}");
async fn stream_zero_bytes_are_unchanged_end_to_end(pool: PgPool) -> anyhow::Result<()> {
let transcript = exchange(
&session,
b"(:id 1 :form (rpc-protocol-version))\n(:id 2 :stream 0 :form (rpc-protocol-version))\n",
)
assert_eq!(transcript.output, b"(:id 1 :value 1)\n(:id 2 :value 1)\n");
/// Eval forms of one frame each: a closed channel keeps draining them, answered `interrupted`.
fn many_frames() -> String {
"(:id 1 :form 1)\n".repeat(20_000)
async fn a_closed_channel_counts_until_it_is_gone(pool: PgPool) -> anyhow::Result<()> {
let closing = open_subsystem(&session).await?;
let _idle = open_subsystem(&session).await?;
closing.data(many_frames().as_bytes()).await?;
closing.close().await?;
third.eof().await?;
let accepted = loop {
let transcript = exchange(&session, b"(:id 2 :form (rpc-protocol-version))\n").await?;
if !transcript.failed {
break transcript;
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
assert_eq!(response_lines(&accepted.output), ["(:id 2 :value 1)"]);