oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package hostaddrs
2
3import (
4 "errors"
5 "io/fs"
6 "net"
7 "net/netip"
8 "os"
9 "path/filepath"
10 "slices"
11 "strings"
12 "syscall"
13 "testing"
14
15 "amadan.net/rastrillo/rastrillo/budget"
16)
17
18func TestMain(m *testing.M) { os.Exit(budget.Main(m)) }
19
20// refused is netlink as on CARLOS, whose instance units allow only
21// AF_INET, AF_INET6 and AF_UNIX sockets: equipo-staging's log.
22func refused() ([]net.Addr, error) {
23 return nil, &net.OpError{Op: "route", Net: "ip+net", Err: os.NewSyscallError("netlinkrib", syscall.EAFNOSUPPORT)}
24}
25
26// dir holds these fib_trie and if_inet6 files; a nil one is absent.
27func dir(t *testing.T, fibTrie, ifInet6 []string) string {
28 t.Helper()
29 d := t.TempDir()
30 for name, lines := range map[string][]string{"fib_trie": fibTrie, "if_inet6": ifInet6} {
31 if lines == nil {
32 continue
33 }
34 if err := os.WriteFile(filepath.Join(d, name), []byte(strings.Join(lines, "\n")+"\n"), 0o600); err != nil {
35 t.Fatal(err)
36 }
37 }
38 return d
39}
40
41// A box's /proc/net/fib_trie: loopback, a private address on its VPC and
42// a public one, in the main and local tables.
43var boxFibTrie = []string{
44 "Main:",
45 " +-- 0.0.0.0/0 3 0 5",
46 " |-- 0.0.0.0",
47 " /0 universe UNICAST",
48 " +-- 127.0.0.0/8 2 0 2",
49 " +-- 127.0.0.0/31 1 0 0",
50 " |-- 127.0.0.0",
51 " /8 host LOCAL",
52 " |-- 127.0.0.1",
53 " /32 host LOCAL",
54 " |-- 127.255.255.255",
55 " /32 link BROADCAST",
56 " +-- 172.31.0.0/16 2 0 2",
57 " |-- 172.31.0.0",
58 " /16 link UNICAST",
59 " |-- 172.31.5.9",
60 " /32 host LOCAL",
61 " |-- 172.31.255.255",
62 " /32 link BROADCAST",
63 " |-- 192.0.43.5",
64 " /32 host LOCAL",
65 "Local:",
66 " +-- 0.0.0.0/0 3 0 5",
67 " |-- 127.0.0.1",
68 " /32 host LOCAL",
69 " |-- 172.31.5.9",
70 " /32 host LOCAL",
71 " |-- 192.0.43.5",
72 " /32 host LOCAL",
73}
74
75// Its /proc/net/if_inet6: loopback, a public address and its link-local
76// one, and a tailnet address.
77var boxIfInet6 = []string{
78 "00000000000000000000000000000001 01 80 10 80 lo",
79 "2a0104f8000000000000000000000005 02 40 00 80 eth0",
80 "fe80000000000000020000fffe000005 02 40 20 80 eth0",
81 "fd7a115ca1e00000000000007537b43a 06 80 00 80 tailscale0",
82}
83
84func strs(addrs []netip.Addr) []string {
85 out := make([]string, len(addrs))
86 for i, a := range addrs {
87 out[i] = a.String()
88 }
89 return out
90}
91
92// Netlink refused: every address from procfs, then the public ones.
93func TestProcfsWithoutNetlink(t *testing.T) {
94 d := dir(t, boxFibTrie, boxIfInet6)
95 all, err := All(refused, d)
96 if err != nil {
97 t.Fatal(err)
98 }
99 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"} {
100 if !slices.Contains(strs(all), want) {
101 t.Errorf("All left out %s: %v", want, strs(all))
102 }
103 }
104 pub, err := Public(refused, d)
105 if err != nil {
106 t.Fatal(err)
107 }
108 if want := []string{"192.0.43.5", "2a01:4f8::5"}; !slices.Equal(strs(pub), want) {
109 t.Errorf("Public %v, want %v", strs(pub), want)
110 }
111}
112
113// Netlink working: its addresses, and procfs isn't read.
114func TestNetlinkFirst(t *testing.T) {
115 nl := func() ([]net.Addr, error) {
116 var out []net.Addr
117 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"} {
118 out = append(out, &net.IPNet{IP: net.ParseIP(a), Mask: net.CIDRMask(24, 32)})
119 }
120 return out, nil
121 }
122 pub, err := Public(nl, filepath.Join(t.TempDir(), "absent"))
123 if err != nil {
124 t.Fatal(err)
125 }
126 if want := []string{"192.0.32.7", "192.0.43.5"}; !slices.Equal(strs(pub), want) {
127 t.Errorf("Public %v, want %v", strs(pub), want)
128 }
129}
130
131// Neither source: unknown, never none, with both causes.
132func TestNeitherSource(t *testing.T) {
133 _, err := Public(refused, dir(t, nil, boxIfInet6))
134 if err == nil {
135 t.Fatal("listed with nothing to read")
136 }
137 if !errors.Is(err, syscall.EAFNOSUPPORT) || !errors.Is(err, fs.ErrNotExist) {
138 t.Errorf("the error leaves out a cause: %v", err)
139 }
140}
141
142// procfs that can't be trusted to list every address is an unknown list
143// too. A LOCAL route with a TOS is still LOCAL (Astra, fix-lookup-inventory
144// round 1, P2): the kernel prints " tos=<n>" after the type.
145func TestProcfsShapes(t *testing.T) {
146 with := func(lines ...string) []string { return append(slices.Clone(boxFibTrie), lines...) }
147 cases := []struct {
148 name string
149 fibTrie []string
150 ifInet6 []string
151 want []string // nil: refused
152 }{
153 {"local range", with(" |-- 192.0.44.0", " /24 host LOCAL"), boxIfInet6, nil},
154 {"local range with a TOS", with(" |-- 192.0.44.0", " /24 host LOCAL tos=16"), boxIfInet6, nil},
155 {"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"}},
156 {"unknown field after LOCAL", with(" |-- 192.0.45.7", " /32 host LOCAL metric=3"), nil, nil},
157 {"no local route", []string{"Main:", " +-- 0.0.0.0/0 3 0 5", " |-- 0.0.0.0", " /0 universe UNICAST"}, boxIfInet6, nil},
158 {"bad if_inet6", boxFibTrie, []string{"not an address 01 80 10 80 lo"}, nil},
159 {"no IPv6", boxFibTrie, nil, []string{"192.0.43.5"}},
160 }
161 for _, tc := range cases {
162 t.Run(tc.name, func(t *testing.T) {
163 got, err := Public(refused, dir(t, tc.fibTrie, tc.ifInet6))
164 switch {
165 case tc.want == nil && err == nil:
166 t.Fatalf("listed %v", strs(got))
167 case tc.want != nil && err != nil:
168 t.Fatal(err)
169 case tc.want != nil && !slices.Equal(strs(got), tc.want):
170 t.Errorf("Public %v, want %v", strs(got), tc.want)
171 }
172 })
173 }
174}
175