1 ----------------------------------------
2 -- script-name: tvb.lua
3 -- This tests the Tvb/TvbRange and proto_add_XXX_item API.
4 ----------------------------------------
5 local testlib
= require("testlib")
10 -- expected number of runs per type
12 -- CHANGE THIS TO MATCH HOW MANY TESTS THERE ARE
14 -- The number of tests in a specific category (other than FRAME) is the
15 -- number of times execute() is called by any function below testing().
16 -- From the user's perspective, it can be calculated with the following
19 -- N = number of execute() you call +
20 -- number of verifyFields() * (1 + number of fields) +
21 -- number of verifyResults() * (1 + 2 * number of values)
23 -- if one happens to know the number of fields and the number of values.
26 local taptests
= { [FRAME
]=n_frames
, [OTHER
]=391*n_frames
}
28 testlib
.init(taptests
)
30 ------------- test script ------------
32 ----------------------------------------
33 -- creates a Proto object for our testing
34 local test_proto
= Proto("test","Test Protocol")
37 function test_proto
.init()
38 numinits
= numinits
+ 1
45 ----------------------------------------
46 -- a table of all of our Protocol's fields
48 { 0, 200, "The first part" },
49 { 201, 233, "The second part" },
50 { 234, 255, "The last part" },
57 STRING
= ProtoField
.string ("test.basic.string", "Basic string"),
58 BOOLEAN
= ProtoField
.bool ("test.basic.boolean", "Basic boolean", 16, {"yes","no"}, 0x0001),
59 UINT8
= ProtoField
.uint8 ("test.basic.uint8", "Basic uint8 with range string", base
.RANGE_STRING
, range_string
),
60 UINT16
= ProtoField
.uint16 ("test.basic.uint16", "Basic uint16"),
61 UINT32
= ProtoField
.uint32 ("test.basic.uint32", "Basic uint32 test with a unit string", base
.UINT_STRING
, { "femtoFarads" }),
62 INT24
= ProtoField
.int24 ("test.basic.uint24", "Basic uint24"),
63 BYTES
= ProtoField
.bytes ("test.basic.bytes", "Basic Bytes"),
64 UINT_BYTES
= ProtoField
.ubytes ("test.basic.ubytes", "Basic Uint Bytes"),
65 OID
= ProtoField
.oid ("test.basic.oid", "Basic OID"),
66 REL_OID
= ProtoField
.rel_oid("test.basic.rel_oid", "Basic Relative OID"),
67 ABSOLUTE_LOCAL
= ProtoField
.absolute_time("test.basic.absolute.local","Basic absolute local"),
68 ABSOLUTE_UTC
= ProtoField
.absolute_time("test.basic.absolute.utc", "Basic absolute utc", base
.UTC
),
69 IPv4
= ProtoField
.ipv4 ("test.basic.ipv4", "Basic ipv4 address"),
70 IPv6
= ProtoField
.ipv6 ("test.basic.ipv6", "Basic ipv6 address"),
71 ETHER
= ProtoField
.ether ("test.basic.ether", "Basic ethernet address"),
72 -- GUID = ProtoField.guid ("test.basic.guid", "Basic GUID"),
77 ABSOLUTE_LOCAL
= ProtoField
.absolute_time("test.time.absolute.local","Time absolute local"),
78 ABSOLUTE_UTC
= ProtoField
.absolute_time("test.time.absolute.utc", "Time absolute utc", base
.UTC
),
83 BYTES
= ProtoField
.bytes ("test.bytes.bytes", "Bytes"),
84 UINT_BYTES
= ProtoField
.ubytes ("test.bytes.ubytes", "Uint Bytes"),
85 OID
= ProtoField
.oid ("test.bytes.oid", "OID"),
86 REL_OID
= ProtoField
.rel_oid("test.bytes.rel_oid", "Relative OID"),
87 -- GUID = ProtoField.guid ("test.bytes.guid", "GUID"),
91 -- create a flat array table of the above that can be registered
93 for _
,t
in pairs(testfield
) do
94 for k
,v
in pairs(t
) do
95 pfields
[#pfields
+1] = v
100 test_proto
.fields
= pfields
102 print("test_proto ProtoFields registered")
109 STRING
= Field
.new ("test.basic.string"),
110 BOOLEAN
= Field
.new ("test.basic.boolean"),
111 UINT8
= Field
.new ("test.basic.uint8"),
112 UINT16
= Field
.new ("test.basic.uint16"),
113 INT24
= Field
.new ("test.basic.uint24"),
114 BYTES
= Field
.new ("test.basic.bytes"),
115 UINT_BYTES
= Field
.new ("test.basic.ubytes"),
116 OID
= Field
.new ("test.basic.oid"),
117 REL_OID
= Field
.new ("test.basic.rel_oid"),
118 ABSOLUTE_LOCAL
= Field
.new ("test.basic.absolute.local"),
119 ABSOLUTE_UTC
= Field
.new ("test.basic.absolute.utc"),
120 IPv4
= Field
.new ("test.basic.ipv4"),
121 IPv6
= Field
.new ("test.basic.ipv6"),
122 ETHER
= Field
.new ("test.basic.ether"),
123 -- GUID = Field.new ("test.basic.guid"),
128 ABSOLUTE_LOCAL
= Field
.new ("test.time.absolute.local"),
129 ABSOLUTE_UTC
= Field
.new ("test.time.absolute.utc"),
134 BYTES
= Field
.new ("test.bytes.bytes"),
135 UINT_BYTES
= Field
.new ("test.bytes.ubytes"),
136 OID
= Field
.new ("test.bytes.oid"),
137 REL_OID
= Field
.new ("test.bytes.rel_oid"),
138 -- GUID = Field.new ("test.bytes.guid"),
142 print("test_proto Fields created")
144 local function addMatchFields(match_fields
, ... )
145 match_fields
[#match_fields
+ 1] = { ... }
148 local function getFieldInfos(name
)
149 local base
, field
= name
:match("([^.]+)%.(.+)")
150 if not base
or not field
then
151 error("failed to get base.field from '" .. name
.. "'")
153 local t
= { getfield
[base
][field
]() }
157 local function verifyFields(name
, match_fields
)
158 local finfos
= getFieldInfos(name
)
160 testlib
.test(OTHER
, "verify-fields-size-" .. name
, #finfos
== #match_fields
,
161 "#finfos=" .. #finfos
.. ", #match_fields=" .. #match_fields
)
163 for i
, t
in ipairs(match_fields
) do
164 if type(t
) ~= 'table' then
165 error("verifyFields didn't get a table inside the matches table")
168 error("verifyFields matches table's table is not size 1")
170 local result
= finfos
[i
]()
174 "\n\tfinfos [" .. i
.. "]='" .. tostring( result
) .. "'",
175 "\n\tmatches[" .. i
.. "]='" .. tostring( value
) .. "'"
177 testlib
.test(OTHER
, "verify-fields-value-" .. name
.. "-" .. i
, result
== value
)
182 local function addMatchValues(match_values
, ... )
183 match_values
[#match_values
+ 1] = { ... }
186 local function addMatchFieldValues(match_fields
, match_values
, match_both
, ...)
187 addMatchFields(match_fields
, match_both
)
188 addMatchValues(match_values
, match_both
, ...)
191 local result_values
= {}
192 local function resetResults()
196 local function treeAddPField(...)
197 local t
= { pcall ( TreeItem
.add_packet_field
, ... ) }
201 -- it gives back a TreeItem, then the results
202 if typeof(t
[2]) ~= 'TreeItem' then
203 return nil, "did not get a TreeItem returned from TreeItem.add_packet_field, "..
204 "got a '" .. typeof(t
[2]) .."'"
208 return nil, "did not get 3 return values from TreeItem.add_packet_field"
211 result_values
[#result_values
+ 1] = { t
[3], t
[4] }
216 local function verifyResults(name
, match_values
)
217 testlib
.test(OTHER
, "verify-results-size-" .. name
, #result_values
== #match_values
,
218 "#result_values=" .. #result_values
..
219 ", #match_values=" .. #match_values
)
221 for j
, t
in ipairs(match_values
) do
222 if type(t
) ~= 'table' then
223 error("verifyResults didn't get a table inside the matches table")
225 for i
, match
in ipairs(t
) do
226 local r
= result_values
[j
][i
]
229 "\n\tresults[" .. j
.. "][" .. i
.. "]='" .. tostring( r
) .. "'",
230 "\n\tmatches[" .. j
.. "][" .. i
.. "]='" .. tostring( match
) .. "'"
232 local result_type
, match_type
233 if type(match
) == 'userdata' then
234 match_type
= typeof(match
)
236 match_type
= type(match
)
238 if type(r
) == 'userdata' then
239 result_type
= typeof(r
)
241 result_type
= type(r
)
243 testlib
.test(OTHER
, "verify-results-type-" .. name
.. "-" .. i
, result_type
== match_type
)
244 testlib
.test(OTHER
, "verify-results-value-" .. name
.. "-" .. i
, r
== match
)
249 -- Compute the difference in seconds between local time and UTC
250 -- from http://lua-users.org/wiki/TimeZone
251 local function get_timezone()
252 local now
= os
.time()
253 return os
.difftime(now
, os
.time(os
.date("!*t", now
)))
255 local timezone
= get_timezone()
256 print ("timezone = " .. timezone
)
258 ----------------------------------------
259 -- The following creates the callback function for the dissector.
260 -- The 'tvbuf' is a Tvb object, 'pktinfo' is a Pinfo object, and 'root' is a TreeItem object.
261 function test_proto
.dissector(tvbuf
,pktinfo
,root
)
263 testlib
.countPacket(FRAME
)
264 testlib
.countPacket(OTHER
)
266 testlib
.testing(OTHER
, "Basic string")
268 local tree
= root
:add(test_proto
, tvbuf
:range(0,tvbuf
:len()))
270 -- create a fake Tvb to use for testing
271 local teststring
= "this is the string for the first test"
272 local bytearray
= ByteArray
.new(teststring
, true)
273 local tvb_string
= bytearray
:tvb("Basic string")
275 local function callTreeAdd(tree
,...)
279 local string_match_fields
= {}
281 testlib
.test(OTHER
, "basic-tvb_get_string", tvb_string
:range():string() == teststring
)
283 testlib
.test(OTHER
, "basic-string", tree
:add(testfield
.basic
.STRING
, tvb_string
:range(0,tvb_string
:len())) ~= nil )
284 addMatchFields(string_match_fields
, teststring
)
286 testlib
.test(OTHER
, "basic-string", pcall (callTreeAdd
, tree
, testfield
.basic
.STRING
, tvb_string
:range() ) )
287 addMatchFields(string_match_fields
, teststring
)
289 verifyFields("basic.STRING", string_match_fields
)
291 ----------------------------------------
292 testlib
.testing(OTHER
, "Basic boolean")
294 local barray_bytes_hex
= "00FF00018000"
295 local barray_bytes
= ByteArray
.new(barray_bytes_hex
)
296 local tvb_bytes
= barray_bytes
:tvb("Basic bytes")
297 local bool_match_fields
= {}
299 testlib
.test(OTHER
, "basic-boolean", pcall (callTreeAdd
, tree
, testfield
.basic
.BOOLEAN
, tvb_bytes
:range(0,2)) )
300 addMatchFields(bool_match_fields
, true)
302 testlib
.test(OTHER
, "basic-boolean", pcall (callTreeAdd
, tree
, testfield
.basic
.BOOLEAN
, tvb_bytes
:range(2,2)) )
303 addMatchFields(bool_match_fields
, true)
305 testlib
.test(OTHER
, "basic-boolean", pcall (callTreeAdd
, tree
, testfield
.basic
.BOOLEAN
, tvb_bytes
:range(4,2)) )
306 addMatchFields(bool_match_fields
, false)
308 verifyFields("basic.BOOLEAN", bool_match_fields
)
310 ----------------------------------------
311 testlib
.testing(OTHER
, "Basic uint16")
313 local uint16_match_fields
= {}
315 testlib
.test(OTHER
, "basic-uint16", pcall (callTreeAdd
, tree
, testfield
.basic
.UINT16
, tvb_bytes
:range(0,2)) )
316 addMatchFields(uint16_match_fields
, 255)
318 testlib
.test(OTHER
, "basic-uint16", pcall (callTreeAdd
, tree
, testfield
.basic
.UINT16
, tvb_bytes
:range(2,2)) )
319 addMatchFields(uint16_match_fields
, 1)
321 testlib
.test(OTHER
, "basic-uint16", pcall (callTreeAdd
, tree
, testfield
.basic
.UINT16
, tvb_bytes
:range(4,2)) )
322 addMatchFields(uint16_match_fields
, 32768)
324 verifyFields("basic.UINT16", uint16_match_fields
)
326 ----------------------------------------
327 testlib
.testing(OTHER
, "Basic uint16-le")
329 local function callTreeAddLE(tree
,...)
333 testlib
.test(OTHER
, "basic-uint16-le", pcall (callTreeAddLE
, tree
, testfield
.basic
.UINT16
, tvb_bytes
:range(0,2)) )
334 addMatchFields(uint16_match_fields
, 65280)
336 testlib
.test(OTHER
, "basic-uint16-le", pcall (callTreeAddLE
, tree
, testfield
.basic
.UINT16
, tvb_bytes
:range(2,2)) )
337 addMatchFields(uint16_match_fields
, 256)
339 testlib
.test(OTHER
, "basic-uint16-le", pcall (callTreeAddLE
, tree
, testfield
.basic
.UINT16
, tvb_bytes
:range(4,2)) )
340 addMatchFields(uint16_match_fields
, 128)
342 verifyFields("basic.UINT16", uint16_match_fields
)
344 ----------------------------------------
345 testlib
.testing(OTHER
, "Basic int24")
347 local int24_match_fields
= {}
349 testlib
.test(OTHER
, "basic-int24", pcall (callTreeAdd
, tree
, testfield
.basic
.INT24
, tvb_bytes
:range(0,3)) )
350 addMatchFields(int24_match_fields
, 65280)
352 testlib
.test(OTHER
, "basic-int24", pcall (callTreeAdd
, tree
, testfield
.basic
.INT24
, tvb_bytes
:range(3,3)) )
353 addMatchFields(int24_match_fields
, 98304)
355 verifyFields("basic.INT24", int24_match_fields
)
357 ----------------------------------------
358 testlib
.testing(OTHER
, "Basic int24-le")
360 testlib
.test(OTHER
, "basic-int24", pcall (callTreeAddLE
, tree
, testfield
.basic
.INT24
, tvb_bytes
:range(0,3)) )
361 addMatchFields(int24_match_fields
, 65280)
363 testlib
.test(OTHER
, "basic-int24", pcall (callTreeAddLE
, tree
, testfield
.basic
.INT24
, tvb_bytes
:range(3,3)) )
364 addMatchFields(int24_match_fields
, 32769)
366 verifyFields("basic.INT24", int24_match_fields
)
368 ----------------------------------------
369 testlib
.testing(OTHER
, "Basic bytes")
371 local bytes_match_fields
= {}
373 testlib
.test(OTHER
, "basic-tvb_get_string_bytes",
374 string.lower(tostring(tvb_bytes
:range():bytes())) == string.lower(barray_bytes_hex
))
376 testlib
.test(OTHER
, "basic-bytes", pcall (callTreeAdd
, tree
, testfield
.basic
.BYTES
, tvb_bytes
:range()) )
377 addMatchFields(bytes_match_fields
, barray_bytes
)
379 -- TODO: it's silly that tree:add_packet_field() requires an encoding argument
380 -- need to fix that separately in a bug fix
381 testlib
.test(OTHER
, "add_pfield-bytes", treeAddPField(tree
, testfield
.basic
.BYTES
,
382 tvb_bytes
:range(), ENC_BIG_ENDIAN
))
383 addMatchFields(bytes_match_fields
, barray_bytes
)
385 verifyFields("basic.BYTES", bytes_match_fields
)
387 ----------------------------------------
388 testlib
.testing(OTHER
, "Basic uint bytes")
390 local len_string
= string.format("%02x", barray_bytes
:len())
391 local barray_uint_bytes
= ByteArray
.new(len_string
) .. barray_bytes
392 local tvb_uint_bytes
= barray_uint_bytes
:tvb("Basic UINT_BYTES")
393 local uint_bytes_match_fields
= {}
395 testlib
.test(OTHER
, "basic-uint-bytes", pcall (callTreeAdd
, tree
, testfield
.basic
.UINT_BYTES
,
396 tvb_uint_bytes
:range(0,1)) )
397 addMatchFields(uint_bytes_match_fields
, barray_bytes
)
399 testlib
.test(OTHER
, "add_pfield-uint-bytes", treeAddPField(tree
, testfield
.basic
.UINT_BYTES
,
400 tvb_uint_bytes
:range(0,1), ENC_BIG_ENDIAN
) )
401 addMatchFields(uint_bytes_match_fields
, barray_bytes
)
403 verifyFields("basic.UINT_BYTES", uint_bytes_match_fields
)
405 ----------------------------------------
406 testlib
.testing(OTHER
, "Basic OID")
408 -- note: the tvb being dissected and compared isn't actually a valid OID.
409 -- tree:add() and tree:add_packet-field() don't care about its validity right now.
411 local oid_match_fields
= {}
413 testlib
.test(OTHER
, "basic-oid", pcall(callTreeAdd
, tree
, testfield
.basic
.OID
, tvb_bytes
:range()) )
414 addMatchFields(oid_match_fields
, barray_bytes
)
416 testlib
.test(OTHER
, "add_pfield-oid", treeAddPField(tree
, testfield
.basic
.OID
,
417 tvb_bytes
:range(), ENC_BIG_ENDIAN
) )
418 addMatchFields(oid_match_fields
, barray_bytes
)
420 verifyFields("basic.OID", oid_match_fields
)
422 ----------------------------------------
423 testlib
.testing(OTHER
, "Basic REL_OID")
425 -- note: the tvb being dissected and compared isn't actually a valid OID.
426 -- tree:add() and tree:add_packet-field() don't care about its validity right now.
428 local rel_oid_match_fields
= {}
430 testlib
.test(OTHER
, "basic-rel-oid", pcall(callTreeAdd
, tree
, testfield
.basic
.REL_OID
, tvb_bytes
:range()))
431 addMatchFields(rel_oid_match_fields
, barray_bytes
)
433 testlib
.test(OTHER
, "add_pfield-rel_oid", treeAddPField(tree
, testfield
.basic
.REL_OID
,
434 tvb_bytes
:range(), ENC_BIG_ENDIAN
) )
435 addMatchFields(rel_oid_match_fields
, barray_bytes
)
437 verifyFields("basic.REL_OID", rel_oid_match_fields
)
439 -- TODO: a FT_GUID is not really a ByteArray, so we can't simply treat it as one
440 -- local barray_guid = ByteArray.new("00FF0001 80001234 567890AB CDEF00FF")
441 -- local tvb_guid = barray_guid:tvb("Basic GUID")
442 -- local guid_match_fields = {}
444 -- testlib.test(OTHER, "basic-guid", pcall(callTreeAdd, tree, testfield.basic.GUID, tvb_guid:range()) )
445 -- addMatchFields(guid_match_fields, barray_guid)
447 -- testlib.test(OTHER, "add_pfield-guid", treeAddPField(tree, testfield.basic.GUID,
448 -- tvb_guid:range(), ENC_BIG_ENDIAN) )
449 -- addMatchFields(guid_match_fields, barray_guid)
451 -- verifyFields("basic.GUID", guid_match_fields)
453 ----------------------------------------
454 testlib
.testing(OTHER
, "tree:add ipv6")
456 local tvb
= ByteArray
.new("20010db8 00000000 0000ff00 00428329"):tvb("IPv6")
457 local IPv6
= testfield
.basic
.IPv6
458 local ipv6_match_fields
= {}
460 testlib
.test(OTHER
, "ipv6", pcall (callTreeAdd
, tree
, IPv6
, tvb
:range(0,16)))
461 addMatchFields(ipv6_match_fields
, Address
.ipv6('2001:0db8:0000:0000:0000:ff00:0042:8329'))
463 verifyFields("basic.IPv6", ipv6_match_fields
)
465 ----------------------------------------
466 testlib
.testing(OTHER
, "tree:add ipv4")
468 local tvb
= ByteArray
.new("7f000001"):tvb("IPv4")
469 local IPv4
= testfield
.basic
.IPv4
470 local ipv4_match_fields
= {}
472 testlib
.test(OTHER
, "ipv4", pcall (callTreeAdd
, tree
, IPv4
, tvb
:range(0,4)))
473 addMatchFields(ipv4_match_fields
, Address
.ip('127.0.0.1'))
475 -- TODO: currently, tree:add_le only works for numeric values, not IPv4
476 -- addresses. Test this in the future.
478 -- testlib.test(OTHER, "ipv4", pcall (callTreeAddLE, tree, IPv4, tvb:range(0,4)))
479 -- addMatchFields(ipv4_match_fields, Address.ip('1.0.0.127'))
481 verifyFields("basic.IPv4", ipv4_match_fields
)
483 ----------------------------------------
484 testlib
.testing(OTHER
, "tree:add ether")
486 local tvb
= ByteArray
.new("010203040506"):tvb("Ether")
487 local tvb0
= ByteArray
.new("000000000000"):tvb("Ether0")
488 local ether
= testfield
.basic
.ETHER
489 local ether_match_fields
= {}
491 testlib
.test(OTHER
, "ether", pcall (callTreeAdd
, tree
, ether
, tvb
:range(0,6)))
492 addMatchFields(ether_match_fields
, Address
.ether('01:02:03:04:05:06'))
494 testlib
.test(OTHER
, "ether0", pcall (callTreeAdd
, tree
, ether
, tvb0
:range(0,6)))
495 addMatchFields(ether_match_fields
, Address
.ether('11:22:33'))
497 verifyFields("basic.ETHER", ether_match_fields
)
499 ----------------------------------------
500 testlib
.testing(OTHER
, "tree:add_packet_field Bytes")
503 bytes_match_fields
= {}
504 local bytes_match_values
= {}
506 -- something to make this easier to read
507 local function addMatch(...)
508 addMatchFieldValues(bytes_match_fields
, bytes_match_values
, ...)
511 local bytesstring1
= "deadbeef0123456789DEADBEEFabcdef"
512 local bytesstring
= ByteArray
.new(bytesstring1
) -- the binary version of above, for comparing
513 local bytestvb1
= ByteArray
.new(bytesstring1
, true):tvb("Bytes hex-string 1")
514 local bytesstring2
= " de:ad:be:ef:01:23:45:67:89:DE:AD:BE:EF:ab:cd:ef"
515 local bytestvb2
= ByteArray
.new(bytesstring2
.. "-f0-00 foobar", true):tvb("Bytes hex-string 2")
517 local bytestvb1_decode
= bytestvb1
:range():bytes(ENC_STR_HEX
+ ENC_SEP_NONE
+ ENC_SEP_COLON
+ ENC_SEP_DASH
)
518 testlib
.test(OTHER
, "tvb_get_string_bytes", string.lower(tostring(bytestvb1_decode
)) == string.lower(tostring(bytesstring1
)))
520 testlib
.test(OTHER
, "add_pfield-bytes1", treeAddPField(tree
, testfield
.bytes
.BYTES
,
522 ENC_STR_HEX
+ ENC_SEP_NONE
+
523 ENC_SEP_COLON
+ ENC_SEP_DASH
))
524 addMatch(bytesstring
, string.len(bytesstring1
))
526 testlib
.test(OTHER
, "add_pfield-bytes2", treeAddPField(tree
, testfield
.bytes
.BYTES
,
528 ENC_STR_HEX
+ ENC_SEP_NONE
+
529 ENC_SEP_COLON
+ ENC_SEP_DASH
))
530 addMatch(bytesstring
, string.len(bytesstring2
))
532 verifyResults("add_pfield-bytes", bytes_match_values
)
533 verifyFields("bytes.BYTES", bytes_match_fields
)
535 -- extra test of ByteArray
536 local b64padded
= ByteArray
.new("dGVzdA==", true):base64_decode():raw()
537 local b64unpadded
= ByteArray
.new("dGVzdA", true):base64_decode():raw()
538 testlib
.test(OTHER
, "bytearray_base64_padded", b64padded
== "test")
539 testlib
.test(OTHER
, "bytearray_base64_unpadded", b64unpadded
== "test")
542 ----------------------------------------
543 testlib
.testing(OTHER
, "tree:add_packet_field OID")
546 bytes_match_fields
= {}
547 bytes_match_values
= {}
549 testlib
.test(OTHER
, "add_pfield-oid1", treeAddPField(tree
, testfield
.bytes
.OID
,
551 ENC_STR_HEX
+ ENC_SEP_NONE
+
552 ENC_SEP_COLON
+ ENC_SEP_DASH
))
553 addMatch(bytesstring
, string.len(bytesstring1
))
555 testlib
.test(OTHER
, "add_pfield-oid2", treeAddPField(tree
, testfield
.bytes
.OID
,
557 ENC_STR_HEX
+ ENC_SEP_NONE
+
558 ENC_SEP_COLON
+ ENC_SEP_DASH
))
559 addMatch(bytesstring
, string.len(bytesstring2
))
561 verifyResults("add_pfield-oid", bytes_match_values
)
562 verifyFields("bytes.OID", bytes_match_fields
)
565 ----------------------------------------
566 testlib
.testing(OTHER
, "tree:add_packet_field REL_OID")
569 bytes_match_fields
= {}
570 bytes_match_values
= {}
572 testlib
.test(OTHER
, "add_pfield-rel_oid1", treeAddPField(tree
, testfield
.bytes
.REL_OID
,
574 ENC_STR_HEX
+ ENC_SEP_NONE
+
575 ENC_SEP_COLON
+ ENC_SEP_DASH
))
576 addMatch(bytesstring
, string.len(bytesstring1
))
578 testlib
.test(OTHER
, "add_pfield-rel_oid2", treeAddPField(tree
, testfield
.bytes
.REL_OID
,
580 ENC_STR_HEX
+ ENC_SEP_NONE
+
581 ENC_SEP_COLON
+ ENC_SEP_DASH
))
582 addMatch(bytesstring
, string.len(bytesstring2
))
584 verifyResults("add_pfield-rel_oid", bytes_match_values
)
585 verifyFields("bytes.REL_OID", bytes_match_fields
)
588 ----------------------------------------
589 testlib
.testing(OTHER
, "tree:add Time")
591 local tvb
= ByteArray
.new("00000000 00000000 0000FF0F 00FF000F"):tvb("Time")
592 local ALOCAL
= testfield
.time
.ABSOLUTE_LOCAL
593 local alocal_match_fields
= {}
595 testlib
.test(OTHER
, "time-local", pcall (callTreeAdd
, tree
, ALOCAL
, tvb
:range(0,8)) )
596 addMatchFields(alocal_match_fields
, NSTime())
598 testlib
.test(OTHER
, "time-local", pcall (callTreeAdd
, tree
, ALOCAL
, tvb
:range(8,8)) )
599 addMatchFields(alocal_match_fields
, NSTime( 0x0000FF0F, 0x00FF000F) )
601 testlib
.test(OTHER
, "time-local-le", pcall (callTreeAddLE
, tree
, ALOCAL
, tvb
:range(0,8)) )
602 addMatchFields(alocal_match_fields
, NSTime())
604 testlib
.test(OTHER
, "time-local-le", pcall (callTreeAddLE
, tree
, ALOCAL
, tvb
:range(8,8)) )
605 addMatchFields(alocal_match_fields
, NSTime( 0x0FFF0000, 0x0F00FF00 ) )
607 verifyFields("time.ABSOLUTE_LOCAL", alocal_match_fields
)
609 local AUTC
= testfield
.time
.ABSOLUTE_UTC
610 local autc_match_fields
= {}
612 testlib
.test(OTHER
, "time-utc", pcall (callTreeAdd
, tree
, AUTC
, tvb
:range(0,8)) )
613 addMatchFields(autc_match_fields
, NSTime())
615 testlib
.test(OTHER
, "time-utc", pcall (callTreeAdd
, tree
, AUTC
, tvb
:range(8,8)) )
616 addMatchFields(autc_match_fields
, NSTime( 0x0000FF0F, 0x00FF000F) )
618 testlib
.test(OTHER
, "time-utc-le", pcall (callTreeAddLE
, tree
, AUTC
, tvb
:range(0,8)) )
619 addMatchFields(autc_match_fields
, NSTime())
621 testlib
.test(OTHER
, "time-utc-le", pcall (callTreeAddLE
, tree
, AUTC
, tvb
:range(8,8)) )
622 addMatchFields(autc_match_fields
, NSTime( 0x0FFF0000, 0x0F00FF00 ) )
624 verifyFields("time.ABSOLUTE_UTC", autc_match_fields
)
626 ----------------------------------------
627 testlib
.testing(OTHER
, "tree:add_packet_field Time bytes")
630 local autc_match_values
= {}
632 -- something to make this easier to read
633 addMatch
= function(...)
634 addMatchFieldValues(autc_match_fields
, autc_match_values
, ...)
637 -- tree:add_packet_field(ALOCAL, tvb:range(0,8), ENC_BIG_ENDIAN)
638 testlib
.test(OTHER
, "add_pfield-time-bytes-local", treeAddPField ( tree
, AUTC
, tvb
:range(0,8), ENC_BIG_ENDIAN
) )
639 addMatch( NSTime(), 8)
641 testlib
.test(OTHER
, "add_pfield-time-bytes-local", treeAddPField ( tree
, AUTC
, tvb
:range(8,8), ENC_BIG_ENDIAN
) )
642 addMatch( NSTime( 0x0000FF0F, 0x00FF000F), 16)
644 testlib
.test(OTHER
, "add_pfield-time-bytes-local-le", treeAddPField ( tree
, AUTC
, tvb
:range(0,8), ENC_LITTLE_ENDIAN
) )
645 addMatch( NSTime(), 8)
647 testlib
.test(OTHER
, "add_pfield-time-bytes-local-le", treeAddPField ( tree
, AUTC
, tvb
:range(8,8), ENC_LITTLE_ENDIAN
) )
648 addMatch( NSTime( 0x0FFF0000, 0x0F00FF00 ), 16)
650 verifyFields("time.ABSOLUTE_UTC", autc_match_fields
)
652 verifyResults("add_pfield-time-bytes-local", autc_match_values
)
654 ----------------------------------------
655 testlib
.testing(OTHER
, "tree:add_packet_field Time string ENC_ISO_8601_DATE_TIME")
658 autc_match_values
= {}
660 local datetimestring1
= "2013-03-01T22:14:48+00:00" -- this is 1362176088 seconds epoch time
661 local tvb1
= ByteArray
.new(datetimestring1
, true):tvb("Date_Time string 1")
662 local datetimestring2
= " 2013-03-02T03:14:48+05:00" -- this is 1362176088 seconds epoch time
663 local tvb2
= ByteArray
.new(datetimestring2
.. " foobar", true):tvb("Date_Time string 2")
664 local datetimestring3
= " 2013-03-01T16:44-05:30" -- this is 1362176040 seconds epoch time
665 local tvb3
= ByteArray
.new(datetimestring3
, true):tvb("Date_Time string 3")
666 local datetimestring4
= "2013-03-02T01:44:00+03:30" -- this is 1362176040 seconds epoch time
667 local tvb4
= ByteArray
.new(datetimestring4
, true):tvb("Date_Time string 4")
668 local datetimestring5
= "2013-03-01T22:14:48Z" -- this is 1362176088 seconds epoch time
669 local tvb5
= ByteArray
.new(datetimestring5
, true):tvb("Date_Time string 5")
670 local datetimestring6
= "2013-03-01T22:14Z" -- this is 1362176040 seconds epoch time
671 local tvb6
= ByteArray
.new(datetimestring6
, true):tvb("Date_Time string 6")
673 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb1
:range(), ENC_ISO_8601_DATE_TIME
) )
674 addMatch( NSTime( 1362176088, 0), string.len(datetimestring1
))
676 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb2
:range(), ENC_ISO_8601_DATE_TIME
) )
677 addMatch( NSTime( 1362176088, 0), string.len(datetimestring2
))
679 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb3
:range(), ENC_ISO_8601_DATE_TIME
) )
680 addMatch( NSTime( 1362176040, 0), string.len(datetimestring3
))
682 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb4
:range(), ENC_ISO_8601_DATE_TIME
) )
683 addMatch( NSTime( 1362176040, 0), string.len(datetimestring4
))
685 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb5
:range(), ENC_ISO_8601_DATE_TIME
) )
686 addMatch( NSTime( 1362176088, 0), string.len(datetimestring5
))
688 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb6
:range(), ENC_ISO_8601_DATE_TIME
) )
689 addMatch( NSTime( 1362176040, 0), string.len(datetimestring6
))
691 verifyFields("time.ABSOLUTE_UTC", autc_match_fields
)
693 verifyResults("add_pfield-datetime-local", autc_match_values
)
695 ----------------------------------------
696 testlib
.testing(OTHER
, "tree:add_packet_field Time string ENC_ISO_8601_DATE")
699 autc_match_values
= {}
701 local datestring1
= "2013-03-01" -- this is 1362096000 seconds epoch time
702 local d_tvb1
= ByteArray
.new(datestring1
, true):tvb("Date string 1")
703 local datestring2
= " 2013-03-01" -- this is 1362096000 seconds epoch time
704 local d_tvb2
= ByteArray
.new(datestring2
.. " foobar", true):tvb("Date string 2")
706 testlib
.test(OTHER
, "add_pfield-date-local", treeAddPField ( tree
, AUTC
, d_tvb1
:range(), ENC_ISO_8601_DATE
) )
707 addMatch( NSTime( 1362096000, 0), string.len(datestring1
))
709 testlib
.test(OTHER
, "add_pfield-date-local", treeAddPField ( tree
, AUTC
, d_tvb2
:range(), ENC_ISO_8601_DATE
) )
710 addMatch( NSTime( 1362096000, 0), string.len(datestring2
))
712 verifyFields("time.ABSOLUTE_UTC", autc_match_fields
)
714 verifyResults("add_pfield-date-local", autc_match_values
)
716 ----------------------------------------
717 testlib
.testing(OTHER
, "tree:add_packet_field Time string ENC_ISO_8601_TIME")
720 autc_match_values
= {}
722 local timestring1
= "22:14:48" -- this is 80088 seconds
723 local t_tvb1
= ByteArray
.new(timestring1
, true):tvb("Time string 1")
724 local timestring2
= " 22:14:48" -- this is 80088 seconds
725 local t_tvb2
= ByteArray
.new(timestring2
.. " foobar", true):tvb("Time string 2")
727 local now
= os
.date("!*t")
731 local timebase
= os
.time( now
)
732 timebase
= timebase
+ timezone
733 print ("timebase = " .. tostring(timebase
) .. ", timezone=" .. timezone
)
735 testlib
.test(OTHER
, "add_pfield-time-local", treeAddPField ( tree
, AUTC
, t_tvb1
:range(), ENC_ISO_8601_TIME
) )
736 addMatch( NSTime( timebase
, 0), string.len(timestring1
))
738 testlib
.test(OTHER
, "add_pfield-time-local", treeAddPField ( tree
, AUTC
, t_tvb2
:range(), ENC_ISO_8601_TIME
) )
739 addMatch( NSTime( timebase
, 0), string.len(timestring2
))
741 verifyFields("time.ABSOLUTE_UTC", autc_match_fields
)
743 verifyResults("add_pfield-time-local", autc_match_values
)
745 ----------------------------------------
746 testlib
.testing(OTHER
, "tree:add_packet_field Time string ENC_IMF_DATE_TIME")
749 autc_match_values
= {}
751 local imfstring1
= "Fri, 01 Mar 13 22:14:48 GMT" -- this is 1362176088 seconds epoch time
752 local imf_tvb1
= ByteArray
.new(imfstring1
, true):tvb("Internet Message Format Time string 1")
753 local imfstring2
= " Fri, 01 Mar 13 22:14:48 GMT" -- this is 1362176088 seconds epoch time
754 local imf_tvb2
= ByteArray
.new(imfstring2
.. " foobar", true):tvb("Internet Message Format Time string 2")
755 local imfstring3
= "Fri, 01 Mar 2013 22:14:48 GMT" -- this is 1362176088 seconds epoch time
756 local imf_tvb3
= ByteArray
.new(imfstring3
, true):tvb("Internet Message Format Time string 3")
757 local imfstring4
= " Fri, 01 Mar 2013 22:14:48 GMT" -- this is 1362176088 seconds epoch time
758 local imf_tvb4
= ByteArray
.new(imfstring4
.. " foobar", true):tvb("Internet Message Format Time string 4")
760 testlib
.test(OTHER
, "add_pfield-time-local", treeAddPField ( tree
, AUTC
, imf_tvb1
:range(), ENC_IMF_DATE_TIME
) )
761 addMatch( NSTime( 1362176088, 0), string.len(imfstring1
))
763 testlib
.test(OTHER
, "add_pfield-time-local", treeAddPField ( tree
, AUTC
, imf_tvb2
:range(), ENC_IMF_DATE_TIME
) )
764 addMatch( NSTime( 1362176088, 0), string.len(imfstring2
))
766 testlib
.test(OTHER
, "add_pfield-time-local", treeAddPField ( tree
, AUTC
, imf_tvb3
:range(), ENC_IMF_DATE_TIME
) )
767 addMatch( NSTime( 1362176088, 0), string.len(imfstring3
))
769 testlib
.test(OTHER
, "add_pfield-time-local", treeAddPField ( tree
, AUTC
, imf_tvb4
:range(), ENC_IMF_DATE_TIME
) )
770 addMatch( NSTime( 1362176088, 0), string.len(imfstring4
))
772 verifyFields("time.ABSOLUTE_UTC", autc_match_fields
)
774 verifyResults("add_pfield-imf-date-time-local", autc_match_values
)
776 ----------------------------------------
777 testlib
.testing(OTHER
, "tree:add_packet_field Time string ENC_ISO_8601_DATE_TIME_BASIC")
780 autc_match_values
= {}
782 local datetimestring1
= "20130301T221448+0000" -- this is 1362176088 seconds epoch time
783 local tvb1
= ByteArray
.new(datetimestring1
, true):tvb("Date_Time string 1")
784 local datetimestring2
= " 20130301171448-0500" -- this is 1362176088 seconds epoch time
785 local tvb2
= ByteArray
.new(datetimestring2
.. " foobar", true):tvb("Date_Time string 2")
786 local datetimestring3
= " 20130301T1644-0530" -- this is 1362176040 seconds epoch time
787 local tvb3
= ByteArray
.new(datetimestring3
, true):tvb("Date_Time string 3")
788 local datetimestring4
= "20130302 014400+0330" -- this is 1362176040 seconds epoch time
789 local tvb4
= ByteArray
.new(datetimestring4
, true):tvb("Date_Time string 4")
790 local datetimestring5
= "20130301T221448Z" -- this is 1362176088 seconds epoch time
791 local tvb5
= ByteArray
.new(datetimestring5
, true):tvb("Date_Time string 5")
792 local datetimestring6
= "201303012214Z" -- this is 1362176040 seconds epoch time
793 local tvb6
= ByteArray
.new(datetimestring6
, true):tvb("Date_Time string 6")
795 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb1
:range(), ENC_ISO_8601_DATE_TIME_BASIC
) )
796 addMatch( NSTime( 1362176088, 0), string.len(datetimestring1
))
798 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb2
:range(), ENC_ISO_8601_DATE_TIME_BASIC
) )
799 addMatch( NSTime( 1362176088, 0), string.len(datetimestring2
))
801 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb3
:range(), ENC_ISO_8601_DATE_TIME_BASIC
) )
802 addMatch( NSTime( 1362176040, 0), string.len(datetimestring3
))
804 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb4
:range(), ENC_ISO_8601_DATE_TIME_BASIC
) )
805 addMatch( NSTime( 1362176040, 0), string.len(datetimestring4
))
807 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb5
:range(), ENC_ISO_8601_DATE_TIME_BASIC
) )
808 addMatch( NSTime( 1362176088, 0), string.len(datetimestring5
))
810 testlib
.test(OTHER
, "add_pfield-datetime-local", treeAddPField ( tree
, AUTC
, tvb6
:range(), ENC_ISO_8601_DATE_TIME_BASIC
) )
811 addMatch( NSTime( 1362176040, 0), string.len(datetimestring6
))
813 verifyFields("time.ABSOLUTE_UTC", autc_match_fields
)
815 verifyResults("add_pfield-datetime-local", autc_match_values
)
817 ----------------------------------------
818 testlib
.testing(OTHER
, "TvbRange subsets")
830 -- This is the same data from the "tree:add_packet_field Bytes" test
831 -- copied here for clarity
832 local bytesstring1
= "deadbeef0123456789DEADBEEFabcdef"
833 local bytestvb1
= ByteArray
.new(bytesstring1
, true):tvb("Bytes hex-string 1")
835 -- tvbrange with no offset or length (control test case)
837 range_raw
= range
:raw()
838 expected
= range
:bytes():raw()
839 testlib
.test(OTHER
, "tvbrange_raw", range_raw
== expected
,
840 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
841 range_raw
= range
:raw(b_offset
)
842 expected
= range
:bytes():raw(b_offset
)
843 testlib
.test(OTHER
, "tvbrange_raw_offset", range_raw
== expected
,
844 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
845 range_raw
= range
:raw(0, b_len
)
846 expected
= range
:bytes():raw(0, b_len
)
847 testlib
.test(OTHER
, "tvbrange_raw_len", range_raw
== expected
,
848 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
849 range_raw
= range
:raw(b_offset
, b_len
)
850 expected
= range
:bytes():raw(b_offset
, b_len
)
851 testlib
.test(OTHER
, "tvbrange_raw_offset_len", range_raw
== expected
,
852 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
854 -- tvbrange with len only
855 range
= bytestvb1(0, len
)
856 range_raw
= range
:raw()
857 expected
= range
:bytes():raw()
858 testlib
.test(OTHER
, "tvbrange_len_raw", range_raw
== expected
,
859 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
860 range_raw
= range
:raw(b_offset
)
861 expected
= range
:bytes():raw(b_offset
)
862 testlib
.test(OTHER
, "tvbrange_len_raw_offset", range_raw
== expected
,
863 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
864 range_raw
= range
:raw(0, b_len
)
865 expected
= range
:bytes():raw(0, b_len
)
866 testlib
.test(OTHER
, "tvbrange_len_raw_len", range_raw
== expected
,
867 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
868 range_raw
= range
:raw(b_offset
, b_len
)
869 expected
= range
:bytes():raw(b_offset
, b_len
)
870 testlib
.test(OTHER
, "tvbrange_len_raw_offset_len", range_raw
== expected
,
871 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
873 -- tvbrange with offset only
874 range
= bytestvb1(offset
)
875 range_raw
= range
:raw()
876 expected
= range
:bytes():raw()
877 testlib
.test(OTHER
, "tvbrange_offset_raw", range_raw
== expected
,
878 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
879 range_raw
= range
:raw(b_offset
)
880 expected
= range
:bytes():raw(b_offset
)
881 testlib
.test(OTHER
, "tvbrange_offset_raw_offset", range_raw
== expected
,
882 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
883 range_raw
= range
:raw(0, b_len
)
884 expected
= range
:bytes():raw(0, b_len
)
885 testlib
.test(OTHER
, "tvbrange_offset_raw_len", range_raw
== expected
,
886 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
887 range_raw
= range
:raw(b_offset
, b_len
)
888 expected
= range
:bytes():raw(b_offset
, b_len
)
889 testlib
.test(OTHER
, "tvbrange_offset_raw_offset_len", range_raw
== expected
,
890 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
892 -- tvbrange with offset and len
893 range
= bytestvb1(offset
, len
)
894 range_raw
= range
:raw()
895 expected
= range
:bytes():raw()
896 testlib
.test(OTHER
, "tvbrange_offset_len_raw", range_raw
== expected
,
897 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
898 range_raw
= range
:raw(b_offset
)
899 expected
= range
:bytes():raw(b_offset
)
900 testlib
.test(OTHER
, "tvbrange_offset_len_raw_offset", range_raw
== expected
,
901 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
902 range_raw
= range
:raw(0, b_len
)
903 expected
= range
:bytes():raw(0, b_len
)
904 testlib
.test(OTHER
, "tvbrange_offset_len_raw_len", range_raw
== expected
,
905 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
906 range_raw
= range
:raw(b_offset
, b_len
)
907 expected
= range
:bytes():raw(b_offset
, b_len
)
908 testlib
.test(OTHER
, "tvbrange_offset_len_raw_offset_len", range_raw
== expected
,
909 string.format('range_raw="%s" expected="%s"', range_raw
, expected
))
911 ----------------------------------------
916 ----------------------------------------
917 -- we want to have our protocol dissection invoked for a specific UDP port,
918 -- so get the udp dissector table and add our protocol to it
919 DissectorTable
.get("udp.port"):add(65333, test_proto
)
920 DissectorTable
.get("udp.port"):add(65346, test_proto
)
922 print("test_proto dissector registered")