Public: anyone can read this; the crew can change it.

A .pcap capture shows its packets as a table: time, source, destination, protocol, length #14572

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14553-a-pcap-capture-shows-its-packets-as-a-ta → main 9 commits fork 2 threads · 2 unresolved

  • Merged. scout/14553-a-pcap-capture-shows-its-packets-as-a-ta was merged into main as 2d8155a.

Review

Sign in to review this change.
8 files changed +360 −1
docs/features/file-kinds.md +1
@@ -17,6 +17,7 @@ The forge draws a file on its page when its kind is below and it is within the s
1717 - **Table (CSV, TSV, JSON lines)**, drawn up to 10 MB: `.csv`, `.tsv`, `.jsonl`, `.ndjson`
1818 - **SQLite database**, drawn up to 5 MB: `.db`, `.sqlite`, `.sqlite3`
1919 - **Parquet**, drawn up to 5 MB: `.parquet`
20 - **Packet capture**, drawn up to 5 MB: `.pcap`, `.pcapng`, `.cap`
2021 - **3D model**, drawn up to 100 MB: `.stl`, `.3mf`, `.ply`, `.glb`, `.gltf`, `.obj`, `.dae`
2122 - **Archive**, drawn up to 50 MB: `.zip`, `.tar`, `.tgz`, `.tar.gz`
2223 - **Spreadsheet**, drawn up to 10 MB: `.xlsx`
scripts/kinds-guide.ts +1 −1
@@ -10,7 +10,7 @@ export const END = "<!-- file-kinds:end -->";
1010 /** The name a person says for each kind. A new kind without one fails the type check. */
1111 export const NAMES: Readonly<Record<FileKind, string>> = {
1212 image: "Image", svg: "SVG", pdf: "PDF", html: "HTML page", audio: "Audio", video: "Video", csv: "Table (CSV, TSV, JSON lines)",
13 sqlite: "SQLite database", parquet: "Parquet", model3d: "3D model", archive: "Archive", sheet: "Spreadsheet", document: "Word document",
13 sqlite: "SQLite database", parquet: "Parquet", pcap: "Packet capture", model3d: "3D model", archive: "Archive", sheet: "Spreadsheet", document: "Word document",
1414 slides: "Slides", layers: "Layered image", font: "Font", psd: "Photoshop", cad: "CAD or 3D source (header facts)", config: "Config", markdown: "Markdown",
1515 notebook: "Notebook", patch: "Patch", log: "Log", subtitles: "Subtitles", gpx: "GPS track", har: "HTTP archive",
1616 geojson: "GeoJSON", kml: "KML", calendar: "Calendar", otio: "OpenTimelineIO", edl: "Edit list", fcpxml: "Final Cut XML", kdenlive: "Kdenlive project",
src/web/render/kinds.ts +18
@@ -25,6 +25,7 @@ export type FileKind =
2525 | "csv"
2626 | "sqlite"
2727 | "parquet"
28 | "pcap"
2829 | "model3d"
2930 | "archive"
3031 | "sheet"
@@ -111,6 +112,8 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = {
111112 sqlite: 5 * MB,
112113 // Only the footer is read, but the page reads the whole body: under its 5 MB ceiling.
113114 parquet: 5 * MB,
115 // Packet records are walked by their headers; the page reads the whole body.
116 pcap: 5 * MB,
114117 model3d: 100 * MB,
115118 archive: 50 * MB,
116119 sheet: 10 * MB,
@@ -428,6 +431,15 @@ const isGerberHead = (head: Uint8Array): boolean =>
428431 const isStepHead = (head: Uint8Array): boolean =>
429432 /^\uFEFF?\s*ISO-10303-21\s*;/.test(new TextDecoder().decode(head.subarray(0, 64)));
430433
434 /** A classic pcap or a pcapng capture opens on its magic number, in either byte order. */
435 const isPcap = (path: string, head: Uint8Array): boolean =>
436 (extension(path) === "pcap" || extension(path) === "pcapng" || extension(path) === "cap") &&
437 (bytes(0xa1, 0xb2, 0xc3, 0xd4)(head) ||
438 bytes(0xd4, 0xc3, 0xb2, 0xa1)(head) ||
439 bytes(0xa1, 0xb2, 0x3c, 0x4d)(head) ||
440 bytes(0x4d, 0x3c, 0xb2, 0xa1)(head) ||
441 bytes(0x0a, 0x0d, 0x0d, 0x0a)(head));
442
431443 /** For a caller that has only a name: the kind its extension gives. */
432444 export const NO_BYTES: Uint8Array = new Uint8Array(0);
433445
@@ -513,6 +525,7 @@ export function fileKind(path: string, head: Uint8Array): FileKind {
513525 const office = isOffice(path, head);
514526 if (office !== undefined) return office;
515527 if (isArchive(path, head)) return "archive";
528 if (isPcap(path, head)) return "pcap";
516529 // A CAD or 3D source is named by its magic as well as its name, and has NULs in its head.
517530 if (binaryFacts(extension(path), head) !== undefined) return "cad";
518531 if (looksBinary(head)) return "binary";
@@ -563,6 +576,7 @@ export function kindByName(path: string): FileKind {
563576 if (isWorkflowName(path)) return "workflow";
564577 if (isComposeName(path)) return "compose";
565578 if (ext === "xlsx") return "sheet";
579 if (ext === "pcap" || ext === "pcapng" || ext === "cap") return "pcap";
566580 if (OFFICE[ext] !== undefined) return OFFICE[ext]!;
567581 if (FACTS_EXTENSIONS.includes(ext)) return "cad";
568582 return MEDIA[ext]?.kind ?? TEXTUAL[ext] ?? "text";
@@ -596,6 +610,9 @@ export const KNOWN_EXTENSIONS: readonly string[] = [
596610 "tar",
597611 "tgz",
598612 "xlsx",
613 "pcap",
614 "pcapng",
615 "cap",
599616 ...Object.keys(OFFICE),
600617 ];
601618
@@ -666,6 +683,7 @@ const BY_CONTENT: Readonly<Partial<Record<FileKind, string>>> = {
666683 const SNIFFED_EXTENSIONS: Readonly<Partial<Record<FileKind, readonly string[]>>> = {
667684 sheet: ["xlsx"],
668685 archive: ["zip", "tar", "tgz"],
686 pcap: ["pcap", "pcapng", "cap"],
669687 cad: [...FACTS_EXTENSIONS],
670688 };
671689
src/web/render/pcap.ts added +217
@@ -0,0 +1,217 @@
1 /**
2 * A classic pcap capture as a packet table: relative time, source, destination, protocol and
3 * length. Parsed by hand: the 24-byte file header (both byte orders, micro- and nanosecond stamps),
4 * then each record header, then just enough of Ethernet, IPv4, IPv6, TCP, UDP and ICMP to name the
5 * endpoints. Nothing is decoded past the transport header and nothing runs.
6 *
7 * Bounds: the file is capped by `RENDER_CAP.pcap` before it gets here; every read is checked
8 * against the bytes left; the walk visits at most `WALK_CAP` records and each record advances by
9 * at least its 16-byte header, so a hostile length cannot spin it. Only `ROW_CAP` packets are
10 * drawn; the rest are counted. pcapng and an unknown link type fall back to the plain file view
11 * with one sentence, never an error page.
12 */
13
14 import { escapeHtml, html, raw } from "../html.ts";
15 import { fileSize } from "../ui.ts";
16 import type { Rendered, RenderInput } from "./renderers.ts";
17
18 export const ROW_CAP = 500;
19 const WALK_CAP = 2_000_000;
20
21 export interface Packet {
22 /** Seconds since the first packet, as text with six decimals. */
23 readonly time: string;
24 readonly source: string;
25 readonly destination: string;
26 readonly protocol: string;
27 /** The length on the wire. */
28 readonly length: number;
29 }
30 export interface Pcap {
31 readonly nano: boolean;
32 readonly linkType: number;
33 readonly packets: readonly Packet[];
34 /** Records found, shown or not. */
35 readonly total: number;
36 /** The file ends inside a record. */
37 readonly truncated: boolean;
38 }
39
40 /** Thrown with a sentence-sized reason when the file cannot be shown as packets. */
41 export class Unreadable extends Error {}
42
43 const LINKS: ReadonlySet<number> = new Set([0, 1, 101, 113]);
44
45 const hex = (n: number, w = 1): string => n.toString(16).padStart(w, "0");
46
47 function ipv4(b: Uint8Array, o: number): string {
48 return `${b[o]}.${b[o + 1]}.${b[o + 2]}.${b[o + 3]}`;
49 }
50
51 /** RFC 5952 text: the longest run of zero groups (two or more) becomes `::`. */
52 function ipv6(b: Uint8Array, o: number): string {
53 const g: number[] = [];
54 for (let i = 0; i < 8; i++) g.push((b[o + 2 * i]! << 8) | b[o + 2 * i + 1]!);
55 let best = -1;
56 let bestLen = 1;
57 for (let i = 0; i < 8; ) {
58 if (g[i] !== 0) { i++; continue; }
59 let j = i;
60 while (j < 8 && g[j] === 0) j++;
61 if (j - i > bestLen) { best = i; bestLen = j - i; }
62 i = j;
63 }
64 if (best < 0) return g.map((x) => hex(x)).join(":");
65 return `${g.slice(0, best).map((x) => hex(x)).join(":")}::${g.slice(best + bestLen).map((x) => hex(x)).join(":")}`;
66 }
67
68 interface Row { source: string; destination: string; protocol: string }
69
70 const endpoint = (ip: string, port: number | undefined): string =>
71 port === undefined ? ip : ip.includes(":") ? `[${ip}]:${port}` : `${ip}:${port}`;
72
73 const PROTOS: Readonly<Record<number, string>> = { 1: "ICMP", 6: "TCP", 17: "UDP", 58: "ICMPv6" };
74 const protoName = (proto: number): string => PROTOS[proto] ?? `IP proto ${proto}`;
75
76 function transport(b: Uint8Array, o: number, proto: number, src: string, dst: string): Row {
77 if ((proto === 6 || proto === 17) && b.length - o >= 4) {
78 const sp = (b[o]! << 8) | b[o + 1]!;
79 const dp = (b[o + 2]! << 8) | b[o + 3]!;
80 return { source: endpoint(src, sp), destination: endpoint(dst, dp), protocol: protoName(proto) };
81 }
outdated

Dead code: void v6; exists only to silence an unused parameter. Drop the parameter.

82 return { source: src, destination: dst, protocol: protoName(proto) };
83 }
84
85 function ip(b: Uint8Array, o: number): Row | undefined {
86 if (o >= b.length) return undefined;
87 const version = b[o]! >> 4;
88 if (version === 4) {
89 if (b.length - o < 20) return undefined;
90 const ihl = (b[o]! & 15) * 4;
91 const proto = b[o + 9]!;
92 const src = ipv4(b, o + 12);
93 const dst = ipv4(b, o + 16);
94 const fragment = (((b[o + 6]! << 8) | b[o + 7]!) & 0x1fff) !== 0;
95 if (fragment || ihl < 20) return { source: src, destination: dst, protocol: protoName(proto) };
outdated

Protocol-name ladder duplicated from transport (line 80). Extract one protoName helper.

96 return transport(b, o + ihl, proto, src, dst);
97 }
98 if (version === 6) {
99 if (b.length - o < 40) return undefined;
100 const src = ipv6(b, o + 8);
101 const dst = ipv6(b, o + 24);
102 let next = b[o + 6]!;
103 let at = o + 40;
104 // Hop-by-hop, routing and destination options: skipped, at most four.
105 for (let i = 0; i < 4 && (next === 0 || next === 43 || next === 60); i++) {
106 if (at + 2 > b.length) break;
107 next = b[at]!;
108 at += (b[at + 1]! + 1) * 8;
109 }
110 return transport(b, at, next, src, dst);
111 }
112 return undefined;
113 }
114
115 function frame(b: Uint8Array, link: number): Row {
116 let row: Row | undefined;
117 let kind = "";
118 if (link === 1) {
119 if (b.length < 14) return { source: "", destination: "", protocol: "short frame" };
120 let type = (b[12]! << 8) | b[13]!;
121 let o = 14;
122 for (let i = 0; i < 2 && (type === 0x8100 || type === 0x88a8) && b.length >= o + 4; i++) {
123 type = (b[o + 2]! << 8) | b[o + 3]!;
124 o += 4;
125 }
126 if (type === 0x0800 || type === 0x86dd) row = ip(b, o);
127 else kind = type === 0x0806 ? "ARP" : `ethertype 0x${hex(type, 4)}`;
128 } else if (link === 113) {
129 if (b.length < 16) return { source: "", destination: "", protocol: "short frame" };
130 const type = (b[14]! << 8) | b[15]!;
131 if (type === 0x0800 || type === 0x86dd) row = ip(b, 16);
132 else kind = type === 0x0806 ? "ARP" : `ethertype 0x${hex(type, 4)}`;
133 } else if (link === 0) row = ip(b, 4);
134 else row = ip(b, 0);
135 return row ?? { source: "", destination: "", protocol: kind || "not IP" };
136 }
137
138 /** `sec.micro` after `sec0.micro0`, with six decimals; a stamp before the first keeps a minus. */
139 function relative(sec: number, frac: number, sec0: number, frac0: number, nano: boolean): string {
140 const unit = nano ? 1e9 : 1e6;
141 let ds = sec - sec0;
142 let df = frac - frac0;
143 let sign = "";
144 if (ds < 0 || (ds === 0 && df < 0)) { ds = -ds; df = -df; sign = "-"; }
145 if (df < 0) { ds--; df += unit; }
146 const micro = nano ? Math.floor(df / 1000) : df;
147 return `${sign}${ds}.${String(micro).padStart(6, "0").slice(0, 6)}`;
148 }
149
150 /** The capture as plain data; throws `Unreadable` when it is not a classic pcap this can show. */
151 export function readPcap(body: Uint8Array): Pcap {
152 if (body.length < 4) throw new Unreadable("it is too short to be a capture");
153 const dv = new DataView(body.buffer, body.byteOffset, body.byteLength);
154 const magic = dv.getUint32(0, true);
155 if (magic === 0x0a0d0d0a) throw new Unreadable("it is a pcapng capture, which is not read here yet");
156 // Starts as little-endian, microsecond stamps (0xa1b2c3d4); the chain changes only what differs.
157 let le = true;
158 let nano = false;
159 if (magic === 0xa1b23c4d) nano = true;
160 else if (magic === 0xd4c3b2a1) le = false;
161 else if (magic === 0x4d3cb2a1) { le = false; nano = true; }
162 else if (magic !== 0xa1b2c3d4) throw new Unreadable("it does not start with a pcap signature");
163 if (body.length < 24) throw new Unreadable("its header is cut short");
164 const link = dv.getUint32(20, le) & 0x0fffffff;
165 if (!LINKS.has(link)) throw new Unreadable(`its link type (${link}) is not one this reads (Ethernet, Linux cooked, loopback and raw IP are)`);
166
167 const packets: Packet[] = [];
168 let total = 0;
169 let truncated = false;
170 let pos = 24;
171 let first: { sec: number; frac: number } | undefined;
172 while (pos < body.length && total < WALK_CAP) {
173 if (body.length - pos < 16) { truncated = true; break; }
174 const sec = dv.getUint32(pos, le);
175 const frac = dv.getUint32(pos + 4, le);
176 const incl = dv.getUint32(pos + 8, le);
177 const orig = dv.getUint32(pos + 12, le);
178 if (incl > body.length - pos - 16) { truncated = true; break; }
179 first ??= { sec, frac };
180 if (packets.length < ROW_CAP) {
181 const row = frame(body.subarray(pos + 16, pos + 16 + incl), link);
182 packets.push({ time: relative(sec, frac, first.sec, first.frac, nano), ...row, length: orig });
183 }
184 total++;
185 pos += 16 + incl;
186 }
187 return { nano, linkType: link, packets, total, truncated };
188 }
189
190 const plural = (n: number, one: string): string => `${n.toLocaleString()} ${one}${n === 1 ? "" : "s"}`;
191 const clean = (s: string): string => escapeHtml(s.replace(/[\u0000-\u001f\u007f]/g, "�"));
192
193 export function pcap(input: RenderInput): Rendered {
194 let p: Pcap;
195 try {
196 p = readPcap(input.body);
197 } catch (e) {
198 const why = e instanceof Unreadable ? e.message : "it could not be read";
199 return { content: html``, lines: 0, fallback: `This capture cannot be shown as packets: ${why}.` };
200 }
201 const summary = `pcap · ${plural(p.total, "packet")} · ${fileSize(input.size)}`;
202 const rows = p.packets
203 .map(
204 (k) =>
205 `<tr><td class="num">${k.time}</td><td>${clean(k.source)}</td><td>${clean(k.destination)}</td><td>${clean(k.protocol)}</td><td class="num">${k.length}</td></tr>`,
206 )
207 .join("");
208 const notes: string[] = [];
209 if (p.total > p.packets.length)
210 notes.push(`${(p.total - p.packets.length).toLocaleString()} of ${p.total.toLocaleString()} packets are not shown.`);
211 if (p.truncated) notes.push("The file ends inside a packet, so the capture is cut short.");
212 const content = [
213 `<div class="csv-scroll"><table class="table" data-dense aria-label="Packets"><thead><tr><th class="num">Time (s)</th><th>Source</th><th>Destination</th><th>Protocol</th><th class="num">Length</th></tr></thead><tbody>${rows}</tbody></table></div>`,
214 ...notes.map((n) => `<p class="muted small">${escapeHtml(n)}</p>`),
215 ].join("\n");
216 return { content: html`${raw(content)}`, lines: 0, summary };
217 }
src/web/render/renderers.ts +2
@@ -67,6 +67,7 @@ import { pptx } from "./pptx.ts";
6767 import { ora } from "./ora.ts";
6868 import { sqlite } from "./sqlite.ts";
6969 import { parquet } from "./parquet.ts";
70 import { pcap } from "./pcap.ts";
7071 import { subtitles } from "./subtitles.ts";
7172 import { video } from "./video.ts";
7273
@@ -137,6 +138,7 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = {
137138 csv,
138139 sqlite,
139140 parquet,
141 pcap,
140142 audio,
141143 video,
142144 model3d,
test/fuzz-renderers.test.ts +2
@@ -341,6 +341,7 @@ export const EXCLUDED: Readonly<Record<string, string>> = {
341341 video: "a <video> pointing at the media route; binary, needs a fixture",
342342 sqlite: "binary database pages; needs a fixture to splice into",
343343 parquet: "binary columnar file; needs a fixture to splice into",
344 pcap: "binary capture; the parser is exercised by test/pcap-render.test.ts with hostile lengths",
344345 archive: "zip/tar bytes; needs a fixture to splice into",
345346 sheet: "a zip of XML (xlsx); needs a fixture to splice into",
346347 document: "a zip of XML (docx); needs a fixture to splice into",
@@ -597,6 +598,7 @@ export const RAW_PATHS: Readonly<Record<string, string>> = {
597598 video: "a.mp4",
598599 sqlite: "a.sqlite",
599600 parquet: "a.parquet",
601 pcap: "a.pcap",
600602 archive: "a.zip",
601603 sheet: "a.xlsx",
602604 document: "a.docx",
test/pcap-render.test.ts added +116
@@ -0,0 +1,116 @@
1 /** A pcap capture (built here) as a packet table. */
2 import { describe, expect, test } from "bun:test";
3 import { fileKind, kindMatches } from "../src/web/render/kinds.ts";
4 import { pcap, readPcap, ROW_CAP } from "../src/web/render/pcap.ts";
5 import { RENDERERS } from "../src/web/render/renderers.ts";
6
7 interface Opt { le?: boolean; nano?: boolean; link?: number }
8
9 function build(frames: { sec: number; frac: number; data: number[]; orig?: number }[], o: Opt = {}): Uint8Array {
10 const le = o.le ?? true;
11 const out: number[] = [];
12 const u32 = (n: number) => out.push(...(le ? [n & 255, (n >>> 8) & 255, (n >>> 16) & 255, n >>> 24] : [n >>> 24, (n >>> 16) & 255, (n >>> 8) & 255, n & 255]));
13 const u16 = (n: number) => out.push(...(le ? [n & 255, n >> 8] : [n >> 8, n & 255]));
14 u32(o.nano ? 0xa1b23c4d : 0xa1b2c3d4);
15 u16(2); u16(4); u32(0); u32(0); u32(65535); u32(o.link ?? 1);
16 for (const f of frames) {
17 u32(f.sec); u32(f.frac); u32(f.data.length); u32(f.orig ?? f.data.length);
18 out.push(...f.data);
19 }
20 return Uint8Array.from(out);
21 }
22
23 const eth = (type: number, rest: number[]) => [1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1, type >> 8, type & 255, ...rest];
24 const v4 = (proto: number, l4: number[]) => [0x45, 0, 0, 0, 0, 0, 0, 0, 64, proto, 0, 0, 10, 0, 0, 1, 192, 168, 1, 2, ...l4];
25 const ports = (a: number, b: number) => [a >> 8, a & 255, b >> 8, b & 255, 0, 0, 0, 0];
26 const v6 = (next: number, l4: number[]) => [
27 0x60, 0, 0, 0, 0, 0, next, 64,
28 0x20, 1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
29 0x20, 1, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
30 ...l4,
31 ];
32
33 const tcp = eth(0x0800, v4(6, ports(51000, 443)));
34 const udp = eth(0x0800, v4(17, ports(53, 5353)));
35 const icmp = eth(0x0800, v4(1, [8, 0, 0, 0]));
36 const tcp6 = eth(0x86dd, v6(6, ports(1234, 80)));
37 const arp = eth(0x0806, [0, 1]);
38 const sample = () =>
39 build([
40 { sec: 100, frac: 0, data: tcp, orig: 1500 },
41 { sec: 100, frac: 250_000, data: udp },
42 { sec: 101, frac: 5, data: icmp },
43 { sec: 102, frac: 0, data: tcp6 },
44 { sec: 102, frac: 1, data: arp },
45 ]);
46 const draw = (body: Uint8Array) => pcap({ path: "a.pcap", size: body.length, body, media: "", download: "" });
47
48 describe("a .pcap capture", () => {
49 test("is a kind of its own, by name and magic number", () => {
50 expect(fileKind("a.pcap", sample())).toBe("pcap");
51 expect(fileKind("a.pcap", Uint8Array.from([0, 2, 0, 4, 0]))).toBe("binary");
52 expect(RENDERERS.pcap).toBe(pcap);
53 expect(kindMatches("pcap")).toContain(".pcap");
54 });
55
56 test("lists packets with relative time, endpoints, protocol and length", () => {
57 const p = readPcap(sample());
58 expect(p.total).toBe(5);
59 expect(p.packets[0]).toEqual({ time: "0.000000", source: "10.0.0.1:51000", destination: "192.168.1.2:443", protocol: "TCP", length: 1500 });
60 expect(p.packets[1]).toMatchObject({ time: "0.250000", source: "10.0.0.1:53", destination: "192.168.1.2:5353", protocol: "UDP" });
61 expect(p.packets[2]).toMatchObject({ time: "1.000005", source: "10.0.0.1", protocol: "ICMP" });
62 expect(p.packets[3]).toMatchObject({ source: "[2001:db8::1]:1234", destination: "[2001:db8::2]:80", protocol: "TCP" });
63 expect(p.packets[4]).toMatchObject({ source: "", protocol: "ARP" });
64 const r = draw(sample());
65 expect(r.summary).toContain("pcap · 5 packets");
66 expect(String(r.content)).toContain("<td>10.0.0.1:51000</td>");
67 expect(r.fallback).toBeUndefined();
68 });
69
70 test("reads the other byte order and nanosecond stamps", () => {
71 const p = readPcap(build([{ sec: 7, frac: 0, data: tcp }, { sec: 7, frac: 1_500_000, data: udp }], { le: false, nano: true }));
72 expect(p.nano).toBe(true);
73 expect(p.packets.map((k) => k.time)).toEqual(["0.000000", "0.001500"]);
74 expect(p.packets[1]!.protocol).toBe("UDP");
75 const be = readPcap(build([{ sec: 1, frac: 0, data: tcp }], { le: false }));
76 expect(be.packets[0]!.protocol).toBe("TCP");
77 });
78
79 test("reads raw IP and Linux cooked captures", () => {
80 const raw = readPcap(build([{ sec: 1, frac: 0, data: v4(17, ports(1, 2)) }], { link: 101 }));
81 expect(raw.packets[0]!.protocol).toBe("UDP");
82 const sll = readPcap(build([{ sec: 1, frac: 0, data: [0, 0, 0, 1, 0, 6, 1, 2, 3, 4, 5, 6, 0, 0, 8, 0, ...v4(6, ports(9, 10))] }], { link: 113 }));
83 expect(sll.packets[0]).toMatchObject({ destination: "192.168.1.2:10", protocol: "TCP" });
84 });
85
86 test("shows the first 500 packets and says how many were not shown", () => {
87 const many = build(Array.from({ length: ROW_CAP + 25 }, (_, i) => ({ sec: i, frac: 0, data: tcp })));
88 const r = draw(many);
89 expect((String(r.content).match(/<tr><td/g) ?? []).length).toBe(ROW_CAP);
90 expect(String(r.content)).toContain("25 of 525 packets are not shown.");
91 });
92
93 test("a truncated file keeps the packets before the cut and says so", () => {
94 const whole = sample();
95 const cut = whole.subarray(0, whole.length - 5);
96 const p = readPcap(cut);
97 expect(p.total).toBe(4);
98 expect(p.truncated).toBe(true);
99 expect(String(draw(cut).content)).toContain("ends inside a packet");
100 // A hostile record length is refused, not followed.
101 const evil = sample().slice();
102 evil.set([0xff, 0xff, 0xff, 0x7f], 24 + 8);
103 expect(readPcap(evil).total).toBe(0);
104 });
105
106 test("a pcapng header, an unknown link type and junk fall back with one sentence", () => {
107 const ng = Uint8Array.from([0x0a, 0x0d, 0x0d, 0x0a, 28, 0, 0, 0, 0x4d, 0x3c, 0x2b, 0x1a, 1, 0, 0, 0]);
108 expect(fileKind("a.pcapng", ng)).toBe("pcap");
109 const r = draw(ng);
110 expect(r.fallback).toContain("pcapng");
111 expect(draw(build([], { link: 147 })).fallback).toContain("link type (147)");
112 expect(draw(Uint8Array.from([0xa1, 0xb2, 0xc3, 0xd4, 1])).fallback).toContain("cut short");
113 expect(draw(new Uint8Array(0)).fallback).toBeDefined();
114 });
115
116 });
test/render-kinds.test.ts +3
@@ -47,6 +47,9 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = {
4747 sqlite: [enc("SQLite format 3\0"), "sqlite", "application/vnd.sqlite3"],
4848 sqlite3: [enc("SQLite format 3\0"), "sqlite", "application/vnd.sqlite3"],
4949 parquet: [enc("PAR1"), "parquet", "application/vnd.apache.parquet"],
50 pcap: [of(0xd4, 0xc3, 0xb2, 0xa1, 2, 0, 4, 0), "pcap", "application/octet-stream"],
51 pcapng: [of(0x0a, 0x0d, 0x0d, 0x0a, 28, 0, 0, 0), "pcap", "application/octet-stream"],
52 cap: [of(0xa1, 0xb2, 0xc3, 0xd4, 0, 2, 0, 4), "pcap", "application/octet-stream"],
5053 csv: [enc("a,b\n1,2\n"), "csv", "application/octet-stream"],
5154 tsv: [enc("a\tb\n"), "csv", "application/octet-stream"],
5255 jsonl: [enc("{\"a\":1}\n"), "csv", "application/octet-stream"],

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs