Files
MikrotikManager/backend/src/services/traffic-flow-overlay-model.test.ts
T

39 lines
3.9 KiB
TypeScript

import assert from "node:assert/strict"
import { overlayMatchesDesired } from "./traffic-flow-overlay-reconcile.js"
import { ipfixFields } from "./traffic-flow-overlay-execution.js"
import { overlayPeerEndpoint, type OverlayState } from "./traffic-flow-overlay-state.js"
const server={id:126,name:"router",host:"192.0.2.126",mgmtTunnelIp:"",updatedAt:"before"}
const router={"/interface":[{".id":"*E",name:"ether1",type:"ether"}],"/interface/wireguard":[],"/interface/wireguard/peers":[],"/ip/address":[],"/ip/route":[],"/ip/firewall/filter":[],"/ip/traffic-flow":[{".id":"*F",enabled:"false"}],"/ip/traffic-flow/ipfix":[{".id":"*I",bytes:"false"}],"/ip/traffic-flow/target":[],"/interface/gre":[{".id":"*G","allow-fast-path":"true"}]}
const settings={prefix:"10.255.254.0/24",collectorIp:"10.255.254.1",flowListenPort:4739,hasHostPrivateKey:true,hasHostPublicKey:true,hostPrivateKeyProof:"hostprivate",hostPublicKeyProof:"hostpublic",peersJson:[],enabled:false}
const before={server,catalog:[server],settings,router,configuration:{agentSocketProof:"agent",listenerHostProof:"host"},proof:"before"} as unknown as OverlayState
const after=structuredClone(before)
after.catalog[0].mgmtTunnelIp="10.255.254.127";after.catalog[0].updatedAt="after"
after.settings.enabled=true
after.settings.peersJson=[{serverId:126,name:"router",publicKeyProof:"routerpublic",allowedIps:["10.255.254.127/32"],address:"10.255.254.127",endpoint:"router.example:13232"}]
after.router["/interface"]!.push({".id":"*W",name:"wg-flow",type:"wg"})
after.router["/interface/wireguard"]=[{".id":"*W",name:"wg-flow","listen-port":"13232",mtu:"1420",comment:"MikrotikManager traffic-flow overlay","public-key":"proof:routerpublic"}]
after.router["/interface/wireguard/peers"]=[{".id":"*P",interface:"wg-flow","public-key":"proof:hostpublic","allowed-address":"10.255.254.1/32",comment:"MM traffic-flow collector",name:"mm-collector"}]
after.router["/ip/address"]=[{".id":"*A",interface:"wg-flow",address:"10.255.254.127/24"}]
after.router["/ip/route"]=[{".id":"*R","dst-address":"10.255.254.1/32",gateway:"wg-flow"}]
after.router["/ip/firewall/filter"]=[{".id":"*B",chain:"input",protocol:"udp","dst-port":"13232",action:"accept",comment:"mm-wg-flow"}]
after.router["/ip/traffic-flow"]=[{".id":"*F",enabled:"true",interfaces:"all","cache-entries":"256k","active-flow-timeout":"1m","inactive-flow-timeout":"15s"}]
after.router["/ip/traffic-flow/ipfix"]=[{".id":"*I",...Object.fromEntries(Object.keys(ipfixFields).map(key=>[key,"true"]))}]
after.router["/ip/traffic-flow/target"]=[{".id":"*T","dst-address":"10.255.254.1","src-address":"0.0.0.0",port:"4739",version:"ipfix"}]
after.router["/interface/gre"]=[{".id":"*G","allow-fast-path":"false"}]
const opts={publicEndpoint:"router.example",disableGreFastPath:true}
assert.equal(overlayMatchesDesired(before,after,opts),true)
for(const change of [
(state:OverlayState)=>{state.catalog[0].host="192.0.2.200"},
(state:OverlayState)=>{state.settings.hostPrivateKeyProof="rotated"},
(state:OverlayState)=>{state.router["/interface"]![0].comment="foreign drift"},
(state:OverlayState)=>{state.router["/interface/wireguard/peers"]![0]["endpoint-address"]="unexpected.example"},
(state:OverlayState)=>{state.router["/ip/traffic-flow/target"]![0].port="9999"},
(state:OverlayState)=>{state.router["/ip/firewall/filter"]![0].disabled="true"},
(state:OverlayState)=>{state.router["/interface/gre"]![0]["allow-fast-path"]="true"},
(state:OverlayState)=>{state.settings.peersJson.push({serverId:999,publicKeyProof:"foreign"})},
]){const changed=structuredClone(after);change(changed);assert.equal(overlayMatchesDesired(before,changed,opts),false)}
assert.equal(overlayPeerEndpoint("2001:db8::1"),"[2001:db8::1]:13232")
assert.equal(overlayPeerEndpoint("router.example"),"router.example:13232")
console.log("traffic-flow-overlay-model.test.ts: desired native/DB graph, unrelated/key/endpoint/firewall/GRE drift and IPv6 endpoints verified")