summaryrefslogtreecommitdiffstats
path: root/test/lua/protofield.lua
blob: f9ce2dc099df7ce2d4a990ada3c950b45e2d8dcd (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
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
----------------------------------------
-- script-name: protofield.lua
-- test the ProtoField API
----------------------------------------

local testlib = require("testlib")

local FRAME = "frame"
local PER_FRAME = "per-frame"
local OTHER = "other"

-- expected number of runs
local n_frames = 4
local taptests = {
    [FRAME]=n_frames,
    [PER_FRAME]=n_frames*8,
    [OTHER]=52,
}
testlib.init(taptests)

------------- test script ------------

----------------------------------------
local test_proto = Proto.new("test", "Test Proto")
test_proto.fields.time_field = ProtoField.uint16("test.time", "Time", base.UNIT_STRING, {" sec", " secs"})
test_proto.fields.dist_field = ProtoField.uint16("test.dist", "Distance", base.UNIT_STRING, {" km"})
test_proto.fields.filtered_field = ProtoField.uint16("test.filtered", "Filtered Field", base.DEC)

-- Field type: CHAR
success = pcall(ProtoField.new, "char", "test.char0", ftypes.CHAR)
testlib.test(OTHER,"ProtoField-char", success)

success = pcall(ProtoField.new, "char base NONE without valuestring", "test.char1", ftypes.CHAR, nil, base.NONE)
testlib.test(OTHER,"ProtoField-char-without-valuestring", not success)

success = pcall(ProtoField.new, "char base NONE with valuestring", "test.char2", ftypes.CHAR, {1, "Value"}, base.NONE)
testlib.test(OTHER,"ProtoField-char-with-valuestring", success)

success = pcall(ProtoField.new, "char base DEC", "test.char3", ftypes.CHAR, nil, base.DEC)
testlib.test(OTHER,"ProtoField-char-base-dec", not success)

success = pcall(ProtoField.new, "char base UNIT_STRING", "test.char4", ftypes.CHAR, {" m"}, base.UNIT_STRING)
testlib.test(OTHER,"ProtoField-char-unit-string", not success)

success = pcall(ProtoField.new, "char base RANGE_STRING", "test.char5", ftypes.CHAR, {{1, 2, "Value"}}, base.RANGE_STRING)
testlib.test(OTHER,"ProtoField-char-range-string", success)

-- Field type: BOOLEAN/UINT64 with (64 bit) mask
success = pcall(ProtoField.new, "boolean", "test.boolean0", ftypes.BOOLEAN, nil, base.HEX, 0x1)
testlib.test(OTHER,"ProtoField-new-bool-mask-trivial", success)

success = pcall(ProtoField.new, "boolean", "test.boolean1", ftypes.BOOLEAN, nil, base.HEX, "1")
testlib.test(OTHER,"ProtoField-new-bool-mask-string", success)

success = pcall(ProtoField.new, "boolean", "test.boolean2", ftypes.BOOLEAN, nil, base.HEX, UInt64(0x00000001, 0x0))
testlib.test(OTHER,"ProtoField-new-bool-mask-uint64", success)

success = pcall(ProtoField.new, "boolean", "test.boolean3", ftypes.BOOLEAN, nil, base.NONE, "invalid") -- 0
testlib.test(OTHER,"ProtoField-new-bool-mask-string-invalid", success)

success = pcall(ProtoField.new, "boolean", "test.boolean4", ftypes.BOOLEAN, nil, base.HEX, "-1") -- 0xFFFFFFFFFFFFFFFF
testlib.test(OTHER,"ProtoField-new-bool-mask-negative", success)

success = pcall(ProtoField.new, "boolean", "test.boolean5", ftypes.BOOLEAN, nil, base.NONE)
testlib.test(OTHER,"ProtoField-new-bool-mask-none", success)

success = pcall(ProtoField.new, "boolean", "test.boolean6", ftypes.BOOLEAN, nil, base.NONE, nil)
testlib.test(OTHER,"ProtoField-new-bool-mask-nil", success)

success = pcall(ProtoField.new, "boolean", "test.boolean7", ftypes.BOOLEAN, nil, base.HEX, "0x00FFFFFF00000000")
testlib.test(OTHER,"ProtoField-new-bool-mask-hex", success)

success = pcall(ProtoField.bool, "test.boolean10", nil, 64, nil, 0x1)
testlib.test(OTHER,"ProtoField-bool-mask-trivial", success)

success = pcall(ProtoField.bool, "test.boolean11", nil, 64, nil, "1")
testlib.test(OTHER,"ProtoField-bool-mask-string", success)

success = pcall(ProtoField.bool, "test.boolean12", nil, 64, nil, UInt64(0x00000001, 0x0))
testlib.test(OTHER,"ProtoField-bool-mask-uint64", success)

success = pcall(ProtoField.bool, "test.boolean13", nil, base.NONE, nil, "invalid") -- 0
testlib.test(OTHER,"ProtoField-bool-mask-string-invalid", success)

success = pcall(ProtoField.bool, "test.boolean14", nil, 64, nil, "-1") -- 0xFFFFFFFFFFFFFFFF
testlib.test(OTHER,"ProtoField-bool-mask-negative", success)

success = pcall(ProtoField.bool, "test.boolean15", nil, base.NONE, nil)
testlib.test(OTHER,"ProtoField-bool-mask-none", success)

success = pcall(ProtoField.bool, "test.boolean16", nil, base.NONE, nil, nil)
testlib.test(OTHER,"ProtoField-bool-mask-nil", success)

success = pcall(ProtoField.new, "uint64", "test.uint64_0", ftypes.UINT64, nil, base.HEX, 0x1)
testlib.test(OTHER,"ProtoField-new-uint64-mask-trivial", success)

success = pcall(ProtoField.new, "uint64", "test.uint64_1", ftypes.UINT64, nil, base.HEX, "1")
testlib.test(OTHER,"ProtoField-new-uint64-mask-string", success)

success = pcall(ProtoField.new, "uint64", "test.uint64_2", ftypes.UINT64, nil, base.HEX, UInt64(0x00000001, 0x0))
testlib.test(OTHER,"ProtoField-new-uint64-mask-uint64", success)

success = pcall(ProtoField.new, "uint64", "test.uint64_3", ftypes.UINT64, nil, base.NONE, "invalid") -- 0
testlib.test(OTHER,"ProtoField-new-uint64-mask-string-invalid", success)

success = pcall(ProtoField.new, "uint64", "test.uint64_4", ftypes.UINT64, nil, base.HEX, "-1") -- 0xFFFFFFFFFFFFFFFF
testlib.test(OTHER,"ProtoField-new-uint64-mask-negative", success)

success = pcall(ProtoField.new, "uint64", "test.uint64_5", ftypes.UINT64, nil, base.NONE)
testlib.test(OTHER,"ProtoField-new-uint64-mask-none", success)

success = pcall(ProtoField.new, "uint64", "test.uint64_6", ftypes.UINT64, nil, base.NONE, nil)
testlib.test(OTHER,"ProtoField-new-uint64-mask-nil", success)

success = pcall(ProtoField.new, "uint64", "test.uint64_7", ftypes.UINT64, nil, base.HEX, "0x00FFFFFF00000000")
testlib.test(OTHER,"ProtoField-new-uint64-mask-hex", success)

success = pcall(ProtoField.uint64, "test.uint64_10", nil, base.HEX, nil, 0x1)
testlib.test(OTHER,"ProtoField-uint64-mask-trivial", success)

success = pcall(ProtoField.uint64, "test.uint64_11", nil, base.HEX, nil, "1")
testlib.test(OTHER,"ProtoField-uint64-mask-string", success)

success = pcall(ProtoField.uint64, "test.uint64_12", nil, base.HEX, nil, UInt64(0x00000001, 0x0))
testlib.test(OTHER,"ProtoField-uint64-mask-uint64", success)

success = pcall(ProtoField.uint64, "test.uint64_13", nil, base.DEC, nil, "invalid") -- 0
testlib.test(OTHER,"ProtoField-uint64-mask-string-invalid", success)

success = pcall(ProtoField.uint64, "test.uint64_14", nil, base.DEC, nil, "-1") -- 0xFFFFFFFFFFFFFFFF
testlib.test(OTHER,"ProtoField-uint64-mask-negative", success)

success = pcall(ProtoField.uint64, "test.uint64_15", nil, base.DEC, nil)
testlib.test(OTHER,"ProtoField-uint64-mask-none", success)

success = pcall(ProtoField.uint64, "test.uint64_16", nil, base.DEC, nil, nil)
testlib.test(OTHER,"ProtoField-uint64-mask-nil", success)


-- Field name: empty, illegal, incompatible
success = pcall(ProtoField.int8, nil, "empty field name 1")
testlib.test(OTHER,"ProtoField-empty-field-name-1", not success)

success = pcall(ProtoField.int8, "", "empty field name 2")
testlib.test(OTHER,"ProtoField-empty-field-name-2", not success)

success = pcall(ProtoField.int8, "test.$", "illegal field name")
testlib.test(OTHER,"ProtoField-illegal-field-name", not success)

success = pcall(ProtoField.int8, "frame.time", "incompatible field name")
testlib.test(OTHER,"ProtoField-incompatible-field-name", not success)

-- Actual name: empty
success = pcall(ProtoField.int8, "test.empty_name_1")
testlib.test(OTHER,"ProtoField-empty-name-1", success)  -- will use abbrev

success = pcall(ProtoField.int8, "test.empty_name_2", "")
testlib.test(OTHER,"ProtoField-empty-name-2", not success)

-- Signed integer base values, only base.DEC should work
success = pcall(ProtoField.int8, "test.int.base_none", "int base NONE", base.NONE)
testlib.test(OTHER,"ProtoField-int-base-none", not success)

success = pcall(ProtoField.int8, "test.int.base_dec", "int base DEC", base.DEC)
testlib.test(OTHER,"ProtoField-int-base-dec", success)

success = pcall(ProtoField.int8, "test.int.base_hex", "int base HEX", base.HEX)
testlib.test(OTHER,"ProtoField-int-base-hex", not success)

success = pcall(ProtoField.int8, "test.int.base_oct", "int base OCT", base.OCT)
testlib.test(OTHER,"ProtoField-int-base-oct", not success)

success = pcall(ProtoField.int8, "test.int.base_dec_hex", "int base DEC_HEX", base.DEC_HEX)
testlib.test(OTHER,"ProtoField-int-base-dec-hex", not success)

success = pcall(ProtoField.int8, "test.int.base_hex_dec", "int base HEX_DEC", base.HEX_DEC)
testlib.test(OTHER,"ProtoField-int-base-hex-dec", not success)

-- Passing no table should not work
success = pcall(ProtoField.uint16, "test.bad0", "Bad0", base.UNIT_STRING)
testlib.test(OTHER,"ProtoField-unitstring-no-table", not success)

-- Passing an empty table should not work
success = pcall(ProtoField.uint16, "test.bad1", "Bad1", base.UNIT_STRING, {})
testlib.test(OTHER,"ProtoField-unitstring-empty-table", not success)

-- Passing userdata should not work
success = pcall(ProtoField.uint16, "test.bad2", "Bad2", base.UNIT_STRING, {test_proto})
testlib.test(OTHER,"ProtoField-unitstring-userdata", not success)

-- Too many items are not supported
success = pcall(ProtoField.uint16, "test.bad3", "Bad3", base.UNIT_STRING, {"too", "many", "items"})
testlib.test(OTHER,"ProtoField-unitstring-too-many-items", not success)

local numinits = 0
function test_proto.init()
    numinits = numinits + 1
    if numinits == 2 then
        testlib.getResults()
    end
end

-- Test expected text with singular and plural forms
function test_proto.dissector(tvb, pinfo, tree)
    local ti
    testlib.countPacket(FRAME)

    local tvb1 = ByteArray.new("00 00"):tvb("Tvb1")
    ti = tree:add(test_proto.fields.time_field, tvb1())
    testlib.test(PER_FRAME,"Time: 0 secs", ti.text == "Time: 0 secs")
    ti = tree:add(test_proto.fields.dist_field, tvb1())
    testlib.test(PER_FRAME,"Distance: 0 km", ti.text == "Distance: 0 km")

    local tvb2 = ByteArray.new("00 01"):tvb("Tvb2")
    ti = tree:add(test_proto.fields.time_field, tvb2())
    testlib.test(PER_FRAME,"Time: 1 sec", ti.text == "Time: 1 sec")
    ti = tree:add(test_proto.fields.dist_field, tvb2())
    testlib.test(PER_FRAME,"Distance: 1 km", ti.text == "Distance: 1 km")

    local tvb3 = ByteArray.new("ff ff"):tvb("Tvb3")
    ti = tree:add(test_proto.fields.time_field, tvb3())
    testlib.test(PER_FRAME,"Time: 65535 secs", ti.text == "Time: 65535 secs")
    ti = tree:add(test_proto.fields.dist_field, tvb3())
    testlib.test(PER_FRAME,"Distance: 65535 km", ti.text == "Distance: 65535 km")

    ti = tree:add(test_proto.fields.filtered_field, tvb2())
    -- Note that this file should be loaded in tshark twice. Once with a visible
    -- tree (-V) and once without a visible tree.
    if tree.visible then
        -- Tree is visible so both fields should be referenced
        testlib.test(PER_FRAME,"Visible tree: Time is referenced", tree:referenced(test_proto.fields.time_field) == true)
        testlib.test(PER_FRAME,"Visible tree: Filtered field is referenced", tree:referenced(test_proto.fields.filtered_field) == true)
    else
        -- Tree is not visible so only the field that appears in a filter should be referenced
        testlib.test(PER_FRAME,"Invisible tree: Time is NOT referenced", tree:referenced(test_proto.fields.time_field) == false)
        testlib.test(PER_FRAME,"Invisible tree: Filtered field is referenced", tree:referenced(test_proto.fields.filtered_field) == true)
    end
    testlib.pass(FRAME)
end

DissectorTable.get("udp.port"):add(65333, test_proto)
DissectorTable.get("udp.port"):add(65346, test_proto)