nv_tests.cc revision 279425
1/*-
2 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libnv/tests/nv_tests.cc 279425 2015-03-01 00:21:24Z rstone $");
29
30#include <atf-c++.hpp>
31#include <nv.h>
32
33#include <errno.h>
34/*
35 * Test that a newly created nvlist has no errors, and is empty.
36 */
37ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
38ATF_TEST_CASE_BODY(nvlist_create__is_empty)
39{
40	nvlist_t *nvl;
41	int type;
42	void *it;
43
44	nvl = nvlist_create(0);
45
46	ATF_REQUIRE(nvl != NULL);
47
48	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
49	ATF_REQUIRE(nvlist_empty(nvl));
50
51	it = NULL;
52	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
53
54	nvlist_destroy(nvl);
55}
56
57ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
58ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
59{
60	nvlist_t *nvl;
61	void *it;
62	const char *key;
63	int type;
64
65	key = "key";
66	nvl = nvlist_create(0);
67
68	ATF_REQUIRE(nvl != NULL);
69	ATF_REQUIRE(!nvlist_exists(nvl, key));
70
71	nvlist_add_null(nvl, key);
72
73	ATF_REQUIRE(!nvlist_empty(nvl));
74	ATF_REQUIRE(nvlist_exists(nvl, key));
75	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
76	ATF_REQUIRE(nvlist_exists_null(nvl, key));
77	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
78
79	/* Iterate over the nvlist; ensure that it has only our one key. */
80	it = NULL;
81	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
82	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
83	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
84
85	nvlist_destroy(nvl);
86}
87
88ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
89ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
90{
91	nvlist_t *nvl;
92	void *it;
93	const char *key;
94	int type;
95
96	key = "name";
97	nvl = nvlist_create(0);
98
99	ATF_REQUIRE(nvl != NULL);
100	ATF_REQUIRE(!nvlist_exists(nvl, key));
101
102	nvlist_add_bool(nvl, key, true);
103
104	ATF_REQUIRE(!nvlist_empty(nvl));
105	ATF_REQUIRE(nvlist_exists(nvl, key));
106	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
107	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
108	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
109	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
110	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
111
112	/* Iterate over the nvlist; ensure that it has only our one key. */
113	it = NULL;
114	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
115	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
116	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
117
118	nvlist_destroy(nvl);
119}
120
121ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
122ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
123{
124	nvlist_t *nvl;
125	void *it;
126	const char *key;
127	uint64_t value;
128	int type;
129
130	key = "foo123";
131	value = 71965;
132	nvl = nvlist_create(0);
133
134	ATF_REQUIRE(nvl != NULL);
135	ATF_REQUIRE(!nvlist_exists(nvl, key));
136
137	nvlist_add_number(nvl, key, value);
138
139	ATF_REQUIRE(!nvlist_empty(nvl));
140	ATF_REQUIRE(nvlist_exists(nvl, key));
141	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
142	ATF_REQUIRE(nvlist_exists_number(nvl, key));
143	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
144	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
145	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
146
147	/* Iterate over the nvlist; ensure that it has only our one key. */
148	it = NULL;
149	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
150	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
151	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
152
153	nvlist_destroy(nvl);
154}
155
156ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
157ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
158{
159	nvlist_t *nvl;
160	void *it;
161	const char *key;
162	const char *value;
163	int type;
164
165	key = "test";
166	value = "fgjdkgjdk";
167	nvl = nvlist_create(0);
168
169	ATF_REQUIRE(nvl != NULL);
170	ATF_REQUIRE(!nvlist_exists(nvl, key));
171
172	nvlist_add_string(nvl, key, value);
173
174	ATF_REQUIRE(!nvlist_empty(nvl));
175	ATF_REQUIRE(nvlist_exists(nvl, key));
176	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
177	ATF_REQUIRE(nvlist_exists_string(nvl, key));
178	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
179	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
180	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
181
182	/* nvlist_add_* is required to clone the value, so check for that. */
183	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
184
185	/* Iterate over the nvlist; ensure that it has only our one key. */
186	it = NULL;
187	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
188	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
189	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
190
191	nvlist_destroy(nvl);
192}
193
194ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
195ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
196{
197	nvlist_t *nvl;
198	void *it;
199	const char *key, *subkey;
200	nvlist_t *sublist;
201	const nvlist_t *value;
202	int type;
203
204	key = "test";
205	subkey = "subkey";
206	sublist = nvlist_create(0);
207	nvl = nvlist_create(0);
208
209	ATF_REQUIRE(nvl != NULL);
210	ATF_REQUIRE(!nvlist_exists(nvl, key));
211
212	nvlist_add_null(sublist, subkey);
213	nvlist_add_nvlist(nvl, key, sublist);
214
215	ATF_REQUIRE(!nvlist_empty(nvl));
216	ATF_REQUIRE(nvlist_exists(nvl, key));
217	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
218	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
219	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
220
221	value = nvlist_get_nvlist(nvl, key);
222	ATF_REQUIRE(nvlist_exists_null(value, subkey));
223
224	/* nvlist_add_* is required to clone the value, so check for that. */
225	ATF_REQUIRE(sublist != value);
226
227	value = nvlist_getf_nvlist(nvl, "%s", key);
228	ATF_REQUIRE(nvlist_exists_null(value, subkey));
229	ATF_REQUIRE(sublist != value);
230
231	/* Iterate over the nvlist; ensure that it has only our one key. */
232	it = NULL;
233	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
234	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
235	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
236
237	nvlist_destroy(sublist);
238	nvlist_destroy(nvl);
239}
240
241ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
242ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
243{
244	nvlist_t *nvl;
245	void *it;
246	const char *key;
247	void *value;
248	const void *ret_value;
249	size_t value_size, ret_size;
250	int type;
251
252	key = "binary";
253	value_size = 13;
254	value = malloc(value_size);
255	memset(value, 0xa5, value_size);
256	nvl = nvlist_create(0);
257
258	ATF_REQUIRE(nvl != NULL);
259	ATF_REQUIRE(!nvlist_exists(nvl, key));
260
261	nvlist_add_binary(nvl, key, value, value_size);
262
263	ATF_REQUIRE(!nvlist_empty(nvl));
264	ATF_REQUIRE(nvlist_exists(nvl, key));
265	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
266	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
267	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
268
269	ret_value = nvlist_get_binary(nvl, key, &ret_size);
270	ATF_REQUIRE_EQ(value_size, ret_size);
271	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
272
273	/* nvlist_add_* is required to clone the value, so check for that. */
274	ATF_REQUIRE(value != ret_value);
275
276	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
277	ATF_REQUIRE_EQ(value_size, ret_size);
278	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
279	ATF_REQUIRE(value != ret_value);
280
281	/* Iterate over the nvlist; ensure that it has only our one key. */
282	it = NULL;
283	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
284	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
285	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
286
287	nvlist_destroy(nvl);
288	free(value);
289}
290
291ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
292ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
293{
294	nvlist_t *nvl, *clone;
295
296	nvl = nvlist_create(0);
297	ATF_REQUIRE(nvl != NULL);
298
299	clone = nvlist_clone(nvl);
300	ATF_REQUIRE(clone != NULL);
301	ATF_REQUIRE(clone != nvl);
302	ATF_REQUIRE(nvlist_empty(clone));
303
304	nvlist_destroy(clone);
305	nvlist_destroy(nvl);
306}
307
308ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
309ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
310{
311	nvlist_t *nvl, *clone;
312	const char *key;
313	void *it;
314	uint64_t value;
315	int type;
316
317	nvl = nvlist_create(0);
318	ATF_REQUIRE(nvl != NULL);
319
320	key = "testkey";
321	value = 684874;
322	nvlist_add_number(nvl, key, value);
323
324	clone = nvlist_clone(nvl);
325	ATF_REQUIRE(clone != NULL);
326	ATF_REQUIRE(clone != nvl);
327	ATF_REQUIRE(nvlist_exists_number(clone, key));
328	ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value);
329
330	/* Iterate over the nvlist; ensure that it has only our one key. */
331	it = NULL;
332	ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0);
333	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
334	ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), NULL);
335
336	nvlist_destroy(clone);
337	nvlist_destroy(nvl);
338}
339
340static const char * const test_subnvlist_key = "nvlist";
341
342static const char * const test_string_key = "string";
343static const char * const test_string_val = "59525";
344
345static nvlist_t*
346create_test_nvlist(void)
347{
348	nvlist_t *nvl, *sublist;
349
350	nvl = nvlist_create(0);
351	ATF_REQUIRE(nvl != NULL);
352
353	sublist = nvlist_create(0);
354	ATF_REQUIRE(sublist != NULL);
355
356	nvlist_add_string(sublist, test_string_key, test_string_val);
357	nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
358
359	return (nvl);
360}
361
362static void
363verify_test_nvlist(const nvlist_t *nvl)
364{
365	void *it;
366	const nvlist_t *value;
367	int type;
368
369	ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
370
371	value = nvlist_get_nvlist(nvl, test_subnvlist_key);
372
373	ATF_REQUIRE(nvlist_exists_string(value, test_string_key));
374	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0);
375	ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val);
376
377	/* Iterate over both nvlists; ensure that each has only the one key. */
378	it = NULL;
379	ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it),
380	    test_string_key), 0);
381	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
382	ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), NULL);
383
384	it = NULL;
385	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it),
386	    test_subnvlist_key), 0);
387	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
388	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
389}
390
391ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
392ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
393{
394	nvlist_t *nvl, *clone;
395
396	nvl = create_test_nvlist();
397	clone = nvlist_clone(nvl);
398
399	ATF_REQUIRE(clone != NULL);
400	ATF_REQUIRE(clone != nvl);
401	verify_test_nvlist(clone);
402
403	nvlist_destroy(clone);
404	nvlist_destroy(nvl);
405}
406
407ATF_INIT_TEST_CASES(tp)
408{
409	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
410	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
411	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
412	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
413	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
414	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
415	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
416
417	ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
418	ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
419	ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
420}
421/*-
422 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
423 * All rights reserved.
424 *
425 * Redistribution and use in source and binary forms, with or without
426 * modification, are permitted provided that the following conditions
427 * are met:
428 * 1. Redistributions of source code must retain the above copyright
429 *    notice, this list of conditions and the following disclaimer.
430 * 2. Redistributions in binary form must reproduce the above copyright
431 *    notice, this list of conditions and the following disclaimer in the
432 *    documentation and/or other materials provided with the distribution.
433 *
434 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
435 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
436 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
437 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
438 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
439 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
440 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
441 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
442 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
443 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
444 * SUCH DAMAGE.
445 */
446
447#include <atf-c++.hpp>
448#include <nv.h>
449
450#include <errno.h>
451/*
452 * Test that a newly created nvlist has no errors, and is empty.
453 */
454ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
455ATF_TEST_CASE_BODY(nvlist_create__is_empty)
456{
457	nvlist_t *nvl;
458	int type;
459	void *it;
460
461	nvl = nvlist_create(0);
462
463	ATF_REQUIRE(nvl != NULL);
464
465	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
466	ATF_REQUIRE(nvlist_empty(nvl));
467
468	it = NULL;
469	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
470
471	nvlist_destroy(nvl);
472}
473
474ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
475ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
476{
477	nvlist_t *nvl;
478	void *it;
479	const char *key;
480	int type;
481
482	key = "key";
483	nvl = nvlist_create(0);
484
485	ATF_REQUIRE(nvl != NULL);
486	ATF_REQUIRE(!nvlist_exists(nvl, key));
487
488	nvlist_add_null(nvl, key);
489
490	ATF_REQUIRE(!nvlist_empty(nvl));
491	ATF_REQUIRE(nvlist_exists(nvl, key));
492	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
493	ATF_REQUIRE(nvlist_exists_null(nvl, key));
494	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
495
496	/* Iterate over the nvlist; ensure that it has only our one key. */
497	it = NULL;
498	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
499	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
500	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
501
502	nvlist_destroy(nvl);
503}
504
505ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
506ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
507{
508	nvlist_t *nvl;
509	void *it;
510	const char *key;
511	int type;
512
513	key = "name";
514	nvl = nvlist_create(0);
515
516	ATF_REQUIRE(nvl != NULL);
517	ATF_REQUIRE(!nvlist_exists(nvl, key));
518
519	nvlist_add_bool(nvl, key, true);
520
521	ATF_REQUIRE(!nvlist_empty(nvl));
522	ATF_REQUIRE(nvlist_exists(nvl, key));
523	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
524	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
525	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
526	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
527	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
528
529	/* Iterate over the nvlist; ensure that it has only our one key. */
530	it = NULL;
531	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
532	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
533	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
534
535	nvlist_destroy(nvl);
536}
537
538ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
539ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
540{
541	nvlist_t *nvl;
542	void *it;
543	const char *key;
544	uint64_t value;
545	int type;
546
547	key = "foo123";
548	value = 71965;
549	nvl = nvlist_create(0);
550
551	ATF_REQUIRE(nvl != NULL);
552	ATF_REQUIRE(!nvlist_exists(nvl, key));
553
554	nvlist_add_number(nvl, key, value);
555
556	ATF_REQUIRE(!nvlist_empty(nvl));
557	ATF_REQUIRE(nvlist_exists(nvl, key));
558	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
559	ATF_REQUIRE(nvlist_exists_number(nvl, key));
560	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
561	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
562	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
563
564	/* Iterate over the nvlist; ensure that it has only our one key. */
565	it = NULL;
566	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
567	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
568	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
569
570	nvlist_destroy(nvl);
571}
572
573ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
574ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
575{
576	nvlist_t *nvl;
577	void *it;
578	const char *key;
579	const char *value;
580	int type;
581
582	key = "test";
583	value = "fgjdkgjdk";
584	nvl = nvlist_create(0);
585
586	ATF_REQUIRE(nvl != NULL);
587	ATF_REQUIRE(!nvlist_exists(nvl, key));
588
589	nvlist_add_string(nvl, key, value);
590
591	ATF_REQUIRE(!nvlist_empty(nvl));
592	ATF_REQUIRE(nvlist_exists(nvl, key));
593	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
594	ATF_REQUIRE(nvlist_exists_string(nvl, key));
595	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
596	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
597	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
598
599	/* nvlist_add_* is required to clone the value, so check for that. */
600	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
601
602	/* Iterate over the nvlist; ensure that it has only our one key. */
603	it = NULL;
604	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
605	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
606	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
607
608	nvlist_destroy(nvl);
609}
610
611ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
612ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
613{
614	nvlist_t *nvl;
615	void *it;
616	const char *key, *subkey;
617	nvlist_t *sublist;
618	const nvlist_t *value;
619	int type;
620
621	key = "test";
622	subkey = "subkey";
623	sublist = nvlist_create(0);
624	nvl = nvlist_create(0);
625
626	ATF_REQUIRE(nvl != NULL);
627	ATF_REQUIRE(!nvlist_exists(nvl, key));
628
629	nvlist_add_null(sublist, subkey);
630	nvlist_add_nvlist(nvl, key, sublist);
631
632	ATF_REQUIRE(!nvlist_empty(nvl));
633	ATF_REQUIRE(nvlist_exists(nvl, key));
634	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
635	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
636	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
637
638	value = nvlist_get_nvlist(nvl, key);
639	ATF_REQUIRE(nvlist_exists_null(value, subkey));
640
641	/* nvlist_add_* is required to clone the value, so check for that. */
642	ATF_REQUIRE(sublist != value);
643
644	value = nvlist_getf_nvlist(nvl, "%s", key);
645	ATF_REQUIRE(nvlist_exists_null(value, subkey));
646	ATF_REQUIRE(sublist != value);
647
648	/* Iterate over the nvlist; ensure that it has only our one key. */
649	it = NULL;
650	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
651	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
652	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
653
654	nvlist_destroy(sublist);
655	nvlist_destroy(nvl);
656}
657
658ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
659ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
660{
661	nvlist_t *nvl;
662	void *it;
663	const char *key;
664	void *value;
665	const void *ret_value;
666	size_t value_size, ret_size;
667	int type;
668
669	key = "binary";
670	value_size = 13;
671	value = malloc(value_size);
672	memset(value, 0xa5, value_size);
673	nvl = nvlist_create(0);
674
675	ATF_REQUIRE(nvl != NULL);
676	ATF_REQUIRE(!nvlist_exists(nvl, key));
677
678	nvlist_add_binary(nvl, key, value, value_size);
679
680	ATF_REQUIRE(!nvlist_empty(nvl));
681	ATF_REQUIRE(nvlist_exists(nvl, key));
682	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
683	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
684	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
685
686	ret_value = nvlist_get_binary(nvl, key, &ret_size);
687	ATF_REQUIRE_EQ(value_size, ret_size);
688	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
689
690	/* nvlist_add_* is required to clone the value, so check for that. */
691	ATF_REQUIRE(value != ret_value);
692
693	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
694	ATF_REQUIRE_EQ(value_size, ret_size);
695	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
696	ATF_REQUIRE(value != ret_value);
697
698	/* Iterate over the nvlist; ensure that it has only our one key. */
699	it = NULL;
700	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
701	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
702	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
703
704	nvlist_destroy(nvl);
705	free(value);
706}
707
708ATF_INIT_TEST_CASES(tp)
709{
710	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
711	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
712	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
713	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
714	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
715	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
716	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
717}
718/*-
719 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
720 * All rights reserved.
721 *
722 * Redistribution and use in source and binary forms, with or without
723 * modification, are permitted provided that the following conditions
724 * are met:
725 * 1. Redistributions of source code must retain the above copyright
726 *    notice, this list of conditions and the following disclaimer.
727 * 2. Redistributions in binary form must reproduce the above copyright
728 *    notice, this list of conditions and the following disclaimer in the
729 *    documentation and/or other materials provided with the distribution.
730 *
731 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
732 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
733 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
734 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
735 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
736 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
737 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
738 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
739 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
740 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
741 * SUCH DAMAGE.
742 */
743
744#include <atf-c++.hpp>
745#include <nv.h>
746
747#include <errno.h>
748/*
749 * Test that a newly created nvlist has no errors, and is empty.
750 */
751ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
752ATF_TEST_CASE_BODY(nvlist_create__is_empty)
753{
754	nvlist_t *nvl;
755	int type;
756	void *it;
757
758	nvl = nvlist_create(0);
759
760	ATF_REQUIRE(nvl != NULL);
761
762	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
763	ATF_REQUIRE(nvlist_empty(nvl));
764
765	it = NULL;
766	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
767
768	nvlist_destroy(nvl);
769}
770
771ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
772ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
773{
774	nvlist_t *nvl;
775	void *it;
776	const char *key;
777	int type;
778
779	key = "key";
780	nvl = nvlist_create(0);
781
782	ATF_REQUIRE(nvl != NULL);
783	ATF_REQUIRE(!nvlist_exists(nvl, key));
784
785	nvlist_add_null(nvl, key);
786
787	ATF_REQUIRE(!nvlist_empty(nvl));
788	ATF_REQUIRE(nvlist_exists(nvl, key));
789	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
790	ATF_REQUIRE(nvlist_exists_null(nvl, key));
791	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
792
793	/* Iterate over the nvlist; ensure that it has only our one key. */
794	it = NULL;
795	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
796	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
797	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
798
799	nvlist_destroy(nvl);
800}
801
802ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
803ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
804{
805	nvlist_t *nvl;
806	void *it;
807	const char *key;
808	int type;
809
810	key = "name";
811	nvl = nvlist_create(0);
812
813	ATF_REQUIRE(nvl != NULL);
814	ATF_REQUIRE(!nvlist_exists(nvl, key));
815
816	nvlist_add_bool(nvl, key, true);
817
818	ATF_REQUIRE(!nvlist_empty(nvl));
819	ATF_REQUIRE(nvlist_exists(nvl, key));
820	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
821	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
822	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
823	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
824	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
825
826	/* Iterate over the nvlist; ensure that it has only our one key. */
827	it = NULL;
828	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
829	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
830	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
831
832	nvlist_destroy(nvl);
833}
834
835ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
836ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
837{
838	nvlist_t *nvl;
839	void *it;
840	const char *key;
841	uint64_t value;
842	int type;
843
844	key = "foo123";
845	value = 71965;
846	nvl = nvlist_create(0);
847
848	ATF_REQUIRE(nvl != NULL);
849	ATF_REQUIRE(!nvlist_exists(nvl, key));
850
851	nvlist_add_number(nvl, key, value);
852
853	ATF_REQUIRE(!nvlist_empty(nvl));
854	ATF_REQUIRE(nvlist_exists(nvl, key));
855	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
856	ATF_REQUIRE(nvlist_exists_number(nvl, key));
857	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
858	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
859	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
860
861	/* Iterate over the nvlist; ensure that it has only our one key. */
862	it = NULL;
863	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
864	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
865	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
866
867	nvlist_destroy(nvl);
868}
869
870ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
871ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
872{
873	nvlist_t *nvl;
874	void *it;
875	const char *key;
876	const char *value;
877	int type;
878
879	key = "test";
880	value = "fgjdkgjdk";
881	nvl = nvlist_create(0);
882
883	ATF_REQUIRE(nvl != NULL);
884	ATF_REQUIRE(!nvlist_exists(nvl, key));
885
886	nvlist_add_string(nvl, key, value);
887
888	ATF_REQUIRE(!nvlist_empty(nvl));
889	ATF_REQUIRE(nvlist_exists(nvl, key));
890	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
891	ATF_REQUIRE(nvlist_exists_string(nvl, key));
892	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
893	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
894	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
895
896	/* nvlist_add_* is required to clone the value, so check for that. */
897	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
898
899	/* Iterate over the nvlist; ensure that it has only our one key. */
900	it = NULL;
901	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
902	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
903	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
904
905	nvlist_destroy(nvl);
906}
907
908ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
909ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
910{
911	nvlist_t *nvl;
912	void *it;
913	const char *key, *subkey;
914	nvlist_t *sublist;
915	const nvlist_t *value;
916	int type;
917
918	key = "test";
919	subkey = "subkey";
920	sublist = nvlist_create(0);
921	nvl = nvlist_create(0);
922
923	ATF_REQUIRE(nvl != NULL);
924	ATF_REQUIRE(!nvlist_exists(nvl, key));
925
926	nvlist_add_null(sublist, subkey);
927	nvlist_add_nvlist(nvl, key, sublist);
928
929	ATF_REQUIRE(!nvlist_empty(nvl));
930	ATF_REQUIRE(nvlist_exists(nvl, key));
931	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
932	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
933	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
934
935	value = nvlist_get_nvlist(nvl, key);
936	ATF_REQUIRE(nvlist_exists_null(value, subkey));
937
938	/* nvlist_add_* is required to clone the value, so check for that. */
939	ATF_REQUIRE(sublist != value);
940
941	value = nvlist_getf_nvlist(nvl, "%s", key);
942	ATF_REQUIRE(nvlist_exists_null(value, subkey));
943	ATF_REQUIRE(sublist != value);
944
945	/* Iterate over the nvlist; ensure that it has only our one key. */
946	it = NULL;
947	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
948	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
949	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
950
951	nvlist_destroy(sublist);
952	nvlist_destroy(nvl);
953}
954
955ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
956ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
957{
958	nvlist_t *nvl;
959	void *it;
960	const char *key;
961	void *value;
962	const void *ret_value;
963	size_t value_size, ret_size;
964	int type;
965
966	key = "binary";
967	value_size = 13;
968	value = malloc(value_size);
969	memset(value, 0xa5, value_size);
970	nvl = nvlist_create(0);
971
972	ATF_REQUIRE(nvl != NULL);
973	ATF_REQUIRE(!nvlist_exists(nvl, key));
974
975	nvlist_add_binary(nvl, key, value, value_size);
976
977	ATF_REQUIRE(!nvlist_empty(nvl));
978	ATF_REQUIRE(nvlist_exists(nvl, key));
979	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
980	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
981	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
982
983	ret_value = nvlist_get_binary(nvl, key, &ret_size);
984	ATF_REQUIRE_EQ(value_size, ret_size);
985	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
986
987	/* nvlist_add_* is required to clone the value, so check for that. */
988	ATF_REQUIRE(value != ret_value);
989
990	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
991	ATF_REQUIRE_EQ(value_size, ret_size);
992	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
993	ATF_REQUIRE(value != ret_value);
994
995	/* Iterate over the nvlist; ensure that it has only our one key. */
996	it = NULL;
997	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
998	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
999	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1000
1001	nvlist_destroy(nvl);
1002	free(value);
1003}
1004
1005ATF_INIT_TEST_CASES(tp)
1006{
1007	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1008	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1009	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1010	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1011	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1012	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1013	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1014}
1015/*-
1016 * Copyright (c) 2014-2015 Sandvine Inc.  All rights reserved.
1017 * All rights reserved.
1018 *
1019 * Redistribution and use in source and binary forms, with or without
1020 * modification, are permitted provided that the following conditions
1021 * are met:
1022 * 1. Redistributions of source code must retain the above copyright
1023 *    notice, this list of conditions and the following disclaimer.
1024 * 2. Redistributions in binary form must reproduce the above copyright
1025 *    notice, this list of conditions and the following disclaimer in the
1026 *    documentation and/or other materials provided with the distribution.
1027 *
1028 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1029 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1030 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1031 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1032 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1033 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1034 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1035 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1036 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1037 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1038 * SUCH DAMAGE.
1039 */
1040
1041#include <atf-c++.hpp>
1042#include <nv.h>
1043
1044#include <errno.h>
1045#include <limits>
1046#include <set>
1047#include <sstream>
1048#include <string>
1049
1050/*
1051 * Test that a newly created nvlist has no errors, and is empty.
1052 */
1053ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
1054ATF_TEST_CASE_BODY(nvlist_create__is_empty)
1055{
1056	nvlist_t *nvl;
1057	int type;
1058	void *it;
1059
1060	nvl = nvlist_create(0);
1061
1062	ATF_REQUIRE(nvl != NULL);
1063
1064	ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
1065	ATF_REQUIRE(nvlist_empty(nvl));
1066
1067	it = NULL;
1068	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1069
1070	nvlist_destroy(nvl);
1071}
1072
1073ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
1074ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
1075{
1076	nvlist_t *nvl;
1077	void *it;
1078	const char *key;
1079	int type;
1080
1081	key = "key";
1082	nvl = nvlist_create(0);
1083
1084	ATF_REQUIRE(nvl != NULL);
1085	ATF_REQUIRE(!nvlist_exists(nvl, key));
1086
1087	nvlist_add_null(nvl, key);
1088
1089	ATF_REQUIRE(!nvlist_empty(nvl));
1090	ATF_REQUIRE(nvlist_exists(nvl, key));
1091	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1092	ATF_REQUIRE(nvlist_exists_null(nvl, key));
1093	ATF_REQUIRE(nvlist_existsf_null(nvl, "key"));
1094
1095	/* Iterate over the nvlist; ensure that it has only our one key. */
1096	it = NULL;
1097	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1098	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
1099	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1100
1101	nvlist_destroy(nvl);
1102}
1103
1104ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
1105ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
1106{
1107	nvlist_t *nvl;
1108	void *it;
1109	const char *key;
1110	int type;
1111
1112	key = "name";
1113	nvl = nvlist_create(0);
1114
1115	ATF_REQUIRE(nvl != NULL);
1116	ATF_REQUIRE(!nvlist_exists(nvl, key));
1117
1118	nvlist_add_bool(nvl, key, true);
1119
1120	ATF_REQUIRE(!nvlist_empty(nvl));
1121	ATF_REQUIRE(nvlist_exists(nvl, key));
1122	ATF_REQUIRE(nvlist_existsf(nvl, "%s%s", "na", "me"));
1123	ATF_REQUIRE(nvlist_exists_bool(nvl, key));
1124	ATF_REQUIRE(nvlist_existsf_bool(nvl, "%s%c", "nam", 'e'));
1125	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
1126	ATF_REQUIRE_EQ(nvlist_getf_bool(nvl, "%c%s", 'n', "ame"), true);
1127
1128	/* Iterate over the nvlist; ensure that it has only our one key. */
1129	it = NULL;
1130	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1131	ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
1132	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1133
1134	nvlist_destroy(nvl);
1135}
1136
1137ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
1138ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
1139{
1140	nvlist_t *nvl;
1141	void *it;
1142	const char *key;
1143	uint64_t value;
1144	int type;
1145
1146	key = "foo123";
1147	value = 71965;
1148	nvl = nvlist_create(0);
1149
1150	ATF_REQUIRE(nvl != NULL);
1151	ATF_REQUIRE(!nvlist_exists(nvl, key));
1152
1153	nvlist_add_number(nvl, key, value);
1154
1155	ATF_REQUIRE(!nvlist_empty(nvl));
1156	ATF_REQUIRE(nvlist_exists(nvl, key));
1157	ATF_REQUIRE(nvlist_existsf(nvl, "%s%d", "foo", 123));
1158	ATF_REQUIRE(nvlist_exists_number(nvl, key));
1159	ATF_REQUIRE(nvlist_existsf_number(nvl, "%s", key));
1160	ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
1161	ATF_REQUIRE_EQ(nvlist_getf_number(nvl, "%s", key), value);
1162
1163	/* Iterate over the nvlist; ensure that it has only our one key. */
1164	it = NULL;
1165	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1166	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1167	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1168
1169	nvlist_destroy(nvl);
1170}
1171
1172ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
1173ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
1174{
1175	nvlist_t *nvl;
1176	void *it;
1177	const char *key;
1178	const char *value;
1179	int type;
1180
1181	key = "test";
1182	value = "fgjdkgjdk";
1183	nvl = nvlist_create(0);
1184
1185	ATF_REQUIRE(nvl != NULL);
1186	ATF_REQUIRE(!nvlist_exists(nvl, key));
1187
1188	nvlist_add_string(nvl, key, value);
1189
1190	ATF_REQUIRE(!nvlist_empty(nvl));
1191	ATF_REQUIRE(nvlist_exists(nvl, key));
1192	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1193	ATF_REQUIRE(nvlist_exists_string(nvl, key));
1194	ATF_REQUIRE(nvlist_existsf_string(nvl, "%s", key));
1195	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
1196	ATF_REQUIRE_EQ(strcmp(nvlist_getf_string(nvl, "%s", key), value), 0);
1197
1198	/* nvlist_add_* is required to clone the value, so check for that. */
1199	ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
1200
1201	/* Iterate over the nvlist; ensure that it has only our one key. */
1202	it = NULL;
1203	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1204	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
1205	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1206
1207	nvlist_destroy(nvl);
1208}
1209
1210ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
1211ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
1212{
1213	nvlist_t *nvl;
1214	void *it;
1215	const char *key, *subkey;
1216	nvlist_t *sublist;
1217	const nvlist_t *value;
1218	int type;
1219
1220	key = "test";
1221	subkey = "subkey";
1222	sublist = nvlist_create(0);
1223	nvl = nvlist_create(0);
1224
1225	ATF_REQUIRE(nvl != NULL);
1226	ATF_REQUIRE(!nvlist_exists(nvl, key));
1227
1228	nvlist_add_null(sublist, subkey);
1229	nvlist_add_nvlist(nvl, key, sublist);
1230
1231	ATF_REQUIRE(!nvlist_empty(nvl));
1232	ATF_REQUIRE(nvlist_exists(nvl, key));
1233	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1234	ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
1235	ATF_REQUIRE(nvlist_existsf_nvlist(nvl, "%s", key));
1236
1237	value = nvlist_get_nvlist(nvl, key);
1238	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1239
1240	/* nvlist_add_* is required to clone the value, so check for that. */
1241	ATF_REQUIRE(sublist != value);
1242
1243	value = nvlist_getf_nvlist(nvl, "%s", key);
1244	ATF_REQUIRE(nvlist_exists_null(value, subkey));
1245	ATF_REQUIRE(sublist != value);
1246
1247	/* Iterate over the nvlist; ensure that it has only our one key. */
1248	it = NULL;
1249	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1250	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1251	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1252
1253	nvlist_destroy(sublist);
1254	nvlist_destroy(nvl);
1255}
1256
1257ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
1258ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
1259{
1260	nvlist_t *nvl;
1261	void *it;
1262	const char *key;
1263	void *value;
1264	const void *ret_value;
1265	size_t value_size, ret_size;
1266	int type;
1267
1268	key = "binary";
1269	value_size = 13;
1270	value = malloc(value_size);
1271	memset(value, 0xa5, value_size);
1272	nvl = nvlist_create(0);
1273
1274	ATF_REQUIRE(nvl != NULL);
1275	ATF_REQUIRE(!nvlist_exists(nvl, key));
1276
1277	nvlist_add_binary(nvl, key, value, value_size);
1278
1279	ATF_REQUIRE(!nvlist_empty(nvl));
1280	ATF_REQUIRE(nvlist_exists(nvl, key));
1281	ATF_REQUIRE(nvlist_existsf(nvl, "%s", key));
1282	ATF_REQUIRE(nvlist_exists_binary(nvl, key));
1283	ATF_REQUIRE(nvlist_existsf_binary(nvl, "%s", key));
1284
1285	ret_value = nvlist_get_binary(nvl, key, &ret_size);
1286	ATF_REQUIRE_EQ(value_size, ret_size);
1287	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1288
1289	/* nvlist_add_* is required to clone the value, so check for that. */
1290	ATF_REQUIRE(value != ret_value);
1291
1292	ret_value = nvlist_getf_binary(nvl, &ret_size, "%s", key);
1293	ATF_REQUIRE_EQ(value_size, ret_size);
1294	ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
1295	ATF_REQUIRE(value != ret_value);
1296
1297	/* Iterate over the nvlist; ensure that it has only our one key. */
1298	it = NULL;
1299	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
1300	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
1301	ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), NULL);
1302
1303	nvlist_destroy(nvl);
1304	free(value);
1305}
1306
1307ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
1308ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
1309{
1310	nvlist_t *nvl, *clone;
1311
1312	nvl = nvlist_create(0);
1313	ATF_REQUIRE(nvl != NULL);
1314
1315	clone = nvlist_clone(nvl);
1316	ATF_REQUIRE(clone != NULL);
1317	ATF_REQUIRE(clone != nvl);
1318	ATF_REQUIRE(nvlist_empty(clone));
1319
1320	nvlist_destroy(clone);
1321	nvlist_destroy(nvl);
1322}
1323
1324ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
1325ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
1326{
1327	nvlist_t *nvl, *clone;
1328	const char *key;
1329	void *it;
1330	uint64_t value;
1331	int type;
1332
1333	nvl = nvlist_create(0);
1334	ATF_REQUIRE(nvl != NULL);
1335
1336	key = "testkey";
1337	value = 684874;
1338	nvlist_add_number(nvl, key, value);
1339
1340	clone = nvlist_clone(nvl);
1341	ATF_REQUIRE(clone != NULL);
1342	ATF_REQUIRE(clone != nvl);
1343	ATF_REQUIRE(nvlist_exists_number(clone, key));
1344	ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value);
1345
1346	/* Iterate over the nvlist; ensure that it has only our one key. */
1347	it = NULL;
1348	ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0);
1349	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1350	ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), NULL);
1351
1352	nvlist_destroy(clone);
1353	nvlist_destroy(nvl);
1354}
1355
1356static const char * const test_subnvlist_key = "nvlist";
1357
1358static const char * const test_string_key = "string";
1359static const char * const test_string_val = "59525";
1360
1361static nvlist_t*
1362create_test_nvlist(void)
1363{
1364	nvlist_t *nvl, *sublist;
1365
1366	nvl = nvlist_create(0);
1367	ATF_REQUIRE(nvl != NULL);
1368
1369	sublist = nvlist_create(0);
1370	ATF_REQUIRE(sublist != NULL);
1371
1372	nvlist_add_string(sublist, test_string_key, test_string_val);
1373	nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
1374
1375	return (nvl);
1376}
1377
1378static void
1379verify_test_nvlist(const nvlist_t *nvl)
1380{
1381	void *it;
1382	const nvlist_t *value;
1383	int type;
1384
1385	ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
1386
1387	value = nvlist_get_nvlist(nvl, test_subnvlist_key);
1388
1389	ATF_REQUIRE(nvlist_exists_string(value, test_string_key));
1390	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0);
1391	ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val);
1392
1393	/* Iterate over both nvlists; ensure that each has only the one key. */
1394	it = NULL;
1395	ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it),
1396	    test_string_key), 0);
1397	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
1398	ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), NULL);
1399
1400	it = NULL;
1401	ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it),
1402	    test_subnvlist_key), 0);
1403	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1404	ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), NULL);
1405}
1406
1407ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
1408ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
1409{
1410	nvlist_t *nvl, *clone;
1411
1412	nvl = create_test_nvlist();
1413	clone = nvlist_clone(nvl);
1414
1415	ATF_REQUIRE(clone != NULL);
1416	ATF_REQUIRE(clone != nvl);
1417	verify_test_nvlist(clone);
1418
1419	nvlist_destroy(clone);
1420	nvlist_destroy(nvl);
1421}
1422
1423ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist);
1424ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)
1425{
1426	nvlist_t *nvl, *unpacked;
1427	void *packed;
1428	size_t packed_size;
1429
1430	nvl = nvlist_create(0);
1431	ATF_REQUIRE(nvl != NULL);
1432
1433	packed = nvlist_pack(nvl, &packed_size);
1434	ATF_REQUIRE(packed != NULL);
1435
1436	unpacked = nvlist_unpack(packed, packed_size);
1437	ATF_REQUIRE(unpacked != NULL);
1438	ATF_REQUIRE(unpacked != nvl);
1439	ATF_REQUIRE(nvlist_empty(unpacked));
1440
1441	nvlist_destroy(unpacked);
1442	nvlist_destroy(nvl);
1443	free(packed);
1444}
1445
1446static void
1447verify_null(const nvlist_t *nvl, int type)
1448{
1449
1450	ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
1451}
1452
1453static void
1454verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value)
1455{
1456
1457	ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
1458	ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value);
1459}
1460
1461static void
1462verify_string(const nvlist_t *nvl, const char *name, int type,
1463    const char * value)
1464{
1465
1466	ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
1467	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0);
1468}
1469
1470static void
1471verify_nvlist(const nvlist_t *nvl, const char *name, int type)
1472{
1473
1474	ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
1475	verify_test_nvlist(nvlist_get_nvlist(nvl, name));
1476}
1477
1478static void
1479verify_binary(const nvlist_t *nvl, const char *name, int type,
1480    const void * value, size_t size)
1481{
1482	const void *actual_value;
1483	size_t actual_size;
1484
1485	ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
1486	actual_value = nvlist_get_binary(nvl, name, &actual_size);
1487	ATF_REQUIRE_EQ(size, actual_size);
1488	ATF_REQUIRE_EQ(memcmp(value, actual_value, size), 0);
1489}
1490
1491ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values);
1492ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)
1493{
1494	std::ostringstream msg;
1495	std::set<std::string> keys_seen;
1496	nvlist_t *nvl, *unpacked, *nvvalue;
1497	const char *nullkey, *numkey, *strkey, *nvkey, *binkey, *name;
1498	int numvalue;
1499	const char * strvalue;
1500	void *binvalue, *packed, *it;
1501	size_t binsize, packed_size;
1502	int type;
1503
1504	nvl = nvlist_create(0);
1505
1506	nullkey = "null";
1507	nvlist_add_null(nvl, nullkey);
1508
1509	numkey = "number";
1510	numvalue = 939853984;
1511	nvlist_add_number(nvl, numkey, numvalue);
1512
1513	strkey = "string";
1514	strvalue = "jfieutijf";
1515	nvlist_add_string(nvl, strkey, strvalue);
1516
1517	nvkey = "nvlist";
1518	nvvalue = create_test_nvlist();
1519	nvlist_move_nvlist(nvl, nvkey, nvvalue);
1520
1521	binkey = "binary";
1522	binsize = 4;
1523	binvalue = malloc(binsize);
1524	memset(binvalue, 'b', binsize);
1525	nvlist_move_binary(nvl, binkey, binvalue, binsize);
1526
1527	packed = nvlist_pack(nvl, &packed_size);
1528	ATF_REQUIRE(packed != NULL);
1529
1530	unpacked = nvlist_unpack(packed, packed_size);
1531	ATF_REQUIRE(unpacked != 0);
1532
1533	it = NULL;
1534	while ((name = nvlist_next(unpacked, &type, &it)) != NULL) {
1535		/* Ensure that we see every key only once. */
1536		ATF_REQUIRE_EQ(keys_seen.count(name), 0);
1537
1538		if (strcmp(name, nullkey) == 0)
1539			verify_null(unpacked, type);
1540		else if (strcmp(name, numkey) == 0)
1541			verify_number(unpacked, name, type, numvalue);
1542		else if (strcmp(name, strkey) == 0)
1543			verify_string(unpacked, name, type, strvalue);
1544		else if (strcmp(name, nvkey) == 0)
1545			verify_nvlist(unpacked, name, type);
1546		else if (strcmp(name, binkey) == 0)
1547			verify_binary(unpacked, name, type, binvalue, binsize);
1548		else {
1549			msg << "Unexpected key :'" << name << "'";
1550			ATF_FAIL(msg.str().c_str());
1551		}
1552
1553		keys_seen.insert(name);
1554	}
1555
1556	/* Ensure that we saw every key. */
1557	ATF_REQUIRE_EQ(keys_seen.size(), 5);
1558
1559	nvlist_destroy(nvl);
1560	nvlist_destroy(unpacked);
1561	free(packed);
1562}
1563
1564ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key);
1565ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)
1566{
1567	nvlist_t *nvl, *unpacked;
1568	const char *key1, *key2;
1569	void *packed, *keypos;
1570	size_t size, keylen;
1571
1572	nvl = nvlist_create(0);
1573
1574	key1 = "key1";
1575	keylen = strlen(key1);
1576	nvlist_add_number(nvl, key1, 5);
1577
1578	key2 = "key2";
1579	ATF_REQUIRE_EQ(keylen, strlen(key2));
1580	nvlist_add_number(nvl, key2, 10);
1581
1582	packed = nvlist_pack(nvl, &size);
1583
1584	/*
1585	 * Mangle the packed nvlist by replacing key1 with key2, creating a
1586	 * packed nvlist with a duplicate key.
1587	 */
1588	keypos = memmem(packed, size, key1, keylen);
1589	ATF_REQUIRE(keypos != NULL);
1590	memcpy(keypos, key2, keylen);
1591
1592	unpacked = nvlist_unpack(packed, size);
1593	ATF_REQUIRE(nvlist_error(unpacked) != 0);
1594
1595	free(packed);
1596	nvlist_destroy(nvl);
1597	nvlist_destroy(unpacked);
1598}
1599
1600ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
1601ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
1602{
1603	nvlist_t *nvl;
1604	const char *key;
1605	char *value;
1606
1607	nvl = nvlist_create(0);
1608	ATF_REQUIRE(nvl != NULL);
1609
1610	key = "testkey";
1611	value = strdup("testval");
1612	ATF_REQUIRE(value != NULL);
1613
1614	nvlist_move_string(nvl, key, value);
1615	ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
1616
1617	nvlist_destroy(nvl);
1618}
1619
1620ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
1621ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
1622{
1623	nvlist_t *parent;
1624
1625	parent = nvlist_create(0);
1626
1627	nvlist_move_nvlist(parent, "test", NULL);
1628
1629	ATF_REQUIRE(nvlist_error(parent) != 0);
1630
1631	nvlist_destroy(parent);
1632}
1633
1634ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
1635ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
1636{
1637	nvlist_t *nvl;
1638	const char *key;
1639	nvlist_t *value;
1640
1641	nvl = nvlist_create(0);
1642	ATF_REQUIRE(nvl != NULL);
1643
1644	key = "testkey";
1645	value = nvlist_create(0);
1646	ATF_REQUIRE(value != NULL);
1647
1648	nvlist_move_nvlist(nvl, key, value);
1649	ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
1650
1651	nvlist_destroy(nvl);
1652}
1653
1654ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
1655ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
1656{
1657	nvlist_t *nvl;
1658	const char *key;
1659	void *value;
1660	size_t size, actual_size;
1661
1662	nvl = nvlist_create(0);
1663	ATF_REQUIRE(nvl != NULL);
1664
1665	key = "testkey";
1666	size = 73;
1667	value = malloc(size);
1668	ATF_REQUIRE(value != NULL);
1669
1670	nvlist_move_binary(nvl, key, value, size);
1671	ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
1672	ATF_REQUIRE_EQ(size, actual_size);
1673
1674	nvlist_destroy(nvl);
1675}
1676
1677ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
1678ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
1679{
1680	nvlist_t *nvl;
1681	const char *testkey;
1682	bool testval;
1683
1684	nvl = nvlist_create(0);
1685	ATF_REQUIRE(nvl != NULL);
1686
1687	testkey = "boolkey";
1688	testval = false;
1689	nvlist_add_bool(nvl, testkey, testval);
1690
1691	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
1692	ATF_REQUIRE(nvlist_empty(nvl));
1693
1694	nvlist_destroy(nvl);
1695}
1696
1697ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
1698ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
1699{
1700	nvlist_t *nvl;
1701	const char *testkey, *otherkey1, *otherkey2;
1702	bool testval, otherval1;
1703	nvlist_t *otherval2;
1704
1705	nvl = nvlist_create(0);
1706	ATF_REQUIRE(nvl != NULL);
1707
1708	testkey = "boolkey";
1709	testval = true;
1710	nvlist_add_bool(nvl, testkey, testval);
1711
1712	otherkey1 = "key1";
1713	otherval1 = false;
1714	nvlist_add_bool(nvl, otherkey1, otherval1);
1715
1716	otherkey2 = "key2";
1717	otherval2 = create_test_nvlist();
1718	nvlist_move_nvlist(nvl, otherkey2, otherval2);
1719
1720	ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
1721
1722	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
1723	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
1724
1725	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
1726	verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
1727
1728	nvlist_destroy(nvl);
1729}
1730
1731ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
1732ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
1733{
1734	nvlist_t *nvl;
1735	const char *testkey;
1736	uint64_t testval;
1737
1738	nvl = nvlist_create(0);
1739	ATF_REQUIRE(nvl != NULL);
1740
1741	testkey = "numkey";
1742	testval = std::numeric_limits<uint64_t>::max();
1743	nvlist_add_number(nvl, testkey, testval);
1744
1745	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
1746	ATF_REQUIRE(nvlist_empty(nvl));
1747
1748	nvlist_destroy(nvl);
1749}
1750
1751ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
1752ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
1753{
1754	nvlist_t *nvl;
1755	const char *testkey, *otherkey1, *otherkey2;
1756	uint64_t testval, otherval1;
1757	const char *otherval2;
1758
1759	nvl = nvlist_create(0);
1760	ATF_REQUIRE(nvl != NULL);
1761
1762	otherkey1 = "key1";
1763	otherval1 = 5;
1764	nvlist_add_number(nvl, otherkey1, otherval1);
1765
1766	testkey = "numkey";
1767	testval = 1654;
1768	nvlist_add_number(nvl, testkey, testval);
1769
1770	otherkey2 = "key2";
1771	otherval2 = "string";
1772	nvlist_add_string(nvl, otherkey2, otherval2);
1773
1774	ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
1775
1776	ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
1777	ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
1778
1779	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
1780	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
1781
1782	nvlist_destroy(nvl);
1783}
1784
1785ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
1786ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
1787{
1788	nvlist_t *nvl;
1789	const char *testkey;
1790	const char *testval;
1791
1792	nvl = nvlist_create(0);
1793	ATF_REQUIRE(nvl != NULL);
1794
1795	testkey = "numkey";
1796	testval = "nvlist";
1797	nvlist_add_string(nvl, testkey, testval);
1798
1799	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
1800	ATF_REQUIRE(nvlist_empty(nvl));
1801
1802	nvlist_destroy(nvl);
1803}
1804
1805ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
1806ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
1807{
1808	nvlist_t *nvl;
1809	const char *testkey, *otherkey1, *otherkey2;
1810	const char *testval, *otherval1;
1811	bool otherval2;
1812
1813	nvl = nvlist_create(0);
1814	ATF_REQUIRE(nvl != NULL);
1815
1816	otherkey1 = "key1";
1817	otherval1 = "fjdifjdk";
1818	nvlist_add_string(nvl, otherkey1, otherval1);
1819
1820	otherkey2 = "key2";
1821	otherval2 = true;
1822	nvlist_add_bool(nvl, otherkey2, otherval2);
1823
1824	testkey = "strkey";
1825	testval = "1654";
1826	nvlist_add_string(nvl, testkey, testval);
1827
1828	ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
1829
1830	ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
1831	ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
1832
1833	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
1834	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
1835
1836	nvlist_destroy(nvl);
1837}
1838
1839ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
1840ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
1841{
1842	nvlist_t *nvl;
1843	const char *testkey;
1844	nvlist_t *testval;
1845
1846	nvl = nvlist_create(0);
1847	ATF_REQUIRE(nvl != NULL);
1848
1849	testkey = "numkey";
1850	testval = create_test_nvlist();
1851	nvlist_move_nvlist(nvl, testkey, testval);
1852
1853	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
1854	ATF_REQUIRE(nvlist_empty(nvl));
1855
1856	nvlist_destroy(nvl);
1857}
1858
1859ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
1860ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
1861{
1862	nvlist_t *nvl;
1863	const char *testkey, *otherkey1, *otherkey2;
1864	nvlist_t *testval, *otherval1;
1865
1866	nvl = nvlist_create(0);
1867	ATF_REQUIRE(nvl != NULL);
1868
1869	testkey = "strkey";
1870	testval = create_test_nvlist();
1871	nvlist_move_nvlist(nvl, testkey, testval);
1872
1873	otherkey1 = "key1";
1874	otherval1 = nvlist_create(0);
1875	nvlist_move_nvlist(nvl, otherkey1, otherval1);
1876
1877	otherkey2 = "key2";
1878	nvlist_add_null(nvl, otherkey2);
1879
1880	verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
1881
1882	ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
1883	ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
1884
1885	ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
1886
1887	nvlist_destroy(nvl);
1888}
1889
1890ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
1891ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
1892{
1893	nvlist_t *nvl;
1894	const char *testkey;
1895	void *testval;
1896	const void *actual_val;
1897	size_t testsize, actual_size;
1898
1899	nvl = nvlist_create(0);
1900	ATF_REQUIRE(nvl != NULL);
1901
1902	testkey = "numkey";
1903	testsize = 457;
1904	testval = malloc(testsize);
1905	memset(testval, '5', testsize);
1906	nvlist_move_binary(nvl, testkey, testval, testsize);
1907
1908	actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
1909	ATF_REQUIRE_EQ(testsize, actual_size);
1910	ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
1911	ATF_REQUIRE(nvlist_empty(nvl));
1912
1913	nvlist_destroy(nvl);
1914}
1915
1916ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
1917ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
1918{
1919	nvlist_t *nvl;
1920	const char *testkey, *otherkey1, *otherkey2;
1921	const void *actual_value;
1922	char testval[] = "gjiertj";
1923	char otherval1[] = "fdreg";
1924	size_t testsize, othersize, actual_size;
1925	bool otherval2;
1926
1927	nvl = nvlist_create(0);
1928	ATF_REQUIRE(nvl != NULL);
1929
1930	otherkey1 = "key1";
1931	othersize = sizeof(otherval1);
1932	nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
1933
1934	otherkey2 = "key2";
1935	otherval2 = true;
1936	nvlist_add_bool(nvl, otherkey2, otherval2);
1937
1938	testkey = "strkey";
1939	testsize = sizeof(testval);
1940	nvlist_add_binary(nvl, testkey, testval, testsize);
1941
1942	actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
1943	ATF_REQUIRE_EQ(testsize, actual_size);
1944	ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
1945
1946	ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
1947	actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
1948	ATF_REQUIRE_EQ(othersize, actual_size);
1949	ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
1950
1951	ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
1952	ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
1953
1954	nvlist_destroy(nvl);
1955}
1956
1957ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
1958ATF_TEST_CASE_BODY(nvlist_free__single_null)
1959{
1960	nvlist_t *nvl;
1961	const char *key;
1962
1963	nvl = nvlist_create(0);
1964	key = "test";
1965	nvlist_add_null(nvl, key);
1966
1967	nvlist_free(nvl, key);
1968	ATF_REQUIRE(nvlist_empty(nvl));
1969
1970	nvlist_destroy(nvl);
1971}
1972
1973ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
1974ATF_TEST_CASE_BODY(nvlist_free__single_bool)
1975{
1976	nvlist_t *nvl;
1977	const char *key;
1978
1979	nvl = nvlist_create(0);
1980	key = "test";
1981	nvlist_add_bool(nvl, key, true);
1982
1983	nvlist_free(nvl, key);
1984	ATF_REQUIRE(nvlist_empty(nvl));
1985
1986	nvlist_destroy(nvl);
1987}
1988
1989ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
1990ATF_TEST_CASE_BODY(nvlist_free__single_number)
1991{
1992	nvlist_t *nvl;
1993	const char *key;
1994
1995	nvl = nvlist_create(0);
1996	key = "test";
1997	nvlist_add_number(nvl, key, 584);
1998
1999	nvlist_free(nvl, key);
2000	ATF_REQUIRE(nvlist_empty(nvl));
2001
2002	nvlist_destroy(nvl);
2003}
2004
2005ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
2006ATF_TEST_CASE_BODY(nvlist_free__single_string)
2007{
2008	nvlist_t *nvl;
2009	const char *key;
2010
2011	nvl = nvlist_create(0);
2012	key = "test";
2013	nvlist_add_string(nvl, key, "gjkfkjd");
2014
2015	nvlist_free(nvl, key);
2016	ATF_REQUIRE(nvlist_empty(nvl));
2017
2018	nvlist_destroy(nvl);
2019}
2020
2021ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
2022ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
2023{
2024	nvlist_t *nvl;
2025	const char *key;
2026
2027	nvl = nvlist_create(0);
2028	key = "test";
2029	nvlist_add_nvlist(nvl, key, nvlist_create(0));
2030
2031	nvlist_free(nvl, key);
2032	ATF_REQUIRE(nvlist_empty(nvl));
2033
2034	nvlist_destroy(nvl);
2035}
2036
2037ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
2038ATF_TEST_CASE_BODY(nvlist_free__single_binary)
2039{
2040	nvlist_t *nvl;
2041	const char *key;
2042
2043	nvl = nvlist_create(0);
2044	key = "test";
2045	nvlist_add_binary(nvl, key, "jgjgfd", 6);
2046
2047	nvlist_free(nvl, key);
2048	ATF_REQUIRE(nvlist_empty(nvl));
2049
2050	nvlist_destroy(nvl);
2051}
2052
2053ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
2054ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
2055{
2056	nvlist_t *nvl;
2057	const char *key;
2058
2059	nvl = nvlist_create(0);
2060	key = "test";
2061	nvlist_add_null(nvl, key);
2062
2063	nvlist_free_null(nvl, key);
2064	ATF_REQUIRE(nvlist_empty(nvl));
2065
2066	nvlist_destroy(nvl);
2067}
2068
2069ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
2070ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
2071{
2072	nvlist_t *nvl;
2073	const char *key;
2074
2075	nvl = nvlist_create(0);
2076	key = "test";
2077	nvlist_add_bool(nvl, key, true);
2078
2079	nvlist_free_bool(nvl, key);
2080	ATF_REQUIRE(nvlist_empty(nvl));
2081
2082	nvlist_destroy(nvl);
2083}
2084
2085ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
2086ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
2087{
2088	nvlist_t *nvl;
2089	const char *key;
2090
2091	nvl = nvlist_create(0);
2092	key = "test";
2093	nvlist_add_number(nvl, key, 584);
2094
2095	nvlist_free_number(nvl, key);
2096	ATF_REQUIRE(nvlist_empty(nvl));
2097
2098	nvlist_destroy(nvl);
2099}
2100
2101ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
2102ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
2103{
2104	nvlist_t *nvl;
2105	const char *key;
2106
2107	nvl = nvlist_create(0);
2108	key = "test";
2109	nvlist_add_string(nvl, key, "gjkfkjd");
2110
2111	nvlist_free_string(nvl, key);
2112	ATF_REQUIRE(nvlist_empty(nvl));
2113
2114	nvlist_destroy(nvl);
2115}
2116
2117ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
2118ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
2119{
2120	nvlist_t *nvl;
2121	const char *key;
2122
2123	nvl = nvlist_create(0);
2124	key = "test";
2125	nvlist_add_nvlist(nvl, key, nvlist_create(0));
2126
2127	nvlist_free_nvlist(nvl, key);
2128	ATF_REQUIRE(nvlist_empty(nvl));
2129
2130	nvlist_destroy(nvl);
2131}
2132
2133ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
2134ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
2135{
2136	nvlist_t *nvl;
2137	const char *key;
2138
2139	nvl = nvlist_create(0);
2140	key = "test";
2141	nvlist_add_binary(nvl, key, "jgjgfd", 6);
2142
2143	nvlist_free_binary(nvl, key);
2144	ATF_REQUIRE(nvlist_empty(nvl));
2145
2146	nvlist_destroy(nvl);
2147}
2148
2149ATF_INIT_TEST_CASES(tp)
2150{
2151	ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
2152	ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
2153	ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
2154	ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
2155	ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
2156	ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
2157	ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
2158
2159	ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
2160	ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
2161	ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
2162
2163	ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
2164	ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
2165	ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
2166
2167	ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
2168	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
2169	ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
2170	ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
2171
2172	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
2173	ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
2174	ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
2175	ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
2176	ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
2177	ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
2178	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
2179	ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
2180	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
2181	ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
2182
2183	ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
2184	ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
2185	ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
2186	ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
2187	ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
2188	ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
2189
2190	ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
2191	ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
2192	ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
2193	ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
2194	ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
2195	ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);
2196}
2197