Index: head/lib/libnv/tests/cnv_tests.cc =================================================================== --- head/lib/libnv/tests/cnv_tests.cc (revision 335342) +++ head/lib/libnv/tests/cnv_tests.cc (revision 335343) @@ -1,1508 +1,1508 @@ /*- * Copyright (c) 2016 Adam Starak * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #define fd_is_valid(fd) (fcntl((fd), F_GETFL) != -1 || errno != EBADF) /* ATF cnvlist_get tests. */ ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_bool); ATF_TEST_CASE_BODY(cnvlist_get_bool) { nvlist_t *nvl; const char *key; bool value; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = true; nvlist_add_bool(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_bool(nvl, key)); ATF_REQUIRE_EQ(cnvlist_get_bool(cookie), value); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_number); ATF_TEST_CASE_BODY(cnvlist_get_number) { nvlist_t *nvl; const char *key; uint64_t value; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = 420; nvlist_add_number(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_number(nvl, key)); ATF_REQUIRE_EQ(cnvlist_get_number(cookie), value); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_string); ATF_TEST_CASE_BODY(cnvlist_get_string) { nvlist_t *nvl; const char *key; const char *value; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = "text"; nvlist_add_string(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_STRING); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_string(nvl, key)); ATF_REQUIRE_EQ(strcmp(cnvlist_get_string(cookie), value), 0); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_nvlist); ATF_TEST_CASE_BODY(cnvlist_get_nvlist) { nvlist_t *nvl, *value; const nvlist_t *result; const char *key, *subkey; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); value = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); key = "name"; subkey = "subname"; cookie = NULL; /* Add null to 'value' nvlist. */ nvlist_add_null(value, subkey); ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(value, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(value), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NULL); ATF_REQUIRE(!nvlist_empty(value)); ATF_REQUIRE(nvlist_exists(value, subkey)); ATF_REQUIRE(nvlist_exists_null(value, subkey)); ATF_REQUIRE_EQ(nvlist_next(value, &type, &cookie), static_cast(NULL)); /* Add 'value' nvlist. */ cookie = NULL; nvlist_add_nvlist(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); /* * Assuming nvlist_get_nvlist() is correct check if cnvlist returns * the same pointer. */ result = cnvlist_get_nvlist(cookie); ATF_REQUIRE_EQ(result, nvlist_get_nvlist(nvl, key)); ATF_REQUIRE(result != value); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); /* Validate data inside nvlist. */ cookie = NULL; ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(result, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(result), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NULL); ATF_REQUIRE(!nvlist_empty(result)); ATF_REQUIRE(nvlist_exists(result, subkey)); ATF_REQUIRE(nvlist_exists_null(result, subkey)); ATF_REQUIRE_EQ(nvlist_next(result, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); nvlist_destroy(value); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_descriptor); ATF_TEST_CASE_BODY(cnvlist_get_descriptor) { nvlist_t *nvl; const char *key; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_descriptor(nvl, key, STDERR_FILENO); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_DESCRIPTOR); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_descriptor(nvl, key)); ATF_REQUIRE_EQ(fd_is_valid(cnvlist_get_descriptor(cookie)), 1); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_binary); ATF_TEST_CASE_BODY(cnvlist_get_binary) { nvlist_t *nvl; const char *key; void *in_binary; const void *out_binary; void *cookie; int type; size_t in_size, out_size; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; in_size = 13; in_binary = malloc(in_size); ATF_REQUIRE(in_binary != NULL); memset(in_binary, 0xa5, in_size); nvlist_add_binary(nvl, key, in_binary, in_size); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_binary(nvl, key)); out_binary = cnvlist_get_binary(cookie, &out_size); ATF_REQUIRE_EQ(out_size, in_size); ATF_REQUIRE_EQ(memcmp(in_binary, out_binary, out_size), 0); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } /* ATF cnvlist_get array tests. */ ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_bool_array); ATF_TEST_CASE_BODY(cnvlist_get_bool_array) { nvlist_t *nvl; bool in_array[16]; const bool *out_array; const char *key; void *cookie; int type, i; size_t nitems; for (i = 0; i < 16; i++) in_array[i] = (i % 2 == 0); nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_bool_array(nvl, key, in_array, 16); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BOOL_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_bool_array(nvl, key)); out_array = cnvlist_get_bool_array(cookie, &nitems); ATF_REQUIRE_EQ(nitems, 16); ATF_REQUIRE(out_array != NULL); for (i = 0; i < 16; i++) ATF_REQUIRE_EQ(out_array[i], in_array[i]); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_number_array); ATF_TEST_CASE_BODY(cnvlist_get_number_array) { nvlist_t *nvl; uint64_t in_array[16]; const uint64_t *out_array; const char *key; void *cookie; int type, i; size_t nitems; for (i = 0; i < 16; i++) in_array[i] = i; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_number_array(nvl, key, in_array, 16); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_number_array(nvl, key)); out_array = cnvlist_get_number_array(cookie, &nitems); ATF_REQUIRE(out_array != NULL); ATF_REQUIRE_EQ(nitems, 16); for (i = 0; i < 16; i++) ATF_REQUIRE_EQ(out_array[i], in_array[i]); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_string_array); ATF_TEST_CASE_BODY(cnvlist_get_string_array) { nvlist_t *nvl; const char *in_array[4] = {"inequality", "sucks", ".", ""}; const char * const *out_array; const char *key; void *cookie; int type, i; size_t nitems; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_string_array(nvl, key, in_array, 4); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_STRING_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_string_array(nvl, key)); out_array = cnvlist_get_string_array(cookie, &nitems); ATF_REQUIRE_EQ(nitems, 4); ATF_REQUIRE(out_array != NULL); for (i = 0; i < 4; i++) { ATF_REQUIRE(out_array[i] != NULL); ATF_REQUIRE_EQ(strcmp(out_array[i], in_array[i]), 0); } ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_nvlist_array); ATF_TEST_CASE_BODY(cnvlist_get_nvlist_array) { nvlist_t *nvl; nvlist_t *in_array[6]; const nvlist_t * const *out_array; const nvlist_t * const *out_result; void *cookie; const char *key; const char *subkeys; int type, i; size_t nitems; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); subkeys = "123456"; for (i = 0; i < 6; i++) { in_array[i] = nvlist_create(0); ATF_REQUIRE(in_array[i] != NULL); ATF_REQUIRE_EQ(nvlist_error(in_array[i]), 0); ATF_REQUIRE(nvlist_empty(in_array[i])); cookie = NULL; nvlist_add_null(in_array[i], subkeys+i); ATF_REQUIRE_EQ(strcmp(subkeys+i, nvlist_next(in_array[i], &type, &cookie)),0); ATF_REQUIRE_EQ(nvlist_error(in_array[i]), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NULL); ATF_REQUIRE(!nvlist_empty(in_array[i])); ATF_REQUIRE(nvlist_exists(in_array[i], subkeys+i)); ATF_REQUIRE(nvlist_exists_null(in_array[i], subkeys+i)); ATF_REQUIRE_EQ(nvlist_next(in_array[i], &type, &cookie), static_cast(NULL)); } cookie = NULL; key = "name"; nvlist_add_nvlist_array(nvl, key, in_array, 6); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key)); /* Get nvlist array by cnvlist function. */ out_array = cnvlist_get_nvlist_array(cookie, &nitems); ATF_REQUIRE(out_array != NULL); ATF_REQUIRE_EQ(nitems, 6); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); /* Get nvlist array by nvlist function. */ out_result = nvlist_get_nvlist_array(nvl, key, &nitems); ATF_REQUIRE(out_result != NULL); ATF_REQUIRE_EQ(nitems, 6); /* Validate assuming that nvlist returned a proper pointer */ for (i = 0; i < 6; i++) { ATF_REQUIRE_EQ(out_result[i], out_array[i]); ATF_REQUIRE(out_array[i] != in_array[i]); /* Validate data inside nvlist. */ cookie = NULL; ATF_REQUIRE_EQ(strcmp(subkeys+i, nvlist_next(out_array[i], &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(out_array[i]), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NULL); ATF_REQUIRE(!nvlist_empty(out_array[i])); ATF_REQUIRE(nvlist_exists(out_array[i], subkeys+i)); ATF_REQUIRE(nvlist_exists_null(out_array[i], subkeys+i)); ATF_REQUIRE_EQ(nvlist_next(out_array[i], &type, &cookie), static_cast(NULL)); } nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_get_descriptor_array); ATF_TEST_CASE_BODY(cnvlist_get_descriptor_array) { nvlist_t *nvl; size_t count, i, nitems; const int *out_array; int *in_array, type; const char *key; void *cookie; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; count = 50; in_array = static_cast(malloc(sizeof(*in_array)*count)); ATF_REQUIRE(in_array != NULL); for (i = 0; i < count; i++) { in_array[i] = dup(STDERR_FILENO); ATF_REQUIRE(fd_is_valid(in_array[i])); } nvlist_add_descriptor_array(nvl, key, in_array, count); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_DESCRIPTOR_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_descriptor_array(nvl, key)); out_array = cnvlist_get_descriptor_array(cookie, &nitems); ATF_REQUIRE_EQ(nitems, count); ATF_REQUIRE(out_array != NULL); for (i = 0; i < count; i++) ATF_REQUIRE_EQ(fd_is_valid(out_array[i]), 1); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } /* ATF cnvlist_take tests. */ ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_bool); ATF_TEST_CASE_BODY(cnvlist_take_bool) { nvlist_t *nvl; const char *key; bool value; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = true; nvlist_add_bool(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_bool(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - ATF_REQUIRE_EQ(cnvlist_take_bool(nvl, cookie), value); + ATF_REQUIRE_EQ(cnvlist_take_bool(cookie), value); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_bool(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_number); ATF_TEST_CASE_BODY(cnvlist_take_number) { nvlist_t *nvl; const char *key; uint64_t value; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = 69; nvlist_add_number(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_number(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - ATF_REQUIRE_EQ(cnvlist_take_number(nvl, cookie), value); + ATF_REQUIRE_EQ(cnvlist_take_number(cookie), value); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_number(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_string); ATF_TEST_CASE_BODY(cnvlist_take_string) { nvlist_t *nvl; const char *key; const char *value; char *out_string; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = "text"; nvlist_add_string(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_STRING); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_string(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - out_string = cnvlist_take_string(nvl, cookie); + out_string = cnvlist_take_string(cookie); ATF_REQUIRE(out_string != NULL); ATF_REQUIRE_EQ(strcmp(out_string, value), 0); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_string(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); free(out_string); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_nvlist); ATF_TEST_CASE_BODY(cnvlist_take_nvlist) { nvlist_t *nvl, *value, *result; const char *key, *subkey; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); value = nvlist_create(0); ATF_REQUIRE(value != NULL); ATF_REQUIRE_EQ(nvlist_error(value), 0); ATF_REQUIRE(nvlist_empty(value)); key = "name"; subkey = "subname"; cookie = NULL; /* Add null to 'value' nvlist. */ nvlist_add_null(value, subkey); ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(value, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(value), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NULL); ATF_REQUIRE(!nvlist_empty(value)); ATF_REQUIRE(nvlist_exists(value, subkey)); ATF_REQUIRE(nvlist_exists_null(value, subkey)); ATF_REQUIRE_EQ(nvlist_next(value, &type, &cookie), static_cast(NULL)); /* Add 'value' nvlist. */ cookie = NULL; nvlist_move_nvlist(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - result = cnvlist_take_nvlist(nvl, cookie); + result = cnvlist_take_nvlist(cookie); ATF_REQUIRE(!nvlist_exists_nvlist(nvl, key)); ATF_REQUIRE(result == value); /* Validate data inside nvlist. */ cookie = NULL; ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(result, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(value), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NULL); ATF_REQUIRE(!nvlist_empty(value)); ATF_REQUIRE(nvlist_exists(value, subkey)); ATF_REQUIRE(nvlist_exists_null(value, subkey)); ATF_REQUIRE_EQ(nvlist_next(value, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); nvlist_destroy(value); } /* ATF cnvlist_take array tests */ ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_bool_array); ATF_TEST_CASE_BODY(cnvlist_take_bool_array) { nvlist_t *nvl; bool in_array[16]; const bool *out_array; const char *key; void *cookie; int type, i; size_t nitems; for (i = 0; i < 16; i++) in_array[i] = (i % 2 == 0); nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_bool_array(nvl, key, in_array, 16); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BOOL_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_bool_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - out_array = cnvlist_take_bool_array(nvl, cookie, &nitems); + out_array = cnvlist_take_bool_array(cookie, &nitems); ATF_REQUIRE_EQ(nitems, 16); ATF_REQUIRE(out_array != NULL); for (i = 0; i < 16; i++) ATF_REQUIRE_EQ(out_array[i], in_array[i]); cookie = NULL; ATF_REQUIRE(!nvlist_exists_bool_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_number_array); ATF_TEST_CASE_BODY(cnvlist_take_number_array) { nvlist_t *nvl; uint64_t in_array[16]; const uint64_t *out_array; const char *key; void *cookie; int type, i; size_t nitems; for (i = 0; i < 16; i++) in_array[i] = i; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_number_array(nvl, key, in_array, 16); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_number_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - out_array = cnvlist_take_number_array(nvl, cookie, &nitems); + out_array = cnvlist_take_number_array(cookie, &nitems); ATF_REQUIRE(out_array != NULL); ATF_REQUIRE_EQ(nitems, 16); for (i = 0; i < 16; i++) ATF_REQUIRE_EQ(out_array[i], in_array[i]); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_number_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_string_array); ATF_TEST_CASE_BODY(cnvlist_take_string_array) { nvlist_t *nvl; const char *in_array[4] = {"inequality", "sks", ".", ""}; char **out_array; const char *key; void *cookie; int type, i; size_t nitems; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_string_array(nvl, key, in_array, 4); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_STRING_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_string_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - out_array = cnvlist_take_string_array(nvl, cookie, &nitems); + out_array = cnvlist_take_string_array(cookie, &nitems); ATF_REQUIRE_EQ(nitems, 4); for (i = 0; i < 4; i++) { ATF_REQUIRE(out_array[i] != NULL); ATF_REQUIRE_EQ(strcmp(out_array[i], in_array[i]), 0); } ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_number_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); free(out_array); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_nvlist_array); ATF_TEST_CASE_BODY(cnvlist_take_nvlist_array) { nvlist_t *testnvl[8]; nvlist_t **result; nvlist_t *nvl; void *cookie; size_t num_items; unsigned int i; int type; const char *somestr[8] = { "a", "b", "c", "d", "e", "f", "g", "h" }; const char *key; for (i = 0; i < 8; i++) { testnvl[i] = nvlist_create(0); ATF_REQUIRE(testnvl[i] != NULL); ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0); ATF_REQUIRE(nvlist_empty(testnvl[i])); nvlist_add_string(testnvl[i], "nvl/string", somestr[i]); cookie = NULL; ATF_REQUIRE_EQ(strcmp("nvl/string", nvlist_next(testnvl[i], &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0); ATF_REQUIRE_EQ(type, NV_TYPE_STRING); ATF_REQUIRE(!nvlist_empty(testnvl[i])); ATF_REQUIRE(nvlist_exists(testnvl[i], "nvl/string")); ATF_REQUIRE(nvlist_exists_string(testnvl[i], "nvl/string")); ATF_REQUIRE_EQ(nvlist_next(testnvl[i], &type, &cookie), static_cast(NULL)); } nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); key = "nvl/nvlist"; cookie = NULL; nvlist_add_nvlist_array(nvl, key, (const nvlist_t * const *)testnvl, 8); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - result = cnvlist_take_nvlist_array(nvl, cookie, &num_items); + result = cnvlist_take_nvlist_array(cookie, &num_items); ATF_REQUIRE(result != NULL); ATF_REQUIRE_EQ(num_items, 8); for (i = 0; i < num_items; i++) { ATF_REQUIRE_EQ(nvlist_error(result[i]), 0); ATF_REQUIRE(nvlist_get_array_next(result[i]) == NULL); } ATF_REQUIRE(!nvlist_exists_string_array(nvl, key)); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_nvlist_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); for (i = 0; i < 8; i++) { nvlist_destroy(result[i]); nvlist_destroy(testnvl[i]); } free(result); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_take_binary); ATF_TEST_CASE_BODY(cnvlist_take_binary) { nvlist_t *nvl; const char *key; void *in_binary; const void *out_binary; void *cookie; int type; size_t in_size, out_size; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; in_size = 13; in_binary = malloc(in_size); ATF_REQUIRE(in_binary != NULL); memset(in_binary, 0xa5, in_size); nvlist_add_binary(nvl, key, in_binary, in_size); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_binary(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - out_binary = cnvlist_take_binary(nvl, cookie, &out_size); + out_binary = cnvlist_take_binary(cookie, &out_size); ATF_REQUIRE_EQ(out_size, in_size); ATF_REQUIRE_EQ(memcmp(in_binary, out_binary, out_size), 0); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_binary(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } /* ATF cnvlist_free tests. */ ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_bool); ATF_TEST_CASE_BODY(cnvlist_free_bool) { nvlist_t *nvl; const char *key; bool value; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = true; nvlist_add_bool(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_bool(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_bool(nvl, cookie); + cnvlist_free_bool(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_bool(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_number); ATF_TEST_CASE_BODY(cnvlist_free_number) { nvlist_t *nvl; const char *key; uint64_t value; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = 69; nvlist_add_number(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_number(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_number(nvl, cookie); + cnvlist_free_number(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_number(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_string); ATF_TEST_CASE_BODY(cnvlist_free_string) { nvlist_t *nvl; const char *key; const char *value; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; value = "text"; nvlist_add_string(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_STRING); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_string(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_string(nvl, cookie); + cnvlist_free_string(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_string(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_nvlist); ATF_TEST_CASE_BODY(cnvlist_free_nvlist) { nvlist_t *nvl, *value; const char *key, *subkey; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); value = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); key = "name"; subkey = "subname"; cookie = NULL; /* Add null to 'value' nvlist. */ nvlist_add_null(value, subkey); ATF_REQUIRE_EQ(strcmp(subkey, nvlist_next(value, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(value), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NULL); ATF_REQUIRE(!nvlist_empty(value)); ATF_REQUIRE(nvlist_exists(value, subkey)); ATF_REQUIRE(nvlist_exists_null(value, subkey)); ATF_REQUIRE_EQ(nvlist_next(value, &type, &cookie), static_cast(NULL)); /* Add 'value' nvlist. */ cookie = NULL; nvlist_move_nvlist(nvl, key, value); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_nvlist(nvl, cookie); + cnvlist_free_nvlist(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_nvlist(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_binary); ATF_TEST_CASE_BODY(cnvlist_free_binary) { nvlist_t *nvl; const char *key; void *in_binary; void *cookie; int type; size_t in_size; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; in_size = 13; in_binary = malloc(in_size); ATF_REQUIRE(in_binary != NULL); memset(in_binary, 0xa5, in_size); nvlist_add_binary(nvl, key, in_binary, in_size); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_binary(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_binary(nvl, cookie); + cnvlist_free_binary(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_nvlist(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } /* ATF cnvlist_free array tests. */ ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_bool_array); ATF_TEST_CASE_BODY(cnvlist_free_bool_array) { nvlist_t *nvl; bool in_array[16]; const char *key; void *cookie; int type, i; for (i = 0; i < 16; i++) in_array[i] = (i % 2 == 0); nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_bool_array(nvl, key, in_array, 16); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_BOOL_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_bool_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_bool_array(nvl, cookie); + cnvlist_free_bool_array(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_bool(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_number_array); ATF_TEST_CASE_BODY(cnvlist_free_number_array) { nvlist_t *nvl; uint64_t in_array[16]; const char *key; void *cookie; int type, i; for (i = 0; i < 16; i++) in_array[i] = i; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_number_array(nvl, key, in_array, 16); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_number_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_number_array(nvl, cookie); + cnvlist_free_number_array(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_number_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_string_array); ATF_TEST_CASE_BODY(cnvlist_free_string_array) { nvlist_t *nvl; const char *in_array[4] = {"inequality", "sucks", ".", ""}; const char *key; void *cookie; int type; nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); cookie = NULL; key = "name"; nvlist_add_string_array(nvl, key, in_array, 4); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_STRING_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_string_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_string_array(nvl, cookie); + cnvlist_free_string_array(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_string_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); nvlist_destroy(nvl); } ATF_TEST_CASE_WITHOUT_HEAD(cnvlist_free_nvlist_array); ATF_TEST_CASE_BODY(cnvlist_free_nvlist_array) { nvlist_t *testnvl[8]; nvlist_t *nvl; void *cookie; unsigned int i; int type; const char *somestr[8] = { "a", "b", "c", "d", "e", "f", "g", "h" }; const char *key; for (i = 0; i < 8; i++) { testnvl[i] = nvlist_create(0); ATF_REQUIRE(testnvl[i] != NULL); ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0); ATF_REQUIRE(nvlist_empty(testnvl[i])); nvlist_add_string(testnvl[i], "nvl/string", somestr[i]); cookie = NULL; ATF_REQUIRE_EQ(strcmp("nvl/string", nvlist_next(testnvl[i], &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(testnvl[i]), 0); ATF_REQUIRE_EQ(type, NV_TYPE_STRING); ATF_REQUIRE(!nvlist_empty(testnvl[i])); ATF_REQUIRE(nvlist_exists(testnvl[i], "nvl/string")); ATF_REQUIRE(nvlist_exists_string(testnvl[i], "nvl/string")); ATF_REQUIRE_EQ(nvlist_next(testnvl[i], &type, &cookie), static_cast(NULL)); } nvl = nvlist_create(0); ATF_REQUIRE(nvl != NULL); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); key = "nvl/nvlist"; cookie = NULL; nvlist_add_nvlist_array(nvl, key, (const nvlist_t * const *)testnvl, 8); ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST_ARRAY); ATF_REQUIRE(!nvlist_empty(nvl)); ATF_REQUIRE(nvlist_exists(nvl, key)); ATF_REQUIRE(nvlist_exists_nvlist_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); cookie = NULL; ATF_REQUIRE_EQ(strcmp(key, nvlist_next(nvl, &type, &cookie)), 0); - cnvlist_free_nvlist_array(nvl, cookie); + cnvlist_free_nvlist_array(cookie); cookie = NULL; ATF_REQUIRE_EQ(nvlist_error(nvl), 0); ATF_REQUIRE(nvlist_empty(nvl)); ATF_REQUIRE(!nvlist_exists(nvl, key)); ATF_REQUIRE(!nvlist_exists_nvlist_array(nvl, key)); ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &cookie), static_cast(NULL)); for (i = 0; i < 8; i++) nvlist_destroy(testnvl[i]); nvlist_destroy(nvl); } ATF_INIT_TEST_CASES(tp) { ATF_ADD_TEST_CASE(tp, cnvlist_get_bool); ATF_ADD_TEST_CASE(tp, cnvlist_get_bool_array); ATF_ADD_TEST_CASE(tp, cnvlist_get_number); ATF_ADD_TEST_CASE(tp, cnvlist_get_string); ATF_ADD_TEST_CASE(tp, cnvlist_get_nvlist); ATF_ADD_TEST_CASE(tp, cnvlist_get_descriptor); ATF_ADD_TEST_CASE(tp, cnvlist_get_binary); ATF_ADD_TEST_CASE(tp, cnvlist_get_number_array); ATF_ADD_TEST_CASE(tp, cnvlist_get_string_array); ATF_ADD_TEST_CASE(tp, cnvlist_get_nvlist_array); ATF_ADD_TEST_CASE(tp, cnvlist_get_descriptor_array); ATF_ADD_TEST_CASE(tp, cnvlist_take_bool); ATF_ADD_TEST_CASE(tp, cnvlist_take_number); ATF_ADD_TEST_CASE(tp, cnvlist_take_string); ATF_ADD_TEST_CASE(tp, cnvlist_take_nvlist); ATF_ADD_TEST_CASE(tp, cnvlist_take_binary); ATF_ADD_TEST_CASE(tp, cnvlist_take_bool_array); ATF_ADD_TEST_CASE(tp, cnvlist_take_number_array); ATF_ADD_TEST_CASE(tp, cnvlist_take_string_array); ATF_ADD_TEST_CASE(tp, cnvlist_take_nvlist_array); ATF_ADD_TEST_CASE(tp, cnvlist_free_bool); ATF_ADD_TEST_CASE(tp, cnvlist_free_number); ATF_ADD_TEST_CASE(tp, cnvlist_free_string); ATF_ADD_TEST_CASE(tp, cnvlist_free_nvlist); ATF_ADD_TEST_CASE(tp, cnvlist_free_binary); ATF_ADD_TEST_CASE(tp, cnvlist_free_bool_array); ATF_ADD_TEST_CASE(tp, cnvlist_free_number_array); ATF_ADD_TEST_CASE(tp, cnvlist_free_string_array); ATF_ADD_TEST_CASE(tp, cnvlist_free_nvlist_array); } Index: head/share/man/man9/cnv.9 =================================================================== --- head/share/man/man9/cnv.9 (revision 335342) +++ head/share/man/man9/cnv.9 (revision 335343) @@ -1,214 +1,214 @@ .\" .\" Copyright (c) 2016 Adam Starak .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" .Dd June 18, 2018 .Dt CNV 9 .Os .Sh NAME .Nm cnvlist_get , .Nm cnvlist_take , .Nm cnvlist_free .Nd "API for managing name/value pairs by cookie." .Sh LIBRARY .Lb libnv .Sh SYNOPSIS .In sys/cnv.h .Ft const char * .Fn cnvlist_name "const void *cookie" .Ft int .Fn cnvlist_type "const void *cookie" .\" .Ft bool .Fn cnvlist_get_bool "const void *cookie" .Ft uint64_t .Fn cnvlist_get_number "const void *cookie" .Ft "const char *" .Fn cnvlist_get_string "const void *cookie" .Ft "const nvlist_t *" .Fn cnvlist_get_nvlist "const void *cookie" .Ft "const void *" .Fn cnvlist_get_binary "const void *cookie" "size_t *sizep" .Ft "const bool *" .Fn cnvlist_get_bool_array "const void *cookie" "size_t *nitemsp" .Ft "const uint64_t *" .Fn cnvlist_get_number_array "const void *cookie" "size_t *nitemsp" .Ft "const char * const *" .Fn cnvlist_get_string_array "const void *cookie" "size_t *nitemsp" .Ft "const nvlist_t * const *" .Fn cnvlist_get_nvlist_array "const void *cookie" "size_t *nitemsp" .Ft int .Fn cnvlist_get_descriptor "const void *cookie" .Ft "const int *" .Fn cnvlist_get_descriptor_array "const void *cookie" "size_t *nitemsp" .\" .Ft bool -.Fn cnvlist_take_bool "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_take_bool "void *cookie" .Ft uint64_t -.Fn cnvlist_take_number "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_take_number "void *cookie" .Ft "const char *" -.Fn cnvlist_take_string "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_take_string "void *cookie" .Ft "const nvlist_t *" -.Fn cnvlist_take_nvlist "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_take_nvlist "void *cookie" .Ft "const void *" -.Fn cnvlist_take_binary "nvlist_t *nvl" "void *cookie" "size_t *sizep" +.Fn cnvlist_take_binary "void *cookie" "size_t *sizep" .Ft "const bool *" -.Fn cnvlist_take_bool_array "nvlist_t *nvl" "void *cookie" "size_t *nitemsp" +.Fn cnvlist_take_bool_array "void *cookie" "size_t *nitemsp" .Ft "const uint64_t *" -.Fn cnvlist_take_number_array "nvlist_t *nvl" "void *cookie" "size_t *nitemsp" +.Fn cnvlist_take_number_array "void *cookie" "size_t *nitemsp" .Ft "const char * const *" -.Fn cnvlist_take_string_array "nvlist_t *nvl" "void *cookie" "size_t *nitemsp" +.Fn cnvlist_take_string_array "void *cookie" "size_t *nitemsp" .Ft "const nvlist_t * const *" -.Fn cnvlist_take_nvlist_array "nvlist_t *nvl" "void *cookie" "size_t *nitemsp" +.Fn cnvlist_take_nvlist_array "void *cookie" "size_t *nitemsp" .Ft int -.Fn cnvlist_take_descriptor "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_take_descriptor "void *cookie" .Ft "const int *" -.Fn cnvlist_take_descriptor_array "nvlist_t *nvl" "void *cookie" "size_t *nitemsp" +.Fn cnvlist_take_descriptor_array "void *cookie" "size_t *nitemsp" .\" .Ft void -.Fn cnvlist_free_null "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_null "void *cookie" .Ft void -.Fn cnvlist_free_bool "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_bool "void *cookie" .Ft void -.Fn cnvlist_free_number "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_number "void *cookie" .Ft void -.Fn cnvlist_free_string "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_string "void *cookie" .Ft void -.Fn cnvlist_free_nvlist "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_nvlist "void *cookie" .Ft void -.Fn cnvlist_free_descriptor "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_descriptor "void *cookie" .Ft void -.Fn cnvlist_free_binary "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_binary "void *cookie" .Ft void -.Fn cnvlist_free_bool_array "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_bool_array "void *cookie" .Ft void -.Fn cnvlist_free_number_array "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_number_array "void *cookie" .Ft void -.Fn cnvlist_free_string_array "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_string_array "void *cookie" .Ft void -.Fn cnvlist_free_nvlist_array "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_nvlist_array "void *cookie" .Ft void -.Fn cnvlist_free_descriptor_array "nvlist_t *nvl" "void *cookie" +.Fn cnvlist_free_descriptor_array "void *cookie" .Sh DESCRIPTION The .Nm libnv library permits easy management of name/value pairs and can send and receive them over sockets. For more information, also see .Xr nv 9 . .Pp The concept of cookies is explained in .Fn nvlist_next , .Fn nvlist_get_parent , and .Fn nvlist_get_pararr from .Xr nv 9 . .Pp The .Fn cnvlist_name function returns the name of an element associated with the given cookie. .Pp The .Fn cnvlist_type function returns the type of an element associated with the given cookie. Types which can be returned are described in .Xr nv 9 . .Pp The .Nm cnvlist_get family of functions obtains the value associated with the given cookie. Returned strings, nvlists, descriptors, binaries, or arrays must not be modified by the user, since they still belong to the nvlist. The nvlist must not be in an error state. .Pp The .Nm cnvlist_take family of functions returns the value associated with the given cookie and removes the element from the nvlist. When the value is a string, binary, or array value, the caller is responsible for freeing the returned memory with .Fn free 3 . When the value is an nvlist, the caller is responsible for destroying the returned nvlist with .Fn nvlist_destroy . When the value is a descriptor, the caller is responsible for closing the returned descriptor with the .Fn close 2 . .Pp The .Nm cnvlist_free family of functions removes an element of the supplied cookie and frees all resources. If an element of the given cookie has the wrong type or does not exist, the program is aborted. .Sh EXAMPLE The following example demonstrates how to deal with cnvlist API. .Bd -literal int type; void *cookie, *scookie, *bcookie; nvlist_t *nvl; char *name; nvl = nvlist_create(0); nvlist_add_bool(nvl, "test", 1 == 2); nvlist_add_string(nvl, "test2", "cnvlist"); cookie = NULL; while (nvlist_next(nvl, &type, &cookie) != NULL) { switch (type) { case NV_TYPE_BOOL: printf("test: %d\\n", cnvlist_get_bool(cookie)); bcookie = cookie; break; case NV_TYPE_STRING: printf("test2: %s\\n", cnvlist_get_string(cookie)); scookie = cookie; break; } } -name = cnvlist_take_string(nvl, scookie); -cnvlist_free_bool(nvl, bcookie); +name = cnvlist_take_string(scookie); +cnvlist_free_bool(bcookie); printf("test2: %s\\n", name); free(name); printf("nvlist_empty = %d\\n", nvlist_empty(nvl)); nvlist_destroy(nvl); return (0); .Ed .Sh SEE ALSO .Xr close 2 , .Xr free 3 , .Xr nv 9 .Sh AUTHORS The .Nm cnv API was created during the Google Summer Of Code 2016 by .An Adam Starak . Index: head/sys/contrib/libnv/cnvlist.c =================================================================== --- head/sys/contrib/libnv/cnvlist.c (revision 335342) +++ head/sys/contrib/libnv/cnvlist.c (revision 335343) @@ -1,213 +1,219 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2016 Adam Starak * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #include __FBSDID("$FreeBSD$"); #ifdef _KERNEL #include #include #include #include #include #include #else #include #include #include #include #endif #include #include #include "nv_impl.h" #include "nvlist_impl.h" #include "nvpair_impl.h" const char * cnvlist_name(const void *cookie) { return (nvpair_name(cookie)); } int cnvlist_type(const void *cookie) { return (nvpair_type(cookie)); } #define CNVLIST_GET(ftype, type, NVTYPE) \ ftype \ cnvlist_get_##type(const void *cookie) \ { \ \ if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \ nvlist_report_missing(NV_TYPE_##NVTYPE, \ nvpair_name(cookie)); \ } \ return (nvpair_get_##type(cookie)); \ } CNVLIST_GET(bool, bool, BOOL) CNVLIST_GET(uint64_t, number, NUMBER) CNVLIST_GET(const char *, string, STRING) CNVLIST_GET(const nvlist_t *, nvlist, NVLIST) #ifndef _KERNEL CNVLIST_GET(int, descriptor, DESCRIPTOR) #endif #undef CNVLIST_GET #define CNVLIST_GET_ARRAY(ftype, type, NVTYPE) \ ftype \ cnvlist_get_##type(const void *cookie, size_t *nitemsp) \ { \ \ if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \ nvlist_report_missing(NV_TYPE_##NVTYPE, \ nvpair_name(cookie)); \ } \ return (nvpair_get_##type(cookie, nitemsp)); \ } CNVLIST_GET_ARRAY(const bool *, bool_array, BOOL_ARRAY) CNVLIST_GET_ARRAY(const uint64_t *, number_array, NUMBER_ARRAY) CNVLIST_GET_ARRAY(const char * const *, string_array, STRING_ARRAY) CNVLIST_GET_ARRAY(const nvlist_t * const *, nvlist_array, NVLIST_ARRAY) #ifndef _KERNEL CNVLIST_GET_ARRAY(const int *, descriptor_array, DESCRIPTOR_ARRAY) #endif #undef CNVLIST_GET_ARRAY const void * cnvlist_get_binary(const void *cookie, size_t *sizep) { if (nvpair_type(cookie) != NV_TYPE_BINARY) nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie)); return (nvpair_get_binary(cookie, sizep)); } #define CNVLIST_TAKE(ftype, type, NVTYPE) \ ftype \ -cnvlist_take_##type(nvlist_t *nvl, void *cookie) \ +cnvlist_take_##type(void *cookie) \ { \ ftype value; \ + nvlist_t *nvl; \ \ if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \ nvlist_report_missing(NV_TYPE_##NVTYPE, \ nvpair_name(cookie)); \ } \ + nvl = nvpair_nvlist(cookie); \ value = (ftype)(intptr_t)nvpair_get_##type(cookie); \ nvlist_remove_nvpair(nvl, cookie); \ nvpair_free_structure(cookie); \ return (value); \ } CNVLIST_TAKE(bool, bool, BOOL) CNVLIST_TAKE(uint64_t, number, NUMBER) CNVLIST_TAKE(char *, string, STRING) CNVLIST_TAKE(nvlist_t *, nvlist, NVLIST) #ifndef _KERNEL CNVLIST_TAKE(int, descriptor, DESCRIPTOR) #endif #undef CNVLIST_TAKE #define CNVLIST_TAKE_ARRAY(ftype, type, NVTYPE) \ ftype \ -cnvlist_take_##type(nvlist_t *nvl, void *cookie, size_t *nitemsp) \ +cnvlist_take_##type(void *cookie, size_t *nitemsp) \ { \ ftype value; \ + nvlist_t *nvl; \ \ if (nvpair_type(cookie) != NV_TYPE_##NVTYPE) { \ nvlist_report_missing(NV_TYPE_##NVTYPE, \ nvpair_name(cookie)); \ } \ + nvl = nvpair_nvlist(cookie); \ value = (ftype)(intptr_t)nvpair_get_##type(cookie, nitemsp); \ nvlist_remove_nvpair(nvl, cookie); \ nvpair_free_structure(cookie); \ return (value); \ } CNVLIST_TAKE_ARRAY(bool *, bool_array, BOOL_ARRAY) CNVLIST_TAKE_ARRAY(uint64_t *, number_array, NUMBER_ARRAY) CNVLIST_TAKE_ARRAY(char **, string_array, STRING_ARRAY) CNVLIST_TAKE_ARRAY(nvlist_t **, nvlist_array, NVLIST_ARRAY) #ifndef _KERNEL CNVLIST_TAKE_ARRAY(int *, descriptor_array, DESCRIPTOR_ARRAY); #endif #undef CNVLIST_TAKE_ARRAY void * -cnvlist_take_binary(nvlist_t *nvl, void *cookie, size_t *sizep) +cnvlist_take_binary(void *cookie, size_t *sizep) { void *value; + nvlist_t *nvl; if (nvpair_type(cookie) != NV_TYPE_BINARY) nvlist_report_missing(NV_TYPE_BINARY, nvpair_name(cookie)); + nvl = nvpair_nvlist(cookie); value = (void *)(intptr_t)nvpair_get_binary(cookie, sizep); nvlist_remove_nvpair(nvl, cookie); nvpair_free_structure(cookie); return (value); } #define CNVLIST_FREE(type) \ void \ -cnvlist_free_##type(nvlist_t *nvl, void *cookie) \ +cnvlist_free_##type(void *cookie) \ { \ \ - nvlist_free_nvpair(nvl, cookie); \ + nvlist_free_nvpair(nvpair_nvlist(cookie), cookie); \ } CNVLIST_FREE(bool) CNVLIST_FREE(number) CNVLIST_FREE(string) CNVLIST_FREE(nvlist) CNVLIST_FREE(binary); CNVLIST_FREE(bool_array) CNVLIST_FREE(number_array) CNVLIST_FREE(string_array) CNVLIST_FREE(nvlist_array) #ifndef _KERNEL CNVLIST_FREE(descriptor) CNVLIST_FREE(descriptor_array) #endif #undef CNVLIST_FREE Index: head/sys/sys/cnv.h =================================================================== --- head/sys/sys/cnv.h (revision 335342) +++ head/sys/sys/cnv.h (revision 335343) @@ -1,120 +1,120 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2016 Adam Starak * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _CNV_H_ #define _CNV_H_ #include #ifndef _KERNEL #include #include #include #include #endif #ifndef _NVLIST_T_DECLARED #define _NVLIST_T_DECLARED struct nvlist; typedef struct nvlist nvlist_t; #endif __BEGIN_DECLS /* * Functions which returns information about the given cookie. */ const char *cnvlist_name(const void *cookie); int cnvlist_type(const void *cookie); /* * The cnvlist_get functions returns value associated with the given cookie. * If it returns a pointer, the pointer represents internal buffer and should * not be freed by the caller. */ bool cnvlist_get_bool(const void *cookie); uint64_t cnvlist_get_number(const void *cookie); const char *cnvlist_get_string(const void *cookie); const nvlist_t *cnvlist_get_nvlist(const void *cookie); const void *cnvlist_get_binary(const void *cookie, size_t *sizep); const bool *cnvlist_get_bool_array(const void *cookie, size_t *nitemsp); const uint64_t *cnvlist_get_number_array(const void *cookie, size_t *nitemsp); const char * const *cnvlist_get_string_array(const void *cookie, size_t *nitemsp); const nvlist_t * const *cnvlist_get_nvlist_array(const void *cookie, size_t *nitemsp); #ifndef _KERNEL int cnvlist_get_descriptor(const void *cookie); const int *cnvlist_get_descriptor_array(const void *cookie, size_t *nitemsp); #endif /* * The cnvlist_take functions returns value associated with the given cookie and * remove the given entry from the nvlist. * The caller is responsible for freeing received data. */ -bool cnvlist_take_bool(nvlist_t *nvl, void *cookie); -uint64_t cnvlist_take_number(nvlist_t *nvl, void *cookie); -char *cnvlist_take_string(nvlist_t *nvl, void *cookie); -nvlist_t *cnvlist_take_nvlist(nvlist_t *nvl, void *cookie); -void *cnvlist_take_binary(nvlist_t *nvl, void *cookie, size_t *sizep); -bool *cnvlist_take_bool_array(nvlist_t *nvl, void *cookie, size_t *nitemsp); -uint64_t *cnvlist_take_number_array(nvlist_t *nvl, void *cookie, size_t *nitemsp); -char **cnvlist_take_string_array(nvlist_t *nvl, void *cookie, size_t *nitemsp); -nvlist_t **cnvlist_take_nvlist_array(nvlist_t *nvl, void *cookie, size_t *nitemsp); +bool cnvlist_take_bool(void *cookie); +uint64_t cnvlist_take_number(void *cookie); +char *cnvlist_take_string(void *cookie); +nvlist_t *cnvlist_take_nvlist(void *cookie); +void *cnvlist_take_binary(void *cookie, size_t *sizep); +bool *cnvlist_take_bool_array(void *cookie, size_t *nitemsp); +uint64_t *cnvlist_take_number_array(void *cookie, size_t *nitemsp); +char **cnvlist_take_string_array(void *cookie, size_t *nitemsp); +nvlist_t **cnvlist_take_nvlist_array(void *cookie, size_t *nitemsp); #ifndef _KERNEL -int cnvlist_take_descriptor(nvlist_t *nvl, void *cookie); -int *cnvlist_take_descriptor_array(nvlist_t *nvl, void *cookie, size_t *nitemsp); +int cnvlist_take_descriptor(void *cookie); +int *cnvlist_take_descriptor_array(void *cookie, size_t *nitemsp); #endif /* * The cnvlist_free functions removes the given name/value pair from the nvlist based on cookie * and frees memory associated with it. */ -void cnvlist_free_bool(nvlist_t *nvl, void *cookie); -void cnvlist_free_number(nvlist_t *nvl, void *cookie); -void cnvlist_free_string(nvlist_t *nvl, void *cookie); -void cnvlist_free_nvlist(nvlist_t *nvl, void *cookie); -void cnvlist_free_binary(nvlist_t *nvl, void *cookie); -void cnvlist_free_bool_array(nvlist_t *nvl, void *cookie); -void cnvlist_free_number_array(nvlist_t *nvl, void *cookie); -void cnvlist_free_string_array(nvlist_t *nvl, void *cookie); -void cnvlist_free_nvlist_array(nvlist_t *nvl, void *cookie); +void cnvlist_free_bool(void *cookie); +void cnvlist_free_number(void *cookie); +void cnvlist_free_string(void *cookie); +void cnvlist_free_nvlist(void *cookie); +void cnvlist_free_binary(void *cookie); +void cnvlist_free_bool_array(void *cookie); +void cnvlist_free_number_array(void *cookie); +void cnvlist_free_string_array(void *cookie); +void cnvlist_free_nvlist_array(void *cookie); #ifndef _KERNEL -void cnvlist_free_descriptor(nvlist_t *nvl, void *cookie); -void cnvlist_free_descriptor_array(nvlist_t *nvl, void *cookie); +void cnvlist_free_descriptor(void *cookie); +void cnvlist_free_descriptor_array(void *cookie); #endif __END_DECLS #endif /* !_CNV_H_ */