Skip to content

Commit

Permalink
a bit more code covering with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xHasKx committed Nov 25, 2023
1 parent 7213da2 commit 8efe2e6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 4 additions & 2 deletions mqtt/protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
Here is a generic implementation of MQTT protocols of all supported versions.
MQTT v3.1.1 documentation (DOCv3.1.1):
http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html
DOC[1]: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html
MQTT v5.0 documentation (DOCv5.0):
http://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html
DOC[2]: http://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html
CONVENTIONS:
Expand Down Expand Up @@ -263,6 +263,8 @@ local max_mult = 128 * 128 * 128
-- @treturn number parser value
-- @return OR false and error message on failure
function protocol.parse_var_length(read_func)
-- DOC[1]: 2.2.3 Remaining Length
-- DOC[2]: 1.5.5 Variable Byte Integer
assert(type(read_func) == "function", "expecting read_func to be a function")
local mult = 1
local val = 0
Expand Down
8 changes: 8 additions & 0 deletions tests/spec/module-basics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ describe("MQTT lua library component test:", function()
assert.are.equal("01", tools.hex(protocol.make_uint8(1)))
assert.are.equal("32", tools.hex(protocol.make_uint8(0x32)))
assert.are.equal("FF", tools.hex(protocol.make_uint8(0xFF)))
assert.has.errors(function() protocol.make_uint8(0 - 1) end)
assert.has.errors(function() protocol.make_uint8(0xFF + 1) end)
end)

it("protocol.make_uint8_0_or_1", function()
Expand All @@ -147,6 +149,8 @@ describe("MQTT lua library component test:", function()
assert.are.equal("FF00", tools.hex(protocol.make_uint16(0xFF00)))
assert.are.equal("7F4A", tools.hex(protocol.make_uint16(0x7F4A)))
assert.are.equal("FFFF", tools.hex(protocol.make_uint16(0xFFFF)))
assert.has.errors(function() protocol.make_uint16(0 - 1) end)
assert.has.errors(function() protocol.make_uint16(0xFFFF + 1) end)
end)

it("protocol.make_uint16_nonzero", function()
Expand All @@ -158,6 +162,7 @@ describe("MQTT lua library component test:", function()
assert.are.equal("FF00", tools.hex(protocol.make_uint16_nonzero(0xFF00)))
assert.are.equal("7F4A", tools.hex(protocol.make_uint16_nonzero(0x7F4A)))
assert.are.equal("FFFF", tools.hex(protocol.make_uint16_nonzero(0xFFFF)))
assert.has.errors(function() protocol.make_uint16_nonzero(0xFFFF + 1) end)
end)

it("protocol.make_uint32", function()
Expand All @@ -171,6 +176,8 @@ describe("MQTT lua library component test:", function()
assert.are.equal("0000FFFF", tools.hex(protocol.make_uint32(0xFFFF)))
assert.are.equal("7FFFFFFF", tools.hex(protocol.make_uint32(0x7FFFFFFF)))
assert.are.equal("FFFFFFFF", tools.hex(protocol.make_uint32(0xFFFFFFFF)))
assert.has.errors(function() protocol.make_uint32(0 - 1) end)
assert.has.errors(function() protocol.make_uint32(0xFFFFFFFF + 1) end)
end)

it("protocol.make_string", function()
Expand Down Expand Up @@ -263,6 +270,7 @@ describe("MQTT lua library component test:", function()
assert.are.equal(2097152, protocol.parse_var_length(make_read_func("80808001")))
assert.are.equal(268435455, protocol.parse_var_length(make_read_func("FFFFFF7F")))
assert.is_false(protocol.parse_var_length(make_read_func("FFFFFFFF")))
assert.is_false(protocol.parse_var_length(make_read_func("FFFFFFFFFF")))
end)

it("protocol.parse_var_length_nonzero", function()
Expand Down

0 comments on commit 8efe2e6

Please sign in to comment.