1 ----------------------------------------
2 -- script-name: protofield.lua
3 -- test the ProtoField API
4 ----------------------------------------
6 local testlib
= require("testlib")
9 local PER_FRAME
= "per-frame"
12 -- expected number of runs
16 [PER_FRAME
]=n_frames
*8,
19 testlib
.init(taptests
)
21 ------------- test script ------------
23 ----------------------------------------
24 local test_proto
= Proto
.new("test", "Test Proto")
25 test_proto
.fields
.time_field
= ProtoField
.uint16("test.time", "Time", base
.UNIT_STRING
, {" sec", " secs"})
26 test_proto
.fields
.dist_field
= ProtoField
.uint16("test.dist", "Distance", base
.UNIT_STRING
, {" km"})
27 test_proto
.fields
.filtered_field
= ProtoField
.uint16("test.filtered", "Filtered Field", base
.DEC
)
30 success
= pcall(ProtoField
.new
, "char", "test.char0", ftypes
.CHAR
)
31 testlib
.test(OTHER
,"ProtoField-char", success
)
33 success
= pcall(ProtoField
.new
, "char base NONE without valuestring", "test.char1", ftypes
.CHAR
, nil, base
.NONE
)
34 testlib
.test(OTHER
,"ProtoField-char-without-valuestring", not success
)
36 success
= pcall(ProtoField
.new
, "char base NONE with valuestring", "test.char2", ftypes
.CHAR
, {1, "Value"}, base
.NONE
)
37 testlib
.test(OTHER
,"ProtoField-char-with-valuestring", success
)
39 success
= pcall(ProtoField
.new
, "char base DEC", "test.char3", ftypes
.CHAR
, nil, base
.DEC
)
40 testlib
.test(OTHER
,"ProtoField-char-base-dec", not success
)
42 success
= pcall(ProtoField
.new
, "char base UNIT_STRING", "test.char4", ftypes
.CHAR
, {" m"}, base
.UNIT_STRING
)
43 testlib
.test(OTHER
,"ProtoField-char-unit-string", not success
)
45 success
= pcall(ProtoField
.new
, "char base RANGE_STRING", "test.char5", ftypes
.CHAR
, {{1, 2, "Value"}}, base
.RANGE_STRING
)
46 testlib
.test(OTHER
,"ProtoField-char-range-string", success
)
48 -- Field type: BOOLEAN/UINT64 with (64 bit) mask
49 success
= pcall(ProtoField
.new
, "boolean", "test.boolean0", ftypes
.BOOLEAN
, nil, base
.HEX
, 0x1)
50 testlib
.test(OTHER
,"ProtoField-new-bool-mask-trivial", success
)
52 success
= pcall(ProtoField
.new
, "boolean", "test.boolean1", ftypes
.BOOLEAN
, nil, base
.HEX
, "1")
53 testlib
.test(OTHER
,"ProtoField-new-bool-mask-string", success
)
55 success
= pcall(ProtoField
.new
, "boolean", "test.boolean2", ftypes
.BOOLEAN
, nil, base
.HEX
, UInt64(0x00000001, 0x0))
56 testlib
.test(OTHER
,"ProtoField-new-bool-mask-uint64", success
)
58 success
= pcall(ProtoField
.new
, "boolean", "test.boolean3", ftypes
.BOOLEAN
, nil, base
.NONE
, "invalid") -- 0
59 testlib
.test(OTHER
,"ProtoField-new-bool-mask-string-invalid", success
)
61 success
= pcall(ProtoField
.new
, "boolean", "test.boolean4", ftypes
.BOOLEAN
, nil, base
.HEX
, "-1") -- 0xFFFFFFFFFFFFFFFF
62 testlib
.test(OTHER
,"ProtoField-new-bool-mask-negative", success
)
64 success
= pcall(ProtoField
.new
, "boolean", "test.boolean5", ftypes
.BOOLEAN
, nil, base
.NONE
)
65 testlib
.test(OTHER
,"ProtoField-new-bool-mask-none", success
)
67 success
= pcall(ProtoField
.new
, "boolean", "test.boolean6", ftypes
.BOOLEAN
, nil, base
.NONE
, nil)
68 testlib
.test(OTHER
,"ProtoField-new-bool-mask-nil", success
)
70 success
= pcall(ProtoField
.new
, "boolean", "test.boolean7", ftypes
.BOOLEAN
, nil, base
.HEX
, "0x00FFFFFF00000000")
71 testlib
.test(OTHER
,"ProtoField-new-bool-mask-hex", success
)
73 success
= pcall(ProtoField
.bool
, "test.boolean10", nil, 64, nil, 0x1)
74 testlib
.test(OTHER
,"ProtoField-bool-mask-trivial", success
)
76 success
= pcall(ProtoField
.bool
, "test.boolean11", nil, 64, nil, "1")
77 testlib
.test(OTHER
,"ProtoField-bool-mask-string", success
)
79 success
= pcall(ProtoField
.bool
, "test.boolean12", nil, 64, nil, UInt64(0x00000001, 0x0))
80 testlib
.test(OTHER
,"ProtoField-bool-mask-uint64", success
)
82 success
= pcall(ProtoField
.bool
, "test.boolean13", nil, base
.NONE
, nil, "invalid") -- 0
83 testlib
.test(OTHER
,"ProtoField-bool-mask-string-invalid", success
)
85 success
= pcall(ProtoField
.bool
, "test.boolean14", nil, 64, nil, "-1") -- 0xFFFFFFFFFFFFFFFF
86 testlib
.test(OTHER
,"ProtoField-bool-mask-negative", success
)
88 success
= pcall(ProtoField
.bool
, "test.boolean15", nil, base
.NONE
, nil)
89 testlib
.test(OTHER
,"ProtoField-bool-mask-none", success
)
91 success
= pcall(ProtoField
.bool
, "test.boolean16", nil, base
.NONE
, nil, nil)
92 testlib
.test(OTHER
,"ProtoField-bool-mask-nil", success
)
94 success
= pcall(ProtoField
.new
, "uint64", "test.uint64_0", ftypes
.UINT64
, nil, base
.HEX
, 0x1)
95 testlib
.test(OTHER
,"ProtoField-new-uint64-mask-trivial", success
)
97 success
= pcall(ProtoField
.new
, "uint64", "test.uint64_1", ftypes
.UINT64
, nil, base
.HEX
, "1")
98 testlib
.test(OTHER
,"ProtoField-new-uint64-mask-string", success
)
100 success
= pcall(ProtoField
.new
, "uint64", "test.uint64_2", ftypes
.UINT64
, nil, base
.HEX
, UInt64(0x00000001, 0x0))
101 testlib
.test(OTHER
,"ProtoField-new-uint64-mask-uint64", success
)
103 success
= pcall(ProtoField
.new
, "uint64", "test.uint64_3", ftypes
.UINT64
, nil, base
.NONE
, "invalid") -- 0
104 testlib
.test(OTHER
,"ProtoField-new-uint64-mask-string-invalid", success
)
106 success
= pcall(ProtoField
.new
, "uint64", "test.uint64_4", ftypes
.UINT64
, nil, base
.HEX
, "-1") -- 0xFFFFFFFFFFFFFFFF
107 testlib
.test(OTHER
,"ProtoField-new-uint64-mask-negative", success
)
109 success
= pcall(ProtoField
.new
, "uint64", "test.uint64_5", ftypes
.UINT64
, nil, base
.NONE
)
110 testlib
.test(OTHER
,"ProtoField-new-uint64-mask-none", success
)
112 success
= pcall(ProtoField
.new
, "uint64", "test.uint64_6", ftypes
.UINT64
, nil, base
.NONE
, nil)
113 testlib
.test(OTHER
,"ProtoField-new-uint64-mask-nil", success
)
115 success
= pcall(ProtoField
.new
, "uint64", "test.uint64_7", ftypes
.UINT64
, nil, base
.HEX
, "0x00FFFFFF00000000")
116 testlib
.test(OTHER
,"ProtoField-new-uint64-mask-hex", success
)
118 success
= pcall(ProtoField
.uint64
, "test.uint64_10", nil, base
.HEX
, nil, 0x1)
119 testlib
.test(OTHER
,"ProtoField-uint64-mask-trivial", success
)
121 success
= pcall(ProtoField
.uint64
, "test.uint64_11", nil, base
.HEX
, nil, "1")
122 testlib
.test(OTHER
,"ProtoField-uint64-mask-string", success
)
124 success
= pcall(ProtoField
.uint64
, "test.uint64_12", nil, base
.HEX
, nil, UInt64(0x00000001, 0x0))
125 testlib
.test(OTHER
,"ProtoField-uint64-mask-uint64", success
)
127 success
= pcall(ProtoField
.uint64
, "test.uint64_13", nil, base
.DEC
, nil, "invalid") -- 0
128 testlib
.test(OTHER
,"ProtoField-uint64-mask-string-invalid", success
)
130 success
= pcall(ProtoField
.uint64
, "test.uint64_14", nil, base
.DEC
, nil, "-1") -- 0xFFFFFFFFFFFFFFFF
131 testlib
.test(OTHER
,"ProtoField-uint64-mask-negative", success
)
133 success
= pcall(ProtoField
.uint64
, "test.uint64_15", nil, base
.DEC
, nil)
134 testlib
.test(OTHER
,"ProtoField-uint64-mask-none", success
)
136 success
= pcall(ProtoField
.uint64
, "test.uint64_16", nil, base
.DEC
, nil, nil)
137 testlib
.test(OTHER
,"ProtoField-uint64-mask-nil", success
)
140 -- Field name: empty, illegal, incompatible
141 success
= pcall(ProtoField
.int8
, nil, "empty field name 1")
142 testlib
.test(OTHER
,"ProtoField-empty-field-name-1", not success
)
144 success
= pcall(ProtoField
.int8
, "", "empty field name 2")
145 testlib
.test(OTHER
,"ProtoField-empty-field-name-2", not success
)
147 success
= pcall(ProtoField
.int8
, "test.$", "illegal field name")
148 testlib
.test(OTHER
,"ProtoField-illegal-field-name", not success
)
150 success
= pcall(ProtoField
.int8
, "frame.time", "incompatible field name")
151 testlib
.test(OTHER
,"ProtoField-incompatible-field-name", not success
)
153 -- Actual name: empty
154 success
= pcall(ProtoField
.int8
, "test.empty_name_1")
155 testlib
.test(OTHER
,"ProtoField-empty-name-1", success
) -- will use abbrev
157 success
= pcall(ProtoField
.int8
, "test.empty_name_2", "")
158 testlib
.test(OTHER
,"ProtoField-empty-name-2", not success
)
160 -- Signed integer base values, only base.DEC should work
161 success
= pcall(ProtoField
.int8
, "test.int.base_none", "int base NONE", base
.NONE
)
162 testlib
.test(OTHER
,"ProtoField-int-base-none", not success
)
164 success
= pcall(ProtoField
.int8
, "test.int.base_dec", "int base DEC", base
.DEC
)
165 testlib
.test(OTHER
,"ProtoField-int-base-dec", success
)
167 success
= pcall(ProtoField
.int8
, "test.int.base_hex", "int base HEX", base
.HEX
)
168 testlib
.test(OTHER
,"ProtoField-int-base-hex", not success
)
170 success
= pcall(ProtoField
.int8
, "test.int.base_oct", "int base OCT", base
.OCT
)
171 testlib
.test(OTHER
,"ProtoField-int-base-oct", not success
)
173 success
= pcall(ProtoField
.int8
, "test.int.base_dec_hex", "int base DEC_HEX", base
.DEC_HEX
)
174 testlib
.test(OTHER
,"ProtoField-int-base-dec-hex", not success
)
176 success
= pcall(ProtoField
.int8
, "test.int.base_hex_dec", "int base HEX_DEC", base
.HEX_DEC
)
177 testlib
.test(OTHER
,"ProtoField-int-base-hex-dec", not success
)
179 -- Passing no table should not work
180 success
= pcall(ProtoField
.uint16
, "test.bad0", "Bad0", base
.UNIT_STRING
)
181 testlib
.test(OTHER
,"ProtoField-unitstring-no-table", not success
)
183 -- Passing an empty table should not work
184 success
= pcall(ProtoField
.uint16
, "test.bad1", "Bad1", base
.UNIT_STRING
, {})
185 testlib
.test(OTHER
,"ProtoField-unitstring-empty-table", not success
)
187 -- Passing userdata should not work
188 success
= pcall(ProtoField
.uint16
, "test.bad2", "Bad2", base
.UNIT_STRING
, {test_proto
})
189 testlib
.test(OTHER
,"ProtoField-unitstring-userdata", not success
)
191 -- Too many items are not supported
192 success
= pcall(ProtoField
.uint16
, "test.bad3", "Bad3", base
.UNIT_STRING
, {"too", "many", "items"})
193 testlib
.test(OTHER
,"ProtoField-unitstring-too-many-items", not success
)
196 function test_proto
.init()
197 numinits
= numinits
+ 1
198 if numinits
== 2 then
203 -- Test expected text with singular and plural forms
204 function test_proto
.dissector(tvb
, pinfo
, tree
)
207 testlib
.countPacket(FRAME
)
209 local tvb1
= ByteArray
.new("00 00"):tvb("Tvb1")
210 ti_time
= tree
:add(test_proto
.fields
.time_field
, tvb1())
211 ti_distance
= tree
:add(test_proto
.fields
.dist_field
, tvb1())
213 testlib
.test(PER_FRAME
,"Time: 0 secs", ti_time
.text
== "Time: 0 secs")
214 testlib
.test(PER_FRAME
,"Distance: 0 km", ti_distance
.text
== "Distance: 0 km")
216 testlib
.test(PER_FRAME
,"Time: 0 secs", ti_time
.text
== nil)
217 testlib
.test(PER_FRAME
,"Distance: 0 km", ti_distance
.text
== nil)
220 local tvb2
= ByteArray
.new("00 01"):tvb("Tvb2")
221 ti_time
= tree
:add(test_proto
.fields
.time_field
, tvb2())
222 ti_distance
= tree
:add(test_proto
.fields
.dist_field
, tvb2())
224 testlib
.test(PER_FRAME
,"Time: 1 sec", ti_time
.text
== "Time: 1 sec")
225 testlib
.test(PER_FRAME
,"Distance: 1 km", ti_distance
.text
== "Distance: 1 km")
227 testlib
.test(PER_FRAME
,"Time: 1 sec", ti_time
.text
== nil)
228 testlib
.test(PER_FRAME
,"Distance: 1 km", ti_distance
.text
== nil)
231 local tvb3
= ByteArray
.new("ff ff"):tvb("Tvb3")
232 ti_time
= tree
:add(test_proto
.fields
.time_field
, tvb3())
233 ti_distance
= tree
:add(test_proto
.fields
.dist_field
, tvb3())
235 testlib
.test(PER_FRAME
,"Time: 65535 secs", ti_time
.text
== "Time: 65535 secs")
236 testlib
.test(PER_FRAME
,"Distance: 65535 km", ti_distance
.text
== "Distance: 65535 km")
238 testlib
.test(PER_FRAME
,"Time: 65535 secs", ti_time
.text
== nil)
239 testlib
.test(PER_FRAME
,"Distance: 65535 km", ti_distance
.text
== nil)
242 ti
= tree
:add(test_proto
.fields
.filtered_field
, tvb2())
243 -- Note that this file should be loaded in tshark twice. Once with a visible
244 -- tree (-V) and once without a visible tree.
246 -- Tree is visible so both fields should be referenced
247 testlib
.test(PER_FRAME
,"Visible tree: Time is referenced", tree
:referenced(test_proto
.fields
.time_field
) == true)
248 testlib
.test(PER_FRAME
,"Visible tree: Filtered field is referenced", tree
:referenced(test_proto
.fields
.filtered_field
) == true)
250 -- Tree is not visible so only the field that appears in a filter should be referenced
251 testlib
.test(PER_FRAME
,"Invisible tree: Time is NOT referenced", tree
:referenced(test_proto
.fields
.time_field
) == false)
252 testlib
.test(PER_FRAME
,"Invisible tree: Filtered field is referenced", tree
:referenced(test_proto
.fields
.filtered_field
) == true)
257 DissectorTable
.get("udp.port"):add("65333,65346", test_proto
)