| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "unicode/utf8" |
| 13 | |
| 14 | "the-valley/change" |
| 15 | ) |
| 16 | |
| 17 | // A project's URL keeps the instance's transport, user and host, and |
| 18 | // replaces the last path segment alone. |
| 19 | func TestProjectURL(t *testing.T) { |
| 20 | for _, c := range []struct{ instance, want string }{ |
| 21 | {"git@valley-host:valley.git", "git@valley-host:app.git"}, |
| 22 | {"git@valley-host:valley", "git@valley-host:app"}, |
| 23 | {"valley-host:srv/git/valley.git", "valley-host:srv/git/app.git"}, |
| 24 | {"git@valley-host:/srv/git/valley.git/", "git@valley-host:/srv/git/app.git"}, |
| 25 | {"ssh://git@valley-host/valley.git", "ssh://git@valley-host/app.git"}, |
| 26 | {"ssh://git@valley-host:2222/srv/git/valley.git", "ssh://git@valley-host:2222/srv/git/app.git"}, |
| 27 | {"ssh://git@[fe80::1]:22/valley.git", "ssh://git@[fe80::1]:22/app.git"}, |
| 28 | {"https://git.example/valley/valley", "https://git.example/valley/app"}, |
| 29 | {"file:///srv/git/valley.git", "file:///srv/git/app.git"}, |
| 30 | {"/srv/git/valley.git", "/srv/git/app.git"}, |
| 31 | {"./a:b/valley.git", "./a:b/app.git"}, |
| 32 | } { |
| 33 | got, err := projectURL(c.instance, "app") |
| 34 | if err != nil || got != c.want { |
| 35 | t.Errorf("projectURL(%q) = %q, %v; want %q", c.instance, got, err, c.want) |
| 36 | } |
| 37 | } |
| 38 | for _, bad := range []string{"ssh://valley-host", "ssh://valley-host/", "git@valley-host:", "git@valley-host:.git"} { |
| 39 | if got, err := projectURL(bad, "app"); err == nil { |
| 40 | t.Errorf("projectURL(%q) = %q, where it has no path to replace", bad, got) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestHostOf(t *testing.T) { |
| 46 | for url, want := range map[string]string{ |
| 47 | "git@valley-host:app.git": "valley-host", |
| 48 | "ssh://git@valley-host:2222/app.git": "valley-host:2222", |
| 49 | "https://user@git.example/valley/app.git": "git.example", |
| 50 | "/srv/git/app.git": "localhost", |
| 51 | "file:///srv/git/app.git": "localhost", |
| 52 | } { |
| 53 | if got := hostOf(url); got != want { |
| 54 | t.Errorf("hostOf(%q) = %q, want %q", url, got, want) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // pointerURL reads the owning valley's address out of policy/valley. |
| 60 | func TestPointerURL(t *testing.T) { |
| 61 | got := pointerURL("# the owning valley\n\n git@valley-host:valley.git # floor\nother\n") |
| 62 | if got != "git@valley-host:valley.git" { |
| 63 | t.Errorf("pointerURL = %q", got) |
| 64 | } |
| 65 | if got := pointerURL("# nothing\n\n"); got != "" { |
| 66 | t.Errorf("pointerURL of comments alone = %q", got) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Every handle among changes listed from several repositories is unique, |
| 71 | // and minimal: one letter shorter, down to four, and another change |
| 72 | // listed shares it. |
| 73 | func TestHandlesUniqueAndMinimal(t *testing.T) { |
| 74 | app := []string{ |
| 75 | "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", |
| 76 | "nxvpwlzzqtsrzyoukklnpmqrstuvwxyz", |
| 77 | "xunxkkkkkkkkkkkkkkkkkkkkkkkkkkkk", |
| 78 | } |
| 79 | valley := []string{ |
| 80 | "nxvpwlkmqtsrzyoukklnpmqrstuvwxyk", // shares 31 letters with app's first |
| 81 | "xunykkkkkkkkkkkkkkkkkkkkkkkkkkkk", |
| 82 | "qqqqkkkkkkkkkkkkkkkkkkkkkkkkkkkk", |
| 83 | } |
| 84 | repos := []listed{ |
| 85 | {source: source{name: "app"}, snap: &snapshot{all: recordsOf(app)}}, |
| 86 | {source: source{name: "valley"}, snap: &snapshot{all: recordsOf(valley)}}, |
| 87 | {source: source{name: "ghost", err: errUnread}}, |
| 88 | } |
| 89 | got := handles(repos) |
| 90 | h := map[string]string{} |
| 91 | for _, id := range app { |
| 92 | h[id] = got[handleKey{"app", "main", id}] |
| 93 | } |
| 94 | for _, id := range valley { |
| 95 | h[id] = got[handleKey{"valley", "main", id}] |
| 96 | } |
| 97 | all := append(append([]string(nil), app...), valley...) |
| 98 | seen := map[string]string{} |
| 99 | for _, id := range all { |
| 100 | handle := h[id] |
| 101 | if !strings.HasPrefix(id, handle) || len(handle) < 4 { |
| 102 | t.Fatalf("%s has the handle %q", id, handle) |
| 103 | } |
| 104 | if other, ok := seen[handle]; ok { |
| 105 | t.Fatalf("%s and %s share the handle %s", id, other, handle) |
| 106 | } |
| 107 | seen[handle] = id |
| 108 | for _, other := range all { |
| 109 | if other != id && strings.HasPrefix(other, handle) { |
| 110 | t.Fatalf("the handle %s of %s is a prefix of %s too", handle, id, other) |
| 111 | } |
| 112 | } |
| 113 | if shorter := handle[:len(handle)-1]; len(shorter) >= 4 { |
| 114 | shared := false |
| 115 | for _, other := range all { |
| 116 | shared = shared || other != id && strings.HasPrefix(other, shorter) |
| 117 | } |
| 118 | if !shared { |
| 119 | t.Errorf("the handle %s of %s is not minimal: %s names it alone", handle, id, shorter) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | if h["xunxkkkkkkkkkkkkkkkkkkkkkkkkkkkk"] != "xunx" || h["qqqqkkkkkkkkkkkkkkkkkkkkkkkkkkkk"] != "qqqq" { |
| 124 | t.Errorf("four-letter handles grew: %v", h) |
| 125 | } |
| 126 | if got := h["nxvpwlkmqtsrzyoukklnpmqrstuvwxyz"]; len(got) != 32 { |
| 127 | t.Errorf("an ID sharing 31 letters with another listed elsewhere has the handle %s", got) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // One change ID recorded in two repositories has a handle qualified by |
| 132 | // its repository in each, and the other changes keep theirs. |
| 133 | func TestHandlesQualifiedAcrossRepos(t *testing.T) { |
| 134 | shared := "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz" |
| 135 | repos := []listed{ |
| 136 | {source: source{name: "app"}, snap: &snapshot{all: recordsOf([]string{shared, "xunxkkkkkkkkkkkkkkkkkkkkkkkkkkkk"})}}, |
| 137 | {source: source{name: "origin"}, snap: &snapshot{all: recordsOf([]string{shared})}}, |
| 138 | } |
| 139 | h := handles(repos) |
| 140 | want := handleMap{ |
| 141 | {"app", "main", shared}: "app:nxvp", |
| 142 | {"origin", "main", shared}: "origin:nxvp", |
| 143 | {"app", "main", "xunxkkkkkkkkkkkkkkkkkkkkkkkkkkkk"}: "xunx", |
| 144 | } |
| 145 | if len(h) != len(want) { |
| 146 | t.Fatalf("handles = %v, want %v", h, want) |
| 147 | } |
| 148 | for k, v := range want { |
| 149 | if h[k] != v { |
| 150 | t.Errorf("the handle of %v is %q, want %q", k, h[k], v) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // A URL from data that git would read as an option, or that names a |
| 156 | // remote helper or another scheme, is refused. ssh, git, https, file, |
| 157 | // scp-like and local URLs are read. |
| 158 | func TestCheckURL(t *testing.T) { |
| 159 | for _, ok := range []string{ |
| 160 | "git@valley-host:valley.git", "valley-host:valley", "ssh://git@valley-host:22/valley.git", |
| 161 | "ssh://git@[::1]/valley.git", "git://valley-host/valley.git", "https://git.example/valley", |
| 162 | "file:///srv/git/valley.git", "/srv/git/valley.git", "./valley.git", |
| 163 | } { |
| 164 | if err := checkURL(ok); err != nil { |
| 165 | t.Errorf("checkURL(%q) = %v", ok, err) |
| 166 | } |
| 167 | } |
| 168 | for _, bad := range []string{ |
| 169 | "", "--upload-pack=touch /tmp/pwned; git-upload-pack", "-oProxyCommand=sh:x", |
| 170 | "ext::sh -c touch% /tmp/pwned", "fd::0", "remote-x::address", "http://git.example/valley", |
| 171 | "ftp://git.example/valley", "ssh://-oProxyCommand=sh/valley", "git@host:q\nvalley", "git@host:q\x1bvalley", |
| 172 | } { |
| 173 | if err := checkURL(bad); err == nil { |
| 174 | t.Errorf("checkURL(%q) let it through", bad) |
| 175 | } |
| 176 | } |
| 177 | if url, err := projectURL("ext::sh -c x/valley", "app"); err == nil { |
| 178 | t.Errorf("projectURL derived %q from a remote helper's URL", url) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | var errUnread = failure("could not read") |
| 183 | |
| 184 | func recordsOf(ids []string) []recorded { |
| 185 | var all []recorded |
| 186 | for _, id := range ids { |
| 187 | all = append(all, recorded{id: id, record: &change.Record{Target: "main"}}) |
| 188 | } |
| 189 | return all |
| 190 | } |
| 191 | |
| 192 | // listingWith is a valley-changes/v1 document holding one change whose one |
| 193 | // version has the title and branch given. |
| 194 | func listingWith(title, branch string) jsonChanges { |
| 195 | tip := strings.Repeat("b", 40) |
| 196 | return jsonChanges{Schema: changesSchema, Host: "valley-host", Repos: []jsonRepo{{ |
| 197 | Repo: "app", URL: "git@valley-host:app.git", |
| 198 | Changes: []jsonChange{{ |
| 199 | ChangeID: "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", Handle: "nxvp", Target: "main", |
| 200 | Owner: "alice", State: "ready", Record: strings.Repeat("c", 40), Seq: 1, Active: 1, |
| 201 | Versions: []jsonVersion{{ |
| 202 | N: 1, Head: strings.Repeat("a", 40), Title: title, Branch: branch, Uploader: "alice", |
| 203 | Landing: notLanded, Outcome: &versionOutcome{Decision: "failed", Head: strings.Repeat("a", 40), Tip: &tip}, |
| 204 | }}, |
| 205 | Includes: []string{}, |
| 206 | }}, |
| 207 | Drafts: &[]jsonChange{{ChangeID: "qqqqkkkkkkkkkkkkkkkkkkkkkkkkkkkk", Handle: "qqqq", Target: "main", Owner: "alice", State: "draft", Record: tip, Seq: 2, Active: 1, Versions: []jsonVersion{{N: 1, Head: tip, Title: title, Branch: branch, Uploader: "alice", Landing: notLanded}}, Includes: []string{}}}, |
| 208 | Branches: []jsonBranch{{Branch: branch, Head: tip}}, |
| 209 | }}} |
| 210 | } |
| 211 | |
| 212 | // The writer puts out one line, with no control character in it, that |
| 213 | // reads back as exactly the document written. A string that is not UTF-8 |
| 214 | // is refused rather than written as another one. |
| 215 | func FuzzWriteDocument(f *testing.F) { |
| 216 | for _, seed := range [][2]string{ |
| 217 | {"fix the frobnicator", "topic"}, |
| 218 | {"tab\there, newline\nthere, nul\x00, del\x7f, esc\x1b[31m", "a/b"}, |
| 219 | {"non-ASCII: café, 漢字, emoji 🎉, U+2028
and U+2029
", "zweig/ä"}, |
| 220 | {"quotes \" and backslash \\ and <html> & ampersand", "x"}, |
| 221 | {"� replacement already there", "y"}, |
| 222 | {"not UTF-8: \xff\xfe", "z"}, |
| 223 | {"title", "branch\xc3"}, |
| 224 | } { |
| 225 | f.Add(seed[0], seed[1]) |
| 226 | } |
| 227 | f.Fuzz(func(t *testing.T, title, branch string) { |
| 228 | doc := listingWith(title, branch) |
| 229 | var buf bytes.Buffer |
| 230 | err := writeDocument(&buf, doc) |
| 231 | if !utf8.ValidString(title) || !utf8.ValidString(branch) { |
| 232 | if err == nil { |
| 233 | t.Fatalf("a string that is not UTF-8 was written: %q", buf.String()) |
| 234 | } |
| 235 | return |
| 236 | } |
| 237 | if err != nil { |
| 238 | t.Fatalf("writeDocument: %v", err) |
| 239 | } |
| 240 | out := buf.Bytes() |
| 241 | if len(out) == 0 || out[len(out)-1] != '\n' || bytes.Count(out, []byte("\n")) != 1 { |
| 242 | t.Fatalf("not one line: %q", out) |
| 243 | } |
| 244 | for _, c := range out[:len(out)-1] { |
| 245 | if c < 0x20 { |
| 246 | t.Fatalf("a raw control character %q in %q", c, out) |
| 247 | } |
| 248 | } |
| 249 | var back jsonChanges |
| 250 | dec := json.NewDecoder(bytes.NewReader(out)) |
| 251 | dec.DisallowUnknownFields() |
| 252 | if err := dec.Decode(&back); err != nil { |
| 253 | t.Fatalf("does not read back: %v: %q", err, out) |
| 254 | } |
| 255 | v := back.Repos[0].Changes[0].Versions[0] |
| 256 | d := (*back.Repos[0].Drafts)[0].Versions[0] |
| 257 | if v.Title != title || v.Branch != branch || back.Repos[0].Branches[0].Branch != branch || d.Title != title || d.Branch != branch { |
| 258 | t.Fatalf("read back as %q, %q, where %q, %q was written", v.Title, v.Branch, title, branch) |
| 259 | } |
| 260 | if back.Schema != changesSchema || v.LandedCommit != nil || v.Request != nil || *v.Outcome.Tip != strings.Repeat("b", 40) { |
| 261 | t.Fatalf("the fields around the strings moved: %q", out) |
| 262 | } |
| 263 | }) |
| 264 | } |
| 265 | |
| 266 | // Each change in valley changes and valley pending says which version it |
| 267 | // landed as, and the commit on main it landed as, by the verdict valley |
| 268 | // status gives and valley pending leaves landed changes out by: a change |
| 269 | // landed at its active version; one whose first version landed while a |
| 270 | // later version beside it was active, which landed as the first; and one |
| 271 | // whose next round was made on the main that holds its landing, which has |
| 272 | // not. Each lands by fast-forward, its head on main. |
| 273 | func TestListingsSayWhichVersionAChangeLandedAs(t *testing.T) { |
| 274 | dir := t.TempDir() |
| 275 | t.Setenv("HOME", dir) |
| 276 | t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config")) |
| 277 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 278 | t.Setenv("VALLEY_KNOWN_SIGNERS", "") |
| 279 | origin, work := filepath.Join(dir, "origin.git"), filepath.Join(dir, "work") |
| 280 | git := func(args ...string) string { |
| 281 | t.Helper() |
| 282 | cmd := exec.Command("git", args...) |
| 283 | cmd.Dir = work |
| 284 | out, err := cmd.CombinedOutput() |
| 285 | if err != nil { |
| 286 | t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 287 | } |
| 288 | return strings.TrimSpace(string(out)) |
| 289 | } |
| 290 | if out, err := exec.Command("git", "init", "--quiet", "--bare", "--initial-branch=main", origin).CombinedOutput(); err != nil { |
| 291 | t.Fatalf("git init: %v\n%s", err, out) |
| 292 | } |
| 293 | if out, err := exec.Command("git", "clone", "--quiet", origin, work).CombinedOutput(); err != nil { |
| 294 | t.Fatalf("git clone: %v\n%s", err, out) |
| 295 | } |
| 296 | git("config", "user.name", "landed-test") |
| 297 | git("config", "user.email", "landed-test@localhost") |
| 298 | git("config", "valley.principal", "alice") |
| 299 | git("checkout", "--quiet", "-b", "main") |
| 300 | git("commit", "--quiet", "--allow-empty", "-m", "base") |
| 301 | git("push", "--quiet", "origin", "main") |
| 302 | commits := 0 |
| 303 | branch := func(name, from string) string { |
| 304 | t.Helper() |
| 305 | commits++ |
| 306 | git("checkout", "--quiet", "-B", name, from) |
| 307 | git("commit", "--quiet", "--allow-empty", "-m", fmt.Sprintf("%s, commit %d", name, commits)) |
| 308 | git("push", "--quiet", "--force", "origin", name) |
| 309 | return git("rev-parse", "HEAD") |
| 310 | } |
| 311 | |
| 312 | t.Chdir(work) |
| 313 | valley := func(args ...string) []byte { |
| 314 | t.Helper() |
| 315 | var stdout, stderr bytes.Buffer |
| 316 | if code := run(args, &stdout, &stderr, os.Exit); code != 0 { |
| 317 | t.Fatalf("valley %s: %s", strings.Join(args, " "), stderr.String()) |
| 318 | } |
| 319 | return stdout.Bytes() |
| 320 | } |
| 321 | active, below, next := strings.Repeat("k", 32), strings.Repeat("m", 32), strings.Repeat("n", 32) |
| 322 | activeHead := branch("active", "main") |
| 323 | valley("ready", "--change", active, "active") |
| 324 | belowV1 := branch("below", "active") |
| 325 | valley("ready", "--change", below, "below") |
| 326 | nextV1 := branch("next", "below") |
| 327 | valley("ready", "--change", next, "next") |
| 328 | branch("below", "active") |
| 329 | valley("ready", "--change", below, "below") |
| 330 | git("push", "--quiet", "origin", nextV1+":refs/heads/main") |
| 331 | branch("next", nextV1) |
| 332 | valley("ready", "--change", next, "next") |
| 333 | |
| 334 | verdict := func(v *int, commit *string) string { |
| 335 | if v == nil || commit == nil { |
| 336 | return fmt.Sprintf("%v %v", v, commit) |
| 337 | } |
| 338 | return fmt.Sprintf("v%d %s", *v, *commit) |
| 339 | } |
| 340 | listing := func(args ...string) map[string]string { |
| 341 | t.Helper() |
| 342 | var doc struct { |
| 343 | Repos []struct { |
| 344 | Changes []struct { |
| 345 | ID string `json:"change_id"` |
| 346 | Version *int `json:"landed_version"` |
| 347 | Commit *string `json:"landed_commit"` |
| 348 | } `json:"changes"` |
| 349 | } `json:"repos"` |
| 350 | } |
| 351 | if err := json.Unmarshal(valley(args...), &doc); err != nil || len(doc.Repos) != 1 { |
| 352 | t.Fatalf("valley %s: %v, %d repos", strings.Join(args, " "), err, len(doc.Repos)) |
| 353 | } |
| 354 | got := map[string]string{} |
| 355 | for _, c := range doc.Repos[0].Changes { |
| 356 | got[c.ID] = verdict(c.Version, c.Commit) |
| 357 | } |
| 358 | return got |
| 359 | } |
| 360 | want := map[string]string{active: "v1 " + activeHead, below: "v1 " + belowV1, next: "<nil> <nil>"} |
| 361 | changes := listing("changes", "--json") |
| 362 | for id, w := range want { |
| 363 | if changes[id] != w { |
| 364 | t.Errorf("valley changes --json: %.4s landed as %q, want %q", id, changes[id], w) |
| 365 | } |
| 366 | var status struct { |
| 367 | Version *int `json:"landed_version"` |
| 368 | } |
| 369 | if err := json.Unmarshal(valley("status", "--json", id), &status); err != nil { |
| 370 | t.Fatalf("valley status --json %s: %v", id, err) |
| 371 | } |
| 372 | said := "<nil>" |
| 373 | if status.Version != nil { |
| 374 | said = fmt.Sprintf("v%d", *status.Version) |
| 375 | } |
| 376 | if said != strings.Fields(w)[0] { |
| 377 | t.Errorf("%.4s: valley status --json says landed_version %s, the listing %q", id, said, w) |
| 378 | } |
| 379 | } |
| 380 | if pending := listing("pending", "--json"); len(pending) != 1 || pending[next] != want[next] { |
| 381 | t.Errorf("valley pending --json lists %v, want only %.4s, unlanded", pending, next) |
| 382 | } |
| 383 | } |