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