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