xref: /freebsd/lib/libnv/tests/nvlist_exists_test.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2013 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Pawel Jakub Dawidek under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/nv.h>
31 
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 static int ntest = 1;
36 
37 #define	CHECK(expr)	do {						\
38 	if ((expr))							\
39 		printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__);	\
40 	else								\
41 		printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
42 	ntest++;							\
43 } while (0)
44 
45 int
46 main(void)
47 {
48 	nvlist_t *nvl;
49 
50 	printf("1..232\n");
51 
52 	nvl = nvlist_create(0);
53 
54 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
55 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
56 	CHECK(!nvlist_exists_bool(nvl, "nvlist/null"));
57 	CHECK(!nvlist_exists_number(nvl, "nvlist/null"));
58 	CHECK(!nvlist_exists_string(nvl, "nvlist/null"));
59 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/null"));
60 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/null"));
61 	CHECK(!nvlist_exists_binary(nvl, "nvlist/null"));
62 	nvlist_add_null(nvl, "nvlist/null");
63 	CHECK(nvlist_error(nvl) == 0);
64 	CHECK(nvlist_exists(nvl, "nvlist/null"));
65 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
66 	CHECK(!nvlist_exists_bool(nvl, "nvlist/null"));
67 	CHECK(!nvlist_exists_number(nvl, "nvlist/null"));
68 	CHECK(!nvlist_exists_string(nvl, "nvlist/null"));
69 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/null"));
70 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/null"));
71 	CHECK(!nvlist_exists_binary(nvl, "nvlist/null"));
72 
73 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
74 	CHECK(!nvlist_exists_null(nvl, "nvlist/bool"));
75 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
76 	CHECK(!nvlist_exists_number(nvl, "nvlist/bool"));
77 	CHECK(!nvlist_exists_string(nvl, "nvlist/bool"));
78 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/bool"));
79 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/bool"));
80 	CHECK(!nvlist_exists_binary(nvl, "nvlist/bool"));
81 	nvlist_add_bool(nvl, "nvlist/bool", true);
82 	CHECK(nvlist_error(nvl) == 0);
83 	CHECK(nvlist_exists(nvl, "nvlist/bool"));
84 	CHECK(!nvlist_exists_null(nvl, "nvlist/bool"));
85 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
86 	CHECK(!nvlist_exists_number(nvl, "nvlist/bool"));
87 	CHECK(!nvlist_exists_string(nvl, "nvlist/bool"));
88 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/bool"));
89 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/bool"));
90 	CHECK(!nvlist_exists_binary(nvl, "nvlist/bool"));
91 
92 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
93 	CHECK(!nvlist_exists_null(nvl, "nvlist/number"));
94 	CHECK(!nvlist_exists_bool(nvl, "nvlist/number"));
95 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
96 	CHECK(!nvlist_exists_string(nvl, "nvlist/number"));
97 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/number"));
98 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/number"));
99 	CHECK(!nvlist_exists_binary(nvl, "nvlist/number"));
100 	nvlist_add_number(nvl, "nvlist/number", 0);
101 	CHECK(nvlist_error(nvl) == 0);
102 	CHECK(nvlist_exists(nvl, "nvlist/number"));
103 	CHECK(!nvlist_exists_null(nvl, "nvlist/number"));
104 	CHECK(!nvlist_exists_bool(nvl, "nvlist/number"));
105 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
106 	CHECK(!nvlist_exists_string(nvl, "nvlist/number"));
107 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/number"));
108 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/number"));
109 	CHECK(!nvlist_exists_binary(nvl, "nvlist/number"));
110 
111 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
112 	CHECK(!nvlist_exists_null(nvl, "nvlist/string"));
113 	CHECK(!nvlist_exists_bool(nvl, "nvlist/string"));
114 	CHECK(!nvlist_exists_number(nvl, "nvlist/string"));
115 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
116 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/string"));
117 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/string"));
118 	CHECK(!nvlist_exists_binary(nvl, "nvlist/string"));
119 	nvlist_add_string(nvl, "nvlist/string", "test");
120 	CHECK(nvlist_error(nvl) == 0);
121 	CHECK(nvlist_exists(nvl, "nvlist/string"));
122 	CHECK(!nvlist_exists_null(nvl, "nvlist/string"));
123 	CHECK(!nvlist_exists_bool(nvl, "nvlist/string"));
124 	CHECK(!nvlist_exists_number(nvl, "nvlist/string"));
125 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
126 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/string"));
127 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/string"));
128 	CHECK(!nvlist_exists_binary(nvl, "nvlist/string"));
129 
130 	CHECK(!nvlist_exists(nvl, "nvlist/nvlist"));
131 	CHECK(!nvlist_exists_null(nvl, "nvlist/nvlist"));
132 	CHECK(!nvlist_exists_bool(nvl, "nvlist/nvlist"));
133 	CHECK(!nvlist_exists_number(nvl, "nvlist/nvlist"));
134 	CHECK(!nvlist_exists_string(nvl, "nvlist/nvlist"));
135 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
136 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/nvlist"));
137 	CHECK(!nvlist_exists_binary(nvl, "nvlist/nvlist"));
138 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
139 	CHECK(nvlist_error(nvl) == 0);
140 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
141 	CHECK(!nvlist_exists_null(nvl, "nvlist/nvlist"));
142 	CHECK(!nvlist_exists_bool(nvl, "nvlist/nvlist"));
143 	CHECK(!nvlist_exists_number(nvl, "nvlist/nvlist"));
144 	CHECK(!nvlist_exists_string(nvl, "nvlist/nvlist"));
145 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
146 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/nvlist"));
147 	CHECK(!nvlist_exists_binary(nvl, "nvlist/nvlist"));
148 
149 	CHECK(!nvlist_exists(nvl, "nvlist/descriptor"));
150 	CHECK(!nvlist_exists_null(nvl, "nvlist/descriptor"));
151 	CHECK(!nvlist_exists_bool(nvl, "nvlist/descriptor"));
152 	CHECK(!nvlist_exists_number(nvl, "nvlist/descriptor"));
153 	CHECK(!nvlist_exists_string(nvl, "nvlist/descriptor"));
154 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/descriptor"));
155 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
156 	CHECK(!nvlist_exists_binary(nvl, "nvlist/descriptor"));
157 	nvlist_add_descriptor(nvl, "nvlist/descriptor", STDERR_FILENO);
158 	CHECK(nvlist_error(nvl) == 0);
159 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
160 	CHECK(!nvlist_exists_null(nvl, "nvlist/descriptor"));
161 	CHECK(!nvlist_exists_bool(nvl, "nvlist/descriptor"));
162 	CHECK(!nvlist_exists_number(nvl, "nvlist/descriptor"));
163 	CHECK(!nvlist_exists_string(nvl, "nvlist/descriptor"));
164 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/descriptor"));
165 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
166 	CHECK(!nvlist_exists_binary(nvl, "nvlist/descriptor"));
167 
168 	CHECK(!nvlist_exists(nvl, "nvlist/binary"));
169 	CHECK(!nvlist_exists_null(nvl, "nvlist/binary"));
170 	CHECK(!nvlist_exists_bool(nvl, "nvlist/binary"));
171 	CHECK(!nvlist_exists_number(nvl, "nvlist/binary"));
172 	CHECK(!nvlist_exists_string(nvl, "nvlist/binary"));
173 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/binary"));
174 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/binary"));
175 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary"));
176 	nvlist_add_binary(nvl, "nvlist/binary", "test", 4);
177 	CHECK(nvlist_error(nvl) == 0);
178 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
179 	CHECK(!nvlist_exists_null(nvl, "nvlist/binary"));
180 	CHECK(!nvlist_exists_bool(nvl, "nvlist/binary"));
181 	CHECK(!nvlist_exists_number(nvl, "nvlist/binary"));
182 	CHECK(!nvlist_exists_string(nvl, "nvlist/binary"));
183 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/binary"));
184 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/binary"));
185 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
186 
187 	CHECK(nvlist_exists(nvl, "nvlist/null"));
188 	CHECK(nvlist_exists(nvl, "nvlist/bool"));
189 	CHECK(nvlist_exists(nvl, "nvlist/number"));
190 	CHECK(nvlist_exists(nvl, "nvlist/string"));
191 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
192 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
193 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
194 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
195 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
196 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
197 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
198 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
199 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
200 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
201 
202 	nvlist_free_null(nvl, "nvlist/null");
203 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
204 	CHECK(nvlist_exists(nvl, "nvlist/bool"));
205 	CHECK(nvlist_exists(nvl, "nvlist/number"));
206 	CHECK(nvlist_exists(nvl, "nvlist/string"));
207 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
208 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
209 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
210 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
211 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
212 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
213 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
214 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
215 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
216 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
217 
218 	nvlist_free_bool(nvl, "nvlist/bool");
219 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
220 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
221 	CHECK(nvlist_exists(nvl, "nvlist/number"));
222 	CHECK(nvlist_exists(nvl, "nvlist/string"));
223 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
224 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
225 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
226 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
227 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
228 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
229 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
230 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
231 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
232 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
233 
234 	nvlist_free_number(nvl, "nvlist/number");
235 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
236 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
237 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
238 	CHECK(nvlist_exists(nvl, "nvlist/string"));
239 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
240 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
241 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
242 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
243 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
244 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
245 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
246 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
247 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
248 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
249 
250 	nvlist_free_string(nvl, "nvlist/string");
251 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
252 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
253 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
254 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
255 	CHECK(nvlist_exists(nvl, "nvlist/nvlist"));
256 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
257 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
258 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
259 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
260 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
261 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
262 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
263 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
264 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
265 
266 	nvlist_free_nvlist(nvl, "nvlist/nvlist");
267 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
268 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
269 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
270 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
271 	CHECK(!nvlist_exists(nvl, "nvlist/nvlist"));
272 	CHECK(nvlist_exists(nvl, "nvlist/descriptor"));
273 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
274 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
275 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
276 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
277 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
278 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
279 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
280 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
281 
282 	nvlist_free_descriptor(nvl, "nvlist/descriptor");
283 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
284 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
285 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
286 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
287 	CHECK(!nvlist_exists(nvl, "nvlist/nvlist"));
288 	CHECK(!nvlist_exists(nvl, "nvlist/descriptor"));
289 	CHECK(nvlist_exists(nvl, "nvlist/binary"));
290 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
291 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
292 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
293 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
294 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
295 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
296 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
297 
298 	nvlist_free_binary(nvl, "nvlist/binary");
299 	CHECK(!nvlist_exists(nvl, "nvlist/null"));
300 	CHECK(!nvlist_exists(nvl, "nvlist/bool"));
301 	CHECK(!nvlist_exists(nvl, "nvlist/number"));
302 	CHECK(!nvlist_exists(nvl, "nvlist/string"));
303 	CHECK(!nvlist_exists(nvl, "nvlist/nvlist"));
304 	CHECK(!nvlist_exists(nvl, "nvlist/descriptor"));
305 	CHECK(!nvlist_exists(nvl, "nvlist/binary"));
306 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
307 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
308 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
309 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
310 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
311 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
312 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary"));
313 
314 	CHECK(nvlist_empty(nvl));
315 
316 	nvlist_destroy(nvl);
317 
318 	return (0);
319 }
320