-- This is a test script for tshark/wireshark. -- This script runs inside tshark/wireshark, so to run it do: -- wireshark -X lua_script:/lua/struct.lua -- tshark -r bogus.cap -X lua_script:/lua/struct.lua -- Tests Int64/UInt64 functions local function testing(...) print("---- Testing "..tostring(...).." ----") end local function test(name, ...) io.stdout:write("test "..name.."...") if (...) == true then io.stdout:write("passed\n") else io.stdout:write("failed!\n") error(name.." test failed!") end end -- -- auxiliar function to print an hexadecimal `dump' of a given string -- (not used by the test) -- local function tohex(s, sep) local patt = "%02x" .. (sep or "") s = string.gsub(s, "(.)", function(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 ----------------------------- print("Lua version: ".._VERSION) testing("Struct library") local lib = Struct test("global",_G.Struct == lib) for name, val in pairs(lib) do print("\t"..name.." = "..type(val)) end test("class1",type(lib) == 'table') test("class2",type(lib.pack) == 'function') test("class3",type(lib.unpack) == 'function') test("class4",type(lib.size) == 'function') local val1 = "\42\00\00\00\00\00\00\01\00\00\00\02\00\00\00\03\00\00\00\04" local fmt1_le = "!4biii4i4" local fmt1_64le = "!4ieE" local fmt2_be = ">!4bi(ii4)i" testing("basic size") test("basic_size1", lib.size(fmt1_le) == string.len(val1)) 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("basic values") test("basic_values1", lib.values(fmt1_le) == 5) test("basic_values2", lib.values(fmt1_be) == lib.values(fmt1_le)) test("basic_values3", lib.values(fmt1_64le) == 3) test("basic_values4", lib.values(fmt2_be) == lib.values(fmt1_64le)) test("basic_values4", lib.values(" (I) s x i XxX c0") == 3) 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) test("basic_unpack1", ret1 == 42 and ret2 == 0x01000000 and ret3 == 0x02000000 and ret4 == 0x03000000 and ret5 == 0x04000000) test("basic_unpack_position1", pos == string.len(val1) + 1) ret1, ret2, ret3, ret4, ret5, pos = lib.unpack(fmt1_be, val1) test("basic_unpack2", ret1 == 42 and ret2 == 1 and ret3 == 2 and ret4 == 3 and ret5 == 4) test("basic_unpack_position2", pos == string.len(val1) + 1) ret1, ret2, ret3, pos = lib.unpack(fmt1_64le, val1) test("basic_unpack3", ret1 == 42 and ret2 == Int64.new( 0x01000000, 0x02000000) and ret3 == UInt64.new( 0x03000000, 0x04000000)) print(typeof(ret2),typeof(ret3)) test("basic_unpack3b", typeof(ret2) == "Int64" and typeof(ret3) == "UInt64") test("basic_unpack_position3", pos == string.len(val1) + 1) ret1, ret2, ret3, pos = lib.unpack(fmt1_64be, val1) test("basic_unpack4", ret1 == 0x2A000000 and ret2 == Int64.new( 2, 1) and ret3 == UInt64.new( 4, 3)) test("basic_unpack4b", typeof(ret2) == "Int64" and typeof(ret3) == "UInt64") test("basic_unpack_position4", pos == string.len(val1) + 1) ret1, ret2, ret3, pos = lib.unpack(fmt2_be, val1) test("basic_unpack5", ret1 == 42 and ret2 == 1 and ret3 == 4) test("basic_unpack_position5", pos == string.len(val1) + 1) testing("basic pack") local pval1 = lib.pack(fmt1_le, lib.unpack(fmt1_le, val1)) test("basic_pack1", pval1 == val1) test("basic_pack2", val1 == lib.pack(fmt1_be, lib.unpack(fmt1_be, val1))) test("basic_pack3", val1 == lib.pack(fmt1_64le, lib.unpack(fmt1_64le, val1))) test("basic_pack4", val1 == lib.pack(fmt1_64be, lib.unpack(fmt1_64be, val1))) test("basic_pack5", lib.pack(fmt2_be, lib.unpack(fmt1_be, val1)) == lib.pack(">!4biiii", 42, 1, 0, 0, 2)) ---------------------------------- -- following comes from: -- http://www.inf.puc-rio.br/~roberto/struct/teststruct -- unfortunately many of his tests assumed a local machine word -- size of 4 bytes for long and such, so I had to muck with this -- to make it handle 64-bit compiles. -- $Id: teststruct.lua,v 1.2 2008/04/18 20:06:01 roberto Exp $ -- some pack/unpack commands are host-size dependent, so we need to pad local l_pad, ln_pad = "","" if lib.size("l") == 8 then -- the machine running this script uses a long of 8 bytes l_pad = "\00\00\00\00" ln_pad = "\255\255\255\255" end local a,b,c,d,e,f,x testing("pack") test("pack_I",#Struct.pack("I", 67324752) == 4) test("pack_b1",lib.pack('b', 10) == string.char(10)) test("pack_b2",lib.pack('bbb', 10, 20, 30) == string.char(10, 20, 30)) test("pack_h1",lib.pack('h', 10) == string.char(0, 10)) test("pack_h3",lib.pack('l', 10) == l_pad..string.char(0, 0, 0, 10)) test("pack_l3",lib.pack('h', string.char(0, 10)) == 10) test("unpack_h3",lib.unpack('l', l_pad..string.char(0, 1, 0, 10)) == 10 + 2^(2*8)) test("unpack_l3",lib.unpack('', '<'} do local i = 1 for _, l in pairs(lims) do local fmt = a .. l[1] test("limit"..i.."("..l[1]..")", lib.unpack(fmt, lib.pack(fmt, l[2])) == l[2]) i = i + 1 end end testing("fixed-sized ints") -- tests for fixed-sized ints local num = 1 for _, i in pairs{1,2,4} do x = lib.pack('c7", 1, "alo alo") == "\1alo alo") test("string_pack5",lib.pack("!2iiiii", 1, 2, 3, 4, 5) local i = 1 local k = 1 num = 1 while i < #x do local v, j = lib.unpack("!>i", x, i) test("index_unpack"..num, j == i + 4 and v == k) i = j; k = k + 1 num = num + 1 end testing("absolute") -- alignments are relative to 'absolute' positions x = lib.pack("!8 xd", 12) test("absolute_unpack1",lib.unpack("!8d", x, 3) == 12) test("absolute_pack1",lib.pack("lBxxH", -20, 10, 250) == ln_pad..string.char(255, 255, 255, 236, 10, 0, 0, 0, 250)) testing("position") a, b, c, d = lib.unpack(">lBxxH", ln_pad..string.char(255, 255, 255, 236, 10, 0, 0, 0, 250)) -- the 'd' return val is position in string, so will depend on size of long 'l' local vald = 10 + string.len(l_pad) test("position_unpack1",a == -20 and b == 10 and c == 250 and d == vald) a,b,c,d,e = lib.unpack(">fdfH", '000'..lib.pack(">fdfH", 3.5, -24e-5, 200.5, 30000), 4) test("position_unpack2",a == 3.5 and b == -24e-5 and c == 200.5 and d == 30000 and e == 22) a,b,c,d,e = lib.unpack("I2fi4I2", 10, 20, -30, 40001) test("position_pack1",string.len(x) == 2+4+4+2) test("position_unpack4",lib.unpack(">f", x, 3) == 20) a,b,c,d = lib.unpack(">i2fi4I2", x) test("position_unpack5",a == 10 and b == 20 and c == -30 and d == 40001) testing("string length") local s = "hello hello" x = lib.pack(" b c0 ", string.len(s), s) test("stringlen_unpack1",lib.unpack("bc0", x) == s) x = lib.pack("Lc0", string.len(s), s) test("stringlen_unpack2",lib.unpack(" L c0 ", x) == s) x = lib.pack("cc3b", s, s, 0) test("stringlen_pack1",x == "hhel\0") test("stringlen_unpack3",lib.unpack("xxxxb", x) == 0) testing("padding") test("padding_pack1",lib.pack("sh", "aloi", 3) == "aloi\0\0\3") test("format_pack6",lib.pack(">!sh", "aloi", 3) == "aloi\0\0\0\3") x = "aloi\0\0\0\0\3\2\0\0" a, b, c = lib.unpack(">>h >><<