Skip to content

Commit

Permalink
check MessageId doesn't owerflow 65535 (moquette-io#586)
Browse files Browse the repository at this point in the history
Message ID should always be in the range 1 - 65535. This adds limited to test,
and, and resets the counter to 1 when needed

Fixes moquette-io#585
  • Loading branch information
hylkevds authored Jul 2, 2021
1 parent c1cccfa commit 06c64bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public void resendNotAckedPublishes() {
}

int nextPacketId() {
return lastPacketId.incrementAndGet();
return lastPacketId.updateAndGet(v -> v == 65535 ? 1 : v + 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,13 @@ public void testForceClientDisconnection_issue116() {
assertFalse(channel.isOpen(), "First 'FAKE_CLIENT_ID' channel MUST be closed by the broker");
assertTrue(anotherChannel.isOpen(), "Second 'FAKE_CLIENT_ID' channel MUST be still open");
}

@Test
public void testMessageIdGeneration() {
for (int i = 0; i < 65_536; i++) {
int nextPacketId = sut.nextPacketId();
assertTrue(nextPacketId > 0, "Packet ID must be > 0");
assertTrue(nextPacketId <= 65_535, "Packet ID must be <= 65_535");
}
}
}

0 comments on commit 06c64bf

Please sign in to comment.