aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua
diff options
context:
space:
mode:
authorHuang Qiangxiong <qiangxiong.huang@qq.com>2020-11-22 21:55:47 +0800
committerhuangqiangxiong <qiangxiong.huang@qq.com>2020-12-01 12:06:43 +0000
commitdcf6bdda846430f09898f201f2ae4de9eb7759f6 (patch)
tree1332dccfc7f7bbddc4809da5e8de24e4f86b78b5 /test/lua
parent6a0feb8d0a51fcb950591b1cfa0cb00cf83f3394 (diff)
Protobuf/gRPC: add test cases for Protobuf and gRPC
Add case_dissect_protobuf and case_dissect_grpc in test/suite_dissection.py. Add *.proto into the sub directories of test/protobuf_lang_files/. Run command like 'pytest --program-path .\run\Debug\ -k "grpc or protobuf"' in build directory (in windows) to test these cases only.
Diffstat (limited to 'test/lua')
-rw-r--r--test/lua/protobuf_test_called_by_custom_dissector.lua68
-rw-r--r--test/lua/protobuf_test_field_subdissector_table.lua6
2 files changed, 74 insertions, 0 deletions
diff --git a/test/lua/protobuf_test_called_by_custom_dissector.lua b/test/lua/protobuf_test_called_by_custom_dissector.lua
new file mode 100644
index 0000000000..7a16365eea
--- /dev/null
+++ b/test/lua/protobuf_test_called_by_custom_dissector.lua
@@ -0,0 +1,68 @@
+do
+ local protobuf_dissector = Dissector.get("protobuf")
+
+ -- Create protobuf dissector based on UDP or TCP.
+ -- The UDP dissector will take the whole tvb as a message.
+ -- The TCP dissector will parse tvb as format:
+ -- [4bytes length][a message][4bytes length][a message]...
+ -- @param name The name of the new dissector.
+ -- @param desc The description of the new dissector.
+ -- @param for_udp Register the new dissector to UDP table.(Enable 'Decode as')
+ -- @param for_tcp Register the new dissector to TCP table.(Enable 'Decode as')
+ -- @param msgtype Message type. This must be the root message defined in your .proto file.
+ local function create_protobuf_dissector(name, desc, for_udp, for_tcp, msgtype)
+ local proto = Proto(name, desc)
+ local f_length = ProtoField.uint32(name .. ".length", "Length", base.DEC)
+ proto.fields = { f_length }
+
+ proto.dissector = function(tvb, pinfo, tree)
+ local subtree = tree:add(proto, tvb())
+ if for_udp and pinfo.port_type == 3 then -- UDP
+ if msgtype ~= nil then
+ pinfo.private["pb_msg_type"] = "message," .. msgtype
+ end
+ pcall(Dissector.call, protobuf_dissector, tvb, pinfo, subtree)
+ elseif for_tcp and pinfo.port_type == 2 then -- TCP
+ local offset = 0
+ local remaining_len = tvb:len()
+ while remaining_len > 0 do
+ if remaining_len < 4 then -- head not enough
+ pinfo.desegment_offset = offset
+ pinfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT
+ return -1
+ end
+
+ local data_len = tvb(offset, 4):uint()
+
+ if remaining_len - 4 < data_len then -- data not enough
+ pinfo.desegment_offset = offset
+ pinfo.desegment_len = data_len - (remaining_len - 4)
+ return -1
+ end
+ subtree:add(f_length, tvb(offset, 4))
+
+ if msgtype ~= nil then
+ pinfo.private["pb_msg_type"] = "message," .. msgtype
+ end
+ pcall(Dissector.call, protobuf_dissector,
+ tvb(offset + 4, data_len):tvb(), pinfo, subtree)
+
+ offset = offset + 4 + data_len
+ remaining_len = remaining_len - 4 - data_len
+ end
+ end
+ pinfo.columns.protocol:set(name)
+ end
+
+ if for_udp then DissectorTable.get("udp.port"):add(0, proto) end
+ if for_tcp then DissectorTable.get("tcp.port"):add(0, proto) end
+ return proto
+ end
+
+ -- default pure protobuf udp and tcp dissector without message type
+ create_protobuf_dissector("protobuf_udp", "Protobuf UDP")
+ create_protobuf_dissector("protobuf_tcp", "Protobuf TCP")
+ -- add more protobuf dissectors with message types
+ create_protobuf_dissector("AddrBook", "Tutorial AddressBook",
+ true, true, "tutorial.AddressBook")
+end
diff --git a/test/lua/protobuf_test_field_subdissector_table.lua b/test/lua/protobuf_test_field_subdissector_table.lua
new file mode 100644
index 0000000000..379b2f5a44
--- /dev/null
+++ b/test/lua/protobuf_test_field_subdissector_table.lua
@@ -0,0 +1,6 @@
+-- Test protobuf_field dissector table
+do
+ local protobuf_field_table = DissectorTable.get("protobuf_field")
+ local png_dissector = Dissector.get("png")
+ protobuf_field_table:add("tutorial.Person.portrait_image", png_dissector)
+end