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