xref: /freebsd/lib/libnv/tests/nvlist_free_test.c (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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..114\n");
51 
52 	nvl = nvlist_create(0);
53 
54 	nvlist_add_null(nvl, "nvlist/null");
55 	nvlist_add_bool(nvl, "nvlist/bool", true);
56 	nvlist_add_number(nvl, "nvlist/number", 0);
57 	nvlist_add_string(nvl, "nvlist/string", "test");
58 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
59 	nvlist_add_descriptor(nvl, "nvlist/descriptor", STDERR_FILENO);
60 	nvlist_add_binary(nvl, "nvlist/binary", "test", 4);
61 
62 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
63 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
64 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
65 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
66 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
67 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
68 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
69 
70 	nvlist_free_null(nvl, "nvlist/null");
71 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
72 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
73 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
74 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
75 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
76 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
77 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
78 
79 	nvlist_free_bool(nvl, "nvlist/bool");
80 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
81 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
82 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
83 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
84 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
85 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
86 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
87 
88 	nvlist_free_number(nvl, "nvlist/number");
89 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
90 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
91 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
92 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
93 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
94 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
95 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
96 
97 	nvlist_free_string(nvl, "nvlist/string");
98 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
99 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
100 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
101 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
102 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
103 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
104 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
105 
106 	nvlist_free_nvlist(nvl, "nvlist/nvlist");
107 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
108 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
109 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
110 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
111 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
112 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
113 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
114 
115 	nvlist_free_descriptor(nvl, "nvlist/descriptor");
116 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
117 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
118 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
119 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
120 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
121 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
122 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
123 
124 	nvlist_free_binary(nvl, "nvlist/binary");
125 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
126 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
127 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
128 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
129 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
130 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
131 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary"));
132 
133 	CHECK(nvlist_empty(nvl));
134 
135 	nvlist_add_null(nvl, "nvlist/null");
136 	nvlist_add_bool(nvl, "nvlist/bool", true);
137 	nvlist_add_number(nvl, "nvlist/number", 0);
138 	nvlist_add_string(nvl, "nvlist/string", "test");
139 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
140 	nvlist_add_descriptor(nvl, "nvlist/descriptor", STDERR_FILENO);
141 	nvlist_add_binary(nvl, "nvlist/binary", "test", 4);
142 
143 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
144 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
145 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
146 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
147 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
148 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
149 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
150 
151 	nvlist_free(nvl, "nvlist/null");
152 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
153 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool"));
154 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
155 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
156 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
157 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
158 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
159 
160 	nvlist_free(nvl, "nvlist/bool");
161 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
162 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
163 	CHECK(nvlist_exists_number(nvl, "nvlist/number"));
164 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
165 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
166 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
167 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
168 
169 	nvlist_free(nvl, "nvlist/number");
170 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
171 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
172 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
173 	CHECK(nvlist_exists_string(nvl, "nvlist/string"));
174 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
175 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
176 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
177 
178 	nvlist_free(nvl, "nvlist/string");
179 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
180 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
181 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
182 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
183 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
184 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
185 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
186 
187 	nvlist_free(nvl, "nvlist/nvlist");
188 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
189 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
190 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
191 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
192 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
193 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
194 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
195 
196 	nvlist_free(nvl, "nvlist/descriptor");
197 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
198 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
199 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
200 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
201 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
202 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
203 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary"));
204 
205 	nvlist_free(nvl, "nvlist/binary");
206 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
207 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool"));
208 	CHECK(!nvlist_exists_number(nvl, "nvlist/number"));
209 	CHECK(!nvlist_exists_string(nvl, "nvlist/string"));
210 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
211 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor"));
212 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary"));
213 
214 	CHECK(nvlist_empty(nvl));
215 
216 	nvlist_destroy(nvl);
217 
218 	return (0);
219 }
220