summaryrefslogtreecommitdiff
path: root/tests/sftp.rs
blob: b28e5492a9abdab6643c561330e09c995e0a9090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::io::prelude::*;
use std::fs::{self, File};
use tempdir::TempDir;

#[test]
fn smoke() {
    let (_tcp, sess) = ::authed_session();
    sess.sftp().unwrap();
}

#[test]
fn ops() {
    let td = TempDir::new("foo").unwrap();
    File::create(&td.path().join("foo")).unwrap();
    fs::create_dir(&td.path().join("bar")).unwrap();

    let (_tcp, sess) = ::authed_session();
    let sftp = sess.sftp().unwrap();
    sftp.opendir(&td.path().join("bar")).unwrap();
    let mut foo = sftp.open(&td.path().join("foo")).unwrap();
    sftp.mkdir(&td.path().join("bar2"), 0o755).unwrap();
    assert_eq!(fs::metadata(&td.path().join("bar2")).map(|m| m.is_dir()),
               Ok(true));
    sftp.rmdir(&td.path().join("bar2")).unwrap();

    sftp.create(&td.path().join("foo5")).unwrap().write_all(b"foo").unwrap();
    let mut v = Vec::new();
    File::open(&td.path().join("foo5")).unwrap().read_to_end(&mut v).unwrap();
    assert_eq!(v, b"foo");

    assert_eq!(sftp.stat(&td.path().join("foo")).unwrap().size, Some(0));
    v.truncate(0);
    foo.read_to_end(&mut v).unwrap();
    assert_eq!(v, Vec::new());

    sftp.symlink(&td.path().join("foo"),
                 &td.path().join("foo2")).unwrap();
    let readlink = sftp.readlink(&td.path().join("foo2")).unwrap();
    assert!(readlink == td.path().join("foo"));
    let realpath = sftp.realpath(&td.path().join("foo2")).unwrap();
    assert!(realpath == td.path().join("foo"));

    let files = sftp.readdir(td.path()).unwrap();
    assert_eq!(files.len(), 4);
}