Bugfix: Some packets need fixed flags in header, missed that in the spec

This commit is contained in:
Johannes Schriewer 2018-07-30 02:54:37 +02:00
parent 05b939dfea
commit 31cf6d965d
3 changed files with 25 additions and 14 deletions

View file

@ -376,6 +376,17 @@ Buffer *make_buffer_for_header(size_t sz, MQTTControlPacketType type) {
Buffer *buffer = buffer_allocate(sz);
buffer->data[0] = type << 4;
// MQTT Spec means we should set a bit in the flags field for some packet types
switch (type) {
case PacketTypePubRel:
case PacketTypeSubscribe:
case PacketTypeUnsubscribe:
buffer->data[0] |= 0x02;
break;
default:
break;
}
buffer->position += 1;
variable_length_int_encode(sz - 2, buffer);

View file

@ -260,7 +260,7 @@ TestResult test_decode_publish_with_msg(void) {
TestResult test_decode_puback(void) {
char data[] = {
0x40, 0x02, // header
0x00, 0x0a // packet id
0x00, 0x0a // packet id
};
Buffer *buffer = buffer_from_data_copy(data, sizeof(data));
MQTTPacket *packet = mqtt_packet_decode(buffer);
@ -280,7 +280,7 @@ TestResult test_decode_puback(void) {
TestResult test_decode_pubrec(void) {
char data[] = {
0x50, 0x02, // header
0x00, 0x0a // packet id
0x00, 0x0a // packet id
};
Buffer *buffer = buffer_from_data_copy(data, sizeof(data));
MQTTPacket *packet = mqtt_packet_decode(buffer);
@ -299,8 +299,8 @@ TestResult test_decode_pubrec(void) {
TestResult test_decode_pubrel(void) {
char data[] = {
0x60, 0x02, // header
0x00, 0x0a // packet id
0x62, 0x02, // header
0x00, 0x0a // packet id
};
Buffer *buffer = buffer_from_data_copy(data, sizeof(data));
MQTTPacket *packet = mqtt_packet_decode(buffer);
@ -320,7 +320,7 @@ TestResult test_decode_pubrel(void) {
TestResult test_decode_pubcomp(void) {
char data[] = {
0x70, 0x02, // header
0x00, 0x0a // packet id
0x00, 0x0a // packet id
};
Buffer *buffer = buffer_from_data_copy(data, sizeof(data));
MQTTPacket *packet = mqtt_packet_decode(buffer);
@ -339,7 +339,7 @@ TestResult test_decode_pubcomp(void) {
TestResult test_decode_subscribe(void) {
char data[] = {
0x80, 0x0f, // header
0x82, 0x0f, // header
0x00, 0x0a, // packet id
0x00, 0x0a, 't', 'e', 's', 't', '/', 't', 'o', 'p', 'i', 'c',
0x01 // qos
@ -385,7 +385,7 @@ TestResult test_decode_suback(void) {
TestResult test_decode_unsubscribe(void) {
char data[] = {
0xa0, 0x0e, // header
0xa2, 0x0e, // header
0x00, 0x0a, // packet id
0x00, 0x0a, 't', 'e', 's', 't', '/', 't', 'o', 'p', 'i', 'c',
};

View file

@ -302,7 +302,7 @@ TestResult test_encode_publish_with_msg(void) {
TestResult test_encode_puback(void) {
char data[] = {
0x40, 0x02, // header
0x00, 0x0a // packet id
0x00, 0x0a // packet id
};
PubAckPayload *payload = calloc(1, sizeof(PubAckPayload));
@ -320,7 +320,7 @@ TestResult test_encode_puback(void) {
TestResult test_encode_pubrec(void) {
char data[] = {
0x50, 0x02, // header
0x00, 0x0a // packet id
0x00, 0x0a // packet id
};
PubRecPayload *payload = calloc(1, sizeof(PubRecPayload));
@ -337,8 +337,8 @@ TestResult test_encode_pubrec(void) {
TestResult test_encode_pubrel(void) {
char data[] = {
0x60, 0x02, // header
0x00, 0x0a // packet id
0x62, 0x02, // header
0x00, 0x0a // packet id
};
PubRelPayload *payload = calloc(1, sizeof(PubRelPayload));
@ -356,7 +356,7 @@ TestResult test_encode_pubrel(void) {
TestResult test_encode_pubcomp(void) {
char data[] = {
0x70, 0x02, // header
0x00, 0x0a // packet id
0x00, 0x0a // packet id
};
PubCompPayload *payload = calloc(1, sizeof(PubCompPayload));
@ -373,7 +373,7 @@ TestResult test_encode_pubcomp(void) {
TestResult test_encode_subscribe(void) {
char data[] = {
0x80, 0x0f, // header
0x82, 0x0f, // header
0x00, 0x0a, // packet id
0x00, 0x0a, 't', 'e', 's', 't', '/', 't', 'o', 'p', 'i', 'c',
0x01 // qos
@ -415,7 +415,7 @@ TestResult test_encode_suback(void) {
TestResult test_encode_unsubscribe(void) {
char data[] = {
0xa0, 0x0e, // header
0xa2, 0x0e, // header
0x00, 0x0a, // packet id
0x00, 0x0a, 't', 'e', 's', 't', '/', 't', 'o', 'p', 'i', 'c',
};