Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 1 Mar 2015 00:20:57 +0000 (UTC)
From:      Ryan Stone <rstone@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r279424 - head/lib/libnv/tests
Message-ID:  <201503010020.t210Kv8m079953@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <bsd.test.mk>

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 <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <atf-c++.hpp>
+#include <nv.h>
+
+#include <errno.h>
+/*
+ * 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 <atf-c++.hpp>
+#include <nv.h>
+
+#include <errno.h>
+/*
+ * 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 <atf-c++.hpp>
+#include <nv.h>
+
+#include <errno.h>
+/*
+ * 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 <atf-c++.hpp>
+#include <nv.h>
+
+#include <errno.h>
+#include <limits>
+#include <set>
+#include <sstream>
+#include <string>
+
+/*
+ * 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 ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201503010020.t210Kv8m079953>