-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotocolV11_test.go
165 lines (134 loc) · 3.65 KB
/
protocolV11_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
package p2p
import (
"bytes"
"encoding/binary"
"io"
"math/rand"
"net"
"reflect"
"testing"
"time"
)
// Protobuf is a fairly robust third party protocol with its own unit tests
// These tests serve to verify our own code, not whether or not protobuf can encode proto messages
type testRW struct {
r io.Reader
w io.Writer
}
func newTestRW(r io.Reader, w io.Writer) *testRW {
trw := new(testRW)
trw.r = r
trw.w = w
return trw
}
func (trw *testRW) Read(p []byte) (int, error) { return trw.r.Read(p) }
func (trw *testRW) Write(p []byte) (int, error) { return trw.w.Write(p) }
func TestProtocolV11_readWriteCheck(t *testing.T) {
A, B := net.Pipe()
A.SetDeadline(time.Now().Add(time.Millisecond * 500))
B.SetDeadline(time.Now().Add(time.Millisecond * 500))
defer A.Close()
defer B.Close()
reader := newProtocolV11(A)
sender := newProtocolV11(B)
// write 100 random byte slices
data := make([][]byte, 100)
for i := 0; i < len(data); i++ {
data[i] = make([]byte, rand.Intn(4094)+4) // between 4 byte and 4KiB
rand.Read(data[i])
}
go func() {
for _, d := range data {
if err := sender.writeCheck(d); err != nil {
t.Error(err)
}
}
}()
buf := make([]byte, 4096)
for i := 0; i < len(data); i++ {
ibuf := buf[:len(data[i])]
if err := reader.readCheck(ibuf); err != nil {
t.Error(err)
}
if !bytes.Equal(data[i], ibuf) {
t.Errorf("invalid byte sequence read. want = %x, got = %x", data[i], ibuf)
}
}
// test case where it goes wrong (not sending enough data)
dl := time.Now().Add(time.Millisecond * 50)
A.SetDeadline(dl)
B.SetDeadline(dl)
go func() {
buf := make([]byte, 32)
rand.Read(buf)
if err := sender.writeCheck(buf); err != nil {
t.Error(err)
}
}()
buf = make([]byte, 64)
if err := reader.readCheck(buf); err == nil {
t.Errorf("did not receive an error when it should have timed out")
}
}
func TestProtocolV11_readMessage(t *testing.T) {
msg := new(V11Msg)
msg.Type = 1
msg.Payload = []byte("foo")
data, err := msg.Marshal()
if err != nil {
t.Fatal(data)
}
size := make([]byte, 4)
binary.BigEndian.PutUint32(size, uint32(len(data)))
prot := new(ProtocolV11)
// should produce no errors
valid := append(size, data...)
prot.rw = newTestRW(bytes.NewReader(valid), nil)
validReply := new(V11Msg)
if err := prot.readMessage(validReply); err != nil {
t.Errorf("error valid: %v", err)
}
short := append(size, data[:len(data)-1]...)
prot.rw = newTestRW(bytes.NewReader(short), nil)
invalidReply := new(V11Msg)
if err := prot.readMessage(invalidReply); err == nil {
t.Errorf("short buf didn't give us an error: %v", err)
}
binary.BigEndian.PutUint32(size, V11MaxParcelSize+1)
long := append(size, data[:len(data)]...)
prot.rw = newTestRW(bytes.NewReader(long), nil)
invalidReply = new(V11Msg)
if err := prot.readMessage(invalidReply); err == nil {
t.Errorf("long buf didn't give us an error: %v", err)
}
}
func TestProtocolV11_readWriteMessage(t *testing.T) {
messages := make([]*V11Msg, 128)
for i := range messages {
messages[i] = new(V11Msg)
messages[i].Type = rand.Uint32()
messages[i].Payload = make([]byte, 1+rand.Intn(4095))
rand.Read(messages[i].Payload)
}
A, B := net.Pipe()
dl := time.Now().Add(time.Millisecond * 500)
A.SetDeadline(dl)
B.SetDeadline(dl)
sender := newProtocolV11(A)
reader := newProtocolV11(B)
go func() {
for _, m := range messages {
if err := sender.writeMessage(m); err != nil {
t.Error(err)
}
}
}()
for _, m := range messages {
reply := new(V11Msg)
if err := reader.readMessage(reply); err != nil {
t.Error(err)
} else if !reflect.DeepEqual(m, reply) {
t.Errorf("received wrong message. sent = %+v, got = %+v", m, reply)
}
}
}