| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "log/slog" |
| 5 | "strings" |
| 6 | "sync/atomic" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // An untrusted report is not probed again on every look (every hello |
| 12 | // and check looks): only when what it was made with changes, or an hour |
| 13 | // on with the wait doubling, and each decision is warned once. A request |
| 14 | // (equipo-runner probe) still runs the probes at once. |
| 15 | func TestMacProbesUntrustedDoNotStorm(t *testing.T) { |
| 16 | x := newProbeRig(t) |
| 17 | x.pinned = false |
| 18 | var fp atomic.Value |
| 19 | fp.Store("tools-a") |
| 20 | x.p.fingerprint = func() string { return fp.Load().(string) } |
| 21 | logs := &syncBuffer{} |
| 22 | x.p.log = slog.New(slog.NewTextHandler(logs, nil)) |
| 23 | x.started(t) |
| 24 | look := func() { |
| 25 | t.Helper() |
| 26 | for range 50 { |
| 27 | if _, ok := x.p.trusted(); ok { |
| 28 | t.Fatal("a report made with tools off their pins trusted") |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | runs := func(want int32) { |
| 33 | t.Helper() |
| 34 | look() |
| 35 | deadline := time.Now().Add(5 * time.Second) |
| 36 | for { |
| 37 | x.p.mu.Lock() |
| 38 | pending := x.p.pending |
| 39 | x.p.mu.Unlock() |
| 40 | if x.runs.Load() >= want && !pending { |
| 41 | break |
| 42 | } |
| 43 | if time.Now().After(deadline) { |
| 44 | t.Fatalf("%d probe runs, want %d (pending %v):\n%s", x.runs.Load(), want, pending, logs) |
| 45 | } |
| 46 | time.Sleep(10 * time.Millisecond) |
| 47 | } |
| 48 | look() |
| 49 | time.Sleep(100 * time.Millisecond) |
| 50 | if n := x.runs.Load(); n != want { |
| 51 | t.Fatalf("%d probe runs, want %d:\n%s", n, want, logs) |
| 52 | } |
| 53 | } |
| 54 | warned := func() int { return strings.Count(logs.String(), `level=WARN msg="probe report not trusted"`) } |
| 55 | |
| 56 | runs(1) |
| 57 | if n := warned(); n != 1 || !strings.Contains(logs.String(), "probe_again=false") { |
| 58 | t.Fatalf("an unchanged untrusted report warned %d times:\n%s", n, logs) |
| 59 | } |
| 60 | fp.Store("tools-b") |
| 61 | runs(2) |
| 62 | if !strings.Contains(logs.String(), `because="its inputs changed"`) { |
| 63 | t.Fatalf("no warning for the rerun:\n%s", logs) |
| 64 | } |
| 65 | x.now = x.now.Add(probeRetry) |
| 66 | runs(3) |
| 67 | // The next retry waits two hours: one more hour is not enough. |
| 68 | x.now = x.now.Add(probeRetry) |
| 69 | runs(3) |
| 70 | x.now = x.now.Add(probeRetry) |
| 71 | runs(4) |
| 72 | if n := warned(); n != 4 { |
| 73 | t.Fatalf("%d warnings for 3 reruns and the first look:\n%s", n, logs) |
| 74 | } |
| 75 | x.p.probeSoon() |
| 76 | runs(5) |
| 77 | } |
| 78 | |