Refactor property functions to common static library.

This commit is contained in:
Roger A. Light 2024-03-17 15:07:27 +00:00
parent b5c25cf1d0
commit 9fe8b95dcf
50 changed files with 1344 additions and 1235 deletions

View file

@ -22,6 +22,7 @@ target_include_directories(mosquitto_db_dump PRIVATE
"${mosquitto_SOURCE_DIR}/common"
"${mosquitto_SOURCE_DIR}/include"
"${mosquitto_SOURCE_DIR}/lib"
"${mosquitto_SOURCE_DIR}/libcommon"
"${mosquitto_SOURCE_DIR}/src"
"${OPENSSL_INCLUDE_DIR}"
)

View file

@ -35,38 +35,39 @@ static void print__properties(mosquitto_property *properties)
printf("\tProperties:\n");
while(properties){
switch(properties->identifier){
switch(mosquitto_property_identifier(properties)){
/* Only properties for base messages are valid for saving */
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
printf("\t\tPayload format indicator: %d\n", properties->value.i8);
printf("\t\tPayload format indicator: %d\n", mosquitto_property_byte_value(properties));
break;
case MQTT_PROP_CONTENT_TYPE:
printf("\t\tContent type: %s\n", properties->value.s.v);
printf("\t\tContent type: %s\n", mosquitto_property_string_value(properties));
break;
case MQTT_PROP_RESPONSE_TOPIC:
printf("\t\tResponse topic: %s\n", properties->value.s.v);
printf("\t\tResponse topic: %s\n", mosquitto_property_string_value(properties));
break;
case MQTT_PROP_CORRELATION_DATA:
printf("\t\tCorrelation data: ");
for(i=0; i<properties->value.bin.len; i++){
printf("%02X", properties->value.bin.v[i]);
const uint8_t *bin = mosquitto_property_binary_value(properties);
for(i=0; i<mosquitto_property_binary_value_length(properties); i++){
printf("%02X", bin[i]);
}
printf("\n");
break;
case MQTT_PROP_USER_PROPERTY:
printf("\t\tUser property: %s , %s\n", properties->name.v, properties->value.s.v);
printf("\t\tUser property: %s , %s\n", mosquitto_property_string_name(properties), mosquitto_property_string_value(properties));
break;
default:
printf("\t\tInvalid property type: %d\n", properties->identifier);
printf("\t\tInvalid property type: %d\n", mosquitto_property_identifier(properties));
break;
}
properties = properties->next;
properties = mosquitto_property_next(properties);
}
}

View file

@ -745,6 +745,11 @@ libmosq_EXPORT const char *mosquitto_property_identifier_to_string(int identifie
libmosq_EXPORT int mosquitto_string_to_property_info(const char *propname, int *identifier, int *type);
void mosquitto_property_free(mosquitto_property **property);
unsigned int mosquitto_property_get_length(const mosquitto_property *property);
unsigned int mosquitto_property_get_length_all(const mosquitto_property *property);
unsigned int mosquitto_property_get_remaining_length(const mosquitto_property *props);
#ifdef __cplusplus
}
#endif

View file

@ -19,6 +19,8 @@ Contributors:
#ifndef MQTT_PROTOCOL_H
#define MQTT_PROTOCOL_H
#include <stdint.h>
/*
* File: mosquitto/mqtt_protocol.h
*
@ -289,4 +291,6 @@ enum mqtt5_sub_options {
#define MQTT_SUB_OPT_SET(opt, val) ((opt) |= val)
#define MQTT_SUB_OPT_CLEAR(opt, val) ((opt) = (opt) & !val)
unsigned int mosquitto_varint_bytes(uint32_t size);
#endif

View file

@ -108,6 +108,7 @@ target_include_directories(libmosquitto
PRIVATE
"${mosquitto_SOURCE_DIR}"
"${mosquitto_SOURCE_DIR}/common"
"${mosquitto_SOURCE_DIR}/libcommon"
)
if(WITH_BUNDLED_DEPS)

View file

@ -2,7 +2,7 @@ R=..
include ${R}/config.mk
LOCAL_CFLAGS+=-fPIC
LOCAL_CPPFLAGS+=
LOCAL_CPPFLAGS+=-I${R}/libcommon
LOCAL_LDFLAGS+=-Wl,--version-script=linker.version -Wl,-soname,libmosquitto.so.$(SOVERSION) -fPIC -shared
LOCAL_LIBADD+=-lcjson -lc ${LIB_ARGON2} ${LIBMOSQ_COMMON}
STATIC_LIB_DEPS:=

View file

@ -26,6 +26,7 @@ Contributors:
#include "mosquitto/mqtt_protocol.h"
#include "packet_mosq.h"
#include "property_mosq.h"
#include "property_common.h"
#include "send_mosq.h"
#include "util_mosq.h"
@ -98,7 +99,7 @@ int mosquitto_publish_v5(struct mosquitto *mosq, int *mid, const char *topic, in
}
if(mosq->maximum_packet_size > 0){
remaining_length = 1 + 2+(uint32_t)tlen + (uint32_t)payloadlen + property__get_length_all(outgoing_properties);
remaining_length = 1 + 2+(uint32_t)tlen + (uint32_t)payloadlen + mosquitto_property_get_length_all(outgoing_properties);
if(qos > 0){
remaining_length++;
}

View file

@ -23,6 +23,7 @@ Contributors:
#include "mosquitto/mqtt_protocol.h"
#include "net_mosq.h"
#include "packet_mosq.h"
#include "property_common.h"
#include "send_mosq.h"
@ -77,7 +78,7 @@ int mosquitto_subscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count
}
if(mosq->maximum_packet_size > 0){
remaining_length += 2 + property__get_length_all(outgoing_properties);
remaining_length += 2 + mosquitto_property_get_length_all(outgoing_properties);
if(packet__check_oversize(mosq, remaining_length)){
return MOSQ_ERR_OVERSIZE_PACKET;
}

View file

@ -25,6 +25,7 @@ Contributors:
#include "net_mosq.h"
#include "packet_mosq.h"
#include "property_mosq.h"
#include "property_common.h"
#include "send_mosq.h"
@ -73,7 +74,7 @@ int mosquitto_unsubscribe_multiple(struct mosquitto *mosq, int *mid, int sub_cou
}
if(mosq->maximum_packet_size > 0){
remaining_length += 2U + property__get_length_all(outgoing_properties);
remaining_length += 2U + mosquitto_property_get_length_all(outgoing_properties);
if(packet__check_oversize(mosq, remaining_length)){
return MOSQ_ERR_OVERSIZE_PACKET;
}

View file

@ -27,6 +27,7 @@ Contributors:
#include "logging_mosq.h"
#include "messages_mosq.h"
#include "packet_mosq.h"
#include "property_common.h"
#include "net_mosq.h"
#include "send_mosq.h"
#include "socks_mosq.h"

View file

@ -251,19 +251,3 @@ int packet__write_varint(struct mosquitto__packet *packet, uint32_t word)
}
return MOSQ_ERR_SUCCESS;
}
unsigned int packet__varint_bytes(uint32_t word)
{
if(word < 128){
return 1;
}else if(word < 16384){
return 2;
}else if(word < 2097152){
return 3;
}else if(word < 268435456){
return 4;
}else{
return 5;
}
}

View file

@ -227,7 +227,7 @@ int packet__check_oversize(struct mosquitto *mosq, uint32_t remaining_length)
if(mosq->maximum_packet_size == 0) return MOSQ_ERR_SUCCESS;
len = remaining_length + packet__varint_bytes(remaining_length);
len = remaining_length + mosquitto_varint_bytes(remaining_length);
if(len > mosq->maximum_packet_size){
return MOSQ_ERR_OVERSIZE_PACKET;
}else{

View file

@ -45,8 +45,6 @@ void packet__write_uint16(struct mosquitto__packet *packet, uint16_t word);
void packet__write_uint32(struct mosquitto__packet *packet, uint32_t word);
int packet__write_varint(struct mosquitto__packet *packet, uint32_t word);
unsigned int packet__varint_bytes(uint32_t word);
int packet__write(struct mosquitto *mosq);
int packet__read(struct mosquitto *mosq);

File diff suppressed because it is too large Load diff

View file

@ -19,37 +19,8 @@ Contributors:
#define PROPERTY_MOSQ_H
#include "mosquitto_internal.h"
#include "mosquitto.h"
struct mqtt__string {
char *v;
uint16_t len;
};
struct mqtt5__property {
struct mqtt5__property *next;
union {
uint8_t i8;
uint16_t i16;
uint32_t i32;
uint32_t varint;
struct mqtt__string bin;
struct mqtt__string s;
} value;
struct mqtt__string name;
int32_t identifier;
uint8_t property_type;
bool client_generated;
};
int property__read_all(int command, struct mosquitto__packet_in *packet, mosquitto_property **property);
int property__write_all(struct mosquitto__packet *packet, const mosquitto_property *property, bool write_len);
void property__free(mosquitto_property **property);
unsigned int property__get_length(const mosquitto_property *property);
unsigned int property__get_length_all(const mosquitto_property *property);
unsigned int property__get_remaining_length(const mosquitto_property *props);
#endif

View file

@ -81,9 +81,9 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session
version = MQTT_PROTOCOL_V5;
headerlen = 10;
proplen = 0;
proplen += property__get_length_all(properties);
proplen += property__get_length_all(local_props);
varbytes = packet__varint_bytes(proplen);
proplen += mosquitto_property_get_remaining_length(properties);
proplen += mosquitto_property_get_remaining_length(local_props);
varbytes = mosquitto_varint_bytes(proplen);
headerlen += proplen + varbytes;
}else if(mosq->protocol == mosq_p_mqtt311){
version = MQTT_PROTOCOL_V311;
@ -110,7 +110,7 @@ int send__connect(struct mosquitto *mosq, uint16_t keepalive, bool clean_session
payloadlen += (uint32_t)(2+strlen(mosq->will->msg.topic) + 2+(uint32_t)mosq->will->msg.payloadlen);
if(mosq->protocol == mosq_p_mqtt5){
payloadlen += property__get_remaining_length(mosq->will->properties);
payloadlen += mosquitto_property_get_remaining_length(mosq->will->properties);
}
}

View file

@ -59,7 +59,7 @@ int send__disconnect(struct mosquitto *mosq, uint8_t reason_code, const mosquitt
if(mosq->protocol == mosq_p_mqtt5 && (reason_code != 0 || properties)){
remaining_length = 1;
if(properties){
remaining_length += property__get_remaining_length(properties);
remaining_length += mosquitto_property_get_remaining_length(properties);
}
}else{
remaining_length = 0;

View file

@ -141,7 +141,7 @@ int send__command_with_mid(struct mosquitto *mosq, uint8_t command, uint16_t mid
}
if(properties){
remaining_length += property__get_remaining_length(properties);
remaining_length += mosquitto_property_get_remaining_length(properties);
}
}

View file

@ -36,6 +36,7 @@ Contributors:
#include "net_mosq.h"
#include "packet_mosq.h"
#include "property_mosq.h"
#include "property_common.h"
#include "send_mosq.h"
#include "utlist.h"
@ -224,7 +225,7 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic,
if(qos > 0) packetlen += 2; /* For message id */
if(mosq->protocol == mosq_p_mqtt5){
proplen = 0;
proplen += property__get_length_all(store_props);
proplen += mosquitto_property_get_length_all(store_props);
if(expiry_interval > 0){
expiry_prop.next = NULL;
expiry_prop.value.i32 = expiry_interval;
@ -232,7 +233,7 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic,
expiry_prop.property_type = MQTT_PROP_TYPE_INT32;
expiry_prop.client_generated = false;
proplen += property__get_length_all(&expiry_prop);
proplen += mosquitto_property_get_length_all(&expiry_prop);
}
#ifdef WITH_BROKER
if(topic_alias != 0){
@ -242,7 +243,7 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic,
topic_alias_prop.property_type = MQTT_PROP_TYPE_INT16;
topic_alias_prop.client_generated = false;
proplen += property__get_length_all(&topic_alias_prop);
proplen += mosquitto_property_get_length_all(&topic_alias_prop);
}
if(subscription_identifier){
subscription_id_prop.next = NULL;
@ -251,11 +252,11 @@ int send__real_publish(struct mosquitto *mosq, uint16_t mid, const char *topic,
subscription_id_prop.property_type = MQTT_PROP_TYPE_VARINT;
subscription_id_prop.client_generated = false;
proplen += property__get_length_all(&subscription_id_prop);
proplen += mosquitto_property_get_length_all(&subscription_id_prop);
}
#endif
varbytes = packet__varint_bytes(proplen);
varbytes = mosquitto_varint_bytes(proplen);
if(varbytes > 4){
/* FIXME - Properties too big, don't publish any - should remove some first really */
store_props = NULL;

View file

@ -50,7 +50,7 @@ int send__subscribe(struct mosquitto *mosq, int *mid, int topic_count, char *con
packetlen = 2;
if(mosq->protocol == mosq_p_mqtt5){
packetlen += property__get_remaining_length(properties);
packetlen += mosquitto_property_get_remaining_length(properties);
}
for(i=0; i<topic_count; i++){
tlen = strlen(topic[i]);

View file

@ -57,7 +57,7 @@ int send__unsubscribe(struct mosquitto *mosq, int *mid, int topic_count, char *c
}
if(mosq->protocol == mosq_p_mqtt5){
packetlen += property__get_remaining_length(properties);
packetlen += mosquitto_property_get_remaining_length(properties);
}
rc = packet__alloc(&packet, CMD_UNSUBSCRIBE | 2, packetlen);

View file

@ -1,5 +1,7 @@
set(C_SRC
memory_common.c
mqtt_common.c
property_common.c
strings_common.c
time_common.c
topic_common.c

View file

@ -13,6 +13,8 @@ LOCAL_LIBADD+=
OBJS= \
memory_common.o \
mqtt_common.o \
property_common.o \
strings_common.o \
time_common.o \
topic_common.o \

39
libcommon/mqtt_common.c Normal file
View file

@ -0,0 +1,39 @@
/*
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <stdint.h>
#include "mosquitto/mqtt_protocol.h"
unsigned int mosquitto_varint_bytes(uint32_t word)
{
if(word < 128){
return 1;
}else if(word < 16384){
return 2;
}else if(word < 2097152){
return 3;
}else if(word < 268435456){
return 4;
}else{
return 5;
}
}

1138
libcommon/property_common.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,47 @@
/*
Copyright (c) 2018-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#ifndef PROPERTY_COMMON_H
#define PROPERTY_COMMON_H
#include <stdbool.h>
#include <stdint.h>
#include "mosquitto.h"
struct mqtt__string {
char *v;
uint16_t len;
};
struct mqtt5__property {
struct mqtt5__property *next;
union {
uint8_t i8;
uint16_t i16;
uint32_t i32;
uint32_t varint;
struct mqtt__string bin;
struct mqtt__string s;
} value;
struct mqtt__string name;
int32_t identifier;
uint8_t property_type;
bool client_generated;
};
#endif

View file

@ -232,6 +232,7 @@ target_include_directories(mosquitto
PRIVATE
"${mosquitto_SOURCE_DIR}/common"
"${mosquitto_SOURCE_DIR}/lib"
"${mosquitto_SOURCE_DIR}/libcommon"
"${mosquitto_SOURCE_DIR}/src"
)
if(WITH_BUNDLED_DEPS)

View file

@ -4,7 +4,7 @@ include ${R}/config.mk
.PHONY: all install uninstall clean reallyclean
LOCAL_CFLAGS+=
LOCAL_CPPFLAGS+=-DWITH_BROKER -I${R}/lib
LOCAL_CPPFLAGS+=-DWITH_BROKER -I${R}/lib -I${R}/libcommon
LOCAL_LDFLAGS+=
LOCAL_LDADD+=-lcjson -lm ${LIB_ARGON2} ${LIBMOSQ_COMMON}

View file

@ -41,12 +41,12 @@ Contributors:
#include <ws2tcpip.h>
#endif
#include "mosquitto/mqtt_protocol.h"
#include "mosquitto.h"
#include "mosquitto_broker_internal.h"
#include "mosquitto_internal.h"
#include "net_mosq.h"
#include "packet_mosq.h"
#include "property_common.h"
#include "send_mosq.h"
#include "sys_tree.h"
#include "tls_mosq.h"

View file

@ -48,6 +48,7 @@ Contributors:
#include "mosquitto_broker_internal.h"
#include "mosquitto/mqtt_protocol.h"
#include "packet_mosq.h"
#include "property_common.h"
#include "send_mosq.h"
#include "sys_tree.h"
#include "util_mosq.h"
@ -116,7 +117,7 @@ static void read_message_expiry_interval(mosquitto_property **proplist, uint32_t
}else{
previous->next = mosquitto_property_next(p);
}
property__free(&p);
mosquitto_property_free(&p);
return;
}

View file

@ -34,6 +34,7 @@ Contributors:
#include "mosquitto_broker_internal.h"
#include "persist.h"
#include "packet_mosq.h"
#include "property_common.h"
#include "property_mosq.h"
#include "util_mosq.h"
@ -95,7 +96,7 @@ int persist__chunk_client_msg_write_v6(FILE *db_fptr, struct P_client_msg *chunk
if(chunk->subscription_identifier){
subscription_id_prop.value.varint = chunk->subscription_identifier;
proplen += property__get_remaining_length(&subscription_id_prop);
proplen += mosquitto_property_get_remaining_length(&subscription_id_prop);
}
chunk->F.mid = htons(chunk->F.mid);
@ -145,7 +146,7 @@ int persist__chunk_message_store_write_v6(FILE *db_fptr, struct P_base_msg *chun
int rc;
if(chunk->properties){
proplen += property__get_remaining_length(chunk->properties);
proplen += mosquitto_property_get_remaining_length(chunk->properties);
}
chunk->F.payloadlen = htonl(chunk->F.payloadlen);

View file

@ -24,6 +24,7 @@ Contributors:
#include "mosquitto_broker_internal.h"
#include "mosquitto/mqtt_protocol.h"
#include "property_mosq.h"
#include "property_common.h"
/* Process the incoming properties, we should be able to assume that only valid
* properties for CONNECT are present here. */

View file

@ -47,7 +47,7 @@ int send__auth(struct mosquitto *context, uint8_t reason_code, const void *auth_
if(rc) goto error;
}
remaining_length += property__get_remaining_length(properties);
remaining_length += mosquitto_property_get_remaining_length(properties);
rc = packet__check_oversize(context, remaining_length);
if(rc) goto error;

View file

@ -75,7 +75,7 @@ int send__connack(struct mosquitto *context, uint8_t ack, uint8_t reason_code, c
}
}
remaining_length += property__get_remaining_length(connack_props);
remaining_length += mosquitto_property_get_remaining_length(connack_props);
}
if(packet__check_oversize(context, remaining_length)){

View file

@ -37,7 +37,7 @@ int send__suback(struct mosquitto *context, uint16_t mid, uint32_t payloadlen, c
remaining_length = 2+payloadlen;
if(context->protocol == mosq_p_mqtt5){
remaining_length += property__get_remaining_length(properties);
remaining_length += mosquitto_property_get_remaining_length(properties);
}
rc = packet__alloc(&packet, CMD_SUBACK, remaining_length);
if(rc){

View file

@ -37,7 +37,7 @@ int send__unsuback(struct mosquitto *mosq, uint16_t mid, int reason_code_count,
remaining_length = 2;
if(mosq->protocol == mosq_p_mqtt5){
remaining_length += property__get_remaining_length(properties);
remaining_length += mosquitto_property_get_remaining_length(properties);
remaining_length += (uint32_t)reason_code_count;
}

View file

@ -4,6 +4,7 @@ add_library(bridge-topic-obj
../../../src/bridge_topic.c
)
target_compile_definitions(bridge-topic-obj PRIVATE WITH_BRIDGE WITH_BROKER)
target_include_directories(bridge-topic-obj PRIVATE ${mosquitto_SOURCE_DIR}/libcommon)
target_link_libraries(bridge-topic-obj PUBLIC common-unit-test-header)
add_executable(bridge-topic-test
@ -44,6 +45,7 @@ add_executable(persist-read-test
../../../lib/util_mosq.c
)
target_compile_definitions(persist-read-test PRIVATE TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}" WITH_PERSISTENCE WITH_BROKER)
target_include_directories(persist-read-test PRIVATE ${mosquitto_SOURCE_DIR}/libcommon)
target_link_libraries(persist-read-test PRIVATE persistence-read-obj libmosquitto_common)
add_test(NAME unit-persist-read-test COMMAND persist-read-test)
@ -62,6 +64,7 @@ add_library(persistence-write-obj
../../../src/topic_tok.c
)
target_compile_definitions(persistence-write-obj PRIVATE WITH_PERSISTENCE WITH_BROKER)
target_include_directories(persistence-write-obj PRIVATE ${mosquitto_SOURCE_DIR}/libcommon)
target_link_libraries(persistence-write-obj PUBLIC common-unit-test-header)
add_executable(persist-write-test
@ -73,6 +76,7 @@ add_executable(persist-write-test
../../../lib/packet_mosq.c
)
target_compile_definitions(persist-write-test PRIVATE TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}" WITH_PERSISTENCE WITH_BROKER WITH_SYS_TREE)
target_include_directories(persist-write-test PRIVATE ${mosquitto_SOURCE_DIR}/libcommon)
target_link_libraries(persist-write-test PRIVATE persistence-write-obj OpenSSL::SSL libmosquitto_common)
add_test(NAME unit-persist-write-test COMMAND persist-write-test)
@ -86,6 +90,7 @@ add_library(subs-obj
../../../src/topic_tok.c
)
target_compile_definitions(subs-obj PRIVATE WITH_BROKER)
target_include_directories(subs-obj PRIVATE ${mosquitto_SOURCE_DIR}/libcommon)
target_link_libraries(subs-obj PUBLIC common-unit-test-header)
add_executable(subs-test

View file

@ -5,7 +5,7 @@ include ${R}/make/broker.mk
.PHONY: all check test test-compile clean coverage
LOCAL_CFLAGS+=-coverage
LOCAL_CPPFLAGS+=-DWITH_BROKER -I${R}/src -I${R}/test -I${R}/lib -DTEST_SOURCE_DIR='"$(realpath .)"' -I${R}/lib
LOCAL_CPPFLAGS+=-DWITH_BROKER -I${R}/src -I${R}/test -I${R}/lib -DTEST_SOURCE_DIR='"$(realpath .)"' -I${R}/lib -I${R}/libcommon
LOCAL_LDFLAGS+=-coverage
LOCAL_LDADD+=-lcunit ${LIBMOSQ_COMMON}

View file

@ -10,6 +10,7 @@
#include "mosquitto_broker_internal.h"
#include "persist.h"
#include "property_mosq.h"
#include "property_common.h"
char *last_sub = NULL;
int last_qos;

View file

@ -2,11 +2,9 @@ add_executable(lib-test
datatype_read.c
datatype_write.c
misc_trim_test.c
property_add.c
property_read.c
property_user_read.c
property_write.c
property_value.c
stubs.c
# main test files
test.c
@ -17,5 +15,6 @@ add_executable(lib-test
../../../lib/util_mosq.c
)
target_include_directories(lib-test PRIVATE ${mosquitto_SOURCE_DIR}/libcommon)
target_link_libraries(lib-test PRIVATE common-unit-test-header OpenSSL::SSL libmosquitto_common)
add_test(NAME unit-lib-test COMMAND lib-test)

View file

@ -4,7 +4,7 @@ include ${R}/config.mk
.PHONY: all check test test-compile clean coverage
LOCAL_CFLAGS+=-coverage
LOCAL_CPPFLAGS+=-I${R}/src -I${R}/test -I${R}/lib -DTEST_SOURCE_DIR='"$(realpath .)"'
LOCAL_CPPFLAGS+=-I${R}/src -I${R}/test -I${R}/lib -I${R}/libcommon -DTEST_SOURCE_DIR='"$(realpath .)"'
LOCAL_LDFLAGS+=-coverage
LOCAL_LDADD+=-lcunit ${LIBMOSQ_COMMON}
@ -17,11 +17,9 @@ TEST_OBJS = \
datatype_read.o \
datatype_write.o \
misc_trim_test.o \
property_add.o \
property_read.o \
property_user_read.o \
property_write.o \
property_value.o \
stubs.o \
LIB_OBJS = \

View file

@ -2,6 +2,7 @@
#include <CUnit/Basic.h>
#include "mosquitto/mqtt_protocol.h"
#include "property_common.h"
#include "property_mosq.h"
#include "packet_mosq.h"
@ -28,7 +29,7 @@ static void byte_prop_read_helper(
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(properties->value.i8, value_expected);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 2);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 2);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -87,7 +88,7 @@ static void int32_prop_read_helper(
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(properties->value.i32, value_expected);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 5);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 5);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -137,7 +138,7 @@ static void int16_prop_read_helper(
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(properties->value.i16, value_expected);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 3);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 3);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -183,7 +184,7 @@ static void string_prop_read_helper(
CU_ASSERT_EQUAL(properties->value.s.len, strlen(value_expected));
CU_ASSERT_STRING_EQUAL(properties->value.s.v, value_expected);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+strlen(value_expected));
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 1+2+strlen(value_expected));
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -248,7 +249,7 @@ static void binary_prop_read_helper(
CU_ASSERT_EQUAL(properties->value.bin.len, len_expected);
CU_ASSERT_EQUAL(memcmp(properties->value.bin.v, value_expected, (size_t)len_expected), 0);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+(unsigned int)len_expected);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 1+2+(unsigned int)len_expected);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -302,7 +303,7 @@ static void string_pair_prop_read_helper(
CU_ASSERT_PTR_NOT_NULL(properties->next);
}else{
CU_ASSERT_PTR_NULL(properties->next);
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+strlen(name_expected)+2+strlen(value_expected));
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 1+2+strlen(name_expected)+2+strlen(value_expected));
}
mosquitto_property_free_all(&properties);
}
@ -333,7 +334,7 @@ static void varint_prop_read_helper(
CU_ASSERT_EQUAL(properties->identifier, identifier);
CU_ASSERT_EQUAL(properties->value.varint, value_expected);
CU_ASSERT_PTR_NULL(properties->next);
CU_ASSERT_EQUAL(property__get_length_all(properties), packet__varint_bytes(value_expected)+1);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), mosquitto_varint_bytes(value_expected)+1);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_NULL(properties);

View file

@ -2,6 +2,7 @@
#include <CUnit/Basic.h>
#include "mosquitto/mqtt_protocol.h"
#include "property_common.h"
#include "property_mosq.h"
#include "packet_mosq.h"
@ -24,7 +25,7 @@ static void byte_prop_write_helper(
property.value.i8 = value_expected;
property.property_type = MQTT_PROP_TYPE_BYTE;
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
rc = packet__alloc(&packet, 0, mosquitto_property_get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
if(rc != MOSQ_ERR_SUCCESS) return;
@ -48,7 +49,7 @@ static void byte_prop_write_helper(
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_BYTE);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 2);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 2);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -75,7 +76,7 @@ static void int32_prop_write_helper(
property.value.i32 = value_expected;
property.property_type = MQTT_PROP_TYPE_INT32;
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
rc = packet__alloc(&packet, 0, mosquitto_property_get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
if(rc != MOSQ_ERR_SUCCESS) return;
@ -99,7 +100,7 @@ static void int32_prop_write_helper(
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_INT32);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 5);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 5);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -126,7 +127,7 @@ static void int16_prop_write_helper(
property.value.i16 = value_expected;
property.property_type = MQTT_PROP_TYPE_INT16;
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
rc = packet__alloc(&packet, 0, mosquitto_property_get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
if(rc != MOSQ_ERR_SUCCESS) return;
@ -150,7 +151,7 @@ static void int16_prop_write_helper(
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_INT16);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 3);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 3);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -180,7 +181,7 @@ static void string_prop_write_helper(
property.value.s.len = (uint16_t)strlen(value_expected);
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
rc = packet__alloc(&packet, 0, mosquitto_property_get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
if(rc != MOSQ_ERR_SUCCESS) return;
@ -205,7 +206,7 @@ static void string_prop_write_helper(
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_STRING);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+strlen(value_expected));
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 1+2+strlen(value_expected));
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -239,7 +240,7 @@ static void binary_prop_write_helper(
memcpy(property.value.bin.v, value_expected, len_expected);
property.value.bin.len = len_expected;
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
rc = packet__alloc(&packet, 0, mosquitto_property_get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
if(rc != MOSQ_ERR_SUCCESS) return;
@ -264,7 +265,7 @@ static void binary_prop_write_helper(
CU_ASSERT_EQUAL(mosquitto_property_type(properties), MQTT_PROP_TYPE_BINARY);
CU_ASSERT_PTR_EQUAL(properties->next, NULL);
CU_ASSERT_PTR_EQUAL(mosquitto_property_next(properties), NULL);
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+len_expected);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 1+2+len_expected);
mosquitto_property_free_all(&properties);
}
CU_ASSERT_PTR_EQUAL(properties, NULL);
@ -301,7 +302,7 @@ static void string_pair_prop_write_helper(
property.name.len = (uint16_t)strlen(name_expected);
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
rc = packet__alloc(&packet, 0, mosquitto_property_get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
if(rc != MOSQ_ERR_SUCCESS) return;
@ -332,7 +333,7 @@ static void string_pair_prop_write_helper(
}else{
CU_ASSERT_PTR_NULL(properties->next);
CU_ASSERT_PTR_NULL(mosquitto_property_next(properties));
CU_ASSERT_EQUAL(property__get_length_all(properties), 1+2+strlen(name_expected)+2+strlen(value_expected));
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 1+2+strlen(name_expected)+2+strlen(value_expected));
}
mosquitto_property_free_all(&properties);
}
@ -360,9 +361,9 @@ static void varint_prop_write_helper(
property.property_type = MQTT_PROP_TYPE_VARINT;
property.value.varint = value_expected;
CU_ASSERT_EQUAL(remaining_length, property__get_length_all(&property)+1);
CU_ASSERT_EQUAL(remaining_length, mosquitto_property_get_length_all(&property)+1);
rc = packet__alloc(&packet, 0, property__get_length_all(&property)+11);
rc = packet__alloc(&packet, 0, mosquitto_property_get_length_all(&property)+11);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
if(rc != MOSQ_ERR_SUCCESS) return;
@ -386,13 +387,13 @@ static void varint_prop_write_helper(
CU_ASSERT_PTR_NULL(properties->next);
CU_ASSERT_PTR_NULL(mosquitto_property_next(properties));
if(value_expected < 128){
CU_ASSERT_EQUAL(property__get_length_all(properties), 2);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 2);
}else if(value_expected < 16384){
CU_ASSERT_EQUAL(property__get_length_all(properties), 3);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 3);
}else if(value_expected < 2097152){
CU_ASSERT_EQUAL(property__get_length_all(properties), 4);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 4);
}else if(value_expected < 268435456){
CU_ASSERT_EQUAL(property__get_length_all(properties), 5);
CU_ASSERT_EQUAL(mosquitto_property_get_length_all(properties), 5);
}else{
CU_FAIL("Incorrect varint value.");
}

View file

@ -6,11 +6,9 @@
int init_datatype_read_tests(void);
int init_datatype_write_tests(void);
int init_property_add_tests(void);
int init_property_read_tests(void);
int init_property_user_read_tests(void);
int init_property_write_tests(void);
int init_property_value_tests(void);
int init_misc_trim_tests(void);
int main(int argc, char *argv[])
@ -28,11 +26,9 @@ int main(int argc, char *argv[])
if(0
|| init_datatype_read_tests()
|| init_datatype_write_tests()
|| init_property_add_tests()
|| init_property_read_tests()
|| init_property_user_read_tests()
|| init_property_write_tests()
|| init_property_value_tests()
|| init_misc_trim_tests()
){

View file

@ -1,10 +1,16 @@
add_executable(libcommon-test
property_add.c
property_value.c
strings_test.c
test.c
topic_test.c
utf8.c
)
target_include_directories(libcommon-test
PRIVATE
${mosquitto_SOURCE_DIR}/libcommon
)
target_link_libraries(libcommon-test
PRIVATE
common-unit-test-header

View file

@ -4,7 +4,7 @@ include ${R}/config.mk
.PHONY: all check test test-compile clean coverage
LOCAL_CFLAGS+=-coverage
LOCAL_CPPFLAGS+=-I${R}/src -I${R}/test -I${R}/lib -DTEST_SOURCE_DIR='"$(realpath .)"'
LOCAL_CPPFLAGS+=-I${R}/libcommon -DTEST_SOURCE_DIR='"$(realpath .)"'
LOCAL_LDFLAGS+=-coverage
LOCAL_LDADD+=-lcunit ${LIBMOSQ_COMMON}
@ -13,6 +13,8 @@ ifeq ($(WITH_TLS),yes)
endif
TEST_OBJS = \
property_add.o \
property_value.o \
strings_test.o \
test.o \
topic_test.o \

View file

@ -2,8 +2,7 @@
#include <CUnit/Basic.h>
#include "mosquitto/mqtt_protocol.h"
#include "property_mosq.h"
#include "packet_mosq.h"
#include "property_common.h"
static void check_count(mosquitto_property *proplist, int expected)
{
@ -585,7 +584,7 @@ static void TEST_check_length(void)
unsigned int varbytes;
unsigned int i;
len = property__get_remaining_length(proplist);
len = mosquitto_property_get_remaining_length(proplist);
CU_ASSERT_EQUAL(len, 1);
for(i=1; i<10000; i++){
@ -593,7 +592,7 @@ static void TEST_check_length(void)
CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS);
CU_ASSERT_PTR_NOT_NULL(proplist);
if(proplist){
len = property__get_remaining_length(proplist);
len = mosquitto_property_get_remaining_length(proplist);
if(i < 64){
varbytes = 1;
}else if(i < 8192){
@ -615,7 +614,7 @@ static void TEST_remove_single(void)
int rc;
unsigned int len;
len = property__get_remaining_length(proplist);
len = mosquitto_property_get_remaining_length(proplist);
CU_ASSERT_EQUAL(len, 1);
for(int i=1; i<10; i++){
@ -688,7 +687,7 @@ static void TEST_remove_non_existent(void)
int rc;
unsigned int len;
len = property__get_remaining_length(proplist);
len = mosquitto_property_get_remaining_length(proplist);
CU_ASSERT_EQUAL(len, 1);
rc = mosquitto_property_add_byte(&proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);
@ -716,7 +715,7 @@ static void TEST_remove_invalid(void)
rc = mosquitto_property_remove(NULL, NULL);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_INVAL);
len = property__get_remaining_length(proplist);
len = mosquitto_property_get_remaining_length(proplist);
CU_ASSERT_EQUAL(len, 1);
rc = mosquitto_property_add_byte(&proplist, MQTT_PROP_SHARED_SUB_AVAILABLE, 0);

View file

@ -2,8 +2,7 @@
#include <CUnit/Basic.h>
#include "mosquitto/mqtt_protocol.h"
#include "property_mosq.h"
#include "packet_mosq.h"
#include "property_common.h"
static void TEST_value_byte_success(void)
{

View file

@ -4,6 +4,8 @@
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
int init_property_add_tests(void);
int init_property_value_tests(void);
int init_strings_tests(void);
int init_topic_tests(void);
int init_utf8_tests(void);
@ -21,6 +23,8 @@ int main(int argc, char *argv[])
}
if(0
|| init_property_add_tests()
|| init_property_value_tests()
|| init_strings_tests()
|| init_topic_tests()
|| init_utf8_tests()

View file

@ -1,7 +1,7 @@
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include <util_mosq.h>
#include <mosquitto.h>
struct topic_test{
const char *topic_filter;