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