1 -- test script for various Lua functions
2 -- use with dhcp.pcap in test/captures directory
4 local testlib
= require("testlib")
6 ------------- general test helper funcs ------------
12 local PDISS
= "postdissector"
14 -- expected number of runs per type
15 -- note ip (5 tests) only runs 3 times because it gets removed
16 -- and dhcp (5 tests) only runs twice because the filter makes it run
17 -- once and then it gets replaced with a different one for the second time
27 testlib
.init(taptests
)
29 local pkt_fields
= { [FRAME
] = {}, [PDISS
] = {} }
30 local function getAllFieldInfos(group
)
31 local fields
= { all_field_infos() }
33 for i
,v
in ipairs(fields
) do
34 fieldnames
[i
] = v
.name
36 local pktnum
= testlib
.getPktCount(group
)
37 pkt_fields
[group
][pktnum
] = { ["num"] = #fields
, ["fields"] = fieldnames
}
40 local function dumpAllFieldInfos()
41 for i
,v
in ipairs(pkt_fields
[FRAME
]) do
42 print("In frame tap for packet ".. i
..":")
43 print(" number of fields = ".. v
.num
)
44 for _
,name
in ipairs(v
.fields
) do
45 print(" field = ".. name
)
47 local w
= pkt_fields
[PDISS
][i
]
48 print("In postdissector for packet ".. i
..":")
49 print(" number of fields = ".. w
.num
)
50 for _
,name
in ipairs(w
.fields
) do
51 print(" field = ".. name
)
56 local function checkAllFieldInfos()
57 for i
,v
in ipairs(pkt_fields
[FRAME
]) do
58 local numfields
= v
.num
59 if numfields
~= pkt_fields
[PDISS
][i
].num
then
60 print("Tap and postdissector do not have same number of fields!")
63 if numfields
< 100 then
64 print("Too few fields!")
73 -- the following are so we can use pcall (which needs a function to call)
74 local function makeListener(...)
75 local foo
= Listener
.new(...)
78 local function setListener(tap
,name
,value
)
82 local function getListener(tap
,name
)
86 ------------- test script ------------
87 testlib
.testing(OTHER
,"negative tests")
88 testlib
.test(OTHER
,"Listener.new-1",not pcall(makeListener
,"FooBARhowdy"))
89 testlib
.test(OTHER
,"Listener.new-2",not pcall(makeListener
,"ip","FooBARhowdy"))
90 local tmptap
= Listener
.new()
91 local func
= function(...)
93 error("This shouldn't be called!")
95 testlib
.test(OTHER
,"Listener.set-3",pcall(setListener
,tmptap
,"packet",func
))
96 testlib
.test(OTHER
,"Listener.set-4",pcall(setListener
,tmptap
,"reset",func
))
97 testlib
.test(OTHER
,"Listener.set-5",pcall(setListener
,tmptap
,"draw",func
))
98 testlib
.test(OTHER
,"Listener.set-6",not pcall(setListener
,Listener
,"packet",func
))
99 testlib
.test(OTHER
,"Listener.set-7",not pcall(setListener
,Listener
,"reset",func
))
100 testlib
.test(OTHER
,"Listener.set-8",not pcall(setListener
,Listener
,"draw",func
))
101 testlib
.test(OTHER
,"Listener.set-9",not pcall(setListener
,Listener
,"foobar",func
))
103 testlib
.test(OTHER
,"Listener.get-10",not pcall(getListener
,tmptap
,"packet",func
))
104 testlib
.test(OTHER
,"Listener.get-11",not pcall(getListener
,tmptap
,"reset",func
))
105 testlib
.test(OTHER
,"Listener.get-12",not pcall(getListener
,tmptap
,"draw",func
))
107 print("removing tmptap twice")
108 testlib
.test(OTHER
,"Listener.remove-13",pcall(tmptap
.remove,tmptap
))
109 testlib
.test(OTHER
,"Listener.remove-14",pcall(tmptap
.remove,tmptap
))
111 testlib
.test(OTHER
,"typeof-15", typeof(tmptap
) == "Listener")
114 -- declare some field extractors
115 local f_eth_src
= Field
.new("eth.src")
116 local f_eth_dst
= Field
.new("eth.dst")
117 local f_eth_mac
= Field
.new("eth.addr")
118 local f_ip_src
= Field
.new("ip.src")
119 local f_ip_dst
= Field
.new("ip.dst")
120 local f_dhcp_hw
= Field
.new("dhcp.hw.mac_addr")
121 local f_dhcp_opt
= Field
.new("dhcp.option.type")
123 local tap_frame
= Listener
.new(nil,nil,true)
124 local tap_eth
= Listener
.new("eth")
125 local tap_ip
= Listener
.new("ip","dhcp")
126 local tap_dhcp
= Listener
.new("dhcp","dhcp.option.dhcp == 1")
128 local second_time
= false
130 function tap_frame
.packet(pinfo
,tvb
,frame
)
131 testlib
.countPacket(FRAME
)
132 testlib
.testing(FRAME
,"Frame")
134 testlib
.test(FRAME
,"arg-1", typeof(pinfo
) == "Pinfo")
135 testlib
.test(FRAME
,"arg-2", typeof(tvb
) == "Tvb")
136 testlib
.test(FRAME
,"arg-3", frame
== nil)
138 testlib
.test(FRAME
,"pinfo.number-1",pinfo
.number == testlib
.getPktCount(FRAME
))
141 local eth_src1
= tostring(f_eth_src().range
)
142 local eth_src2
= tostring(tvb
:range(6,6))
143 testlib
.test(FRAME
,"FieldInfo.range-1", eth_src1
== eth_src2
)
145 getAllFieldInfos(FRAME
)
148 function tap_eth
.packet(pinfo
,tvb
,eth
)
149 testlib
.countPacket(ETH
)
151 -- on the 4th run of eth, remove the ip one and add a new dhcp one
152 if testlib
.getPktCount(ETH
) == 4 then
153 testlib
.testing(ETH
,"removing ip tap, replacing dhcp tap")
156 tap_dhcp
= Listener
.new("dhcp")
157 tap_dhcp
.packet
= dhcp_packet
161 testlib
.testing(ETH
,"Eth")
163 testlib
.test(ETH
,"arg-1", typeof(pinfo
) == "Pinfo")
164 testlib
.test(ETH
,"arg-2", typeof(tvb
) == "Tvb")
165 testlib
.test(ETH
,"arg-3", type(eth
) == "table")
167 testlib
.test(ETH
,"pinfo.number-1",pinfo
.number == testlib
.getPktCount(ETH
))
170 local eth_src1
= tostring(f_eth_src().range
)
171 local eth_src2
= tostring(tvb
:range(6,6))
172 testlib
.test(ETH
,"FieldInfo.range-1", eth_src1
== eth_src2
)
175 function tap_ip
.packet(pinfo
,tvb
,ip
)
176 testlib
.countPacket(IP
)
177 testlib
.testing(IP
,"IP")
179 testlib
.test(IP
,"arg-1", typeof(pinfo
) == "Pinfo")
180 testlib
.test(IP
,"arg-2", typeof(tvb
) == "Tvb")
181 testlib
.test(IP
,"arg-3", type(ip
) == "table")
183 testlib
.test(IP
,"pinfo.number-1",pinfo
.number == testlib
.getPktCount(IP
))
186 local eth_src1
= tostring(f_eth_src().range
)
187 local eth_src2
= tostring(tvb
:range(6,6))
188 testlib
.test(IP
,"FieldInfo.range-1", eth_src1
== eth_src2
)
191 dhcp_packet
= function (pinfo
,tvb
,dhcp
)
192 testlib
.countPacket(DHCP
)
193 testlib
.testing(DHCP
,"DHCP")
195 testlib
.test(DHCP
,"arg-1", typeof(pinfo
) == "Pinfo")
196 testlib
.test(DHCP
,"arg-2", typeof(tvb
) == "Tvb")
197 testlib
.test(DHCP
,"arg-3", dhcp
== nil)
199 if not second_time
then
200 testlib
.test(DHCP
,"pinfo.number-1",pinfo
.number == testlib
.getPktCount(DHCP
))
202 testlib
.test(DHCP
,"pinfo.number-1",pinfo
.number == 4)
206 local eth_src1
= tostring(f_eth_src().range
)
207 local eth_src2
= tostring(tvb
:range(6,6))
208 testlib
.test(DHCP
,"FieldInfo.range-1", eth_src1
== eth_src2
)
210 tap_dhcp
.packet
= dhcp_packet
212 function tap_frame
.reset()
213 -- reset never gets called in tshark (sadly)
214 --[[ XXX: this is no longer the case?!
215 if not GUI_ENABLED then
216 error("reset called!!")
221 function tap_frame
.draw()
222 testlib
.test(OTHER
,"all_field_infos", checkAllFieldInfos())
227 -- create a gap.max field containing the maximum gap between two packets between two ip nodes
229 -- we create a "protocol" for our tree
230 local max_gap_p
= Proto("gap","Gap in IP conversations")
232 -- we create our fields
233 local max_gap_field
= ProtoField
.float("gap.max")
235 -- we add our fields to the protocol
236 max_gap_p
.fields
= { max_gap_field
}
238 -- then we register max_gap_p as a postdissector
239 register_postdissector(max_gap_p
,true)
240 function max_gap_p
.dissector(tvb
,pinfo
,tree
)
241 testlib
.countPacket(PDISS
)
242 getAllFieldInfos(PDISS
)