Skip to content

Commit

Permalink
protocol5: more parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xHasKx committed Nov 25, 2023
1 parent 9575afb commit 137412c
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 5 deletions.
12 changes: 10 additions & 2 deletions mqtt/protocol5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ local function make_properties(ptype, args)
-- detect property identifier and check it's allowed for that packet type
local prop_id = assert(properties[name], "unknown property: "..tostring(name))
assert(prop_id ~= uprop_id, "user properties should be passed in .user_properties table")
assert(allowed[prop_id], "property "..name.." is not allowed for packet type "..ptype)
if not allowed[prop_id] then
error("property "..name.." is not allowed for packet type "..packet_type[ptype])
end
order[#order + 1] = { prop_id, name, value }
end
-- sort props in the identifier ascending order
Expand Down Expand Up @@ -764,7 +766,9 @@ local function parse_properties(ptype, read_data, input, packet)
return false, "failed to parse property length: "..err
end
if not allowed[prop_id] then
return false, "property "..prop_id.." is not allowed for packet type "..ptype
if not allowed[prop_id] then
return false, "property "..tostring(properties[prop_id]).." ("..prop_id..") is not allowed for that packet type"
end
end
if prop_id == uprop_id then
-- parse name=value string pair
Expand Down Expand Up @@ -1107,6 +1111,10 @@ local function parse_packet_unsubscribe(ptype, flags, input)
return false, packet_type[ptype]..": failed to parse packet properties: "..err
end
-- 3.10.3 UNSUBSCRIBE Payload
-- DOC: An UNSUBSCRIBE packet with no Payload is a Protocol Error.
if input.available == 0 then
return false, packet_type[ptype]..": empty subscriptions list"
end
local subscriptions = {}
while input.available > 0 do
local topic_filter
Expand Down
103 changes: 100 additions & 3 deletions tests/spec/protocol5-parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -803,12 +803,25 @@ describe("MQTT v5.0 protocol: parsing packets: PUBCOMP[7]", function()
end)
end)


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")

it("with invalid empty subscription list", function()
local packet, err = protocol5.parse_packet(make_read_func_hex(
extract_hex[[
82 -- packet type == 8 (SUBSCRIBE), flags == 0x2
03 -- variable length == 3 bytes
0005 -- packet_id
00 -- properties length == 0
]]
))
assert.are.same(false, packet)
assert.are.same("SUBSCRIBE: empty subscriptions list", err)
end)

it("with minimal params, without properties", function()
local packet, err = protocol5.parse_packet(make_read_func_hex(
extract_hex[[
Expand Down Expand Up @@ -932,8 +945,8 @@ describe("MQTT v5.0 protocol: parsing packets: SUBSCRIBE[8]", function()
00 -- Subscription Options == 0
]]
))
assert.are.same('SUBSCRIBE: failed to parse packet properties: it is a Protocol Error to include the subscription_identifiers (11) property more than once', err)
assert.are.same(false, packet)
assert.are.same('SUBSCRIBE: failed to parse packet properties: it is a Protocol Error to include the subscription_identifiers (11) property more than once', err)
end)
end)

Expand Down Expand Up @@ -1021,7 +1034,91 @@ describe("MQTT v5.0 protocol: parsing packets: SUBACK[9]", function()
end)
end)

-- TODO: UNSUBSCRIBE[10]
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")

it("without subscriptions", function()
local packet, err = protocol5.parse_packet(make_read_func_hex(
extract_hex[[
A2 -- packet type == 10 (UNSUBSCRIBE), flags == 0x2
03 -- variable length == 3 bytes
0001 -- packet_id
00 -- properties length == 0
]]
))
assert.are.same(false, packet)
assert.are.same("UNSUBSCRIBE: empty subscriptions list", err)
end)

it("with minimal params, without properties", function()
local packet, err = protocol5.parse_packet(make_read_func_hex(
extract_hex[[
A2 -- packet type == 10 (UNSUBSCRIBE), flags == 0x2
09 -- variable length == 9 bytes
0002 -- packet_id
00 -- properties length == 0
0004 74657374 -- topic name == "test"
]]
))
assert.is_nil(err)
assert.are.same(
{
type=pt.UNSUBSCRIBE, packet_id=2, properties={}, user_properties={},
subscriptions = { "test" },
},
packet
)
end)

it("with invalid property", function()
local packet, err = protocol5.parse_packet(make_read_func_hex(
extract_hex[[
A2 -- packet type == 10 (UNSUBSCRIBE), flags == 0x2
0C -- variable length == 12 bytes
0001 -- packet_id
02 -- properties length == 2
0B 01 -- property 0x0B == 1 -- DOC: 3.3.2.3.8 Subscription Identifier
0005 7465737431 -- topic name == "test1"
]]
))
assert.are.same(false, packet)
assert.are.same("UNSUBSCRIBE: failed to parse packet properties: property subscription_identifiers (11) is not allowed for that packet type", err)
end)

it("with properties and sever lsubscriptions", function()
local packet, err = protocol5.parse_packet(make_read_func_hex(
extract_hex[[
A2 -- packet type == 10 (UNSUBSCRIBE), flags == 0x2
27 -- variable length == 39 bytes
0003 -- packet_id
0F -- properties length == 15
26 0005 68656C6C6F 0005 776F726C64 -- property 0x26 (user) == ("hello", "world") -- DOC: 3.2.2.3.10 User Property
0005 7465737431 -- topic name == "test1"
0005 7465737432 -- topic name == "test2"
0005 7465737433 -- topic name == "test3"
]]
))
assert.is_nil(err)
assert.are.same(
{
type=pt.UNSUBSCRIBE, packet_id=3, properties={}, user_properties={ hello="world" },
subscriptions = { 'test1', 'test2', 'test3' },
},
packet
)
end)
end)

describe("MQTT v5.0 protocol: parsing packets: UNSUBACK[11]", function()
local protocol = require("mqtt.protocol")
Expand Down

0 comments on commit 137412c

Please sign in to comment.