diff --git a/mqtt/tools.lua b/mqtt/tools.lua index cba3a90..1867b81 100644 --- a/mqtt/tools.lua +++ b/mqtt/tools.lua @@ -70,6 +70,37 @@ function tools.sortedpairs(tbl) end end +-- Converts multi-line string to a HEX string, removing all whitespace and line-comments started with "--" +function tools.extract_hex(str) + local res = {} + -- iterate through lines + local n = 0 + for line in str:gmatch("[^\n]+") do + n = n + 1 + -- find a comment start + local comment_begin = line:find("--", 1, true) + if comment_begin then + -- remove comment from the line + line = line:sub(1, comment_begin - 1) + end + -- remove all whitespace from the line + line = line:gsub("%s", "") + -- check for the non-hex chars + local non_hex = line:find("[^0-9A-Fa-f]+") + if non_hex then + error(string.format("non-hex char '%s' at %s:%s", line:sub(non_hex, non_hex), n, non_hex)) + end + -- append line to concat list + res[#res + 1] = line + end + -- finally concat all lines onto one HEX-string + local hexstr = tbl_concat(res) + if (#hexstr % 2) ~= 0 then + error("odd number of chars in the resulting HEX string") + end + return hexstr +end + -- export module table return tools diff --git a/tests/spec/module-basics.lua b/tests/spec/module-basics.lua index cec9e6c..c9aa56c 100644 --- a/tests/spec/module-basics.lua +++ b/tests/spec/module-basics.lua @@ -12,11 +12,10 @@ describe("MQTT lua library component test:", function() local tools local protocol - local extract_hex = require("./tools/extract_hex") - it("modules presented", function() tools = require("mqtt.tools") protocol = require("mqtt.protocol") + require("mqtt.const") require("mqtt.client") require("mqtt.ioloop") require("mqtt.luasocket") @@ -64,6 +63,7 @@ describe("MQTT lua library component test:", function() end) it("extract_hex", function() + local extract_hex = assert(tools.extract_hex) assert.are.equal("", extract_hex("")) assert.are.equal("", extract_hex(" ")) assert.are.equal("", extract_hex("\t")) @@ -85,6 +85,9 @@ describe("MQTT lua library component test:", function() 02 03 04 -- other comment ]])) + assert.has.errors(function() extract_hex("0") end) + assert.has.errors(function() extract_hex("x") end) + assert.has.errors(function() extract_hex("xx") end) end) it("tools.div", function() diff --git a/tests/spec/protocol4-make.lua b/tests/spec/protocol4-make.lua index 67cf881..73b856c 100644 --- a/tests/spec/protocol4-make.lua +++ b/tests/spec/protocol4-make.lua @@ -3,7 +3,7 @@ describe("MQTT v3.1.1 protocol: making packets", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = tools.extract_hex local protocol = require("mqtt.protocol") local protocol4 = require("mqtt.protocol4") diff --git a/tests/spec/protocol4-parse.lua b/tests/spec/protocol4-parse.lua index 993f962..d697ed8 100644 --- a/tests/spec/protocol4-parse.lua +++ b/tests/spec/protocol4-parse.lua @@ -2,10 +2,10 @@ -- DOC: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html describe("MQTT v3.1.1 protocol: parsing packets", function() - local extract_hex = require("./tools/extract_hex") local mqtt = require("mqtt") local protocol = require("mqtt.protocol") local protocol4 = require("mqtt.protocol4") + local extract_hex = require("mqtt.tools").extract_hex -- returns read_func-compatible function local function make_read_func_hex(hex) diff --git a/tests/spec/protocol5-make.lua b/tests/spec/protocol5-make.lua index 36de60e..341a213 100644 --- a/tests/spec/protocol5-make.lua +++ b/tests/spec/protocol5-make.lua @@ -3,7 +3,7 @@ describe("MQTT v5.0 protocol: making packets: CONNECT[1]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -193,7 +193,7 @@ end) describe("MQTT v5.0 protocol: making packets: PUBLISH[3]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -327,7 +327,7 @@ end) describe("MQTT v5.0 protocol: making packets: PUBACK[4]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -403,7 +403,7 @@ end) describe("MQTT v5.0 protocol: making packets: PUBREC[5]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -479,7 +479,7 @@ end) describe("MQTT v5.0 protocol: making packets: PUBREL[6]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -555,7 +555,7 @@ end) describe("MQTT v5.0 protocol: making packets: PUBCOMP[7]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -631,7 +631,7 @@ end) describe("MQTT v5.0 protocol: making packets: SUBSCRIBE[8]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -766,7 +766,7 @@ end) describe("MQTT v5.0 protocol: making packets: UNSUBSCRIBE[10]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -836,7 +836,7 @@ end) describe("MQTT v5.0 protocol: making packets: PINGREQ[12]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -857,7 +857,7 @@ end) describe("MQTT v5.0 protocol: making packets: DISCONNECT[14]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") @@ -928,7 +928,7 @@ end) describe("MQTT v5.0 protocol: making packets: AUTH[15]", function() local tools = require("mqtt.tools") - local extract_hex = require("./tools/extract_hex") + local extract_hex = require("mqtt.tools").extract_hex local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") diff --git a/tests/spec/protocol5-parse.lua b/tests/spec/protocol5-parse.lua index a553d18..ae7feaf 100644 --- a/tests/spec/protocol5-parse.lua +++ b/tests/spec/protocol5-parse.lua @@ -26,7 +26,6 @@ local function make_read_func_hex(hex) end end -local extract_hex = require("./tools/extract_hex") describe("MQTT v5.0 protocol: parsing packets: generic", function() local protocol5 = require("mqtt.protocol5") @@ -45,6 +44,7 @@ describe("MQTT v5.0 protocol: parsing packets: CONNECT[1]", function() local mqtt = require("mqtt") local protocol = require("mqtt.protocol") local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("minimal properties", function() assert.are.same( @@ -246,6 +246,7 @@ describe("MQTT v5.0 protocol: parsing packets: CONNACK[2]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("CONNACK with invalid flags", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -367,6 +368,7 @@ describe("MQTT v5.0 protocol: parsing packets: PUBLISH[3]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("PUBLISH with minimal params, without payload and without properties", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -509,6 +511,7 @@ describe("MQTT v5.0 protocol: parsing packets: PUBACK[4]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("with minimal params, without properties", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -583,6 +586,7 @@ describe("MQTT v5.0 protocol: parsing packets: PUBREC[5]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("with minimal params, without properties", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -659,6 +663,7 @@ describe("MQTT v5.0 protocol: parsing packets: PUBREL[6]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("with minimal params, without properties", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -733,6 +738,7 @@ describe("MQTT v5.0 protocol: parsing packets: PUBCOMP[7]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("with minimal params, without properties", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -807,6 +813,7 @@ describe("MQTT v5.0 protocol: parsing packets: SUBSCRIBE[8]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("with invalid empty subscription list", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -954,6 +961,7 @@ describe("MQTT v5.0 protocol: parsing packets: SUBACK[9]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("one subscription, without properties", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -1038,6 +1046,7 @@ describe("MQTT v5.0 protocol: parsing packets: UNSUBSCRIBE[10]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("without subscriptions", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -1124,6 +1133,7 @@ describe("MQTT v5.0 protocol: parsing packets: UNSUBACK[11]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("one subscription, without properties", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -1208,6 +1218,7 @@ describe("MQTT v5.0 protocol: parsing packets: PINGREQ[12]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("the only variant", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -1243,6 +1254,7 @@ describe("MQTT v5.0 protocol: parsing packets: PINGRESP[13]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("the only variant", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -1265,6 +1277,7 @@ describe("MQTT v5.0 protocol: parsing packets: DISCONNECT[14]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("minimal", function() local packet, err = protocol5.parse_packet(make_read_func_hex( @@ -1358,6 +1371,7 @@ describe("MQTT v5.0 protocol: parsing packets: AUTH[15]", function() local protocol = require("mqtt.protocol") local pt = assert(protocol.packet_type) local protocol5 = require("mqtt.protocol5") + local extract_hex = require("mqtt.tools").extract_hex it("minimal", function() local packet, err = protocol5.parse_packet(make_read_func_hex( diff --git a/tools/extract_hex.lua b/tools/extract_hex.lua deleted file mode 100644 index 51f6ce0..0000000 --- a/tools/extract_hex.lua +++ /dev/null @@ -1,21 +0,0 @@ -local tbl_concat = table.concat - --- Extract only HEX symbols from each line of str, ignoring comments after "--" -local function extract_hex(str) - local res = {} - -- iterate through lines - for line in str:gmatch("[^\n]+") do - -- find a comment start - local comment_begin = line:find("--", 1, true) - if comment_begin then - line = line:sub(1, comment_begin - 1) - end - -- remove all non-hex symbols - line = line:gsub("[^0-9A-Fa-f]+", "") - -- and append line to concat list - res[#res + 1] = line - end - return tbl_concat(res) -end - -return extract_hex