Add client id sanity check

Re #10
This commit is contained in:
Johannes Schriewer 2018-07-30 22:13:37 +02:00
parent 9b89eba536
commit 254194dfc6
2 changed files with 16 additions and 2 deletions

View file

@ -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);

View file

@ -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!");