From owner-svn-src-head@FreeBSD.ORG Sun Mar 1 00:20:58 2015 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id C4382DA5; Sun, 1 Mar 2015 00:20:58 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id AE4CBDE1; Sun, 1 Mar 2015 00:20:58 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.9/8.14.9) with ESMTP id t210KwNI079955; Sun, 1 Mar 2015 00:20:58 GMT (envelope-from rstone@FreeBSD.org) Received: (from rstone@localhost) by svn.freebsd.org (8.14.9/8.14.9/Submit) id t210Kv8m079953; Sun, 1 Mar 2015 00:20:57 GMT (envelope-from rstone@FreeBSD.org) Message-Id: <201503010020.t210Kv8m079953@svn.freebsd.org> X-Authentication-Warning: svn.freebsd.org: rstone set sender to rstone@FreeBSD.org using -f From: Ryan Stone Date: Sun, 1 Mar 2015 00:20:57 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r279424 - head/lib/libnv/tests X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 01 Mar 2015 00:20:59 -0000 Author: rstone Date: Sun Mar 1 00:20:57 2015 New Revision: 279424 URL: https://svnweb.freebsd.org/changeset/base/279424 Log: Tests of basic nvlist add functions Differential Revision: https://reviews.freebsd.org/D1869 Reviewed by: jfv, pjd MFC after: 1 month Sponsored by: Sandvine Inc. Added: head/lib/libnv/tests/nv_tests.cc (contents, props changed) Modified: head/lib/libnv/tests/Makefile Modified: head/lib/libnv/tests/Makefile ============================================================================== --- head/lib/libnv/tests/Makefile Sun Mar 1 00:05:45 2015 (r279423) +++ head/lib/libnv/tests/Makefile Sun Mar 1 00:20:57 2015 (r279424) @@ -2,6 +2,8 @@ TESTSDIR= ${TESTSBASE}/lib/libnv +ATF_TESTS_CXX= nv_tests + TAP_TESTS_C+= nvlist_add_test TAP_TESTS_C+= nvlist_exists_test TAP_TESTS_C+= nvlist_free_test @@ -11,6 +13,6 @@ TAP_TESTS_C+= nvlist_send_recv_test LIBADD+= nv -WARNS?= 6 +WARNS?= 3 .include Added: head/lib/libnv/tests/nv_tests.cc ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/lib/libnv/tests/nv_tests.cc Sun Mar 1 00:20:57 2015 (r279424) @@ -0,0 +1,2076 @@ +/*- + * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved. + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include +/* + * Test that a newly created nvlist has no errors, and is empty. + */ +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty); +ATF_TEST_CASE_BODY(nvlist_create__is_empty) +{ + nvlist_t *nvl; + int type; + void *it; + + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + + ATF_REQUIRE_EQ(nvlist_error(nvl), 0); + ATF_REQUIRE(nvlist_empty(nvl)); + + it = NULL; + ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_null__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + int type; + + key = "key"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_null(nvl, key); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_null(nvl, key)); + ATF_REQUIRE(nvlist_existsf_null(nvl, "key")); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NULL); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + int type; + + key = "name"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_bool(nvl, key, true); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me")); + ATF_REQUIRE(nvlist_exists_bool(nvl, key)); + ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e')); + ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true); + ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_number__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + uint64_t value; + int type; + + key = "foo123"; + value = 71965; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_number(nvl, key, value); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123)); + ATF_REQUIRE(nvlist_exists_number(nvl, key)); + ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key)); + ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value); + ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_string__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + const char *value; + int type; + + key = "test"; + value = "fgjdkgjdk"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_string(nvl, key, value); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_string(nvl, key)); + ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key)); + ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0); + ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(nvlist_get_string(nvl, key) != value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_STRING); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key, *subkey; + nvlist_t *sublist; + const nvlist_t *value; + int type; + + key = "test"; + subkey = "subkey"; + sublist = nvlist_create(0); + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_null(sublist, subkey); + nvlist_add_nvlist(nvl, key, sublist); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); + ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key)); + + value = nvlist_get_nvlist(nvl, key); + ATF_REQUIRE(nvlist_exists_null(value, subkey)); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(sublist != value); + + value = nvlist_getf_nvlist(nvl, "%s", key); + ATF_REQUIRE(nvlist_exists_null(value, subkey)); + ATF_REQUIRE(sublist != value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(sublist); + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + void *value; + const void *ret_value; + size_t value_size, ret_size; + int type; + + key = "binary"; + value_size = 13; + value = malloc(value_size); + memset(value, 0xa5, value_size); + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_binary(nvl, key, value, value_size); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_binary(nvl, key)); + ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key)); + + ret_value = nvlist_get_binary(nvl, key, &ret_size); + ATF_REQUIRE_EQ(value_size, ret_size); + ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(value != ret_value); + + ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key); + ATF_REQUIRE_EQ(value_size, ret_size); + ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); + ATF_REQUIRE(value != ret_value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); + free(value); +} + +ATF_INIT_TEST_CASES(tp) +{ + ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty); + ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert); +} +/*- + * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved. + * 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. + */ + +#include +#include + +#include +/* + * Test that a newly created nvlist has no errors, and is empty. + */ +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty); +ATF_TEST_CASE_BODY(nvlist_create__is_empty) +{ + nvlist_t *nvl; + int type; + void *it; + + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + + ATF_REQUIRE_EQ(nvlist_error(nvl), 0); + ATF_REQUIRE(nvlist_empty(nvl)); + + it = NULL; + ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_null__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + int type; + + key = "key"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_null(nvl, key); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_null(nvl, key)); + ATF_REQUIRE(nvlist_existsf_null(nvl, "key")); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NULL); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + int type; + + key = "name"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_bool(nvl, key, true); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me")); + ATF_REQUIRE(nvlist_exists_bool(nvl, key)); + ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e')); + ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true); + ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_number__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + uint64_t value; + int type; + + key = "foo123"; + value = 71965; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_number(nvl, key, value); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123)); + ATF_REQUIRE(nvlist_exists_number(nvl, key)); + ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key)); + ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value); + ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_string__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + const char *value; + int type; + + key = "test"; + value = "fgjdkgjdk"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_string(nvl, key, value); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_string(nvl, key)); + ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key)); + ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0); + ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(nvlist_get_string(nvl, key) != value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_STRING); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key, *subkey; + nvlist_t *sublist; + const nvlist_t *value; + int type; + + key = "test"; + subkey = "subkey"; + sublist = nvlist_create(0); + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_null(sublist, subkey); + nvlist_add_nvlist(nvl, key, sublist); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); + ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key)); + + value = nvlist_get_nvlist(nvl, key); + ATF_REQUIRE(nvlist_exists_null(value, subkey)); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(sublist != value); + + value = nvlist_getf_nvlist(nvl, "%s", key); + ATF_REQUIRE(nvlist_exists_null(value, subkey)); + ATF_REQUIRE(sublist != value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(sublist); + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + void *value; + const void *ret_value; + size_t value_size, ret_size; + int type; + + key = "binary"; + value_size = 13; + value = malloc(value_size); + memset(value, 0xa5, value_size); + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_binary(nvl, key, value, value_size); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_binary(nvl, key)); + ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key)); + + ret_value = nvlist_get_binary(nvl, key, &ret_size); + ATF_REQUIRE_EQ(value_size, ret_size); + ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(value != ret_value); + + ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key); + ATF_REQUIRE_EQ(value_size, ret_size); + ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); + ATF_REQUIRE(value != ret_value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); + free(value); +} + +ATF_INIT_TEST_CASES(tp) +{ + ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty); + ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert); +} +/*- + * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved. + * 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. + */ + +#include +#include + +#include +/* + * Test that a newly created nvlist has no errors, and is empty. + */ +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty); +ATF_TEST_CASE_BODY(nvlist_create__is_empty) +{ + nvlist_t *nvl; + int type; + void *it; + + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + + ATF_REQUIRE_EQ(nvlist_error(nvl), 0); + ATF_REQUIRE(nvlist_empty(nvl)); + + it = NULL; + ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_null__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + int type; + + key = "key"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_null(nvl, key); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_null(nvl, key)); + ATF_REQUIRE(nvlist_existsf_null(nvl, "key")); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NULL); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + int type; + + key = "name"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_bool(nvl, key, true); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me")); + ATF_REQUIRE(nvlist_exists_bool(nvl, key)); + ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e')); + ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true); + ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_BOOL); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_number__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + uint64_t value; + int type; + + key = "foo123"; + value = 71965; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_number(nvl, key, value); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123)); + ATF_REQUIRE(nvlist_exists_number(nvl, key)); + ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key)); + ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value); + ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_string__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + const char *value; + int type; + + key = "test"; + value = "fgjdkgjdk"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_string(nvl, key, value); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_string(nvl, key)); + ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key)); + ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0); + ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(nvlist_get_string(nvl, key) != value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_STRING); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key, *subkey; + nvlist_t *sublist; + const nvlist_t *value; + int type; + + key = "test"; + subkey = "subkey"; + sublist = nvlist_create(0); + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_null(sublist, subkey); + nvlist_add_nvlist(nvl, key, sublist); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_nvlist(nvl, key)); + ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key)); + + value = nvlist_get_nvlist(nvl, key); + ATF_REQUIRE(nvlist_exists_null(value, subkey)); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(sublist != value); + + value = nvlist_getf_nvlist(nvl, "%s", key); + ATF_REQUIRE(nvlist_exists_null(value, subkey)); + ATF_REQUIRE(sublist != value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(sublist); + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + void *value; + const void *ret_value; + size_t value_size, ret_size; + int type; + + key = "binary"; + value_size = 13; + value = malloc(value_size); + memset(value, 0xa5, value_size); + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_binary(nvl, key, value, value_size); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_binary(nvl, key)); + ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key)); + + ret_value = nvlist_get_binary(nvl, key, &ret_size); + ATF_REQUIRE_EQ(value_size, ret_size); + ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); + + /* nvlist_add_* is required to clone the value, so check for that. */ + ATF_REQUIRE(value != ret_value); + + ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key); + ATF_REQUIRE_EQ(value_size, ret_size); + ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0); + ATF_REQUIRE(value != ret_value); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_BINARY); + ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL); + + nvlist_destroy(nvl); + free(value); +} + +ATF_INIT_TEST_CASES(tp) +{ + ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty); + ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert); + ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert); +} +/*- + * Copyright (c) 2014-2015 Sandvine Inc. All rights reserved. + * 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. + */ + +#include +#include + +#include +#include +#include +#include +#include + +/* + * Test that a newly created nvlist has no errors, and is empty. + */ +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty); +ATF_TEST_CASE_BODY(nvlist_create__is_empty) +{ + nvlist_t *nvl; + int type; + void *it; + + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + + ATF_REQUIRE_EQ(nvlist_error(nvl), 0); + ATF_REQUIRE(nvlist_empty(nvl)); + + it = NULL; + ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL); + + nvlist_destroy(nvl); +} + +ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert); +ATF_TEST_CASE_BODY(nvlist_add_null__single_insert) +{ + nvlist_t *nvl; + void *it; + const char *key; + int type; + + key = "key"; + nvl = nvlist_create(0); + + ATF_REQUIRE(nvl != NULL); + ATF_REQUIRE(!nvlist_exists(nvl, key)); + + nvlist_add_null(nvl, key); + + ATF_REQUIRE(!nvlist_empty(nvl)); + ATF_REQUIRE(nvlist_exists(nvl, key)); + ATF_REQUIRE(nvlist_existsf(nvl, "%s", key)); + ATF_REQUIRE(nvlist_exists_null(nvl, key)); + ATF_REQUIRE(nvlist_existsf_null(nvl, "key")); + + /* Iterate over the nvlist; ensure that it has only our one key. */ + it = NULL; + ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0); + ATF_REQUIRE_EQ(type, NV_TYPE_NULL); *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***