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