forked from jwhited/bgpls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_test.go
89 lines (76 loc) · 1.82 KB
/
packet_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
package bgpls
import (
"encoding/binary"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMessageTypeString(t *testing.T) {
assert.Equal(t, OpenMessageType.String(), "open")
assert.Equal(t, UpdateMessageType.String(), "update")
assert.Equal(t, NotificationMessageType.String(), "notification")
assert.Equal(t, KeepAliveMessageType.String(), "keepalive")
}
func TestMessageFromBytes(t *testing.T) {
k := &keepAliveMessage{}
b, err := k.serialize()
if err != nil {
t.Fatal(err)
}
// invalid message header length
binary.BigEndian.PutUint16(b[16:18], 0)
_, err = messagesFromBytes(b)
assert.NotNil(t, err)
// error on keepalive deserialization
b = append(b, 0)
binary.BigEndian.PutUint16(b[16:18], 20)
_, err = messagesFromBytes(b)
assert.NotNil(t, err)
// invalid marker
b[15] = 0
_, err = messagesFromBytes(b)
assert.NotNil(t, err)
// message < 19 bytes
b = b[:18]
_, err = messagesFromBytes(b)
assert.NotNil(t, err)
// error on open message deserialization
o := &openMessage{}
b, err = o.serialize()
if err != nil {
t.Fatal(err)
}
_, err = messagesFromBytes(b)
assert.NotNil(t, err)
// error on update message deserialization
u := &UpdateMessage{}
b, err = u.serialize()
if err != nil {
t.Fatal(err)
}
_, err = messagesFromBytes(b)
assert.NotNil(t, err)
// error on notification message deserialization
n := &NotificationMessage{}
b, err = n.serialize()
if err != nil {
t.Fatal(err)
}
b = b[:len(b)-2]
binary.BigEndian.PutUint16(b[16:18], 19)
_, err = messagesFromBytes(b)
assert.NotNil(t, err)
// invalid message type
b[18] = 5
_, err = messagesFromBytes(b)
assert.NotNil(t, err)
// 2 messages
k = &keepAliveMessage{}
b, err = k.serialize()
if err != nil {
t.Fatal(err)
}
b = append(b, b...)
m, err := messagesFromBytes(b)
assert.Len(t, m, 2)
assert.Nil(t, err)
}