File
Blob: test/receive-missing-parent.worker.test.ts
| 1 | import { it, expect } from "vitest"; |
| 2 | import { env } from "cloudflare:workers"; |
| 3 | import { pktLine, flushPkt, concatChunks } from "@/worker/git"; |
| 4 | import { buildPack, makeTree, postReceivePack, uniqueRepoId, zero40 } from "./util/test-helpers"; |
| 5 | import { setupRepoForTests } from "./util/repoSeed"; |
| 6 | |
| 7 | it("receive-pack connectivity: rejects commit whose parent is missing", async () => { |
| 8 | const owner = "o"; |
| 9 | const repo = uniqueRepoId("r-missing-parent"); |
| 10 | await setupRepoForTests(env, owner, repo); |
| 11 | const url = `https://example.com/${owner}/${repo}/git-receive-pack`; |
| 12 | |
| 13 | // Build an empty tree and a commit that points to a nonexistent parent |
| 14 | const { oid: treeOid, payload: treePayload } = await makeTree(); |
| 15 | const missingParent = "d".repeat(40); |
| 16 | const author = `You <you@example.com> 0 +0000`; |
| 17 | const msg = "missing parent\n"; |
| 18 | const commitPayload = new TextEncoder().encode( |
| 19 | `tree ${treeOid}\n` + |
| 20 | `parent ${missingParent}\n` + |
| 21 | `author ${author}\n` + |
| 22 | `committer ${author}\n\n${msg}` |
| 23 | ); |
| 24 | const commitHead = new TextEncoder().encode(`commit ${commitPayload.byteLength}\0`); |
| 25 | const commitRaw = new Uint8Array(commitHead.length + commitPayload.length); |
| 26 | commitRaw.set(commitHead, 0); |
| 27 | commitRaw.set(commitPayload, commitHead.length); |
| 28 | const commitOid = Array.from(new Uint8Array(await crypto.subtle.digest("SHA-1", commitRaw))) |
| 29 | .map((b) => b.toString(16).padStart(2, "0")) |
| 30 | .join(""); |
| 31 | |
| 32 | // Build pack containing tree and the commit-with-missing-parent |
| 33 | const pack = await buildPack([ |
| 34 | { type: "tree", payload: treePayload }, |
| 35 | { type: "commit", payload: commitPayload }, |
| 36 | ]); |
| 37 | |
| 38 | // Commands section then pack |
| 39 | const cmd = `${zero40()} ${commitOid} refs/heads/dev\0 report-status ofs-delta agent=test\n`; |
| 40 | const body = concatChunks([pktLine(cmd), flushPkt(), pack]); |
| 41 | |
| 42 | const res = await postReceivePack(url, body); |
| 43 | expect(res.status).toBe(200); |
| 44 | const bytes = new Uint8Array(await res.arrayBuffer()); |
| 45 | const lines = (await import("@/worker/git")) |
| 46 | .decodePktLines(bytes) |
| 47 | .filter((i: any) => i.type === "line") |
| 48 | .map((i: any) => i.text.trim()); |
| 49 | const ng = lines.find((l) => l.startsWith("ng refs/heads/dev ")); |
| 50 | expect(ng && /missing-objects$/.test(ng)).toBeTruthy(); |
| 51 | }); |