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