oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "errors"
6 "net"
7 "net/netip"
8 "os"
9 "path/filepath"
10 "slices"
11 "syscall"
12 "testing"
13
14 "amadan.net/oficina/equipo/internal/websource/guard"
15 "amadan.net/oficina/equipo/internal/wire"
16)
17
18// scanning has the store's next scans see addrs as its interfaces'.
19func scanning(h *abilitiesHarness, addrs ...string) {
20 h.s.ifaceAddrs = func() ([]net.Addr, error) {
21 var out []net.Addr
22 for _, a := range addrs {
23 out = append(out, &net.IPNet{IP: net.ParseIP(a), Mask: net.CIDRMask(24, 32)})
24 }
25 return out, nil
26 }
27}
28
29func scan(t *testing.T, h *abilitiesHarness) {
30 t.Helper()
31 if err := h.s.ScanInstanceAddresses(context.Background()); err != nil {
32 t.Fatal(err)
33 }
34}
35
36// openInventoryCards are the open instance_inventory cards, by person.
37func openInventoryCards(t *testing.T, h *abilitiesHarness) map[string]cardRecord {
38 t.Helper()
39 var cards []cardRecord
40 if err := h.s.db.G.Where("kind = ? AND state = ?", wire.CardInstanceInventory, wire.CardOpen).Find(&cards).Error; err != nil {
41 t.Fatal(err)
42 }
43 out := map[string]cardRecord{}
44 for _, c := range cards {
45 out[c.Person] = c
46 }
47 return out
48}
49
50// With no prefixes configured the inventory is never ready, and nobody
51// is asked.
52func TestInventoryNotReadyWithoutPrefixes(t *testing.T) {
53 h := newAbilitiesHarness(t)
54 scanning(h, "192.0.43.5")
55 scan(t, h)
56 if err := h.s.InstanceInventory().Ready(context.Background()); !errors.Is(err, guard.ErrInventoryUnconfirmed) {
57 t.Errorf("ready with no prefixes: %v", err)
58 }
59 if n := len(openInventoryCards(t, h)); n != 0 {
60 t.Errorf("%d cards", n)
61 }
62}
63
64// Prefixes configured: one card per owner, showing the prefixes and the
65// addresses seen. A member can't answer it (it isn't theirs to see); an owner's Confirm makes the
66// inventory ready, and a scan with nothing changed asks nothing more.
67func TestInventoryCardAndConfirm(t *testing.T) {
68 h := newAbilitiesHarness(t)
69 ctx := context.Background()
70 if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil {
71 t.Fatal(err)
72 }
73 scanning(h, "192.0.43.5")
74 scan(t, h)
75 if err := h.s.InstanceInventory().Ready(ctx); !errors.Is(err, guard.ErrInventoryUnconfirmed) {
76 t.Fatalf("ready before Confirm: %v", err)
77 }
78 cards := openInventoryCards(t, h)
79 c, ok := cards["owner@example.com"]
80 if len(cards) != 1 || !ok {
81 t.Fatalf("cards %+v", cards)
82 }
83 view, _, err := h.s.AnswerDevice(ctx, abPerson, "", c.ID, wire.AnswerCard{Choice: wire.ChoiceConfirm, Revision: c.Revision})
84 if !errors.Is(err, ErrForbidden) && !errors.Is(err, ErrNotFound) {
85 t.Errorf("a member's Confirm: %+v %v", view.Card, err)
86 }
87 var shown wire.Card
88 if err := h.s.tx(ctx, func(tx *txn) error {
89 var err error
90 shown, err = tx.cardView(c)
91 return err
92 }); err != nil {
93 t.Fatal(err)
94 }
95 if shown.Inventory == nil || len(shown.Inventory.Addresses) != 2 || shown.Inventory.Addresses[0] != "192.0.43.0/24" || shown.Inventory.Addresses[1] != "192.0.43.5" {
96 t.Errorf("the card shows %+v", shown.Inventory)
97 }
98 if _, _, err := h.s.AnswerDevice(ctx, "owner@example.com", "", c.ID, wire.AnswerCard{Choice: wire.ChoiceConfirm, Revision: c.Revision}); err != nil {
99 t.Fatal(err)
100 }
101 if err := h.s.InstanceInventory().Ready(ctx); err != nil {
102 t.Errorf("not ready after Confirm: %v", err)
103 }
104 scan(t, h)
105 if n := len(openInventoryCards(t, h)); n != 0 {
106 t.Errorf("%d cards after Confirm", n)
107 }
108}
109
110// An address seen outside the confirmed prefixes stops the inventory
111// being ready, and asks again.
112func TestInventoryDriftStopsRequests(t *testing.T) {
113 h := newAbilitiesHarness(t)
114 ctx := context.Background()
115 if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil {
116 t.Fatal(err)
117 }
118 scanning(h, "192.0.43.5")
119 scan(t, h)
120 c := openInventoryCards(t, h)["owner@example.com"]
121 if _, _, err := h.s.AnswerDevice(ctx, "owner@example.com", "", c.ID, wire.AnswerCard{Choice: wire.ChoiceConfirm, Revision: c.Revision}); err != nil {
122 t.Fatal(err)
123 }
124 scanning(h, "192.0.43.5", "192.0.32.7")
125 scan(t, h)
126 if err := h.s.InstanceInventory().Ready(ctx); !errors.Is(err, guard.ErrInventoryUnconfirmed) {
127 t.Errorf("ready with an address outside: %v", err)
128 }
129 again, ok := openInventoryCards(t, h)["owner@example.com"]
130 if !ok || again.ID == c.ID {
131 t.Errorf("no new card: %+v", again)
132 }
133}
134
135// Inside is the instance's own network: its prefixes and the addresses
136// it has seen.
137func TestInventoryInside(t *testing.T) {
138 h := newAbilitiesHarness(t)
139 if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil {
140 t.Fatal(err)
141 }
142 scanning(h, "192.0.32.7")
143 scan(t, h)
144 inv := h.s.InstanceInventory()
145 for addr, want := range map[string]bool{"192.0.43.9": true, "192.0.32.7": true, "93.184.216.34": false} {
146 if got := inv.Inside(netip.MustParseAddr(addr)); got != want {
147 t.Errorf("Inside(%s) = %v", addr, got)
148 }
149 }
150 if err := h.s.SetInstancePrefixes("not a prefix"); err == nil {
151 t.Error("a bad prefix was taken")
152 }
153}
154
155// Confirmed rows alone aren't enough: until this process has scanned, or
156// after a scan fails, the inventory isn't ready (Astra phase 3 round 1,
157// P1).
158func TestInventoryNeedsItsOwnScan(t *testing.T) {
159 h := newAbilitiesHarness(t)
160 ctx := context.Background()
161 if err := h.s.SetInstancePrefixes("192.0.43.0/24"); err != nil {
162 t.Fatal(err)
163 }
164 if err := h.s.db.G.Create(&instancePrefixRecord{Prefix: "192.0.43.0/24", ConfirmedBy: "owner@example.com"}).Error; err != nil {
165 t.Fatal(err)
166 }
167 if err := h.s.InstanceInventory().Ready(ctx); !errors.Is(err, guard.ErrInventoryUnconfirmed) {
168 t.Errorf("ready before any scan: %v", err)
169 }
170 scanning(h, "192.0.43.5")
171 scan(t, h)
172 if err := h.s.InstanceInventory().Ready(ctx); err != nil {
173 t.Fatalf("not ready after a scan: %v", err)
174 }
175 h.s.ifaceAddrs = func() ([]net.Addr, error) { return nil, errors.New("no interfaces") }
176 h.s.procNet = t.TempDir()
177 if err := h.s.ScanInstanceAddresses(ctx); err == nil {
178 t.Fatal("the failing scan succeeded")
179 }
180 if err := h.s.InstanceInventory().Ready(ctx); !errors.Is(err, guard.ErrInventoryUnconfirmed) {
181 t.Errorf("ready after a failed scan: %v", err)
182 }
183}
184
185// Without netlink (CARLOS, equipo-staging: "route ip+net: netlinkrib:
186// address family not supported by protocol"), the scan reads the box's
187// addresses from procfs and keeps the public ones; an owner's Confirm
188// makes the inventory ready. hostaddrs's tests cover procfs itself.
189func TestInventoryWithoutNetlink(t *testing.T) {
190 h := newAbilitiesHarness(t)
191 ctx := context.Background()
192 if err := h.s.SetInstancePrefixes("192.0.43.0/24,2a01:4f8::/64"); err != nil {
193 t.Fatal(err)
194 }
195 h.s.ifaceAddrs = func() ([]net.Addr, error) {
196 return nil, &net.OpError{Op: "route", Net: "ip+net", Err: os.NewSyscallError("netlinkrib", syscall.EAFNOSUPPORT)}
197 }
198 h.s.procNet = t.TempDir()
199 for name, body := range map[string]string{"fib_trie": "|-- 127.0.0.1\n/32 host LOCAL\n|-- 172.31.5.9\n/32 host LOCAL\n|-- 192.0.43.5\n/32 host LOCAL\n", "if_inet6": "2a0104f8000000000000000000000005 02 40 00 80 eth0\n"} {
200 if err := os.WriteFile(filepath.Join(h.s.procNet, name), []byte(body), 0o600); err != nil {
201 t.Fatal(err)
202 }
203 }
204 scan(t, h)
205 var seen []string
206 if err := h.s.db.G.Model(&instanceAddressRecord{}).Order("address").Pluck("address", &seen).Error; err != nil || !slices.Equal(seen, []string{"192.0.43.5", "2a01:4f8::5"}) {
207 t.Fatalf("seen %v %v", seen, err)
208 }
209 c := openInventoryCards(t, h)["owner@example.com"]
210 if _, _, err := h.s.AnswerDevice(ctx, "owner@example.com", "", c.ID, wire.AnswerCard{Choice: wire.ChoiceConfirm, Revision: c.Revision}); err != nil {
211 t.Fatal(err)
212 }
213 if err := h.s.InstanceInventory().Ready(ctx); err != nil {
214 t.Errorf("not ready after Confirm: %v", err)
215 }
216}
217