gunk-dev/the-valley

record_test.go

sourcedc7708a21f33main's published tip history
12.2 KiB
1package change
2
3import (
4 "bytes"
5 "fmt"
6 "os/exec"
7 "reflect"
8 "strings"
9 "testing"
10)
11
12const (
13 id1 = "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz"
14 head1 = HeadPrefix + "1111111111111111111111111111111111111111"
15 head2 = HeadPrefix + "2222222222222222222222222222222222222222"
16)
17
18// record is a well-formed record with two versions, as text, so each case
19// below can change one thing about it.
20func record() string {
21 return `{
22 "schema": "valley-change-record/v1",
23 "change_id": "` + id1 + `",
24 "target": "main",
25 "owner": "klaus-env",
26 "state": "ready",
27 "seq": 2,
28 "versions": [
29 {"n": 1, "head": "` + head1 + `", "title": "the first", "branch": "topic", "uploader": "klaus-env"},
30 {"n": 2, "head": "` + head2 + `", "title": "the second", "branch": "", "uploader": "operator"}
31 ],
32 "active": 2
33}
34`
35}
36
37func TestAWellFormedRecordReadsWhole(t *testing.T) {
38 r, err := Parse([]byte(record()))
39 if err != nil {
40 t.Fatal(err)
41 }
42 want := &Record{
43 Schema: Schema, ChangeID: id1, Target: "main", Owner: "klaus-env", State: Ready, Seq: 2,
44 Versions: []Version{
45 {N: 1, Head: head1, Title: "the first", Branch: "topic", Uploader: "klaus-env"},
46 {N: 2, Head: head2, Title: "the second", Branch: "", Uploader: "operator"},
47 },
48 Active: 2,
49 }
50 if !reflect.DeepEqual(r, want) {
51 t.Errorf("Parse = %+v, want %+v", r, want)
52 }
53 encoded, err := Encode(r)
54 if err != nil {
55 t.Fatal(err)
56 }
57 again, err := Parse(encoded)
58 if err != nil || !reflect.DeepEqual(again, r) {
59 t.Errorf("an encoded record read back as %+v, %v", again, err)
60 }
61 for _, state := range []string{Draft, Withdrawn} {
62 r, err := Parse([]byte(strings.Replace(record(), `"state": "ready"`, `"state": "`+state+`"`, 1)))
63 if err != nil || r.State != state {
64 t.Errorf("a %s record read as %+v, %v", state, r, err)
65 }
66 }
67}
68
69// Each case changes one thing about a well-formed record, and each change
70// is one the reading has to refuse: two readers that disagree about a
71// record's contents could disagree about who owns it or what its versions
72// are.
73func TestTheReadingIsStrict(t *testing.T) {
74 r := record()
75 swap := func(from, to string) string {
76 if !strings.Contains(r, from) {
77 t.Fatalf("the record holds no %q to change", from)
78 }
79 return strings.Replace(r, from, to, 1)
80 }
81 for _, c := range []struct {
82 what, text, says string
83 }{
84 // Encoding.
85 {"bytes that are not UTF-8", swap("the first", "the \xff first"), "not valid UTF-8"},
86 {"an overlong encoding", swap("the first", "the \xc0\xaf first"), "not valid UTF-8"},
87 {"a lone surrogate escape", swap("the first", `the \ud800 first`), "surrogate"},
88 {"a low surrogate escape alone", swap("the first", `the \udc00 first`), "surrogate"},
89 {"an unknown escape", swap("the first", `the \x41 first`), "escape"},
90 {"a raw control character", swap("the first", "the \x01 first"), "control character"},
91
92 // Structure.
93 {"a duplicate top-level key", swap(`"seq": 2,`, `"seq": 2, "seq": 2,`), `"seq" twice`},
94 {"a duplicate key in a version", swap(`"n": 1,`, `"n": 1, "n": 1,`), `"n" twice`},
95 {"a duplicate key under another spelling", swap(`"seq": 2,`, `"seq": 2, "seq": 2,`), `"seq" twice`},
96 {"an unknown top-level field", swap(`"seq": 2,`, `"seq": 2, "extra": 1,`), `"extra"`},
97 {"an unknown version field", swap(`"n": 1,`, `"n": 1, "note": "x",`), `"note"`},
98 {"a missing field", swap(`"seq": 2,`, ``), `no field "seq"`},
99 {"a missing version field", swap(`"branch": "topic", `, ``), `no field "branch"`},
100 {"trailing data", r + "{}", "more after the value"},
101 {"a second value", r + "1", "more after the value"},
102 {"a trailing comma", swap(`"active": 2`, `"active": 2,`), "a key"},
103 {"no object at all", `[]`, "where an object is expected"},
104 {"a truncated record", r[:len(r)/2], "ends where"},
105 {"nesting past the bound", strings.Repeat("[", 20) + strings.Repeat("]", 20), "nests values"},
106
107 // Types.
108 {"seq as a string", swap(`"seq": 2`, `"seq": "2"`), "where an integer is expected"},
109 {"seq as a fraction", swap(`"seq": 2`, `"seq": 2.0`), "where an integer is expected"},
110 {"seq with an exponent", swap(`"seq": 2`, `"seq": 2e0`), "where an integer is expected"},
111 {"seq negative", swap(`"seq": 2`, `"seq": -2`), "where an integer is expected"},
112 {"active as null", swap(`"active": 2`, `"active": null`), "where an integer is expected"},
113 {"owner as a number", swap(`"owner": "klaus-env"`, `"owner": 7`), "where a string is expected"},
114 {"versions as an object", `{"schema": "valley-change-record/v1", "change_id": "` + id1 +
115 `", "target": "main", "owner": "o", "state": "ready", "seq": 1, "versions": {}, "active": 1}`,
116 "where an array is expected"},
117
118 // Ranges.
119 {"seq zero", swap(`"seq": 2`, `"seq": 0`), "outside 1 to 100000"},
120 {"seq past its bound", swap(`"seq": 2`, `"seq": 100001`), "outside 1 to 100000"},
121 {"seq past any integer", swap(`"seq": 2`, `"seq": 99999999999999999999999`), "outside 1 to 100000"},
122 {"active past its bound", swap(`"active": 2`, `"active": 1001`), "outside 1 to 1000"},
123 {"active naming no version", swap(`"active": 2`, `"active": 3`), "names none of its 2 versions"},
124 {"n zero", swap(`"n": 1`, `"n": 0`), "outside 1 to 1000"},
125
126 // Order.
127 {"a skipped n", swap(`"n": 2`, `"n": 3`), "no gap"},
128 {"versions out of order", swap(`"n": 1`, `"n": 2`), "no gap"},
129 {"no version", `{"schema": "valley-change-record/v1", "change_id": "` + id1 +
130 `", "target": "main", "owner": "o", "state": "ready", "seq": 1, "versions": [], "active": 1}`,
131 "holds no version"},
132
133 // Formats.
134 {"another schema", swap(`change-record/v1`, `change-record/v2`), "its schema"},
135 {"a short change ID", swap(id1, id1[:31]), "not a change ID"},
136 {"a change ID outside k to z", swap(id1, "a"+id1[1:]), "not a change ID"},
137 {"another state", swap(`"state": "ready"`, `"state": "merged"`), "its state"},
138 {"an uppercase head", swap(head1, strings.ToUpper(head1)), "not git-sha1:"},
139 {"a SHA-256 head", swap(head1, "git-sha256:"+strings.Repeat("1", 64)), "not git-sha1:"},
140 {"an empty title", swap(`"the first"`, `""`), "title"},
141 {"a title past 200 bytes", swap(`"the first"`, `"`+strings.Repeat("é", 101)+`"`), "title"},
142 {"an escaped control character in a title", swap(`"the first"`, `"the\nfirst"`), "title"},
143 {"a C1 control character in a title", swap(`"the first"`, `"the`+"\u0085"+`first"`), "title"},
144 {"a branch git would refuse", swap(`"branch": "topic"`, `"branch": "to..pic"`), "not a branch name"},
145 {"an owner that is no principal", swap(`"owner": "klaus-env"`, `"owner": "-klaus"`), "not a principal name"},
146 {"an empty uploader", swap(`"uploader": "operator"`, `"uploader": ""`), "not a principal name"},
147 {"a target that is no branch", swap(`"target": "main"`, `"target": "ma in"`), "its target"},
148 } {
149 _, err := Parse([]byte(c.text))
150 if err == nil || !strings.Contains(err.Error(), c.says) {
151 t.Errorf("%s: error = %v, want one saying %q", c.what, err, c.says)
152 }
153 }
154
155 // A title of exactly 200 bytes, and white space anywhere JSON allows
156 // it, are fine.
157 if _, err := Parse([]byte(swap(`"the first"`, `"`+strings.Repeat("é", 100)+`"`))); err != nil {
158 t.Errorf("a 200-byte title: %v", err)
159 }
160 if _, err := Parse([]byte("\r\n\t " + strings.ReplaceAll(r, ": ", " :\t") + "\n\n")); err != nil {
161 t.Errorf("white space: %v", err)
162 }
163}
164
165func TestATitleIsMadeFromAnySubject(t *testing.T) {
166 for subject, want := range map[string]string{
167 "fix the hook": "fix the hook",
168 "tab\there": "tab here",
169 "bad\xffbyte": "bad�byte",
170 "": "(no subject)",
171 "\x01\x02": "(no subject)",
172 strings.Repeat("é", 150): strings.Repeat("é", 100),
173 strings.Repeat("a", 199) + "é": strings.Repeat("a", 199),
174 } {
175 got := Title(subject)
176 if got != want || !ValidTitle(got) {
177 t.Errorf("Title(%q) = %q, want %q", subject, got, want)
178 }
179 }
180}
181
182func TestAHandleIsTheShortestUniquePrefix(t *testing.T) {
183 ids := []string{
184 "nxvpwlkmqtsrzyoukklnpmqrstuvwxyz",
185 "nxvpzzzzqtsrzyoukklnpmqrstuvwxyz",
186 "nxvpzzzyqtsrzyoukklnpmqrstuvwxyz",
187 "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk",
188 }
189 for id, want := range map[string]string{
190 ids[0]: "nxvpw",
191 ids[1]: "nxvpzzzz",
192 ids[2]: "nxvpzzzy",
193 ids[3]: "kkkk",
194 } {
195 if got := Handle(id, ids); got != want {
196 t.Errorf("Handle(%s) = %s, want %s", id, got, want)
197 }
198 if got := Matching(want, ids); len(got) != 1 || got[0] != id {
199 t.Errorf("Matching(%s) = %v, want %s alone", want, got, id)
200 }
201 }
202 if got := Matching("nxvp", ids); len(got) != 3 {
203 t.Errorf("Matching(nxvp) = %v, want three", got)
204 }
205 for _, name := range []string{"nxv", "topic", "nxvp/x", ids[0] + "k"} {
206 if got := Matching(name, ids); got != nil {
207 t.Errorf("Matching(%q) = %v, want nothing", name, got)
208 }
209 }
210}
211
212func TestANewIDIsInJJsForm(t *testing.T) {
213 seen := map[string]bool{}
214 for i := 0; i < 100; i++ {
215 id, err := NewID()
216 if err != nil || !ValidID(id) || seen[id] {
217 t.Fatalf("NewID = %q, %v", id, err)
218 }
219 seen[id] = true
220 }
221}
222
223// ValidBranch stands in for `git check-ref-format refs/heads/<name>`, and a
224// ref name check is wrong exactly at git's edge cases, so it is held to git
225// over each of them.
226func TestValidBranchAgreesWithGit(t *testing.T) {
227 git, err := exec.LookPath("git")
228 if err != nil {
229 t.Skip("no git to compare with")
230 }
231 names := []string{
232 "main", "topic/one", "a/b/c", "-dash", "@", "a@b", "a@{b", "@{", "a.b", ".a", "a/.b",
233 "a.", "a/b.", "a..b", "a.lock", "a/b.lock", "a.lock/b", "a.lockx", "a//b", "/a", "a/",
234 "a b", "a~b", "a^b", "a:b", "a?b", "a*b", "a[b", "a\\b", "a\tb", "a\x7fb", "a\x01b",
235 "café", "日本", "a{b", "a}b", "a!b", "a#b", "a'b", "a\"b", "a,b", "a;b", "a|b", "a=b",
236 "...", ".", "..", "a/..", "a/./b", "x.lock.lock", "@/a", "a/@", "HEAD", "-", "a\xffb",
237 }
238 for c := byte(0x20); c < 0x7f; c++ {
239 names = append(names, string(c), "a"+string(c)+"b", string(c)+"z", "z"+string(c))
240 }
241 for _, name := range names {
242 err := exec.Command(git, "check-ref-format", "refs/heads/"+name).Run()
243 if got, want := ValidBranch(name), err == nil; got != want {
244 t.Errorf("ValidBranch(%q) = %v; git check-ref-format says %v", name, got, want)
245 }
246 }
247 if ValidBranch("") {
248 t.Error("an empty branch name was accepted")
249 }
250}
251
252func TestARefNamesItsTargetAndChange(t *testing.T) {
253 for ref, ok := range map[string]bool{
254 Ref("main", id1): true,
255 Ref("release/1", id1): true,
256 Ref("main", id1[:31]): false,
257 Ref("main", id1) + "/x": false,
258 RefPrefix + id1: false,
259 Ref("ma..in", id1): false,
260 "refs/the-valley/change/main/" + id1: false,
261 "refs/the-valley/changes//" + id1: false,
262 fmt.Sprintf("%smain/%s", RefPrefix, id1): true,
263 } {
264 if _, _, got := ParseRef(ref); got != ok {
265 t.Errorf("ParseRef(%q) ok = %v, want %v", ref, got, ok)
266 }
267 }
268}
269
270func TestEncodeRefusesWhatParseWould(t *testing.T) {
271 r, err := Parse([]byte(record()))
272 if err != nil {
273 t.Fatal(err)
274 }
275 r.Versions[1].N = 3
276 if _, err := Encode(r); err == nil || !strings.Contains(err.Error(), "no gap") {
277 t.Errorf("Encode of a record with a gap: %v", err)
278 }
279 r.Versions[1].N = 2
280 r.Versions[1].Branch = "to\xffpic"
281 if _, err := Encode(r); err == nil || !strings.Contains(err.Error(), "not a branch name in UTF-8") {
282 t.Errorf("Encode of a branch that is not UTF-8: %v", err)
283 }
284 r.Versions[1].Branch = ""
285 r.Versions[1].Title = "<&>"
286 out, err := Encode(r)
287 if err != nil || !bytes.Contains(out, []byte(`"<&>"`)) {
288 t.Errorf("Encode escaped a title it need not: %s, %v", out, err)
289 }
290}
291
292// A request name of the form c-<change ID> is reserved for the change with
293// that ID, and nothing else is: the integrator reads a request so named as
294// that change's, and a name one letter off is a branch's.
295func TestOnlyCAndAChangeIDIsAChangesRequestName(t *testing.T) {
296 if got := RequestName(id1); got != "c-"+id1 {
297 t.Fatalf("RequestName = %q", got)
298 }
299 if id, ok := RequestID(RequestName(id1)); !ok || id != id1 {
300 t.Fatalf("RequestID(RequestName) = %q, %v", id, ok)
301 }
302 for _, name := range []string{
303 id1, // no prefix
304 "c-" + id1[:31], // 31 letters
305 "c-" + id1 + "k", // 33 letters
306 "c-" + "a" + id1[1:], // a letter before k
307 "C-" + id1, // another prefix
308 "c-" + strings.ToUpper(id1),
309 "topic-c-" + id1,
310 } {
311 if _, ok := RequestID(name); ok {
312 t.Errorf("RequestID(%q) reads a change ID", name)
313 }
314 }
315}