2 * Copyright (c) 2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 #define CHECK(expr) do { \
43 printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__); \
45 printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
49 #define fd_is_valid(fd) (fcntl((fd), F_GETFL) != -1 || errno != EBADF)
60 nvl = nvlist_create(0);
62 CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/true"));
63 nvlist_add_bool(nvl, "nvlist/bool/true", true);
64 CHECK(nvlist_error(nvl) == 0);
65 CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true);
67 CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/false"));
68 nvlist_add_bool(nvl, "nvlist/bool/false", false);
69 CHECK(nvlist_error(nvl) == 0);
70 CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false);
72 CHECK(!nvlist_exists_number(nvl, "nvlist/number/0"));
73 nvlist_add_number(nvl, "nvlist/number/0", 0);
74 CHECK(nvlist_error(nvl) == 0);
75 CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0);
77 CHECK(!nvlist_exists_number(nvl, "nvlist/number/1"));
78 nvlist_add_number(nvl, "nvlist/number/1", 1);
79 CHECK(nvlist_error(nvl) == 0);
80 CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1);
82 CHECK(!nvlist_exists_number(nvl, "nvlist/number/-1"));
83 nvlist_add_number(nvl, "nvlist/number/-1", -1);
84 CHECK(nvlist_error(nvl) == 0);
85 CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1);
87 CHECK(!nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
88 nvlist_add_number(nvl, "nvlist/number/UINT64_MAX", UINT64_MAX);
89 CHECK(nvlist_error(nvl) == 0);
90 CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
92 CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
93 nvlist_add_number(nvl, "nvlist/number/INT64_MIN", INT64_MIN);
94 CHECK(nvlist_error(nvl) == 0);
95 CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN);
97 CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
98 nvlist_add_number(nvl, "nvlist/number/INT64_MAX", INT64_MAX);
99 CHECK(nvlist_error(nvl) == 0);
100 CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX);
102 CHECK(!nvlist_exists_string(nvl, "nvlist/string/"));
103 nvlist_add_string(nvl, "nvlist/string/", "");
104 CHECK(nvlist_error(nvl) == 0);
105 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0);
107 CHECK(!nvlist_exists_string(nvl, "nvlist/string/x"));
108 nvlist_add_string(nvl, "nvlist/string/x", "x");
109 CHECK(nvlist_error(nvl) == 0);
110 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0);
112 CHECK(!nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
113 nvlist_add_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
114 CHECK(nvlist_error(nvl) == 0);
115 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
117 CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
118 nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", STDERR_FILENO);
119 CHECK(nvlist_error(nvl) == 0);
120 CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")));
122 CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x"));
123 nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1);
124 CHECK(nvlist_error(nvl) == 0);
125 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0);
126 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0);
129 CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
130 nvlist_add_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz"));
131 CHECK(nvlist_error(nvl) == 0);
132 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
133 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
134 CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
136 CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
137 nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
138 CHECK(nvlist_error(nvl) == 0);
139 cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist");
140 CHECK(nvlist_get_bool(cnvl, "nvlist/bool/true") == true);
141 CHECK(nvlist_get_bool(cnvl, "nvlist/bool/false") == false);
142 CHECK(nvlist_get_number(cnvl, "nvlist/number/0") == 0);
143 CHECK(nvlist_get_number(cnvl, "nvlist/number/1") == 1);
144 CHECK((int)nvlist_get_number(cnvl, "nvlist/number/-1") == -1);
145 CHECK(nvlist_get_number(cnvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
146 CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MIN") == INT64_MIN);
147 CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MAX") == INT64_MAX);
148 CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/"), "") == 0);
149 CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/x"), "x") == 0);
150 CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
152 CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", NULL), "x", 1) == 0);
153 CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", &size), "x", 1) == 0);
155 CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
156 CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
157 CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
159 CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true);
160 CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false);
161 CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0);
162 CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1);
163 CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1);
164 CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
165 CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN);
166 CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX);
167 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0);
168 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0);
169 CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
170 CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")));
171 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0);
172 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0);
174 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
175 CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
176 CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));