xref: /freebsd/lib/libnv/tests/nvlist_get_test.c (revision 7431dfd4580e850375fe5478d92ec770344db098)
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 <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include <nv.h>
39 
40 static int ntest = 1;
41 
42 #define	CHECK(expr)	do {						\
43 	if ((expr))							\
44 		printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__);	\
45 	else								\
46 		printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
47 	ntest++;							\
48 } while (0)
49 
50 #define	fd_is_valid(fd)	(fcntl((fd), F_GETFL) != -1 || errno != EBADF)
51 
52 int
53 main(void)
54 {
55 	const nvlist_t *cnvl;
56 	nvlist_t *nvl;
57 	size_t size;
58 
59 	printf("1..83\n");
60 
61 	nvl = nvlist_create(0);
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_get_bool(nvl, "nvlist/bool/true") == 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_get_bool(nvl, "nvlist/bool/false") == 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_get_number(nvl, "nvlist/number/0") == 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_get_number(nvl, "nvlist/number/1") == 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((int)nvlist_get_number(nvl, "nvlist/number/-1") == -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_get_number(nvl, "nvlist/number/UINT64_MAX") == 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((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == 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((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == 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(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0);
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(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0);
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(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
117 
118 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
119 	nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", STDERR_FILENO);
120 	CHECK(nvlist_error(nvl) == 0);
121 	CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")));
122 
123 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x"));
124 	nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1);
125 	CHECK(nvlist_error(nvl) == 0);
126 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0);
127 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0);
128 	CHECK(size == 1);
129 
130 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
131 	nvlist_add_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz"));
132 	CHECK(nvlist_error(nvl) == 0);
133 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
134 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
135 	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
136 
137 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
138 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
139 	CHECK(nvlist_error(nvl) == 0);
140 	cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist");
141 	CHECK(nvlist_get_bool(cnvl, "nvlist/bool/true") == true);
142 	CHECK(nvlist_get_bool(cnvl, "nvlist/bool/false") == false);
143 	CHECK(nvlist_get_number(cnvl, "nvlist/number/0") == 0);
144 	CHECK(nvlist_get_number(cnvl, "nvlist/number/1") == 1);
145 	CHECK((int)nvlist_get_number(cnvl, "nvlist/number/-1") == -1);
146 	CHECK(nvlist_get_number(cnvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
147 	CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MIN") == INT64_MIN);
148 	CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MAX") == INT64_MAX);
149 	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/"), "") == 0);
150 	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/x"), "x") == 0);
151 	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
152 	/* TODO */
153 	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", NULL), "x", 1) == 0);
154 	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", &size), "x", 1) == 0);
155 	CHECK(size == 1);
156 	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
157 	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
158 	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
159 
160 	CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true);
161 	CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false);
162 	CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0);
163 	CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1);
164 	CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1);
165 	CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
166 	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN);
167 	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX);
168 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0);
169 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0);
170 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
171 	CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")));
172 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0);
173 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0);
174 	CHECK(size == 1);
175 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
176 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
177 	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
178 
179 	nvlist_destroy(nvl);
180 
181 	return (0);
182 }
183