gunk-dev/the-valley

vectors.py

sourcefa56c705b99emain's published tip history
7.8 KiB
1#!/usr/bin/env python3
2"""The valley-delta-v1 conformance vectors, computed from the specification.
3
4This script is the source of every expected result under this directory. It
5reads nothing but the vector specs, and uses nothing but Python's standard
6library: no git, and none of the Go code it checks. See README.md.
7
8 python3 -I vectors.py <testdata dir> write the expected results
9 python3 -I vectors.py --check <testdata dir> fail if any differs
10
11For each valid/<name>.json, which lists the files of a pre tree and a post
12tree, it writes valid/<name>.delta, the encoding, and valid/<name>.sha256, its
13digest. It writes each malformed vector below to malformed/<name>.delta.
14"""
15
16import hashlib
17import json
18import pathlib
19import sys
20
21HEADER = b"valley-delta-v1\n"
22
23
24def files(listing):
25 """A tree's files: path bytes to (mode, SHA-256 of the content)."""
26 tree = {}
27 for f in listing:
28 path = bytes.fromhex(f["path_hex"]) if "path_hex" in f else f["path"].encode("utf-8")
29 content = bytes.fromhex(f["content_hex"]) if "content_hex" in f else f["content"].encode("utf-8")
30 if path in tree:
31 raise SystemExit(f"{path!r} is listed twice")
32 tree[path] = (f["mode"], hashlib.sha256(content).hexdigest())
33 return tree
34
35
36def image(entry):
37 return b"-" if entry is None else f"{entry[0]} {entry[1]}".encode()
38
39
40def encode(pre, post):
41 """The header, then a line for each path whose entries differ, bytewise."""
42 out = bytearray(HEADER)
43 for path in sorted(set(pre) | set(post)):
44 before, after = pre.get(path), post.get(path)
45 if before == after:
46 continue
47 out += b"%d %s %s %s\n" % (len(path), path, image(before), image(after))
48 return bytes(out)
49
50
51H1 = hashlib.sha256(b"one\n").hexdigest().encode()
52H2 = hashlib.sha256(b"two\n").hexdigest().encode()
53EMPTY = hashlib.sha256(b"").hexdigest().encode()
54
55
56def line(path, pre, post):
57 return b"%d %s %s %s\n" % (len(path), path, pre, post)
58
59
60# Each malformed vector: its name, why it is refused, and its bytes.
61MALFORMED = [
62 ("empty-file", "no header", b""),
63 ("other-header", "the header names another scheme",
64 b"valley-delta-v2\n" + line(b"a", b"-", b"100644 " + H1)),
65 ("header-unterminated", "the header has no newline", b"valley-delta-v1"),
66 ("header-crlf", "the header ends in CRLF", b"valley-delta-v1\r\n"),
67 ("duplicate-path", "a path is given twice",
68 HEADER + line(b"a", b"-", b"100644 " + H1) + line(b"a", b"-", b"100644 " + H2)),
69 ("unsorted", "paths are out of bytewise order",
70 HEADER + line(b"b", b"-", b"100644 " + H1) + line(b"a", b"-", b"100644 " + H2)),
71 ("length-leading-zero", "the length has a leading zero",
72 HEADER + b"01 a - 100644 " + H1 + b"\n"),
73 ("length-plus", "the length has a sign", HEADER + b"+1 a - 100644 " + H1 + b"\n"),
74 ("length-zero", "the path is empty", HEADER + b"0 - 100644 " + H1 + b"\n"),
75 ("length-short", "the length stops inside the path",
76 HEADER + b"1 ab - 100644 " + H1 + b"\n"),
77 ("length-long", "the length runs into the images",
78 HEADER + b"3 a - 100644 " + H1 + b"\n"),
79 ("length-past-end", "the length runs past the end", HEADER + b"9999 a\n"),
80 ("length-huge", "the length has more digits than any path could need",
81 HEADER + b"99999999999999999999 a - 100644 " + H1 + b"\n"),
82 ("length-missing", "the line opens with the path", HEADER + b"a - 100644 " + H1 + b"\n"),
83 ("mode-invalid", "the mode is no mode git writes for a file",
84 HEADER + line(b"a", b"-", b"100664 " + H1)),
85 ("mode-short", "the mode is a prefix of a valid one", HEADER + line(b"a", b"-", b"10064 " + H1)),
86 ("mode-directory", "a directory is no entry", HEADER + line(b"a", b"-", b"40000 " + H1)),
87 ("mode-directory-padded", "a directory is no entry, however written",
88 HEADER + line(b"a", b"-", b"040000 " + H1)),
89 ("mode-submodule", "a submodule is an unsupported entry",
90 HEADER + line(b"a", b"-", b"160000 " + H1)),
91 ("hash-uppercase", "the hash is upper case",
92 HEADER + line(b"a", b"-", b"100644 " + H1.upper())),
93 ("hash-short", "the hash has 63 digits", HEADER + line(b"a", b"-", b"100644 " + H1[:63])),
94 ("hash-long", "the hash has 65 digits", HEADER + line(b"a", b"-", b"100644 " + H1 + b"0")),
95 ("hash-not-hex", "the hash holds a letter past f",
96 HEADER + line(b"a", b"-", b"100644 " + H1[:63] + b"g")),
97 ("hash-sha1", "the hash is a git object ID, not SHA-256",
98 HEADER + line(b"a", b"-", b"100644 " + b"e" * 40)),
99 ("image-missing", "the line has one image", HEADER + b"1 a -\n"),
100 ("both-absent", "a path absent on both sides is no change", HEADER + line(b"a", b"-", b"-")),
101 ("unchanged", "a path with equal images is no change",
102 HEADER + line(b"a", b"100644 " + H1, b"100644 " + H1)),
103 ("double-space", "two spaces between the images",
104 HEADER + b"1 a - 100644 " + H1 + b"\n"),
105 ("line-crlf", "a line ends in CRLF", HEADER + b"1 a - 100644 " + H1 + b"\r\n"),
106 ("line-unterminated", "the last line has no newline", HEADER + b"1 a - 100644 " + H1),
107 ("trailing-bytes", "something follows the last line",
108 HEADER + line(b"a", b"-", b"100644 " + H1) + b"\n"),
109 ("path-dot-dot", "a path climbs out of the tree", HEADER + line(b"../a", b"-", b"100644 " + H1)),
110 ("path-dot", "a path has the component .", HEADER + line(b"./a", b"-", b"100644 " + H1)),
111 ("path-dotgit", "a path is inside .git", HEADER + line(b".git/config", b"-", b"100644 " + H1)),
112 ("path-dotgit-case", "a path is inside .git, in another case",
113 HEADER + line(b"sub/.GiT/hooks", b"-", b"100644 " + H1)),
114 ("path-leading-slash", "a path is absolute", HEADER + line(b"/a", b"-", b"100644 " + H1)),
115 ("path-trailing-slash", "a path ends in a slash", HEADER + line(b"a/", b"-", b"100644 " + H1)),
116 ("path-double-slash", "a path has an empty component",
117 HEADER + line(b"a//b", b"-", b"100644 " + H1)),
118 ("path-nul", "a path holds a NUL", HEADER + line(b"a\x00b", b"-", b"100644 " + H1)),
119 ("path-component-long", "a path has a component of 256 bytes, past Linux's limit of 255",
120 HEADER + line(b"d/" + b"n" * 256, b"-", b"100644 " + H1)),
121 ("symlink-empty-target", "a symlink has the hash of empty content, and no target is empty",
122 HEADER + line(b"a", b"-", b"120000 " + EMPTY)),
123 ("alias-post", "the post side has a file at a path and another under it",
124 HEADER + line(b"a", b"-", b"100644 " + H1) + line(b"a/b", b"-", b"100644 " + H2)),
125 ("alias-pre", "the pre side has a symlink at a path and a file under it",
126 HEADER + line(b"d", b"120000 " + H1, b"-") + line(b"d/e/f", b"100644 " + H2, b"-")),
127]
128
129
130def main(argv):
131 check = argv[:1] == ["--check"]
132 if check:
133 argv = argv[1:]
134 if len(argv) != 1:
135 raise SystemExit(__doc__)
136 root = pathlib.Path(argv[0])
137 want = {}
138 for spec in sorted((root / "valid").glob("*.json")):
139 vector = json.loads(spec.read_text(encoding="utf-8"))
140 encoding = encode(files(vector["pre"]), files(vector["post"]))
141 want[spec.with_suffix(".delta")] = encoding
142 want[spec.with_suffix(".sha256")] = hashlib.sha256(encoding).hexdigest().encode() + b"\n"
143 for name, _, data in MALFORMED:
144 want[root / "malformed" / f"{name}.delta"] = data
145 stale = []
146 for path, data in sorted(want.items()):
147 if check:
148 if not path.exists() or path.read_bytes() != data:
149 stale.append(str(path))
150 else:
151 path.write_bytes(data)
152 for directory, pattern in (("valid", "*.delta"), ("valid", "*.sha256"), ("malformed", "*.delta")):
153 for path in (root / directory).glob(pattern):
154 if path not in want:
155 stale.append(f"{path} (no vector makes it)")
156 if stale:
157 raise SystemExit("not what vectors.py writes:\n " + "\n ".join(stale))
158
159
160if __name__ == "__main__":
161 main(sys.argv[1:])