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