forked from reiver/go-telnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_writer_test.go
116 lines (86 loc) · 2.38 KB
/
data_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
package telnet
import (
"bytes"
"testing"
)
func TestDataWriter(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{255},
Expected: []byte{255,255},
},
{
Bytes: []byte{255,255},
Expected: []byte{255,255,255,255},
},
{
Bytes: []byte{255,255,255},
Expected: []byte{255,255,255,255,255,255},
},
{
Bytes: []byte{255,255,255,255},
Expected: []byte{255,255,255,255,255,255,255,255},
},
{
Bytes: []byte{255,255,255,255,255},
Expected: []byte{255,255,255,255,255,255,255,255,255,255},
},
{
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)
writer := newDataWriter(subWriter)
n, err := writer.Write(test.Bytes)
if nil != err {
t.Errorf("For test #%d, did not expected 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
}
}
}