-
Notifications
You must be signed in to change notification settings - Fork 12
/
pcap-test
executable file
·98 lines (69 loc) · 2.21 KB
/
pcap-test
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
#!/usr/bin/env lua5.1
local pcap = require "pcap"
require "netutil"
local function gc()
for _=1,5 do
collectgarbage()
end
end
function assertfail(...)
--print("assertfail", ...)
local ok, emsg = pcall(...)
--print("pcall", ok, emsg)
assert(ok == false, tostring(ok)..","..tostring(emsg))
assert(emsg)
return emsg
end
function assertnext(cap, _pkt, _stamp, _wirelen)
local pkt, stamp, wirelen = cap:next()
print("want", _pkt and h(_pkt) or "nil", _stamp, _wirelen)
print("next", pkt and h(pkt) or "nil", stamp, wirelen)
if _pkt then assert(_pkt == pkt) end
if _stamp then assert(_stamp == stamp) end
if _wirelen then assert(_wirelen == wirelen) end
end
assert(pcap)
print"test: tv to secs conversions"
for _, tvsecs in ipairs{0, 1, 2, 3, 4, 5, 100, 788964454} do
for tvusecs=0,1000 do
local secs = pcap.tv2secs(tvsecs, tvusecs)
local _tvsecs, _tvusecs = pcap.secs2tv(secs)
if tvusecs ~= _tvusecs or tvsecs ~= _tvsecs then
if tvsecs == 0 then
print("tv in", tvsecs, tvusecs, "secs", string.format("%.8f", secs), "tv out", _tvsecs, _tvusecs)
end
end
assert(tvsecs == _tvsecs)
assert(tvusecs == _tvusecs, "diff is "..(tvusecs - _tvusecs))
end
end
print"+ok"
print"pcap test"
print("pcap lib version", assert(pcap._LIB_VERSION))
cap = assert(pcap.open_dead())
dmp = assert(cap:dump_open("cap0.pcap"))
eth = b"112233445566aabbccddeeff080045" -- eth header + 1 byte of ipv4
assert(dmp:dump(eth, 1.000010, #eth - 1 + 20))
assert(dmp:dump(eth, 2.000020))
assert(dmp:dump(eth))
assertfail(dmp.dump, dmp)
assertfail(dmp.dump, dmp, nil)
_, emsg = dmp:dump(nil, "msg")
assert(_ == nil)
assert(emsg == "msg")
cap:close()
assert(dmp:dump("pcap closed"))
dmp:close()
cap = assert(pcap.open_offline("cap0.pcap"))
assertnext(cap, eth, 1.000010, #eth - 1 + 20)
assertnext(cap, eth, 2.000020, #eth)
assertnext(cap, eth, nil, #eth)
assertnext(cap, "pcap closed", nil, #"pcap closed")
assertnext(cap, nil, nil, nil)
print"dlt table test"
assert(pcap.DLT)
assert(pcap.DLT.EN10MB == 1)
assert(pcap.DLT.LINUX_SLL == 113)
assert(pcap.DLT[1] == "EN10MB")
assert(pcap.DLT[113] == "LINUX_SLL")
print"+ok"