You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.3 KiB
Go
89 lines
3.3 KiB
Go
//go:build linux
|
|
|
|
package vpcrouting
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNFTScriptOwnsOneDedicatedTableAndFailsClosedPerBridge(t *testing.T) {
|
|
network := testNetwork(1)
|
|
script := nftScript(map[string]Network{network.VPCKey: network}, false)
|
|
bridge, _ := BridgeName(network.VPCKey)
|
|
short, _ := nftObjectSuffix(network.VPCKey)
|
|
for _, want := range []string{
|
|
"add table inet virt_cloud_vpc",
|
|
"add chain inet virt_cloud_vpc owner_v1",
|
|
"add set inet virt_cloud_vpc local_" + short,
|
|
"add set inet virt_cloud_vpc remote_" + short,
|
|
"iifname \"" + bridge + "\" drop",
|
|
"oifname \"" + bridge + "\" drop",
|
|
"oifname \"br-private\"",
|
|
} {
|
|
if !strings.Contains(script, want) {
|
|
t.Fatalf("nft script missing %q:\n%s", want, script)
|
|
}
|
|
}
|
|
if strings.Contains(script, "policy drop") || strings.Contains(script, " masquerade") || strings.Contains(script, " dnat ") || strings.Contains(script, " snat ") {
|
|
t.Fatalf("VPC table escaped its fail-closed forwarding scope:\n%s", script)
|
|
}
|
|
}
|
|
|
|
type recordingRunner struct {
|
|
output []byte
|
|
calls [][]string
|
|
}
|
|
|
|
func (r *recordingRunner) Run(_ context.Context, _ string, arguments []string, _ []byte) ([]byte, error) {
|
|
r.calls = append(r.calls, append([]string(nil), arguments...))
|
|
return append([]byte(nil), r.output...), nil
|
|
}
|
|
|
|
func TestRemoveBridgeNeverDeletesForeignSameNamedBridge(t *testing.T) {
|
|
bridge, _ := BridgeName(testVPCKey)
|
|
runner := &recordingRunner{output: []byte(`[{"ifname":"` + bridge + `","address":"02:00:00:00:00:01","linkinfo":{"info_kind":"bridge"}}]`)}
|
|
driver := &linuxDriver{ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft", run: runner}
|
|
if err := driver.removeBridge(t.Context(), testVPCKey); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(runner.calls) != 1 {
|
|
t.Fatalf("foreign bridge received destructive command: %#v", runner.calls)
|
|
}
|
|
}
|
|
|
|
func TestRemoveBridgeDeletesOnlyExactOwnedBridge(t *testing.T) {
|
|
bridge, _ := BridgeName(testVPCKey)
|
|
ownerMAC, _ := bridgeMAC(testVPCKey)
|
|
runner := &recordingRunner{output: []byte(`[{"ifname":"` + bridge + `","address":"` + ownerMAC + `","linkinfo":{"info_kind":"bridge"}}]`)}
|
|
driver := &linuxDriver{ipPath: "/usr/sbin/ip", nftPath: "/usr/sbin/nft", run: runner}
|
|
if err := driver.removeBridge(t.Context(), testVPCKey); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(runner.calls) != 2 || strings.Join(runner.calls[1], " ") != "link delete dev "+bridge+" type bridge" {
|
|
t.Fatalf("owned bridge was not deleted exactly: %#v", runner.calls)
|
|
}
|
|
}
|
|
|
|
func TestNFTTableOwnershipRequiresExactMarkerChain(t *testing.T) {
|
|
owned := []byte(`{"nftables":[{"table":{"family":"inet","name":"virt_cloud_vpc"}},{"chain":{"family":"inet","table":"virt_cloud_vpc","name":"owner_v1"}}]}`)
|
|
foreign := []byte(`{"nftables":[{"table":{"family":"inet","name":"virt_cloud_vpc"}},{"chain":{"family":"inet","table":"virt_cloud_vpc","name":"forward"}}]}`)
|
|
if !nftTableOwned(owned) || nftTableOwned(foreign) || nftTableOwned([]byte(`not-json`)) {
|
|
t.Fatal("reserved nftables table ownership marker was not enforced")
|
|
}
|
|
}
|
|
|
|
func TestManagedRouteProtocolRecognitionIsExact(t *testing.T) {
|
|
for _, value := range []any{float64(186), "186"} {
|
|
if !managedProtocol(value) {
|
|
t.Fatalf("managed protocol rejected: %#v", value)
|
|
}
|
|
}
|
|
for _, value := range []any{float64(185), "static", 186} {
|
|
if managedProtocol(value) {
|
|
t.Fatalf("unmanaged protocol accepted: %#v", value)
|
|
}
|
|
}
|
|
}
|