gunk-dev/the-valley

list_test.go

source262d8ca88ba9main's published tip history
9.7 KiB
1package main
2
3import (
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.
15func TestProjectURL(t *testing.T) {
16 for _, c := range []struct{ instance, want string }{
17 {"git@valley-host:valley.git", "git@valley-host:app.git"},
18 {"git@valley-host:valley", "git@valley-host:app"},
19 {"valley-host:srv/git/valley.git", "valley-host:srv/git/app.git"},
20 {"git@valley-host:/srv/git/valley.git/", "git@valley-host:/srv/git/app.git"},
21 {"ssh://git@valley-host/valley.git", "ssh://git@valley-host/app.git"},
22 {"ssh://git@valley-host:2222/srv/git/valley.git", "ssh://git@valley-host:2222/srv/git/app.git"},
23 {"ssh://git@[fe80::1]:22/valley.git", "ssh://git@[fe80::1]:22/app.git"},
24 {"https://git.example/valley/valley", "https://git.example/valley/app"},
25 {"file:///srv/git/valley.git", "file:///srv/git/app.git"},
26 {"/srv/git/valley.git", "/srv/git/app.git"},
27 {"./a:b/valley.git", "./a:b/app.git"},
28 } {
29 got, err := projectURL(c.instance, "app")
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://valley-host", "ssh://valley-host/", "git@valley-host:", "git@valley-host:.git"} {
35 if got, err := projectURL(bad, "app"); err == nil {
36 t.Errorf("projectURL(%q) = %q, where it has no path to replace", bad, got)
37 }
38 }
39}
40
41func TestHostOf(t *testing.T) {
42 for url, want := range map[string]string{
43 "git@valley-host:app.git": "valley-host",
44 "ssh://git@valley-host:2222/app.git": "valley-host:2222",
45 "https://user@git.example/valley/app.git": "git.example",
46 "/srv/git/app.git": "localhost",
47 "file:///srv/git/app.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.
56func TestPointerURL(t *testing.T) {
57 got := pointerURL("# the owning valley\n\n git@valley-host:valley.git # floor\nother\n")
58 if got != "git@valley-host:valley.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.
69func TestHandlesUniqueAndMinimal(t *testing.T) {
70 app := []string{
71 "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz",
72 "nxvpwlzzqtsrzyoukklnpmqrstuvwxyz",
73 "xunxkkkkkkkkkkkkkkkkkkkkkkkkkkkk",
74 }
75 valley := []string{
76 "nxvpwlkmqtsrzyoukklnpmqrstuvwxyk", // shares 31 letters with app's first
77 "xunykkkkkkkkkkkkkkkkkkkkkkkkkkkk",
78 "qqqqkkkkkkkkkkkkkkkkkkkkkkkkkkkk",
79 }
80 repos := []listed{
81 {source: source{name: "app"}, snap: &snapshot{all: recordsOf(app)}},
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 app {
88 h[id] = got[handleKey{"app", "main", id}]
89 }
90 for _, id := range valley {
91 h[id] = got[handleKey{"valley", "main", id}]
92 }
93 all := append(append([]string(nil), app...), 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.
129func TestHandlesQualifiedAcrossRepos(t *testing.T) {
130 shared := "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz"
131 repos := []listed{
132 {source: source{name: "app"}, snap: &snapshot{all: recordsOf([]string{shared, "xunxkkkkkkkkkkkkkkkkkkkkkkkkkkkk"})}},
133 {source: source{name: "origin"}, snap: &snapshot{all: recordsOf([]string{shared})}},
134 }
135 h := handles(repos)
136 want := handleMap{
137 {"app", "main", shared}: "app:nxvp",
138 {"origin", "main", shared}: "origin:nxvp",
139 {"app", "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.
154func TestCheckURL(t *testing.T) {
155 for _, ok := range []string{
156 "git@valley-host:valley.git", "valley-host:valley", "ssh://git@valley-host:22/valley.git",
157 "ssh://git@[::1]/valley.git", "git://valley-host/valley.git", "https://git.example/valley",
158 "file:///srv/git/valley.git", "/srv/git/valley.git", "./valley.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/valley",
167 "ftp://git.example/valley", "ssh://-oProxyCommand=sh/valley", "git@host:q\nvalley", "git@host:q\x1bvalley",
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/valley", "app"); err == nil {
174 t.Errorf("projectURL derived %q from a remote helper's URL", url)
175 }
176}
177
178var errUnread = failure("could not read")
179
180func 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.
190func listingWith(title, branch string) jsonChanges {
191 tip := strings.Repeat("b", 40)
192 return jsonChanges{Schema: changesSchema, Host: "valley-host", Repos: []jsonRepo{{
193 Repo: "app", URL: "git@valley-host:app.git",
194 Changes: []jsonChange{{
195 ChangeID: "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz", Handle: "nxvp", Target: "main",
196 Owner: "alice", 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: "alice",
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: "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{}}},
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.
211func 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}