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