xref: /xnu-10063.121.3/libkern/c++/Tests/TestSerialization/test2/test2_main.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions #include <libkern/OSBase.h>
29*2c2f96dcSApple OSS Distributions 
30*2c2f96dcSApple OSS Distributions __BEGIN_DECLS
31*2c2f96dcSApple OSS Distributions #include <mach/mach_types.h>
32*2c2f96dcSApple OSS Distributions #include <mach/vm_types.h>
33*2c2f96dcSApple OSS Distributions #include <mach/kmod.h>
34*2c2f96dcSApple OSS Distributions 
35*2c2f96dcSApple OSS Distributions kmod_start_func_t test2_start;
36*2c2f96dcSApple OSS Distributions kmod_stop_func_t test2_stop;
37*2c2f96dcSApple OSS Distributions __END_DECLS
38*2c2f96dcSApple OSS Distributions 
39*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSContainers.h>
40*2c2f96dcSApple OSS Distributions #include <iokit/IOLib.h>
41*2c2f96dcSApple OSS Distributions 
42*2c2f96dcSApple OSS Distributions const char *testBuffer =
43*2c2f96dcSApple OSS Distributions     " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \n"
44*2c2f96dcSApple OSS Distributions     " <!DOCTYPE plist SYSTEM \"file://localhost/System/Library/DTDs/PropertyList.dtd\"> \n"
45*2c2f96dcSApple OSS Distributions     " <plist version=\"1.0\"> \n"
46*2c2f96dcSApple OSS Distributions     " <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> \n"
47*2c2f96dcSApple OSS Distributions     " <plist version=\"0.9\"> \n"
48*2c2f96dcSApple OSS Distributions     " <!-- this is a comment --> \n"
49*2c2f96dcSApple OSS Distributions     " <!-- this is a comment with embedded XML statements \n"
50*2c2f96dcSApple OSS Distributions     " <key>ignore me</key>	<true/> \n"
51*2c2f96dcSApple OSS Distributions     " --> \n"
52*2c2f96dcSApple OSS Distributions 
53*2c2f96dcSApple OSS Distributions     " <dict> \n"
54*2c2f96dcSApple OSS Distributions 
55*2c2f96dcSApple OSS Distributions     " <key>key true</key>	<true/> \n"
56*2c2f96dcSApple OSS Distributions     " <key>key false</key>	<false/> \n"
57*2c2f96dcSApple OSS Distributions 
58*2c2f96dcSApple OSS Distributions     " <key>key d0</key>	<data> </data> \n"
59*2c2f96dcSApple OSS Distributions     " <key>key d1</key>	<data>AQ==</data> \n"
60*2c2f96dcSApple OSS Distributions     " <key>key d2</key>	<data>ASM=</data> \n"
61*2c2f96dcSApple OSS Distributions     " <key>key d3</key>	<data>ASNF</data> \n"
62*2c2f96dcSApple OSS Distributions     " <key>key d4</key>	<data ID=\"1\">ASNFZw==</data> \n"
63*2c2f96dcSApple OSS Distributions 
64*2c2f96dcSApple OSS Distributions     " <key>key i0</key>	<integer></integer> \n"
65*2c2f96dcSApple OSS Distributions     " <key>key i1</key>	<integer>123456789</integer> \n"
66*2c2f96dcSApple OSS Distributions     " <key>key i2</key>	<integer>-123456789</integer> \n"
67*2c2f96dcSApple OSS Distributions     " <key>key i3</key>	<integer size=\"32\" ID=\"2\">0x12345678</integer> \n"
68*2c2f96dcSApple OSS Distributions 
69*2c2f96dcSApple OSS Distributions     " <key>key s0</key>	<string></string> \n"
70*2c2f96dcSApple OSS Distributions     " <key>key s1</key>	<string>string 1</string> \n"
71*2c2f96dcSApple OSS Distributions     " <key>key s2</key>	<string ID=\"3\">string 2</string> \n"
72*2c2f96dcSApple OSS Distributions     " <key>key mr �</key>	<string>mac roman copyright �</string> \n"
73*2c2f96dcSApple OSS Distributions     " <key>key uft8 \xc2\xa9</key>	<string>utf-8 copyright \xc2\xa9</string> \n"
74*2c2f96dcSApple OSS Distributions     " <key>key &lt;&amp;&gt;</key>	<string>&lt;&amp;&gt;</string> \n"
75*2c2f96dcSApple OSS Distributions 
76*2c2f96dcSApple OSS Distributions     " <key>key D0</key>	<dict ID=\"4\"> \n"
77*2c2f96dcSApple OSS Distributions     "                        </dict> \n"
78*2c2f96dcSApple OSS Distributions 
79*2c2f96dcSApple OSS Distributions     " <key>key a0</key>	<array> \n"
80*2c2f96dcSApple OSS Distributions     "                        </array> \n"
81*2c2f96dcSApple OSS Distributions 
82*2c2f96dcSApple OSS Distributions     " <key>key a1</key>	<array ID=\"5\"> \n"
83*2c2f96dcSApple OSS Distributions     "                            <string>array string 1</string> \n"
84*2c2f96dcSApple OSS Distributions     "                            <string>array string 2</string> \n"
85*2c2f96dcSApple OSS Distributions     "                        </array> \n"
86*2c2f96dcSApple OSS Distributions 
87*2c2f96dcSApple OSS Distributions     " <key>key r1</key>	<ref IDREF=\"1\"/> \n"
88*2c2f96dcSApple OSS Distributions     " <key>key r2</key>	<ref IDREF=\"2\"/> \n"
89*2c2f96dcSApple OSS Distributions     " <key>key r3</key>	<ref IDREF=\"3\"/> \n"
90*2c2f96dcSApple OSS Distributions     " <key>key r4</key>	<ref IDREF=\"4\"/> \n"
91*2c2f96dcSApple OSS Distributions     " <key>key r5</key>	<ref IDREF=\"5\"/> \n"
92*2c2f96dcSApple OSS Distributions 
93*2c2f96dcSApple OSS Distributions     " <key>key e1</key>	<array/> \n"
94*2c2f96dcSApple OSS Distributions     " <key>key e2</key>	<dict/> \n"
95*2c2f96dcSApple OSS Distributions     " <key>key e4</key>	<integer/> \n"
96*2c2f96dcSApple OSS Distributions     " <key>key e5</key>	<string/> \n"
97*2c2f96dcSApple OSS Distributions     " <key>key e6</key>	<data/> \n"
98*2c2f96dcSApple OSS Distributions 
99*2c2f96dcSApple OSS Distributions     " <key>key S0</key>	<set> \n"
100*2c2f96dcSApple OSS Distributions     "                        </set> \n"
101*2c2f96dcSApple OSS Distributions     " <key>key S1</key>	<set ID=\"6\"> \n"
102*2c2f96dcSApple OSS Distributions     "                             <string>set string 1</string> \n"
103*2c2f96dcSApple OSS Distributions     "                             <string>set string 2</string> \n"
104*2c2f96dcSApple OSS Distributions     "                         </set> \n"
105*2c2f96dcSApple OSS Distributions     " <key>key r6</key>	<ref IDREF=\"6\"/> \n"
106*2c2f96dcSApple OSS Distributions     " <key>key e3</key>	<set/> \n"
107*2c2f96dcSApple OSS Distributions 
108*2c2f96dcSApple OSS Distributions     " </dict> \n"
109*2c2f96dcSApple OSS Distributions     " </plist> \n"
110*2c2f96dcSApple OSS Distributions ;
111*2c2f96dcSApple OSS Distributions 
112*2c2f96dcSApple OSS Distributions /*
113*2c2f96dcSApple OSS Distributions  *  this causes the parser to return an empty string? it doesn't look like yyerror gets called
114*2c2f96dcSApple OSS Distributions  *  char *testBuffer = "<array ID=1><array IDREF=\"1\"/></array>"
115*2c2f96dcSApple OSS Distributions  *
116*2c2f96dcSApple OSS Distributions  */
117*2c2f96dcSApple OSS Distributions 
118*2c2f96dcSApple OSS Distributions kern_return_t
test2_start(struct kmod_info * ki,void * data)119*2c2f96dcSApple OSS Distributions test2_start(struct kmod_info *ki, void *data)
120*2c2f96dcSApple OSS Distributions {
121*2c2f96dcSApple OSS Distributions 	IOLog("test buffer start:\n%s\n:test buffer end.\n", testBuffer);
122*2c2f96dcSApple OSS Distributions 
123*2c2f96dcSApple OSS Distributions 	// test unserialize
124*2c2f96dcSApple OSS Distributions 	OSString *errmsg = 0;
125*2c2f96dcSApple OSS Distributions 	OSObject *d = OSUnserializeXML(testBuffer, &errmsg);
126*2c2f96dcSApple OSS Distributions 	if (!d) {
127*2c2f96dcSApple OSS Distributions 		if (errmsg) {
128*2c2f96dcSApple OSS Distributions 			IOLog("%s\n", errmsg->getCStringNoCopy());
129*2c2f96dcSApple OSS Distributions 		} else {
130*2c2f96dcSApple OSS Distributions 			IOLog("bogus error message\n");
131*2c2f96dcSApple OSS Distributions 		}
132*2c2f96dcSApple OSS Distributions 
133*2c2f96dcSApple OSS Distributions 		return KMOD_RETURN_SUCCESS;
134*2c2f96dcSApple OSS Distributions 	}
135*2c2f96dcSApple OSS Distributions 
136*2c2f96dcSApple OSS Distributions 	// test serialize
137*2c2f96dcSApple OSS Distributions 	OSSerialize *s = OSSerialize::withCapacity(5);
138*2c2f96dcSApple OSS Distributions 	if (!d->serialize(s)) {
139*2c2f96dcSApple OSS Distributions 		IOLog("serialization failed\n");
140*2c2f96dcSApple OSS Distributions 		return KMOD_RETURN_SUCCESS;
141*2c2f96dcSApple OSS Distributions 	}
142*2c2f96dcSApple OSS Distributions 
143*2c2f96dcSApple OSS Distributions 	IOLog("serialized object's length = %d, capacity = %d\n", s->getLength(), s->getCapacity());
144*2c2f96dcSApple OSS Distributions 	IOLog("object unformatted = %s\n", s->text());
145*2c2f96dcSApple OSS Distributions 
146*2c2f96dcSApple OSS Distributions 	// try second time
147*2c2f96dcSApple OSS Distributions 	OSObject *d2 = OSUnserializeXML(s->text(), &errmsg);
148*2c2f96dcSApple OSS Distributions 	if (!d2) {
149*2c2f96dcSApple OSS Distributions 		IOLog("%s\n", errmsg->getCStringNoCopy());
150*2c2f96dcSApple OSS Distributions 		return KMOD_RETURN_SUCCESS;
151*2c2f96dcSApple OSS Distributions 	}
152*2c2f96dcSApple OSS Distributions 	OSSerialize *s2 = OSSerialize::withCapacity(5);
153*2c2f96dcSApple OSS Distributions 	if (!d2->serialize(s2)) {
154*2c2f96dcSApple OSS Distributions 		IOLog("serialization #2 failed\n");
155*2c2f96dcSApple OSS Distributions 		return KMOD_RETURN_SUCCESS;
156*2c2f96dcSApple OSS Distributions 	}
157*2c2f96dcSApple OSS Distributions 
158*2c2f96dcSApple OSS Distributions 	IOLog("serialized object's length = %d, capacity = %d\n",
159*2c2f96dcSApple OSS Distributions 	    s2->getLength(), s2->getCapacity());
160*2c2f96dcSApple OSS Distributions 	IOLog("object unformatted = %s\n", s2->text());
161*2c2f96dcSApple OSS Distributions 
162*2c2f96dcSApple OSS Distributions 	IOLog("\nserialized objects compared %ssuccessfully textually\n\n",
163*2c2f96dcSApple OSS Distributions 	    strcmp(s->text(), s2->text()) ? "un":"");
164*2c2f96dcSApple OSS Distributions 
165*2c2f96dcSApple OSS Distributions 	IOLog("\nserialized objects compared %ssuccessfully objectwise\n\n",
166*2c2f96dcSApple OSS Distributions 	    d->isEqualTo(d2) ? "":"un");
167*2c2f96dcSApple OSS Distributions 
168*2c2f96dcSApple OSS Distributions 	s2->release();
169*2c2f96dcSApple OSS Distributions 	if (d2) {
170*2c2f96dcSApple OSS Distributions 		d2->release();
171*2c2f96dcSApple OSS Distributions 	}
172*2c2f96dcSApple OSS Distributions 	s->release();
173*2c2f96dcSApple OSS Distributions 	if (d) {
174*2c2f96dcSApple OSS Distributions 		d->release();
175*2c2f96dcSApple OSS Distributions 	}
176*2c2f96dcSApple OSS Distributions 
177*2c2f96dcSApple OSS Distributions 	return KMOD_RETURN_SUCCESS;
178*2c2f96dcSApple OSS Distributions }
179*2c2f96dcSApple OSS Distributions 
180*2c2f96dcSApple OSS Distributions kern_return_t
test2_stop(struct kmod_info * ki,void * data)181*2c2f96dcSApple OSS Distributions test2_stop(struct kmod_info *ki, void *data)
182*2c2f96dcSApple OSS Distributions {
183*2c2f96dcSApple OSS Distributions 	return KMOD_RETURN_SUCCESS;
184*2c2f96dcSApple OSS Distributions }
185