From 860747e1e78aefdaf31e77408ad590b9d759d1aa Mon Sep 17 00:00:00 2001 From: Hadriel Kaplan Date: Tue, 18 Feb 2014 10:03:04 -0500 Subject: Adds some Lua helper functions: some commonly used functions, and to help troubleshooting Lua scripts There are some common things people need to do, such as convert to/from hex or get the raw binary string in a ByteArray/Tvb/TvbRange. These have been added, as well as some tests for them in the testsuites. Also, functions have been added to allow a script to get all the available tap types and filter fields, since they are not exactly what one can see in the Wireshark gui. Change-Id: I92e5e4eae713bb90d79b0c024eaa4e55b99cc96b Reviewed-on: https://code.wireshark.org/review/249 Reviewed-by: Alexis La Goutte --- test/lua/dissector.lua | 19 ++----------------- test/lua/struct.lua | 23 +++++++++++++++++++++-- 2 files changed, 23 insertions(+), 19 deletions(-) (limited to 'test/lua') diff --git a/test/lua/dissector.lua b/test/lua/dissector.lua index 1bff56cfc0..635cdb5d49 100644 --- a/test/lua/dissector.lua +++ b/test/lua/dissector.lua @@ -167,7 +167,7 @@ local MYDNS_PROTO_UDP_PORT = 65333 -- some forward "declarations" of helper functions we use in the dissector -- I don't usually use this trick, but it'll help reading/grok'ing this script I think -- if we don't focus on them. -local byteArray2String, getQueryName +local getQueryName ---------------------------------------- @@ -326,19 +326,6 @@ udp_encap_table:add(MYDNS_PROTO_UDP_PORT, dns) -- our protocol (Proto) gets automatically registered after this script finishes loading ---------------------------------------- ----------------------------------------- --- a helper function used later --- note that it doesn't use "local" because it's already been declared as a local --- variable way earlier in this script (as a form of forward declaration) -byteArray2String = function (barray, begin, length) - local word = {} - for i = 1, length do - word[i] = string.char(barray:get_index(begin)) - begin = begin + 1 - end - return table.concat(word) -end - ---------------------------------------- -- DNS query names are not just null-terminated strings; they're actually a sequence of -- 'labels', with a length octet before each one. So "foobar.com" is actually the @@ -373,10 +360,8 @@ getQueryName = function (tvbr) return nil, "invalid label length of "..label_len end pos = pos + 1 -- move past label length octet - -- sadly, there's no current way to get a raw Lua string from a ByteArray (nor from Tvb for that matter) - -- so we need to do it one character at a a time -- append the label and a dot to name string - name = name .. byteArray2String(barray, pos, label_len) .. "." + name = name .. barray:raw(pos, label_len) .. "." len_remaining = len_remaining - (label_len + 1) -- subtract label and its length octet label_count = label_count + 1 pos = pos + label_len -- move past label diff --git a/test/lua/struct.lua b/test/lua/struct.lua index 9ff0d59dd3..384e12414e 100644 --- a/test/lua/struct.lua +++ b/test/lua/struct.lua @@ -23,10 +23,17 @@ end -- auxiliar function to print an hexadecimal `dump' of a given string -- (not used by the test) -- -local function bp (s) +local function tohex(s, sep) + local patt = "%02x" .. (sep or "") s = string.gsub(s, "(.)", function(c) - return string.format("\\%02x", string.byte(c)) + return string.format(patt, string.byte(c)) end) + if sep then s = s:sub(1,-(sep:len()+1)) end + return s +end + +local function bp (s) + s = tohex(s) print(s) end @@ -64,6 +71,18 @@ test("basic_size2", lib.size(fmt1_le) == Struct.size(fmt1_be)) test("basic_size3", lib.size(fmt1_le) == Struct.size(fmt1_64le)) test("basic_size4", lib.size(fmt2_be) == Struct.size(fmt1_64le)) +testing("tohex") +local val1hex = "2A:00:00:00:00:00:00:01:00:00:00:02:00:00:00:03:00:00:00:04" +test("tohex1", Struct.tohex(val1) == tohex(val1):upper()) +test("tohex2", Struct.tohex(val1,true) == tohex(val1)) +test("tohex3", Struct.tohex(val1,false,":") == val1hex) +test("tohex4", Struct.tohex(val1,true,":") == val1hex:lower()) + +testing("fromhex") +test("fromhex1", Struct.fromhex(val1hex,":") == val1) +local val1hex2 = val1hex:gsub(":","") +test("fromhex2", Struct.fromhex(val1hex2) == val1) +test("fromhex3", Struct.fromhex(val1hex2:lower()) == val1) testing("basic unpack") local ret1, ret2, ret3, ret4, ret5, pos = lib.unpack(fmt1_le, val1) -- cgit v1.2.3