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.
109 lines
3.7 KiB
Go
109 lines
3.7 KiB
Go
package publicnetwork
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func validIntent() Intent {
|
|
return Intent{
|
|
Contract: ContractVersion,
|
|
NetworkSelector: "bridge:br0",
|
|
IPv4: AddressIntent{
|
|
PoolKey: "0123456789abcdef0123456789abcdef", PoolCIDR: "8.8.8.0/24",
|
|
AddressCIDR: "8.8.8.10/24", Gateway: "8.8.8.1",
|
|
},
|
|
IPv6: AddressIntent{
|
|
PoolKey: "abcdef0123456789abcdef0123456789", PoolCIDR: "2404:c140:156:3a::/64",
|
|
AddressCIDR: "2404:c140:156:3a::1000/64", Gateway: "2404:c140:156::1",
|
|
GatewayOnLink: true,
|
|
},
|
|
DNSServers: []string{"1.1.1.1", "2606:4700:4700::1111"},
|
|
}
|
|
}
|
|
|
|
func TestIntentCanonicalizesACompleteDualStackIdentity(t *testing.T) {
|
|
value := validIntent()
|
|
value.IPv6.PoolCIDR = "2404:C140:156:3A:0:0:0:0/64"
|
|
value.IPv6.AddressCIDR = "2404:C140:156:3A:0:0:0:1000/64"
|
|
|
|
canonical, err := value.Canonical()
|
|
if err != nil {
|
|
t.Fatalf("canonicalize intent: %v", err)
|
|
}
|
|
if canonical.IPv6.PoolCIDR != "2404:c140:156:3a::/64" ||
|
|
canonical.IPv6.AddressCIDR != "2404:c140:156:3a::1000/64" {
|
|
t.Fatalf("canonical IPv6 identity = %#v", canonical.IPv6)
|
|
}
|
|
if got := canonical.IdentityCIDRs(); !reflect.DeepEqual(got, []string{"8.8.8.10/24", "2404:c140:156:3a::1000/64"}) {
|
|
t.Fatalf("identity CIDRs = %v", got)
|
|
}
|
|
}
|
|
|
|
func TestIntentRequiresBothFamiliesAndExplicitOffPrefixGateway(t *testing.T) {
|
|
tests := map[string]func(*Intent){
|
|
"missing IPv4": func(v *Intent) { v.IPv4 = AddressIntent{} },
|
|
"missing IPv6": func(v *Intent) { v.IPv6 = AddressIntent{} },
|
|
"IPv6 in IPv4 slot": func(v *Intent) { v.IPv4.AddressCIDR = v.IPv6.AddressCIDR },
|
|
"IPv4 in IPv6 slot": func(v *Intent) { v.IPv6.AddressCIDR = v.IPv4.AddressCIDR },
|
|
"off-prefix without onlink": func(v *Intent) { v.IPv6.GatewayOnLink = false },
|
|
"duplicate DNS": func(v *Intent) { v.DNSServers = []string{"1.1.1.1", "1.1.1.1"} },
|
|
"unsafe selector": func(v *Intent) { v.NetworkSelector = "bridge:br0;reboot" },
|
|
"special-use IPv4": func(v *Intent) {
|
|
v.IPv4.PoolCIDR, v.IPv4.AddressCIDR, v.IPv4.Gateway = "198.51.100.0/24", "198.51.100.10/24", "198.51.100.1"
|
|
},
|
|
"legacy contract": func(v *Intent) { v.Contract = 0 },
|
|
}
|
|
for name, mutate := range tests {
|
|
t.Run(name, func(t *testing.T) {
|
|
value := validIntent()
|
|
mutate(&value)
|
|
if _, err := value.Canonical(); err == nil {
|
|
t.Fatal("invalid dual-stack intent was accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIntentWireContractHasNoLegacyFlatFields(t *testing.T) {
|
|
encoded, err := json.Marshal(validIntent())
|
|
if err != nil {
|
|
t.Fatalf("marshal intent: %v", err)
|
|
}
|
|
var wire map[string]any
|
|
if err := json.Unmarshal(encoded, &wire); err != nil {
|
|
t.Fatalf("decode intent: %v", err)
|
|
}
|
|
for _, key := range []string{"public_ip_cidr", "public_pool_cidr", "public_pool_key", "gateway"} {
|
|
if _, exists := wire[key]; exists {
|
|
t.Fatalf("legacy flat field %q escaped: %#v", key, wire)
|
|
}
|
|
}
|
|
if _, ok := wire["ipv4"].(map[string]any); !ok {
|
|
t.Fatalf("missing IPv4 object: %#v", wire)
|
|
}
|
|
if _, ok := wire["ipv6"].(map[string]any); !ok {
|
|
t.Fatalf("missing IPv6 object: %#v", wire)
|
|
}
|
|
}
|
|
|
|
func TestLinkLocalIPv6IsFrozenFromCanonicalMAC(t *testing.T) {
|
|
address, err := LinkLocalIPv6("52:54:00:12:34:56")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if address.String() != "fe80::5054:ff:fe12:3456" {
|
|
t.Fatalf("link-local IPv6 = %s", address)
|
|
}
|
|
cidr, err := LinkLocalIPv6CIDR("52:54:00:12:34:56")
|
|
if err != nil || cidr != "fe80::5054:ff:fe12:3456/64" {
|
|
t.Fatalf("link-local CIDR = %q err=%v", cidr, err)
|
|
}
|
|
for _, value := range []string{"", "ff:ff:ff:ff:ff:ff", "00:00:00:00:00:00", "52:54:00:12:34"} {
|
|
if _, err := LinkLocalIPv6(value); err == nil {
|
|
t.Fatalf("invalid MAC %q was accepted", value)
|
|
}
|
|
}
|
|
}
|