-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_test.go
65 lines (59 loc) · 1.71 KB
/
filter_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
package filter
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
func randomBytes(buffer []byte) {
for i := 0; i < len(buffer); i++ {
buffer[i] = byte(rand.Intn(256))
}
}
func TestPittleAndChonkle(t *testing.T) {
rand.Seed(42)
var output [1024]byte
output[0] = 0x32
iterations := 1000
for i := 0; i < iterations; i++ {
var magic [8]byte
var fromAddress [4]byte
var toAddress [4]byte
randomBytes(magic[:])
randomBytes(fromAddress[:])
randomBytes(toAddress[:])
packetLength := 18 + (i % (len(output) - 18))
GeneratePittle(output[1:3], fromAddress[:], toAddress[:], packetLength)
GenerateChonkle(output[3:18], magic[:], fromAddress[:], toAddress[:], packetLength)
assert.Equal(t, true, BasicPacketFilter(output[:], packetLength))
assert.Equal(t, true, AdvancedPacketFilter(output[:], magic[:], fromAddress[:], toAddress[:], packetLength))
}
}
func TestBasicPacketFilter(t *testing.T) {
rand.Seed(42)
var output [256]byte
pass := 0
iterations := 10000
for i := 0; i < iterations; i++ {
randomBytes(output[:])
packetLength := i % len(output)
assert.Equal(t, false, BasicPacketFilter(output[:], packetLength))
}
assert.Equal(t, 0, pass)
}
func TestAdvancedBasicPacketFilter(t *testing.T) {
rand.Seed(42)
var output [1000]byte
iterations := 10000
for i := 0; i < iterations; i++ {
var magic [8]byte
var fromAddress [4]byte
var toAddress [4]byte
randomBytes(magic[:])
randomBytes(fromAddress[:])
randomBytes(toAddress[:])
randomBytes(output[:])
packetLength := i % len(output)
assert.Equal(t, false, BasicPacketFilter(output[:], packetLength))
assert.Equal(t, false, AdvancedPacketFilter(output[:], magic[:], fromAddress[:], toAddress[:], packetLength))
}
}