Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit test for base64 & property apis #633

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions src/supplemental/mqtt/mqtt_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1522,22 +1522,60 @@ test_property_api(void)
CONTENT_TYPE, "aaaaaa", strlen("aaaaaa"), true);
mqtt_property_append(plist, p7);

// get value type
NUTS_TRUE(
mqtt_property_get_value_type(PAYLOAD_FORMAT_INDICATOR) == U8);

property_data *p_data =
mqtt_property_get_value(plist, PAYLOAD_FORMAT_INDICATOR);
// get value
property_data *p_data = NULL;
p_data = mqtt_property_get_value(plist, PAYLOAD_FORMAT_INDICATOR);
NUTS_TRUE(p_data->p_value.u8 == 1);

// len
NUTS_TRUE(get_mqtt_properties_len(plist) == 54);

// dup
property *_plist = NULL;
NUTS_PASS(mqtt_property_dup(&_plist, plist));
NUTS_TRUE(get_mqtt_properties_len(_plist) == 54);
NUTS_PASS(mqtt_property_free(_plist));

// foreach
mqtt_property_foreach(plist, test_property_cb);

// pub by will
property *_p1 = NULL;
_p1 = mqtt_property_pub_by_will(plist);
p_data = mqtt_property_get_value(_p1, PAYLOAD_FORMAT_INDICATOR);
NUTS_TRUE(p_data->p_value.u8 == 1);
NUTS_PASS(mqtt_property_free(_p1));

// value copy
property *_p2 = NULL;
_p2 = property_alloc();
NUTS_PASS(mqtt_property_value_copy(_p2, p1));
NUTS_TRUE(_p2->data.p_value.u8 == 1);
memset(_p2, 0, sizeof(property));
NUTS_PASS(mqtt_property_value_copy(_p2, p2));
NUTS_TRUE(_p2->data.p_value.u16 == 10);
memset(_p2, 0, sizeof(property));
NUTS_PASS(mqtt_property_value_copy(_p2, p3));
NUTS_TRUE(_p2->data.p_value.u32 == 10);
memset(_p2, 0, sizeof(property));
NUTS_PASS(mqtt_property_value_copy(_p2, p4));
NUTS_PASS(strncmp((char *) _p2->data.p_value.str.buf, "aaaaaa", 6));
NUTS_PASS(property_free(_p2));
_p2 = property_alloc();
NUTS_PASS(mqtt_property_value_copy(_p2, p5));
NUTS_PASS(strncmp((char *) _p2->data.p_value.binary.buf, "aaaaaa", 6));
NUTS_PASS(property_free(_p2));
_p2 = property_alloc();
NUTS_PASS(mqtt_property_value_copy(_p2, p6));
NUTS_PASS(strncmp((char *) _p2->data.p_value.strpair.key.buf, "aaaaaa", 6));
NUTS_PASS(strncmp((char *) _p2->data.p_value.strpair.value.buf, "aaaaaa", 6));
NUTS_PASS(property_free(_p2));

NUTS_PASS(mqtt_property_free(plist));
NUTS_PASS(mqtt_property_free(_plist));
}

TEST_LIST = {
Expand Down
1 change: 1 addition & 0 deletions src/supplemental/nanolib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ nng_test(hash_test)
nng_test(dbtree_test)
nng_test(conf_test)
nng_test(rule_test)
nng_test(lib_base64_test)

if (SUPP_RULE_ENGINE)
nng_sources(rule.c)
Expand Down
82 changes: 82 additions & 0 deletions src/supplemental/nanolib/lib_base64_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include <string.h>

#include <nng/nng.h>

#include "nng/supplemental/nanolib/base64.h"

#include <acutest.h>

typedef struct {
char *decoded;
char *encoded;
} test_case;

static test_case cases[] = {
{ "", "" },
{ "f", "Zg==" },
{ "fo", "Zm8=" },
{ "foo", "Zm9v" },
{ "foob", "Zm9vYg==" },
{ "fooba", "Zm9vYmE=" },
{ "foobar", "Zm9vYmFy" },
{ NULL, NULL },
};

void
test_encode(void)
{
int i;
void *dec;

for (i = 0; (dec = cases[i].decoded) != NULL; i++) {
char buf[1024];
char name[8];
int rv;

(void) snprintf(name, sizeof(name), "%d", i);
TEST_CASE(name);
rv = base64_encode(dec, strlen(dec), buf);
TEST_CHECK(rv >= 0);
TEST_CHECK(rv == (int) strlen(cases[i].encoded));
buf[rv] = 0;
TEST_CHECK(strcmp(buf, cases[i].encoded) == 0);
}
}

void
test_decode(void)
{
int i;
void *enc;

for (i = 0; (enc = cases[i].encoded) != NULL; i++) {
char buf[1024];
char name[8];
size_t sz;

(void) snprintf(name, sizeof(name), "%d", i);
TEST_CASE(name);

sz = base64_decode(enc, strlen(enc), (void *) buf);
TEST_CHECK(sz != (size_t) -1);
TEST_CHECK(sz == strlen(cases[i].decoded));
buf[sz] = 0;
TEST_CHECK(strcmp(buf, cases[i].decoded) == 0);
}
}

TEST_LIST = {
{ "encode", test_encode },
{ "decode", test_decode },
{ NULL, NULL },
};
Loading