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