package outward import ( "errors" "net/netip" "reflect" "testing" ) func prefixes(s ...string) []netip.Prefix { var out []netip.Prefix for _, p := range s { out = append(out, netip.MustParsePrefix(p)) } return out } // **Each link facing outside says the networks it is attached to** (novox/hq ADR 0340 §7), as the // network's address and prefix, never the machine's own address on it — and every one, public and // link-local included, because which count is the controller's judgement and said with why. func TestEachOutwardLinkSaysTheNetworksItIsOn(t *testing.T) { addrs := map[string][]netip.Prefix{ "eth0": prefixes("192.168.1.10/24", "fe80::1/64", "fd12:3456:789a::5/64"), "eth1": prefixes("203.0.113.7/24"), // Not asked: the bridge is not a link facing outside. "docker0": prefixes("172.17.0.1/16"), } got, err := Networks([]string{"eth1", "eth0"}, func(link string) ([]netip.Prefix, error) { return addrs[link], nil }) if err != nil { t.Fatal(err) } want := []Network{ {Link: "eth0", Network: "192.168.1.0/24"}, {Link: "eth0", Network: "fd12:3456:789a::/64"}, {Link: "eth0", Network: "fe80::/64"}, {Link: "eth1", Network: "203.0.113.0/24"}, } if !reflect.DeepEqual(got, want) { t.Fatalf("got %+v\nwant %+v", got, want) } } // A link whose addresses cannot be read fails the whole, so the machine says the fact is unknown rather // than reporting part of its networks as all of them. func TestAnUnreadableLinkIsNotAPartialAccount(t *testing.T) { _, err := Networks([]string{"eth0"}, func(string) ([]netip.Prefix, error) { return nil, errors.New("denied") }) if err == nil { t.Fatal("an unreadable link was reported as having no networks") } } // A link gone between reading the routing table and reading its addresses has no networks, and the // rest are still said. func TestALinkGoneSinceItWasListedHasNoNetworks(t *testing.T) { got, err := Networks([]string{"gone0"}, RealAddresses) if err != nil || len(got) != 0 { t.Fatalf("got %v, %v", got, err) } }