-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet_test.go
156 lines (108 loc) · 2.9 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
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
package dht
import (
"crypto/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPacketManagerFragment(t *testing.T) {
m := newPacketManager()
// build a packet that's exactly 3 fragments
id := randomID()
data := make([]byte, MaxPayloadSize*3)
rand.Read(data)
assert.True(t, m.needsFragmenting(data))
p := m.fragment(id, data)
assert.Equal(t, 3, p.frg)
assert.Equal(t, MaxPacketSize*3, p.len)
for i := 0; i < 3; i++ {
pf := p.next()
require.NotNil(t, data)
assert.Equal(t, id, pf[:KEY_BYTES])
assert.Equal(t, byte(i+1), pf[KEY_BYTES])
assert.Equal(t, byte(3), pf[KEY_BYTES+1])
assert.Equal(t, data[MaxPayloadSize*i:MaxPayloadSize*(i+1)], pf[PacketHeaderSize:])
}
assert.Nil(t, p.next())
// return the packet so we reuse it
m.done(p)
// build a packet that's slightly smaller than 3 max fragments
id = randomID()
data = make([]byte, (MaxPayloadSize*3)-300)
rand.Read(data)
assert.True(t, m.needsFragmenting(data))
p = m.fragment(id, data)
assert.Equal(t, 3, p.frg)
assert.Equal(t, MaxPacketSize*3-300, p.len)
read := len(data)
for i := 0; i < 3; i++ {
pf := p.next()
require.NotNil(t, data)
pread := pf[PacketHeaderSize:]
assert.Equal(t, id, pf[:KEY_BYTES])
assert.Equal(t, byte(i+1), pf[KEY_BYTES])
assert.Equal(t, byte(3), pf[KEY_BYTES+1])
if read < MaxPayloadSize {
assert.Equal(t, data[MaxPayloadSize*i:], pread)
} else {
assert.Equal(t, data[MaxPayloadSize*i:MaxPayloadSize*(i+1)], pread)
}
read = read - len(pread)
}
assert.Nil(t, p.next())
// return the packet so we reuse it
m.done(p)
}
func TestPacketManagerAssemble(t *testing.T) {
m := newPacketManager()
id := randomID()
data := make([]byte, MaxPayloadSize*5)
rand.Read(data)
p := m.fragment(id, data)
var fragments [][]byte
f := p.next()
for f != nil {
fragments = append(fragments, f)
f = p.next()
}
// assemble them "out of order" (in this case, just reverse order)
for i := p.frg - 1; i > 0; i-- {
p := m.assemble(fragments[i])
assert.Nil(t, p)
}
// on the last fragment, we should be returned a full packet
p = m.assemble(fragments[0])
assert.NotNil(t, p)
assert.Equal(t, data, p.data())
m.done(p)
}
func TestPacketManagerFragmentAssemble(t *testing.T) {
m := newPacketManager()
id := randomID()
data := make([]byte, MaxPayloadSize/2)
rand.Read(data)
p := m.fragment(id, data)
f := p.next()
assert.Len(t, f, len(data)+PacketHeaderSize)
// on the last fragment, we should be returned a full packet
p = m.assemble(f)
assert.NotNil(t, p)
assert.Equal(t, data, p.data())
m.done(p)
id = randomID()
data = make([]byte, 38424)
rand.Read(data)
p = m.fragment(id, data)
var fragments [][]byte
f = p.next()
for f != nil {
fragments = append(fragments, f)
f = p.next()
}
var p2 *packet
for i := range fragments {
p2 = m.assemble(fragments[i])
}
assert.Equal(t, data, p2.data())
m.done(p)
}