2 * Copyright (c) 2013 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
42 #define CHECK(expr) do { \
44 printf("ok # %d %s:%u\n", ntest, __FILE__, __LINE__); \
46 printf("not ok # %d %s:%u\n", ntest, __FILE__, __LINE__);\
50 #define fd_is_valid(fd) (fcntl((fd), F_GETFL) != -1 || errno != EBADF)
61 nvl = nvlist_create(0);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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")));
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);
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"));
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);
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);
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"));
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);
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"));