package hostaddrs import ( "errors" "io/fs" "net" "net/netip" "os" "path/filepath" "slices" "strings" "syscall" "testing" "amadan.net/rastrillo/rastrillo/budget" ) func TestMain(m *testing.M) { os.Exit(budget.Main(m)) } // refused is netlink as on CARLOS, whose instance units allow only // AF_INET, AF_INET6 and AF_UNIX sockets: equipo-staging's log. func refused() ([]net.Addr, error) { return nil, &net.OpError{Op: "route", Net: "ip+net", Err: os.NewSyscallError("netlinkrib", syscall.EAFNOSUPPORT)} } // dir holds these fib_trie and if_inet6 files; a nil one is absent. func dir(t *testing.T, fibTrie, ifInet6 []string) string { t.Helper() d := t.TempDir() for name, lines := range map[string][]string{"fib_trie": fibTrie, "if_inet6": ifInet6} { if lines == nil { continue } if err := os.WriteFile(filepath.Join(d, name), []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil { t.Fatal(err) } } return d } // A box's /proc/net/fib_trie: loopback, a private address on its VPC and // a public one, in the main and local tables. var boxFibTrie = []string{ "Main:", " +-- 0.0.0.0/0 3 0 5", " |-- 0.0.0.0", " /0 universe UNICAST", " +-- 127.0.0.0/8 2 0 2", " +-- 127.0.0.0/31 1 0 0", " |-- 127.0.0.0", " /8 host LOCAL", " |-- 127.0.0.1", " /32 host LOCAL", " |-- 127.255.255.255", " /32 link BROADCAST", " +-- 172.31.0.0/16 2 0 2", " |-- 172.31.0.0", " /16 link UNICAST", " |-- 172.31.5.9", " /32 host LOCAL", " |-- 172.31.255.255", " /32 link BROADCAST", " |-- 192.0.43.5", " /32 host LOCAL", "Local:", " +-- 0.0.0.0/0 3 0 5", " |-- 127.0.0.1", " /32 host LOCAL", " |-- 172.31.5.9", " /32 host LOCAL", " |-- 192.0.43.5", " /32 host LOCAL", } // Its /proc/net/if_inet6: loopback, a public address and its link-local // one, and a tailnet address. var boxIfInet6 = []string{ "00000000000000000000000000000001 01 80 10 80 lo", "2a0104f8000000000000000000000005 02 40 00 80 eth0", "fe80000000000000020000fffe000005 02 40 20 80 eth0", "fd7a115ca1e00000000000007537b43a 06 80 00 80 tailscale0", } func strs(addrs []netip.Addr) []string { out := make([]string, len(addrs)) for i, a := range addrs { out[i] = a.String() } return out } // Netlink refused: every address from procfs, then the public ones. func TestProcfsWithoutNetlink(t *testing.T) { d := dir(t, boxFibTrie, boxIfInet6) all, err := All(refused, d) if err != nil { t.Fatal(err) } for _, want := range []string{"127.0.0.1", "172.31.5.9", "192.0.43.5", "::1", "2a01:4f8::5", "fe80::200:ff:fe00:5", "fd7a:115c:a1e0::7537:b43a"} { if !slices.Contains(strs(all), want) { t.Errorf("All left out %s: %v", want, strs(all)) } } pub, err := Public(refused, d) if err != nil { t.Fatal(err) } if want := []string{"192.0.43.5", "2a01:4f8::5"}; !slices.Equal(strs(pub), want) { t.Errorf("Public %v, want %v", strs(pub), want) } } // Netlink working: its addresses, and procfs isn't read. func TestNetlinkFirst(t *testing.T) { nl := func() ([]net.Addr, error) { var out []net.Addr for _, a := range []string{"10.0.0.5", "100.100.1.1", "169.254.169.254", "fd7a:115c:a1e0::1", "::ffff:192.0.43.5", "192.0.43.5", "192.0.32.7"} { out = append(out, &net.IPNet{IP: net.ParseIP(a), Mask: net.CIDRMask(24, 32)}) } return out, nil } pub, err := Public(nl, filepath.Join(t.TempDir(), "absent")) if err != nil { t.Fatal(err) } if want := []string{"192.0.32.7", "192.0.43.5"}; !slices.Equal(strs(pub), want) { t.Errorf("Public %v, want %v", strs(pub), want) } } // Neither source: unknown, never none, with both causes. func TestNeitherSource(t *testing.T) { _, err := Public(refused, dir(t, nil, boxIfInet6)) if err == nil { t.Fatal("listed with nothing to read") } if !errors.Is(err, syscall.EAFNOSUPPORT) || !errors.Is(err, fs.ErrNotExist) { t.Errorf("the error leaves out a cause: %v", err) } } // procfs that can't be trusted to list every address is an unknown list // too. A LOCAL route with a TOS is still LOCAL (Astra, fix-lookup-inventory // round 1, P2): the kernel prints " tos=" after the type. func TestProcfsShapes(t *testing.T) { with := func(lines ...string) []string { return append(slices.Clone(boxFibTrie), lines...) } cases := []struct { name string fibTrie []string ifInet6 []string want []string // nil: refused }{ {"local range", with(" |-- 192.0.44.0", " /24 host LOCAL"), boxIfInet6, nil}, {"local range with a TOS", with(" |-- 192.0.44.0", " /24 host LOCAL tos=16"), boxIfInet6, nil}, {"local address with a TOS", with(" |-- 192.0.45.7", " /32 host LOCAL tos=16"), nil, []string{"192.0.43.5", "192.0.45.7"}}, {"unknown field after LOCAL", with(" |-- 192.0.45.7", " /32 host LOCAL metric=3"), nil, nil}, {"no local route", []string{"Main:", " +-- 0.0.0.0/0 3 0 5", " |-- 0.0.0.0", " /0 universe UNICAST"}, boxIfInet6, nil}, {"bad if_inet6", boxFibTrie, []string{"not an address 01 80 10 80 lo"}, nil}, {"no IPv6", boxFibTrie, nil, []string{"192.0.43.5"}}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { got, err := Public(refused, dir(t, tc.fibTrie, tc.ifInet6)) switch { case tc.want == nil && err == nil: t.Fatalf("listed %v", strs(got)) case tc.want != nil && err != nil: t.Fatal(err) case tc.want != nil && !slices.Equal(strs(got), tc.want): t.Errorf("Public %v, want %v", strs(got), tc.want) } }) } }