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