| 1 | // Package exporttest is the web UI's shared fixture: a complete, valid |
| 2 | // export built from a small synthetic valley, with its signed manifest and |
| 3 | // the verifier key that opens it. The publisher's, the receiver's and the |
| 4 | // server's tests all build it, so they test against the same bytes. |
| 5 | // |
| 6 | // The synthetic valley has two projects. app is public; site is hidden. |
| 7 | // Both belong to one group, whose identifier is core and whose name, which |
| 8 | // its web URLs open with, is tools; t is its alias. Each project's web |
| 9 | // name is its identifier, and its short alias too. app was renamed from |
| 10 | // old-app, and site from old-site, which each keeps as a former name. The |
| 11 | // export carries app's names alone. |
| 12 | // app has a milestone with five children, among them a claimed knot, an |
| 13 | // unclaimed knot in progress, a knot blocked by a local knot and by one of |
| 14 | // site's, a bug whose body and comment hold HTML, and comments by mapped |
| 15 | // and unmapped principals, one of them edited. It has one ready change, whose earlier version, |
| 16 | // draft, branch and repository URL the export drops. Each project has a |
| 17 | // real bare repository whose main has a few commits. |
| 18 | // |
| 19 | // The valley's JSON is literal, under valley/, in the shapes valley plan, |
| 20 | // valley pending and valley knot show print. testdata/export.json is the |
| 21 | // export built from it, kept as the contract's expected output. |
| 22 | package exporttest |
| 23 | |
| 24 | import ( |
| 25 | "bytes" |
| 26 | "context" |
| 27 | "crypto/ed25519" |
| 28 | "embed" |
| 29 | "errors" |
| 30 | "fmt" |
| 31 | "io/fs" |
| 32 | "net/http/httptest" |
| 33 | "os" |
| 34 | "os/exec" |
| 35 | "path/filepath" |
| 36 | "sort" |
| 37 | "strings" |
| 38 | "testing" |
| 39 | "time" |
| 40 | |
| 41 | "the-valley/note" |
| 42 | "the-valley/web/export" |
| 43 | ) |
| 44 | |
| 45 | //go:embed valley |
| 46 | var valley embed.FS |
| 47 | |
| 48 | const ( |
| 49 | // Instance and Destination are what the fixture's manifest is signed |
| 50 | // for, and what a receiver admitting it is configured with. |
| 51 | Instance = "valley-host" |
| 52 | Destination = "example.org" |
| 53 | // SignerName is the export key's name in the manifest's signature. |
| 54 | SignerName = "valley-host/web-export" |
| 55 | ) |
| 56 | |
| 57 | var ( |
| 58 | // Collected is the synthetic valley's plan time, and SignedAt the |
| 59 | // manifest's signing time, whose Unix nanoseconds are its sequence. |
| 60 | Collected = time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC) |
| 61 | SignedAt = time.Date(2026, 10, 25, 12, 0, 30, 0, time.UTC) |
| 62 | |
| 63 | // Principals is the publisher's display mapping. drive-by, who wrote |
| 64 | // a bug and a comment, is not in it. |
| 65 | Principals = export.Principals{"app-operator": "operator", "app-agent": "agent"} |
| 66 | |
| 67 | // Identities export app alone, with its publication identity. |
| 68 | Identities = map[string]string{"app": "9da6809f038df92138c131cd16bed76d"} |
| 69 | |
| 70 | // Groups are the instance's one group, and Placements both projects' |
| 71 | // places in it. |
| 72 | Groups = []export.Group{{ID: "core", Name: "tools", Aliases: []string{"t"}}} |
| 73 | Placements = map[string]export.Placement{ |
| 74 | "app": {Group: "core", Name: "app", FormerNames: []string{"old-app"}, Aliases: []string{"app"}}, |
| 75 | "site": {Group: "core", Name: "site", FormerNames: []string{"old-site"}, Aliases: []string{"site"}}, |
| 76 | } |
| 77 | ) |
| 78 | |
| 79 | // Key is the fixture's export key. It is derived from a fixed seed, so its |
| 80 | // verifier is the same in every run. |
| 81 | func Key() ed25519.PrivateKey { |
| 82 | return ed25519.NewKeyFromSeed([]byte("the-valley web export test key!!")) |
| 83 | } |
| 84 | |
| 85 | // Verifier is the key a receiver pins to admit the fixture's manifest. |
| 86 | func Verifier() note.VerifierKey { |
| 87 | return note.NewVerifierKey(SignerName, Key().Public().(ed25519.PublicKey)) |
| 88 | } |
| 89 | |
| 90 | // Fixture is the synthetic valley and everything built from it. |
| 91 | type Fixture struct { |
| 92 | // Inputs are the synthetic valley's JSON, its visibility, its source |
| 93 | // tips and the display mapping: what the projection reads. |
| 94 | Inputs export.Inputs |
| 95 | // Repos are both projects' bare repositories, by project name. |
| 96 | Repos map[string]string |
| 97 | |
| 98 | // Export is the export built from Inputs, and ExportJSON its bytes. |
| 99 | Export *export.Export |
| 100 | ExportJSON []byte |
| 101 | // Bundles are each exported project's bundle of main, by project. |
| 102 | Bundles map[string]string |
| 103 | // Manifest is the export's manifest, and Signed the manifest signed |
| 104 | // with Key. |
| 105 | Manifest *export.Manifest |
| 106 | Signed []byte |
| 107 | |
| 108 | // Dir is a complete export directory, laid out as a generation is: |
| 109 | // export.json and repos/<project>.git. |
| 110 | Dir string |
| 111 | } |
| 112 | |
| 113 | // New builds the fixture under a temporary directory of t's. |
| 114 | func New(t testing.TB) *Fixture { |
| 115 | t.Helper() |
| 116 | root := t.TempDir() |
| 117 | f := &Fixture{Repos: map[string]string{}, Bundles: map[string]string{}} |
| 118 | ctx := context.Background() |
| 119 | |
| 120 | in := export.Inputs{ |
| 121 | Plan: read(t, "valley/plan.json"), |
| 122 | Pending: read(t, "valley/pending.json"), |
| 123 | Identities: Identities, |
| 124 | SourceTips: map[string]string{}, |
| 125 | Principals: Principals, |
| 126 | Groups: Groups, |
| 127 | Placements: Placements, |
| 128 | } |
| 129 | details, err := fs.Glob(valley, "valley/details/*/*.json") |
| 130 | if err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | for _, path := range details { |
| 134 | in.Details = append(in.Details, read(t, path)) |
| 135 | } |
| 136 | |
| 137 | for name, commits := range history { |
| 138 | repo := filepath.Join(root, "valley", name+".git") |
| 139 | git(t, "", nil, "init", "--quiet", "--bare", "--template=", "--initial-branch=main", repo) |
| 140 | git(t, repo, fastImport(commits), "fast-import", "--quiet") |
| 141 | f.Repos[name] = repo |
| 142 | in.SourceTips[name] = strings.TrimSpace(string(git(t, repo, nil, "rev-parse", "refs/heads/main"))) |
| 143 | } |
| 144 | // The source tip of a project that is not exported is never read. |
| 145 | for name := range in.SourceTips { |
| 146 | if _, ok := Identities[name]; !ok { |
| 147 | delete(in.SourceTips, name) |
| 148 | } |
| 149 | } |
| 150 | f.Inputs = in |
| 151 | |
| 152 | f.Export, err = export.Build(in) |
| 153 | if err != nil { |
| 154 | t.Fatal(err) |
| 155 | } |
| 156 | f.ExportJSON, err = f.Export.Encode() |
| 157 | if err != nil { |
| 158 | t.Fatal(err) |
| 159 | } |
| 160 | |
| 161 | bodies := map[string]export.Body{} |
| 162 | for _, p := range f.Export.Projects { |
| 163 | path := filepath.Join(root, "bundles", p.Name+".bundle") |
| 164 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | git(t, f.Repos[p.Name], nil, "bundle", "create", "--quiet", path, "main") |
| 168 | f.Bundles[p.Name] = path |
| 169 | if bodies[p.Name], err = export.FileBody(p.Name, path); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | } |
| 173 | f.Manifest, err = export.NewManifest(Instance, Destination, SignedAt.UnixNano(), f.Export, f.ExportJSON, bodies) |
| 174 | if err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | f.Signed = f.Sign(t, f.Manifest) |
| 178 | |
| 179 | f.Dir = filepath.Join(root, "export") |
| 180 | if err := os.MkdirAll(filepath.Join(f.Dir, "repos"), 0o755); err != nil { |
| 181 | t.Fatal(err) |
| 182 | } |
| 183 | if err := os.WriteFile(filepath.Join(f.Dir, export.ExportFile), f.ExportJSON, 0o644); err != nil { |
| 184 | t.Fatal(err) |
| 185 | } |
| 186 | for _, p := range f.Export.Projects { |
| 187 | repo := filepath.Join(f.Dir, "repos", p.Name+".git") |
| 188 | if err := export.AcceptBundle(ctx, f.Bundles[p.Name], repo, p.SourceTip); err != nil { |
| 189 | t.Fatal(err) |
| 190 | } |
| 191 | } |
| 192 | return f |
| 193 | } |
| 194 | |
| 195 | // Sign signs a manifest with the fixture's key, so a test can sign a |
| 196 | // manifest it changed. |
| 197 | func (f *Fixture) Sign(t testing.TB, m *export.Manifest) []byte { |
| 198 | t.Helper() |
| 199 | signed, err := m.Sign(SignerName, Key()) |
| 200 | if err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | return signed |
| 204 | } |
| 205 | |
| 206 | // StorePath is the path of a new store's descriptor, in a directory of |
| 207 | // its own. |
| 208 | func StorePath(t testing.TB) string { |
| 209 | t.Helper() |
| 210 | return filepath.Join(t.TempDir(), "store") |
| 211 | } |
| 212 | |
| 213 | // Authorization is an authorization with sequence seq that lets anyone |
| 214 | // read each project given, under its identity in Identities. |
| 215 | func Authorization(seq int64, projects ...string) *export.Authorization { |
| 216 | a := &export.Authorization{Instance: Instance, Destination: Destination, Sequence: seq, Checked: Collected} |
| 217 | for _, name := range projects { |
| 218 | a.Projects = append(a.Projects, export.AuthorizedProject{Name: name, Identity: Identities[name], Readers: []string{export.Anyone}}) |
| 219 | } |
| 220 | return a |
| 221 | } |
| 222 | |
| 223 | // SignAuthorization is the authorization signed by the fixture's key. |
| 224 | func SignAuthorization(t testing.TB, a *export.Authorization) []byte { |
| 225 | t.Helper() |
| 226 | signed, err := a.Sign(SignerName, Key()) |
| 227 | if err != nil { |
| 228 | t.Fatal(err) |
| 229 | } |
| 230 | return signed |
| 231 | } |
| 232 | |
| 233 | // Authorize stores in s, as a receiver does, an authorization one |
| 234 | // sequence past the stored one's that lets anyone read each project given. |
| 235 | func Authorize(t testing.TB, s *export.Store, projects ...string) *export.Authorization { |
| 236 | t.Helper() |
| 237 | hw, err := s.AuthorizationHighWater() |
| 238 | if err != nil { |
| 239 | t.Fatal(err) |
| 240 | } |
| 241 | a := Authorization(hw+1, projects...) |
| 242 | if err := s.Authorize(context.Background(), a, SignAuthorization(t, a)); err != nil { |
| 243 | t.Fatal(err) |
| 244 | } |
| 245 | return a |
| 246 | } |
| 247 | |
| 248 | // Admission is what a receiver admits the fixture's manifest with: the |
| 249 | // pinned verifier, the instance and destination, no high-water mark, and a |
| 250 | // clock a second past the signing time. |
| 251 | func (f *Fixture) Admission() export.Admission { |
| 252 | return export.Admission{ |
| 253 | Verifiers: []note.VerifierKey{Verifier()}, |
| 254 | Instance: Instance, |
| 255 | Destination: Destination, |
| 256 | Now: SignedAt.Add(time.Second), |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // BodyBytes is the content of the body the manifest declares under name: |
| 261 | // export.json, or a project's bundle. |
| 262 | func (f *Fixture) BodyBytes(t testing.TB, name string) []byte { |
| 263 | t.Helper() |
| 264 | if name == export.ExportFile { |
| 265 | return f.ExportJSON |
| 266 | } |
| 267 | path, ok := f.Bundles[name] |
| 268 | if !ok { |
| 269 | t.Fatalf("the fixture has no body %s", name) |
| 270 | } |
| 271 | data, err := os.ReadFile(path) |
| 272 | if err != nil { |
| 273 | t.Fatal(err) |
| 274 | } |
| 275 | return data |
| 276 | } |
| 277 | |
| 278 | // Upload runs the upload's three steps against the store, as a receiver |
| 279 | // does: it admits the signed manifest, stages it, stages every body the |
| 280 | // inventory needs, and activates it. It returns the active generation. A |
| 281 | // store that holds no authorization is first given one that lets anyone |
| 282 | // read every project the fixture exports, as a publisher sends one before |
| 283 | // content. |
| 284 | func (f *Fixture) Upload(t testing.TB, s *export.Store) export.Generation { |
| 285 | t.Helper() |
| 286 | if _, err := s.ReadAuthorization(); errors.Is(err, export.ErrNoAuthorization) { |
| 287 | var names []string |
| 288 | for _, p := range f.Export.Projects { |
| 289 | names = append(names, p.Name) |
| 290 | } |
| 291 | Authorize(t, s, names...) |
| 292 | } |
| 293 | hw, err := s.HighWater() |
| 294 | if err != nil { |
| 295 | t.Fatal(err) |
| 296 | } |
| 297 | a := f.Admission() |
| 298 | a.HighWater = hw |
| 299 | m, err := export.VerifyManifest(bytes.NewReader(f.Signed), a) |
| 300 | if err != nil { |
| 301 | t.Fatal(err) |
| 302 | } |
| 303 | st, err := s.Stage(context.Background(), m) |
| 304 | if err != nil { |
| 305 | t.Fatal(err) |
| 306 | } |
| 307 | inv, err := s.Inventory(st) |
| 308 | if err != nil { |
| 309 | t.Fatal(err) |
| 310 | } |
| 311 | for _, need := range inv.Needs { |
| 312 | body := f.BodyBytes(t, need.Name) |
| 313 | req := httptest.NewRequest("PUT", export.BodyPath(m.Sequence, need.SHA256), bytes.NewReader(body)) |
| 314 | want := export.Body{Name: need.Name, SHA256: need.SHA256, Size: need.Size} |
| 315 | if _, err := export.StageBody(st.Bodies(), want, 64<<20, req); err != nil { |
| 316 | t.Fatal(err) |
| 317 | } |
| 318 | } |
| 319 | g, err := s.Activate(context.Background(), st) |
| 320 | if err != nil { |
| 321 | t.Fatal(err) |
| 322 | } |
| 323 | return g |
| 324 | } |
| 325 | |
| 326 | func read(t testing.TB, path string) []byte { |
| 327 | t.Helper() |
| 328 | data, err := valley.ReadFile(path) |
| 329 | if err != nil { |
| 330 | t.Fatal(err) |
| 331 | } |
| 332 | return data |
| 333 | } |
| 334 | |
| 335 | // commit is one commit of a fixture repository's main: its time, its |
| 336 | // message, and the files it writes. |
| 337 | type commit struct { |
| 338 | at time.Time |
| 339 | message string |
| 340 | files map[string]string |
| 341 | } |
| 342 | |
| 343 | // history is each project's main, oldest commit first. Every commit's |
| 344 | // author, committer and time are fixed, so the tips are the same in every |
| 345 | // run. |
| 346 | var history = map[string][]commit{ |
| 347 | "app": { |
| 348 | {time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC), "app: the first README", map[string]string{ |
| 349 | "README.md": "# app\n\nA synthetic project for the web UI's tests.\n", |
| 350 | }}, |
| 351 | {time.Date(2026, 10, 2, 9, 0, 0, 0, time.UTC), "app: a program", map[string]string{ |
| 352 | "src/main.go": "package main\n\nfunc main() {}\n", |
| 353 | }}, |
| 354 | {time.Date(2026, 10, 3, 9, 0, 0, 0, time.UTC), "docs: a guide, and the README names it", map[string]string{ |
| 355 | "README.md": "# app\n\nA synthetic project for the web UI's tests. See docs/guide.md.\n\n<script>alert(\"readme\")</script>\n", |
| 356 | "docs/guide.md": "# Guide\n\nRead the plan, then the source.\n", |
| 357 | }}, |
| 358 | }, |
| 359 | "site": { |
| 360 | {time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC), "site: hidden", map[string]string{ |
| 361 | "README.md": "# site\n\nThis project is not exported.\n", |
| 362 | }}, |
| 363 | }, |
| 364 | } |
| 365 | |
| 366 | // fastImport is the git fast-import stream that writes commits to main. |
| 367 | func fastImport(commits []commit) []byte { |
| 368 | var b bytes.Buffer |
| 369 | data := func(s string) { fmt.Fprintf(&b, "data %d\n%s\n", len(s), s) } |
| 370 | for _, c := range commits { |
| 371 | who := fmt.Sprintf("Example Author <author@example.org> %d +0000", c.at.Unix()) |
| 372 | fmt.Fprintf(&b, "commit refs/heads/main\nauthor %s\ncommitter %s\n", who, who) |
| 373 | data(c.message + "\n") |
| 374 | var paths []string |
| 375 | for path := range c.files { |
| 376 | paths = append(paths, path) |
| 377 | } |
| 378 | sort.Strings(paths) |
| 379 | for _, path := range paths { |
| 380 | fmt.Fprintf(&b, "M 100644 inline %s\n", path) |
| 381 | data(c.files[path]) |
| 382 | } |
| 383 | } |
| 384 | return b.Bytes() |
| 385 | } |
| 386 | |
| 387 | // gitTimeout is how long the fixture waits for one git command. |
| 388 | const gitTimeout = 2 * time.Minute |
| 389 | |
| 390 | // git runs git as the export package does: in dir unless it is "", with no |
| 391 | // GIT_ variable of the caller's and no system or global configuration. It |
| 392 | // runs git with Run, so git and anything it started are killed once git |
| 393 | // exits, or once gitTimeout has passed, which fails the test. |
| 394 | func git(t testing.TB, dir string, stdin []byte, args ...string) []byte { |
| 395 | t.Helper() |
| 396 | if dir != "" { |
| 397 | args = append([]string{"-C", dir}, args...) |
| 398 | } |
| 399 | cmd := exec.Command(export.GitProgram, args...) |
| 400 | for _, kv := range os.Environ() { |
| 401 | if !strings.HasPrefix(kv, "GIT_") { |
| 402 | cmd.Env = append(cmd.Env, kv) |
| 403 | } |
| 404 | } |
| 405 | cmd.Env = append(cmd.Env, "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null") |
| 406 | if stdin != nil { |
| 407 | cmd.Stdin = bytes.NewReader(stdin) |
| 408 | } |
| 409 | var out, stderr bytes.Buffer |
| 410 | cmd.Stdout, cmd.Stderr = &out, &stderr |
| 411 | if err := Run(t, cmd, gitTimeout); err != nil { |
| 412 | t.Fatalf("git %s: %v: %s", strings.Join(args, " "), err, stderr.String()) |
| 413 | } |
| 414 | return out.Bytes() |
| 415 | } |