xref: /freebsd-src/lib/libnv/tests/nvlist_add_test.c (revision 5c2bc3db201a4fe8d7911cf816bea104d5dc2138)
1b236bcf1SEnji Cooper /*-
2b236bcf1SEnji Cooper  * Copyright (c) 2013 The FreeBSD Foundation
3b236bcf1SEnji Cooper  *
4b236bcf1SEnji Cooper  * This software was developed by Pawel Jakub Dawidek under sponsorship from
5b236bcf1SEnji Cooper  * the FreeBSD Foundation.
6b236bcf1SEnji Cooper  *
7b236bcf1SEnji Cooper  * Redistribution and use in source and binary forms, with or without
8b236bcf1SEnji Cooper  * modification, are permitted provided that the following conditions
9b236bcf1SEnji Cooper  * are met:
10b236bcf1SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
11b236bcf1SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
12b236bcf1SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
13b236bcf1SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
14b236bcf1SEnji Cooper  *    documentation and/or other materials provided with the distribution.
15b236bcf1SEnji Cooper  *
16b236bcf1SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17b236bcf1SEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b236bcf1SEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b236bcf1SEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20b236bcf1SEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b236bcf1SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b236bcf1SEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b236bcf1SEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b236bcf1SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b236bcf1SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b236bcf1SEnji Cooper  * SUCH DAMAGE.
27b236bcf1SEnji Cooper  */
28b236bcf1SEnji Cooper 
29*c36e54bbSMariusz Zaborski #include <sys/nv.h>
30*c36e54bbSMariusz Zaborski 
31b236bcf1SEnji Cooper #include <errno.h>
32b236bcf1SEnji Cooper #include <stdio.h>
33b236bcf1SEnji Cooper #include <unistd.h>
34b236bcf1SEnji Cooper 
35b236bcf1SEnji Cooper static int ntest = 1;
36b236bcf1SEnji Cooper 
37b236bcf1SEnji Cooper #define	CHECK(expr)	do {						\
38b236bcf1SEnji Cooper 	if ((expr))							\
39b236bcf1SEnji Cooper 		printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__);	\
40b236bcf1SEnji Cooper 	else								\
41b236bcf1SEnji Cooper 		printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
42b236bcf1SEnji Cooper 	ntest++;							\
43b236bcf1SEnji Cooper } while (0)
44b236bcf1SEnji Cooper 
45b236bcf1SEnji Cooper int
46b236bcf1SEnji Cooper main(void)
47b236bcf1SEnji Cooper {
48b236bcf1SEnji Cooper 	const nvlist_t *cnvl;
49b236bcf1SEnji Cooper 	nvlist_t *nvl;
50b236bcf1SEnji Cooper 
51b236bcf1SEnji Cooper 	printf("1..94\n");
52b236bcf1SEnji Cooper 
53b236bcf1SEnji Cooper 	nvl = nvlist_create(0);
54b236bcf1SEnji Cooper 
55b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
56b236bcf1SEnji Cooper 	nvlist_add_null(nvl, "nvlist/null");
57b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
58b236bcf1SEnji Cooper 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
59b236bcf1SEnji Cooper 
60b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/true"));
61b236bcf1SEnji Cooper 	nvlist_add_bool(nvl, "nvlist/bool/true", true);
62b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
63b236bcf1SEnji Cooper 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool/true"));
64b236bcf1SEnji Cooper 
65b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/false"));
66b236bcf1SEnji Cooper 	nvlist_add_bool(nvl, "nvlist/bool/false", false);
67b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
68b236bcf1SEnji Cooper 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool/false"));
69b236bcf1SEnji Cooper 
70b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/0"));
71b236bcf1SEnji Cooper 	nvlist_add_number(nvl, "nvlist/number/0", 0);
72b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
73b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/0"));
74b236bcf1SEnji Cooper 
75b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/1"));
76b236bcf1SEnji Cooper 	nvlist_add_number(nvl, "nvlist/number/1", 1);
77b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
78b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/1"));
79b236bcf1SEnji Cooper 
80b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/-1"));
81b236bcf1SEnji Cooper 	nvlist_add_number(nvl, "nvlist/number/-1", -1);
82b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
83b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/-1"));
84b236bcf1SEnji Cooper 
85b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
86b236bcf1SEnji Cooper 	nvlist_add_number(nvl, "nvlist/number/UINT64_MAX", UINT64_MAX);
87b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
88b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
89b236bcf1SEnji Cooper 
90b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
91b236bcf1SEnji Cooper 	nvlist_add_number(nvl, "nvlist/number/INT64_MIN", INT64_MIN);
92b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
93b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
94b236bcf1SEnji Cooper 
95b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
96b236bcf1SEnji Cooper 	nvlist_add_number(nvl, "nvlist/number/INT64_MAX", INT64_MAX);
97b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
98b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
99b236bcf1SEnji Cooper 
100b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/"));
101b236bcf1SEnji Cooper 	nvlist_add_string(nvl, "nvlist/string/", "");
102b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
103b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/string/"));
104b236bcf1SEnji Cooper 
105b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/x"));
106b236bcf1SEnji Cooper 	nvlist_add_string(nvl, "nvlist/string/x", "x");
107b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
108b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/string/x"));
109b236bcf1SEnji Cooper 
110b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
111b236bcf1SEnji Cooper 	nvlist_add_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
112b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
113b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
114b236bcf1SEnji Cooper 
115b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_string(nvl, "nvlist/stringf/"));
116b236bcf1SEnji Cooper 	nvlist_add_stringf(nvl, "nvlist/stringf/", "%s", "");
117b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
118b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/"));
119b236bcf1SEnji Cooper 
120b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_string(nvl, "nvlist/stringf/x"));
121b236bcf1SEnji Cooper 	nvlist_add_stringf(nvl, "nvlist/stringf/x", "%s", "x");
122b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
123b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/x"));
124b236bcf1SEnji Cooper 
125b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_string(nvl, "nvlist/stringf/666Xabc"));
126b236bcf1SEnji Cooper 	nvlist_add_stringf(nvl, "nvlist/stringf/666Xabc", "%d%c%s", 666, 'X', "abc");
127b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
128b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/666Xabc"));
129b236bcf1SEnji Cooper 
130b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
131b236bcf1SEnji Cooper 	nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", STDERR_FILENO);
132b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
133b236bcf1SEnji Cooper 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
134b236bcf1SEnji Cooper 
135b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x"));
136b236bcf1SEnji Cooper 	nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1);
137b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
138b236bcf1SEnji Cooper 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/x"));
139b236bcf1SEnji Cooper 
140b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
141b236bcf1SEnji Cooper 	nvlist_add_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz"));
142b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
143b236bcf1SEnji Cooper 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
144b236bcf1SEnji Cooper 
145b236bcf1SEnji Cooper 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
146b236bcf1SEnji Cooper 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
147b236bcf1SEnji Cooper 	CHECK(nvlist_error(nvl) == 0);
148b236bcf1SEnji Cooper 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
149b236bcf1SEnji Cooper 
150b236bcf1SEnji Cooper 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
151b236bcf1SEnji Cooper 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool/true"));
152b236bcf1SEnji Cooper 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool/false"));
153b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/0"));
154b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/1"));
155b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/-1"));
156b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
157b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
158b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
159b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/string/"));
160b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/string/x"));
161b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
162b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/"));
163b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/x"));
164b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/666Xabc"));
165b236bcf1SEnji Cooper 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
166b236bcf1SEnji Cooper 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/x"));
167b236bcf1SEnji Cooper 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
168b236bcf1SEnji Cooper 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
169b236bcf1SEnji Cooper 
170b236bcf1SEnji Cooper 	cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist");
171b236bcf1SEnji Cooper 	CHECK(nvlist_exists_null(cnvl, "nvlist/null"));
172b236bcf1SEnji Cooper 	CHECK(nvlist_exists_bool(cnvl, "nvlist/bool/true"));
173b236bcf1SEnji Cooper 	CHECK(nvlist_exists_bool(cnvl, "nvlist/bool/false"));
174b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/0"));
175b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/1"));
176b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/-1"));
177b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/UINT64_MAX"));
178b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/INT64_MIN"));
179b236bcf1SEnji Cooper 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/INT64_MAX"));
180b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(cnvl, "nvlist/string/"));
181b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(cnvl, "nvlist/string/x"));
182b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(cnvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
183b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(cnvl, "nvlist/stringf/"));
184b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(cnvl, "nvlist/stringf/x"));
185b236bcf1SEnji Cooper 	CHECK(nvlist_exists_string(cnvl, "nvlist/stringf/666Xabc"));
186b236bcf1SEnji Cooper 	CHECK(nvlist_exists_descriptor(cnvl, "nvlist/descriptor/STDERR_FILENO"));
187b236bcf1SEnji Cooper 	CHECK(nvlist_exists_binary(cnvl, "nvlist/binary/x"));
188b236bcf1SEnji Cooper 	CHECK(nvlist_exists_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
189b236bcf1SEnji Cooper 
190b236bcf1SEnji Cooper 	nvlist_destroy(nvl);
191b236bcf1SEnji Cooper 
192b236bcf1SEnji Cooper 	return (0);
193b236bcf1SEnji Cooper }
194