xref: /freebsd/lib/libnv/tests/nvlist_move_test.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
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 
30 #include <sys/nv.h>
31 
32 #include <errno.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.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 	void *ptr;
54 	size_t size;
55 	int fd;
56 
57 	printf("1..52\n");
58 
59 	nvl = nvlist_create(0);
60 
61 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/"));
62 	ptr = strdup("");
63 	CHECK(ptr != NULL);
64 	nvlist_move_string(nvl, "nvlist/string/", ptr);
65 	CHECK(nvlist_error(nvl) == 0);
66 	CHECK(nvlist_exists_string(nvl, "nvlist/string/"));
67 	CHECK(ptr == nvlist_get_string(nvl, "nvlist/string/"));
68 
69 	CHECK(!nvlist_exists_string(nvl, "nvlist/string/x"));
70 	ptr = strdup("x");
71 	CHECK(ptr != NULL);
72 	nvlist_move_string(nvl, "nvlist/string/x", ptr);
73 	CHECK(nvlist_error(nvl) == 0);
74 	CHECK(nvlist_exists_string(nvl, "nvlist/string/x"));
75 	CHECK(ptr == nvlist_get_string(nvl, "nvlist/string/x"));
76 
77 	CHECK(!nvlist_exists_string(nvl,
78 	    "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
79 	ptr = strdup("abcdefghijklmnopqrstuvwxyz");
80 	CHECK(ptr != NULL);
81 	nvlist_move_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz",
82 	    ptr);
83 	CHECK(nvlist_error(nvl) == 0);
84 	CHECK(nvlist_exists_string(nvl,
85 	    "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
86 	CHECK(ptr ==
87 	    nvlist_get_string(nvl, "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
88 
89 	CHECK(!nvlist_exists_descriptor(nvl,
90 	    "nvlist/descriptor/STDERR_FILENO"));
91 	fd = dup(STDERR_FILENO);
92 	CHECK(fd >= 0);
93 	nvlist_move_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO", fd);
94 	CHECK(nvlist_error(nvl) == 0);
95 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
96 	CHECK(fd ==
97 	    nvlist_get_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
98 
99 	CHECK(!nvlist_exists_binary(nvl, "nvlist/binary/x"));
100 	ptr = malloc(1);
101 	CHECK(ptr != NULL);
102 	memcpy(ptr, "x", 1);
103 	nvlist_move_binary(nvl, "nvlist/binary/x", ptr, 1);
104 	CHECK(nvlist_error(nvl) == 0);
105 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/x"));
106 	CHECK(ptr == nvlist_get_binary(nvl, "nvlist/binary/x", NULL));
107 	CHECK(ptr == nvlist_get_binary(nvl, "nvlist/binary/x", &size));
108 	CHECK(size == 1);
109 
110 	CHECK(!nvlist_exists_binary(nvl,
111 	    "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
112 	ptr = malloc(sizeof("abcdefghijklmnopqrstuvwxyz"));
113 	CHECK(ptr != NULL);
114 	memcpy(ptr, "abcdefghijklmnopqrstuvwxyz",
115 	    sizeof("abcdefghijklmnopqrstuvwxyz"));
116 	nvlist_move_binary(nvl, "nvlist/binary/abcdefghijklmnopqrstuvwxyz",
117 	    ptr, sizeof("abcdefghijklmnopqrstuvwxyz"));
118 	CHECK(nvlist_error(nvl) == 0);
119 	CHECK(nvlist_exists_binary(nvl,
120 	    "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
121 	CHECK(ptr == nvlist_get_binary(nvl,
122 	    "nvlist/binary/abcdefghijklmnopqrstuvwxyz", NULL));
123 	CHECK(ptr == nvlist_get_binary(nvl,
124 	    "nvlist/binary/abcdefghijklmnopqrstuvwxyz", &size));
125 	CHECK(size == sizeof("abcdefghijklmnopqrstuvwxyz"));
126 
127 	CHECK(!nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
128 	ptr = nvlist_clone(nvl);
129 	CHECK(ptr != NULL);
130 	nvlist_move_nvlist(nvl, "nvlist/nvlist", ptr);
131 	CHECK(nvlist_error(nvl) == 0);
132 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
133 	CHECK(ptr == nvlist_get_nvlist(nvl, "nvlist/nvlist"));
134 
135 	CHECK(nvlist_exists_string(nvl, "nvlist/string/"));
136 	CHECK(nvlist_exists_string(nvl, "nvlist/string/x"));
137 	CHECK(nvlist_exists_string(nvl,
138 	    "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
139 	CHECK(nvlist_exists_descriptor(nvl, "nvlist/descriptor/STDERR_FILENO"));
140 	CHECK(nvlist_exists_binary(nvl, "nvlist/binary/x"));
141 	CHECK(nvlist_exists_binary(nvl,
142 	    "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
143 	CHECK(nvlist_exists_nvlist(nvl, "nvlist/nvlist"));
144 
145 	cnvl = nvlist_get_nvlist(nvl, "nvlist/nvlist");
146 	CHECK(nvlist_exists_string(cnvl, "nvlist/string/"));
147 	CHECK(nvlist_exists_string(cnvl, "nvlist/string/x"));
148 	CHECK(nvlist_exists_string(cnvl,
149 	    "nvlist/string/abcdefghijklmnopqrstuvwxyz"));
150 	CHECK(nvlist_exists_descriptor(cnvl,
151 	    "nvlist/descriptor/STDERR_FILENO"));
152 	CHECK(nvlist_exists_binary(cnvl, "nvlist/binary/x"));
153 	CHECK(nvlist_exists_binary(cnvl,
154 	    "nvlist/binary/abcdefghijklmnopqrstuvwxyz"));
155 
156 	nvlist_destroy(nvl);
157 
158 	return (0);
159 }
160