-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrouting_test.go
More file actions
203 lines (193 loc) · 4.78 KB
/
routing_test.go
File metadata and controls
203 lines (193 loc) · 4.78 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"net"
"testing"
)
func TestParsePortRange(t *testing.T) {
tests := []struct {
input string
expected PortRange
hasError bool
}{
{"80", PortRange{Start: 80, End: 80}, false},
{"8080-9000", PortRange{Start: 8080, End: 9000}, false},
{"any", PortRange{Start: 1, End: 65535}, false},
{"", PortRange{Start: 1, End: 65535}, false},
{"invalid", PortRange{}, true},
{"80-70", PortRange{}, true},
{"0-100", PortRange{}, true},
{"100-70000", PortRange{}, true},
}
for _, test := range tests {
result, err := ParsePortRange(test.input)
if test.hasError {
if err == nil {
t.Errorf("Expected error for input %s, but got none", test.input)
}
} else {
if err != nil {
t.Errorf("Unexpected error for input %s: %v", test.input, err)
}
if result != test.expected {
t.Errorf("For input %s, expected %v but got %v", test.input, test.expected, result)
}
}
}
}
func TestParseRoutingPolicy(t *testing.T) {
tests := []struct {
input string
priority int
expected RoutingPolicy
hasError bool
}{
{
"192.168.1.0/24",
0,
RoutingPolicy{
DestinationCIDR: "192.168.1.0/24",
Protocol: "any",
PortRange: PortRange{Start: 1, End: 65535},
Priority: 0,
},
false,
},
{
"0.0.0.0/0:tcp:80",
1,
RoutingPolicy{
DestinationCIDR: "0.0.0.0/0",
Protocol: "tcp",
PortRange: PortRange{Start: 80, End: 80},
Priority: 1,
},
false,
},
{
"10.0.0.0/8:udp:5000-6000",
2,
RoutingPolicy{
DestinationCIDR: "10.0.0.0/8",
Protocol: "udp",
PortRange: PortRange{Start: 5000, End: 6000},
Priority: 2,
},
false,
},
{
"invalid-cidr",
0,
RoutingPolicy{},
true,
},
{
"192.168.1.0/24:invalid-protocol:80",
0,
RoutingPolicy{},
true,
},
}
for _, test := range tests {
result, err := ParseRoutingPolicy(test.input, test.priority)
if test.hasError {
if err == nil {
t.Errorf("Expected error for input %s, but got none", test.input)
}
} else {
if err != nil {
t.Errorf("Unexpected error for input %s: %v", test.input, err)
}
if result == nil {
t.Errorf("Expected non-nil result for input %s", test.input)
} else if *result != test.expected {
t.Errorf("For input %s, expected %+v but got %+v", test.input, test.expected, *result)
}
}
}
}
func TestRoutingEngine(t *testing.T) {
// Create a test configuration
config := &WireGuardConfig{
Interface: InterfaceConfig{
Address: "10.150.0.2/24",
},
Peers: []PeerConfig{
{
PublicKey: "peer1",
Endpoint: "vpn1.example.com:51820",
AllowedIPs: []string{"0.0.0.0/0"},
RoutingPolicies: []RoutingPolicy{
{
DestinationCIDR: "0.0.0.0/0",
Protocol: "any",
PortRange: PortRange{Start: 1, End: 65535},
Priority: 0,
},
},
},
{
PublicKey: "peer2",
Endpoint: "vpn2.example.com:51820",
AllowedIPs: []string{"192.168.0.0/16", "172.16.0.0/12"},
RoutingPolicies: []RoutingPolicy{
{
DestinationCIDR: "192.168.1.0/24",
Protocol: "tcp",
PortRange: PortRange{Start: 80, End: 443},
Priority: 1,
},
{
DestinationCIDR: "0.0.0.0/0",
Protocol: "tcp",
PortRange: PortRange{Start: 8080, End: 9000},
Priority: 2,
},
},
},
{
PublicKey: "peer3",
Endpoint: "dev-vpn.example.com:51820",
AllowedIPs: []string{"10.0.0.0/8"},
RoutingPolicies: []RoutingPolicy{
{
DestinationCIDR: "10.0.0.0/8",
Protocol: "any",
PortRange: PortRange{Start: 1, End: 65535},
Priority: 0,
},
},
},
},
}
engine := NewRoutingEngine(config)
tests := []struct {
name string
dstIP string
dstPort int
protocol string
expectedPeer int
}{
{"General traffic", "8.8.8.8", 53, "udp", 0},
{"HTTP to 192.168.1.x", "192.168.1.100", 80, "tcp", 1},
{"HTTPS to 192.168.1.x", "192.168.1.100", 443, "tcp", 1},
{"Port 8080 to any IP", "1.2.3.4", 8080, "tcp", 1},
{"Development network", "10.1.2.3", 3000, "tcp", 2},
{"SSH to 192.168.1.x (no specific rule)", "192.168.1.100", 22, "tcp", 0},
{"UDP to port 8080 (TCP-only rule)", "1.2.3.4", 8080, "udp", 0},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ip := net.ParseIP(test.dstIP)
if ip == nil {
t.Fatalf("Failed to parse IP: %s", test.dstIP)
}
peer, peerIdx := engine.FindPeerForDestination(ip, test.dstPort, test.protocol)
if peerIdx != test.expectedPeer {
t.Errorf("Expected peer %d, but got peer %d", test.expectedPeer, peerIdx)
}
if test.expectedPeer >= 0 && peer == nil {
t.Errorf("Expected non-nil peer for peer index %d", test.expectedPeer)
}
})
}
}