Add missing int to string conversions.

pull/2345/merge
Roger A. Light 3 years ago
parent ba665a3477
commit 1e0a07ba5a

@ -175,7 +175,7 @@ static void broker__payload_callback(struct mosq_ctrl *ctrl, long payloadlen, co
j_error = cJSON_GetObjectItem(j_response, "error");
if(j_error){
fprintf(stderr, "%s: Error: %s\n", j_command->valuestring, j_error->valuestring);
fprintf(stderr, "%s: Error: %s.\n", j_command->valuestring, j_error->valuestring);
}else{
if(!strcasecmp(j_command->valuestring, "listPlugins")){
print_plugin_info(j_response);

@ -394,7 +394,7 @@ static void dynsec__payload_callback(struct mosq_ctrl *ctrl, long payloadlen, co
j_error = cJSON_GetObjectItem(j_response, "error");
if(j_error){
fprintf(stderr, "%s: Error: %s\n", j_command->valuestring, j_error->valuestring);
fprintf(stderr, "%s: Error: %s.\n", j_command->valuestring, j_error->valuestring);
}else{
if(!strcasecmp(j_command->valuestring, "listClients")){
print_list(j_response, "clients", "username");

@ -110,7 +110,7 @@ int main(int argc, char *argv[])
}else if(rc == MOSQ_ERR_UNKNOWN){
/* Message printed already */
}else{
fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
fprintf(stderr, "Error: %s.\n", mosquitto_strerror(rc));
}
}

@ -709,7 +709,7 @@ int client_connect(struct mosquitto *mosq, struct mosq_config *cfg)
#else
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL);
#endif
fprintf(stderr, "Error: %s\n", err);
fprintf(stderr, "Error: %s.\n", err);
}else{
fprintf(stderr, "Unable to connect (%s).\n", mosquitto_strerror(rc));
}

@ -31,62 +31,109 @@ Contributors:
const char *mosquitto_strerror(int mosq_errno)
{
switch(mosq_errno){
case MOSQ_ERR_QUOTA_EXCEEDED:
return "Quota exceeded";
case MOSQ_ERR_AUTH_DELAYED:
return "Authentication delayed";
case MOSQ_ERR_AUTH_CONTINUE:
return "Continue with authentication.";
return "Continue with authentication";
case MOSQ_ERR_NO_SUBSCRIBERS:
return "No subscribers.";
return "No subscribers";
case MOSQ_ERR_SUB_EXISTS:
return "Subscription already exists.";
return "Subscription already exists";
case MOSQ_ERR_CONN_PENDING:
return "Connection pending.";
return "Connection pending";
case MOSQ_ERR_SUCCESS:
return "No error.";
return "No error";
case MOSQ_ERR_NOMEM:
return "Out of memory.";
return "Out of memory";
case MOSQ_ERR_PROTOCOL:
return "A network protocol error occurred when communicating with the broker.";
return "A network protocol error occurred when communicating with the broker";
case MOSQ_ERR_INVAL:
return "Invalid input.";
return "Invalid input";
case MOSQ_ERR_NO_CONN:
return "The client is not currently connected.";
return "The client is not currently connected";
case MOSQ_ERR_CONN_REFUSED:
return "The connection was refused.";
return "The connection was refused";
case MOSQ_ERR_NOT_FOUND:
return "Message not found (internal error).";
return "Message not found (internal error)";
case MOSQ_ERR_CONN_LOST:
return "The connection was lost.";
return "The connection was lost";
case MOSQ_ERR_TLS:
return "A TLS error occurred.";
return "A TLS error occurred";
case MOSQ_ERR_PAYLOAD_SIZE:
return "Payload too large.";
return "Payload too large";
case MOSQ_ERR_NOT_SUPPORTED:
return "This feature is not supported.";
return "This feature is not supported";
case MOSQ_ERR_AUTH:
return "Authorisation failed.";
return "Authorisation failed";
case MOSQ_ERR_ACL_DENIED:
return "Access denied by ACL.";
return "Access denied by ACL";
case MOSQ_ERR_UNKNOWN:
return "Unknown error.";
return "Unknown error";
case MOSQ_ERR_ERRNO:
return strerror(errno);
case MOSQ_ERR_EAI:
return "Lookup error.";
return "Lookup error";
case MOSQ_ERR_PROXY:
return "Proxy error.";
return "Proxy error";
case MOSQ_ERR_PLUGIN_DEFER:
return "Plugin deferring result";
case MOSQ_ERR_MALFORMED_UTF8:
return "Malformed UTF-8";
case MOSQ_ERR_KEEPALIVE:
return "Keepalive exceeded";
case MOSQ_ERR_LOOKUP:
return "Lookup failed";
case MOSQ_ERR_MALFORMED_PACKET:
return "Malformed packet";
case MOSQ_ERR_DUPLICATE_PROPERTY:
return "Duplicate property in property list";
case MOSQ_ERR_TLS_HANDSHAKE:
return "TLS handshake failed.";
return "TLS handshake failed";
case MOSQ_ERR_QOS_NOT_SUPPORTED:
return "Requested QoS not supported on server.";
return "Requested QoS not supported on server";
case MOSQ_ERR_OVERSIZE_PACKET:
return "Packet larger than supported by the server.";
return "Packet larger than supported by the server";
case MOSQ_ERR_OCSP:
return "OCSP error.";
return "OCSP error";
case MOSQ_ERR_TIMEOUT:
return "Timeout";
case MOSQ_ERR_ALREADY_EXISTS:
return "Entry already exists";
case MOSQ_ERR_PLUGIN_IGNORE:
return "Ignore plugin";
case MOSQ_ERR_UNSPECIFIED:
return "Unspecified error";
case MOSQ_ERR_IMPLEMENTATION_SPECIFIC:
return "Implementaion specific error";
case MOSQ_ERR_CLIENT_IDENTIFIER_NOT_VALID:
return "Client identifier not valid";
case MOSQ_ERR_BAD_USERNAME_OR_PASSWORD:
return "Bad username or password";
case MOSQ_ERR_SERVER_UNAVAILABLE:
return "Server unavailable";
case MOSQ_ERR_SERVER_BUSY:
return "Server busy";
case MOSQ_ERR_BANNED:
return "Banned";
case MOSQ_ERR_BAD_AUTHENTICATION_METHOD:
return "Bad authentication method";
case MOSQ_ERR_SESSION_TAKEN_OVER:
return "Session taken over";
case MOSQ_ERR_RECEIVE_MAXIMUM_EXCEEDED:
return "Receive maximum exceeded";
case MOSQ_ERR_TOPIC_ALIAS_INVALID:
return "Topic alias invalid";
case MOSQ_ERR_ADMINISTRATIVE_ACTION:
return "Administrative action";
case MOSQ_ERR_RETAIN_NOT_SUPPORTED:
return "Retain not supported";
case MOSQ_ERR_CONNECTION_RATE_EXCEEDED:
return "Connection rate exceeded";
default:
return "Unknown error.";
return "Unknown error";
}
}
@ -94,19 +141,19 @@ const char *mosquitto_connack_string(int connack_code)
{
switch(connack_code){
case 0:
return "Connection Accepted.";
return "Connection Accepted";
case 1:
return "Connection Refused: unacceptable protocol version.";
return "Connection Refused: unacceptable protocol version";
case 2:
return "Connection Refused: identifier rejected.";
return "Connection Refused: identifier rejected";
case 3:
return "Connection Refused: broker unavailable.";
return "Connection Refused: broker unavailable";
case 4:
return "Connection Refused: bad user name or password.";
return "Connection Refused: bad user name or password";
case 5:
return "Connection Refused: not authorised.";
return "Connection Refused: not authorised";
default:
return "Connection Refused: unknown reason.";
return "Connection Refused: unknown reason";
}
}
@ -224,8 +271,12 @@ int mosquitto_string_to_command(const char *str, int *cmd)
*cmd = CMD_PUBCOMP;
}else if(!strcasecmp(str, "subscribe")){
*cmd = CMD_SUBSCRIBE;
}else if(!strcasecmp(str, "suback")){
*cmd = CMD_SUBACK;
}else if(!strcasecmp(str, "unsubscribe")){
*cmd = CMD_UNSUBSCRIBE;
}else if(!strcasecmp(str, "unsuback")){
*cmd = CMD_UNSUBACK;
}else if(!strcasecmp(str, "disconnect")){
*cmd = CMD_DISCONNECT;
}else if(!strcasecmp(str, "auth")){
@ -233,6 +284,7 @@ int mosquitto_string_to_command(const char *str, int *cmd)
}else if(!strcasecmp(str, "will")){
*cmd = CMD_WILL;
}else{
*cmd = 0;
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_SUCCESS;

@ -26,6 +26,7 @@ add_executable(broker-test
property_read.c
property_user_read.c
property_write.c
strings_test.c
stubs.c
util_topic_test.c
utf8.c
@ -36,6 +37,7 @@ add_executable(broker-test
../../lib/property_mosq.c
../../lib/memory_mosq.c
../../common/misc_mosq.c
../../lib/strings_mosq.c
../../lib/util_mosq.c
../../lib/util_topic.c
../../lib/utf8_mosq.c

@ -24,6 +24,7 @@ TEST_OBJS = \
property_read.o \
property_user_read.o \
property_write.o \
strings_test.o \
lib_stubs.o \
util_topic_test.o \
utf8.o
@ -34,6 +35,7 @@ LIB_OBJS = \
${R}/lib/packet_datatypes.o \
${R}/lib/packet_mosq.o \
${R}/lib/property_mosq.o \
${R}/lib/strings_mosq.o \
${R}/lib/util_mosq.o \
${R}/lib/util_topic.o \
${R}/lib/utf8_mosq.o

@ -0,0 +1,205 @@
/* Tests for int to string functions. */
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include "mosquitto.h"
static void TEST_mosquitto_strerror(void)
{
const char *str;
int used[] = {-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, /* 13, */ 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 31, 32,
128, 131, 133, 134, 136, 137, 138, 140, 142, 147, 148, 152, 154, 159};
/* Iterate over all possible errors, checking we have a place holder for all
* unused errors, and that all used errors do not have place holder text. */
for(int err=-256; err<256; err++){
str = mosquitto_strerror(err);
CU_ASSERT_PTR_NOT_NULL(str);
if(str){
bool is_used = false;
for(size_t i=0; i<sizeof(used)/sizeof(int); i++){
if(err == used[i]){
is_used = true;
break;
}
}
if(is_used){
CU_ASSERT_STRING_NOT_EQUAL(str, "Unknown error");
if(!strcmp(str, "Unknown error")){
printf("%d: %s\n", err, str);
}
}else{
CU_ASSERT_STRING_EQUAL(str, "Unknown error");
if(strcmp(str, "Unknown error")){
printf("%d: %s\n", err, str);
}
}
}
}
}
static void TEST_mosquitto_connack_string(void)
{
const char *str;
uint8_t used[] = {0, 1, 2, 3, 4, 5};
/* Iterate over all possible codes, checking we have a place holder for all
* unused codes, and that all used codes do not have place holder text. */
for(int code=0; code<256; code++){
str = mosquitto_connack_string(code);
CU_ASSERT_PTR_NOT_NULL(str);
if(str){
bool is_used = false;
for(size_t i=0; i<sizeof(used); i++){
if(code == used[i]){
is_used = true;
break;
}
}
if(is_used){
CU_ASSERT_STRING_NOT_EQUAL(str, "Connection Refused: unknown reason");
if(!strcmp(str, "Connection Refused: unknown reason.")){
printf("%d: %s\n", code, str);
}
}else{
CU_ASSERT_STRING_EQUAL(str, "Connection Refused: unknown reason");
if(strcmp(str, "Connection Refused: unknown reason")){
printf("%d: %s\n", code, str);
}
}
}
}
}
static void TEST_mosquitto_reason_string(void)
{
const char *str;
uint8_t used[] = {0, 1, 2, 4, 16, 17, 24, 25, 128, 129, 130, 131, 132, 133,
134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147,
148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161,
162};
/* Iterate over all possible codes, checking we have a place holder for all
* unused codes, and that all used codes do not have place holder text. */
for(int code=0; code<256; code++){
str = mosquitto_reason_string(code);
CU_ASSERT_PTR_NOT_NULL(str);
if(str){
bool is_used = false;
for(size_t i=0; i<sizeof(used); i++){
if(code == used[i]){
is_used = true;
break;
}
}
if(is_used){
CU_ASSERT_STRING_NOT_EQUAL(str, "Unknown reason");
if(!strcmp(str, "Unknown reason")){
printf("%d: %s\n", code, str);
}
}else{
CU_ASSERT_STRING_EQUAL(str, "Unknown reason");
if(strcmp(str, "Unknown reason")){
printf("%d: %s\n", code, str);
}
}
}
}
}
static void TEST_mosquitto_string_to_command(void)
{
int rc, cmd;
rc = mosquitto_string_to_command("CONNECT", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_CONNECT);
rc = mosquitto_string_to_command("CONNACK", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_CONNACK);
rc = mosquitto_string_to_command("PUBLISH", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_PUBLISH);
rc = mosquitto_string_to_command("PUBACK", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_PUBACK);
rc = mosquitto_string_to_command("PUBREC", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_PUBREC);
rc = mosquitto_string_to_command("PUBREL", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_PUBREL);
rc = mosquitto_string_to_command("PUBCOMP", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_PUBCOMP);
rc = mosquitto_string_to_command("SUBSCRIBE", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_SUBSCRIBE);
rc = mosquitto_string_to_command("SUBACK", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_SUBACK);
rc = mosquitto_string_to_command("UNSUBSCRIBE", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_UNSUBSCRIBE);
rc = mosquitto_string_to_command("UNSUBACK", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_UNSUBACK);
rc = mosquitto_string_to_command("DISCONNECT", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_DISCONNECT);
rc = mosquitto_string_to_command("AUTH", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_AUTH);
rc = mosquitto_string_to_command("WILL", &cmd);
CU_ASSERT_EQUAL(rc, 0);
CU_ASSERT_EQUAL(cmd, CMD_WILL);
rc = mosquitto_string_to_command("CONNACT", &cmd);
CU_ASSERT_EQUAL(rc, MOSQ_ERR_INVAL);
CU_ASSERT_EQUAL(cmd, 0);
}
/* ========================================================================
* TEST SUITE SETUP
* ======================================================================== */
int init_strings_tests(void)
{
CU_pSuite test_suite = NULL;
test_suite = CU_add_suite("Strings", NULL, NULL);
if(!test_suite){
printf("Error adding CUnit test suite.\n");
return 1;
}
if(0
|| !CU_add_test(test_suite, "mosquitto_strerror", TEST_mosquitto_strerror)
|| !CU_add_test(test_suite, "mosquitto_connack_string", TEST_mosquitto_connack_string)
|| !CU_add_test(test_suite, "mosquitto_reason_string", TEST_mosquitto_reason_string)
|| !CU_add_test(test_suite, "mosquitto_string_to_command", TEST_mosquitto_string_to_command)
){
printf("Error adding Strings CUnit tests.\n");
return 1;
}
return 0;
}

@ -10,6 +10,7 @@ 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_strings_tests(void);
int init_utf8_tests(void);
int init_util_topic_tests(void);
int init_misc_trim_tests(void);
@ -36,6 +37,7 @@ int main(int argc, char *argv[])
|| init_property_write_tests()
|| init_util_topic_tests()
|| init_misc_trim_tests()
|| init_strings_tests()
){
CU_cleanup_registry();

Loading…
Cancel
Save