Crate ssh_test_server

Source
Expand description

This crate allow running mockup ssh server in integration tests.

§Examples

use ssh_test_server::{SshServerBuilder, SshExecuteContext, SshExecuteResult, User};

fn cmd_check_password(
    context: &SshExecuteContext,
    _program: &str,
    args: &[&str],
) -> SshExecuteResult {
    let mut args = args.iter();
    let (Some(login), Some(password)) = (args.next(), args.next()) else {
        return SshExecuteResult::stderr(2, "Usage: check_password <login> <password>");
    };

    if !context.current_admin() {
        return SshExecuteResult::stderr(1, "Permission denied.");
    }

    let users = context.users.lock().unwrap();
    let Some(user) = users.get(*login) else {
        return SshExecuteResult::stderr(1, format!("Unknown user {login}."));
    };

    if user.password() == *password {
        SshExecuteResult::stdout(0, "Password correct.")
    } else {
        SshExecuteResult::stderr(1, "Password does not match.")
    }
}

let ssh = SshServerBuilder::default()
    .add_user(User::new("user", "123"))
    .add_user(User::new_admin("root", "abc123"))
    .add_program("check_password", Box::new(cmd_check_password))
    .run()
    .await
    .unwrap();

println!("ssh -p {} root@{} check_password user 123", ssh.port(), ssh.host());

Structs§

SshExecuteContext
Context of ssh server passed to every custom function.
SshExecuteResult
Response that have to be returned by custom command handler.
SshServer
Running SSH server.
SshServerBuilder
Builder for the ssh server.
User
Ssh user.

Type Aliases§

SshExecuteHandler
Function signature for custom commands.
UsersMap
Users required in ssh server context. Key of the hash map is a user login.