-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter_test.go
133 lines (115 loc) · 3.49 KB
/
writer_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package telnet
import (
"bytes"
"testing"
)
func TestWriter_Write(t *testing.T) {
tests := []struct {
Bytes []byte
Expected []byte
}{
{
Bytes: []byte{},
Expected: []byte{},
},
{
Bytes: []byte("apple"),
Expected: []byte("apple"),
},
{
Bytes: []byte("banana"),
Expected: []byte("banana"),
},
{
Bytes: []byte("cherry"),
Expected: []byte("cherry"),
},
{
Bytes: []byte("apple banana cherry"),
Expected: []byte("apple banana cherry"),
},
{
Bytes: []byte{IAC},
Expected: []byte{IAC, IAC},
},
{
Bytes: []byte{IAC, IAC},
Expected: []byte{IAC, IAC, IAC, IAC},
},
{
Bytes: []byte{IAC, IAC, IAC},
Expected: []byte{IAC, IAC, IAC, IAC, IAC, IAC},
},
{
Bytes: []byte{IAC, IAC, IAC, IAC},
Expected: []byte{IAC, IAC, IAC, IAC, IAC, IAC, IAC, IAC},
},
{
Bytes: []byte{IAC, IAC, IAC, IAC, IAC},
Expected: []byte{IAC, IAC, IAC, IAC, IAC, IAC, IAC, IAC, IAC, IAC},
},
{
Bytes: []byte("apple\xffbanana\xffcherry"),
Expected: []byte("apple\xff\xffbanana\xff\xffcherry"),
},
{
Bytes: []byte("\xffapple\xffbanana\xffcherry\xff"),
Expected: []byte("\xff\xffapple\xff\xffbanana\xff\xffcherry\xff\xff"),
},
{
Bytes: []byte("apple\xff\xffbanana\xff\xffcherry"),
Expected: []byte("apple\xff\xff\xff\xffbanana\xff\xff\xff\xffcherry"),
},
{
Bytes: []byte("\xff\xffapple\xff\xffbanana\xff\xffcherry\xff\xff"),
Expected: []byte("\xff\xff\xff\xffapple\xff\xff\xff\xffbanana\xff\xff\xff\xffcherry\xff\xff\xff\xff"),
},
}
// TODO: Add random tests.
for testNumber, test := range tests {
subWriter := new(bytes.Buffer)
telnetWriter := newWriter(subWriter)
n, err := telnetWriter.Write(test.Bytes)
if err != nil {
t.Errorf("For test #%d, did not expect an error, but actually got one: (%T) %v; for %q -> %q.", testNumber, err, err, string(test.Bytes), string(test.Expected))
continue
}
if expected, actual := len(test.Bytes), n; expected != actual {
t.Errorf("For test #%d, expected %d, but actually got %d; for %q -> %q.", testNumber, expected, actual, string(test.Bytes), string(test.Expected))
continue
}
if expected, actual := string(test.Expected), subWriter.String(); expected != actual {
t.Errorf("For test #%d, expected %q, but actually got %q; for %q -> %q.", testNumber, expected, actual, string(test.Bytes), string(test.Expected))
continue
}
}
}
func TestWriter_WriteCommand(t *testing.T) {
tests := []struct {
Bytes []byte
Expected []byte
}{
{
Bytes: []byte{IAC, WILL, ECHO},
Expected: []byte{IAC, WILL, ECHO},
},
}
// TODO: Add random tests.
for testNumber, test := range tests {
subWriter := new(bytes.Buffer)
telnetWriter := newWriter(subWriter)
n, err := WriteCommand(telnetWriter, test.Bytes[0], test.Bytes[1], test.Bytes[2])
if err != nil {
t.Errorf("For test #%d, did not expect an error, but actually got one: (%T) %v; for %q -> %q.", testNumber, err, err, string(test.Bytes), string(test.Expected))
continue
}
if expected, actual := len(test.Bytes), n; expected != actual {
t.Errorf("For test #%d, expected %d, but actually got %d; for %q -> %q.", testNumber, expected, actual, string(test.Bytes), string(test.Expected))
continue
}
if expected, actual := string(test.Expected), subWriter.String(); expected != actual {
t.Errorf("For test #%d, expected %q, but actually got %q; for %q -> %q.", testNumber, expected, actual, string(test.Bytes), string(test.Expected))
continue
}
}
}