Bridge remapping refactoring and tests.
parent
8463c33720
commit
fad184c9c2
@ -0,0 +1,214 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2009-2019 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 v1.0
|
||||||
|
and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||||
|
|
||||||
|
The Eclipse Public License is available at
|
||||||
|
http://www.eclipse.org/legal/epl-v10.html
|
||||||
|
and the Eclipse Distribution License is available at
|
||||||
|
http://www.eclipse.org/org/documents/edl-v10.php.
|
||||||
|
|
||||||
|
Contributors:
|
||||||
|
Roger Light - initial implementation and documentation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "mosquitto.h"
|
||||||
|
#include "mosquitto_broker_internal.h"
|
||||||
|
#include "memory_mosq.h"
|
||||||
|
|
||||||
|
#ifdef WITH_BRIDGE
|
||||||
|
|
||||||
|
|
||||||
|
/* topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix] */
|
||||||
|
int bridge__add_topic(struct mosquitto__bridge *bridge, const char *topic, enum mosquitto__bridge_direction direction, int qos, const char *local_prefix, const char *remote_prefix)
|
||||||
|
{
|
||||||
|
struct mosquitto__bridge_topic *topics;
|
||||||
|
struct mosquitto__bridge_topic *cur_topic;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
|
||||||
|
if(bridge == NULL) return MOSQ_ERR_INVAL;
|
||||||
|
if(direction != bd_out && direction != bd_in && direction != bd_both){
|
||||||
|
return MOSQ_ERR_INVAL;
|
||||||
|
}
|
||||||
|
if(qos < 0 || qos > 2){
|
||||||
|
return MOSQ_ERR_INVAL;
|
||||||
|
}
|
||||||
|
if(local_prefix && mosquitto_pub_topic_check(local_prefix)){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge topic local prefix '%s'.", local_prefix);
|
||||||
|
return MOSQ_ERR_INVAL;
|
||||||
|
}
|
||||||
|
if(remote_prefix && mosquitto_pub_topic_check(remote_prefix)){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge topic remote prefix '%s'.", remote_prefix);
|
||||||
|
return MOSQ_ERR_INVAL;
|
||||||
|
}
|
||||||
|
if((topic == NULL || !strcmp(topic, "\"\"")) &&
|
||||||
|
(local_prefix == NULL || remote_prefix == NULL)){
|
||||||
|
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Invalid bridge remapping.");
|
||||||
|
return MOSQ_ERR_INVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bridge->topic_count++;
|
||||||
|
topics = mosquitto__realloc(bridge->topics,
|
||||||
|
sizeof(struct mosquitto__bridge_topic)*bridge->topic_count);
|
||||||
|
|
||||||
|
if(topics == NULL){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
bridge->topics = topics;
|
||||||
|
|
||||||
|
cur_topic = &bridge->topics[bridge->topic_count-1];
|
||||||
|
cur_topic->direction = direction;
|
||||||
|
cur_topic->qos = qos;
|
||||||
|
cur_topic->local_prefix = NULL;
|
||||||
|
cur_topic->remote_prefix = NULL;
|
||||||
|
|
||||||
|
if(topic == NULL || !strcmp(topic, "\"\"")){
|
||||||
|
cur_topic->topic = NULL;
|
||||||
|
}else{
|
||||||
|
cur_topic->topic = mosquitto__strdup(topic);
|
||||||
|
if(cur_topic->topic == NULL){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(local_prefix || remote_prefix){
|
||||||
|
bridge->topic_remapping = true;
|
||||||
|
if(local_prefix){
|
||||||
|
cur_topic->local_prefix = mosquitto__strdup(local_prefix);
|
||||||
|
if(cur_topic->local_prefix == NULL){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(remote_prefix){
|
||||||
|
cur_topic->remote_prefix = mosquitto__strdup(remote_prefix);
|
||||||
|
if(cur_topic->remote_prefix == NULL){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cur_topic->local_prefix){
|
||||||
|
if(cur_topic->topic){
|
||||||
|
len = strlen(cur_topic->topic) + strlen(cur_topic->local_prefix)+1;
|
||||||
|
cur_topic->local_topic = mosquitto__malloc(len+1);
|
||||||
|
if(!cur_topic->local_topic){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
snprintf(cur_topic->local_topic, len+1, "%s%s", cur_topic->local_prefix, cur_topic->topic);
|
||||||
|
cur_topic->local_topic[len] = '\0';
|
||||||
|
}else{
|
||||||
|
cur_topic->local_topic = mosquitto__strdup(cur_topic->local_prefix);
|
||||||
|
if(!cur_topic->local_topic){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
cur_topic->local_topic = mosquitto__strdup(cur_topic->topic);
|
||||||
|
if(!cur_topic->local_topic){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cur_topic->remote_prefix){
|
||||||
|
if(cur_topic->topic){
|
||||||
|
len = strlen(cur_topic->topic) + strlen(cur_topic->remote_prefix)+1;
|
||||||
|
cur_topic->remote_topic = mosquitto__malloc(len+1);
|
||||||
|
if(!cur_topic->remote_topic){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
snprintf(cur_topic->remote_topic, len, "%s%s", cur_topic->remote_prefix, cur_topic->topic);
|
||||||
|
cur_topic->remote_topic[len] = '\0';
|
||||||
|
}else{
|
||||||
|
cur_topic->remote_topic = mosquitto__strdup(cur_topic->remote_prefix);
|
||||||
|
if(!cur_topic->remote_topic){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
cur_topic->remote_topic = mosquitto__strdup(cur_topic->topic);
|
||||||
|
if(!cur_topic->remote_topic){
|
||||||
|
log__printf(NULL, MOSQ_LOG_ERR, "Error: Out of memory.");
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MOSQ_ERR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int bridge__remap_topic_in(struct mosquitto *context, char **topic)
|
||||||
|
{
|
||||||
|
struct mosquitto__bridge_topic *cur_topic;
|
||||||
|
char *topic_temp;
|
||||||
|
int i;
|
||||||
|
int len;
|
||||||
|
int rc;
|
||||||
|
bool match;
|
||||||
|
|
||||||
|
if(context->bridge && context->bridge->topics && context->bridge->topic_remapping){
|
||||||
|
for(i=0; i<context->bridge->topic_count; i++){
|
||||||
|
cur_topic = &context->bridge->topics[i];
|
||||||
|
if((cur_topic->direction == bd_both || cur_topic->direction == bd_in)
|
||||||
|
&& (cur_topic->remote_prefix || cur_topic->local_prefix)){
|
||||||
|
|
||||||
|
/* Topic mapping required on this topic if the message matches */
|
||||||
|
|
||||||
|
rc = mosquitto_topic_matches_sub(cur_topic->remote_topic, *topic, &match);
|
||||||
|
if(rc){
|
||||||
|
mosquitto__free(*topic);
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
if(match){
|
||||||
|
if(cur_topic->remote_prefix){
|
||||||
|
/* This prefix needs removing. */
|
||||||
|
if(!strncmp(cur_topic->remote_prefix, *topic, strlen(cur_topic->remote_prefix))){
|
||||||
|
topic_temp = mosquitto__strdup((*topic)+strlen(cur_topic->remote_prefix));
|
||||||
|
if(!topic_temp){
|
||||||
|
mosquitto__free(*topic);
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
mosquitto__free(*topic);
|
||||||
|
*topic = topic_temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cur_topic->local_prefix){
|
||||||
|
/* This prefix needs adding. */
|
||||||
|
len = strlen(*topic) + strlen(cur_topic->local_prefix)+1;
|
||||||
|
topic_temp = mosquitto__malloc(len+1);
|
||||||
|
if(!topic_temp){
|
||||||
|
mosquitto__free(*topic);
|
||||||
|
return MOSQ_ERR_NOMEM;
|
||||||
|
}
|
||||||
|
snprintf(topic_temp, len, "%s%s", cur_topic->local_prefix, *topic);
|
||||||
|
topic_temp[len] = '\0';
|
||||||
|
|
||||||
|
mosquitto__free(*topic);
|
||||||
|
*topic = topic_temp;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return MOSQ_ERR_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,124 @@
|
|||||||
|
#include "config.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <CUnit/CUnit.h>
|
||||||
|
#include <CUnit/Basic.h>
|
||||||
|
|
||||||
|
#define WITH_BRIDGE
|
||||||
|
#define WITH_BROKER
|
||||||
|
|
||||||
|
#include "mosquitto_broker_internal.h"
|
||||||
|
#include "property_mosq.h"
|
||||||
|
#include "packet_mosq.h"
|
||||||
|
|
||||||
|
static void map_valid_helper(const char *topic, const char *local_prefix, const char *remote_prefix, const char *incoming, const char *expected)
|
||||||
|
{
|
||||||
|
struct mosquitto mosq;
|
||||||
|
struct mosquitto__bridge bridge;
|
||||||
|
char *map_topic;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
memset(&mosq, 0, sizeof(struct mosquitto));
|
||||||
|
memset(&bridge, 0, sizeof(struct mosquitto__bridge));
|
||||||
|
|
||||||
|
mosq.bridge = &bridge;
|
||||||
|
|
||||||
|
rc = bridge__add_topic(&bridge, topic, bd_in, 0, local_prefix, remote_prefix);
|
||||||
|
CU_ASSERT_EQUAL(rc, 0);
|
||||||
|
|
||||||
|
map_topic = strdup(incoming);
|
||||||
|
rc = bridge__remap_topic_in(&mosq, &map_topic);
|
||||||
|
CU_ASSERT_EQUAL(rc, 0);
|
||||||
|
CU_ASSERT_PTR_NOT_NULL(map_topic);
|
||||||
|
if(topic){
|
||||||
|
CU_ASSERT_STRING_EQUAL(map_topic, expected);
|
||||||
|
free(map_topic);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void map_invalid_helper(const char *topic, const char *local_prefix, const char *remote_prefix)
|
||||||
|
{
|
||||||
|
struct mosquitto mosq;
|
||||||
|
struct mosquitto__bridge bridge;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
memset(&mosq, 0, sizeof(struct mosquitto));
|
||||||
|
memset(&bridge, 0, sizeof(struct mosquitto__bridge));
|
||||||
|
|
||||||
|
mosq.bridge = &bridge;
|
||||||
|
|
||||||
|
rc = bridge__add_topic(&bridge, topic, bd_in, 0, local_prefix, remote_prefix);
|
||||||
|
CU_ASSERT_NOT_EQUAL(rc, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void TEST_remap_valid(void)
|
||||||
|
{
|
||||||
|
/* Examples from man page */
|
||||||
|
map_valid_helper("pattern", "L/", "R/", "R/pattern", "L/pattern");
|
||||||
|
map_valid_helper("pattern", "L/", NULL, "pattern", "L/pattern");
|
||||||
|
map_valid_helper("pattern", NULL, "R/", "R/pattern", "pattern");
|
||||||
|
map_valid_helper("pattern", NULL, NULL, "pattern", "pattern");
|
||||||
|
map_valid_helper(NULL, "local", "remote", "local", "remote");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void TEST_remap_invalid(void)
|
||||||
|
{
|
||||||
|
/* Examples from man page */
|
||||||
|
map_invalid_helper(NULL, "L/", NULL);
|
||||||
|
map_invalid_helper(NULL, NULL, "R/");
|
||||||
|
map_invalid_helper(NULL, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ========================================================================
|
||||||
|
* TEST SUITE SETUP
|
||||||
|
* ======================================================================== */
|
||||||
|
|
||||||
|
int init_bridge_tests(void)
|
||||||
|
{
|
||||||
|
CU_pSuite test_suite = NULL;
|
||||||
|
|
||||||
|
test_suite = CU_add_suite("Bridge remap", NULL, NULL);
|
||||||
|
if(!test_suite){
|
||||||
|
printf("Error adding CUnit Bridge remap test suite.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(0
|
||||||
|
|| !CU_add_test(test_suite, "Remap valid", TEST_remap_valid)
|
||||||
|
|| !CU_add_test(test_suite, "Remap invalid", TEST_remap_invalid)
|
||||||
|
){
|
||||||
|
|
||||||
|
printf("Error adding Bridge remap CUnit tests.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
unsigned int fails;
|
||||||
|
|
||||||
|
if(CU_initialize_registry() != CUE_SUCCESS){
|
||||||
|
printf("Error initializing CUnit registry.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(0
|
||||||
|
|| init_bridge_tests()
|
||||||
|
){
|
||||||
|
|
||||||
|
CU_cleanup_registry();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
CU_basic_set_mode(CU_BRM_VERBOSE);
|
||||||
|
CU_basic_run_tests();
|
||||||
|
fails = CU_get_number_of_failures();
|
||||||
|
CU_cleanup_registry();
|
||||||
|
|
||||||
|
return (int)fails;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue