From 84cbf0df831063c543c4c1885fa6c92aa96ca3cc Mon Sep 17 00:00:00 2001 From: Alexander Gall Date: Thu, 26 Jul 2018 17:21:55 +0200 Subject: [PATCH] apps.ipfix: fix selftests --- src/apps/ipfix/ipfix.lua | 14 +++++++++----- src/apps/ipfix/template.lua | 12 +++++++++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/apps/ipfix/ipfix.lua b/src/apps/ipfix/ipfix.lua index 8b8ead847c..e80518e901 100644 --- a/src/apps/ipfix/ipfix.lua +++ b/src/apps/ipfix/ipfix.lua @@ -615,7 +615,9 @@ function selftest() local ipfix = IPFIX:new({ exporter_ip = "192.168.1.2", collector_ip = "192.168.1.1", collector_port = 4739, - flush_timeout = 0 }) + flush_timeout = 0, + scan_time = 1}) + ipfix.shm = shm.create_frame("apps/ipfix", ipfix.shm) -- Mock input and output. local input_name, input = new_internal_link('ipfix selftest input') @@ -719,10 +721,12 @@ function selftest() -- Template message; no data yet. assert(link.nreadable(output) == 1) - -- Cause expiry. By default we do 1e-5th of the table per push, - -- so this should be good. - for i=1,2e5 do ipfix:push() end - -- Template message and data message. + -- Wait for a full scan of the table to complete (1 second, + -- "scan_time") + local now = engine.now() + while engine.now() - now < 1 do + ipfix:push() + end assert(link.nreadable(output) == 2) local filter = require("pf").compile_filter([[ diff --git a/src/apps/ipfix/template.lua b/src/apps/ipfix/template.lua index 9e8d43045d..a3e21800c5 100644 --- a/src/apps/ipfix/template.lua +++ b/src/apps/ipfix/template.lua @@ -783,7 +783,8 @@ function selftest() local function test(src_ip, dst_ip, src_port, dst_port) local is_ipv6 = not not src_ip:match(':') - local proto = is_ipv6 and ethertype_ipv6 or ethertype_ipv4 + local proto = is_ipv6 and consts.ethertype_ipv6 or + consts.ethertype_ipv4 local eth = ether:new({ src = ether:pton("00:11:22:33:44:55"), dst = ether:pton("55:44:33:22:11:00"), type = proto }) @@ -792,9 +793,11 @@ function selftest() if is_ipv6 then ip = ipv6:new({ src = ipv6:pton(src_ip), dst = ipv6:pton(dst_ip), next_header = IP_PROTO_UDP, ttl = 64 }) + ip:payload_length(udp:sizeof()) else ip = ipv4:new({ src = ipv4:pton(src_ip), dst = ipv4:pton(dst_ip), protocol = IP_PROTO_UDP, ttl = 64 }) + ip:total_length(ip:total_length() + udp:sizeof()) end local udp = udp:new({ src_port = src_port, dst_port = dst_port }) local dg = datagram:new() @@ -804,13 +807,16 @@ function selftest() dg:push(eth) local pkt = dg:packet() + metadata.add(pkt) + local v4 = make_template_info(templates.v4) + local v6 = make_template_info(templates.v6) assert(v4.match(pkt.data, pkt.length) == not is_ipv6) assert(v6.match(pkt.data, pkt.length) == is_ipv6) local templ = is_ipv6 and v6 or v4 local entry = templ.record_t() local timestamp = 13 - templ.extract(pkt, 13, entry) + templ.extract(templ, pkt, 13, entry) if is_ipv6 then assert(ip:src_eq(entry.key.sourceIPv6Address)) assert(ip:dst_eq(entry.key.destinationIPv6Address)) @@ -824,7 +830,7 @@ function selftest() assert(entry.value.flowStartMilliseconds == timestamp) assert(entry.value.flowEndMilliseconds == timestamp) assert(entry.value.packetDeltaCount == 1) - assert(entry.value.octetDeltaCount == pkt.length - ethernet_header_size) + assert(entry.value.octetDeltaCount == pkt.length - consts.ethernet_header_size) packet.free(pkt) end