GitHubivarvong

ply

main
ply7 git calls124 ms32 KiB5 cachedprotocol v2
352 lines25 KiBRaw
1// The pkt-frame space (SPACES.md holes #6 and #14): pkt-line length framing, bounded-exhaustive, judged by git.
2//
3// length every class of the 4-byte prefix: 0000 flush, 0001 delim, 0002 response-end, 0003 invalid, 0004 empty,
4// 0005, 0006, 000a, 0100, fff0 (LARGE_PACKET_MAX, git's writer bound), fff1, fff2, fff3 (git's reader
5// bound: len-4 < LARGE_PACKET_MAX), fff4 (the first refused), ffff; upper-, lower- and mixed-case hex; a
6// non-hex byte (each neighbour of a hex range: '/', ':', '@', 'G', '`', 'g', and ' ', NUL, 0xff, '-', '+')
7// in each of the 4 positions
8// relation exact | short (one payload byte missing, then EOF) | long (one extra byte, then the rest) | cut1..cut3 (EOF
9// after 1..3 bytes of the prefix) | last (the frame, then EOF: the stream's terminator is missing)
10// position adv-v2 (discover: the v2 capability advertisement) | adv-v0 (receiveCaps: the receive-pack ref
11// advertisement) | ls-refs (a v2 command response section) | band1 | band2 | band3 (a sideband packet in the
12// fetch packfile section) | push-report (report-status, no side-band) | push-sideband (side-band-64k)
13// writer (hole #14) field {push option, ls-refs prefix, want, deepen-not, filter, ref} × payload length {65515,
14// 65516, 65517}: git's format_packet dies "impossibly long line" past LARGE_PACKET_MAX before writing a byte
15//
16// Spec: git v2.50.0 pkt-line.c packet_read_with_status (ported below as gitRead), checked against real git on every
17// framing cell: `git upload-pack --stateless-rpc` (protocol v2) reads the cell's bytes with GIT_TRACE_PACKET on, and the
18// frames it traces and the error it dies with must be the model's. ply's pktStep/pktFinish must then equal the model on
19// every framing cell (one chunk and byte by byte), and every position cell runs ply's real consumer over a real http
20// transport (test/space/wire.js) with the expected outcome below.
21import { test } from 'vitest';
22import assert from 'node:assert/strict';
23import { spawnSync, execFileSync } from 'node:child_process';
24import { mkdtempSync, rmSync } from 'node:fs';
25import { tmpdir } from 'node:os';
26import { join } from 'node:path';
27import { pkt, pktInit, pktStep, pktFinish } from '../../src/pktline.js';
28import { discover as discoverR } from '../../src/discover.js';
29import { receiveCaps as receiveCapsR } from '../../src/receivecaps.js';
30import { lsRefs as lsRefsR, lsRefsBody as lsRefsBodyR } from '../../src/lsrefs.js';
31import { fetchObject as fetchObjectR, fetchBody as fetchBodyR } from '../../src/fetch.js';
32import { push } from '../../src/push.js';
33import { pushWithOptions as pushWithOptionsR } from '../../src/receivecaps.js';
34import { transport, outcome, outcomeSync, cat, enc, band, FLUSH, DELIM } from './wire.js';
35import { git } from './pack-fixtures.js';
36import { unwrap, unwrapAsync } from '../unwrap.js';
38// Throwing adapters over the Result API (0.2.0): the assertions below are about the throw.
39const discover = (...a) => unwrapAsync(discoverR(...a));
40const receiveCaps = (...a) => unwrapAsync(receiveCapsR(...a));
41const lsRefs = (...a) => unwrapAsync(lsRefsR(...a));
42const lsRefsBody = (...a) => unwrap(lsRefsBodyR(...a));
43const fetchObject = (...a) => unwrapAsync(fetchObjectR(...a));
44const fetchBody = (...a) => unwrap(fetchBodyR(...a));
45const pushWithOptions = (...a) => unwrapAsync(pushWithOptionsR(...a));
46const LARGE_PACKET_MAX = 65520; // pkt-line.h
47const OID = 'a'.repeat(40);
49// ---- the spec: git's packet_read_with_status over a whole buffer (size = LARGE_PACKET_MAX, the packet_buffer) ----
50const hexval = c => (c >= 48 && c <= 57 ? c - 48 : c >= 97 && c <= 102 ? c - 87 : c >= 65 && c <= 70 ? c - 55 : -1);
51export function gitRead(buf) {
52 const frames = [];
53 let p = 0;
54 for (;;) {
55 if (p === buf.length) return { frames, end: 'eof' };
56 if (buf.length - p < 4) return { frames, end: 'hangup' }; // get_packet_data: "the remote end hung up unexpectedly"
57 const v = [0, 1, 2, 3].map(i => hexval(buf[p + i]));
58 if (v.some(x => x < 0)) return { frames, end: 'badchar' }; // "protocol error: bad line length character: %.4s"
59 const len = (v[0] << 12) | (v[1] << 8) | (v[2] << 4) | v[3];
60 if (len < 3) { frames.push({ kind: ['flush', 'delim', 'end'][len] }); p += 4; continue; }
61 if (len === 3) return { frames, end: 'badlen' }; // "protocol error: bad line length %d"
62 if (len - 4 >= LARGE_PACKET_MAX) return { frames, end: 'badlen' };
63 if (buf.length - p - 4 < len - 4) return { frames, end: 'hangup' };
64 frames.push({ kind: 'data', payload: buf.subarray(p + 4, p + len) });
65 p += len;
66 }
69// ---- the axes ----
70const NONHEX = ['/', ':', '@', 'G', '`', 'g', ' ', '\0', '\xff', '-', '+'];
71const LENGTHS = [
72 ...[['0000', 'flush'], ['0001', 'delim'], ['0002', 'end']].map(([hex, kind]) => ({ tag: `ctl-${kind}`, hex, control: kind })),
73 { tag: 'len3', hex: '0003', bad: true },
74 ...[4, 5, 6, 10, 256, 65520, 65521, 65522, 65523].map(n => ({ tag: `len${n}`, hex: n.toString(16).padStart(4, '0'), n })),
75 ...[65524, 65535].map(n => ({ tag: `len${n}`, hex: n.toString(16).padStart(4, '0'), n, bad: true })),
76 ...[['00AF', 175], ['00aF', 175], ['00Af', 175], ['FFF3', 65523]].map(([hex, n]) => ({ tag: `case-${hex}`, hex, n })),
77 { tag: 'case-FFF4', hex: 'FFF4', n: 65524, bad: true },
78 ...NONHEX.flatMap(ch => [0, 1, 2, 3].map(i => ({ tag: `nonhex-${ch.charCodeAt(0).toString(16)}@${i}`, hex: '000a'.slice(0, i) + ch + '000a'.slice(i + 1), n: 10, bad: true }))),
79];
80const RELATIONS = ['exact', 'short', 'long', 'cut1', 'cut2', 'cut3', 'last'];
81const hexBytes = s => Uint8Array.from([...s].map(c => c.charCodeAt(0)));
83// The cell's bytes: the prefix and the payload a position chooses (`payload(n)` returns exactly n - 4 bytes).
84function cellBytes(L, rel, payload) {
85 const prefix = hexBytes(L.hex);
86 const whole = L.control || L.tag === 'len3' ? prefix : cat([prefix, payload(L.n)]);
87 if (rel === 'exact' || rel === 'last') return whole;
88 if (rel === 'short') return whole.subarray(0, whole.length - 1);
89 if (rel === 'long') return cat([whole, Uint8Array.of(0x61)]);
90 return prefix.subarray(0, Number(rel.slice(3)));
92// the relation ends the stream (nothing after the cell) or leaves the rest of the response after it
93const endsStream = rel => rel !== 'exact' && rel !== 'long';
94const cells = () => LENGTHS.flatMap(L => RELATIONS.filter(r => !(L.control || L.tag === 'len3') || r !== 'short').map(rel => ({ L, rel })));
96// ---- framing cells: the model against real git, then ply against the model ----
97const gitDir = mkdtempSync(join(tmpdir(), 'ply-pkt-frame-'));
98git(gitDir, ['init', '-q', '--bare']);
99process.on('exit', () => rmSync(gitDir, { recursive: true, force: true }));
100const agentPayload = n => enc(n - 4 >= 6 ? `agent=${'a'.repeat(n - 10)}` : 'a'.repeat(n - 4));
101const traceText = b => { let s = ''; for (const c of b) { if (c === 10) continue; s += c >= 0x20 && c <= 0x7e ? String.fromCharCode(c) : `\\${c.toString(8)}`; } return s; };
103function runGit(bytes) {
104 const r = spawnSync('git', ['upload-pack', '--stateless-rpc', gitDir], { input: bytes, env: { ...process.env, GIT_PROTOCOL: 'version=2', GIT_TRACE_PACKET: '1' }, maxBuffer: 1 << 24 });
105 const err = r.stderr.toString('latin1');
106 const read = [];
107 for (const l of err.split('\n')) { const m = /packet: +upload-pack< (.*)$/.exec(l); if (m) read.push(m[1]); }
108 const fatal = (/fatal: (.*)/.exec(err) ?? [])[1] ?? null;
109 const end = fatal === null ? null : /bad line length character/.test(fatal) ? 'badchar' : /bad line length/.test(fatal) ? 'badlen' : /hung up unexpectedly/.test(fatal) ? 'hangup' : 'semantic';
110 return { read, fatal, end };
112const traced = f => (f.kind === 'data' ? traceText(f.payload) : { flush: '0000', delim: '0001', end: '0002' }[f.kind]);
114function plyRead(bytes, bytewise) {
115 const frames = [];
116 const o = outcomeSync(() => {
117 let st = pktInit();
118 if (bytewise) for (let i = 0; i < bytes.length; i++) { const r = unwrap(pktStep(st, bytes.subarray(i, i + 1))); st = r.state; frames.push(...r.frames); }
119 else { const r = unwrap(pktStep(st, bytes)); st = r.state; frames.push(...r.frames); }
120 unwrap(pktFinish(st));
121 });
122 return { frames, o };
124const plyEnd = o => (o.tag === 'ok' ? 'eof' : o.tag !== 'typed' || o.code !== 'protocol' ? `other:${o.code}` : /trailing/.test(o.message) ? 'hangup' : /bad pkt-line length at/.test(o.message) ? 'badchar' : /invalid pkt-line length|^bad pkt-line length \d+$/.test(o.message) ? 'badlen' : `protocol:${o.message}`);
125const sameFrames = (a, b) => a.length === b.length && a.every((f, i) => f.kind === b[i].kind && (f.kind !== 'data' || Buffer.compare(Buffer.from(f.payload), Buffer.from(b[i].payload)) === 0));
127const census = { framing: 0, gitObserved: 0, positions: 0, outcomes: {}, writer: 0 };
129test('framing: the model is git (upload-pack reads every cell with GIT_TRACE_PACKET), and ply is the model', () => {
130 const lead = unwrap(pkt('command=ls-refs\n'));
131 const wrong = [];
132 for (const { L, rel } of cells()) {
133 const cell = cellBytes(L, rel, agentPayload);
134 const stream = cat([lead, cell, endsStream(rel) ? new Uint8Array(0) : FLUSH]);
135 const m = gitRead(stream);
136 const g = runGit(stream);
137 census.framing++;
138 const id = `${L.tag}/${rel}`;
139 // git: what it traced is a prefix of the model's frames; where it died of framing, the model ends there the same way
140 const want = m.frames.map(traced);
141 if (g.read.some((r, i) => r !== want[i])) wrong.push(`${id}: git read ${JSON.stringify(g.read.map(s => s.slice(0, 20)))}, model ${JSON.stringify(want.map(s => s.slice(0, 20)))}`);
142 else if (g.end === 'hangup' && m.end === 'eof' && g.read.length === m.frames.length) {
143 // the stream ended on a frame boundary before the request's flush: git's request reader is not gentle on EOF
144 // there ("hung up"); the packet reader itself read every frame, which is the model's eof
145 census.gitObserved++;
146 } else if (['badchar', 'badlen', 'hangup'].includes(g.end)) {
147 census.gitObserved++;
148 if (g.end !== m.end || g.read.length !== m.frames.length) wrong.push(`${id}: git died ${g.end} after ${g.read.length} frames (${g.fatal}), model ${m.end} after ${m.frames.length}`);
149 } else if (g.end === null && g.read.length === m.frames.length) {
150 census.gitObserved++;
151 // a clean exit: git read every frame; the model must end cleanly or at a partial prefix git reads gently (EOF)
152 if (m.end !== 'eof' && m.end !== 'hangup') wrong.push(`${id}: git exited cleanly, model ${m.end}`);
153 }
154 // ply against the model, in one chunk and byte by byte (frames are compared byte by byte: a chunk that throws hands
155 // its consumer the error, not the frames before it in the same chunk)
156 for (const bytewise of [false, true]) {
157 const p = plyRead(stream, bytewise);
158 if ((bytewise && !sameFrames(p.frames, m.frames)) || plyEnd(p.o) !== m.end) wrong.push(`${id}${bytewise ? ' bytewise' : ''}: ply ${p.frames.length} frames, ${plyEnd(p.o)}; model ${m.frames.length}, ${m.end}`);
159 }
160 }
161 if (process.env.PKT_DUMP) console.error(wrong.join("\n")); assert.deepEqual(wrong.slice(0, 30), [], `${wrong.length} framing cell(s) disagree`);
162});
164// ---- position cells ----
165const BIG = (() => { // a single-object pack larger than one frame, so a band-1 cell of any legal length carries real pack bytes
166 const dir = mkdtempSync(join(tmpdir(), 'ply-pkt-big-'));
167 try {
168 git(dir, ['init', '-q']);
169 let x = 7; const data = Buffer.alloc(150_000); for (let i = 0; i < data.length; i++) { x = (x * 1103515245 + 12345) % 2147483648; data[i] = x >> 16; }
170 const oid = execFileSync('git', ['hash-object', '-w', '--stdin'], { cwd: dir, input: data }).toString().trim();
171 return { oid, pack: new Uint8Array(git(dir, ['pack-objects', '--stdout', '-q'], `${oid}\n`)) };
172 } finally { rmSync(dir, { recursive: true, force: true }); }
173})();
174const filler = n => enc('a'.repeat(Math.max(0, n)));
175const ZERO = '0'.repeat(40);
177// Each position: payload(n) (n-4 bytes, semantically neutral where git allows it), neutral(n): whether a legal data frame
178// of length n is neutral there, render(L) -> { kind: advertise | post, before, after } (the
179// response is before + cell + after; after ends with the terminator), run(t) the consumer, refuse: a refusal's code there.
180// a flush in the packfile section ends the response: frames after it are refused; as the last frame, the pack is cut
181const BAND_FLUSH = { exact: 'protocol', last: 'pack_corrupt' };
182const POS = {
183 'adv-v2': {
184 // a flush ends the advertisement: on its own an empty one, which names neither ls-refs nor fetch (unsupported: git
185 // "server doesn't support"); anything after it is refused (judgment: git's
186 // remote-curl hands the whole buffer to the client, which would read the rest as the next response)
187 payload: n => filler(n - 4), neutral: () => true, refuse: 'protocol', flush: { exact: 'protocol', last: 'unsupported' },
188 render: () => ({ kind: 'advertise', before: unwrap(pkt('version 2\n')), after: cat([unwrap(pkt('agent=git/2.50.0\n')), unwrap(pkt('ls-refs=unborn\n')), unwrap(pkt('fetch=shallow filter\n')), unwrap(pkt('object-format=sha1\n')), FLUSH]) }),
189 run: t => discover(t),
190 },
191 'adv-v0': {
192 // a ref line: "<oid> refs/heads/<name>\n" (connect.c get_remote_heads process_ref); shorter than one is malformed
193 payload: n => (n - 4 >= 54 ? enc(`${OID} refs/heads/${'a'.repeat(n - 4 - 53)}\n`) : filler(n - 4)), neutral: n => n - 4 >= 54, refuse: 'protocol',
194 // get_remote_heads reads to the first flush and no further: what follows a flush is never read
195 flush: { exact: 'ok', last: 'ok' },
196 render: () => ({ kind: 'advertise', before: cat([unwrap(pkt('# service=git-receive-pack\n')), FLUSH, unwrap(pkt(`${OID} refs/heads/main\0report-status delete-refs push-options\n`))]), after: cat([unwrap(pkt(`${OID} refs/tags/v1\n`)), FLUSH]) }),
197 run: t => receiveCaps(t),
198 },
199 'ls-refs': {
200 // a ref line with an attribute git and ply both skip (process_ref_v2 reads only symref-target: and peeled:)
201 payload: n => (n - 4 >= 64 ? enc(`${OID} refs/heads/pad x-pad:${'a'.repeat(n - 4 - 63)}\n`) : filler(n - 4)), neutral: n => n - 4 >= 64, refuse: 'protocol',
202 // the flush ends the listing: on its own a valid one; a line after it is refused ("after the terminating flush-pkt")
203 flush: { exact: 'protocol', last: 'ok' },
204 render: () => ({ kind: 'post', before: unwrap(pkt(`${OID} HEAD symref-target:refs/heads/main\n`)), after: cat([unwrap(pkt(`${OID} refs/heads/main\n`)), FLUSH]) }),
205 run: t => lsRefs(t),
206 },
207 band1: {
208 payload: n => (n < 5 ? new Uint8Array(0) : cat([Uint8Array.of(1), BIG.pack.subarray(1000, 1000 + n - 5)])), neutral: n => n >= 5, refuse: 'protocol', flush: BAND_FLUSH,
209 render: L => {
210 const used = !L.control && L.tag !== 'len3' && L.n >= 5 ? L.n - 5 : 0; // a data cell carries pack bytes 1000..1000+used
211 return { kind: 'post', before: cat([unwrap(pkt('packfile\n')), band(1, BIG.pack.subarray(0, 1000))]), after: cat([...chunks(BIG.pack.subarray(1000 + used), 60000).map(c => band(1, c)), FLUSH]) };
212 },
213 run: t => fetchObject(t, { oid: BIG.oid }),
214 },
215 band2: {
216 payload: n => (n < 5 ? new Uint8Array(0) : cat([Uint8Array.of(2), filler(n - 5)])), neutral: n => n >= 5, refuse: 'protocol', flush: BAND_FLUSH,
217 render: () => ({ kind: 'post', before: cat([unwrap(pkt('packfile\n')), band(1, BIG.pack.subarray(0, 1000))]), after: cat([...chunks(BIG.pack.subarray(1000), 60000).map(c => band(1, c)), FLUSH]) }),
218 run: t => fetchObject(t, { oid: BIG.oid }),
219 },
220 band3: {
221 payload: n => (n < 5 ? new Uint8Array(0) : cat([Uint8Array.of(3), filler(n - 5)])), neutral: () => false, refuse: 'protocol', remote: true, flush: BAND_FLUSH,
222 render: () => ({ kind: 'post', before: cat([unwrap(pkt('packfile\n')), band(1, BIG.pack.subarray(0, 1000))]), after: cat([...chunks(BIG.pack.subarray(1000), 60000).map(c => band(1, c)), FLUSH]) }),
223 run: t => fetchObject(t, { oid: BIG.oid }),
224 },
225 'push-report': {
226 payload: n => filler(n - 4), neutral: () => false, refuse: 'ambiguous', push: true,
227 render: () => ({ kind: 'post', before: unwrap(pkt('unpack ok\n')), after: cat([unwrap(pkt('ok refs/heads/r0\n')), FLUSH]) }),
228 run: t => unwrapAsync(push(t, { commands: [{ ref: 'refs/heads/r0', oldOid: OID, newOid: ZERO }], sideband: false })),
229 },
230 'push-sideband': {
231 payload: n => (n < 5 ? new Uint8Array(0) : cat([Uint8Array.of(2), filler(n - 5)])), neutral: n => n >= 5, refuse: 'ambiguous', push: true,
232 render: () => ({ kind: 'post', before: band(1, unwrap(pkt('unpack ok\n'))), after: cat([band(1, cat([unwrap(pkt('ok refs/heads/r0\n')), FLUSH])), FLUSH]) }),
233 run: t => unwrapAsync(push(t, { commands: [{ ref: 'refs/heads/r0', oldOid: OID, newOid: ZERO }], sideband: true })),
234 },
235};
236function chunks(b, n) { const out = []; for (let i = 0; i < b.length; i += n) out.push(b.subarray(i, i + n)); return out; }
238// The expected outcome of a position cell, from the model and the position's semantics:
239// framing the model refuses (bad length character, bad length, EOF inside a frame) -> refuse (the position's code)
240// the stream ends without its terminator (relation last; a cut) -> refuse
241// long: one stray byte shifts every later frame; whatever the model frames, they are not the response -> refuse
242// exact, a data frame: neutral payload -> ok; band 3 -> remote_error; a frame the position cannot hold -> refuse
243// exact, flush/delim/response-end where a data line belongs -> refuse (git: "expected flush after capabilities",
244// get_remote_heads "invalid packet", "expected flush after ref listing"; a delim or response-end ends git's sideband
245// demultiplexer, so the pack is cut; push: the report is incomplete)
246function expected(name, L, rel, response) {
247 const P = POS[name];
248 const m = gitRead(response);
249 const refuse = { tag: 'typed', code: P.refuse };
250 const legalData = !L.control && !L.bad;
251 // band 3 is acted on as it is read: git's demultiplexer dies "remote error" before it reads past the frame
252 if (P.remote && legalData && ['exact', 'long', 'last'].includes(rel) && L.n >= 5) return { tag: 'typed', code: 'remote_error' };
253 if (m.end !== 'eof') return refuse;
254 if (L.control === 'flush' && P.flush?.[rel]) return P.flush[rel] === 'ok' ? { tag: 'ok' } : { tag: 'typed', code: P.flush[rel] };
255 if (rel !== 'exact' || L.control) return refuse;
256 return P.neutral(L.n) ? { tag: 'ok' } : refuse;
259test('positions: every length × relation × position cell through ply\'s real consumer has its expected outcome', async () => {
260 const wrong = [];
261 for (const [name, P] of Object.entries(POS)) {
262 for (const { L, rel } of cells()) {
263 const cell = cellBytes(L, rel, P.payload);
264 const r = P.render(L, rel);
265 // a cutting relation ends the stream at the cell: nothing after it is sent
266 const sent = cat([r.before, cell, endsStream(rel) ? new Uint8Array(0) : r.after]);
267 const t = transport((kind, service) => {
268 if (kind === 'advertise') return r.kind === 'advertise' ? { bytes: sent } : undefined;
269 if (service === 'git-upload-pack' || service === 'git-receive-pack') return r.kind === 'post' ? { bytes: sent } : undefined;
270 });
271 const want = expected(name, L, rel, sent);
272 const got = await outcome(() => P.run(t));
273 census.positions++;
274 const k = `${name}:${got.tag === 'typed' ? got.code : got.tag}`; census.outcomes[k] = (census.outcomes[k] ?? 0) + 1;
275 if (got.tag !== want.tag || (want.tag === 'typed' && got.code !== want.code)) wrong.push(`${name} ${L.tag}/${rel}: want ${want.tag}${want.code ? `(${want.code})` : ''}, got ${got.tag}${got.code ? `(${got.code})` : ''} ${(got.message ?? '').slice(0, 90)}`);
276 }
277 }
278 if (process.env.PKT_DUMP) console.error(wrong.join("\n")); assert.deepEqual(wrong.slice(0, 40), [], `${wrong.length} position cell(s) wrong`);
279});
281// The smart-http "# service=" header (remote-curl check_smart_http): optional, its metadata lines run to a flush. Every
282// header form × both advertisements: absent and present-with-flush are the same advertisement; a header whose flush is
283// a delim, or missing (the response cut inside it), is refused.
284test('service header: absent | flush | metadata then flush | delim | cut, on both advertisements', async () => {
285 const wrong = [];
286 const bodies = {
287 'adv-v2': { run: t => discover(t), rest: cat([unwrap(pkt('version 2\n')), unwrap(pkt('agent=git/2.50.0\n')), unwrap(pkt('ls-refs\n')), unwrap(pkt('fetch=shallow filter\n')), FLUSH]) },
288 'adv-v0': { run: t => receiveCaps(t), rest: cat([unwrap(pkt(`${OID} refs/heads/main\0report-status\n`)), FLUSH]) },
289 // a receive-pack GET answered in v2 anyway: names no push capabilities, so a well-placed one is refused by name (#232)
290 'adv-v0 as v2': { run: t => receiveCaps(t), rest: cat([unwrap(pkt('version 2\n')), unwrap(pkt('agent=x\n')), FLUSH]), accepted: 'unsupported' },
291 };
292 const svc = { 'adv-v2': 'git-upload-pack', 'adv-v0': 'git-receive-pack', 'adv-v0 as v2': 'git-receive-pack' };
293 for (const [name, b] of Object.entries(bodies)) {
294 const head = unwrap(pkt(`# service=${svc[name]}\n`));
295 for (const [form, bytes, ok] of [
296 ['absent', b.rest, true], ['flush', cat([head, FLUSH, b.rest]), true], ['metadata', cat([head, unwrap(pkt('x-meta\n')), FLUSH, b.rest]), true],
297 ['delim', cat([head, DELIM, b.rest]), false], ['cut', head, false],
298 ['rest cut before its flush', cat([head, FLUSH, b.rest.subarray(0, b.rest.length - 4)]), false],
299 ]) {
300 const got = await outcome(() => b.run(transport(kind => (kind === 'advertise' ? { bytes } : undefined))));
301 census.positions++;
302 // the v2 refusal lands on the line that says `version 2`, so only a form that never reaches that line stays a framing error
303 const reachedV2 = b.accepted && form !== 'delim' && form !== 'cut';
304 const fine = reachedV2 ? got.tag === 'typed' && got.code === b.accepted : ok ? got.tag === 'ok' : got.tag === 'typed' && got.code === 'protocol';
305 if (!fine) wrong.push(`${name} ${form}: ${JSON.stringify(got).slice(0, 140)}`);
306 }
307 }
308 assert.deepEqual(wrong, []);
309});
311// ---- hole #14: the writer bound ----
312// git format_packet: `if (n > LARGE_PACKET_MAX) die("protocol error: impossibly long line")` with n the framed length, so
313// the largest payload is LARGE_PACKET_MAX - 4 = 65516. ply must refuse a longer field with a typed bad_request before any
314// request is made (I1: every error typed; I5: refuse caller input before I/O), and send a 65516-byte payload intact.
315test('writer: a field that frames past LARGE_PACKET_MAX is bad_request before any request; 65516 bytes frame intact', async () => {
316 const wrong = [];
317 const fields = {
318 // field -> (payload length) -> [the call, the payload prefix the field adds]
319 'push option': n => ({ call: t => pushWithOptions(t, { commands: [{ ref: 'refs/heads/r0', oldOid: OID, newOid: ZERO }], pushOptions: ['o'.repeat(n - 1)], sideband: false }), service: 'git-receive-pack', adv: true }),
320 'ls-refs prefix': n => ({ call: t => lsRefs(t, ['p'.repeat(n - 'ref-prefix \n'.length)]), service: 'git-upload-pack' }),
321 // a want, a have and a shallow are object ids (40 hex digits, #156): none can reach the bound.
322 'deepen-not': n => ({ call: () => fetchBody({ wants: [OID], deepenNot: ['d'.repeat(n - 'deepen-not \n'.length)] }), pure: true }),
323 filter: n => ({ call: () => fetchBody({ wants: [OID], filter: 'f'.repeat(n - 'filter \n'.length) }), pure: true }),
324 'ls-refs body': n => ({ call: () => lsRefsBody(['p'.repeat(n - 'ref-prefix \n'.length)]), pure: true }),
325 };
326 for (const [field, mk] of Object.entries(fields)) for (const n of [LARGE_PACKET_MAX - 5, LARGE_PACKET_MAX - 4, LARGE_PACKET_MAX - 3]) {
327 census.writer++;
328 const c = mk(n);
329 let requests = 0;
330 const t = transport((kind, service) => { requests++; if (kind === 'advertise') return { bytes: cat([unwrap(pkt('# service=git-receive-pack\n')), FLUSH, unwrap(pkt(`${OID} refs/heads/r0\0report-status delete-refs push-options\n`)), FLUSH]) }; return { bytes: service === 'git-upload-pack' ? FLUSH : cat([unwrap(pkt('unpack ok\n')), unwrap(pkt('ok refs/heads/r0\n')), FLUSH]) }; });
331 const got = await outcome(() => c.call(t));
332 const over = n > LARGE_PACKET_MAX - 4;
333 if (over) {
334 if (got.tag !== 'typed' || got.code !== 'bad_request') wrong.push(`${field} payload ${n}: want bad_request, got ${JSON.stringify(got).slice(0, 160)}`);
335 else if (!c.pure && requests > (c.adv ? 1 : 0)) wrong.push(`${field} payload ${n}: ${requests} request(s) before the refusal`);
336 } else if (got.tag !== 'ok') wrong.push(`${field} payload ${n}: want accepted, got ${JSON.stringify(got).slice(0, 160)}`);
337 else if (c.pure) {
338 const m = gitRead(got.value);
339 if (m.end !== 'eof' || !m.frames.some(f => f.kind === 'data' && f.payload.length === n)) wrong.push(`${field} payload ${n}: the body does not frame back to a ${n}-byte line`);
340 }
341 }
342 // pkt() itself, the one writer: every length to the bound frames back exactly; one past it is bad_request
343 for (const n of [0, 1, LARGE_PACKET_MAX - 4]) { const f = gitRead(unwrap(pkt(new Uint8Array(n)))); if (f.end !== 'eof' || f.frames[0].payload.length !== n) wrong.push(`pkt(${n})`); }
344 const over = outcomeSync(() => unwrap(pkt(new Uint8Array(LARGE_PACKET_MAX - 3))));
345 if (over.tag !== 'typed' || over.code !== 'bad_request') wrong.push(`pkt(${LARGE_PACKET_MAX - 3}): ${JSON.stringify(over)}`);
346 assert.deepEqual(wrong, []);
347});
349test('census', t => {
350 console.log(`${LENGTHS.length} length classes × ${RELATIONS.length} relations: ${census.framing} framing cells (${census.gitObserved} with git's verdict observed to the end), ${census.positions} position cells over ${Object.keys(POS).length} positions, ${census.writer} writer cells`);
351 for (const [k, v] of Object.entries(census.outcomes).sort()) console.log(` ${k}: ${v}`);
352});
Diagnostics7 git calls · 124 ms
CallStatusSizeTime0124 ms
discover200191 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=191
git.server.agent=git/github-9922ec520325-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=191
git.pkt.payload.max=36
ls-refs20012 KiB · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=11990
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=152
git.pkt.data=151
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11990
git.pkt.payload.max=122
git.ls_refs.count=151
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=4347
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4347
git.pkt.payload.max=4247
git.sideband.band1.bytes=4247
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=4247
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400
git.pack.full=10
Open pack map
fetchtree:0200919 B · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=919
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=5724
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5724
git.pkt.payload.max=5697
git.sideband.band1.bytes=5697
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=5697
git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetchtree:02001.4 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=1428
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=74d7bb09987bbe78a9c0ea8f43a1b5a4e079f538
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=1428
git.pkt.payload.max=1401
git.sideband.band1.bytes=1401
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=1401
git.pack.trailer=65ea1a3aeef333a80a2dfa3f441e629d90497e8e
Open pack map
fetch2008.4 KiB113 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=113
git.response.bytes=8649
git.response.cache=MISS
git.response.duration_ms=113
git.fetch.wants=c7ca9139367ea38ae0ec56834c55f4d24fd39aac
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=8649
git.pkt.payload.max=8622
git.sideband.band1.bytes=8622
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://github.com/ivarvong/ply
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e2f0a50d76fda9
Request
2b3c692f…
Protocol
v2 · sha1
Agent
git/github-9922ec520325-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format