diff --git a/.golangci.yml b/.golangci.yml index a94231a..d7f5b89 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -56,6 +56,15 @@ linters: exclusions: rules: + # gosec finds G703 (a path from tainted input) with a taint analysis + # whose result changes from run to run, with the same code and the + # same version (#186). So nolintlint sometimes reports a G703 + # directive as unused. Only that report is off, only for a directive + # that names G703 alone; any other unused directive is still + # reported, and gosec still reports a G703 without a directive. + - linters: + - nolintlint + text: '^directive `//nolint:gosec // G703: [^`]+` is unused for linter "gosec"$' # Code in cmd/ may print. Only the rules about printing and logging # (their messages start with "library code must not") are off there. - path: ^cmd/ diff --git a/internal/approval/approvaltest/approver.go b/internal/approval/approvaltest/approver.go index 5f2e9e6..5d99752 100644 --- a/internal/approval/approvaltest/approver.go +++ b/internal/approval/approvaltest/approver.go @@ -300,6 +300,13 @@ func (a *Approver) WaitEnded(n int, timeout time.Duration) ([]Ended, error) { return a.Ended(), err } +// WaitAlarms waits until at least n alarms went out, and returns them. It +// fails after timeout. +func (a *Approver) WaitAlarms(n int, timeout time.Duration) ([]approval.Alarm, error) { + err := a.wait(func() bool { return len(a.alarms) >= n }, timeout) + return a.Alarms(), err +} + // WaitRunning waits until Run runs. It fails after timeout. func (a *Approver) WaitRunning(timeout time.Duration) error { return a.wait(func() bool { return a.answer != nil }, timeout) diff --git a/internal/broker/standinguse_test.go b/internal/broker/standinguse_test.go index f809173..3c2470a 100644 --- a/internal/broker/standinguse_test.go +++ b/internal/broker/standinguse_test.go @@ -188,6 +188,7 @@ func TestStandingOldToken(t *testing.T) { func TestStandingOutsideWindow(t *testing.T) { e, tok0, id := useEnv(t) e.clock.Advance(3 * time.Hour) // 15:00 + e.waitMissedAlarm() for range 2 { res, _ := e.useToken("laptop", tok0) mustStatus(t, res, http.StatusForbidden) @@ -451,22 +452,35 @@ func TestStandingForeignToken(t *testing.T) { func TestStandingOutsideWindowAlarmFails(t *testing.T) { e, tok0, id := useEnv(t) e.clock.Advance(3 * time.Hour) // 15:00 + e.waitMissedAlarm() e.approver.FailAlarm(errors.New("made-up: the alarm cannot be sent")) res, _ := e.useToken("laptop", tok0) mustStatus(t, res, http.StatusForbidden) - if n := len(e.approver.Alarms()); n != 0 { - t.Fatalf("%d alarms went out", n) + if n := len(e.approver.Alarms()); n != 1 { + t.Fatalf("%d alarms went out, want only the missed-window alarm", n) } e.approver.FailAlarm(nil) for range 2 { res, _ = e.useToken("laptop", tok0) mustStatus(t, res, http.StatusForbidden) } - if as := e.approver.Alarms(); len(as) != 1 || as[0].Cause != audit.AlarmOutsideWindow || as[0].GrantID != id { + if as := e.approver.Alarms(); len(as) != 2 || as[1].Cause != audit.AlarmOutsideWindow || as[1].GrantID != id { t.Errorf("the alarms: %+v", as) } } +// waitMissedAlarm waits for the alarm of the window of 11:00-13:00 of +// useEnv, after the clock went past 13:00 with no use in it. The core +// sends it from its own goroutine, at a time that the test does not +// control; a test that counts alarms waits for it first (#186). +func (e *env) waitMissedAlarm() { + e.t.Helper() + as, err := e.approver.WaitAlarms(1, waitLimit) + if err != nil || len(as) != 1 || as[0].Cause != audit.AlarmMissedWindow { + e.t.Fatalf("the missed-window alarm: %+v, %v", as, err) + } +} + // TestStandingBeforeStart: a valid token before the start of the grant is // denied, with an outside_window alarm, and the grant stays. func TestStandingBeforeStart(t *testing.T) {