Add 4 more test scripts for Lua, and its own testsuite menu

This adds test scripts for verifying Pinfo, Address, Field, FieldInfo, NSTime
and Listener classes/functions.  It also moves Lua test scripts out of
unittests and into its own new testsuite.

Change-Id: I65c238fd459efb96db3f8f9145842cd038dea7c7
Reviewed-on: https://code.wireshark.org/review/270
Reviewed-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
Tested-by: Alexis La Goutte <alexis.lagoutte@gmail.com>
This commit is contained in:
Hadriel Kaplan 2014-02-20 10:12:33 -05:00 committed by Alexis La Goutte
parent d574fd89f4
commit fe769e7350
7 changed files with 1086 additions and 104 deletions

140
test/lua/field.lua Normal file
View File

@ -0,0 +1,140 @@
-- test script for wslua Field/FieldInfo functions
-- use with dhcp.pcap in test/captures directory
------------- helper funcs ------------
local packet_count = 0
local function incPktCount(name)
packet_count = packet_count + 1
end
local function testing(...)
print("---- Testing "..tostring(...).." for packet #"..packet_count.." ----")
end
local function test(name, ...)
io.stdout:write("test "..name.."-"..packet_count.."...")
if (...) == true then
io.stdout:write("passed\n")
else
io.stdout:write("failed!\n")
error(name.." test failed!")
end
end
local function toMacAddr(addrhex)
return addrhex:gsub("..","%0:"):sub(1,-2)
end
-- the following are so we can use pcall (which needs a function to call)
local function makeField(name)
local foo = Field.new(name)
return true
end
local function makeFieldInfo(field)
local foo = field()
return true
end
local function setFieldInfo(finfo,name,value)
finfo[name] = value
return true
end
local function getFieldInfo(finfo,name)
local foo = finfo[name]
return true
end
--------------------------
testing("Field")
test("Field.new-0",pcall(makeField,"ip.src"))
test("Field.new-1",not pcall(makeField,"FooBARhowdy"))
test("Field.new-2",not pcall(makeField))
test("Field.new-3",not pcall(makeField,""))
test("Field.new-4",not pcall(makeField,"IP.SRC"))
-- declare some field extractors
local f_frame_proto = Field.new("frame.protocols")
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_udp_srcport = Field.new("udp.srcport")
local f_udp_dstport = Field.new("udp.dstport")
local f_bootp_hw = Field.new("bootp.hw.mac_addr")
local f_bootp_opt = Field.new("bootp.option.type")
test("Field__tostring-1", tostring(f_frame_proto) == "frame.protocols")
-- make sure can't create a FieldInfo outside tap
test("Field__call-1",not pcall(makeFieldInfo,f_eth_src))
local tap = Listener.new()
--------------------------
function tap.packet(pinfo,tvb)
incPktCount()
testing("Field")
test("Field__tostring-2", tostring(f_frame_proto) == "frame.protocols")
-- make sure can't create a Field inside tap
test("Field.new-5",not pcall(makeField,"ip.src"))
test("Field__call-2",pcall(makeFieldInfo,f_eth_src))
testing("FieldInfo")
-- check ether addr
local fi_eth_src = f_eth_src()
test("FieldInfo.range-0",pcall(getFieldInfo,fi_eth_src,"range"))
local eth_macs = { f_eth_mac() }
local eth_src1 = tostring(f_eth_src().range)
local eth_src2 = tostring(tvb:range(6,6))
local eth_src3 = tostring(eth_macs[2].tvb)
test("FieldInfo.range-1", eth_src1 == eth_src2)
test("FieldInfo.range-2", eth_src1 == eth_src3)
test("FieldInfo.range-3",not pcall(setFieldInfo,fi_eth_src,"range",3))
test("FieldInfo.generated-1", f_frame_proto().generated == true)
test("FieldInfo.generated-2", eth_macs[2].generated == false)
test("FieldInfo.generated-3",not pcall(setFieldInfo,fi_eth_src,"generated",3))
test("FieldInfo.name-1", fi_eth_src.name == "eth.src")
test("FieldInfo.name-2",not pcall(setFieldInfo,fi_eth_src,"name","3"))
test("FieldInfo.label-1", fi_eth_src.label == tostring(fi_eth_src))
test("FieldInfo.label-2", fi_eth_src.label == toMacAddr(eth_src1))
test("FieldInfo.label-3",not pcall(setFieldInfo,fi_eth_src,"label","3"))
test("FieldInfo.display-1", select(1, string.find(fi_eth_src.display, toMacAddr(eth_src1))) ~= nil)
test("FieldInfo.display-2",not pcall(setFieldInfo,fi_eth_src,"display","3"))
test("FieldInfo.eq-1", eth_macs[2] == select(2, f_eth_mac()))
test("FieldInfo.eq-2", eth_macs[1] ~= fi_eth_src)
test("FieldInfo.eq-3", eth_macs[1] == f_eth_dst())
test("FieldInfo.offset-1", eth_macs[1].offset == 0)
test("FieldInfo.offset-2", -fi_eth_src == 6)
test("FieldInfo.offset-3",not pcall(setFieldInfo,fi_eth_src,"offset","3"))
test("FieldInfo.len-1", fi_eth_src.len == 6)
test("FieldInfo.len-2",not pcall(setFieldInfo,fi_eth_src,"len",6))
if packet_count == 4 then
print("\n-----------------------------\n")
print("All tests passed!\n\n")
end
end

