gunk-dev/the-valley

vectors.py

source885985671589main's published tip history
7.4 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()
53
54
55def line(path, pre, post):
56 return b"%d %s %s %s\n" % (len(path), path, pre, post)
57
58
59# Each malformed vector: its name, why it is refused, and its bytes.
60MALFORMED = [
61 ("empty-file", "no header", b""),
62 ("other-header", "the header names another scheme",
63 b"valley-delta-v2\n" + line(b"a", b"-", b"100644 " + H1)),
64 ("header-unterminated", "the header has no newline", b"valley-delta-v1"),
65 ("header-crlf", "the header ends in CRLF", b"valley-delta-v1\r\n"),
66 ("duplicate-path", "a path is given twice",
67 HEADER + line(b"a", b"-", b"100644 " + H1) + line(b"a", b"-", b"100644 " + H2)),
68 ("unsorted", "paths are out of bytewise order",
69 HEADER + line(b"b", b"-", b"100644 " + H1) + line(b"a", b"-", b"100644 " + H2)),
70 ("length-leading-zero", "the length has a leading zero",
71 HEADER + b"01 a - 100644 " + H1 + b"\n"),
72 ("length-plus", "the length has a sign", HEADER + b"+1 a - 100644 " + H1 + b"\n"),
73 ("length-zero", "the path is empty", HEADER + b"0 - 100644 " + H1 + b"\n"),
74 ("length-short", "the length stops inside the path",
75 HEADER + b"1 ab - 100644 " + H1 + b"\n"),
76 ("length-long", "the length runs into the images",
77 HEADER + b"3 a - 100644 " + H1 + b"\n"),
78 ("length-past-end", "the length runs past the end", HEADER + b"9999 a\n"),
79 ("length-huge", "the length has more digits than any path could need",
80 HEADER + b"99999999999999999999 a - 100644 " + H1 + b"\n"),
81 ("length-missing", "the line opens with the path", HEADER + b"a - 100644 " + H1 + b"\n"),
82 ("mode-invalid", "the mode is no mode git writes for a file",
83 HEADER + line(b"a", b"-", b"100664 " + H1)),
84 ("mode-short", "the mode is a prefix of a valid one", HEADER + line(b"a", b"-", b"10064 " + H1)),
85 ("mode-directory", "a directory is no entry", HEADER + line(b"a", b"-", b"40000 " + H1)),
86 ("mode-directory-padded", "a directory is no entry, however written",
87 HEADER + line(b"a", b"-", b"040000 " + H1)),
88 ("mode-submodule", "a submodule is an unsupported entry",
89 HEADER + line(b"a", b"-", b"160000 " + H1)),
90 ("hash-uppercase", "the hash is upper case",
91 HEADER + line(b"a", b"-", b"100644 " + H1.upper())),
92 ("hash-short", "the hash has 63 digits", HEADER + line(b"a", b"-", b"100644 " + H1[:63])),
93 ("hash-long", "the hash has 65 digits", HEADER + line(b"a", b"-", b"100644 " + H1 + b"0")),
94 ("hash-not-hex", "the hash holds a letter past f",
95 HEADER + line(b"a", b"-", b"100644 " + H1[:63] + b"g")),
96 ("hash-sha1", "the hash is a git object ID, not SHA-256",
97 HEADER + line(b"a", b"-", b"100644 " + b"e" * 40)),
98 ("image-missing", "the line has one image", HEADER + b"1 a -\n"),
99 ("both-absent", "a path absent on both sides is no change", HEADER + line(b"a", b"-", b"-")),
100 ("unchanged", "a path with equal images is no change",
101 HEADER + line(b"a", b"100644 " + H1, b"100644 " + H1)),
102 ("double-space", "two spaces between the images",
103 HEADER + b"1 a - 100644 " + H1 + b"\n"),
104 ("line-crlf", "a line ends in CRLF", HEADER + b"1 a - 100644 " + H1 + b"\r\n"),
105 ("line-unterminated", "the last line has no newline", HEADER + b"1 a - 100644 " + H1),
106 ("trailing-bytes", "something follows the last line",
107 HEADER + line(b"a", b"-", b"100644 " + H1) + b"\n"),
108 ("path-dot-dot", "a path climbs out of the tree", HEADER + line(b"../a", b"-", b"100644 " + H1)),
109 ("path-dot", "a path has the component .", HEADER + line(b"./a", b"-", b"100644 " + H1)),
110 ("path-dotgit", "a path is inside .git", HEADER + line(b".git/config", b"-", b"100644 " + H1)),
111 ("path-dotgit-case", "a path is inside .git, in another case",
112 HEADER + line(b"sub/.GiT/hooks", b"-", b"100644 " + H1)),
113 ("path-leading-slash", "a path is absolute", HEADER + line(b"/a", b"-", b"100644 " + H1)),
114 ("path-trailing-slash", "a path ends in a slash", HEADER + line(b"a/", b"-", b"100644 " + H1)),
115 ("path-double-slash", "a path has an empty component",
116 HEADER + line(b"a//b", b"-", b"100644 " + H1)),
117 ("path-nul", "a path holds a NUL", HEADER + line(b"a\x00b", b"-", b"100644 " + H1)),
118 ("alias-post", "the post side has a file at a path and another under it",
119 HEADER + line(b"a", b"-", b"100644 " + H1) + line(b"a/b", b"-", b"100644 " + H2)),
120 ("alias-pre", "the pre side has a symlink at a path and a file under it",
121 HEADER + line(b"d", b"120000 " + H1, b"-") + line(b"d/e/f", b"100644 " + H2, b"-")),
122]
123
124
125def main(argv):
126 check = argv[:1] == ["--check"]
127 if check:
128 argv = argv[1:]
129 if len(argv) != 1:
130 raise SystemExit(__doc__)
131 root = pathlib.Path(argv[0])
132 want = {}
133 for spec in sorted((root / "valid").glob("*.json")):
134 vector = json.loads(spec.read_text(encoding="utf-8"))
135 encoding = encode(files(vector["pre"]), files(vector["post"]))
136 want[spec.with_suffix(".delta")] = encoding
137 want[spec.with_suffix(".sha256")] = hashlib.sha256(encoding).hexdigest().encode() + b"\n"
138 for name, _, data in MALFORMED:
139 want[root / "malformed" / f"{name}.delta"] = data
140 stale = []
141 for path, data in sorted(want.items()):
142 if check:
143 if not path.exists() or path.read_bytes() != data:
144 stale.append(str(path))
145 else:
146 path.write_bytes(data)
147 for directory, pattern in (("valid", "*.delta"), ("valid", "*.sha256"), ("malformed", "*.delta")):
148 for path in (root / directory).glob(pattern):
149 if path not in want:
150 stale.append(f"{path} (no vector makes it)")
151 if stale:
152 raise SystemExit("not what vectors.py writes:\n " + "\n ".join(stale))
153
154
155if __name__ == "__main__":
156 main(sys.argv[1:])