Skip to content

Commit

Permalink
moved extract_hex function into mqtt.tools lua module
Browse files Browse the repository at this point in the history
  • Loading branch information
xHasKx committed Nov 26, 2023
1 parent 8efe2e6 commit 7ea64e6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 37 deletions.
31 changes: 31 additions & 0 deletions mqtt/tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 5 additions & 2 deletions tests/spec/module-basics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"))
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/protocol4-make.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion tests/spec/protocol4-parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions tests/spec/protocol5-make.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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")

Expand All @@ -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")

Expand Down Expand Up @@ -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")

Expand Down
16 changes: 15 additions & 1 deletion tests/spec/protocol5-parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 0 additions & 21 deletions tools/extract_hex.lua

This file was deleted.

0 comments on commit 7ea64e6

Please sign in to comment.