| 1 | // Package teammodetest is team mode's test fixture, shared by every app's |
| 2 | // team tests: a directory that signs roster pages in memory from a member |
| 3 | // list a test can change or take down, a provisioning record signed for it, |
| 4 | // and a roster clock a test can step past the lease. It is Docs' |
| 5 | // testTeamRoster and Sheets' team_fixture_test.go, extracted. Never import |
| 6 | // it outside tests. |
| 7 | package teammodetest |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "encoding/base64" |
| 12 | "errors" |
| 13 | "io" |
| 14 | "net/http" |
| 15 | "sort" |
| 16 | "strings" |
| 17 | "sync" |
| 18 | "testing" |
| 19 | "time" |
| 20 | |
| 21 | "amadan.net/oficina/home/roster" |
| 22 | "amadan.net/oficina/home/teammode" |
| 23 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 24 | "github.com/go-json-experiment/json" |
| 25 | ) |
| 26 | |
| 27 | // Test deployment values. Any https origins would do; these are the ones |
| 28 | // Docs' and Sheets' fixtures use. |
| 29 | const ( |
| 30 | Issuer = "https://id.example.com" |
| 31 | DirectoryOrigin = "https://home.example.com" |
| 32 | Team = "TeamA" |
| 33 | ) |
| 34 | |
| 35 | // Subject is a well-formed person ref: "pr_" and 26 copies of c, which must |
| 36 | // be a lower-case letter or a digit 2-7. |
| 37 | func Subject(c byte) string { return "pr_" + strings.Repeat(string(c), 26) } |
| 38 | |
| 39 | // Bindings for app, its Home consumer ID and its origin, on the test |
| 40 | // issuer, directory and team. |
| 41 | func Bindings(app, consumer, origin string) roster.Bindings { |
| 42 | return roster.Bindings{Issuer: Issuer, Directory: DirectoryOrigin, Consumer: consumer, App: app, Team: Team, Audience: origin} |
| 43 | } |
| 44 | |
| 45 | // Home is the teammode.Home the bindings name. |
| 46 | func Home(b roster.Bindings) *teammode.Home { |
| 47 | return &teammode.Home{Issuer: b.Issuer, ConsumerID: b.Consumer} |
| 48 | } |
| 49 | |
| 50 | // Clock is a steppable roster.Clock. Tick, when set, advances it by that |
| 51 | // much after every reading, so time passes between two observations in one |
| 52 | // request (a lease checked twice). |
| 53 | type Clock struct { |
| 54 | mu sync.Mutex |
| 55 | base time.Time |
| 56 | elapsed time.Duration |
| 57 | wall time.Duration |
| 58 | tick time.Duration |
| 59 | } |
| 60 | |
| 61 | func NewClock() *Clock { return &Clock{base: time.Unix(1800000000, 0)} } |
| 62 | |
| 63 | func (c *Clock) Now() (time.Time, time.Duration) { |
| 64 | c.mu.Lock() |
| 65 | defer c.mu.Unlock() |
| 66 | w, m := c.base.Add(c.elapsed+c.wall), c.elapsed |
| 67 | c.elapsed += c.tick |
| 68 | return w, m |
| 69 | } |
| 70 | |
| 71 | // Step advances wall and monotonic time together. |
| 72 | func (c *Clock) Step(d time.Duration) { c.mu.Lock(); c.elapsed += d; c.mu.Unlock() } |
| 73 | |
| 74 | // WallStep moves wall time alone, as a clock adjustment would. |
| 75 | func (c *Clock) WallStep(d time.Duration) { c.mu.Lock(); c.wall += d; c.mu.Unlock() } |
| 76 | |
| 77 | // SetTick makes every reading advance the clock by d. |
| 78 | func (c *Clock) SetTick(d time.Duration) { c.mu.Lock(); c.tick = d; c.mu.Unlock() } |
| 79 | |
| 80 | // Directory is an in-memory Home directory serving one team's roster. It is |
| 81 | // an http.RoundTripper for teammode.RuntimeConfig.Transport. |
| 82 | type Directory struct { |
| 83 | mu sync.Mutex |
| 84 | key *family.Keypair |
| 85 | bindings roster.Bindings |
| 86 | generation string |
| 87 | epoch int64 |
| 88 | members map[string]string |
| 89 | down bool |
| 90 | fetches int |
| 91 | } |
| 92 | |
| 93 | // Set changes subject's role ("owner" or "member"), or removes them with |
| 94 | // "". Every change is a new epoch, as Home's triggers make it. |
| 95 | func (d *Directory) Set(subject, role string) { |
| 96 | d.mu.Lock() |
| 97 | defer d.mu.Unlock() |
| 98 | if role == "" { |
| 99 | delete(d.members, subject) |
| 100 | } else { |
| 101 | d.members[subject] = role |
| 102 | } |
| 103 | d.epoch++ |
| 104 | } |
| 105 | |
| 106 | // SetDown makes every fetch fail (true) or succeed again (false). |
| 107 | func (d *Directory) SetDown(down bool) { d.mu.Lock(); d.down = down; d.mu.Unlock() } |
| 108 | |
| 109 | // Fetches counts roster requests, failed ones included. |
| 110 | func (d *Directory) Fetches() int { d.mu.Lock(); defer d.mu.Unlock(); return d.fetches } |
| 111 | |
| 112 | func (d *Directory) RoundTrip(r *http.Request) (*http.Response, error) { |
| 113 | d.mu.Lock() |
| 114 | defer d.mu.Unlock() |
| 115 | d.fetches++ |
| 116 | if d.down || r.URL.Scheme+"://"+r.URL.Host != d.bindings.Directory || r.URL.Path != "/roster" { |
| 117 | return nil, errors.New("directory unavailable") |
| 118 | } |
| 119 | members := []roster.MemberEntry{} |
| 120 | for s, role := range d.members { |
| 121 | members = append(members, roster.MemberEntry{Subject: s, Role: role}) |
| 122 | } |
| 123 | sort.Slice(members, func(i, j int) bool { return members[i].Subject < members[j].Subject }) |
| 124 | i, complete := 0, true |
| 125 | q := r.URL.Query() |
| 126 | page := roster.Page{Version: 1, Type: "page", Directory: d.bindings.Directory, Consumer: d.bindings.Consumer, Team: d.bindings.Team, Audience: d.bindings.Audience, |
| 127 | Fetch: q.Get("fetch"), RequestCursorHash: roster.CursorHash(q.Get("cursor")), Generation: d.generation, Epoch: d.epoch, Index: &i, Members: &members, Complete: &complete} |
| 128 | raw, err := roster.SignPage(d.key, "roster1", page) |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(bytes.NewReader(raw))}, nil |
| 133 | } |
| 134 | |
| 135 | // Provision signs a provisioning record for b with a fresh operator key and |
| 136 | // returns the teammode.Config that carries it, plus the directory that |
| 137 | // answers for it (empty roster, epoch 1). |
| 138 | func Provision(t testing.TB, b roster.Bindings) (*teammode.Config, *Directory) { |
| 139 | t.Helper() |
| 140 | op, err := family.Generate() |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | key, err := family.Generate() |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | p := roster.Provisioning{Version: 1, Operation: "provision1", Sequence: 1, Bindings: b, Generation: Fixed(1), MinEpoch: 1, |
| 149 | Keys: []roster.Key{{ID: "roster1", Public: base64.RawURLEncoding.EncodeToString(key.SignPub())}}} |
| 150 | raw, err := roster.SignProvisioning(op, "operator1", p) |
| 151 | if err != nil { |
| 152 | t.Fatal(err) |
| 153 | } |
| 154 | keys, err := json.Marshal([]roster.Key{{ID: "operator1", Public: base64.RawURLEncoding.EncodeToString(op.SignPub())}}) |
| 155 | if err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | dir := &Directory{key: key, bindings: b, generation: p.Generation, epoch: 1, members: map[string]string{}} |
| 159 | cfg := &teammode.Config{TeamID: b.Team, DirectoryOrigin: b.Directory, |
| 160 | ProvisioningBase64: base64.StdEncoding.EncodeToString(raw), OperatorKeysBase64: base64.StdEncoding.EncodeToString(keys), RosterToken: Fixed(7)} |
| 161 | return cfg, dir |
| 162 | } |
| 163 | |
| 164 | // Fixed is 32 bytes of n, base64url: a well-formed token or generation. |
| 165 | func Fixed(n byte) string { return base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{n}, 32)) } |
| 166 | |