| 1 | package directorydata |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "database/sql" |
| 8 | "errors" |
| 9 | "math" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func pendingFixture(t *testing.T) (*RosterSource, RosterRequest, string, int64) { |
| 15 | t.Helper() |
| 16 | s, r, path, team := rosterFixture(t, OperatorSchema) |
| 17 | member(t, s.store, team, personA, "owner") |
| 18 | digest := bytes.Repeat([]byte{33}, 32) |
| 19 | tx, e := s.store.db.Begin() |
| 20 | if e != nil { |
| 21 | t.Fatal(e) |
| 22 | } |
| 23 | defer tx.Rollback() |
| 24 | for _, q := range []struct { |
| 25 | sql string |
| 26 | args []any |
| 27 | }{ |
| 28 | {`UPDATE team_instances SET state='pending' WHERE team_id=?`, []any{team}}, |
| 29 | {`INSERT INTO roster_operator_receipts VALUES('provision-alpha',1,'provision','alpha-docs',?,?,?)`, []any{challenge(3), digest, rosterNow.Unix() - 1}}, |
| 30 | {`INSERT INTO directory_operator_consumer_records VALUES('alpha-docs','provision-alpha',1,?,?)`, []any{challenge(3), digest}}, |
| 31 | {`INSERT INTO verification_attempts SELECT ?,'alpha-docs',id,'docs','https://alpha.docs.oficina.works',?,1,?,roster_epoch,?,?,?,? FROM teams WHERE id=?`, []any{challenge(21), challenge(3), digest, personA, rosterNow.Unix(), rosterNow.Unix() + 600, bytes.Repeat([]byte{34}, 32), team}}, |
| 32 | {`INSERT INTO verification_active VALUES('alpha-docs',?)`, []any{challenge(21)}}, |
| 33 | } { |
| 34 | if _, e = tx.Exec(q.sql, q.args...); e != nil { |
| 35 | t.Fatal(e) |
| 36 | } |
| 37 | } |
| 38 | if e = tx.Commit(); e != nil { |
| 39 | t.Fatal(e) |
| 40 | } |
| 41 | return s, r, path, team |
| 42 | } |
| 43 | func pendingStatus(t *testing.T, s *RosterSource, r RosterRequest, at time.Time, want int) { |
| 44 | t.Helper() |
| 45 | p, status, e := s.Read(context.Background(), r, at) |
| 46 | if status != want { |
| 47 | t.Fatalf("status %d, want %d", status, want) |
| 48 | } |
| 49 | if want == 503 && (!errors.Is(e, ErrUnavailable) || p.Type != "") { |
| 50 | t.Fatal("pending refusal differs") |
| 51 | } |
| 52 | if want == 200 && (e != nil || p.Type != "page") { |
| 53 | t.Fatal("missing page") |
| 54 | } |
| 55 | } |
| 56 | func TestPendingRosterWindowBoundariesAndChooser(t *testing.T) { |
| 57 | s, r, _, _ := pendingFixture(t) |
| 58 | for _, offset := range []int64{0, 599, 600, 601} { |
| 59 | want := 200 |
| 60 | if offset >= 600 { |
| 61 | want = 503 |
| 62 | } |
| 63 | pendingStatus(t, s, r, rosterNow.Add(time.Duration(offset)*time.Second), want) |
| 64 | } |
| 65 | choices, e := s.store.Choices(context.Background(), personA, "docs") |
| 66 | if e != nil || len(choices) != 0 { |
| 67 | t.Fatal("pending binding visible") |
| 68 | } |
| 69 | } |
| 70 | func TestPendingRosterRefusesBrokenAuthority(t *testing.T) { |
| 71 | cases := []struct { |
| 72 | name, sql string |
| 73 | args []any |
| 74 | }{ |
| 75 | {"missing active", `DELETE FROM verification_active`, nil}, |
| 76 | {"missing pointer", `DELETE FROM directory_operator_consumer_records`, nil}, |
| 77 | {"wrong generation", `UPDATE verification_attempts SET generation=?`, []any{challenge(4)}}, |
| 78 | {"changed current generation", `UPDATE roster_generation SET generation=?`, []any{challenge(4)}}, |
| 79 | {"wrong record sequence", `UPDATE verification_attempts SET record_sequence=2`, nil}, |
| 80 | {"wrong record digest", `UPDATE verification_attempts SET record_digest=?`, []any{bytes.Repeat([]byte{5}, 32)}}, |
| 81 | {"wrong pointer generation", `UPDATE directory_operator_consumer_records SET generation=?`, []any{challenge(4)}}, |
| 82 | {"wrong pointer receipt", `UPDATE roster_operator_receipts SET sequence=2`, nil}, |
| 83 | {"wrong owner", `UPDATE verification_attempts SET owner_ref=?`, []any{personB}}, |
| 84 | {"owner removed", `DELETE FROM memberships WHERE person_ref=?`, []any{personA}}, |
| 85 | {"owner demoted", `UPDATE memberships SET role='member' WHERE person_ref=?`, []any{personA}}, |
| 86 | {"wrong team", `UPDATE verification_attempts SET team_id=(SELECT id FROM teams WHERE sqid='B2')`, nil}, |
| 87 | {"wrong app", `UPDATE verification_attempts SET app='sheets'`, nil}, |
| 88 | {"wrong origin", `UPDATE verification_attempts SET origin='https://beta.docs.oficina.works'`, nil}, |
| 89 | {"future issued", `UPDATE verification_attempts SET issued_at=issued_at+1,expires_at=expires_at+1`, nil}, |
| 90 | {"negative issued", `UPDATE verification_attempts SET issued_at=-1`, nil}, |
| 91 | {"zero issued", `UPDATE verification_attempts SET issued_at=0`, nil}, |
| 92 | {"expired", `UPDATE verification_attempts SET issued_at=issued_at-600,expires_at=expires_at-600`, nil}, |
| 93 | {"long lifetime", `UPDATE verification_attempts SET expires_at=expires_at+1`, nil}, |
| 94 | {"overflow lifetime", `UPDATE verification_attempts SET issued_at=?,expires_at=?`, []any{int64(-1), int64(math.MaxInt64)}}, |
| 95 | {"reversed lifetime", `UPDATE verification_attempts SET expires_at=issued_at`, nil}, |
| 96 | {"partial authorization", `UPDATE verification_attempts SET authorization_digest=x'00'`, nil}, |
| 97 | } |
| 98 | for _, tc := range cases { |
| 99 | t.Run(tc.name, func(t *testing.T) { |
| 100 | s, r, _, _ := pendingFixture(t) |
| 101 | exec(t, s.store.db, `PRAGMA ignore_check_constraints=ON`) |
| 102 | exec(t, s.store.db, tc.sql, tc.args...) |
| 103 | pendingStatus(t, s, r, rosterNow, 503) |
| 104 | }) |
| 105 | } |
| 106 | } |
| 107 | func TestPendingRosterTokenScopeAndLiveUnchanged(t *testing.T) { |
| 108 | s, r, _, team := pendingFixture(t) |
| 109 | bad := r |
| 110 | bad.Token = challenge(17) |
| 111 | pendingStatus(t, s, bad, rosterNow, 401) |
| 112 | bad = r |
| 113 | bad.Team = "B2" |
| 114 | pendingStatus(t, s, bad, rosterNow, 404) |
| 115 | bad.Team = "Missing" |
| 116 | pendingStatus(t, s, bad, rosterNow, 404) |
| 117 | token := challenge(18) |
| 118 | digest := sha256.Sum256([]byte(token)) |
| 119 | exec(t, s.store.db, `UPDATE roster_tokens SET slot='previous',issued_at=?,expires_at=?`, rosterNow.Unix(), rosterNow.Unix()+300) |
| 120 | exec(t, s.store.db, `INSERT INTO roster_tokens VALUES(?,'alpha-docs','current',?,NULL)`, digest[:], rosterNow.Unix()) |
| 121 | pendingStatus(t, s, r, rosterNow.Add(299*time.Second), 200) |
| 122 | pendingStatus(t, s, r, rosterNow.Add(300*time.Second), 401) |
| 123 | r.Token = token |
| 124 | pendingStatus(t, s, r, rosterNow.Add(599*time.Second), 200) |
| 125 | exec(t, s.store.db, `UPDATE roster_consumers SET enabled=0`) |
| 126 | pendingStatus(t, s, r, rosterNow, 401) |
| 127 | exec(t, s.store.db, `UPDATE roster_consumers SET enabled=1`) |
| 128 | exec(t, s.store.db, `UPDATE team_instances SET state='live' WHERE team_id=?`, team) |
| 129 | exec(t, s.store.db, `DELETE FROM verification_active`) |
| 130 | pendingStatus(t, s, r, rosterNow.Add(601*time.Second), 200) |
| 131 | } |
| 132 | func TestPendingRosterEpochAndExpiryOnCursor(t *testing.T) { |
| 133 | s, r, _, team := pendingFixture(t) |
| 134 | exec(t, s.store.db, `DELETE FROM memberships WHERE team_id=?`, team) |
| 135 | addRoster(t, s, team, 200) |
| 136 | exec(t, s.store.db, `UPDATE memberships SET role='owner' WHERE team_id=? AND person_ref=?`, team, personA) |
| 137 | p, status, e := s.Read(context.Background(), r, rosterNow.Add(595*time.Second)) |
| 138 | if e != nil || status != 200 || p.Cursor == nil { |
| 139 | t.Fatal("missing first page") |
| 140 | } |
| 141 | r.Cursor = *p.Cursor |
| 142 | pendingStatus(t, s, r, rosterNow.Add(599*time.Second), 200) |
| 143 | pendingStatus(t, s, r, rosterNow.Add(600*time.Second), 503) |
| 144 | r.Cursor = "" |
| 145 | p, _, e = s.Read(context.Background(), r, rosterNow) |
| 146 | if e != nil || p.Cursor == nil { |
| 147 | t.Fatal("missing cursor") |
| 148 | } |
| 149 | r.Cursor = *p.Cursor |
| 150 | member(t, s.store, team, personB, "member") |
| 151 | p, status, e = s.Read(context.Background(), r, rosterNow) |
| 152 | if e != nil || status != 409 || p.Members != nil { |
| 153 | t.Fatal("epoch drift was not conflict") |
| 154 | } |
| 155 | r.Cursor = "" |
| 156 | pendingStatus(t, s, r, rosterNow, 200) |
| 157 | exec(t, s.store.db, `DELETE FROM memberships WHERE person_ref=?`, personA) |
| 158 | pendingStatus(t, s, r, rosterNow, 503) |
| 159 | } |
| 160 | |
| 161 | func TestPendingRosterWindowCannotCrossConsumers(t *testing.T) { |
| 162 | s, r, _, _ := pendingFixture(t) |
| 163 | var beta int64 |
| 164 | if e := s.store.db.QueryRow(`SELECT id FROM teams WHERE sqid='B2'`).Scan(&beta); e != nil { |
| 165 | t.Fatal(e) |
| 166 | } |
| 167 | exec(t, s.store.db, `UPDATE team_instances SET state='pending' WHERE team_id=?`, beta) |
| 168 | exec(t, s.store.db, `INSERT INTO roster_consumers VALUES('beta-docs',?,'docs','https://beta.docs.oficina.works',1)`, beta) |
| 169 | token := challenge(19) |
| 170 | digest := sha256.Sum256([]byte(token)) |
| 171 | exec(t, s.store.db, `INSERT INTO roster_tokens VALUES(?,'beta-docs','current',?,NULL)`, digest[:], rosterNow.Unix()) |
| 172 | exec(t, s.store.db, `DELETE FROM verification_active`) |
| 173 | exec(t, s.store.db, `INSERT INTO verification_active VALUES('beta-docs',?)`, challenge(21)) |
| 174 | r.Team = "B2" |
| 175 | r.Token = token |
| 176 | pendingStatus(t, s, r, rosterNow, 503) |
| 177 | } |
| 178 | |
| 179 | func TestPendingRosterReadSnapshotIncludesWindow(t *testing.T) { |
| 180 | s, r, path, team := pendingFixture(t) |
| 181 | exec(t, s.store.db, `DELETE FROM memberships WHERE team_id=?`, team) |
| 182 | addRoster(t, s, team, 201) |
| 183 | exec(t, s.store.db, `UPDATE memberships SET role='owner' WHERE team_id=? AND person_ref=?`, team, personA) |
| 184 | p, _, e := s.Read(context.Background(), r, rosterNow) |
| 185 | if e != nil || p.Cursor == nil { |
| 186 | t.Fatal("missing page") |
| 187 | } |
| 188 | r.Cursor = *p.Cursor |
| 189 | writer, e := sql.Open("sqlite", path) |
| 190 | if e != nil { |
| 191 | t.Fatal(e) |
| 192 | } |
| 193 | defer writer.Close() |
| 194 | s.store.sealer = &onOpenSealer{Sealer: s.store.sealer, once: func() { exec(t, writer, `DELETE FROM verification_active`) }} |
| 195 | pendingStatus(t, s, r, rosterNow, 200) |
| 196 | pendingStatus(t, s, r, rosterNow, 503) |
| 197 | } |
| 198 | |