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