#ssh #test-server #integration-tests #test

ssh-test-server

In memoery ssh server for integration testing

2 releases

0.1.1 Nov 8, 2024
0.1.0 Nov 8, 2024

#762 in Testing

Download history 182/week @ 2024-11-04 39/week @ 2024-11-11

221 downloads per month
Used in ssh-test-server-cli

MIT/Apache

29KB
513 lines

In memory ssh server

ssh-test-server provides ssh server to that can be used in integration testing.

Usage

use ssh_test_server::{SshServerBuilder, User};

#[tokio::main(flavor = "current_thread")]
async fn main() {
    let ssh = SshServerBuilder::default()
        .add_user(User::new_admin("root", "pass123"))
        .run()
        .await
        .unwrap();

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

    tokio::signal::ctrl_c().await.unwrap();
}

Contributions

Contributions are welcome! Please open an issue or submit a pull request on Gitlab.

Before sending pull request please run lints and tests first:

cargo install just
just install-dev
just lint
just test

License

This project is licensed under the MIT OR Apache-2.0 License.


lib.rs:

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());

Dependencies

~15–45MB
~723K SLoC