1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
-- test script for various Lua functions
-- use with dhcp.pcap in test/captures directory
local testlib = require("testlib")
------------- general test helper funcs ------------
local FRAME = "frame"
local ETH = "eth"
local IP = "ip"
local DHCP = "dhcp"
local OTHER = "other"
local PDISS = "postdissector"
-- expected number of runs per type
-- note ip (5 tests) only runs 3 times because it gets removed
-- and dhcp (5 tests) only runs twice because the filter makes it run
-- once and then it gets replaced with a different one for the second time
local n_frames = 4
local taptests = {
[FRAME]=5*n_frames,
[ETH]=5*n_frames,
[IP]=5*3,
[DHCP]=5*2,
[OTHER]=16,
[PDISS]=n_frames,
}
testlib.init(taptests)
local pkt_fields = { [FRAME] = {}, [PDISS] = {} }
local function getAllFieldInfos(group)
local fields = { all_field_infos() }
local fieldnames = {}
for i,v in ipairs(fields) do
fieldnames[i] = v.name
end
local pktnum = testlib.getPktCount(group)
pkt_fields[group][pktnum] = { ["num"] = #fields, ["fields"] = fieldnames }
end
local function dumpAllFieldInfos()
for i,v in ipairs(pkt_fields[FRAME]) do
print("In frame tap for packet ".. i ..":")
print(" number of fields = ".. v.num)
for _,name in ipairs(v.fields) do
print(" field = ".. name)
end
local w = pkt_fields[PDISS][i]
print("In postdissector for packet ".. i ..":")
print(" number of fields = ".. w.num)
for _,name in ipairs(w.fields) do
print(" field = ".. name)
end
end
end
local function checkAllFieldInfos()
for i,v in ipairs(pkt_fields[FRAME]) do
local numfields = v.num
if numfields ~= pkt_fields[PDISS][i].num then
print("Tap and postdissector do not have same number of fields!")
return false
end
if numfields < 100 then
print("Too few fields!")
return false
end
end
return true
end
---------
-- the following are so we can use pcall (which needs a function to call)
local function makeListener(...)
local foo = Listener.new(...)
end
local function setListener(tap,name,value)
tap[name] = value
end
local function getListener(tap,name)
local foo = tap[name]
end
------------- test script ------------
testlib.testing(OTHER,"negative tests")
testlib.test(OTHER,"Listener.new-1",not pcall(makeListener,"FooBARhowdy"))
testlib.test(OTHER,"Listener.new-2",not pcall(makeListener,"ip","FooBARhowdy"))
local tmptap = Listener.new()
local func = function(...)
passed[OTHER] = 0
error("This shouldn't be called!")
end
testlib.test(OTHER,"Listener.set-3",pcall(setListener,tmptap,"packet",func))
testlib.test(OTHER,"Listener.set-4",pcall(setListener,tmptap,"reset",func))
testlib.test(OTHER,"Listener.set-5",pcall(setListener,tmptap,"draw",func))
testlib.test(OTHER,"Listener.set-6",not pcall(setListener,Listener,"packet",func))
testlib.test(OTHER,"Listener.set-7",not pcall(setListener,Listener,"reset",func))
testlib.test(OTHER,"Listener.set-8",not pcall(setListener,Listener,"draw",func))
testlib.test(OTHER,"Listener.set-9",not pcall(setListener,Listener,"foobar",func))
testlib.test(OTHER,"Listener.get-10",not pcall(getListener,tmptap,"packet",func))
testlib.test(OTHER,"Listener.get-11",not pcall(getListener,tmptap,"reset",func))
testlib.test(OTHER,"Listener.get-12",not pcall(getListener,tmptap,"draw",func))
print("removing tmptap twice")
testlib.test(OTHER,"Listener.remove-13",pcall(tmptap.remove,tmptap))
testlib.test(OTHER,"Listener.remove-14",pcall(tmptap.remove,tmptap))
testlib.test(OTHER,"typeof-15", typeof(tmptap) == "Listener")
-- declare some field extractors
local f_eth_src = Field.new("eth.src")
local f_eth_dst = Field.new("eth.dst")
local f_eth_mac = Field.new("eth.addr")
local f_ip_src = Field.new("ip.src")
local f_ip_dst = Field.new("ip.dst")
local f_dhcp_hw = Field.new("dhcp.hw.mac_addr")
local f_dhcp_opt = Field.new("dhcp.option.type")
local tap_frame = Listener.new(nil,nil,true)
local tap_eth = Listener.new("eth")
local tap_ip = Listener.new("ip","dhcp")
local tap_dhcp = Listener.new("dhcp","dhcp.option.dhcp == 1")
local second_time = false
function tap_frame.packet(pinfo,tvb,frame)
testlib.countPacket(FRAME)
testlib.testing(FRAME,"Frame")
testlib.test(FRAME,"arg-1", typeof(pinfo) == "Pinfo")
testlib.test(FRAME,"arg-2", typeof(tvb) == "Tvb")
testlib.test(FRAME,"arg-3", frame == nil)
testlib.test(FRAME,"pinfo.number-1",pinfo.number == testlib.getPktCount(FRAME))
-- check ether addr
local eth_src1 = tostring(f_eth_src().range)
local eth_src2 = tostring(tvb:range(6,6))
testlib.test(FRAME,"FieldInfo.range-1", eth_src1 == eth_src2)
getAllFieldInfos(FRAME)
end
function tap_eth.packet(pinfo,tvb,eth)
testlib.countPacket(ETH)
-- on the 4th run of eth, remove the ip one and add a new dhcp one
if testlib.getPktCount(ETH) == 4 then
testlib.testing(ETH,"removing ip tap, replacing dhcp tap")
tap_ip:remove()
tap_dhcp:remove()
tap_dhcp = Listener.new("dhcp")
tap_dhcp.packet = dhcp_packet
second_time = true
end
testlib.testing(ETH,"Eth")
testlib.test(ETH,"arg-1", typeof(pinfo) == "Pinfo")
testlib.test(ETH,"arg-2", typeof(tvb) == "Tvb")
testlib.test(ETH,"arg-3", type(eth) == "table")
testlib.test(ETH,"pinfo.number-1",pinfo.number == testlib.getPktCount(ETH))
-- check ether addr
local eth_src1 = tostring(f_eth_src().range)
local eth_src2 = tostring(tvb:range(6,6))
testlib.test(ETH,"FieldInfo.range-1", eth_src1 == eth_src2)
end
function tap_ip.packet(pinfo,tvb,ip)
testlib.countPacket(IP)
testlib.testing(IP,"IP")
testlib.test(IP,"arg-1", typeof(pinfo) == "Pinfo")
testlib.test(IP,"arg-2", typeof(tvb) == "Tvb")
testlib.test(IP,"arg-3", type(ip) == "table")
testlib.test(IP,"pinfo.number-1",pinfo.number == testlib.getPktCount(IP))
-- check ether addr
local eth_src1 = tostring(f_eth_src().range)
local eth_src2 = tostring(tvb:range(6,6))
testlib.test(IP,"FieldInfo.range-1", eth_src1 == eth_src2)
end
dhcp_packet = function (pinfo,tvb,dhcp)
testlib.countPacket(DHCP)
testlib.testing(DHCP,"DHCP")
testlib.test(DHCP,"arg-1", typeof(pinfo) == "Pinfo")
testlib.test(DHCP,"arg-2", typeof(tvb) == "Tvb")
testlib.test(DHCP,"arg-3", dhcp == nil)
if not second_time then
testlib.test(DHCP,"pinfo.number-1",pinfo.number == testlib.getPktCount(DHCP))
else
testlib.test(DHCP,"pinfo.number-1",pinfo.number == 4)
end
-- check ether addr
local eth_src1 = tostring(f_eth_src().range)
local eth_src2 = tostring(tvb:range(6,6))
testlib.test(DHCP,"FieldInfo.range-1", eth_src1 == eth_src2)
end
tap_dhcp.packet = dhcp_packet
function tap_frame.reset()
-- reset never gets called in tshark (sadly)
--[[ XXX: this is no longer the case?!
if not GUI_ENABLED then
error("reset called!!")
end
--]]
end
function tap_frame.draw()
testlib.test(OTHER,"all_field_infos", checkAllFieldInfos())
testlib.getResults()
end
-- max_gap.lua
-- create a gap.max field containing the maximum gap between two packets between two ip nodes
-- we create a "protocol" for our tree
local max_gap_p = Proto("gap","Gap in IP conversations")
-- we create our fields
local max_gap_field = ProtoField.float("gap.max")
-- we add our fields to the protocol
max_gap_p.fields = { max_gap_field }
-- then we register max_gap_p as a postdissector
register_postdissector(max_gap_p,true)
function max_gap_p.dissector(tvb,pinfo,tree)
testlib.countPacket(PDISS)
getAllFieldInfos(PDISS)
testlib.pass(PDISS)
end
|