-
Notifications
You must be signed in to change notification settings - Fork 14
/
transaction_test.go
71 lines (58 loc) · 1.58 KB
/
transaction_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
package pf
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestRuleSetRollback(t *testing.T) {
var rule Rule
rule.SetProtocol(ProtocolUDP)
rule.SetAction(ActionPass)
err := rule.ParseSource("127.0.0.1", "0:65535", false)
assert.NoError(t, err)
err = rule.ParseDestination("8.8.8.8", "53", false)
assert.NoError(t, err)
tx := pfh.NewTransaction(1)
frs := tx.RuleSet(0)
assert.Equal(t, RuleSetScrub, frs.Type())
frs.SetType(RuleSetFilter)
assert.Equal(t, "", frs.Anchor())
assert.NoError(t, frs.SetAnchor("/asd"))
assert.Equal(t, "/asd", frs.Anchor())
err = tx.Begin()
assert.NoError(t, err)
err = frs.AddRule(&rule)
assert.Error(t, err)
err = tx.Rollback()
assert.NoError(t, err)
rules, err := pfh.Rules()
assert.NoError(t, err)
assert.Len(t, rules, 0)
}
func TestAddRuleAndRule(t *testing.T) {
var rule Rule
rule.SetProtocol(ProtocolUDP)
rule.SetLog(true)
rule.SetQuick(true)
rule.SetDirection(DirectionIn)
rule.SetAction(ActionPass)
rule.SetState(StateKeep)
err := rule.ParseSource("127.0.0.1", "0:65535", false)
assert.NoError(t, err)
err = rule.ParseDestination("8.8.8.8", "53", false)
assert.NoError(t, err)
tx := pfh.NewTransaction(1)
frs := tx.RuleSet(0)
frs.SetType(RuleSetFilter)
err = tx.Begin()
assert.NoError(t, err)
err = frs.AddRule(&rule)
assert.NoError(t, err)
err = tx.Commit()
assert.NoError(t, err)
rules, err := pfh.Rules()
assert.NoError(t, err)
assert.Len(t, rules, 1)
assert.Equal(t, "pass in log quick inet proto udp "+
"from 127.0.0.1/32 port 0:65535 "+
"to 8.8.8.8/32 port 53 keep state", rules[0].String())
}