xref: /freebsd/lib/libnv/tests/nvlist_add_test.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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 <errno.h>
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 	const nvlist_t *cnvl;
49 	nvlist_t *nvl;
50 
51 	printf("1..94\n");
52 
53 	nvl = nvlist_create(0);
54 
55 	CHECK(!nvlist_exists_null(nvl, "nvlist/null"));
56 	nvlist_add_null(nvl, "nvlist/null");
57 	CHECK(nvlist_error(nvl) == 0);
58 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
59 
60 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/true"));
61 	nvlist_add_bool(nvl, "nvlist/bool/true", true);
62 	CHECK(nvlist_error(nvl) == 0);
63 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool/true"));
64 
65 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/false"));
66 	nvlist_add_bool(nvl, "nvlist/bool/false", false);
67 	CHECK(nvlist_error(nvl) == 0);
68 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool/false"));
69 
70 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/0"));
71 	nvlist_add_number(nvl, "nvlist/number/0", 0);
72 	CHECK(nvlist_error(nvl) == 0);
73 	CHECK(nvlist_exists_number(nvl, "nvlist/number/0"));
74 
75 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/1"));
76 	nvlist_add_number(nvl, "nvlist/number/1", 1);
77 	CHECK(nvlist_error(nvl) == 0);
78 	CHECK(nvlist_exists_number(nvl, "nvlist/number/1"));
79 
80 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/-1"));
81 	nvlist_add_number(nvl, "nvlist/number/-1", -1);
82 	CHECK(nvlist_error(nvl) == 0);
83 	CHECK(nvlist_exists_number(nvl, "nvlist/number/-1"));
84 
85 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
86 	nvlist_add_number(nvl, "nvlist/number/UINT64_MAX", UINT64_MAX);
87 	CHECK(nvlist_error(nvl) == 0);
88 	CHECK(nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
89 
90 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
91 	nvlist_add_number(nvl, "nvlist/number/INT64_MIN", INT64_MIN);
92 	CHECK(nvlist_error(nvl) == 0);
93 	CHECK(nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
94 
95 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
96 	nvlist_add_number(nvl, "nvlist/number/INT64_MAX", INT64_MAX);
97 	CHECK(nvlist_error(nvl) == 0);
98 	CHECK(nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
99 
100 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/"));
101 	nvlist_add_string(nvl, "nvlist/string/", "");
102 	CHECK(nvlist_error(nvl) == 0);
103 	CHECK(nvlist_exists_string(nvl, "nvlist/string/"));
104 
105 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/x"));
106 	nvlist_add_string(nvl, "nvlist/string/x", "x");
107 	CHECK(nvlist_error(nvl) == 0);
108 	CHECK(nvlist_exists_string(nvl, "nvlist/string/x"));
109 
110 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
111 	nvlist_add_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
112 	CHECK(nvlist_error(nvl) == 0);
113 	CHECK(nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
114 
115 	CHECK(!nvlist_exists_string(nvl, "nvlist/stringf/"));
116 	nvlist_add_stringf(nvl, "nvlist/stringf/", "%s", "");
117 	CHECK(nvlist_error(nvl) == 0);
118 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/"));
119 
120 	CHECK(!nvlist_exists_string(nvl, "nvlist/stringf/x"));
121 	nvlist_add_stringf(nvl, "nvlist/stringf/x", "%s", "x");
122 	CHECK(nvlist_error(nvl) == 0);
123 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/x"));
124 
125 	CHECK(!nvlist_exists_string(nvl, "nvlist/stringf/666Xabc"));
126 	nvlist_add_stringf(nvl, "nvlist/stringf/666Xabc", "%d%c%s", 666, 'X', "abc");
127 	CHECK(nvlist_error(nvl) == 0);
128 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/666Xabc"));
129 
130 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
131 	nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", STDERR_FILENO);
132 	CHECK(nvlist_error(nvl) == 0);
133 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
134 
135 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x"));
136 	nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1);
137 	CHECK(nvlist_error(nvl) == 0);
138 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/x"));
139 
140 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
141 	nvlist_add_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz"));
142 	CHECK(nvlist_error(nvl) == 0);
143 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
144 
145 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
146 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
147 	CHECK(nvlist_error(nvl) == 0);
148 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
149 
150 	CHECK(nvlist_exists_null(nvl, "nvlist/null"));
151 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool/true"));
152 	CHECK(nvlist_exists_bool(nvl, "nvlist/bool/false"));
153 	CHECK(nvlist_exists_number(nvl, "nvlist/number/0"));
154 	CHECK(nvlist_exists_number(nvl, "nvlist/number/1"));
155 	CHECK(nvlist_exists_number(nvl, "nvlist/number/-1"));
156 	CHECK(nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
157 	CHECK(nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
158 	CHECK(nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
159 	CHECK(nvlist_exists_string(nvl, "nvlist/string/"));
160 	CHECK(nvlist_exists_string(nvl, "nvlist/string/x"));
161 	CHECK(nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
162 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/"));
163 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/x"));
164 	CHECK(nvlist_exists_string(nvl, "nvlist/stringf/666Xabc"));
165 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
166 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/x"));
167 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
168 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
169 
170 	cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist");
171 	CHECK(nvlist_exists_null(cnvl, "nvlist/null"));
172 	CHECK(nvlist_exists_bool(cnvl, "nvlist/bool/true"));
173 	CHECK(nvlist_exists_bool(cnvl, "nvlist/bool/false"));
174 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/0"));
175 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/1"));
176 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/-1"));
177 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/UINT64_MAX"));
178 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/INT64_MIN"));
179 	CHECK(nvlist_exists_number(cnvl, "nvlist/number/INT64_MAX"));
180 	CHECK(nvlist_exists_string(cnvl, "nvlist/string/"));
181 	CHECK(nvlist_exists_string(cnvl, "nvlist/string/x"));
182 	CHECK(nvlist_exists_string(cnvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
183 	CHECK(nvlist_exists_string(cnvl, "nvlist/stringf/"));
184 	CHECK(nvlist_exists_string(cnvl, "nvlist/stringf/x"));
185 	CHECK(nvlist_exists_string(cnvl, "nvlist/stringf/666Xabc"));
186 	CHECK(nvlist_exists_descriptor(cnvl, "nvlist/descriptor/STDERR_FILENO"));
187 	CHECK(nvlist_exists_binary(cnvl, "nvlist/binary/x"));
188 	CHECK(nvlist_exists_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
189 
190 	nvlist_destroy(nvl);
191 
192 	return (0);
193 }
194