xref: /freebsd/contrib/netbsd-tests/lib/libprop/t_basic.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /* $NetBSD: t_basic.c,v 1.4 2011/04/20 20:02:58 martin Exp $ */
2 
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Written by Jason Thorpe 5/26/2006.
31  * Public domain.
32  */
33 
34 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 2008\
36  The NetBSD Foundation, inc. All rights reserved.");
37 __RCSID("$NetBSD: t_basic.c,v 1.4 2011/04/20 20:02:58 martin Exp $");
38 
39 #include <stdlib.h>
40 #include <string.h>
41 #include <prop/proplib.h>
42 
43 #include <atf-c.h>
44 
45 static const char compare1[] =
46 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
47 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
48 "<plist version=\"1.0\">\n"
49 "<dict>\n"
50 "	<key>false-val</key>\n"
51 "	<false/>\n"
52 "	<key>one</key>\n"
53 "	<integer>1</integer>\n"
54 "	<key>three</key>\n"
55 "	<array>\n"
56 "		<dict>\n"
57 "			<key>one</key>\n"
58 "			<integer>1</integer>\n"
59 "			<key>two</key>\n"
60 "			<string>number-two</string>\n"
61 "		</dict>\n"
62 "		<dict>\n"
63 "			<key>one</key>\n"
64 "			<integer>1</integer>\n"
65 "			<key>two</key>\n"
66 "			<string>number-two</string>\n"
67 "		</dict>\n"
68 "		<dict>\n"
69 "			<key>one</key>\n"
70 "			<integer>1</integer>\n"
71 "			<key>two</key>\n"
72 "			<string>number-two</string>\n"
73 "		</dict>\n"
74 "	</array>\n"
75 "	<key>true-val</key>\n"
76 "	<true/>\n"
77 "	<key>two</key>\n"
78 "	<string>number-two</string>\n"
79 "</dict>\n"
80 "</plist>\n";
81 
82 ATF_TC(prop_basic);
83 ATF_TC_HEAD(prop_basic, tc)
84 {
85 	atf_tc_set_md_var(tc, "descr", "A basic test of proplib(3)");
86 }
87 
88 ATF_TC_BODY(prop_basic, tc)
89 {
90 	prop_dictionary_t dict;
91 	char *ext1;
92 
93 	dict = prop_dictionary_create();
94 	ATF_REQUIRE(dict != NULL);
95 
96 	{
97 		prop_number_t num = prop_number_create_integer(1);
98 		ATF_REQUIRE(num != NULL);
99 
100 		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "one", num), true);
101 		prop_object_release(num);
102 	}
103 
104 	{
105 		prop_string_t str = prop_string_create_cstring("number-two");
106 		ATF_REQUIRE(str != NULL);
107 
108 		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "two", str), true);
109 		prop_object_release(str);
110 	}
111 
112 	{
113 		prop_array_t arr;
114 		prop_dictionary_t dict_copy;
115 		int i;
116 
117 		arr = prop_array_create();
118 		ATF_REQUIRE(arr != NULL);
119 
120 		for (i = 0; i < 3; ++i) {
121 			dict_copy = prop_dictionary_copy(dict);
122 			ATF_REQUIRE(dict_copy != NULL);
123 			ATF_REQUIRE_EQ(prop_array_add(arr, dict_copy), true);
124 			prop_object_release(dict_copy);
125 		}
126 
127 		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "three", arr), true);
128 		prop_object_release(arr);
129 	}
130 
131 	{
132 		prop_bool_t val = prop_bool_create(true);
133 		ATF_REQUIRE(val != NULL);
134 		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "true-val", val), true);
135 		prop_object_release(val);
136 
137 		val = prop_bool_create(false);
138 		ATF_REQUIRE(val != NULL);
139 		ATF_REQUIRE_EQ(prop_dictionary_set(dict, "false-val", val), true);
140 		prop_object_release(val);
141 	}
142 
143 	ext1 = prop_dictionary_externalize(dict);
144 	ATF_REQUIRE(ext1 != NULL);
145 	ATF_REQUIRE_STREQ(compare1, ext1);
146 
147 	{
148 		prop_dictionary_t dict2;
149 		char *ext2;
150 
151 		dict2 = prop_dictionary_internalize(ext1);
152 		ATF_REQUIRE(dict2 != NULL);
153 		ext2 = prop_dictionary_externalize(dict2);
154 		ATF_REQUIRE(ext2 != NULL);
155 		ATF_REQUIRE_STREQ(ext1, ext2);
156 		prop_object_release(dict2);
157 		free(ext2);
158 	}
159 
160 	prop_object_release(dict);
161 	free(ext1);
162 }
163 
164 ATF_TC(prop_dictionary_equals);
165 ATF_TC_HEAD(prop_dictionary_equals, tc)
166 {
167 	atf_tc_set_md_var(tc, "descr", "Test prop_dictionary_equals(3)");
168 }
169 
170 ATF_TC_BODY(prop_dictionary_equals, tc)
171 {
172 	prop_dictionary_t c, d;
173 
174 	/*
175 	 * Fixed, should not fail any more...
176 	 *
177 	atf_tc_expect_death("PR lib/43964");
178 	 *
179 	 */
180 
181 	d = prop_dictionary_internalize(compare1);
182 
183 	ATF_REQUIRE(d != NULL);
184 
185 	c = prop_dictionary_copy(d);
186 
187 	ATF_REQUIRE(c != NULL);
188 
189 	if (prop_dictionary_equals(c, d) != true)
190 		atf_tc_fail("dictionaries are not equal");
191 
192 	prop_object_release(c);
193 	prop_object_release(d);
194 }
195 
196 ATF_TP_ADD_TCS(tp)
197 {
198 
199 	ATF_TP_ADD_TC(tp, prop_basic);
200 	ATF_TP_ADD_TC(tp, prop_dictionary_equals);
201 
202 	return atf_no_error();
203 }
204