From 9d3b411627280f42887909669c3904056887ff15 Mon Sep 17 00:00:00 2001 From: gc87 Date: Fri, 15 Jul 2022 13:48:57 +0800 Subject: [PATCH] fix(mqtt): setting free * Add unit test for setting * Set mqtt.json format field to optinal --- plugins/mqtt/command/heartbeat.c | 8 +- plugins/mqtt/mqtt.json | 2 +- plugins/mqtt/mqtt_plugin.c | 4 +- plugins/mqtt/mqtt_util.c | 24 +++-- tests/mqtt_test.cc | 157 +++++++++++++++++++++++++++++-- 5 files changed, 169 insertions(+), 26 deletions(-) diff --git a/plugins/mqtt/command/heartbeat.c b/plugins/mqtt/command/heartbeat.c index f668b990..83c63281 100644 --- a/plugins/mqtt/command/heartbeat.c +++ b/plugins/mqtt/command/heartbeat.c @@ -33,13 +33,11 @@ char *command_heartbeat_generate(neu_plugin_t *plugin, UT_array *states) neu_json_states_t json = { 0 }; char * json_str = NULL; json.n_state = utarray_len(states); - if (0 < json.n_state) { - json.states = calloc(json.n_state, sizeof(neu_json_node_state_t)); - } + json.states = calloc(json.n_state, sizeof(neu_json_node_state_t)); - int index = 0; utarray_foreach(states, struct node_state **, state_p) { + int index = utarray_eltidx(states, state_p); struct node_state *ns = (*state_p); json.states[index].node = ns->node; json.states[index].link = ns->link; @@ -49,7 +47,7 @@ char *command_heartbeat_generate(neu_plugin_t *plugin, UT_array *states) neu_json_encode_with_mqtt(&json, neu_json_encode_states_resp, &header, neu_json_encode_state_header_resp, &json_str); - if (0 < json.n_state) { + if (NULL != json.states) { free(json.states); } diff --git a/plugins/mqtt/mqtt.json b/plugins/mqtt/mqtt.json index a454ede0..bb5997de 100644 --- a/plugins/mqtt/mqtt.json +++ b/plugins/mqtt/mqtt.json @@ -32,7 +32,7 @@ "format": { "name": "upload format", "description": "Upload format on mqtt", - "attribute": "required", + "attribute": "optional", "type": "map", "default": 0, "valid": { diff --git a/plugins/mqtt/mqtt_plugin.c b/plugins/mqtt/mqtt_plugin.c index 578768d6..9a8b5b27 100644 --- a/plugins/mqtt/mqtt_plugin.c +++ b/plugins/mqtt/mqtt_plugin.c @@ -465,7 +465,7 @@ static int mqtt_plugin_config(neu_plugin_t *plugin, const char *config) plugin_stop_running(plugin); int rc = plguin_start_running(plugin); if (0 != rc) { - return NEU_ERR_MQTT_FAILURE; + return NEU_ERR_NODE_SETTING_INVALID; } plog_info(plugin, "config plugin: %s", neu_plugin_module.module_name); @@ -478,7 +478,7 @@ static int mqtt_plugin_start(neu_plugin_t *plugin) int rc = plguin_start_running(plugin); if (0 != rc) { - return NEU_ERR_MQTT_FAILURE; + return NEU_ERR_NODE_SETTING_INVALID; } return NEU_ERR_SUCCESS; diff --git a/plugins/mqtt/mqtt_util.c b/plugins/mqtt/mqtt_util.c index 25fa93ae..673afb6a 100644 --- a/plugins/mqtt/mqtt_util.c +++ b/plugins/mqtt/mqtt_util.c @@ -96,6 +96,7 @@ void mqtt_option_uninit(neu_plugin_t *plugin, neu_mqtt_option_t *option) int mqtt_option_init(neu_plugin_t *plugin, char *config, neu_mqtt_option_t *option) { + (void) plugin; if (NULL == config || NULL == option) { return -1; } @@ -120,7 +121,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // client-id, required ret = neu_parse_param(config, &error, 1, &id); if (0 != ret) { - plog_error(plugin, "can't decode client-id from json"); + free(error); free(id.v.val_str); return -1; } else { @@ -130,6 +131,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // upload-topic, optional ret = neu_parse_param(config, &error, 1, &upload); if (0 != ret) { + free(error); free(upload.v.val_str); } else { option->upload_topic = upload.v.val_str; @@ -138,14 +140,16 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // heartbeat-topic, optional ret = neu_parse_param(config, &error, 1, &heartbeat); if (0 != ret) { + free(error); free(heartbeat.v.val_str); } else { option->heartbeat_topic = heartbeat.v.val_str; } - // format, required + // format, optional ret = neu_parse_param(config, &error, 1, &format); if (0 != ret) { + free(error); option->format = 0; } else { option->format = format.v.val_int; @@ -154,7 +158,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // ssl, required ret = neu_parse_param(config, &error, 1, &ssl); if (0 != ret) { - plog_error(plugin, "can't decode ssl from json"); + free(error); return -2; } else { if (false == ssl.v.val_bool) { @@ -165,7 +169,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // ca, required if ssl enabled ret = neu_parse_param(config, &error, 1, &ca); if (0 != ret) { - plog_error(plugin, "can't decode ca from json"); + free(error); free(ca.v.val_str); return -3; } else { @@ -174,7 +178,8 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // cert, optional ret = neu_parse_param(config, &error, 1, &cert); - if (0 == ret) { + if (0 != ret) { + free(error); free(cert.v.val_str); } else { option->cert = cert.v.val_str; @@ -182,7 +187,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // key, required if cert enable ret = neu_parse_param(config, &error, 1, &key); if (0 != ret) { - plog_error(plugin, "can't decode key from json"); + free(error); free(key.v.val_str); return -4; } else { @@ -192,6 +197,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // keypass, optional ret = neu_parse_param(config, &error, 1, &keypass); if (0 != ret) { + free(error); free(keypass.v.val_str); } else { option->keypass = keypass.v.val_str; @@ -203,7 +209,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // host, required ret = neu_parse_param(config, &error, 1, &host); if (0 != ret) { - plog_error(plugin, "can't decode host from json"); + free(error); free(host.v.val_str); return -5; } else { @@ -213,7 +219,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // port, required ret = neu_parse_param(config, &error, 1, &port); if (0 != ret) { - plog_error(plugin, "can't decode port from json"); + free(error); return -6; } else { int32_t p = (int) port.v.val_int; @@ -225,6 +231,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // username, optional ret = neu_parse_param(config, &error, 1, &username); if (0 != ret) { + free(error); free(username.v.val_str); } else { option->username = username.v.val_str; @@ -233,6 +240,7 @@ int mqtt_option_init(neu_plugin_t *plugin, char *config, // password, optional ret = neu_parse_param(config, &error, 1, &password); if (0 != ret) { + free(error); free(password.v.val_str); } else { option->password = password.v.val_str; diff --git a/tests/mqtt_test.cc b/tests/mqtt_test.cc index 7c7efef7..0f3d8079 100644 --- a/tests/mqtt_test.cc +++ b/tests/mqtt_test.cc @@ -13,19 +13,156 @@ TEST(MQTTTest, mqtt_option_init_by_config) neu_plugin_common_init(&plugin->common); neu_mqtt_option_t option; - memset(&option, 0, sizeof(neu_mqtt_option_t)); - char *config = - (char *) "{\"node_id\":5,\"params\":{\"client-id\":" - "\"upload123\", \"upload-topic\":\"\", " - "\"heartbeat-topic\":\"\", \"format\": " - "0,\"ssl\":false,\"host\":\"192.168.10." - "116\",\"port\":1883,\"username\":\"\",\"password\":" - "\"\",\"ca\":\"\", \"cert\":\"\", \"key\":\"\", " - "\"keypass\":\"\"}}"; + // memset(&option, 0, sizeof(neu_mqtt_option_t)); + // char *config = + // (char *) "{\"node_id\":5,\"params\":{\"client-id\":" + // "\"upload123\", \"upload-topic\":\"\", " + // "\"heartbeat-topic\":\"\", \"format\": " + // "0,\"ssl\":false,\"host\":\"192.168.10." + // "116\",\"port\":1883,\"username\":\"\",\"password\":" + // "\"\",\"ca\":\"\", \"cert\":\"\", \"key\":\"\", " + // "\"keypass\":\"\"}}"; - int rc = mqtt_option_init(plugin, config, &option); + // int rc = mqtt_option_init(plugin, config, &option); + // EXPECT_EQ(0, rc); + // mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config0 = (char *) "{\"node_id\":5,\"params\":{}}"; + int rc = mqtt_option_init(plugin, config0, &option); + EXPECT_EQ(-1, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config1 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\"}}"; + rc = mqtt_option_init(plugin, config1, &option); + EXPECT_EQ(-2, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config2 = (char *) "{\"node_id\":5,\"params\":{\"client-id\":" + "\"upload123\", \"upload-topic\":\"\"}}"; + rc = mqtt_option_init(plugin, config2, &option); + EXPECT_EQ(-2, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config3 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\"}}"; + rc = mqtt_option_init(plugin, config3, &option); + EXPECT_EQ(-2, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config4 = (char *) "{\"node_id\":5,\"params\":{\"client-id\":" + "\"upload123\", \"upload-topic\":\"\", " + "\"heartbeat-topic\":\"\", \"format\": 0}}"; + rc = mqtt_option_init(plugin, config4, &option); + EXPECT_EQ(-2, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config5 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":false}}"; + rc = mqtt_option_init(plugin, config5, &option); + EXPECT_EQ(-5, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config6 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":false,\"host\":\"192.168.10.116\"}}"; + rc = mqtt_option_init(plugin, config6, &option); + EXPECT_EQ(-6, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config7 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":false,\"host\":\"192.168.10.116\",\"port\":1883}}"; + rc = mqtt_option_init(plugin, config7, &option); EXPECT_EQ(0, rc); mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config8 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":false,\"host\":\"192.168.10.116\",\"port\":1883," + "\"username\":\"\"}}"; + rc = mqtt_option_init(plugin, config8, &option); + EXPECT_EQ(0, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config9 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":false,\"host\":\"192.168.10.116\",\"port\":1883," + "\"username\":\"\",\"password\":\"\"}}"; + rc = mqtt_option_init(plugin, config9, &option); + EXPECT_EQ(0, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config10 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":true,\"host\":\"192.168.10.116\",\"port\":1883," + "\"username\":\"\",\"password\":\"\"}}"; + rc = mqtt_option_init(plugin, config10, &option); + EXPECT_EQ(-3, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config11 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":true,\"host\":\"192.168.10.116\",\"port\":1883," + "\"username\":\"\",\"password\":\"\",\"ca\":\"\"}}"; + rc = mqtt_option_init(plugin, config11, &option); + EXPECT_EQ(0, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config12 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":true,\"host\":\"192.168.10.116\",\"port\":1883," + "\"username\":\"\",\"password\":\"\",\"ca\":\"\", " + "\"cert\":\"\"}}"; + rc = mqtt_option_init(plugin, config12, &option); + EXPECT_EQ(-4, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config13 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":false,\"host\":\"192.168.10.116\",\"port\":1883," + "\"username\":\"\",\"password\":\"\",\"ca\":\"\", " + "\"cert\":\"\", \"key\":\"\"}}"; + rc = mqtt_option_init(plugin, config13, &option); + EXPECT_EQ(0, rc); + mqtt_option_uninit(plugin, &option); + + memset(&option, 0, sizeof(neu_mqtt_option_t)); + char *config14 = + (char *) "{\"node_id\":5,\"params\":{\"client-id\":\"upload123\", " + "\"upload-topic\":\"\", \"heartbeat-topic\":\"\", \"format\": " + "0,\"ssl\":true,\"host\":\"192.168.10.116\",\"port\":1883," + "\"username\":\"\",\"password\":\"\",\"ca\":\"\", " + "\"cert\":\"\", \"key\":\"\", \"keypass\":\"\"}}"; + rc = mqtt_option_init(plugin, config14, &option); + EXPECT_EQ(0, rc); + mqtt_option_uninit(plugin, &option); + free(plugin); }