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$");
29
30 #include <sys/nv.h>
31
32 #include <atf-c++.hpp>
33
34 #include <errno.h>
35 #include <limits>
36 #include <set>
37 #include <sstream>
38 #include <string>
39
40 /*
41 * Test that a newly created nvlist has no errors, and is empty.
42 */
43 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_create__is_empty);
ATF_TEST_CASE_BODY(nvlist_create__is_empty)44 ATF_TEST_CASE_BODY(nvlist_create__is_empty)
45 {
46 nvlist_t *nvl;
47 int type;
48 void *it;
49
50 nvl = nvlist_create(0);
51
52 ATF_REQUIRE(nvl != NULL);
53
54 ATF_REQUIRE_EQ(nvlist_error(nvl), 0);
55 ATF_REQUIRE(nvlist_empty(nvl));
56
57 it = NULL;
58 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), static_cast<const char *>(NULL));
59
60 nvlist_destroy(nvl);
61 }
62
63 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_null__single_insert);
ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)64 ATF_TEST_CASE_BODY(nvlist_add_null__single_insert)
65 {
66 nvlist_t *nvl;
67 void *it;
68 const char *key;
69 int type;
70
71 key = "key";
72 nvl = nvlist_create(0);
73
74 ATF_REQUIRE(nvl != NULL);
75 ATF_REQUIRE(!nvlist_exists(nvl, key));
76
77 nvlist_add_null(nvl, key);
78
79 ATF_REQUIRE(!nvlist_empty(nvl));
80 ATF_REQUIRE(nvlist_exists(nvl, key));
81 ATF_REQUIRE(nvlist_exists_null(nvl, key));
82 ATF_REQUIRE(nvlist_exists_null(nvl, "key"));
83
84 /* Iterate over the nvlist; ensure that it has only our one key. */
85 it = NULL;
86 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
87 ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
88 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
89
90 nvlist_destroy(nvl);
91 }
92
93 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_bool__single_insert);
ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)94 ATF_TEST_CASE_BODY(nvlist_add_bool__single_insert)
95 {
96 nvlist_t *nvl;
97 void *it;
98 const char *key;
99 int type;
100
101 key = "name";
102 nvl = nvlist_create(0);
103
104 ATF_REQUIRE(nvl != NULL);
105 ATF_REQUIRE(!nvlist_exists(nvl, key));
106
107 nvlist_add_bool(nvl, key, true);
108
109 ATF_REQUIRE(!nvlist_empty(nvl));
110 ATF_REQUIRE(nvlist_exists(nvl, key));
111 ATF_REQUIRE(nvlist_exists(nvl, "name"));
112 ATF_REQUIRE(nvlist_exists_bool(nvl, key));
113 ATF_REQUIRE(nvlist_exists_bool(nvl, "name"));
114 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, key), true);
115
116 /* Iterate over the nvlist; ensure that it has only our one key. */
117 it = NULL;
118 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
119 ATF_REQUIRE_EQ(type, NV_TYPE_BOOL);
120 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
121
122 nvlist_destroy(nvl);
123 }
124
125 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_number__single_insert);
ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)126 ATF_TEST_CASE_BODY(nvlist_add_number__single_insert)
127 {
128 nvlist_t *nvl;
129 void *it;
130 const char *key;
131 uint64_t value;
132 int type;
133
134 key = "foo123";
135 value = 71965;
136 nvl = nvlist_create(0);
137
138 ATF_REQUIRE(nvl != NULL);
139 ATF_REQUIRE(!nvlist_exists(nvl, key));
140
141 nvlist_add_number(nvl, key, value);
142
143 ATF_REQUIRE(!nvlist_empty(nvl));
144 ATF_REQUIRE(nvlist_exists(nvl, key));
145 ATF_REQUIRE(nvlist_exists(nvl, "foo123"));
146 ATF_REQUIRE(nvlist_exists_number(nvl, key));
147 ATF_REQUIRE_EQ(nvlist_get_number(nvl, key), value);
148
149 /* Iterate over the nvlist; ensure that it has only our one key. */
150 it = NULL;
151 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
152 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
153 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
154
155 nvlist_destroy(nvl);
156 }
157
158 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_string__single_insert);
ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)159 ATF_TEST_CASE_BODY(nvlist_add_string__single_insert)
160 {
161 nvlist_t *nvl;
162 void *it;
163 const char *key;
164 const char *value;
165 int type;
166
167 key = "test";
168 value = "fgjdkgjdk";
169 nvl = nvlist_create(0);
170
171 ATF_REQUIRE(nvl != NULL);
172 ATF_REQUIRE(!nvlist_exists(nvl, key));
173
174 nvlist_add_string(nvl, key, value);
175
176 ATF_REQUIRE(!nvlist_empty(nvl));
177 ATF_REQUIRE(nvlist_exists(nvl, key));
178 ATF_REQUIRE(nvlist_exists(nvl, "test"));
179 ATF_REQUIRE(nvlist_exists_string(nvl, key));
180 ATF_REQUIRE(nvlist_exists_string(nvl, "test"));
181 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, key), value), 0);
182
183 /* nvlist_add_* is required to clone the value, so check for that. */
184 ATF_REQUIRE(nvlist_get_string(nvl, key) != value);
185
186 /* Iterate over the nvlist; ensure that it has only our one key. */
187 it = NULL;
188 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
189 ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
190 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
191
192 nvlist_destroy(nvl);
193 }
194
195 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__single_insert);
ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)196 ATF_TEST_CASE_BODY(nvlist_add_nvlist__single_insert)
197 {
198 nvlist_t *nvl;
199 void *it;
200 const char *key, *subkey;
201 nvlist_t *sublist;
202 const nvlist_t *value;
203 int type;
204
205 key = "test";
206 subkey = "subkey";
207 sublist = nvlist_create(0);
208 nvl = nvlist_create(0);
209
210 ATF_REQUIRE(nvl != NULL);
211 ATF_REQUIRE(!nvlist_exists(nvl, key));
212
213 nvlist_add_null(sublist, subkey);
214 nvlist_add_nvlist(nvl, key, sublist);
215
216 ATF_REQUIRE(!nvlist_empty(nvl));
217 ATF_REQUIRE(nvlist_exists(nvl, key));
218 ATF_REQUIRE(nvlist_exists(nvl, "test"));
219 ATF_REQUIRE(nvlist_exists_nvlist(nvl, key));
220 ATF_REQUIRE(nvlist_exists_nvlist(nvl, "test"));
221
222 value = nvlist_get_nvlist(nvl, key);
223 ATF_REQUIRE(nvlist_exists_null(value, subkey));
224
225 /* nvlist_add_* is required to clone the value, so check for that. */
226 ATF_REQUIRE(sublist != value);
227
228 /* Iterate over the nvlist; ensure that it has only our one key. */
229 it = NULL;
230 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
231 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
232 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
233
234 nvlist_destroy(sublist);
235 nvlist_destroy(nvl);
236 }
237
238 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_nvlist__child_with_error);
ATF_TEST_CASE_BODY(nvlist_add_nvlist__child_with_error)239 ATF_TEST_CASE_BODY(nvlist_add_nvlist__child_with_error)
240 {
241 nvlist_t *nvl, *parent;
242
243 nvl = nvlist_create(0);
244 parent = nvlist_create(0);
245
246 nvlist_set_error(nvl, EBADF);
247 nvlist_add_nvlist(parent, "test", nvl);
248 ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
249
250 nvlist_destroy(nvl);
251 nvlist_destroy(parent);
252 }
253
254 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_add_binary__single_insert);
ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)255 ATF_TEST_CASE_BODY(nvlist_add_binary__single_insert)
256 {
257 nvlist_t *nvl;
258 void *it;
259 const char *key;
260 void *value;
261 const void *ret_value;
262 size_t value_size, ret_size;
263 int type;
264
265 key = "binary";
266 value_size = 13;
267 value = malloc(value_size);
268 memset(value, 0xa5, value_size);
269 nvl = nvlist_create(0);
270
271 ATF_REQUIRE(nvl != NULL);
272 ATF_REQUIRE(!nvlist_exists(nvl, key));
273
274 nvlist_add_binary(nvl, key, value, value_size);
275
276 ATF_REQUIRE(!nvlist_empty(nvl));
277 ATF_REQUIRE(nvlist_exists(nvl, key));
278 ATF_REQUIRE(nvlist_exists(nvl, "binary"));
279 ATF_REQUIRE(nvlist_exists_binary(nvl, key));
280 ATF_REQUIRE(nvlist_exists_binary(nvl, "binary"));
281
282 ret_value = nvlist_get_binary(nvl, key, &ret_size);
283 ATF_REQUIRE_EQ(value_size, ret_size);
284 ATF_REQUIRE_EQ(memcmp(value, ret_value, ret_size), 0);
285
286 /* nvlist_add_* is required to clone the value, so check for that. */
287 ATF_REQUIRE(value != ret_value);
288
289 /* Iterate over the nvlist; ensure that it has only our one key. */
290 it = NULL;
291 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it), key), 0);
292 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
293 ATF_REQUIRE_EQ(nvlist_next(nvl, &type,&it), static_cast<const char *>(NULL));
294
295 nvlist_destroy(nvl);
296 free(value);
297 }
298
299 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__empty_nvlist);
ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)300 ATF_TEST_CASE_BODY(nvlist_clone__empty_nvlist)
301 {
302 nvlist_t *nvl, *clone;
303
304 nvl = nvlist_create(0);
305 ATF_REQUIRE(nvl != NULL);
306
307 clone = nvlist_clone(nvl);
308 ATF_REQUIRE(clone != NULL);
309 ATF_REQUIRE(clone != nvl);
310 ATF_REQUIRE(nvlist_empty(clone));
311
312 nvlist_destroy(clone);
313 nvlist_destroy(nvl);
314 }
315
316 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nonempty_nvlist);
ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)317 ATF_TEST_CASE_BODY(nvlist_clone__nonempty_nvlist)
318 {
319 nvlist_t *nvl, *clone;
320 const char *key;
321 void *it;
322 uint64_t value;
323 int type;
324
325 nvl = nvlist_create(0);
326 ATF_REQUIRE(nvl != NULL);
327
328 key = "testkey";
329 value = 684874;
330 nvlist_add_number(nvl, key, value);
331
332 clone = nvlist_clone(nvl);
333 ATF_REQUIRE(clone != NULL);
334 ATF_REQUIRE(clone != nvl);
335 ATF_REQUIRE(nvlist_exists_number(clone, key));
336 ATF_REQUIRE_EQ(nvlist_get_number(clone, key), value);
337
338 /* Iterate over the nvlist; ensure that it has only our one key. */
339 it = NULL;
340 ATF_REQUIRE_EQ(strcmp(nvlist_next(clone, &type, &it), key), 0);
341 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
342 ATF_REQUIRE_EQ(nvlist_next(clone, &type, &it), static_cast<const char *>(NULL));
343
344 nvlist_destroy(clone);
345 nvlist_destroy(nvl);
346 }
347
348 static const char * const test_subnvlist_key = "nvlist";
349
350 static const char * const test_string_key = "string";
351 static const char * const test_string_val = "59525";
352
353 static nvlist_t*
create_test_nvlist(void)354 create_test_nvlist(void)
355 {
356 nvlist_t *nvl, *sublist;
357
358 nvl = nvlist_create(0);
359 ATF_REQUIRE(nvl != NULL);
360
361 sublist = nvlist_create(0);
362 ATF_REQUIRE(sublist != NULL);
363
364 nvlist_add_string(sublist, test_string_key, test_string_val);
365 nvlist_move_nvlist(nvl, test_subnvlist_key, sublist);
366
367 return (nvl);
368 }
369
370 static void
verify_test_nvlist(const nvlist_t * nvl)371 verify_test_nvlist(const nvlist_t *nvl)
372 {
373 void *it;
374 const nvlist_t *value;
375 int type;
376
377 ATF_REQUIRE(nvlist_exists_nvlist(nvl, test_subnvlist_key));
378
379 value = nvlist_get_nvlist(nvl, test_subnvlist_key);
380
381 ATF_REQUIRE(nvlist_exists_string(value, test_string_key));
382 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(value, test_string_key), test_string_val), 0);
383 ATF_REQUIRE(nvlist_get_string(value, test_string_key) != test_string_val);
384
385 /* Iterate over both nvlists; ensure that each has only the one key. */
386 it = NULL;
387 ATF_REQUIRE_EQ(strcmp(nvlist_next(value, &type, &it),
388 test_string_key), 0);
389 ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
390 ATF_REQUIRE_EQ(nvlist_next(value, &type, &it), static_cast<const char *>(NULL));
391
392 it = NULL;
393 ATF_REQUIRE_EQ(strcmp(nvlist_next(nvl, &type, &it),
394 test_subnvlist_key), 0);
395 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
396 ATF_REQUIRE_EQ(nvlist_next(nvl, &type, &it), static_cast<const char *>(NULL));
397 }
398
399 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__nested_nvlist);
ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)400 ATF_TEST_CASE_BODY(nvlist_clone__nested_nvlist)
401 {
402 nvlist_t *nvl, *clone;
403
404 nvl = create_test_nvlist();
405 clone = nvlist_clone(nvl);
406
407 ATF_REQUIRE(clone != NULL);
408 ATF_REQUIRE(clone != nvl);
409 verify_test_nvlist(clone);
410
411 nvlist_destroy(clone);
412 nvlist_destroy(nvl);
413 }
414
415 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_clone__error_nvlist);
ATF_TEST_CASE_BODY(nvlist_clone__error_nvlist)416 ATF_TEST_CASE_BODY(nvlist_clone__error_nvlist)
417 {
418 nvlist_t *nvl, *clone;
419
420 nvl = nvlist_create(0);
421 ATF_REQUIRE(nvl != NULL);
422
423 nvlist_set_error(nvl, ENOMEM);
424
425 clone = nvlist_clone(nvl);
426 ATF_REQUIRE(clone == NULL);
427
428 nvlist_destroy(nvl);
429 }
430
431 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__empty_nvlist);
ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)432 ATF_TEST_CASE_BODY(nvlist_pack__empty_nvlist)
433 {
434 nvlist_t *nvl, *unpacked;
435 void *packed;
436 size_t packed_size;
437
438 nvl = nvlist_create(0);
439 ATF_REQUIRE(nvl != NULL);
440
441 packed = nvlist_pack(nvl, &packed_size);
442 ATF_REQUIRE(packed != NULL);
443
444 unpacked = nvlist_unpack(packed, packed_size, 0);
445 ATF_REQUIRE(unpacked != NULL);
446 ATF_REQUIRE(unpacked != nvl);
447 ATF_REQUIRE(nvlist_empty(unpacked));
448
449 nvlist_destroy(unpacked);
450 nvlist_destroy(nvl);
451 free(packed);
452 }
453
454 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__flags_nvlist);
ATF_TEST_CASE_BODY(nvlist_unpack__flags_nvlist)455 ATF_TEST_CASE_BODY(nvlist_unpack__flags_nvlist)
456 {
457 nvlist_t *nvl, *unpacked;
458 void *packed;
459 size_t packed_size;
460
461 nvl = nvlist_create(NV_FLAG_NO_UNIQUE);
462 ATF_REQUIRE(nvl != NULL);
463
464 nvlist_add_bool(nvl, "name", true);
465 ATF_REQUIRE(!nvlist_empty(nvl));
466 ATF_REQUIRE(nvlist_exists_bool(nvl, "name"));
467
468 packed = nvlist_pack(nvl, &packed_size);
469 ATF_REQUIRE(packed != NULL);
470
471 unpacked = nvlist_unpack(packed, packed_size, 0);
472 ATF_REQUIRE(unpacked == NULL);
473
474 unpacked = nvlist_unpack(packed, packed_size, NV_FLAG_IGNORE_CASE);
475 ATF_REQUIRE(unpacked == NULL);
476
477 unpacked = nvlist_unpack(packed, packed_size, NV_FLAG_NO_UNIQUE);
478 ATF_REQUIRE(unpacked != NULL);
479 ATF_REQUIRE(unpacked != nvl);
480 ATF_REQUIRE(!nvlist_empty(unpacked));
481 ATF_REQUIRE(nvlist_exists_bool(unpacked, "name"));
482
483 nvlist_destroy(unpacked);
484 nvlist_destroy(nvl);
485 free(packed);
486 }
487
488 static void
verify_null(const nvlist_t * nvl,int type)489 verify_null(const nvlist_t *nvl, int type)
490 {
491
492 ATF_REQUIRE_EQ(type, NV_TYPE_NULL);
493 }
494
495 static void
verify_number(const nvlist_t * nvl,const char * name,int type,uint64_t value)496 verify_number(const nvlist_t *nvl, const char *name, int type, uint64_t value)
497 {
498
499 ATF_REQUIRE_EQ(type, NV_TYPE_NUMBER);
500 ATF_REQUIRE_EQ(nvlist_get_number(nvl, name), value);
501 }
502
503 static void
verify_string(const nvlist_t * nvl,const char * name,int type,const char * value)504 verify_string(const nvlist_t *nvl, const char *name, int type,
505 const char * value)
506 {
507
508 ATF_REQUIRE_EQ(type, NV_TYPE_STRING);
509 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, name), value), 0);
510 }
511
512 static void
verify_nvlist(const nvlist_t * nvl,const char * name,int type)513 verify_nvlist(const nvlist_t *nvl, const char *name, int type)
514 {
515
516 ATF_REQUIRE_EQ(type, NV_TYPE_NVLIST);
517 verify_test_nvlist(nvlist_get_nvlist(nvl, name));
518 }
519
520 static void
verify_binary(const nvlist_t * nvl,const char * name,int type,const void * value,size_t size)521 verify_binary(const nvlist_t *nvl, const char *name, int type,
522 const void * value, size_t size)
523 {
524 const void *actual_value;
525 size_t actual_size;
526
527 ATF_REQUIRE_EQ(type, NV_TYPE_BINARY);
528 actual_value = nvlist_get_binary(nvl, name, &actual_size);
529 ATF_REQUIRE_EQ(size, actual_size);
530 ATF_REQUIRE_EQ(memcmp(value, actual_value, size), 0);
531 }
532
533 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__multiple_values);
ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)534 ATF_TEST_CASE_BODY(nvlist_pack__multiple_values)
535 {
536 std::ostringstream msg;
537 std::set<std::string> keys_seen;
538 nvlist_t *nvl, *unpacked, *nvvalue;
539 const char *nullkey, *numkey, *strkey, *nvkey, *binkey, *name;
540 int numvalue;
541 const char * strvalue;
542 void *binvalue, *packed, *it;
543 size_t binsize, packed_size;
544 int type;
545
546 nvl = nvlist_create(0);
547
548 nullkey = "null";
549 nvlist_add_null(nvl, nullkey);
550
551 numkey = "number";
552 numvalue = 939853984;
553 nvlist_add_number(nvl, numkey, numvalue);
554
555 strkey = "string";
556 strvalue = "jfieutijf";
557 nvlist_add_string(nvl, strkey, strvalue);
558
559 nvkey = "nvlist";
560 nvvalue = create_test_nvlist();
561 nvlist_move_nvlist(nvl, nvkey, nvvalue);
562
563 binkey = "binary";
564 binsize = 4;
565 binvalue = malloc(binsize);
566 memset(binvalue, 'b', binsize);
567 nvlist_move_binary(nvl, binkey, binvalue, binsize);
568
569 packed = nvlist_pack(nvl, &packed_size);
570 ATF_REQUIRE(packed != NULL);
571
572 unpacked = nvlist_unpack(packed, packed_size, 0);
573 ATF_REQUIRE(unpacked != 0);
574
575 it = NULL;
576 while ((name = nvlist_next(unpacked, &type, &it)) != NULL) {
577 /* Ensure that we see every key only once. */
578 ATF_REQUIRE_EQ(keys_seen.count(name), 0);
579
580 if (strcmp(name, nullkey) == 0)
581 verify_null(unpacked, type);
582 else if (strcmp(name, numkey) == 0)
583 verify_number(unpacked, name, type, numvalue);
584 else if (strcmp(name, strkey) == 0)
585 verify_string(unpacked, name, type, strvalue);
586 else if (strcmp(name, nvkey) == 0)
587 verify_nvlist(unpacked, name, type);
588 else if (strcmp(name, binkey) == 0)
589 verify_binary(unpacked, name, type, binvalue, binsize);
590 else {
591 msg << "Unexpected key :'" << name << "'";
592 ATF_FAIL(msg.str().c_str());
593 }
594
595 keys_seen.insert(name);
596 }
597
598 /* Ensure that we saw every key. */
599 ATF_REQUIRE_EQ(keys_seen.size(), 5);
600
601 nvlist_destroy(nvl);
602 nvlist_destroy(unpacked);
603 free(packed);
604 }
605
606 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_pack__error_nvlist);
ATF_TEST_CASE_BODY(nvlist_pack__error_nvlist)607 ATF_TEST_CASE_BODY(nvlist_pack__error_nvlist)
608 {
609 nvlist_t *nvl;
610 void *packed;
611 size_t size;
612
613 nvl = nvlist_create(0);
614 ATF_REQUIRE(nvl != NULL);
615
616 nvlist_set_error(nvl, ENOMEM);
617
618 packed = nvlist_pack(nvl, &size);
619 ATF_REQUIRE(packed == NULL);
620
621 nvlist_destroy(nvl);
622 }
623
624 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_unpack__duplicate_key);
ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)625 ATF_TEST_CASE_BODY(nvlist_unpack__duplicate_key)
626 {
627 nvlist_t *nvl, *unpacked;
628 const char *key1, *key2;
629 void *packed, *keypos;
630 size_t size, keylen;
631
632 nvl = nvlist_create(0);
633
634 key1 = "key1";
635 keylen = strlen(key1);
636 nvlist_add_number(nvl, key1, 5);
637
638 key2 = "key2";
639 ATF_REQUIRE_EQ(keylen, strlen(key2));
640 nvlist_add_number(nvl, key2, 10);
641
642 packed = nvlist_pack(nvl, &size);
643 ATF_REQUIRE(packed != NULL);
644
645 /*
646 * Mangle the packed nvlist by replacing key1 with key2, creating a
647 * packed nvlist with a duplicate key.
648 */
649 keypos = memmem(packed, size, key1, keylen);
650 ATF_REQUIRE(keypos != NULL);
651 memcpy(keypos, key2, keylen);
652
653 unpacked = nvlist_unpack(packed, size, 0);
654 ATF_REQUIRE(nvlist_error(unpacked) != 0);
655
656 free(packed);
657 nvlist_destroy(nvl);
658 nvlist_destroy(unpacked);
659 }
660
661 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_string__single_insert);
ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)662 ATF_TEST_CASE_BODY(nvlist_move_string__single_insert)
663 {
664 nvlist_t *nvl;
665 const char *key;
666 char *value;
667
668 nvl = nvlist_create(0);
669 ATF_REQUIRE(nvl != NULL);
670
671 key = "testkey";
672 value = strdup("testval");
673 ATF_REQUIRE(value != NULL);
674
675 nvlist_move_string(nvl, key, value);
676 ATF_REQUIRE_EQ(nvlist_get_string(nvl, key), value);
677
678 nvlist_destroy(nvl);
679 }
680
681 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__null_child);
ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)682 ATF_TEST_CASE_BODY(nvlist_move_nvlist__null_child)
683 {
684 nvlist_t *parent;
685
686 parent = nvlist_create(0);
687
688 nvlist_move_nvlist(parent, "test", NULL);
689
690 ATF_REQUIRE(nvlist_error(parent) != 0);
691
692 nvlist_destroy(parent);
693 }
694
695 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__child_with_error);
ATF_TEST_CASE_BODY(nvlist_move_nvlist__child_with_error)696 ATF_TEST_CASE_BODY(nvlist_move_nvlist__child_with_error)
697 {
698 nvlist_t *nvl, *parent;
699
700 nvl = nvlist_create(0);
701 parent = nvlist_create(0);
702
703 nvlist_set_error(nvl, EBADF);
704 nvlist_move_nvlist(parent, "test", nvl);
705 ATF_REQUIRE_EQ(nvlist_error(parent), EBADF);
706
707 nvlist_destroy(parent);
708 }
709
710 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_nvlist__single_insert);
ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)711 ATF_TEST_CASE_BODY(nvlist_move_nvlist__single_insert)
712 {
713 nvlist_t *nvl;
714 const char *key;
715 nvlist_t *value;
716
717 nvl = nvlist_create(0);
718 ATF_REQUIRE(nvl != NULL);
719
720 key = "testkey";
721 value = nvlist_create(0);
722 ATF_REQUIRE(value != NULL);
723
724 nvlist_move_nvlist(nvl, key, value);
725 ATF_REQUIRE_EQ(nvlist_get_nvlist(nvl, key), value);
726
727 nvlist_destroy(nvl);
728 }
729
730 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_move_binary__single_insert);
ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)731 ATF_TEST_CASE_BODY(nvlist_move_binary__single_insert)
732 {
733 nvlist_t *nvl;
734 const char *key;
735 void *value;
736 size_t size, actual_size;
737
738 nvl = nvlist_create(0);
739 ATF_REQUIRE(nvl != NULL);
740
741 key = "testkey";
742 size = 73;
743 value = malloc(size);
744 ATF_REQUIRE(value != NULL);
745
746 nvlist_move_binary(nvl, key, value, size);
747 ATF_REQUIRE_EQ(nvlist_get_binary(nvl, key, &actual_size), value);
748 ATF_REQUIRE_EQ(size, actual_size);
749
750 nvlist_destroy(nvl);
751 }
752
753 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__single_remove);
ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)754 ATF_TEST_CASE_BODY(nvlist_take_bool__single_remove)
755 {
756 nvlist_t *nvl;
757 const char *testkey;
758 bool testval;
759
760 nvl = nvlist_create(0);
761 ATF_REQUIRE(nvl != NULL);
762
763 testkey = "boolkey";
764 testval = false;
765 nvlist_add_bool(nvl, testkey, testval);
766
767 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
768 ATF_REQUIRE(nvlist_empty(nvl));
769
770 nvlist_destroy(nvl);
771 }
772
773 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_bool__other_keys_unchanged);
ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)774 ATF_TEST_CASE_BODY(nvlist_take_bool__other_keys_unchanged)
775 {
776 nvlist_t *nvl;
777 const char *testkey, *otherkey1, *otherkey2;
778 bool testval, otherval1;
779 nvlist_t *otherval2;
780
781 nvl = nvlist_create(0);
782 ATF_REQUIRE(nvl != NULL);
783
784 testkey = "boolkey";
785 testval = true;
786 nvlist_add_bool(nvl, testkey, testval);
787
788 otherkey1 = "key1";
789 otherval1 = false;
790 nvlist_add_bool(nvl, otherkey1, otherval1);
791
792 otherkey2 = "key2";
793 otherval2 = create_test_nvlist();
794 nvlist_move_nvlist(nvl, otherkey2, otherval2);
795
796 ATF_REQUIRE_EQ(nvlist_take_bool(nvl, testkey), testval);
797
798 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey1));
799 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey1), otherval1);
800
801 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey2));
802 verify_test_nvlist(nvlist_get_nvlist(nvl, otherkey2));
803
804 nvlist_destroy(nvl);
805 }
806
807 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__single_remove);
ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)808 ATF_TEST_CASE_BODY(nvlist_take_number__single_remove)
809 {
810 nvlist_t *nvl;
811 const char *testkey;
812 uint64_t testval;
813
814 nvl = nvlist_create(0);
815 ATF_REQUIRE(nvl != NULL);
816
817 testkey = "numkey";
818 testval = std::numeric_limits<uint64_t>::max();
819 nvlist_add_number(nvl, testkey, testval);
820
821 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
822 ATF_REQUIRE(nvlist_empty(nvl));
823
824 nvlist_destroy(nvl);
825 }
826
827 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_number__other_keys_unchanged);
ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)828 ATF_TEST_CASE_BODY(nvlist_take_number__other_keys_unchanged)
829 {
830 nvlist_t *nvl;
831 const char *testkey, *otherkey1, *otherkey2;
832 uint64_t testval, otherval1;
833 const char *otherval2;
834
835 nvl = nvlist_create(0);
836 ATF_REQUIRE(nvl != NULL);
837
838 otherkey1 = "key1";
839 otherval1 = 5;
840 nvlist_add_number(nvl, otherkey1, otherval1);
841
842 testkey = "numkey";
843 testval = 1654;
844 nvlist_add_number(nvl, testkey, testval);
845
846 otherkey2 = "key2";
847 otherval2 = "string";
848 nvlist_add_string(nvl, otherkey2, otherval2);
849
850 ATF_REQUIRE_EQ(nvlist_take_number(nvl, testkey), testval);
851
852 ATF_REQUIRE(nvlist_exists_number(nvl, otherkey1));
853 ATF_REQUIRE_EQ(nvlist_get_number(nvl, otherkey1), otherval1);
854
855 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey2));
856 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey2), otherval2), 0);
857
858 nvlist_destroy(nvl);
859 }
860
861 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__single_remove);
ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)862 ATF_TEST_CASE_BODY(nvlist_take_string__single_remove)
863 {
864 nvlist_t *nvl;
865 const char *testkey;
866 const char *testval;
867
868 nvl = nvlist_create(0);
869 ATF_REQUIRE(nvl != NULL);
870
871 testkey = "numkey";
872 testval = "nvlist";
873 nvlist_add_string(nvl, testkey, testval);
874
875 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
876 ATF_REQUIRE(nvlist_empty(nvl));
877
878 nvlist_destroy(nvl);
879 }
880
881 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_string__other_keys_unchanged);
ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)882 ATF_TEST_CASE_BODY(nvlist_take_string__other_keys_unchanged)
883 {
884 nvlist_t *nvl;
885 const char *testkey, *otherkey1, *otherkey2;
886 const char *testval, *otherval1;
887 bool otherval2;
888
889 nvl = nvlist_create(0);
890 ATF_REQUIRE(nvl != NULL);
891
892 otherkey1 = "key1";
893 otherval1 = "fjdifjdk";
894 nvlist_add_string(nvl, otherkey1, otherval1);
895
896 otherkey2 = "key2";
897 otherval2 = true;
898 nvlist_add_bool(nvl, otherkey2, otherval2);
899
900 testkey = "strkey";
901 testval = "1654";
902 nvlist_add_string(nvl, testkey, testval);
903
904 ATF_REQUIRE_EQ(strcmp(nvlist_take_string(nvl, testkey), testval), 0);
905
906 ATF_REQUIRE(nvlist_exists_string(nvl, otherkey1));
907 ATF_REQUIRE_EQ(strcmp(nvlist_get_string(nvl, otherkey1), otherval1), 0);
908
909 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
910 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
911
912 nvlist_destroy(nvl);
913 }
914
915 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__single_remove);
ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)916 ATF_TEST_CASE_BODY(nvlist_take_nvlist__single_remove)
917 {
918 nvlist_t *nvl;
919 const char *testkey;
920 nvlist_t *testval;
921
922 nvl = nvlist_create(0);
923 ATF_REQUIRE(nvl != NULL);
924
925 testkey = "numkey";
926 testval = create_test_nvlist();
927 nvlist_move_nvlist(nvl, testkey, testval);
928
929 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
930 ATF_REQUIRE(nvlist_empty(nvl));
931
932 nvlist_destroy(nvl);
933 }
934
935 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_nvlist__other_keys_unchanged);
ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)936 ATF_TEST_CASE_BODY(nvlist_take_nvlist__other_keys_unchanged)
937 {
938 nvlist_t *nvl;
939 const char *testkey, *otherkey1, *otherkey2;
940 nvlist_t *testval, *otherval1;
941
942 nvl = nvlist_create(0);
943 ATF_REQUIRE(nvl != NULL);
944
945 testkey = "strkey";
946 testval = create_test_nvlist();
947 nvlist_move_nvlist(nvl, testkey, testval);
948
949 otherkey1 = "key1";
950 otherval1 = nvlist_create(0);
951 nvlist_move_nvlist(nvl, otherkey1, otherval1);
952
953 otherkey2 = "key2";
954 nvlist_add_null(nvl, otherkey2);
955
956 verify_test_nvlist(nvlist_take_nvlist(nvl, testkey));
957
958 ATF_REQUIRE(nvlist_exists_nvlist(nvl, otherkey1));
959 ATF_REQUIRE(nvlist_empty(nvlist_get_nvlist(nvl, otherkey1)));
960
961 ATF_REQUIRE(nvlist_exists_null(nvl, otherkey2));
962
963 nvlist_destroy(nvl);
964 }
965
966 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__single_remove);
ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)967 ATF_TEST_CASE_BODY(nvlist_take_binary__single_remove)
968 {
969 nvlist_t *nvl;
970 const char *testkey;
971 void *testval;
972 const void *actual_val;
973 size_t testsize, actual_size;
974
975 nvl = nvlist_create(0);
976 ATF_REQUIRE(nvl != NULL);
977
978 testkey = "numkey";
979 testsize = 457;
980 testval = malloc(testsize);
981 memset(testval, '5', testsize);
982 nvlist_move_binary(nvl, testkey, testval, testsize);
983
984 actual_val = nvlist_take_binary(nvl, testkey, &actual_size);
985 ATF_REQUIRE_EQ(testsize, actual_size);
986 ATF_REQUIRE_EQ(memcmp(actual_val, testval, testsize), 0);
987 ATF_REQUIRE(nvlist_empty(nvl));
988
989 nvlist_destroy(nvl);
990 }
991
992 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_take_binary__other_keys_unchanged);
ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)993 ATF_TEST_CASE_BODY(nvlist_take_binary__other_keys_unchanged)
994 {
995 nvlist_t *nvl;
996 const char *testkey, *otherkey1, *otherkey2;
997 const void *actual_value;
998 char testval[] = "gjiertj";
999 char otherval1[] = "fdreg";
1000 size_t testsize, othersize, actual_size;
1001 bool otherval2;
1002
1003 nvl = nvlist_create(0);
1004 ATF_REQUIRE(nvl != NULL);
1005
1006 otherkey1 = "key1";
1007 othersize = sizeof(otherval1);
1008 nvlist_add_binary(nvl, otherkey1, otherval1, othersize);
1009
1010 otherkey2 = "key2";
1011 otherval2 = true;
1012 nvlist_add_bool(nvl, otherkey2, otherval2);
1013
1014 testkey = "strkey";
1015 testsize = sizeof(testval);
1016 nvlist_add_binary(nvl, testkey, testval, testsize);
1017
1018 actual_value = nvlist_take_binary(nvl, testkey, &actual_size);
1019 ATF_REQUIRE_EQ(testsize, actual_size);
1020 ATF_REQUIRE_EQ(memcmp(actual_value, testval, testsize), 0);
1021
1022 ATF_REQUIRE(nvlist_exists_binary(nvl, otherkey1));
1023 actual_value = nvlist_get_binary(nvl, otherkey1, &actual_size);
1024 ATF_REQUIRE_EQ(othersize, actual_size);
1025 ATF_REQUIRE_EQ(memcmp(actual_value, otherval1, othersize), 0);
1026
1027 ATF_REQUIRE(nvlist_exists_bool(nvl, otherkey2));
1028 ATF_REQUIRE_EQ(nvlist_get_bool(nvl, otherkey2), otherval2);
1029
1030 nvlist_destroy(nvl);
1031 }
1032
1033 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_null);
ATF_TEST_CASE_BODY(nvlist_free__single_null)1034 ATF_TEST_CASE_BODY(nvlist_free__single_null)
1035 {
1036 nvlist_t *nvl;
1037 const char *key;
1038
1039 nvl = nvlist_create(0);
1040 key = "test";
1041 nvlist_add_null(nvl, key);
1042
1043 nvlist_free(nvl, key);
1044 ATF_REQUIRE(nvlist_empty(nvl));
1045
1046 nvlist_destroy(nvl);
1047 }
1048
1049 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_bool);
ATF_TEST_CASE_BODY(nvlist_free__single_bool)1050 ATF_TEST_CASE_BODY(nvlist_free__single_bool)
1051 {
1052 nvlist_t *nvl;
1053 const char *key;
1054
1055 nvl = nvlist_create(0);
1056 key = "test";
1057 nvlist_add_bool(nvl, key, true);
1058
1059 nvlist_free(nvl, key);
1060 ATF_REQUIRE(nvlist_empty(nvl));
1061
1062 nvlist_destroy(nvl);
1063 }
1064
1065 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_number);
ATF_TEST_CASE_BODY(nvlist_free__single_number)1066 ATF_TEST_CASE_BODY(nvlist_free__single_number)
1067 {
1068 nvlist_t *nvl;
1069 const char *key;
1070
1071 nvl = nvlist_create(0);
1072 key = "test";
1073 nvlist_add_number(nvl, key, 584);
1074
1075 nvlist_free(nvl, key);
1076 ATF_REQUIRE(nvlist_empty(nvl));
1077
1078 nvlist_destroy(nvl);
1079 }
1080
1081 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_string);
ATF_TEST_CASE_BODY(nvlist_free__single_string)1082 ATF_TEST_CASE_BODY(nvlist_free__single_string)
1083 {
1084 nvlist_t *nvl;
1085 const char *key;
1086
1087 nvl = nvlist_create(0);
1088 key = "test";
1089 nvlist_add_string(nvl, key, "gjkfkjd");
1090
1091 nvlist_free(nvl, key);
1092 ATF_REQUIRE(nvlist_empty(nvl));
1093
1094 nvlist_destroy(nvl);
1095 }
1096
1097 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_nvlist);
ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)1098 ATF_TEST_CASE_BODY(nvlist_free__single_nvlist)
1099 {
1100 nvlist_t *nvl;
1101 const char *key;
1102
1103 nvl = nvlist_create(0);
1104 key = "test";
1105 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1106
1107 nvlist_free(nvl, key);
1108 ATF_REQUIRE(nvlist_empty(nvl));
1109
1110 nvlist_destroy(nvl);
1111 }
1112
1113 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free__single_binary);
ATF_TEST_CASE_BODY(nvlist_free__single_binary)1114 ATF_TEST_CASE_BODY(nvlist_free__single_binary)
1115 {
1116 nvlist_t *nvl;
1117 const char *key;
1118
1119 nvl = nvlist_create(0);
1120 key = "test";
1121 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1122
1123 nvlist_free(nvl, key);
1124 ATF_REQUIRE(nvlist_empty(nvl));
1125
1126 nvlist_destroy(nvl);
1127 }
1128
1129 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_null__single_null);
ATF_TEST_CASE_BODY(nvlist_free_null__single_null)1130 ATF_TEST_CASE_BODY(nvlist_free_null__single_null)
1131 {
1132 nvlist_t *nvl;
1133 const char *key;
1134
1135 nvl = nvlist_create(0);
1136 key = "test";
1137 nvlist_add_null(nvl, key);
1138
1139 nvlist_free_null(nvl, key);
1140 ATF_REQUIRE(nvlist_empty(nvl));
1141
1142 nvlist_destroy(nvl);
1143 }
1144
1145 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_bool__single_bool);
ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)1146 ATF_TEST_CASE_BODY(nvlist_free_bool__single_bool)
1147 {
1148 nvlist_t *nvl;
1149 const char *key;
1150
1151 nvl = nvlist_create(0);
1152 key = "test";
1153 nvlist_add_bool(nvl, key, true);
1154
1155 nvlist_free_bool(nvl, key);
1156 ATF_REQUIRE(nvlist_empty(nvl));
1157
1158 nvlist_destroy(nvl);
1159 }
1160
1161 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_number__single_number);
ATF_TEST_CASE_BODY(nvlist_free_number__single_number)1162 ATF_TEST_CASE_BODY(nvlist_free_number__single_number)
1163 {
1164 nvlist_t *nvl;
1165 const char *key;
1166
1167 nvl = nvlist_create(0);
1168 key = "test";
1169 nvlist_add_number(nvl, key, 584);
1170
1171 nvlist_free_number(nvl, key);
1172 ATF_REQUIRE(nvlist_empty(nvl));
1173
1174 nvlist_destroy(nvl);
1175 }
1176
1177 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_string__single_string);
ATF_TEST_CASE_BODY(nvlist_free_string__single_string)1178 ATF_TEST_CASE_BODY(nvlist_free_string__single_string)
1179 {
1180 nvlist_t *nvl;
1181 const char *key;
1182
1183 nvl = nvlist_create(0);
1184 key = "test";
1185 nvlist_add_string(nvl, key, "gjkfkjd");
1186
1187 nvlist_free_string(nvl, key);
1188 ATF_REQUIRE(nvlist_empty(nvl));
1189
1190 nvlist_destroy(nvl);
1191 }
1192
1193 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_nvlist__single_nvlist);
ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)1194 ATF_TEST_CASE_BODY(nvlist_free_nvlist__single_nvlist)
1195 {
1196 nvlist_t *nvl;
1197 const char *key;
1198
1199 nvl = nvlist_create(0);
1200 key = "test";
1201 nvlist_add_nvlist(nvl, key, nvlist_create(0));
1202
1203 nvlist_free_nvlist(nvl, key);
1204 ATF_REQUIRE(nvlist_empty(nvl));
1205
1206 nvlist_destroy(nvl);
1207 }
1208
1209 ATF_TEST_CASE_WITHOUT_HEAD(nvlist_free_binary__single_binary);
ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)1210 ATF_TEST_CASE_BODY(nvlist_free_binary__single_binary)
1211 {
1212 nvlist_t *nvl;
1213 const char *key;
1214
1215 nvl = nvlist_create(0);
1216 key = "test";
1217 nvlist_add_binary(nvl, key, "jgjgfd", 6);
1218
1219 nvlist_free_binary(nvl, key);
1220 ATF_REQUIRE(nvlist_empty(nvl));
1221
1222 nvlist_destroy(nvl);
1223 }
1224
ATF_INIT_TEST_CASES(tp)1225 ATF_INIT_TEST_CASES(tp)
1226 {
1227 ATF_ADD_TEST_CASE(tp, nvlist_create__is_empty);
1228 ATF_ADD_TEST_CASE(tp, nvlist_add_null__single_insert);
1229 ATF_ADD_TEST_CASE(tp, nvlist_add_bool__single_insert);
1230 ATF_ADD_TEST_CASE(tp, nvlist_add_number__single_insert);
1231 ATF_ADD_TEST_CASE(tp, nvlist_add_string__single_insert);
1232 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__single_insert);
1233 ATF_ADD_TEST_CASE(tp, nvlist_add_nvlist__child_with_error);
1234 ATF_ADD_TEST_CASE(tp, nvlist_add_binary__single_insert);
1235
1236 ATF_ADD_TEST_CASE(tp, nvlist_clone__empty_nvlist);
1237 ATF_ADD_TEST_CASE(tp, nvlist_clone__nonempty_nvlist);
1238 ATF_ADD_TEST_CASE(tp, nvlist_clone__nested_nvlist);
1239 ATF_ADD_TEST_CASE(tp, nvlist_clone__error_nvlist);
1240
1241 ATF_ADD_TEST_CASE(tp, nvlist_pack__empty_nvlist);
1242 ATF_ADD_TEST_CASE(tp, nvlist_pack__multiple_values);
1243 ATF_ADD_TEST_CASE(tp, nvlist_pack__error_nvlist);
1244 ATF_ADD_TEST_CASE(tp, nvlist_unpack__duplicate_key);
1245 ATF_ADD_TEST_CASE(tp, nvlist_unpack__flags_nvlist);
1246
1247 ATF_ADD_TEST_CASE(tp, nvlist_move_string__single_insert);
1248 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__single_insert);
1249 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__null_child);
1250 ATF_ADD_TEST_CASE(tp, nvlist_move_nvlist__child_with_error);
1251 ATF_ADD_TEST_CASE(tp, nvlist_move_binary__single_insert);
1252
1253 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__single_remove);
1254 ATF_ADD_TEST_CASE(tp, nvlist_take_bool__other_keys_unchanged);
1255 ATF_ADD_TEST_CASE(tp, nvlist_take_number__single_remove);
1256 ATF_ADD_TEST_CASE(tp, nvlist_take_number__other_keys_unchanged);
1257 ATF_ADD_TEST_CASE(tp, nvlist_take_string__single_remove);
1258 ATF_ADD_TEST_CASE(tp, nvlist_take_string__other_keys_unchanged);
1259 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__single_remove);
1260 ATF_ADD_TEST_CASE(tp, nvlist_take_nvlist__other_keys_unchanged);
1261 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__single_remove);
1262 ATF_ADD_TEST_CASE(tp, nvlist_take_binary__other_keys_unchanged);
1263
1264 ATF_ADD_TEST_CASE(tp, nvlist_free__single_null);
1265 ATF_ADD_TEST_CASE(tp, nvlist_free__single_bool);
1266 ATF_ADD_TEST_CASE(tp, nvlist_free__single_number);
1267 ATF_ADD_TEST_CASE(tp, nvlist_free__single_string);
1268 ATF_ADD_TEST_CASE(tp, nvlist_free__single_nvlist);
1269 ATF_ADD_TEST_CASE(tp, nvlist_free__single_binary);
1270
1271 ATF_ADD_TEST_CASE(tp, nvlist_free_null__single_null);
1272 ATF_ADD_TEST_CASE(tp, nvlist_free_bool__single_bool);
1273 ATF_ADD_TEST_CASE(tp, nvlist_free_number__single_number);
1274 ATF_ADD_TEST_CASE(tp, nvlist_free_string__single_string);
1275 ATF_ADD_TEST_CASE(tp, nvlist_free_nvlist__single_nvlist);
1276 ATF_ADD_TEST_CASE(tp, nvlist_free_binary__single_binary);
1277 }
1278