-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
177 lines (139 loc) · 3.41 KB
/
utils_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package websocket
import (
"bufio"
"math/rand"
"strings"
"testing"
)
func TestMakeAcceptKey(t *testing.T) {
e := "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
k := makeAcceptKey("dGhlIHNhbXBsZSBub25jZQ==")
if k != e {
t.Errorf(`expected "%s" instead "%s" was returned.`, e, k)
}
}
type payloadMock struct {
p []byte
}
func (m *payloadMock) Read(p []byte) (int, error) {
n := 0
for i := range m.p {
if i == len(p) {
break
}
p[i] = m.p[i]
n++
}
m.p = append(make([]byte, 0), m.p[n:]...)
return n, nil
}
func newBuffer(d []byte) *bufio.Reader {
p := &payloadMock{p: d}
r := bufio.NewReader(p)
return r
}
func TestReadFromBufferSingleRead(t *testing.T) {
var c uint64 = 3
p := []byte{120, 123, 54, 32, 102}
b := newBuffer(p)
n, err := readFromBuffer(b, c)
if err != nil {
t.Fatal("An unexpected error was returned while invoking readFromBuffer():", err)
}
if uint64(len(n)) != c {
t.Errorf("Expected slice of bytes returned from readFromBuffer to be of the length '%d'. Instead it is '%d'.", c, len(n))
}
for i, v := range n {
if v != p[i] {
t.Fatalf("Expected slice of bytes to be '%v'. Instead it is '%v'.", p[:c], n)
}
}
}
func TestReadFromBufferMultiRead(t *testing.T) {
// The slice to be read from the buffer must be greater than 4096. Since
// this is the default size of a bufio buffer.
// GO Ref: https://golang.org/src/bufio/bufio.go#L18
p := make([]byte, 4100)
for i := range p {
rand.Seed(int64(i))
p[i] = byte(rand.Intn(255))
}
b := newBuffer(p)
readFromBuffer(b, 4090)
n, err := readFromBuffer(b, 10)
if err != nil {
t.Error("Unexpected error was returned while invoking readFromBuffer:", err)
}
for i, v := range n {
if v != p[i+4090] {
t.Errorf("%v != %v", p[i+4090], v)
}
}
}
func TestStringExists(t *testing.T) {
l := []string{"one", "two", "three"}
type testCase struct {
k string
v int
}
testCases := []testCase{
{k: "one", v: 0},
{k: "four", v: -1},
}
for i, c := range testCases {
r := stringExists(l, c.k)
if r != c.v {
t.Errorf(`Test Case %d: Expected stringExists("%s") to return '%d' instead returned '%d'`, i, c.k, c.v, r)
}
}
}
func TestHeaderToSlice(t *testing.T) {
l := []string{" both ", " left", "right ", "none"}
r := headerToSlice(strings.Join(l, ","))
if len(l) != len(r) {
t.Errorf("The length of the list of header value are not the same. '%d' != '%d'.", len(l), len(r))
}
if r[0] != "both" {
t.Errorf(`Expected "both" instead got "%s".`, r[0])
}
if r[1] != "left" {
t.Errorf(`Expected "left" instead got "%s".`, r[1])
}
if r[2] != "right" {
t.Errorf(`Expected "right" instead got "%s".`, r[2])
}
if r[3] != "none" {
t.Errorf(`Expected "none" instead got "%s".`, r[3])
}
}
func TestRandomByteSlice(t *testing.T) {
type testCase struct {
l int
}
testCases := []testCase{
{l: 2},
{l: 6},
}
for i, c := range testCases {
if b := randomByteSlice(c.l); len(b) != c.l*4 {
t.Errorf("test case %d: expected slice of bytes to be '%d' in length, but it is '%d'", i, c.l*4, len(b))
}
}
}
func TestCloseErrorExist(t *testing.T) {
type testCase struct {
e int
v bool
}
testCases := []testCase{
// Should return false when opcode is invalid
{e: 15, v: false},
// Should return true when opcode is valid.
{e: CloseNormalClosure, v: true},
}
for i, c := range testCases {
if v := closeErrorExist(c.e); v != c.v {
t.Errorf("test case %d: expected '%t' for '%d'", i, c.v, c.e)
}
}
}