-
Notifications
You must be signed in to change notification settings - Fork 6
/
connection_test.go
99 lines (93 loc) · 1.93 KB
/
connection_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
package telnet_test
import (
"bytes"
"net"
"testing"
"github.com/aprice/telnet"
)
func TestConnection_Write(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
}{
{
name: "plain",
input: []byte("hello world"),
expected: []byte("hello world"),
},
{
name: "iac",
input: []byte("hello \xffworld"),
expected: []byte("hello \xff\xffworld"),
},
{
name: "doubleiac",
input: []byte("hello \xff\xffworld"),
expected: []byte("hello \xff\xff\xff\xffworld"),
},
}
buf := bytes.NewBuffer(nil)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
buf.Reset()
client, server := net.Pipe()
go func() {
conn := telnet.NewConnection(server, nil)
_, err := conn.Write(test.input)
if err != nil {
t.Error(err)
}
conn.Close()
}()
buf.ReadFrom(client)
client.Close()
if !bytes.Equal(test.expected, buf.Bytes()) {
t.Errorf("Expected %v, got %v", test.expected, buf.Bytes())
}
})
}
}
func TestConnection_Read(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
}{
{
name: "plain",
input: []byte("hello world"),
expected: []byte("hello world"),
},
{
name: "iac",
input: []byte("hello \xff\xffworld"),
expected: []byte("hello \xffworld"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client, server := net.Pipe()
go func() {
client.Write(test.input)
t.Log("Finished writing")
client.Close()
t.Log("Closed client")
}()
conn := telnet.NewConnection(server, nil)
b := make([]byte, len(test.expected))
_, err := conn.Read(b)
if err != nil {
t.Error(err)
}
conn.Close()
if !bytes.Equal(test.expected, b) {
t.Errorf("Expected %v, got %v", test.expected, b)
}
})
}
}
type closerBuf struct {
*bytes.Buffer
}
func (c *closerBuf) Close() error { return nil }