233
test/lua/listener.lua Normal file
View File

@ -0,0 +1,233 @@
-- test script for various Lua functions
-- use with dhcp.pcap in test/captures directory
------------- general test helper funcs ------------
local FRAME = "frame"
local ETH = "eth"
local IP = "ip"
local BOOTP = "bootp"
local OTHER = "other"
local packet_counts = {}
local function incPktCount(name)
if not packet_counts[name] then
packet_counts[name] = 1
else
packet_counts[name] = packet_counts[name] + 1
end
end
local function getPktCount(name)
return packet_counts[name] or 0
end
local passed = {}
local function setPassed(name)
if not passed[name] then
passed[name] = 1
else
passed[name] = passed[name] + 1
end
end
-- expected number of runs per type
-- note ip only runs 3 times because it gets removed
-- and bootp only runs twice because the filter makes it run
-- once and then it gets replaced with a different one for the second time
local taptests = { [FRAME]=4, [ETH]=4, [IP]=3, [BOOTP]=2, [OTHER]=15 }
local function getResults()
print("\n-----------------------------\n")
for k,v in pairs(taptests) do
if passed[k] ~= v then
print("Something didn't run or ran too much... tests failed!")
print("Listener type "..k.." expected: "..v..", but got: "..tostring(passed[k]))
return false
end
end
print("All tests passed!\n\n")
return true
end
local function testing(type,...)
print("---- Testing "..type.." ---- "..tostring(...).." for packet # "..getPktCount(type).." ----")
end
local function test(type,name, ...)
io.stdout:write("test "..type.."-->"..name.."-"..getPktCount(type).."...")
if (...) == true then
io.stdout:write("passed\n")
return true
else
io.stdout:write("failed!\n")
error(name.." test failed!")
end
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 ------------
testing(OTHER,"negative tests")
local orig_test = test
test = function (...)
if orig_test(OTHER,...) then
setPassed(OTHER)
end
end
test("Listener.new-1",not pcall(makeListener,"FooBARhowdy"))
test("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
test("Listener.set-3",pcall(setListener,tmptap,"packet",func))
test("Listener.set-4",pcall(setListener,tmptap,"reset",func))
test("Listener.set-5",pcall(setListener,tmptap,"draw",func))
test("Listener.set-6",not pcall(setListener,Listener,"packet",func))
test("Listener.set-7",not pcall(setListener,Listener,"reset",func))
test("Listener.set-8",not pcall(setListener,Listener,"draw",func))
test("Listener.set-9",not pcall(setListener,Listener,"foobar",func))
test("Listener.get-10",not pcall(getListener,tmptap,"packet",func))
test("Listener.get-11",not pcall(getListener,tmptap,"reset",func))
test("Listener.get-12",not pcall(getListener,tmptap,"draw",func))
print("removing tmptap twice")
test("Listener.remove-13",pcall(tmptap.remove,tmptap))
test("Listener.remove-14",pcall(tmptap.remove,tmptap))
test("typeof-15", typeof(tmptap) == "Listener")
-- revert to original test function
test = orig_test
-- 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_bootp_hw = Field.new("bootp.hw.mac_addr")
local f_bootp_opt = Field.new("bootp.option.type")
local tap_frame = Listener.new()
local tap_eth = Listener.new("eth")
local tap_ip = Listener.new("ip","bootp")
local tap_bootp = Listener.new("bootp","bootp.option.dhcp == 1")
local second_time = false
function tap_frame.packet(pinfo,tvb,frame)
incPktCount(FRAME)
testing(FRAME,"Frame")
test(FRAME,"arg-1", typeof(pinfo) == "Pinfo")
test(FRAME,"arg-2", typeof(tvb) == "Tvb")
test(FRAME,"arg-3", frame == nil)
test(FRAME,"pinfo.number-1",pinfo.number == getPktCount(FRAME))
-- check ether addr
local eth_src1 = tostring(f_eth_src().range)
local eth_src2 = tostring(tvb:range(6,6))
test(FRAME,"FieldInfo.range-1", eth_src1 == eth_src2)
setPassed(FRAME)
end
function tap_eth.packet(pinfo,tvb,eth)
incPktCount(ETH)
-- on the 4th run of eth, remove the ip one and add a new bootp one
if getPktCount(ETH) == 4 then
testing(ETH,"removing ip tap, replacing bootp tap")
tap_ip:remove()
tap_bootp:remove()
tap_bootp = Listener.new("bootp")
tap_bootp.packet = bootp_packet
second_time = true
end
testing(ETH,"Eth")
test(ETH,"arg-1", typeof(pinfo) == "Pinfo")
test(ETH,"arg-2", typeof(tvb) == "Tvb")
test(ETH,"arg-3", type(eth) == "table")
test(ETH,"pinfo.number-1",pinfo.number == getPktCount(ETH))
-- check ether addr
local eth_src1 = tostring(f_eth_src().range)
local eth_src2 = tostring(tvb:range(6,6))
test(ETH,"FieldInfo.range-1", eth_src1 == eth_src2)
setPassed(ETH)
end
function tap_ip.packet(pinfo,tvb,ip)
incPktCount(IP)
testing(IP,"IP")
test(IP,"arg-1", typeof(pinfo) == "Pinfo")
test(IP,"arg-2", typeof(tvb) == "Tvb")
test(IP,"arg-3", type(ip) == "table")
test(IP,"pinfo.number-1",pinfo.number == getPktCount(IP))
-- check ether addr
local eth_src1 = tostring(f_eth_src().range)
local eth_src2 = tostring(tvb:range(6,6))
test(IP,"FieldInfo.range-1", eth_src1 == eth_src2)
setPassed(IP)
end
bootp_packet = function (pinfo,tvb,bootp)
incPktCount(BOOTP)
testing(BOOTP,"Bootp")
test(BOOTP,"arg-1", typeof(pinfo) == "Pinfo")
test(BOOTP,"arg-2", typeof(tvb) == "Tvb")
test(BOOTP,"arg-3", bootp == nil)
if not second_time then
test(BOOTP,"pinfo.number-1",pinfo.number == getPktCount(BOOTP))
else
test(BOOTP,"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))
test(BOOTP,"FieldInfo.range-1", eth_src1 == eth_src2)
setPassed(BOOTP)
end
tap_bootp.packet = bootp_packet
function tap_frame.reset()
-- reset never gets called in tshark (sadly)
error("reset called!!")
end
function tap_frame.draw()
getResults()
end

190
test/lua/nstime.lua Normal file
View File

@ -0,0 +1,190 @@
-- test script for various Lua functions
-- use with dhcp.pcap in test/captures directory
------------- general test helper funcs ------------
local FRAME = "frame"
local OTHER = "other"
local packet_counts = {}
local function incPktCount(name)
if not packet_counts[name] then
packet_counts[name] = 1
else
packet_counts[name] = packet_counts[name] + 1
end
end
local function getPktCount(name)
return packet_counts[name] or 0
end
local passed = {}
local function setPassed(name)
if not passed[name] then
passed[name] = 1
else
passed[name] = passed[name] + 1
end
end
-- expected number of runs per type
-- note ip only runs 3 times because it gets removed
-- and bootp only runs twice because the filter makes it run
-- once and then it gets replaced with a different one for the second time
local taptests = { [FRAME]=4, [OTHER]=37 }
local function getResults()
print("\n-----------------------------\n")
for k,v in pairs(taptests) do
if passed[k] ~= v then
print("Something didn't run or ran too much... tests failed!")
print("Listener type "..k.." expected: "..v..", but got: "..tostring(passed[k]))
return false
end
end
print("All tests passed!\n\n")
return true
end
local function testing(type,...)
print("---- Testing "..type.." ---- "..tostring(...).." for packet # "..getPktCount(type).."----")
end
local function test(type,name, ...)
io.stdout:write("test "..type.."-->"..name.."-"..getPktCount(type).."...")
if (...) == true then
io.stdout:write("passed\n")
return true
else
io.stdout:write("failed!\n")
error(name.." test failed!")
end
end
---------
-- the following are so we can use pcall (which needs a function to call)
local function makeNSTime(...)
local foo = NSTime(...)
end
local function setNSTime(nst,name,value)
nst[name] = value
end
local function getNSTime(nst,name)
local foo = nst[name]
end
------------- test script ------------
testing(OTHER,"negative tests")
local orig_test = test
test = function (...)
if orig_test(OTHER,...) then
setPassed(OTHER)
end
end
test("NSTime.new-1",not pcall(makeNSTime,"FooBARhowdy"))
test("NSTime.new-2",not pcall(makeNSTime,"ip","FooBARhowdy"))
local tmptime = NSTime()
test("NSTime.set-3",pcall(setNSTime,tmptime,"secs",10))
test("NSTime.set-4",not pcall(setNSTime,tmptime,"foobar",1000))
test("NSTime.set-5",pcall(setNSTime,tmptime,"nsecs",123))
test("NSTime.set-6",not pcall(setNSTime,NSTime,"secs",0))
test("NSTime.set-7",not pcall(setNSTime,tmptime,"secs","foobar"))
test("NSTime.set-8",not pcall(setNSTime,NSTime,"nsecs",0))
test("NSTime.set-9",not pcall(setNSTime,tmptime,"nsecs","foobar"))
test("NSTime.get-10",pcall(getNSTime,tmptime,"secs"))
test("NSTime.get-11",pcall(getNSTime,tmptime,"nsecs"))
test("NSTime.get-12",not pcall(getNSTime,NSTime,"secs"))
test("NSTime.get-13",not pcall(getNSTime,NSTime,"nsecs"))
testing(OTHER,"basic tests")
local first = NSTime()
local second = NSTime(100,100)
local third = NSTime(0,100)
test("NSTime.secs-14", first.secs == 0)
test("NSTime.secs-15", second.secs == 100)
test("NSTime.secs-16", third.secs == 0)
test("NSTime.nsecs-17", first.nsecs == 0)
test("NSTime.nsecs-18", second.nsecs == 100)
test("NSTime.nsecs-19", third.nsecs == 100)
test("NSTime.eq-20", first == NSTime())
test("NSTime.neq-21", second ~= third)
test("NSTime.add-22", first + second == second)
test("NSTime.add-23", third + NSTime(100,0) == second)
test("NSTime.add-24", NSTime(100) + NSTime(nil,100) == second)
test("NSTime.lt-25", third < second)
test("NSTime.gt-26", third > first)
test("NSTime.le-27", second <= NSTime(100,100))
test("NSTime.unm-28", -first == first)
test("NSTime.unm-29", -(-second) == second)
test("NSTime.unm-30", -second == NSTime(-100,-100))
test("NSTime.unm-31", -third == NSTime(0,-100))
test("NSTime.tostring-32", tostring(first) == "0.000000000")
test("NSTime.tostring-33", tostring(second) == "100.000000100")
test("NSTime.tostring-34", tostring(third) == "0.000000100")
testing(OTHER,"setters/getters")
first.secs = 123
first.nsecs = 100
test("NSTime.set-35", first == NSTime(123,100))
test("NSTime.get-36", first.secs == 123)
test("NSTime.get-37", first.nsecs == 100)
----------------------------------
-- revert to original test function, kinda sorta
test = function (...)
return orig_test(FRAME,...)
end
-- declare some field extractors
local f_frame_time = Field.new("frame.time")
local f_frame_time_rel = Field.new("frame.time_relative")
local f_frame_time_delta = Field.new("frame.time_delta")
local tap = Listener.new()
local begin = NSTime()
local now, previous
function tap.packet(pinfo,tvb,frame)
incPktCount(FRAME)
testing(FRAME,"NSTime in Frame")
local fi_now = f_frame_time()
local fi_rel = f_frame_time_rel()
local fi_delta = f_frame_time_delta()
test("typeof-1", typeof(begin) == "NSTime")
test("typeof-2", typeof(fi_now()) == "NSTime")
now = fi_now()
if getPktCount(FRAME) == 1 then
test("__eq-1", begin == fi_delta())
test("NSTime.secs-1", fi_delta().secs == 0)
test("NSTime.nsecs-1", fi_delta().nsecs == 0)
begin = fi_now()
else
test("__sub__eq-1", now - previous == fi_delta())
test("__sub__eq-2", now - begin == fi_rel())
test("__add-1", (previous - begin) + (now - previous) == fi_rel())
end
previous = now
setPassed(FRAME)
end
function tap.draw()
getResults()
end

301
test/lua/pinfo.lua Normal file
View File

@ -0,0 +1,301 @@
-- test script for Pinfo and Address functions
-- use with dhcp.pcap in test/captures directory
local major, minor, micro = get_version():match("(%d+)%.(%d+)%.(%d+)")
if major then
major = tonumber(major)
minor = tonumber(minor)
micro = tonumber(micro)
else
major = 99
minor = 99
micro = 99
end
------------- general test helper funcs ------------
local FRAME = "frame"
local OTHER = "other"
local packet_counts = {}
local function incPktCount(name)
if not packet_counts[name] then
packet_counts[name] = 1
else
packet_counts[name] = packet_counts[name] + 1
end
end
local function getPktCount(name)
return packet_counts[name] or 0
end
local passed = {}
local function setPassed(name)
if not passed[name] then
passed[name] = 1
else
passed[name] = passed[name] + 1
end
end
-- expected number of runs per type
-- note ip only runs 3 times because it gets removed
-- and bootp only runs twice because the filter makes it run
-- once and then it gets replaced with a different one for the second time
local taptests = { [FRAME]=4, [OTHER]=0 }
local function getResults()
print("\n-----------------------------\n")
for k,v in pairs(taptests) do
if v ~= 0 and passed[k] ~= v then
print("Something didn't run or ran too much... tests failed!")
print("Listener type "..k.." expected: "..v..", but got: "..tostring(passed[k]))
return false
end
end
print("All tests passed!\n\n")
return true
end
local function testing(type,...)
print("---- Testing "..type.." ---- "..tostring(...).." for packet # "..getPktCount(type).." ----")
end
local function test(type,name, ...)
io.stdout:write("test "..type.."-->"..name.."-"..getPktCount(type).."...")
if (...) == true then
io.stdout:write("passed\n")
return true
else
io.stdout:write("failed!\n")
error(name.." test failed!")
end
end
---------
-- the following are so we can use pcall (which needs a function to call)
local function setPinfo(pinfo,name,value)
pinfo[name] = value
end
local function getPinfo(pinfo,name)
local foo = pinfo[name]
end
------------- test script ------------
----------------------------------
-- modify original test function, kinda sorta
local orig_test = test
test = function (...)
return orig_test(FRAME,...)
end
local tap = Listener.new()
function tap.packet(pinfo,tvb)
incPktCount(FRAME)
testing(FRAME,"Pinfo in Frame")
test("typeof-1", typeof(pinfo) == "Pinfo")
test("tostring-1", tostring(pinfo) == "a Pinfo")
testing(FRAME,"negative tests")
-- try to set read-only attributes
--[[
these tests *should* ALL pass, but currently pinfo read-only members
silently accept being set (though nothing happens) Blech!!
test("Pinfo.number-set-1",not pcall(setPinfo,pinfo,"number",0))
test("Pinfo.len-set-1",not pcall(setPinfo,pinfo,"len",0))
test("Pinfo.caplen-set-1",not pcall(setPinfo,pinfo,"caplen",0))
test("Pinfo.rel_ts-set-1",not pcall(setPinfo,pinfo,"rel_ts",0))
test("Pinfo.delta_ts-set-1",not pcall(setPinfo,pinfo,"delta_ts",0))
test("Pinfo.delta_dis_ts-set-1",not pcall(setPinfo,pinfo,"delta_dis_ts",0))
test("Pinfo.visited-set-1",not pcall(setPinfo,pinfo,"visited",0))
test("Pinfo.lo-set-1",not pcall(setPinfo,pinfo,"lo",0))
test("Pinfo.hi-set-1",not pcall(setPinfo,pinfo,"hi",0))
test("Pinfo.port_type-set-1",not pcall(setPinfo,pinfo,"port_type",0))
test("Pinfo.ipproto-set-1",not pcall(setPinfo,pinfo,"ipproto",0))
test("Pinfo.match-set-1",not pcall(setPinfo,pinfo,"match",0))
test("Pinfo.curr_proto-set-1",not pcall(setPinfo,pinfo,"curr_proto",0))
test("Pinfo.columns-set-1",not pcall(setPinfo,pinfo,"columns",0))
test("Pinfo.cols-set-1",not pcall(setPinfo,pinfo,"cols",0))
test("Pinfo.private_data-set-1",not pcall(setPinfo,pinfo,"private_data",0))
test("Pinfo.private-set-1",not pcall(setPinfo,pinfo,"private",0))
test("Pinfo.fragmented-set-1",not pcall(setPinfo,pinfo,"fragmented",0))
test("Pinfo.in_error_pkt-set-1",not pcall(setPinfo,pinfo,"in_error_pkt",0))
test("Pinfo.match_uint-set-1",not pcall(setPinfo,pinfo,"match_uint",0))
test("Pinfo.match_string-set-1",not pcall(setPinfo,pinfo,"match_string",0))
]]
-- wrong type being set
test("Pinfo.src-set-1",not pcall(setPinfo,pinfo,"src","foobar"))
test("Pinfo.dst-set-1",not pcall(setPinfo,pinfo,"dst","foobar"))
test("Pinfo.dl_src-set-1",not pcall(setPinfo,pinfo,"dl_src","foobar"))
test("Pinfo.dl_dst-set-1",not pcall(setPinfo,pinfo,"dl_dst","foobar"))
test("Pinfo.net_src-set-1",not pcall(setPinfo,pinfo,"net_src","foobar"))
test("Pinfo.net_dst-set-1",not pcall(setPinfo,pinfo,"net_dst","foobar"))
test("Pinfo.src_port-set-1",not pcall(setPinfo,pinfo,"src_port","foobar"))
test("Pinfo.dst_port-set-1",not pcall(setPinfo,pinfo,"dst_port","foobar"))
test("Pinfo.circuit_id-set-1",not pcall(setPinfo,pinfo,"circuit_id","foobar"))
if major > 1 or minor > 10 then
test("Pinfo.can_desegment-set-1",not pcall(setPinfo,pinfo,"can_desegment","foobar"))
end
test("Pinfo.desegment_len-set-1",not pcall(setPinfo,pinfo,"desegment_len","foobar"))
test("Pinfo.desegment_offset-set-1",not pcall(setPinfo,pinfo,"desegment_offset","foobar"))
-- invalid attribute names
--[[
again, these *should* pass, but Pinfo silently allows it!
test("Pinfo.set-1",not pcall(setPinfo,pinfo,"foobar","foobar"))
test("Pinfo.get-12",not pcall(getPinfo,pinfo,"foobar"))
]]
testing(FRAME,"basic getter tests")
local pktlen, srcip, dstip, srcport, dstport
if pinfo.number == 1 or pinfo.number == 3 then
pktlen = 314
srcip = "0.0.0.0"
dstip = "255.255.255.255"
srcport = 68
dstport = 67
else
pktlen = 342
srcip = "192.168.0.1"
dstip = "192.168.0.10"
srcport = 67
dstport = 68
end
test("Pinfo.number-get-1",pinfo.number == getPktCount(FRAME))
test("Pinfo.len-get-1",pinfo.len == pktlen)
test("Pinfo.caplen-get-1",pinfo.caplen == pktlen)
test("Pinfo.visited-get-1",pinfo.visited == true)
test("Pinfo.lo-get-1",tostring(pinfo.lo) == srcip)
test("Pinfo.lo-get-2",typeof(pinfo.lo) == "Address")
test("Pinfo.hi-get-1",tostring(pinfo.hi) == dstip)
test("Pinfo.hi-get-2",typeof(pinfo.hi) == "Address")
test("Pinfo.port_type-get-1",pinfo.port_type == 3)
test("Pinfo.ipproto-get-1",pinfo.ipproto == 17)
test("Pinfo.match-get-1",pinfo.match == 0)
test("Pinfo.curr_proto-get-1",tostring(pinfo.curr_proto) == "<Missing Protocol Name>")
test("Pinfo.columns-get-1",tostring(pinfo.columns) == "Columns")
test("Pinfo.columns-get-2",typeof(pinfo.columns) == "Columns")
test("Pinfo.cols-get-1",tostring(pinfo.cols) == "Columns")
test("Pinfo.cols-get-2",typeof(pinfo.cols) == "Columns")
test("Pinfo.private_data-get-1",type(pinfo.private_data) == "userdata")
test("Pinfo.private-get-1",type(pinfo.private) == "userdata")
test("Pinfo.fragmented-get-1",pinfo.fragmented == false)
test("Pinfo.in_error_pkt-get-1",pinfo.in_error_pkt == false)
test("Pinfo.match_uint-get-1",pinfo.match_uint == 0)
test("Pinfo.match_string-get-1",pinfo.match_string == nil)
test("Pinfo.src-get-1",tostring(pinfo.src) == srcip)
test("Pinfo.src-get-2",typeof(pinfo.src) == "Address")
test("Pinfo.dst-get-1",tostring(pinfo.dst) == dstip)
test("Pinfo.dst-get-2",typeof(pinfo.dst) == "Address")
test("Pinfo.dl_src-get-1",typeof(pinfo.dl_src) == "Address")
test("Pinfo.dl_dst-get-1",typeof(pinfo.dl_dst) == "Address")
test("Pinfo.net_src-get-1",tostring(pinfo.net_src) == srcip)
test("Pinfo.net_src-get-2",typeof(pinfo.net_src) == "Address")
test("Pinfo.net_dst-get-1",tostring(pinfo.net_dst) == dstip)
test("Pinfo.net_dst-get-2",typeof(pinfo.net_dst) == "Address")
test("Pinfo.src_port-get-1",pinfo.src_port == srcport)
test("Pinfo.dst_port-get-1",pinfo.dst_port == dstport)
test("Pinfo.circuit_id-get-1",pinfo.circuit_id == 0)
if major > 1 or minor > 10 then
test("Pinfo.can_desegment-get-1",pinfo.can_desegment == 0)
end
test("Pinfo.desegment_len-get-1",pinfo.desegment_len == 0)
test("Pinfo.desegment_offset-get-1",pinfo.desegment_offset == 0)
if pinfo.number == 1 then
test("Pinfo.rel_ts-get-1",pinfo.rel_ts == 0)
test("Pinfo.delta_ts-get-1",pinfo.delta_ts == 0)
test("Pinfo.delta_dis_ts-get-1",pinfo.delta_dis_ts == 0)
elseif pinfo.number == 2 then
test("Pinfo.rel_ts-get-1",pinfo.rel_ts == 0.000295)
test("Pinfo.delta_ts-get-1",pinfo.delta_ts == 0.000295)
test("Pinfo.delta_dis_ts-get-1",pinfo.delta_dis_ts == 0.000295)
elseif pinfo.number == 3 then
test("Pinfo.rel_ts-get-1",pinfo.rel_ts == 0.070031)
test("Pinfo.delta_ts-get-1",pinfo.delta_ts == 0.069736)
test("Pinfo.delta_dis_ts-get-1",pinfo.delta_dis_ts == 0.069736)
elseif pinfo.number == 4 then
test("Pinfo.rel_ts-get-1",pinfo.rel_ts == 0.070345)
test("Pinfo.delta_ts-get-1",pinfo.delta_ts == 0.000314)
test("Pinfo.delta_dis_ts-get-1",pinfo.delta_dis_ts == 0.000314)
end
testing(FRAME,"basic setter tests")
local tmp = pinfo.src
pinfo.src = pinfo.dst
pinfo.dst = tmp
test("Pinfo.src-set-1",tostring(pinfo.src) == dstip)
test("Pinfo.src-set-1",typeof(pinfo.src) == "Address")
test("Pinfo.dst-set-1",tostring(pinfo.dst) == srcip)
test("Pinfo.dst-set-1",typeof(pinfo.dst) == "Address")
local dl_dst_val = tostring(pinfo.dl_dst)
local dl_src_val = tostring(pinfo.dl_src)
tmp = pinfo.dl_src
pinfo.dl_src = pinfo.dl_dst
pinfo.dl_dst = tmp
test("Pinfo.dl_src-set-1",tostring(pinfo.dl_src) == dl_dst_val)
test("Pinfo.dl_dst-set-1",tostring(pinfo.dl_dst) == dl_src_val)
tmp = pinfo.net_src
pinfo.net_src = pinfo.net_dst
pinfo.net_dst = tmp
test("Pinfo.net_src-set-1",tostring(pinfo.net_src) == dstip)
test("Pinfo.net_src-set-1",typeof(pinfo.net_src) == "Address")
test("Pinfo.net_dst-set-1",tostring(pinfo.net_dst) == srcip)
test("Pinfo.net_dst-set-1",typeof(pinfo.net_dst) == "Address")
--[[
--there's a bug 9792 causing the pinfo.dst_port setter to actually set src_port
tmp = pinfo.src_port
pinfo.src_port = pinfo.dst_port
pinfo.dst_port = tmp
test("Pinfo.src_port-set-1",pinfo.src_port == dstport)
test("Pinfo.dst_port-set-1",pinfo.dst_port == srcport)
--]]
pinfo.src_port = pinfo.dst_port
test("Pinfo.src_port-set-1",pinfo.src_port == dstport)
pinfo.circuit_id = 42
test("Pinfo.circuit_id-set-1",pinfo.circuit_id == 42)
if major > 1 or minor > 10 then
pinfo.can_desegment = 12
test("Pinfo.can_desegment-set-1",pinfo.can_desegment == 12)
end
pinfo.desegment_len = 34
test("Pinfo.desegment_len-set-1",pinfo.desegment_len == 34)
pinfo.desegment_offset = 45
test("Pinfo.desegment_offset-set-1",pinfo.desegment_offset == 45)
testing(FRAME,"Address functions")
test("Address-eq-1", pinfo.lo == pinfo.dst)
test("Address-eq-2", pinfo.lo ~= pinfo.hi)
test("Address-lt-1", pinfo.lo < pinfo.hi)
test("Address-lt-2", pinfo.hi > pinfo.lo)
test("Address-le-1", pinfo.lo <= pinfo.hi)
test("Address-le-2", pinfo.lo <= pinfo.dst)
setPassed(FRAME)
end
function tap.draw()
getResults()
end

View File

@ -69,105 +69,6 @@ unittests_step_exntest() {
unittests_step_test
}
unittests_step_lua_dissector_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# First run tshark with the dissector script.
$TSHARK -r $CAPTURE_DIR/dns_port.pcap -V -X lua_script:$TESTS_DIR/lua/dissector.lua > testin.txt 2>&1
RETURNVALUE=$?
if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
echo
cat ./testin_tmp.txt
test_step_failed "exit status of $DUT: $RETURNVALUE"
return
fi
# then run tshark again with the verification script. (it internally reads in testin.txt)
$TSHARK -r $CAPTURE_DIR/dns_port.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
echo
cat ./testin.txt
cat ./testout.txt
test_step_failed "didn't find pass marker"
fi
}
unittests_step_lua_int64_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/int64.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
echo
cat ./testout.txt
test_step_failed "didn't find pass marker"
fi
}
unittests_step_lua_args_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:1 > testout.txt 2>&1
grep -q "All tests passed!" testout.txt
if [ $? -ne 0 ]; then
cat testout.txt
test_step_failed "lua_args_test test 1 failed"
fi
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 -X lua_script1:foo -X lua_script1:bar > testout.txt 2>&1
grep -q "All tests passed!" testout.txt
if [ $? -ne 0 ]; then
cat testout.txt
test_step_failed "lua_args_test test 2 failed"
fi
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 -X lua_script2:1 -X lua_script1:foo -X lua_script1:bar > testout.txt 2>&1
grep -q "All tests passed!" testout.txt
if [ $? -ne 0 ]; then
cat testout.txt
test_step_failed "lua_args_test test 3 failed"
fi
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
cat testout.txt
test_step_failed "lua_args_test negative test 4 failed"
fi
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
cat testout.txt
test_step_failed "lua_args_test negative test 5 failed"
fi
test_step_ok
}
unittests_step_lua_struct_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/struct.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
cat testout.txt
test_step_failed "didn't find pass marker"
fi
}
unittests_step_oids_test() {
DUT=$SOURCE_DIR/epan/oids_test
ARGS=
@ -194,17 +95,12 @@ unittests_step_wmem_test() {
unittests_cleanup_step() {
rm -f ./testout.txt
rm -f ./testin.txt
}
unittests_suite() {
test_step_set_pre unittests_cleanup_step
test_step_set_post unittests_cleanup_step
test_step_add "exntest" unittests_step_exntest
test_step_add "lua dissector" unittests_step_lua_dissector_test
test_step_add "lua int64" unittests_step_lua_int64_test
test_step_add "lua script arguments" unittests_step_lua_args_test
test_step_add "lua struct" unittests_step_lua_struct_test
test_step_add "oids_test" unittests_step_oids_test
test_step_add "reassemble_test" unittests_step_reassemble_test
test_step_add "tvbtest" unittests_step_tvbtest

216
test/suite-wslua.sh Normal file
View File

@ -0,0 +1,216 @@
#!/bin/bash
#
# Run the epan unit tests
#
# $Id$
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <gerald@wireshark.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
wslua_step_dissector_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# First run tshark with the dissector script.
$TSHARK -r $CAPTURE_DIR/dns_port.pcap -V -X lua_script:$TESTS_DIR/lua/dissector.lua > testin.txt 2>&1
RETURNVALUE=$?
if [ ! $RETURNVALUE -eq $EXIT_OK ]; then
echo
cat ./testin.txt
test_step_failed "exit status of $DUT: $RETURNVALUE"
return
fi
# then run tshark again with the verification script. (it internally reads in testin.txt)
$TSHARK -r $CAPTURE_DIR/dns_port.pcap -X lua_script:$TESTS_DIR/lua/verify_dissector.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
echo
cat ./testin.txt
cat ./testout.txt
test_step_failed "didn't find pass marker"
fi
}
wslua_step_field_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/field.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
cat testout.txt
test_step_failed "didn't find pass marker"
fi
}
wslua_step_listener_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/listener.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
cat testout.txt
test_step_failed "didn't find pass marker"
fi
}
wslua_step_nstime_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/nstime.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
cat testout.txt
test_step_failed "didn't find pass marker"
fi
}
wslua_step_pinfo_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/nstime.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
cat testout.txt
test_step_failed "didn't find pass marker"
fi
}
wslua_step_int64_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/int64.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
echo
cat ./testout.txt
test_step_failed "didn't find pass marker"
fi
}
wslua_step_args_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:1 > testout.txt 2>&1
grep -q "All tests passed!" testout.txt
if [ $? -ne 0 ]; then
cat testout.txt
test_step_failed "lua_args_test test 1 failed"
fi
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 -X lua_script1:foo -X lua_script1:bar > testout.txt 2>&1
grep -q "All tests passed!" testout.txt
if [ $? -ne 0 ]; then
cat testout.txt
test_step_failed "lua_args_test test 2 failed"
fi
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 -X lua_script2:1 -X lua_script1:foo -X lua_script1:bar > testout.txt 2>&1
grep -q "All tests passed!" testout.txt
if [ $? -ne 0 ]; then
cat testout.txt
test_step_failed "lua_args_test test 3 failed"
fi
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
cat testout.txt
test_step_failed "lua_args_test negative test 4 failed"
fi
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/script_args.lua -X lua_script1:3 > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
cat testout.txt
test_step_failed "lua_args_test negative test 5 failed"
fi
test_step_ok
}
wslua_step_struct_test() {
if [ $HAVE_LUA -ne 0 ]; then
test_step_skipped
return
fi
# Tshark catches lua script failures, so we have to parse the output.
$TSHARK -r $CAPTURE_DIR/dhcp.pcap -X lua_script:$TESTS_DIR/lua/struct.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok
else
cat testout.txt
test_step_failed "didn't find pass marker"
fi
}
wslua_cleanup_step() {
rm -f ./testout.txt
rm -f ./testin.txt
}
wslua_suite() {
test_step_set_pre wslua_cleanup_step
test_step_set_post wslua_cleanup_step
test_step_add "wslua dissector" wslua_step_dissector_test
test_step_add "wslua field/fieldinfo" wslua_step_field_test
test_step_add "wslua int64" wslua_step_int64_test
test_step_add "wslua listener" wslua_step_listener_test
test_step_add "wslua nstime" wslua_step_nstime_test
test_step_add "wslua pinfo" wslua_step_pinfo_test
test_step_add "wslua script arguments" wslua_step_args_test
test_step_add "wslua struct" wslua_step_struct_test
}
#
# Editor modelines - http://www.wireshark.org/tools/modelines.html
#
# Local variables:
# c-basic-offset: 8
# tab-width: 8
# indent-tabs-mode: t
# End:
#
# vi: set shiftwidth=8 tabstop=8 noexpandtab:
# :indentSize=8:tabSize=8:noTabs=false:
#

View File

@ -58,6 +58,7 @@ Usage: $THIS [-c] [-h] [-s <suite>]
nameres
prerequisites
unittests
wslua
FIN
exit 0
fi
@ -96,6 +97,7 @@ source $TESTS_DIR/suite-unittests.sh
source $TESTS_DIR/suite-fileformats.sh
source $TESTS_DIR/suite-decryption.sh
source $TESTS_DIR/suite-nameres.sh
source $TESTS_DIR/suite-wslua.sh
test_cleanup() {
if [ $TEST_OUTDIR_CLEAN = 1 ]; then
@ -155,6 +157,7 @@ test_suite() {
test_suite_add "File formats" fileformats_suite
test_suite_add "Decryption" decryption_suite
test_suite_add "Name Resolution" name_resolution_suite
test_suite_add "Lua API" wslua_suite
}
@ -200,6 +203,9 @@ if [ -n "$RUN_SUITE" ] ; then
"unittests")
test_suite_run "Unit tests" unittests_suite
exit $? ;;
"wslua")
test_suite_run "Lua API" wslua_suite
exit $? ;;
esac
fi