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