]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/Tests/TestSerialization/test1/test1_main.cpp
xnu-517.tar.gz
[apple/xnu.git] / libkern / c++ / Tests / TestSerialization / test1 / test1_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 test1_start;
33 kmod_stop_func_t test1_stop;
34 __END_DECLS
35
36 #include <libkern/c++/OSContainers.h>
37 #include <iokit/IOLib.h>
38
39 char *testBuffer = "
40 { string = \"this is a 'string' with spaces\";
41 string2 = 'this is also a \"string\" with spaces';
42 offset = 16384:32;
43 true = .true.;
44 false = .false.;
45 data = <0123 4567 89abcdef>;
46 array = (1:8, 2:16, 3:32, 4:64 );
47 set = [ one, two, three, four ];
48 emptydict = { }@1;
49 emptyarray = ( )@2;
50 emptyset = [ ]@3;
51 emptydata = < >@4;
52 emptydict2 = @1;
53 emptyarray2 = @2;
54 emptyset2 = @3;
55 emptydata2 = @4;
56 dict2 = { string = asdfasdf; };
57 dict3 = { string = asdfasdf; };
58 }@0";
59
60 kern_return_t
61 test1_start(struct kmod_info *ki, void *data)
62 {
63 IOLog("test buffer start:\n%s\n:test buffer end.\n", testBuffer);
64
65 // test unserialize
66 OSString *errmsg;
67 OSObject *d = OSUnserialize(testBuffer, &errmsg);
68 if (!d) {
69 IOLog("%s\n", errmsg->getCStringNoCopy());
70 return KMOD_RETURN_SUCCESS;
71 }
72
73 // test serialize
74 OSSerialize *s = OSSerialize::withCapacity(5);
75 if (!d->serialize(s)) {
76 IOLog("serialization failed\n");
77 return KMOD_RETURN_SUCCESS;
78 }
79
80 IOLog("serialized object's length = %d, capacity = %d\n", s->getLength(), s->getCapacity());
81 IOLog("object unformatted = %s\n", s->text());
82
83 // try second time
84 OSObject *d2 = OSUnserializeXML(s->text(), &errmsg);
85 if (!d2) {
86 IOLog("%s\n", errmsg->getCStringNoCopy());
87 return KMOD_RETURN_SUCCESS;
88 }
89
90 IOLog("\nserialized objects compared %ssuccessfully objectwise\n\n",
91 d->isEqualTo(d2) ? "":"un");
92
93 if (d2) d2->release();
94 s->release();
95 if (d) d->release();
96
97 return KMOD_RETURN_SUCCESS;
98 }
99
100 kern_return_t
101 test1_stop(struct kmod_info *ki, void *data)
102 {
103 return KMOD_RETURN_SUCCESS;
104 }