| 1 | package creds |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io/fs" |
| 7 | "slices" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // Task 9 follow-up, round 3, #1: a watch settled at a restart is |
| 12 | // acknowledged (Keychain.Acked) only once its record is durably checked |
| 13 | // or removed, so the proof of its end it used may go. One whose end is |
| 14 | // not proven, or whose settling could not list the Keychain, is not. |
| 15 | func TestSettledWatchAcknowledgedOnceDurable(t *testing.T) { |
| 16 | ctx := context.Background() |
| 17 | f := &fakeKeychain{} |
| 18 | k := newWatch(t, f) |
| 19 | _ = k.Before(ctx, "signin-1", configDir) |
| 20 | f.add(own()) |
| 21 | restarted := &Keychain{Dir: k.Dir, Login: login, User: "paulequipo", List: f.list, Delete: f.delete} |
| 22 | var acked []string |
| 23 | restarted.Acked = func(w Watch) { |
| 24 | rec, err := restarted.load(w.Marker) |
| 25 | if !errors.Is(err, fs.ErrNotExist) && (err != nil || !rec.Checked) { |
| 26 | t.Errorf("acknowledged %s before its record was settled: %+v %v", w.Marker, rec, err) |
| 27 | } |
| 28 | acked = append(acked, w.Marker) |
| 29 | } |
| 30 | _ = restarted.Settle(ctx, true, func(Watch) bool { return false }) |
| 31 | f.broken = true |
| 32 | _ = restarted.Settle(ctx, true, func(Watch) bool { return true }) |
| 33 | f.broken = false |
| 34 | // The second look lists, but the recheck's listing fails: the record |
| 35 | // is never saved as checked. |
| 36 | n := 0 |
| 37 | restarted.List = func(ctx context.Context, kc string) ([]Item, error) { |
| 38 | if n++; n == 2 { |
| 39 | return nil, errors.New("listing failed") |
| 40 | } |
| 41 | return f.list(ctx, kc) |
| 42 | } |
| 43 | _ = restarted.Settle(ctx, true, func(Watch) bool { return true }) |
| 44 | restarted.List = f.list |
| 45 | if len(acked) != 0 { |
| 46 | t.Fatalf("acknowledged without a settled record: %v", acked) |
| 47 | } |
| 48 | if err := restarted.Settle(ctx, true, func(Watch) bool { return true }); err != nil || !slices.Equal(acked, []string{"signin-1"}) { |
| 49 | t.Fatalf("settled watch: err %v, acknowledged %v", err, acked) |
| 50 | } |
| 51 | } |
| 52 | |