58 lines
2.2 KiB
TypeScript
58 lines
2.2 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { projectChrWire } from "./traffic-flow-chr-wire.js"
|
|
|
|
const scope = { sources: ["192.0.2.1", "192.0.2.2"] as const, destination: "192.0.2.100", port: 4739 }
|
|
function capture() {
|
|
const header = Buffer.alloc(24)
|
|
header.writeUInt32LE(0xa1b2c3d4)
|
|
header.writeUInt16LE(2, 4)
|
|
header.writeUInt16LE(4, 6)
|
|
header.writeUInt32LE(4124, 16)
|
|
header.writeUInt32LE(101, 20)
|
|
const frames = [1, 2].map(lastOctet => {
|
|
const record = Buffer.alloc(16 + 20 + 8 + 16)
|
|
record.writeUInt32LE(1700000000)
|
|
record.writeUInt32LE(lastOctet, 4)
|
|
record.writeUInt32LE(44, 8)
|
|
record.writeUInt32LE(44, 12)
|
|
const ip = record.subarray(16)
|
|
ip[0] = 0x45
|
|
ip.writeUInt16BE(44, 2)
|
|
ip[9] = 17
|
|
Buffer.from([192, 0, 2, lastOctet, 192, 0, 2, 100]).copy(ip, 12)
|
|
ip.writeUInt16BE(2055, 20)
|
|
ip.writeUInt16BE(4739, 22)
|
|
ip.writeUInt16BE(24, 24)
|
|
ip.writeUInt16BE(10, 28)
|
|
ip.writeUInt16BE(16, 30)
|
|
ip.writeUInt32BE(lastOctet, 40)
|
|
return record
|
|
})
|
|
return Buffer.concat([header, ...frames])
|
|
}
|
|
|
|
test("projects exact wire payload, exporter source port, time and domain from a two-source pcap", () => {
|
|
const pcap = capture()
|
|
const projected = projectChrWire(pcap, scope)
|
|
assert.deepEqual(projected.frames.map(({ exporter, sourcePort, domain, receivedNs }) => [exporter, sourcePort, domain, receivedNs]),
|
|
[[0, 2055, 1, "1700000000000001000"], [1, 2055, 2, "1700000000000002000"]])
|
|
assert.equal(projected.frames[0].payloadHex, pcap.subarray(68, 84).toString("hex"))
|
|
assert.match(projected.captureSha256, /^[a-f0-9]{64}$/)
|
|
})
|
|
|
|
test("refuses wrong link type, incomplete wire bytes, fragments, wrong destinations and single-exporter capture", () => {
|
|
assert.throws(() => projectChrWire(capture().subarray(0, -1), scope), /Incomplete/)
|
|
assert.throws(() => projectChrWire(capture().subarray(0, 84), scope), /Both exporters/)
|
|
for (const mutate of [
|
|
(bytes: Buffer) => bytes.writeUInt32LE(1, 20),
|
|
(bytes: Buffer) => bytes.writeUInt16BE(0x2000, 46),
|
|
(bytes: Buffer) => { bytes[59] = 101 },
|
|
(bytes: Buffer) => bytes.writeUInt16BE(9999, 62),
|
|
]) {
|
|
const bytes = capture()
|
|
mutate(bytes)
|
|
assert.throws(() => projectChrWire(bytes, scope))
|
|
}
|
|
})
|