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