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