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