Files
MikrotikManager/backend/src/services/traffic-flow-template-lifetime.test.ts
T

270 lines
14 KiB
TypeScript

import assert from "node:assert/strict"
import { readFileSync } from "node:fs"
import { test } from "node:test"
import { runInNewContext } from "node:vm"
import ts from "typescript"
import { FLOW_TEMPLATE_LIFETIME_MS as lifetime, FlowTemplateRefused, parseFlowPacket, resetFlowTemplatesForTests, templateCacheUsageForTests } from "./traffic-flow-parse.js"
type Version = 9 | 10
const transport = { sourcePort: 2055, collectorSocketId: "owned_clock" }
function packet(version: Version, set: number, body: Buffer, domain = 0) {
const header = version === 9 ? 20 : 16, value = Buffer.alloc(header + 4 + body.length)
value.writeUInt16BE(version); value.writeUInt16BE(version === 9 ? 1 : value.length, 2)
value.writeUInt32BE(domain, version === 9 ? 16 : 12)
value.writeUInt16BE(set, header); value.writeUInt16BE(body.length + 4, header + 2); body.copy(value, header + 4)
return value
}
function template(version: Version, id = 256, domain = 0) {
const body = Buffer.from([1, 0, 0, 2, 0, 8, 0, 4, 0, 12, 0, 4]); body.writeUInt16BE(id)
return packet(version, version === 9 ? 0 : 2, body, domain)
}
const data = (version: Version, id = 256, domain = 0) => packet(version, id, Buffer.from([192, 0, 2, 1, 198, 51, 100, 1]), domain)
const parse = (value: Buffer) => parseFlowPacket(value, "known", transport)
const unavailable = (work: () => unknown) => assert.throws(work, error => error instanceof FlowTemplateRefused && error.reason === "missing-template")
test("v9 refuses old layout after same-port exporter reboot until a new template arrives", t => {
resetFlowTemplatesForTests()
t.after(resetFlowTemplatesForTests)
const at = (value: Buffer, seconds: number, uptime: number) => {
value.writeUInt32BE(uptime, 4)
value.writeUInt32BE(seconds, 8)
return value
}
const old = at(template(9), 10000, 1200000)
parse(old)
parse(at(template(9, 256, 1), 10000, 1200000))
assert.equal(parse(at(data(9), 10001, 1201000))[0].src, "192.0.2.1")
unavailable(() => parse(at(data(9), 10002, 500)))
const replacement = packet(9, 0, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4]))
parse(at(replacement, 10003, 1500))
parse(old) // A delayed template from the previous boot must not replace the new layout.
assert.equal(parse(at(data(9), 10004, 2500))[0].src, "198.51.100.1")
unavailable(() => parse(at(data(9), 10001, 1201000)))
assert.equal(parse(at(data(9, 256, 1), 10001, 1201000))[0].src, "192.0.2.1")
parse(at(template(9, 257), 20000, 0xfffffe0c))
assert.equal(parse(at(data(9, 257), 20001, 500))[0].src, "192.0.2.1")
})
test("v9 rejects incompatible clocks even within one second or after new uptime passes the old template", t => {
resetFlowTemplatesForTests()
t.after(resetFlowTemplatesForTests)
const at = (value: Buffer, seconds: number, uptime: number) => {
value.writeUInt32BE(uptime, 4)
value.writeUInt32BE(seconds, 8)
return value
}
const newLayout = () => packet(9, 0, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4]))
parse(at(template(9), 10000, 120000))
assert.equal(parse(at(data(9), 10000, 120500))[0].src, "192.0.2.1")
unavailable(() => parse(at(data(9), 10000, 500)))
parse(at(newLayout(), 10000, 500))
parse(at(template(9), 10000, 120000))
unavailable(() => parse(at(data(9), 10001, 1500)))
parse(at(newLayout(), 10001, 1500))
assert.equal(parse(at(data(9), 10002, 2500))[0].src, "198.51.100.1")
parse(at(template(9, 257), 20000, 10000))
unavailable(() => parse(at(data(9, 257), 20100, 20000)))
parse(at(template(9, 257), 20100, 20000))
assert.equal(parse(at(data(9, 257), 20101, 21000))[0].src, "192.0.2.1")
parse(at(template(9, 258), 30000, 0xffffff00))
assert.equal(parse(at(data(9, 258), 30000, 200))[0].src, "192.0.2.1")
assert.equal(parse(at(data(9, 258), 29999, 0xfffffb18))[0].src, "192.0.2.1")
})
test("v9 retains the clock of a layout change across refresh and refuses older data and templates", t => {
resetFlowTemplatesForTests()
t.after(resetFlowTemplatesForTests)
const at = (value: Buffer, seconds: number, uptime: number, sequence: number) => {
value.writeUInt32BE(uptime, 4)
value.writeUInt32BE(seconds, 8)
value.writeUInt32BE(sequence, 12)
return value
}
const newLayout = () => packet(9, 0, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4]))
parse(at(template(9), 10000, 100000, 0xfffffffd))
parse(at(template(9, 256, 1), 10000, 100000, 1))
parse(at(newLayout(), 10000, 100500, 0xffffffff))
unavailable(() => parse(at(data(9), 10000, 100250, 0xfffffffe)))
unavailable(() => parse(at(data(9), 10000, 100500, 0xfffffffe)))
parse(at(template(9), 10000, 100500, 0xfffffffe))
assert.equal(parse(at(data(9), 10000, 100500, 0))[0].src, "198.51.100.1")
parse(at(newLayout(), 10001, 101000, 1))
assert.equal(parse(at(data(9), 10000, 100500, 0))[0].src, "198.51.100.1")
unavailable(() => parse(at(data(9), 9999, 99500, 0xfffffffc)))
assert.equal(parse(at(data(9, 256, 1), 10001, 101000, 2))[0].src, "192.0.2.1")
})
test("IPFIX rejects delayed data from before a layout change and ignores delayed templates", t => {
resetFlowTemplatesForTests()
t.after(resetFlowTemplatesForTests)
const at = (value: Buffer, seconds: number) => {
value.writeUInt32BE(seconds, 4)
return value
}
parse(at(template(10), 100))
const replacement = packet(10, 2, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4]))
parse(at(replacement, 200))
parse(at(template(10), 150))
unavailable(() => parse(at(data(10), 160)))
parse(at(replacement, 210))
assert.equal(parse(at(data(10), 205))[0].src, "198.51.100.1")
})
test("IPFIX sequence regression with newer export time waits for a fresh template", t => {
resetFlowTemplatesForTests()
t.after(resetFlowTemplatesForTests)
const at = (value: Buffer, seconds: number, sequence = 0) => {
value.writeUInt32BE(seconds, 4)
value.writeUInt32BE(sequence, 8)
return value
}
parse(at(template(10), 100))
parse(at(template(10, 257), 100))
parse(at(template(10, 256, 1), 100))
assert.equal(parse(at(data(10), 110, 1000)).length, 1)
assert.equal(parse(at(data(10), 109, 999)).length, 1, "Delayed older data does not reset the clock")
unavailable(() => parse(at(data(10, 257), 111, 0)))
unavailable(() => parse(at(data(10), 112, 1)))
assert.equal(parse(at(data(10, 256, 1), 111, 0)).length, 1, "The sibling domain stays usable")
parse(at(template(10), 110))
parse(at(template(10), 111))
unavailable(() => parse(at(data(10), 112, 2)))
parse(at(template(10), 112))
assert.equal(parse(at(data(10), 112, 2)).length, 1)
unavailable(() => parse(at(data(10, 257), 113, 3)))
unavailable(() => parse(at(data(10), 110, 1000)))
parse(at(template(10, 257), 114))
assert.equal(parse(at(data(10, 257), 114, 3)).length, 1)
parse(at(template(10, 257, 2), 120))
assert.equal(parse(at(data(10, 257, 2), 121, 0xffffffff)).length, 1)
assert.equal(parse(at(data(10, 257, 2), 122, 0)).length, 1)
})
test("IPFIX accepts a fresh definition followed by data in the regression datagram", t => {
resetFlowTemplatesForTests()
t.after(resetFlowTemplatesForTests)
const at = (value: Buffer, seconds: number, sequence = 0) => {
value.writeUInt32BE(seconds, 4)
value.writeUInt32BE(sequence, 8)
return value
}
parse(at(template(10), 100))
parse(at(template(10, 257), 100))
parse(at(data(10), 110, 1000))
const replacement = packet(10, 2, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4]))
const mixed = Buffer.concat([replacement, data(10).subarray(16)])
mixed.writeUInt16BE(mixed.length, 2)
assert.equal(parse(at(mixed, 111))[0].src, "198.51.100.1")
unavailable(() => parse(at(data(10), 111, 1)))
assert.equal(parse(at(data(10), 112, 1))[0].src, "198.51.100.1")
unavailable(() => parse(at(data(10, 257), 112, 2)))
const reversed = Buffer.concat([data(10), replacement.subarray(16)])
reversed.writeUInt16BE(reversed.length, 2)
parse(at(data(10), 113, 2000))
unavailable(() => parse(at(reversed, 114)))
unavailable(() => parse(at(data(10), 115, 1)))
})
test("IPFIX with conflicting definitions in one second needs a strictly newer template", t => {
resetFlowTemplatesForTests()
t.after(resetFlowTemplatesForTests)
const at = (value: Buffer, seconds: number) => {
value.writeUInt32BE(seconds, 4)
return value
}
const replacement = packet(10, 2, Buffer.from([1, 0, 0, 2, 0, 12, 0, 4, 0, 8, 0, 4]))
parse(at(template(10), 100))
parse(at(replacement, 100))
unavailable(() => parse(at(data(10), 100)))
unavailable(() => parse(at(data(10), 101)))
parse(at(template(10), 100))
unavailable(() => parse(at(data(10), 101)))
parse(at(template(10), 102))
unavailable(() => parse(at(data(10), 101)))
assert.equal(parse(at(data(10), 102))[0].src, "192.0.2.1")
parse(at(replacement, 103))
assert.equal(parse(at(data(10), 103))[0].src, "198.51.100.1")
})
for (const version of [9, 10] as const) {
test(`v${version}: exact monotonic expiry refuses repeated data until a real template refresh`, t => {
let now = 100; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests()
parse(template(version)); now += lifetime - 1
assert.equal(parse(data(version))[0].src, "192.0.2.1")
now++; unavailable(() => parse(data(version))); unavailable(() => parse(data(version)))
assert.equal(templateCacheUsageForTests("known").exporterTemplates, 0)
assert.equal(templateCacheUsageForTests().fields, 0)
parse(template(version)); assert.equal(parse(data(version))[0].dst, "198.51.100.1")
resetFlowTemplatesForTests()
})
test(`v${version}: data never extends lifetime; accepted refresh does; malformed refresh does not`, t => {
let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests()
parse(template(version)); now = lifetime - 1; parse(data(version)); parse(template(version))
now = lifetime; assert.equal(parse(data(version)).length, 1)
now = 2 * lifetime - 2
const malformed = packet(version, version === 9 ? 0 : 2, Buffer.from([1, 0, 0, 1]))
assert.throws(() => parse(malformed), error => error instanceof FlowTemplateRefused && error.reason === "truncated")
assert.equal(parse(data(version)).length, 1)
now++; unavailable(() => parse(data(version))); resetFlowTemplatesForTests()
})
test(`v${version}: expiry releases exact field budgets and preserves freshly renewed neighbour`, t => {
let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests()
parse(template(version)); parse(template(version, 257)); now = lifetime / 2; parse(template(version, 257))
now = lifetime; unavailable(() => parse(data(version)))
assert.equal(parse(data(version, 257)).length, 1)
assert.equal(templateCacheUsageForTests().templates, 1); assert.equal(templateCacheUsageForTests().fields, 2)
parse(template(version, 258)); assert.equal(templateCacheUsageForTests().templates, 2)
assert.equal(templateCacheUsageForTests().fields, 4)
now = 3 * lifetime; parse(template(version, 259))
assert.equal(templateCacheUsageForTests().templates, 1); assert.equal(templateCacheUsageForTests().fields, 2)
resetFlowTemplatesForTests()
})
}
test("IPFIX UDP withdrawals neither delete nor refresh retained template lifetime", t => {
let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests()
parse(template(10)); now = lifetime - 1
for (const set of [2, 3]) parse(packet(10, set, Buffer.from([1, 0, 0, 0])))
assert.equal(parse(data(10)).length, 1)
now++; unavailable(() => parse(data(10))); resetFlowTemplatesForTests()
})
test("template expiry remains isolated across protocol/domain/actual port/receiver/legacy partitions", t => {
let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests()
parse(template(10)); now = lifetime / 2
const others = [
{ packet: template(9), data: data(9), context: transport },
{ packet: template(10, 256, 1), data: data(10, 256, 1), context: transport },
{ packet: template(10), data: data(10), context: { ...transport, sourcePort: 2056 } },
{ packet: template(10), data: data(10), context: { ...transport, collectorSocketId: "replacement" } },
{ packet: template(10), data: data(10), context: undefined },
]
for (const other of others) parseFlowPacket(other.packet, "known", other.context)
now = lifetime; unavailable(() => parse(data(10)))
for (const other of others) assert.equal(parseFlowPacket(other.data, "known", other.context).length, 1)
assert.equal(templateCacheUsageForTests("known").exporterTemplates, 5); resetFlowTemplatesForTests()
})
test("real ingress body exposes missing/expired template as refusal and never queues a fabricated zero", t => {
let now = 0; t.mock.method(performance, "now", () => now); resetFlowTemplatesForTests()
const source = readFileSync(new URL("./traffic-flow-engine.ts", import.meta.url), "utf8")
const ast = ts.createSourceFile("engine.ts", source, ts.ScriptTarget.ES2022, true)
const ingress = ast.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === "ingestDatagram")!
const code = ts.transpileModule(ingress.getText(ast), { compilerOptions: { module: ts.ModuleKind.CommonJS } }).outputText
let error = ""; const queued: unknown[] = [], exports: { ingestDatagram?: (value: Buffer, exporter: string, context: typeof transport) => boolean } = {}
runInNewContext(code, { exports, Buffer, FlowTemplateRefused, parseFlowPacket, bumpPacketMeta() {}, resolveServerId: () => 1,
setEngineError: (value: string) => { error = value }, maybeRefreshIfaces() {}, queueParsedFlows: (_id: number, value: unknown) => queued.push(value) })
const ingest = (value: Buffer) => exports.ingestDatagram!(value, "known", transport)
assert.equal(ingest(template(10)), true); assert.equal(ingest(data(10)), true); assert.equal(queued.length, 1)
now = lifetime
for (let attempt = 0; attempt < 2; attempt++) {
assert.equal(ingest(data(10)), false); assert.match(error, /missing-template/); assert.equal(queued.length, 1)
}
assert.equal(ingest(template(10)), true); assert.match(error, /missing-template/, "Template alone does not claim data coverage")
assert.equal(ingest(data(10)), true); assert.equal(error, ""); assert.equal(queued.length, 2); resetFlowTemplatesForTests()
})