aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorHadriel Kaplan <hadrielk@yahoo.com>2014-04-12 23:20:15 -0400
committerAnders Broman <a.broman58@gmail.com>2014-04-17 14:04:19 +0000
commitf52626cc83fa6362a286f3ffbda1d3d67392ac3f (patch)
tree3db5bc96ff71bdab2ebc881b3a54d70e868acc3f /test
parentc49be78f4e34124b64479aaaf6877f5839374305 (diff)
Add tvb_get and proto_tree_add for string-encoded byte arrays
This commit adds tvb_get_string_bytes and proto_tree_add_bytes_item routines for getting GByteArrays fields from the tvb when they are encoded in ASCII hex string form. The proto_tree_add_bytes_item routine is also usable for normal binary encoded byte arrays, and has the advantage of retrieving the array values even if there's no proto tree. It also exposes the routines to Lua, both so that a Lua script can take advantage of this, but also so I can write a testsuite to test the functions. Change-Id: I112a038653df6482a5d0ebe7c95708f207319e20 Reviewed-on: https://code.wireshark.org/review/1158 Reviewed-by: Hadriel Kaplan <hadrielk@yahoo.com> Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'test')
-rw-r--r--test/lua/tvb.lua263
-rwxr-xr-xtest/suite-wslua.sh8
2 files changed, 246 insertions, 25 deletions
diff --git a/test/lua/tvb.lua b/test/lua/tvb.lua
index 52f271b9da..25c0daacf3 100644
--- a/test/lua/tvb.lua
+++ b/test/lua/tvb.lua
@@ -45,7 +45,7 @@ end
--
-- CHANGE THIS TO MATCH HOW MANY TESTS THERE ARE
--
-local taptests = { [FRAME]=4, [OTHER]=247 }
+local taptests = { [FRAME]=4, [OTHER]=312 }
local function getResults()
print("\n-----------------------------\n")
@@ -132,14 +132,21 @@ end
----------------------------------------
--- a table of all of our Protocol's fields and test input and expected output
+-- a table of all of our Protocol's fields
local testfield =
{
basic =
{
- STRING = ProtoField.string ("test.basic.string", "Basic string"),
- BOOLEAN = ProtoField.bool ("test.basic.boolean", "Basic boolean", 16, {"yes","no"}, 0x0001),
- UINT16 = ProtoField.uint16 ("test.basic.uint16", "Basic uint16")
+ STRING = ProtoField.string ("test.basic.string", "Basic string"),
+ BOOLEAN = ProtoField.bool ("test.basic.boolean", "Basic boolean", 16, {"yes","no"}, 0x0001),
+ UINT16 = ProtoField.uint16 ("test.basic.uint16", "Basic uint16"),
+ BYTES = ProtoField.bytes ("test.basic.bytes", "Basic Bytes"),
+ UINT_BYTES = ProtoField.ubytes ("test.basic.ubytes", "Basic Uint Bytes"),
+ OID = ProtoField.oid ("test.basic.oid", "Basic OID"),
+ REL_OID = ProtoField.rel_oid("test.basic.rel_oid", "Basic Relative OID"),
+ ABSOLUTE_LOCAL = ProtoField.absolute_time("test.basic.absolute.local","Basic absolute local"),
+ ABSOLUTE_UTC = ProtoField.absolute_time("test.basic.absolute.utc", "Basic absolute utc", 1001),
+ -- GUID = ProtoField.guid ("test.basic.guid", "Basic GUID"),
},
time =
@@ -148,6 +155,14 @@ local testfield =
ABSOLUTE_UTC = ProtoField.absolute_time("test.time.absolute.utc", "Time absolute utc", 1001),
},
+ bytes =
+ {
+ BYTES = ProtoField.bytes ("test.bytes.bytes", "Bytes"),
+ UINT_BYTES = ProtoField.ubytes ("test.bytes.ubytes", "Uint Bytes"),
+ OID = ProtoField.oid ("test.bytes.oid", "OID"),
+ REL_OID = ProtoField.rel_oid("test.bytes.rel_oid", "Relative OID"),
+ -- GUID = ProtoField.guid ("test.bytes.guid", "GUID"),
+ },
}
-- create a flat array table of the above that can be registered
@@ -168,9 +183,16 @@ local getfield =
{
basic =
{
- STRING = Field.new ("test.basic.string"),
- BOOLEAN = Field.new ("test.basic.boolean"),
- UINT16 = Field.new ("test.basic.uint16")
+ STRING = Field.new ("test.basic.string"),
+ BOOLEAN = Field.new ("test.basic.boolean"),
+ UINT16 = Field.new ("test.basic.uint16"),
+ BYTES = Field.new ("test.basic.bytes"),
+ UINT_BYTES = Field.new ("test.basic.ubytes"),
+ OID = Field.new ("test.basic.oid"),
+ REL_OID = Field.new ("test.basic.rel_oid"),
+ ABSOLUTE_LOCAL = Field.new ("test.basic.absolute.local"),
+ ABSOLUTE_UTC = Field.new ("test.basic.absolute.utc"),
+ -- GUID = Field.new ("test.basic.guid"),
},
time =
@@ -179,8 +201,18 @@ local getfield =
ABSOLUTE_UTC = Field.new ("test.time.absolute.utc"),
},
+ bytes =
+ {
+ BYTES = Field.new ("test.bytes.bytes"),
+ UINT_BYTES = Field.new ("test.bytes.ubytes"),
+ OID = Field.new ("test.bytes.oid"),
+ REL_OID = Field.new ("test.bytes.rel_oid"),
+ -- GUID = Field.new ("test.bytes.guid"),
+ },
}
+print("test_proto Fields created")
+
local function addMatchFields(match_fields, ... )
match_fields[#match_fields + 1] = { ... }
end
@@ -303,14 +335,14 @@ function test_proto.dissector(tvbuf,pktinfo,root)
incPktCount(FRAME)
incPktCount(OTHER)
- testing(OTHER, "Basic")
+ testing(OTHER, "Basic string")
local tree = root:add(test_proto, tvbuf:range(0,tvbuf:len()))
-- create a fake Tvb to use for testing
local teststring = "this is the string for the first test"
- local bytearray = ByteArray.new(teststring, true)
- local tvb = bytearray:tvb("Basic")
+ local bytearray = ByteArray.new(teststring, true)
+ local tvb_string = bytearray:tvb("Basic string")
local function callTreeAdd(tree,...)
tree:add(...)
@@ -318,61 +350,242 @@ function test_proto.dissector(tvbuf,pktinfo,root)
local string_match_fields = {}
- execute ("basic-string", tree:add(testfield.basic.STRING, tvb:range(0,tvb:len())) ~= nil )
+ execute ("basic-tvb_get_string", tvb_string:range():string() == teststring )
+
+ execute ("basic-string", tree:add(testfield.basic.STRING, tvb_string:range(0,tvb_string:len())) ~= nil )
addMatchFields(string_match_fields, teststring)
- execute ("basic-string", pcall (callTreeAdd, tree, testfield.basic.STRING, tvb:range() ) )
+ execute ("basic-string", pcall (callTreeAdd, tree, testfield.basic.STRING, tvb_string:range() ) )
addMatchFields(string_match_fields, teststring)
verifyFields("basic.STRING", string_match_fields)
- tvb = ByteArray.new("00FF 0001 8000"):tvb("Basic")
+----------------------------------------
+ testing(OTHER, "Basic boolean")
+
+ local barray_bytes_hex = "00FF00018000"
+ local barray_bytes = ByteArray.new(barray_bytes_hex)
+ local tvb_bytes = barray_bytes:tvb("Basic bytes")
local bool_match_fields = {}
- execute ("basic-boolean", pcall (callTreeAdd, tree, testfield.basic.BOOLEAN, tvb:range(0,2)) )
+ execute ("basic-boolean", pcall (callTreeAdd, tree, testfield.basic.BOOLEAN, tvb_bytes:range(0,2)) )
addMatchFields(bool_match_fields, true)
- execute ("basic-boolean", pcall (callTreeAdd, tree, testfield.basic.BOOLEAN, tvb:range(2,2)) )
+ execute ("basic-boolean", pcall (callTreeAdd, tree, testfield.basic.BOOLEAN, tvb_bytes:range(2,2)) )
addMatchFields(bool_match_fields, true)
- execute ("basic-boolean", pcall (callTreeAdd, tree, testfield.basic.BOOLEAN, tvb:range(4,2)) )
+ execute ("basic-boolean", pcall (callTreeAdd, tree, testfield.basic.BOOLEAN, tvb_bytes:range(4,2)) )
addMatchFields(bool_match_fields, false)
verifyFields("basic.BOOLEAN", bool_match_fields )
+----------------------------------------
+ testing(OTHER, "Basic uint16")
+
local uint16_match_fields = {}
- execute ("basic-uint16", pcall (callTreeAdd, tree, testfield.basic.UINT16, tvb:range(0,2)) )
+ execute ("basic-uint16", pcall (callTreeAdd, tree, testfield.basic.UINT16, tvb_bytes:range(0,2)) )
addMatchFields(uint16_match_fields, 255)
- execute ("basic-uint16", pcall (callTreeAdd, tree, testfield.basic.UINT16, tvb:range(2,2)) )
+ execute ("basic-uint16", pcall (callTreeAdd, tree, testfield.basic.UINT16, tvb_bytes:range(2,2)) )
addMatchFields(uint16_match_fields, 1)
- execute ("basic-uint16", pcall (callTreeAdd, tree, testfield.basic.UINT16, tvb:range(4,2)) )
+ execute ("basic-uint16", pcall (callTreeAdd, tree, testfield.basic.UINT16, tvb_bytes:range(4,2)) )
addMatchFields(uint16_match_fields, 32768)
verifyFields("basic.UINT16", uint16_match_fields)
+----------------------------------------
+ testing(OTHER, "Basic uint16-le")
+
local function callTreeAddLE(tree,...)
tree:add_le(...)
end
- execute ("basic-uint16-le", pcall (callTreeAddLE, tree, testfield.basic.UINT16, tvb:range(0,2)) )
+ execute ("basic-uint16-le", pcall (callTreeAddLE, tree, testfield.basic.UINT16, tvb_bytes:range(0,2)) )
addMatchFields(uint16_match_fields, 65280)
- execute ("basic-uint16-le", pcall (callTreeAddLE, tree, testfield.basic.UINT16, tvb:range(2,2)) )
+ execute ("basic-uint16-le", pcall (callTreeAddLE, tree, testfield.basic.UINT16, tvb_bytes:range(2,2)) )
addMatchFields(uint16_match_fields, 256)
- execute ("basic-uint16-le", pcall (callTreeAddLE, tree, testfield.basic.UINT16, tvb:range(4,2)) )
+ execute ("basic-uint16-le", pcall (callTreeAddLE, tree, testfield.basic.UINT16, tvb_bytes:range(4,2)) )
addMatchFields(uint16_match_fields, 128)
verifyFields("basic.UINT16", uint16_match_fields)
+----------------------------------------
+ testing(OTHER, "Basic bytes")
+
+ local bytes_match_fields = {}
+
+ execute ("basic-tvb_get_string_bytes",
+ string.lower(tostring(tvb_bytes:range():bytes())) == string.lower(barray_bytes_hex))
+
+ execute ("basic-bytes", pcall (callTreeAdd, tree, testfield.basic.BYTES, tvb_bytes:range()) )
+ addMatchFields(bytes_match_fields, barray_bytes)
+
+ -- TODO: it's silly that tree:add_packet_field() requires an encoding argument
+ -- need to fix that separately in a bug fix
+ execute ("add_pfield-bytes", treeAddPField(tree, testfield.basic.BYTES,
+ tvb_bytes:range(), ENC_BIG_ENDIAN))
+ addMatchFields(bytes_match_fields, barray_bytes)
+
+ verifyFields("basic.BYTES", bytes_match_fields)
+
+----------------------------------------
+ testing(OTHER, "Basic uint bytes")
+
+ local len_string = string.format("%02x", barray_bytes:len())
+ local barray_uint_bytes = ByteArray.new(len_string) .. barray_bytes
+ local tvb_uint_bytes = barray_uint_bytes:tvb("Basic UINT_BYTES")
+ local uint_bytes_match_fields = {}
+
+ execute ("basic-uint-bytes", pcall (callTreeAdd, tree, testfield.basic.UINT_BYTES,
+ tvb_uint_bytes:range(0,1)) )
+ addMatchFields(uint_bytes_match_fields, barray_bytes)
+
+ execute ("add_pfield-uint-bytes", treeAddPField(tree, testfield.basic.UINT_BYTES,
+ tvb_uint_bytes:range(0,1), ENC_BIG_ENDIAN) )
+ addMatchFields(uint_bytes_match_fields, barray_bytes)
+
+ verifyFields("basic.UINT_BYTES", uint_bytes_match_fields)
+
+----------------------------------------
+ testing(OTHER, "Basic OID")
+
+ -- note: the tvb being dissected and compared isn't actually a valid OID.
+ -- tree:add() and tree:add_packet-field() don't care about its validity right now.
+
+ local oid_match_fields = {}
+
+ execute ("basic-oid", pcall(callTreeAdd, tree, testfield.basic.OID, tvb_bytes:range()) )
+ addMatchFields(oid_match_fields, barray_bytes)
+
+ execute ("add_pfield-oid", treeAddPField(tree, testfield.basic.OID,
+ tvb_bytes:range(), ENC_BIG_ENDIAN) )
+ addMatchFields(oid_match_fields, barray_bytes)
+
+ verifyFields("basic.OID", oid_match_fields)
+
+----------------------------------------
+ testing(OTHER, "Basic REL_OID")
+
+ -- note: the tvb being dissected and compared isn't actually a valid OID.
+ -- tree:add() and tree:add_packet-field() don't care about its validity right now.
+
+ local rel_oid_match_fields = {}
+
+ execute ("basic-rel-oid", pcall(callTreeAdd, tree, testfield.basic.REL_OID, tvb_bytes:range()))
+ addMatchFields(rel_oid_match_fields, barray_bytes)
+
+ execute ("add_pfield-rel_oid", treeAddPField(tree, testfield.basic.REL_OID,
+ tvb_bytes:range(), ENC_BIG_ENDIAN) )
+ addMatchFields(rel_oid_match_fields, barray_bytes)
+
+ verifyFields("basic.REL_OID", rel_oid_match_fields)
+
+ -- TODO: a FT_GUID is not really a ByteArray, so we can't simply treat it as one
+ -- local barray_guid = ByteArray.new("00FF0001 80001234 567890AB CDEF00FF")
+ -- local tvb_guid = barray_guid:tvb("Basic GUID")
+ -- local guid_match_fields = {}
+
+ -- execute ("basic-guid", pcall(callTreeAdd, tree, testfield.basic.GUID, tvb_guid:range()) )
+ -- addMatchFields(guid_match_fields, barray_guid)
+
+ -- execute ("add_pfield-guid", treeAddPField(tree, testfield.basic.GUID,
+ -- tvb_guid:range(), ENC_BIG_ENDIAN) )
+ -- addMatchFields(guid_match_fields, barray_guid)
+
+ -- verifyFields("basic.GUID", guid_match_fields)
+
+
+----------------------------------------
+ testing(OTHER, "tree:add_packet_field Bytes")
+
+ resetResults()
+ bytes_match_fields = {}
+ local bytes_match_values = {}
+
+ -- something to make this easier to read
+ local function addMatch(...)
+ addMatchFieldValues(bytes_match_fields, bytes_match_values, ...)
+ end
+
+ local bytesstring1 = "deadbeef0123456789DEADBEEFabcdef"
+ local bytesstring = ByteArray.new(bytesstring1) -- the binary version of above, for comparing
+ local bytestvb1 = ByteArray.new(bytesstring1, true):tvb("Bytes hex-string 1")
+ local bytesstring2 = " de:ad:be:ef:01:23:45:67:89:DE:AD:BE:EF:ab:cd:ef"
+ local bytestvb2 = ByteArray.new(bytesstring2 .. "-f0-00 foobar", true):tvb("Bytes hex-string 2")
+
+ local bytestvb1_decode = bytestvb1:range():bytes(ENC_STR_HEX + ENC_SEP_NONE + ENC_SEP_COLON + ENC_SEP_DASH)
+ execute ("tvb_get_string_bytes", string.lower(tostring(bytestvb1_decode)) == string.lower(tostring(bytesstring1)))
+
+ execute ("add_pfield-bytes1", treeAddPField(tree, testfield.bytes.BYTES,
+ bytestvb1:range(),
+ ENC_STR_HEX + ENC_SEP_NONE +
+ ENC_SEP_COLON + ENC_SEP_DASH))
+ addMatch(bytesstring, string.len(bytesstring1))
+
+ execute ("add_pfield-bytes2", treeAddPField(tree, testfield.bytes.BYTES,
+ bytestvb2:range(),
+ ENC_STR_HEX + ENC_SEP_NONE +
+ ENC_SEP_COLON + ENC_SEP_DASH))
+ addMatch(bytesstring, string.len(bytesstring2))
+
+ verifyResults("add_pfield-bytes", bytes_match_values)
+ verifyFields("bytes.BYTES", bytes_match_fields)
+
+
+----------------------------------------
+ testing(OTHER, "tree:add_packet_field OID")
+
+ resetResults()
+ bytes_match_fields = {}
+ bytes_match_values = {}
+
+ execute ("add_pfield-oid1", treeAddPField(tree, testfield.bytes.OID,
+ bytestvb1:range(),
+ ENC_STR_HEX + ENC_SEP_NONE +
+ ENC_SEP_COLON + ENC_SEP_DASH))
+ addMatch(bytesstring, string.len(bytesstring1))
+
+ execute ("add_pfield-oid2", treeAddPField(tree, testfield.bytes.OID,
+ bytestvb2:range(),
+ ENC_STR_HEX + ENC_SEP_NONE +
+ ENC_SEP_COLON + ENC_SEP_DASH))
+ addMatch(bytesstring, string.len(bytesstring2))
+
+ verifyResults("add_pfield-oid", bytes_match_values)
+ verifyFields("bytes.OID", bytes_match_fields)
+
+
+----------------------------------------
+ testing(OTHER, "tree:add_packet_field REL_OID")
+
+ resetResults()
+ bytes_match_fields = {}
+ bytes_match_values = {}
+
+ execute ("add_pfield-rel_oid1", treeAddPField(tree, testfield.bytes.REL_OID,
+ bytestvb1:range(),
+ ENC_STR_HEX + ENC_SEP_NONE +
+ ENC_SEP_COLON + ENC_SEP_DASH))
+ addMatch(bytesstring, string.len(bytesstring1))
+
+ execute ("add_pfield-rel_oid2", treeAddPField(tree, testfield.bytes.REL_OID,
+ bytestvb2:range(),
+ ENC_STR_HEX + ENC_SEP_NONE +
+ ENC_SEP_COLON + ENC_SEP_DASH))
+ addMatch(bytesstring, string.len(bytesstring2))
+
+ verifyResults("add_pfield-rel_oid", bytes_match_values)
+ verifyFields("bytes.REL_OID", bytes_match_fields)
+
----------------------------------------
testing(OTHER, "tree:add Time")
- tvb = ByteArray.new("00000000 00000000 0000FF0F 00FF000F"):tvb("Time")
+ local tvb = ByteArray.new("00000000 00000000 0000FF0F 00FF000F"):tvb("Time")
local ALOCAL = testfield.time.ABSOLUTE_LOCAL
local alocal_match_fields = {}
@@ -414,7 +627,7 @@ function test_proto.dissector(tvbuf,pktinfo,root)
local autc_match_values = {}
-- something to make this easier to read
- local function addMatch(...)
+ addMatch = function(...)
addMatchFieldValues(autc_match_fields, autc_match_values, ...)
end
diff --git a/test/suite-wslua.sh b/test/suite-wslua.sh
index fdeb5f0f2f..04e7a70ca3 100755
--- a/test/suite-wslua.sh
+++ b/test/suite-wslua.sh
@@ -371,6 +371,14 @@ wslua_step_tvb_test() {
fi
# Tshark catches lua script failures, so we have to parse the output.
+ # perform this twice: once with a tree, once without
+ $TSHARK -r $CAPTURE_DIR/dns_port.pcap -X lua_script:$TESTS_DIR/lua/tvb.lua -V > testout.txt 2>&1
+ grep -q "All tests passed!" testout.txt
+ if [ $? -ne 0 ]; then
+ cat testout.txt
+ test_step_failed "lua_args_test test 1 failed"
+ fi
+
$TSHARK -r $CAPTURE_DIR/dns_port.pcap -X lua_script:$TESTS_DIR/lua/tvb.lua > testout.txt 2>&1
if grep -q "All tests passed!" testout.txt; then
test_step_ok