plugins types FEATURE binary LYB value support
diff --git a/tests/utests/CMakeLists.txt b/tests/utests/CMakeLists.txt
index caa41b3..0b91121 100644
--- a/tests/utests/CMakeLists.txt
+++ b/tests/utests/CMakeLists.txt
@@ -17,6 +17,7 @@
ly_add_utest(NAME int8 SOURCES types/int8.c)
ly_add_utest(NAME string SOURCES types/string.c)
ly_add_utest(NAME bits SOURCES types/bits.c)
+ly_add_utest(NAME binary SOURCES types/binary.c)
ly_add_utest(NAME inet_types SOURCES types/inet_types.c)
ly_add_utest(NAME yang_types SOURCES types/yang_types.c)
diff --git a/tests/utests/data/test_types.c b/tests/utests/data/test_types.c
index c3164da..4a59581 100644
--- a/tests/utests/data/test_types.c
+++ b/tests/utests/data/test_types.c
@@ -350,6 +350,7 @@
TEST_TYPE_ERROR("enums", "black", "Invalid enumeration value \"black\".", "1");
}
+#if 0
static void
test_binary(void **state)
{
@@ -403,6 +404,8 @@
TEST_TYPE_ERROR("binary", "TQ==", "This base64 value must be of length 5.", "1");
}
+#endif
+
static void
test_boolean(void **state)
{
@@ -1045,7 +1048,7 @@
/* UTEST(test_string, setup),*/
/* UTEST(test_bits, setup), */
UTEST(test_enums, setup),
- UTEST(test_binary, setup),
+ /* UTEST(test_binary, setup), */
UTEST(test_boolean, setup),
UTEST(test_empty, setup),
UTEST(test_identityref, setup),
diff --git a/tests/utests/types/binary.c b/tests/utests/types/binary.c
new file mode 100644
index 0000000..2fbb6e4
--- /dev/null
+++ b/tests/utests/types/binary.c
@@ -0,0 +1,140 @@
+/**
+ * @file binary.c
+ * @author Michal Vaško <mvasko@cesnet.cz>
+ * @brief test for built-in binary type
+ *
+ * Copyright (c) 2021 CESNET, z.s.p.o.
+ *
+ * This source code is licensed under BSD 3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://opensource.org/licenses/BSD-3-Clause
+ */
+
+/* INCLUDE UTEST HEADER */
+#define _UTEST_MAIN_
+#include "../utests.h"
+
+/* LOCAL INCLUDE HEADERS */
+#include "libyang.h"
+
+#define MODULE_CREATE_YANG(MOD_NAME, NODES) \
+ "module " MOD_NAME " {\n" \
+ " yang-version 1.1;\n" \
+ " namespace \"urn:tests:" MOD_NAME "\";\n" \
+ " prefix pref;\n" \
+ NODES \
+ "}\n"
+
+static void
+test_plugin_store(void **state)
+{
+ const char *val, *dec_val;
+ char bin_val[2];
+ struct ly_err_item *err = NULL;
+ const struct lys_module *mod;
+ struct lyd_value value = {0};
+ struct lyplg_type *type = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[LY_TYPE_BINARY]);
+ struct lysc_type *lysc_type;
+ LY_ERR ly_ret;
+ const char *schema;
+
+ /* create schema. Prepare common used variables */
+ schema = MODULE_CREATE_YANG("a", "leaf l {type binary;}");
+ UTEST_ADD_MODULE(schema, LYS_IN_YANG, NULL, &mod);
+ lysc_type = ((struct lysc_node_leaf *)mod->compiled->data)->type;
+
+ /* check proper type */
+ assert_string_equal("libyang 2 - binary, version 1", type->id);
+
+ /* check store XML double pad */
+ val = "YWhveQ==";
+ dec_val = "ahoy";
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, val, strlen(val),
+ 0, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &value, NULL, &err));
+ CHECK_LYD_VALUE(value, BINARY, val, dec_val, strlen(dec_val));
+ assert_ptr_equal(value.realtype, lysc_type);
+ type->free(UTEST_LYCTX, &value);
+
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, dec_val, strlen(dec_val),
+ 0, LY_VALUE_LYB, NULL, 0, NULL, &value, NULL, &err));
+ CHECK_LYD_VALUE(value, BINARY, val, dec_val, strlen(dec_val));
+ assert_ptr_equal(value.realtype, lysc_type);
+ type->free(UTEST_LYCTX, &value);
+
+ /* single pad */
+ val = "YWhveWo=";
+ dec_val = "ahoyj";
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, val, strlen(val),
+ 0, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &value, NULL, &err));
+ CHECK_LYD_VALUE(value, BINARY, val, dec_val, strlen(dec_val));
+ assert_ptr_equal(value.realtype, lysc_type);
+ type->free(UTEST_LYCTX, &value);
+
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, dec_val, strlen(dec_val),
+ 0, LY_VALUE_LYB, NULL, 0, NULL, &value, NULL, &err));
+ CHECK_LYD_VALUE(value, BINARY, val, dec_val, strlen(dec_val));
+ assert_ptr_equal(value.realtype, lysc_type);
+ type->free(UTEST_LYCTX, &value);
+
+ /* no pad */
+ val = "YWhveWoy";
+ dec_val = "ahoyj2";
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, val, strlen(val),
+ 0, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &value, NULL, &err));
+ CHECK_LYD_VALUE(value, BINARY, val, dec_val, strlen(dec_val));
+ assert_ptr_equal(value.realtype, lysc_type);
+ type->free(UTEST_LYCTX, &value);
+
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, dec_val, strlen(dec_val),
+ 0, LY_VALUE_LYB, NULL, 0, NULL, &value, NULL, &err));
+ CHECK_LYD_VALUE(value, BINARY, val, dec_val, strlen(dec_val));
+ assert_ptr_equal(value.realtype, lysc_type);
+ type->free(UTEST_LYCTX, &value);
+
+ /* binary data */
+ val = "q80=";
+ bin_val[0] = 0xab;
+ bin_val[1] = 0xcd;
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, val, strlen(val),
+ 0, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &value, NULL, &err));
+ CHECK_LYD_VALUE(value, BINARY, val, bin_val, 2);
+ assert_ptr_equal(value.realtype, lysc_type);
+ type->free(UTEST_LYCTX, &value);
+
+ assert_int_equal(LY_SUCCESS, type->store(UTEST_LYCTX, lysc_type, bin_val, 2,
+ 0, LY_VALUE_LYB, NULL, 0, NULL, &value, NULL, &err));
+ CHECK_LYD_VALUE(value, BINARY, val, bin_val, 2);
+ assert_ptr_equal(value.realtype, lysc_type);
+ type->free(UTEST_LYCTX, &value);
+
+ /*
+ * ERROR TESTS
+ */
+ val = "q80.";
+ err = NULL;
+ ly_ret = type->store(UTEST_LYCTX, lysc_type, val, strlen(val),
+ 0, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &value, NULL, &err);
+ assert_int_equal(LY_EVALID, ly_ret);
+ assert_string_equal(err->msg, "Invalid Base64 character '.'.");
+ ly_err_free(err);
+
+ val = "q80";
+ err = NULL;
+ ly_ret = type->store(UTEST_LYCTX, lysc_type, val, strlen(val),
+ 0, LY_VALUE_XML, NULL, LYD_VALHINT_STRING, NULL, &value, NULL, &err);
+ assert_int_equal(LY_EVALID, ly_ret);
+ assert_string_equal(err->msg, "Base64 encoded value length must be divisible by 4.");
+ ly_err_free(err);
+}
+
+int
+main(void)
+{
+ const struct CMUnitTest tests[] = {
+ UTEST(test_plugin_store)
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/tests/utests/utests.h b/tests/utests/utests.h
index 9be12c3..9b15c05 100644
--- a/tests/utests/utests.h
+++ b/tests/utests/utests.h
@@ -1069,8 +1069,12 @@
* Example CHECK_LYD_VALUE(node->value, BINARY, "aGVs\nbG8=");
* @param[in] NODE lyd_value variable
* @param[in] CANNONICAL_VAL expected cannonical value
+ * @param[in] VALUE expected value data
+ * @param[in] SIZE expected value data size
*/
-#define CHECK_LYD_VALUE_BINARY(NODE, CANNONICAL_VAL) \
+#define CHECK_LYD_VALUE_BINARY(NODE, CANNONICAL_VAL, VALUE, SIZE) \
+ assert_int_equal((NODE).bin->size, SIZE); \
+ assert_int_equal(0, memcmp((NODE).bin->data, VALUE, SIZE)); \
assert_non_null((NODE).realtype->plugin->print(UTEST_LYCTX, &(NODE), LY_VALUE_CANON, NULL, NULL, NULL)); \
assert_string_equal((NODE)._canonical, CANNONICAL_VAL); \
assert_non_null((NODE).realtype); \