-
Notifications
You must be signed in to change notification settings - Fork 6
/
filter_test.go
91 lines (72 loc) · 2.37 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
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
// SPDX-FileCopyrightText: 2023 Steffen Vogel <post@steffenvogel.de>
// SPDX-License-Identifier: Apache-2.0
package gont_test
import (
"net"
"testing"
g "cunicu.li/gont/v2/pkg"
o "cunicu.li/gont/v2/pkg/options"
fo "cunicu.li/gont/v2/pkg/options/filters"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
func TestFilterIPv4(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
sw, err := n.AddSwitch("sw")
require.NoError(t, err, "Failed to create switch")
_, flt, err := net.ParseCIDR("10.0.3.0/24")
require.NoError(t, err, "Failed to parse CIDR")
h1, err := n.AddHost("h1",
o.Filter(g.FilterInput,
fo.Protocol(unix.AF_INET),
fo.TransportProtocol(unix.IPPROTO_ICMP),
fo.Source(flt),
fo.Drop),
g.NewInterface("veth0", sw,
o.AddressIP("10.0.1.1/16")))
require.NoError(t, err, "Failed to create host")
h2, err := n.AddHost("h2",
g.NewInterface("veth0", sw,
o.AddressIP("10.0.2.1/16")))
require.NoError(t, err, "Failed to create host")
h3, err := n.AddHost("h3",
g.NewInterface("veth0", sw,
o.AddressIP("10.0.3.1/16")))
require.NoError(t, err, "Failed to create host")
_, err = h1.Ping(h2)
require.NoError(t, err, "Failed to ping h2")
_, err = h1.Ping(h3)
require.Error(t, err, "Succeeded to ping h1")
}
func TestFilterIPv6(t *testing.T) {
n, err := g.NewNetwork(*nname, globalNetworkOptions...)
require.NoError(t, err, "Failed to create network")
defer n.Close()
sw, err := n.AddSwitch("sw")
require.NoError(t, err, "Failed to create switch")
_, flt, err := net.ParseCIDR("fc00:0:0:3::1/64")
require.NoError(t, err, "Failed to parse CIDR")
h1, err := n.AddHost("h1",
o.Filter(g.FilterInput,
fo.Protocol(unix.AF_INET6),
fo.TransportProtocol(unix.IPPROTO_ICMPV6),
fo.Source(flt),
fo.Drop),
g.NewInterface("veth0", sw,
o.AddressIP("fc00:0:0:1::1/56")))
require.NoError(t, err, "Failed to create host")
h2, err := n.AddHost("h2",
g.NewInterface("veth0", sw,
o.AddressIP("fc00:0:0:2::1/56")))
require.NoError(t, err, "Failed to create host")
h3, err := n.AddHost("h3",
g.NewInterface("veth0", sw,
o.AddressIP("fc00:0:0:3::1/56")))
require.NoError(t, err, "Failed to create host")
_, err = h1.Ping(h2)
require.NoError(t, err, "Failed to ping h2")
_, err = h1.Ping(h3)
require.Error(t, err, "Succeeded to ping h3")
}