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