From 254194dfc6b52463ff61ecf8f76fbfd1e702bc32 Mon Sep 17 00:00:00 2001 From: Johannes Schriewer Date: Mon, 30 Jul 2018 22:13:37 +0200 Subject: [PATCH] Add client id sanity check Re #10 --- src/mqtt.c | 6 ++++++ tests/connect_publish.c | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/mqtt.c b/src/mqtt.c index b3ec0ce..4b8dc4b 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -183,6 +183,12 @@ static void _mqtt_connect(MQTTHandle *handle, MQTTEventHandler callback, void *c } MQTTHandle *mqtt_connect(MQTTConfig *config, MQTTEventHandler callback, void *context, MQTTErrorHandler error_callback) { + // sanity check + if ((config->client_id != NULL) && (strlen(config->client_id) > 23)) { + DEBUG_LOG("Client ID has to be shorter than 24 characters"); + return NULL; + } + MQTTHandle *handle = calloc(sizeof(struct _MQTTHandle), 1); initialize_platform(handle); diff --git a/tests/connect_publish.c b/tests/connect_publish.c index 23c18ec..5b95ec8 100644 --- a/tests/connect_publish.c +++ b/tests/connect_publish.c @@ -40,14 +40,22 @@ void mqtt_connected(MQTTHandle *handle, void *context) { int main(int argc, char **argv) { MQTTConfig config = { 0 }; - config.client_id = "libmqtt_testsuite"; + config.client_id = "libmqtt_testsuite_this_is_too_long"; config.hostname = "localhost"; config.last_will_topic = "testsuite/last_will"; config.last_will_message = "RIP"; - LOG("Trying to connect to %s", config.hostname); + LOG("Testing too long client id..."); MQTTHandle *mqtt = mqtt_connect(&config, mqtt_connected, NULL, err_handler); + if (mqtt != NULL) { + LOG("Handle should be NULL, but it wasn't"); + return 1; + } + + config.client_id = "libmqtt_testsuite"; + LOG("Trying to connect to %s", config.hostname); + mqtt = mqtt_connect(&config, mqtt_connected, NULL, err_handler); if (mqtt == NULL) { LOG("Connection failed!");