43 lines
2.6 KiB
TypeScript
43 lines
2.6 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { createServer } from "node:http"
|
|
import { randomUUID } from "node:crypto"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import { callFlowAgent, emptyHostStatus, type FlowAgentSnapshot } from "./traffic-flow-agent-client.js"
|
|
|
|
const socket = process.platform === "win32" ? `\\\\.\\pipe\\flow-agent-${randomUUID()}` : join(tmpdir(), `flow-agent-${randomUUID()}.sock`)
|
|
const snapshot: FlowAgentSnapshot = {
|
|
collectorIp: "10.255.254.1", prefix: "10.255.254.0/24", flowListenPort: 4739, wgListenPort: 51821,
|
|
hostPublicKey: "public-fixture", hostPrivateKey: "private-fixture", peers: [],
|
|
}
|
|
let mode = "ok"
|
|
const server = createServer((req, res) => {
|
|
let body = ""
|
|
req.on("data", chunk => { body += chunk })
|
|
req.on("end", () => {
|
|
assert.equal(req.url, "/check"); assert.equal(req.method, "POST")
|
|
assert.deepEqual(JSON.parse(body), snapshot)
|
|
if (mode === "conflict") { res.writeHead(409); res.end(JSON.stringify({ error: "Конфликт адресов" })); return }
|
|
if (mode === "invalid") { res.end("not-json"); return }
|
|
if (mode === "oversize") { res.end("x".repeat(300_000)); return }
|
|
res.end(JSON.stringify({
|
|
interfaceUp: true, configMatches: true, publicKey: "public-fixture", listenPort: 51821,
|
|
addresses: ["10.255.254.1/24"], peers: [{ publicKey: "router-fixture", allowedIps: ["10.255.254.2/32"], lastHandshakeAt: null, receivedBytes: 0, sentBytes: 0 }],
|
|
privateKey: "must-not-be-exposed",
|
|
}))
|
|
})
|
|
})
|
|
await new Promise<void>((resolve, reject) => { server.once("error", reject); server.listen(socket, resolve) })
|
|
try {
|
|
const status = await callFlowAgent("/check", snapshot, socket)
|
|
assert.equal(status.reachable, true); assert.equal(status.configMatches, true)
|
|
assert.equal(JSON.stringify(status).includes("must-not-be-exposed"), false)
|
|
assert.equal(status.peers[0].lastHandshakeAt, null)
|
|
mode = "conflict"; await assert.rejects(callFlowAgent("/check", snapshot, socket), /Конфликт адресов/)
|
|
mode = "invalid"; await assert.rejects(callFlowAgent("/check", snapshot, socket), /некорректный ответ/)
|
|
mode = "oversize"; await assert.rejects(callFlowAgent("/check", snapshot, socket), /некорректный ответ/)
|
|
await assert.rejects(callFlowAgent("/status", undefined, ""), /не настроена/)
|
|
assert.equal(emptyHostStatus(true, "offline").configMatches, null)
|
|
} finally { await new Promise<void>(resolve => server.close(() => resolve())) }
|
|
console.log("NetFlow agent IPC: request, validated status, secret stripping, conflict, invalid/oversize response, manual mode")
|