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::{TcpListener, Listener, Acceptor, TcpStream};
#[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 output = channel.read_to_string().unwrap();
assert_eq!(output.as_slice(), "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(b"foo\n").unwrap();
channel.close().unwrap();
let output = channel.read_to_string().unwrap();
assert_eq!(output.as_slice(), "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 output = channel.read_to_string().unwrap();
assert_eq!(output.as_slice(), "");
}
#[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 mut l = TcpListener::bind("127.0.0.1:0").unwrap();
let addr = l.socket_name().unwrap();
let mut a = l.listen().unwrap();
let (tx, rx) = channel();
spawn(proc() {
let mut s = a.accept().unwrap();
let b = &mut [0, 0, 0];
s.read(b).unwrap();
assert_eq!(b.as_slice(), [1, 2, 3].as_slice());
s.write(&[4, 5, 6]).unwrap();
tx.send(());
});
let (_tcp, sess) = ::authed_session();
let mut channel = sess.channel_direct_tcpip("127.0.0.1",
addr.port, None).unwrap();
channel.write(&[1, 2, 3]).unwrap();
let r = &mut [0, 0, 0];
channel.read(r).unwrap();
assert_eq!(r.as_slice(), [4, 5, 6].as_slice());
rx.recv();
}
#[test]
fn forward() {
let (_tcp, sess) = ::authed_session();
let (mut listen, port) = sess.channel_forward_listen(39249, None, None)
.unwrap();
let (tx, rx) = channel();
spawn(proc() {
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(&[4, 5, 6]).unwrap();
tx.send(());
});
let mut channel = listen.accept().unwrap();
channel.write(&[1, 2, 3]).unwrap();
let r = &mut [0, 0, 0];
channel.read(r).unwrap();
assert_eq!(r.as_slice(), [4, 5, 6].as_slice());
rx.recv();
}
|