diff --git a/libcommon/base64_common.c b/libcommon/base64_common.c index 4acdc9a1..9ce2d012 100644 --- a/libcommon/base64_common.c +++ b/libcommon/base64_common.c @@ -71,6 +71,8 @@ int mosquitto_base64_decode(const char *in, unsigned char **decoded, unsigned in int rc = 1; slen = strlen(in); + *decoded = NULL; + *decoded_len = 0; b64 = BIO_new(BIO_f_base64()); if(!b64){ diff --git a/test/unit/libcommon/CMakeLists.txt b/test/unit/libcommon/CMakeLists.txt index d4f3be37..6dc5974a 100644 --- a/test/unit/libcommon/CMakeLists.txt +++ b/test/unit/libcommon/CMakeLists.txt @@ -1,4 +1,5 @@ add_executable(libcommon-test + base64_test.c property_add.c property_value.c strings_test.c diff --git a/test/unit/libcommon/Makefile b/test/unit/libcommon/Makefile index 913bcbfa..c4293113 100644 --- a/test/unit/libcommon/Makefile +++ b/test/unit/libcommon/Makefile @@ -13,6 +13,7 @@ ifeq ($(WITH_TLS),yes) endif TEST_OBJS = \ + base64_test.o \ property_add.o \ property_value.o \ strings_test.o \ diff --git a/test/unit/libcommon/base64_test.c b/test/unit/libcommon/base64_test.c new file mode 100644 index 00000000..73b1b9e2 --- /dev/null +++ b/test/unit/libcommon/base64_test.c @@ -0,0 +1,157 @@ +#include +#include + +#include "mosquitto/mqtt_protocol.h" +#include "property_common.h" + +//int mosquitto_base64_encode(const unsigned char *in, size_t in_len, char **encoded); +//int mosquitto_base64_decode(const char *in, unsigned char **decoded, unsigned int *decoded_len); + +static void check_encode(const char *input, size_t in_len, const char *expected_output) +{ + char *encoded; + int rc = mosquitto_base64_encode((const unsigned char *)input, in_len, &encoded); + + CU_ASSERT_EQUAL(rc, MOSQ_ERR_SUCCESS); + CU_ASSERT_STRING_EQUAL(encoded, expected_output); + if(strcmp(encoded, expected_output)){ + printf("%s || %s\n", encoded, expected_output); + } + mosquitto_free(encoded); +} + + +static void check_decode(const char *input, int expected_rc, const char *expected_output, unsigned int expected_len) +{ + unsigned char *decoded; + unsigned int len; + int rc = mosquitto_base64_decode(input, &decoded, &len); + + CU_ASSERT_EQUAL(rc, expected_rc); + if(rc != expected_rc){ + printf("rc: %d||%d\n", rc, expected_rc); + } + if(len != expected_len){ + printf("len: %d||%d\n", len, expected_len); + } + CU_ASSERT_EQUAL(len, expected_len); + if(decoded){ + CU_ASSERT_EQUAL(memcmp(decoded, expected_output, len), 0); + mosquitto_free(decoded); + } +} + + +static void TEST_encode_empty(void) +{ + check_encode("", 0, ""); +} + + +static void TEST_encode_string_lengths(void) +{ + check_encode("a", 1, "YQ=="); + check_encode("ab", 2, "YWI="); + check_encode("abc", 3, "YWJj"); + check_encode("abcd", 4, "YWJjZA=="); + check_encode("abcde", 5, "YWJjZGU="); + check_encode("abcdef", 6, "YWJjZGVm"); + check_encode("abcdefg", 7, "YWJjZGVmZw=="); +} + + +static void TEST_encode_binary(void) +{ + const char a[1] = {0}; + const char b[2] = {0, 1}; + const char c[3] = {0, 1, 2}; + const char d[4] = {0, 1, 2, 3}; + const char e[5] = {0, 1, 2, 3, 4}; + const char f[6] = {0, 1, 2, 3, 4, 5}; + const char g[7] = {0, 1, 2, 3, 4, 5, 6}; + + check_encode(a, 1, "AA=="); + check_encode(b, 2, "AAE="); + check_encode(c, 3, "AAEC"); + check_encode(d, 4, "AAECAw=="); + check_encode(e, 5, "AAECAwQ="); + check_encode(f, 6, "AAECAwQF"); + check_encode(g, 7, "AAECAwQFBg=="); +} + + +static void TEST_decode_empty(void) +{ + check_decode("", 1, "", 0); +} + + +static void TEST_decode_invalid(void) +{ + check_decode("abc", 1, "", 0); +} + + +static void TEST_decode_string_lengths(void) +{ + check_decode("YQ==", MOSQ_ERR_SUCCESS, "a", 1); + check_decode("YWI=", MOSQ_ERR_SUCCESS, "ab", 2); + check_decode("YWJj", MOSQ_ERR_SUCCESS, "abc", 3); + check_decode("YWJjZA==", MOSQ_ERR_SUCCESS, "abcd", 4); + check_decode("YWJjZGU=", MOSQ_ERR_SUCCESS, "abcde", 5); + check_decode("YWJjZGVm", MOSQ_ERR_SUCCESS, "abcdef", 6); + check_decode("YWJjZGVmZw==", MOSQ_ERR_SUCCESS, "abcdefg", 7); +} + + +static void TEST_decode_binary(void) +{ + const char a[1] = {0}; + const char b[2] = {0, 1}; + const char c[3] = {0, 1, 2}; + const char d[4] = {0, 1, 2, 3}; + const char e[5] = {0, 1, 2, 3, 4}; + const char f[6] = {0, 1, 2, 3, 4, 5}; + const char g[7] = {0, 1, 2, 3, 4, 5, 6}; + + check_decode("AA==", MOSQ_ERR_SUCCESS, a, 1); + check_decode("AAE=", MOSQ_ERR_SUCCESS, b, 2); + check_decode("AAEC", MOSQ_ERR_SUCCESS, c, 3); + check_decode("AAECAw==", MOSQ_ERR_SUCCESS, d, 4); + check_decode("AAECAwQ=", MOSQ_ERR_SUCCESS, e, 5); + check_decode("AAECAwQF", MOSQ_ERR_SUCCESS, f, 6); + check_decode("AAECAwQFBg==", MOSQ_ERR_SUCCESS, g, 7); +} + + +/* ======================================================================== + * TEST SUITE SETUP + * ======================================================================== */ + + +int init_base64_tests(void) +{ + CU_pSuite test_suite = NULL; + + test_suite = CU_add_suite("base64", NULL, NULL); + if(!test_suite){ + printf("Error adding CUnit base64 test suite.\n"); + return 1; + } + + if(0 + || !CU_add_test(test_suite, "Encode Empty", TEST_encode_empty) + || !CU_add_test(test_suite, "Encode String lengths", TEST_encode_string_lengths) + || !CU_add_test(test_suite, "Encode Binary", TEST_encode_binary) + || !CU_add_test(test_suite, "Decode Empty", TEST_decode_empty) + || !CU_add_test(test_suite, "Decode Invalid", TEST_decode_invalid) + || !CU_add_test(test_suite, "Decode String lengths", TEST_decode_string_lengths) + || !CU_add_test(test_suite, "Decode Binary", TEST_decode_binary) + ){ + + printf("Error adding Property Add CUnit tests.\n"); + return 1; + } + + return 0; +} diff --git a/test/unit/libcommon/test.c b/test/unit/libcommon/test.c index ad240d48..9aa5187f 100644 --- a/test/unit/libcommon/test.c +++ b/test/unit/libcommon/test.c @@ -4,6 +4,7 @@ #include #include +int init_base64_tests(void); int init_property_add_tests(void); int init_property_value_tests(void); int init_strings_tests(void); @@ -25,6 +26,7 @@ int main(int argc, char *argv[]) } if(0 + || init_base64_tests() || init_property_add_tests() || init_property_value_tests() || init_strings_tests() diff --git a/test/unit/libcommon/topic_test.c b/test/unit/libcommon/topic_test.c index 2ef46749..afdf2a1c 100644 --- a/test/unit/libcommon/topic_test.c +++ b/test/unit/libcommon/topic_test.c @@ -1392,6 +1392,7 @@ static void TEST_pub_topic_invalid(void) pub_topic_helper("pub/topic#", MOSQ_ERR_INVAL); pub_topic_helper("pub/topic/#", MOSQ_ERR_INVAL); pub_topic_helper("+/pub/topic", MOSQ_ERR_INVAL); + pub_topic_helper("//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////a", MOSQ_ERR_INVAL); } @@ -1440,6 +1441,7 @@ static void TEST_sub_topic_invalid(void) sub_topic_helper("sub/#topic", MOSQ_ERR_INVAL); sub_topic_helper("sub/topic#", MOSQ_ERR_INVAL); sub_topic_helper("#/sub/topic", MOSQ_ERR_INVAL); + sub_topic_helper("//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////a", MOSQ_ERR_INVAL); }