Skip to content

Commit

Permalink
retain is bit 0 of the flags, not bit 3 (#24)
Browse files Browse the repository at this point in the history
- retain is bit 0 of the flags, not bit 3
- Add publish message packet header test
  • Loading branch information
paulw11 authored and bhargavg committed May 24, 2017
1 parent 51973d4 commit e1e11b3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SwiftMQTT/SwiftMQTT/Models/MQTTPublishPacket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MQTTPublishPacket: MQTTPacket {
class func fixedHeaderFlags(for message: MQTTPubMsg) -> UInt8 {
var flags = UInt8(0)
if message.retain {
flags |= 0x08
flags |= 0x01
}
flags |= message.QoS.rawValue << 1
return flags
Expand Down
36 changes: 36 additions & 0 deletions SwiftMQTT/SwiftMQTTTests/SwiftMQTTTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,42 @@ class SwiftMQTTTests: XCTestCase, MQTTSessionDelegate {
}
}

func testPublishPacketHeader() {
let retainPubMsg = MQTTPubMsg(topic: "test", payload: "Test".data(using: .utf8)!, retain: true, QoS: .atMostOnce)

let retainPubPacket = MQTTPublishPacket(messageID: 1, message: retainPubMsg)

let retainFlag = retainPubPacket.header.flags & 0x01

XCTAssert(retainFlag == 1, "Header retention bit is not set")

let qos0 = (retainPubPacket.header.flags & 0x06) >> 1

XCTAssert(qos0 == 0, "QoS not 0 for .atMostOnce")

let nonretainPubMsg = MQTTPubMsg(topic:"test", payload:"Test".data(using: .utf8)!, retain:false, QoS: .atLeastOnce)

let nonretainPubPacket = MQTTPublishPacket(messageID: 2, message: nonretainPubMsg)

let nonretainFlag = nonretainPubPacket.header.flags & 0x01

XCTAssert(nonretainFlag == 0, "Header retenion bit should not be set")

let qos1 = (nonretainPubPacket.header.flags & 0x06) >> 1

XCTAssert(qos1 == 1, "QoS not 1 for .atLeastOnce")

let qos2PubMsg = MQTTPubMsg(topic:"test", payload:"Test".data(using: .utf8)!, retain:false, QoS: .exactlyOnce)

let qos2PubPacket = MQTTPublishPacket(messageID: 3, message: qos2PubMsg)


let qos2 = (qos2PubPacket.header.flags & 0x06) >> 1

XCTAssert(qos2 == 2, "QoS not 2 for .exactlyOnce")

}

func testUnSubscribe() {
let expectation = self.expectation(description: "unSubscribe")
mqttSession.unSubscribe(from: ["/hey/cool", "/no/ok"]) { (succeeded, error) -> Void in
Expand Down

0 comments on commit e1e11b3

Please sign in to comment.