xref: /freebsd/lib/libnv/tests/nvlist_get_test.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
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 #include <sys/nv.h>
29 
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <string.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 #define	fd_is_valid(fd)	(fcntl((fd), F_GETFL) != -1 || errno != EBADF)
47 
48 int
49 main(void)
50 {
51 	const nvlist_t *cnvl;
52 	nvlist_t *nvl;
53 	size_t size;
54 
55 	printf("1..83\n");
56 
57 	nvl = nvlist_create(0);
58 
59 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/true"));
60 	nvlist_add_bool(nvl, "nvlist/bool/true", true);
61 	CHECK(nvlist_error(nvl) == 0);
62 	CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true);
63 
64 	CHECK(!nvlist_exists_bool(nvl, "nvlist/bool/false"));
65 	nvlist_add_bool(nvl, "nvlist/bool/false", false);
66 	CHECK(nvlist_error(nvl) == 0);
67 	CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false);
68 
69 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/0"));
70 	nvlist_add_number(nvl, "nvlist/number/0", 0);
71 	CHECK(nvlist_error(nvl) == 0);
72 	CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0);
73 
74 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/1"));
75 	nvlist_add_number(nvl, "nvlist/number/1", 1);
76 	CHECK(nvlist_error(nvl) == 0);
77 	CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1);
78 
79 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/-1"));
80 	nvlist_add_number(nvl, "nvlist/number/-1", -1);
81 	CHECK(nvlist_error(nvl) == 0);
82 	CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1);
83 
84 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/UINT64_MAX"));
85 	nvlist_add_number(nvl, "nvlist/number/UINT64_MAX", UINT64_MAX);
86 	CHECK(nvlist_error(nvl) == 0);
87 	CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
88 
89 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MIN"));
90 	nvlist_add_number(nvl, "nvlist/number/INT64_MIN", INT64_MIN);
91 	CHECK(nvlist_error(nvl) == 0);
92 	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN);
93 
94 	CHECK(!nvlist_exists_number(nvl, "nvlist/number/INT64_MAX"));
95 	nvlist_add_number(nvl, "nvlist/number/INT64_MAX", INT64_MAX);
96 	CHECK(nvlist_error(nvl) == 0);
97 	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX);
98 
99 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/"));
100 	nvlist_add_string(nvl, "nvlist/string/", "");
101 	CHECK(nvlist_error(nvl) == 0);
102 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0);
103 
104 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/x"));
105 	nvlist_add_string(nvl, "nvlist/string/x", "x");
106 	CHECK(nvlist_error(nvl) == 0);
107 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0);
108 
109 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
110 	nvlist_add_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
111 	CHECK(nvlist_error(nvl) == 0);
112 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
113 
114 	CHECK(!nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
115 	nvlist_add_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", STDERR_FILENO);
116 	CHECK(nvlist_error(nvl) == 0);
117 	CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")));
118 
119 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x"));
120 	nvlist_add_binary(nvl, "nvlist/binary/x", "x", 1);
121 	CHECK(nvlist_error(nvl) == 0);
122 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0);
123 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0);
124 	CHECK(size == 1);
125 
126 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
127 	nvlist_add_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz"));
128 	CHECK(nvlist_error(nvl) == 0);
129 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
130 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
131 	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
132 
133 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
134 	nvlist_add_nvlist(nvl, "nvlist/nvlist", nvl);
135 	CHECK(nvlist_error(nvl) == 0);
136 	cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist");
137 	CHECK(nvlist_get_bool(cnvl, "nvlist/bool/true") == true);
138 	CHECK(nvlist_get_bool(cnvl, "nvlist/bool/false") == false);
139 	CHECK(nvlist_get_number(cnvl, "nvlist/number/0") == 0);
140 	CHECK(nvlist_get_number(cnvl, "nvlist/number/1") == 1);
141 	CHECK((int)nvlist_get_number(cnvl, "nvlist/number/-1") == -1);
142 	CHECK(nvlist_get_number(cnvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
143 	CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MIN") == INT64_MIN);
144 	CHECK((int64_t)nvlist_get_number(cnvl, "nvlist/number/INT64_MAX") == INT64_MAX);
145 	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/"), "") == 0);
146 	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/x"), "x") == 0);
147 	CHECK(strcmp(nvlist_get_string(cnvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
148 	/* TODO */
149 	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", NULL), "x", 1) == 0);
150 	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/x", &size), "x", 1) == 0);
151 	CHECK(size == 1);
152 	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
153 	CHECK(memcmp(nvlist_get_binary(cnvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
154 	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
155 
156 	CHECK(nvlist_get_bool(nvl, "nvlist/bool/true") == true);
157 	CHECK(nvlist_get_bool(nvl, "nvlist/bool/false") == false);
158 	CHECK(nvlist_get_number(nvl, "nvlist/number/0") == 0);
159 	CHECK(nvlist_get_number(nvl, "nvlist/number/1") == 1);
160 	CHECK((int)nvlist_get_number(nvl, "nvlist/number/-1") == -1);
161 	CHECK(nvlist_get_number(nvl, "nvlist/number/UINT64_MAX") == UINT64_MAX);
162 	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MIN") == INT64_MIN);
163 	CHECK((int64_t)nvlist_get_number(nvl, "nvlist/number/INT64_MAX") == INT64_MAX);
164 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/"), "") == 0);
165 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/x"), "x") == 0);
166 	CHECK(strcmp(nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"), "abcdefghijklmnopqrstuvwxyz") == 0);
167 	CHECK(fd_is_valid(nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO")));
168 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", NULL), "x", 1) == 0);
169 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/x", &size), "x", 1) == 0);
170 	CHECK(size == 1);
171 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
172 	CHECK(memcmp(nvlist_get_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size), "abcdefghijklmnopqrstuvwxyz", sizeof("abcdefghijklmnopqrstuvwxyz")) == 0);
173 	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
174 
175 	nvlist_destroy(nvl);
176 
177 	return (0);
178 }
179