aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/field.lua
blob: 2e9d1c62bb96b0e2f4058254a15dc06d5283dbe9 (plain)
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
-- 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")

test("Field.name-1", f_frame_proto.name == "frame.protocols")
test("Field.name-2", f_eth_src.name == "eth.src")

test("Field.display-1", f_frame_proto.display == "Protocols in frame")
test("Field.display-2", f_eth_src.display == "Source")

test("Field.type-1", f_frame_proto.type == ftypes.STRING)
test("Field.type-2", f_eth_src.type == ftypes.ETHER)
test("Field.type-3", f_ip_src.type == ftypes.IPv4)
test("Field.type-4", f_udp_srcport.type == ftypes.UINT16)
test("Field.type-5", f_bootp_opt.type == ftypes.UINT8)

-- 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))

    test("Field.name-3", f_frame_proto.name == "frame.protocols")
    test("Field.name-4", f_eth_src.name == "eth.src")

    test("Field.display-3", f_frame_proto.display == "Protocols in frame")
    test("Field.display-4", f_eth_src.display == "Source")

    test("Field.type-6", f_frame_proto.type == ftypes.STRING)
    test("Field.type-7", f_eth_src.type == ftypes.ETHER)
    test("Field.type-8", f_ip_src.type == ftypes.IPv4)
    test("Field.type-9", f_udp_srcport.type == ftypes.UINT16)
    test("Field.type-10", f_bootp_opt.type == ftypes.UINT8)

    testing("FieldInfo")

    local finfo_udp_srcport = f_udp_srcport()
    test("FieldInfo.name-1", finfo_udp_srcport.name == "udp.srcport")
    test("FieldInfo.type-1", finfo_udp_srcport.type == ftypes.UINT16)
    test("FieldInfo.little_endian-1", finfo_udp_srcport.little_endian == false)
    -- the following should be true, but UDP doesn't set it right?
    -- test("FieldInfo.big_endian-1", finfo_udp_srcport.big_endian == true)
    test("FieldInfo.is_url-1", finfo_udp_srcport.is_url == false)
    test("FieldInfo.offset-1", finfo_udp_srcport.offset == 34)
    test("FieldInfo.source-1", finfo_udp_srcport.source == tvb)

    -- check ether addr
    local fi_eth_src = f_eth_src()
    test("FieldInfo.type-2", fi_eth_src.type == ftypes.ETHER)
    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