-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_test.go
87 lines (76 loc) · 1.79 KB
/
conn_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
package wire
import (
"bytes"
"crypto/rand"
"net"
"testing"
)
func TestConnectionSimple(t *testing.T) {
c, s := net.Pipe()
client, server := NewConnection(c), NewConnection(s)
garbage := make([]byte, 256)
rand.Read(garbage[:])
go server.WriteMessage(MessageTypeAck, garbage)
mt, p, err := client.ReadNextMessage()
if err != nil {
t.Error(err)
return
}
if mt != MessageTypeAck {
t.Errorf("Expected to receive Ack, but received %x", byte(mt))
return
}
if !bytes.Equal(garbage, p) {
t.Errorf("Message sent is not the one received.")
return
}
}
func TestConnectionDisconnect(t *testing.T) {
c, s := net.Pipe()
client, server := NewConnection(c), NewConnection(s)
server.Close()
client.ReadNextMessage()
_, _, err := client.ReadNextMessage()
if err == nil {
t.Error("Expected error reading from closed connection, got none")
return
}
}
func TestConnectionSendWrongLength(t *testing.T) {
c, s := net.Pipe()
client := NewConnection(c)
go func() {
s.Write([]byte{0x01, 0x02})
s.Close()
}()
client.ReadNextMessage()
_, _, err := client.ReadNextMessage()
if err == nil {
t.Error("Expected error reading insufficient data, got none")
return
}
}
func TestConnectionSendTooLittleData(t *testing.T) {
c, s := net.Pipe()
client := NewConnection(c)
go func() {
s.Write([]byte{0x01, 0x00, 0x02, 0x01})
s.Close()
}()
client.ReadNextMessage()
_, _, err := client.ReadNextMessage()
if err == nil {
t.Error("Expected error reading insufficient data, got none")
return
}
}
func TestConnectionSendToClosedConnection(t *testing.T) {
c, s := net.Pipe()
client, server := NewConnection(c), NewConnection(s)
server.Close()
err := client.WriteMessage(MessageTypeAck, []byte{})
if err == nil {
t.Error("Expected error writing to closed connection, got none")
return
}
}