-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrouting_cli_test.go
More file actions
100 lines (87 loc) · 2.57 KB
/
routing_cli_test.go
File metadata and controls
100 lines (87 loc) · 2.57 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
package main
import (
"testing"
)
func TestApplyCLIRoutes(t *testing.T) {
// Create a test configuration
config := &WireGuardConfig{
Interface: InterfaceConfig{
PrivateKey: "test-private-key",
Address: "10.0.0.2/24",
},
Peers: []PeerConfig{
{
PublicKey: "peer1-public-key",
Endpoint: "192.168.1.100:51820",
AllowedIPs: []string{"10.0.0.0/24"},
},
{
PublicKey: "peer2-public-key",
Endpoint: "192.168.1.101:51820",
AllowedIPs: []string{"10.1.0.0/24"},
},
},
}
// Test exit node
err := ApplyCLIRoutes(config, "10.0.0.3", nil)
if err != nil {
t.Fatalf("Failed to apply exit node: %v", err)
}
// Check that the routing policy was added to the correct peer
peer1 := &config.Peers[0]
if len(peer1.RoutingPolicies) != 1 {
t.Fatalf("Expected 1 routing policy, got %d", len(peer1.RoutingPolicies))
}
policy := peer1.RoutingPolicies[0]
if policy.DestinationCIDR != "0.0.0.0/0" {
t.Errorf("Expected destination CIDR 0.0.0.0/0, got %s", policy.DestinationCIDR)
}
// Test specific routes
routes := []string{"192.168.1.0/24:10.0.0.4", "172.16.0.0/12:10.1.0.5"}
err = ApplyCLIRoutes(config, "", routes)
if err != nil {
t.Fatalf("Failed to apply routes: %v", err)
}
// Check that routes were added to correct peers
if len(peer1.RoutingPolicies) != 2 {
t.Fatalf("Expected 2 routing policies on peer1, got %d", len(peer1.RoutingPolicies))
}
peer2 := &config.Peers[1]
if len(peer2.RoutingPolicies) != 1 {
t.Fatalf("Expected 1 routing policy on peer2, got %d", len(peer2.RoutingPolicies))
}
// Verify the specific route on peer2
if peer2.RoutingPolicies[0].DestinationCIDR != "172.16.0.0/12" {
t.Errorf("Expected destination CIDR 172.16.0.0/12, got %s", peer2.RoutingPolicies[0].DestinationCIDR)
}
}
func TestApplyCLIRoutesErrors(t *testing.T) {
config := &WireGuardConfig{
Interface: InterfaceConfig{
PrivateKey: "test-private-key",
Address: "10.0.0.2/24",
},
Peers: []PeerConfig{
{
PublicKey: "peer1-public-key",
Endpoint: "192.168.1.100:51820",
AllowedIPs: []string{"10.0.0.0/24"},
},
},
}
// Test invalid route format
err := ApplyCLIRoutes(config, "", []string{"invalid-route"})
if err == nil {
t.Error("Expected error for invalid route format")
}
// Test invalid CIDR
err = ApplyCLIRoutes(config, "", []string{"invalid-cidr:10.0.0.3"})
if err == nil {
t.Error("Expected error for invalid CIDR")
}
// Test peer IP not found
err = ApplyCLIRoutes(config, "", []string{"192.168.1.0/24:192.168.1.1"})
if err == nil {
t.Error("Expected error for peer IP not in any AllowedIPs")
}
}