| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "unicode/utf8" |
| 9 | ) |
| 10 | |
| 11 | // A project's URL keeps the instance's transport, user and host, and |
| 12 | // replaces the last path segment alone. |
| 13 | func TestProjectURL(t *testing.T) { |
| 14 | for _, c := range []struct{ instance, want string }{ |
| 15 | {"git@classic-laddie:qinling.git", "git@classic-laddie:cosmo.git"}, |
| 16 | {"git@classic-laddie:qinling", "git@classic-laddie:cosmo"}, |
| 17 | {"classic-laddie:srv/git/qinling.git", "classic-laddie:srv/git/cosmo.git"}, |
| 18 | {"git@classic-laddie:/srv/git/qinling.git/", "git@classic-laddie:/srv/git/cosmo.git"}, |
| 19 | {"ssh://git@classic-laddie/qinling.git", "ssh://git@classic-laddie/cosmo.git"}, |
| 20 | {"ssh://git@classic-laddie:2222/srv/git/qinling.git", "ssh://git@classic-laddie:2222/srv/git/cosmo.git"}, |
| 21 | {"ssh://git@[fe80::1]:22/qinling.git", "ssh://git@[fe80::1]:22/cosmo.git"}, |
| 22 | {"https://git.example/valley/qinling", "https://git.example/valley/cosmo"}, |
| 23 | {"file:///srv/git/qinling.git", "file:///srv/git/cosmo.git"}, |
| 24 | {"/srv/git/qinling.git", "/srv/git/cosmo.git"}, |
| 25 | {"./a:b/qinling.git", "./a:b/cosmo.git"}, |
| 26 | } { |
| 27 | got, err := projectURL(c.instance, "cosmo") |
| 28 | if err != nil || got != c.want { |
| 29 | t.Errorf("projectURL(%q) = %q, %v; want %q", c.instance, got, err, c.want) |
| 30 | } |
| 31 | } |
| 32 | for _, bad := range []string{"ssh://classic-laddie", "ssh://classic-laddie/", "git@classic-laddie:", "git@classic-laddie:.git"} { |
| 33 | if got, err := projectURL(bad, "cosmo"); err == nil { |
| 34 | t.Errorf("projectURL(%q) = %q, where it has no path to replace", bad, got) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestHostOf(t *testing.T) { |
| 40 | for url, want := range map[string]string{ |
| 41 | "git@classic-laddie:cosmo.git": "classic-laddie", |
| 42 | "ssh://git@classic-laddie:2222/cosmo.git": "classic-laddie:2222", |
| 43 | "https://user@git.example/valley/cosmo.git": "git.example", |
| 44 | "/srv/git/cosmo.git": "localhost", |
| 45 | "file:///srv/git/cosmo.git": "localhost", |
| 46 | } { |
| 47 | if got := hostOf(url); got != want { |
| 48 | t.Errorf("hostOf(%q) = %q, want %q", url, got, want) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // pointerURL reads policy/valley as bin/valley's pointer_url does. |
| 54 | func TestPointerURL(t *testing.T) { |
| 55 | got := pointerURL("# the owning valley\n\n git@classic-laddie:qinling.git # floor\nother\n") |
| 56 | if got != "git@classic-laddie:qinling.git" { |
| 57 | t.Errorf("pointerURL = %q", got) |
| 58 | } |
| 59 | if got := pointerURL("# nothing\n\n"); got != "" { |
| 60 | t.Errorf("pointerURL of comments alone = %q", got) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Every handle among changes listed from several repositories is unique, |
| 65 | // and minimal: one letter shorter, down to four, and another change |
| 66 | // listed shares it. |
| 67 | func TestHandlesUniqueAndMinimal(t *testing.T) { |
| 68 | cosmo := []string{ |
| 69 | "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", |
| 70 | "nxvpwlzzqtsrzyoukklnpmqrstuvwxyz", |
| 71 | "xunxkkkkkkkkkkkkkkkkkkkkkkkkkkkk", |
| 72 | } |
| 73 | valley := []string{ |
| 74 | "nxvpwlkmqtsrzyoukklnpmqrstuvwxyk", // shares 31 letters with cosmo's first |
| 75 | "xunykkkkkkkkkkkkkkkkkkkkkkkkkkkk", |
| 76 | "qqqqkkkkkkkkkkkkkkkkkkkkkkkkkkkk", |
| 77 | } |
| 78 | repos := []listed{ |
| 79 | {snap: &snapshot{all: recordsOf(cosmo)}}, |
| 80 | {snap: &snapshot{all: recordsOf(valley)}}, |
| 81 | {source: source{err: errUnread}}, |
| 82 | } |
| 83 | h := handles(repos) |
| 84 | all := append(append([]string(nil), cosmo...), valley...) |
| 85 | seen := map[string]string{} |
| 86 | for _, id := range all { |
| 87 | handle := h[id] |
| 88 | if !strings.HasPrefix(id, handle) || len(handle) < 4 { |
| 89 | t.Fatalf("%s has the handle %q", id, handle) |
| 90 | } |
| 91 | if other, ok := seen[handle]; ok { |
| 92 | t.Fatalf("%s and %s share the handle %s", id, other, handle) |
| 93 | } |
| 94 | seen[handle] = id |
| 95 | for _, other := range all { |
| 96 | if other != id && strings.HasPrefix(other, handle) { |
| 97 | t.Fatalf("the handle %s of %s is a prefix of %s too", handle, id, other) |
| 98 | } |
| 99 | } |
| 100 | if shorter := handle[:len(handle)-1]; len(shorter) >= 4 { |
| 101 | shared := false |
| 102 | for _, other := range all { |
| 103 | shared = shared || other != id && strings.HasPrefix(other, shorter) |
| 104 | } |
| 105 | if !shared { |
| 106 | t.Errorf("the handle %s of %s is not minimal: %s names it alone", handle, id, shorter) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | if h["xunxkkkkkkkkkkkkkkkkkkkkkkkkkkkk"] != "xunx" || h["qqqqkkkkkkkkkkkkkkkkkkkkkkkkkkkk"] != "qqqq" { |
| 111 | t.Errorf("four-letter handles grew: %v", h) |
| 112 | } |
| 113 | if got := h["nxvpwlkmqtsrzyoukklnpmqrstuvwxyz"]; len(got) != 32 { |
| 114 | t.Errorf("an ID sharing 31 letters with another listed elsewhere has the handle %s", got) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | var errUnread = failure("could not read") |
| 119 | |
| 120 | func recordsOf(ids []string) []recorded { |
| 121 | var all []recorded |
| 122 | for _, id := range ids { |
| 123 | all = append(all, recorded{id: id}) |
| 124 | } |
| 125 | return all |
| 126 | } |
| 127 | |
| 128 | // listingWith is a valley-changes/v1 document holding one change whose one |
| 129 | // version has the title and branch given. |
| 130 | func listingWith(title, branch string) jsonChanges { |
| 131 | tip := strings.Repeat("b", 40) |
| 132 | return jsonChanges{Schema: changesSchema, Host: "classic-laddie", Repos: []jsonRepo{{ |
| 133 | Repo: "cosmo", URL: "git@classic-laddie:cosmo.git", |
| 134 | Changes: []jsonChange{{ |
| 135 | ChangeID: "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", Handle: "nxvp", Target: "main", |
| 136 | Owner: "klaus-env", State: "ready", Record: strings.Repeat("c", 40), Seq: 1, Active: 1, |
| 137 | Versions: []jsonVersion{{ |
| 138 | N: 1, Head: strings.Repeat("a", 40), Title: title, Branch: branch, Uploader: "klaus-env", |
| 139 | Landing: notLanded, Outcome: &versionOutcome{Decision: "failed", Head: strings.Repeat("a", 40), Tip: &tip}, |
| 140 | }}, |
| 141 | Includes: []string{}, |
| 142 | }}, |
| 143 | Branches: []jsonBranch{{Branch: branch, Head: tip}}, |
| 144 | }}} |
| 145 | } |
| 146 | |
| 147 | // The writer puts out one line, with no control character in it, that |
| 148 | // reads back as exactly the document written. A string that is not UTF-8 |
| 149 | // is refused rather than written as another one. |
| 150 | func FuzzWriteDocument(f *testing.F) { |
| 151 | for _, seed := range [][2]string{ |
| 152 | {"fix the frobnicator", "topic"}, |
| 153 | {"tab\there, newline\nthere, nul\x00, del\x7f, esc\x1b[31m", "a/b"}, |
| 154 | {"non-ASCII: café, 漢字, emoji 🎉, U+2028
and U+2029
", "zweig/ä"}, |
| 155 | {"quotes \" and backslash \\ and <html> & ampersand", "x"}, |
| 156 | {"� replacement already there", "y"}, |
| 157 | {"not UTF-8: \xff\xfe", "z"}, |
| 158 | {"title", "branch\xc3"}, |
| 159 | } { |
| 160 | f.Add(seed[0], seed[1]) |
| 161 | } |
| 162 | f.Fuzz(func(t *testing.T, title, branch string) { |
| 163 | doc := listingWith(title, branch) |
| 164 | var buf bytes.Buffer |
| 165 | err := writeDocument(&buf, doc) |
| 166 | if !utf8.ValidString(title) || !utf8.ValidString(branch) { |
| 167 | if err == nil { |
| 168 | t.Fatalf("a string that is not UTF-8 was written: %q", buf.String()) |
| 169 | } |
| 170 | return |
| 171 | } |
| 172 | if err != nil { |
| 173 | t.Fatalf("writeDocument: %v", err) |
| 174 | } |
| 175 | out := buf.Bytes() |
| 176 | if len(out) == 0 || out[len(out)-1] != '\n' || bytes.Count(out, []byte("\n")) != 1 { |
| 177 | t.Fatalf("not one line: %q", out) |
| 178 | } |
| 179 | for _, c := range out[:len(out)-1] { |
| 180 | if c < 0x20 { |
| 181 | t.Fatalf("a raw control character %q in %q", c, out) |
| 182 | } |
| 183 | } |
| 184 | var back jsonChanges |
| 185 | dec := json.NewDecoder(bytes.NewReader(out)) |
| 186 | dec.DisallowUnknownFields() |
| 187 | if err := dec.Decode(&back); err != nil { |
| 188 | t.Fatalf("does not read back: %v: %q", err, out) |
| 189 | } |
| 190 | v := back.Repos[0].Changes[0].Versions[0] |
| 191 | if v.Title != title || v.Branch != branch || back.Repos[0].Branches[0].Branch != branch { |
| 192 | t.Fatalf("read back as %q, %q, where %q, %q was written", v.Title, v.Branch, title, branch) |
| 193 | } |
| 194 | if back.Schema != changesSchema || v.LandedCommit != nil || v.Request != nil || *v.Outcome.Tip != strings.Repeat("b", 40) { |
| 195 | t.Fatalf("the fields around the strings moved: %q", out) |
| 196 | } |
| 197 | }) |
| 198 | } |