-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoptions_test.go
118 lines (109 loc) · 2.56 KB
/
options_test.go
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
110
111
112
113
114
115
116
117
118
package telnet_test
import (
"bytes"
"io"
"io/ioutil"
"net"
"sync"
"testing"
"time"
"github.com/aprice/telnet"
)
func TestServerNAWS(t *testing.T) {
client, server := net.Pipe()
text := []byte("hello\n")
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
defer wg.Done()
defer client.Close()
b := make([]byte, 3)
_, err := client.Read(b)
if err != nil {
t.Error(err)
}
if !bytes.Equal(b, []byte{255, 253, 31}) {
t.Errorf("Expected IAC DO NAWS, received %v", b)
}
// IAC WILL NAWS IAC SB NAWS W[1] W[0] H[1] H[0] IAC SE
payload := []byte{255, 251, 31, 255, 250, 31, 0, 80, 0, 20, 255, 240}
payload = append(payload, text...)
_, err = client.Write(payload)
if err != nil {
t.Error(err)
}
}()
conn := telnet.NewConnection(server, []telnet.Option{telnet.NAWSOption})
b := make([]byte, 32)
n, err := conn.Read(b)
if err != nil {
t.Error(err)
}
if !bytes.Equal(text, b[:n]) {
t.Errorf("Expected %q, got %q", text, b[:n])
}
wg.Wait()
conn.Close()
nw := conn.OptionHandlers[31].(*telnet.NAWSHandler)
if nw.Width != 80 || nw.Height != 20 {
t.Logf("%#v", conn)
t.Errorf("Expected w %d, h %d, got w %d, h %d", 80, 20, nw.Width, nw.Height)
}
}
func TestClientNAWS(t *testing.T) {
client, server := net.Pipe()
go func() {
conn := telnet.NewConnection(client, []telnet.Option{telnet.ExposeNAWS})
b := make([]byte, 32)
conn.Read(b)
conn.Close()
}()
_, err := server.Write([]byte{255, 253, 31})
if err != nil {
t.Error(err)
}
expected := []byte{255, 251, 31, 255, 250, 31, 255, 255, 255, 255, 255, 255, 255, 255, 255, 240}
buf := bytes.NewBuffer(nil)
server.SetReadDeadline(time.Now().Add(time.Second))
n, err := io.CopyN(buf, server, int64(len(expected)))
if err != nil {
t.Error(err)
}
b := buf.Bytes()
server.Close()
// IAC WILL NAWS IAC SB NAWS W[1] W[0] H[1] H[0] IAC SE
if !bytes.Equal(b[:n], expected) {
t.Errorf("Expected %v, received %v", expected, b[:n])
}
}
func TestOnlyServerSupportsNAWS(t *testing.T) {
client, server := net.Pipe()
text := []byte("hello\n")
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
conn := telnet.NewConnection(client, []telnet.Option{})
b := make([]byte, 32)
n, err := conn.Read(b)
if err != nil {
t.Error(err)
}
conn.Close()
if !bytes.Equal(text, b[:n]) {
t.Errorf("Expected %q, got %q", text, b[:n])
}
wg.Done()
}()
wg.Add(1)
go func() {
conn := telnet.NewConnection(server, []telnet.Option{telnet.NAWSOption})
go io.Copy(ioutil.Discard, conn)
_, err := conn.Write(text)
if err != nil {
t.Error(err)
}
conn.Close()
wg.Done()
}()
wg.Wait()
}