xref: /freebsd/lib/libnv/tests/nvlist_exists_test.c (revision 5c2bc3db201a4fe8d7911cf816bea104d5dc2138)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  *
4  * This software was developed by Pawel Jakub Dawidek under sponsorship from
5  * the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/nv.h>
30 
31 #include <stdio.h>
32 #include <unistd.h>
33 
34 static int ntest = 1;
35 
36 #define	CHECK(expr)	do {						\
37 	if ((expr))							\
38 		printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__);	\
39 	else								\
40 		printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
41 	ntest++;							\
42 } while (0)
43 
44 int
45 main(void)
46 {
47 	nvlist_t *nvl;
48 
49 	printf("1..232\n");
50 
51 	nvl = nvlist_create(0);
52 
53 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
54 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
55 	CHECK(!nvlist_exists_bool(nvl, "nvlist/null"));
56 	CHECK(!nvlist_exists_number(nvl, "nvlist/null"));
57 	CHECK(!nvlist_exists_string(nvl, "nvlist/null"));
58 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/null"));
59 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/null"));
60 	CHECK(!nvlist_exists_binary(nvl, "nvlist/null"));
61 	nvlist_add_null(nvl, "nvlist/null");
62 	CHECK(nvlist_error(nvl) == 0);
63 	CHECK(nvlist_exists(nvl, "nvlist/null"));
64 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
65 	CHECK(!nvlist_exists_bool(nvl, "nvlist/null"));
66 	CHECK(!nvlist_exists_number(nvl, "nvlist/null"));
67 	CHECK(!nvlist_exists_string(nvl, "nvlist/null"));
68 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/null"));
69 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/null"));
70 	CHECK(!nvlist_exists_binary(nvl, "nvlist/null"));
71 
72 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
73 	CHECK(!nvlist_exists_null(nvl, "nvlist/bool"));
74 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
75 	CHECK(!nvlist_exists_number(nvl, "nvlist/bool"));
76 	CHECK(!nvlist_exists_string(nvl, "nvlist/bool"));
77 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/bool"));
78 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/bool"));
79 	CHECK(!nvlist_exists_binary(nvl, "nvlist/bool"));
80 	nvlist_add_bool(nvl, "nvlist/bool", true);
81 	CHECK(nvlist_error(nvl) == 0);
82 	CHECK(nvlist_exists(nvl, "nvlist/bool"));
83 	CHECK(!nvlist_exists_null(nvl, "nvlist/bool"));
84 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
85 	CHECK(!nvlist_exists_number(nvl, "nvlist/bool"));
86 	CHECK(!nvlist_exists_string(nvl, "nvlist/bool"));
87 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/bool"));
88 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/bool"));
89 	CHECK(!nvlist_exists_binary(nvl, "nvlist/bool"));
90 
91 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
92 	CHECK(!nvlist_exists_null(nvl, "nvlist/number"));
93 	CHECK(!nvlist_exists_bool(nvl, "nvlist/number"));
94 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
95 	CHECK(!nvlist_exists_string(nvl, "nvlist/number"));
96 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/number"));
97 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/number"));
98 	CHECK(!nvlist_exists_binary(nvl, "nvlist/number"));
99 	nvlist_add_number(nvl, "nvlist/number", 0);
100 	CHECK(nvlist_error(nvl) == 0);
101 	CHECK(nvlist_exists(nvl, "nvlist/number"));
102 	CHECK(!nvlist_exists_null(nvl, "nvlist/number"));
103 	CHECK(!nvlist_exists_bool(nvl, "nvlist/number"));
104 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
105 	CHECK(!nvlist_exists_string(nvl, "nvlist/number"));
106 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/number"));
107 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/number"));
108 	CHECK(!nvlist_exists_binary(nvl, "nvlist/number"));
109 
110 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
111 	CHECK(!nvlist_exists_null(nvl, "nvlist/string"));
112 	CHECK(!nvlist_exists_bool(nvl, "nvlist/string"));
113 	CHECK(!nvlist_exists_number(nvl, "nvlist/string"));
114 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
115 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/string"));
116 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/string"));
117 	CHECK(!nvlist_exists_binary(nvl, "nvlist/string"));
118 	nvlist_add_string(nvl, "nvlist/string", "test");
119 	CHECK(nvlist_error(nvl) == 0);
120 	CHECK(nvlist_exists(nvl, "nvlist/string"));
121 	CHECK(!nvlist_exists_null(nvl, "nvlist/string"));
122 	CHECK(!nvlist_exists_bool(nvl, "nvlist/string"));
123 	CHECK(!nvlist_exists_number(nvl, "nvlist/string"));
124 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
125 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/string"));
126 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/string"));
127 	CHECK(!nvlist_exists_binary(nvl, "nvlist/string"));
128 
129 	CHECK(!nvlist_exists(nvl, "nvlist/nvlist"));
130 	CHECK(!nvlist_exists_null(nvl, "nvlist/nvlist"));
131 	CHECK(!nvlist_exists_bool(nvl, "nvlist/nvlist"));
132 	CHECK(!nvlist_exists_number(nvl, "nvlist/nvlist"));
133 	CHECK(!nvlist_exists_string(nvl, "nvlist/nvlist"));
134 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
135 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/nvlist"));
136 	CHECK(!nvlist_exists_binary(nvl, "nvlist/nvlist"));
137 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
138 	CHECK(nvlist_error(nvl) == 0);
139 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
140 	CHECK(!nvlist_exists_null(nvl, "nvlist/nvlist"));
141 	CHECK(!nvlist_exists_bool(nvl, "nvlist/nvlist"));
142 	CHECK(!nvlist_exists_number(nvl, "nvlist/nvlist"));
143 	CHECK(!nvlist_exists_string(nvl, "nvlist/nvlist"));
144 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
145 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/nvlist"));
146 	CHECK(!nvlist_exists_binary(nvl, "nvlist/nvlist"));
147 
148 	CHECK(!nvlist_exists(nvl, "nvlist/descriptor"));
149 	CHECK(!nvlist_exists_null(nvl, "nvlist/descriptor"));
150 	CHECK(!nvlist_exists_bool(nvl, "nvlist/descriptor"));
151 	CHECK(!nvlist_exists_number(nvl, "nvlist/descriptor"));
152 	CHECK(!nvlist_exists_string(nvl, "nvlist/descriptor"));
153 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/descriptor"));
154 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
155 	CHECK(!nvlist_exists_binary(nvl, "nvlist/descriptor"));
156 	nvlist_add_descriptor(nvl, "nvlist/descriptor", STDERR_FILENO);
157 	CHECK(nvlist_error(nvl) == 0);
158 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
159 	CHECK(!nvlist_exists_null(nvl, "nvlist/descriptor"));
160 	CHECK(!nvlist_exists_bool(nvl, "nvlist/descriptor"));
161 	CHECK(!nvlist_exists_number(nvl, "nvlist/descriptor"));
162 	CHECK(!nvlist_exists_string(nvl, "nvlist/descriptor"));
163 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/descriptor"));
164 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
165 	CHECK(!nvlist_exists_binary(nvl, "nvlist/descriptor"));
166 
167 	CHECK(!nvlist_exists(nvl, "nvlist/binary"));
168 	CHECK(!nvlist_exists_null(nvl, "nvlist/binary"));
169 	CHECK(!nvlist_exists_bool(nvl, "nvlist/binary"));
170 	CHECK(!nvlist_exists_number(nvl, "nvlist/binary"));
171 	CHECK(!nvlist_exists_string(nvl, "nvlist/binary"));
172 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/binary"));
173 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/binary"));
174 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary"));
175 	nvlist_add_binary(nvl, "nvlist/binary", "test", 4);
176 	CHECK(nvlist_error(nvl) == 0);
177 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
178 	CHECK(!nvlist_exists_null(nvl, "nvlist/binary"));
179 	CHECK(!nvlist_exists_bool(nvl, "nvlist/binary"));
180 	CHECK(!nvlist_exists_number(nvl, "nvlist/binary"));
181 	CHECK(!nvlist_exists_string(nvl, "nvlist/binary"));
182 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/binary"));
183 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/binary"));
184 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
185 
186 	CHECK(nvlist_exists(nvl, "nvlist/null"));
187 	CHECK(nvlist_exists(nvl, "nvlist/bool"));
188 	CHECK(nvlist_exists(nvl, "nvlist/number"));
189 	CHECK(nvlist_exists(nvl, "nvlist/string"));
190 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
191 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
192 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
193 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
194 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
195 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
196 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
197 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
198 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
199 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
200 
201 	nvlist_free_null(nvl, "nvlist/null");
202 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
203 	CHECK(nvlist_exists(nvl, "nvlist/bool"));
204 	CHECK(nvlist_exists(nvl, "nvlist/number"));
205 	CHECK(nvlist_exists(nvl, "nvlist/string"));
206 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
207 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
208 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
209 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
210 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
211 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
212 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
213 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
214 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
215 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
216 
217 	nvlist_free_bool(nvl, "nvlist/bool");
218 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
219 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
220 	CHECK(nvlist_exists(nvl, "nvlist/number"));
221 	CHECK(nvlist_exists(nvl, "nvlist/string"));
222 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
223 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
224 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
225 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
226 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
227 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
228 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
229 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
230 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
231 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
232 
233 	nvlist_free_number(nvl, "nvlist/number");
234 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
235 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
236 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
237 	CHECK(nvlist_exists(nvl, "nvlist/string"));
238 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
239 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
240 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
241 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
242 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
243 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
244 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
245 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
246 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
247 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
248 
249 	nvlist_free_string(nvl, "nvlist/string");
250 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
251 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
252 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
253 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
254 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
255 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
256 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
257 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
258 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
259 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
260 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
261 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
262 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
263 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
264 
265 	nvlist_free_nvlist(nvl, "nvlist/nvlist");
266 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
267 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
268 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
269 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
270 	CHECK(!nvlist_exists(nvl, "nvlist/nvlist"));
271 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
272 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
273 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
274 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
275 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
276 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
277 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
278 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
279 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
280 
281 	nvlist_free_descriptor(nvl, "nvlist/descriptor");
282 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
283 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
284 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
285 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
286 	CHECK(!nvlist_exists(nvl, "nvlist/nvlist"));
287 	CHECK(!nvlist_exists(nvl, "nvlist/descriptor"));
288 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
289 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
290 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
291 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
292 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
293 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
294 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
295 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
296 
297 	nvlist_free_binary(nvl, "nvlist/binary");
298 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
299 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
300 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
301 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
302 	CHECK(!nvlist_exists(nvl, "nvlist/nvlist"));
303 	CHECK(!nvlist_exists(nvl, "nvlist/descriptor"));
304 	CHECK(!nvlist_exists(nvl, "nvlist/binary"));
305 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
306 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
307 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
308 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
309 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
310 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
311 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary"));
312 
313 	CHECK(nvlist_empty(nvl));
314 
315 	nvlist_destroy(nvl);
316 
317 	return (0);
318 }
319