summaryrefslogtreecommitdiff
path: root/tests/channel.rs
blob: 38a39aa4b71340100cd2572d98accf44c0255d76 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::io::prelude::*;
use std::net::{TcpStream, TcpListener};
use std::thread;

#[test]
fn smoke() {
    let (_tcp, sess) = ::authed_session();
    let mut channel = sess.channel_session().unwrap();
    channel.flush().unwrap();
    channel.exec("true").unwrap();
    channel.wait_eof().unwrap();
    assert!(channel.eof());
    assert_eq!(channel.exit_status().unwrap(), 0);
    channel.close().unwrap();
    channel.wait_close().unwrap();
    assert!(channel.eof());
}

#[test]
fn reading_data() {
    let (_tcp, sess) = ::authed_session();
    let mut channel = sess.channel_session().unwrap();
    channel.exec("echo foo").unwrap();
    let mut output = String::new();
    channel.read_to_string(&mut output).unwrap();
    assert_eq!(output, "foo\n");
}

#[test]
fn writing_data() {
    let (_tcp, sess) = ::authed_session();
    let mut channel = sess.channel_session().unwrap();
    channel.exec("read foo && echo $foo").unwrap();
    channel.write_all(b"foo\n").unwrap();
    channel.close().unwrap();
    let mut output = String::new();
    channel.read_to_string(&mut output).unwrap();
    assert_eq!(output, "foo\n");
}

#[test]
fn eof() {
    let (_tcp, sess) = ::authed_session();
    let mut channel = sess.channel_session().unwrap();
    channel.adjust_receive_window(10, false).unwrap();
    channel.exec("read foo").unwrap();
    channel.send_eof().unwrap();
    let mut output = String::new();
    channel.read_to_string(&mut output).unwrap();
    assert_eq!(output, "");
}

#[test]
fn shell() {
    let (_tcp, sess) = ::authed_session();
    let mut channel = sess.channel_session().unwrap();
    channel.request_pty("xterm", None, None).unwrap();
    channel.shell().unwrap();
}

#[test]
fn setenv() {
    let (_tcp, sess) = ::authed_session();
    let mut channel = sess.channel_session().unwrap();
    let _ = channel.setenv("FOO", "BAR");
    channel.close().unwrap();
}

#[test]
fn direct() {
    let a = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = a.socket_addr().unwrap();
    let t = thread::scoped(move|| {
        let mut s = a.accept().unwrap().0;
        let b = &mut [0, 0, 0];
        s.read(b).unwrap();
        assert_eq!(b.as_slice(), [1, 2, 3].as_slice());
        s.write_all(&[4, 5, 6]).unwrap();
    });
    let (_tcp, sess) = ::authed_session();
    let mut channel = sess.channel_direct_tcpip("127.0.0.1",
                                                addr.port(), None).unwrap();
    channel.write_all(&[1, 2, 3]).unwrap();
    let r = &mut [0, 0, 0];
    channel.read(r).unwrap();
    assert_eq!(r.as_slice(), [4, 5, 6].as_slice());
    t.join();
}

#[test]
fn forward() {
    let (_tcp, sess) = ::authed_session();
    let (mut listen, port) = sess.channel_forward_listen(39249, None, None)
                                 .unwrap();
    let t = thread::scoped(move|| {
        let mut s = TcpStream::connect(&("127.0.0.1", port)).unwrap();
        let b = &mut [0, 0, 0];
        s.read(b).unwrap();
        assert_eq!(b.as_slice(), [1, 2, 3].as_slice());
        s.write_all(&[4, 5, 6]).unwrap();
    });

    let mut channel = listen.accept().unwrap();
    channel.write_all(&[1, 2, 3]).unwrap();
    let r = &mut [0, 0, 0];
    channel.read(r).unwrap();
    assert_eq!(r.as_slice(), [4, 5, 6].as_slice());
    t.join();
}