use async_ssh2::Session;
use futures::io::{AsyncReadExt, AsyncWriteExt};
use ssh2::{HashType, MethodType};
use std::{env, fs::File, io::prelude::*, path::Path};
use tempfile::tempdir;
#[test]
fn session_is_send() {
fn must_be_send<T: Send>(_: &T) -> bool {
true
}
let sess = Session::new().unwrap();
assert!(must_be_send(&sess));
}
#[tokio::test]
async fn smoke() {
let socket = crate::socket().await;
let mut sess = Session::new().unwrap();
sess.set_tcp_stream(socket).unwrap();
assert!(sess.banner_bytes().is_none());
sess.set_banner("foo").await.unwrap();
assert!(!sess.is_blocking());
assert_eq!(sess.timeout(), 0);
sess.set_compress(true);
assert!(sess.host_key().is_none());
sess.method_pref(MethodType::Kex, "diffie-hellman-group14-sha1")
.unwrap();
assert!(sess.methods(MethodType::Kex).is_none());
sess.set_timeout(0);
sess.supported_algs(MethodType::Kex).unwrap();
sess.supported_algs(MethodType::HostKey).unwrap();
sess.channel_session().await.err().unwrap();
}
#[tokio::test]
async fn smoke_handshake() {
let user = env::var("USER").unwrap();
let socket = crate::socket().await;
let mut sess = Session::new().unwrap();
sess.set_tcp_stream(socket).unwrap();
sess.handshake().await.unwrap();
sess.host_key().unwrap();
let methods = sess.auth_methods(&user).await.unwrap();
assert!(methods.contains("publickey"), "{}", methods);
assert!(!sess.authenticated());
let mut agent = sess.agent().unwrap();
agent.connect().await.unwrap();
agent.list_identities().unwrap();
{
let identity = &agent.identities().unwrap()[0];
agent.userauth(&user, &identity).await.unwrap();
}
assert!(sess.authenticated());
sess.host_key_hash(HashType::Md5).unwrap();
}
#[tokio::test]
async fn keepalive() {
let sess = crate::authed_session().await;
sess.set_keepalive(false, 10);
sess.keepalive_send().await.unwrap();
}
#[tokio::test]
async fn scp_recv() {
let sess = crate::authed_session().await;
let p = Path::new(file!()).canonicalize().unwrap();
let (mut ch, _) = sess.scp_recv(&p).await.unwrap();
let mut data = String::new();
ch.read_to_string(&mut data).await.unwrap();
let mut expected = String::new();
File::open(&p)
.unwrap()
.read_to_string(&mut expected)
.unwrap();
assert!(data == expected);
}
#[tokio::test]
async fn scp_send() {
let td = tempdir().unwrap();
let sess = crate::authed_session().await;
let mut ch = sess
.scp_send(&td.path().join("foo"), 0o644, 6, None)
.await
.unwrap();
ch.write_all(b"foobar").await.unwrap();
drop(ch);
std::thread::sleep(std::time::Duration::from_millis(100));
let mut actual = Vec::new();
File::open(&td.path().join("foo"))
.unwrap()
.read_to_end(&mut actual)
.unwrap();
assert_eq!(actual, b"foobar");
}