2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #include <SecureObjectSync/SOSManifest.h>
26 #include <SecureObjectSync/SOSMessage.h>
28 #include "SOSCircle_regressions.h"
30 #include <utilities/SecCFWrappers.h>
31 #include <utilities/der_plist.h>
32 #include <SecureObjectSync/SOSDigestVector.h>
33 #include <securityd/SecDbItem.h>
35 static int kTestTestCount
= 68;
37 static void testNullMessage(uint64_t msgid
)
39 SOSMessageRef sentMessage
= NULL
;
40 SOSMessageRef rcvdMessage
= NULL
;
41 SOSManifestRef sender
= NULL
;
42 CFErrorRef error
= NULL
;
43 CFDataRef data
= NULL
;
46 ok(sender
= SOSManifestCreateWithBytes(NULL
, 0, &error
), "empty sender manifest create: %@", error
);
48 ok(sentMessage
= SOSMessageCreateWithManifests(kCFAllocatorDefault
, sender
, NULL
, NULL
, false, &error
), "sentMessage create: %@", error
);
50 ok(data
= SOSMessageCreateData(sentMessage
, msgid
, &error
), "sentMessage data create: %@", error
);
54 ok(rcvdMessage
= SOSMessageCreateWithData(kCFAllocatorDefault
, data
, &error
), "rcvdMessage create: %@", error
);
56 __block
size_t numObjects
= 0;
57 SOSMessageWithObjects(sentMessage
, &error
, ^(CFDataRef object
, bool *stop
) {
60 ok(numObjects
== 0, "no objects");
62 // Check if we got what we started with
63 ok(sentMessage
&& rcvdMessage
&& CFEqual(sentMessage
, rcvdMessage
), "sent %@ == rcvd %@", sentMessage
, rcvdMessage
);
66 CFReleaseNull(sentMessage
);
67 CFReleaseNull(rcvdMessage
);
68 CFReleaseNull(sender
);
71 __unused
static void testFlaggedMessage(uint64_t msgid
, SOSMessageFlags flags
)
73 SOSMessageRef sentMessage
= NULL
;
74 SOSMessageRef rcvdMessage
= NULL
;
75 SOSManifestRef sender
= NULL
;
76 CFErrorRef error
= NULL
;
77 CFDataRef data
= NULL
;
79 ok(sender
= SOSManifestCreateWithBytes(NULL
, 0, &error
), "empty sender manifest create: %@", error
);
81 ok(sentMessage
= SOSMessageCreateWithManifests(kCFAllocatorDefault
, sender
, NULL
, NULL
, false, &error
), "sentMessage create: %@", error
);
83 SOSMessageSetFlags(sentMessage
, flags
);
84 ok(data
= SOSMessageCreateData(sentMessage
, msgid
, &error
), "sentMessage data create: %@", error
);
88 ok(rcvdMessage
= SOSMessageCreateWithData(kCFAllocatorDefault
, data
, &error
), "rcvdMessage create: %@", error
);
90 __block
size_t numObjects
= 0;
91 SOSMessageWithObjects(sentMessage
, &error
, ^(CFDataRef object
, bool *stop
) {
94 ok(numObjects
== 0, "no objects");
96 is(SOSMessageGetFlags(sentMessage
), flags
, "flags match after roundtrip");
97 ok(sentMessage
&& rcvdMessage
&& CFEqual(sentMessage
, rcvdMessage
), "sent %@ == rcvd %@", sentMessage
, rcvdMessage
);
100 CFReleaseNull(sentMessage
);
101 CFReleaseNull(rcvdMessage
);
102 CFReleaseNull(sender
);
105 __unused
static void testDeltaManifestMessage(uint64_t msgid
)
107 SOSMessageRef sentMessage
= NULL
;
108 SOSMessageRef rcvdMessage
= NULL
;
109 SOSManifestRef sender
= NULL
;
110 SOSManifestRef proposed
= NULL
;
111 SOSManifestRef base
= NULL
;
112 CFErrorRef error
= NULL
;
113 CFDataRef data
= NULL
;
115 struct SOSDigestVector dv
= SOSDigestVectorInit
;
116 SOSDigestVectorAppend(&dv
, (const uint8_t *)"sha1 hash that is 20 bytes long or so and stuff");
117 SOSDigestVectorAppend(&dv
, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
118 SOSDigestVectorSort(&dv
);
119 base
= SOSManifestCreateWithBytes((const uint8_t *)dv
.digest
, dv
.count
* SOSDigestSize
, &error
);
120 SOSDigestVectorAppend(&dv
, (const uint8_t *)"so much more is good to see here is another one for me");
121 SOSDigestVectorAppend(&dv
, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff!");
122 SOSDigestVectorAppend(&dv
, (const uint8_t *)"so much for is good to see here is another one for me");
123 SOSDigestVectorSort(&dv
);
125 proposed
= SOSManifestCreateWithBytes((const uint8_t *)dv
.digest
, dv
.count
* SOSDigestSize
, &error
);
127 CFReleaseNull(error
);
128 ok(sentMessage
= SOSMessageCreateWithManifests(kCFAllocatorDefault
, proposed
, base
, proposed
, true, &error
), "sentMessage create: %@", error
);
129 CFReleaseNull(error
);
130 ok(data
= SOSMessageCreateData(sentMessage
, msgid
, &error
), "sentMessage data create: %@ .. %@", error
, sentMessage
);
131 CFReleaseNull(error
);
134 ok(rcvdMessage
= SOSMessageCreateWithData(kCFAllocatorDefault
, data
, &error
), "rcvdMessage create: %@", error
);
135 CFReleaseNull(error
);
136 __block
size_t numObjects
= 0;
137 SOSMessageWithObjects(sentMessage
, &error
, ^(CFDataRef object
, bool *stop
) {
140 ok(numObjects
== 0, "no objects");
142 ok(sentMessage
&& rcvdMessage
&& CFEqual(sentMessage
, rcvdMessage
), "sent %@ == rcvd %@", sentMessage
, rcvdMessage
);
145 CFReleaseNull(sentMessage
);
146 CFReleaseNull(rcvdMessage
);
147 CFReleaseNull(sender
);
150 static CFDataRef
testCopyAddedObject(SOSMessageRef message
, CFPropertyListRef plist
)
152 CFErrorRef error
= NULL
;
154 ok(der
= kc_plist_copy_der(plist
, &error
), "copy der: %@", error
);
155 CFReleaseNull(error
);
156 ok(SOSMessageAppendObject(message
, der
, &error
), "likes object: %@", error
);
157 CFReleaseNull(error
);
161 __unused
static void testObjectsMessage(uint64_t msgid
)
163 SOSMessageRef sentMessage
= NULL
;
164 SOSMessageRef rcvdMessage
= NULL
;
165 SOSManifestRef sender
= NULL
;
166 SOSManifestRef proposed
= NULL
;
167 SOSManifestRef base
= NULL
;
168 CFErrorRef error
= NULL
;
169 CFDataRef data
= NULL
;
171 struct SOSDigestVector dv1
= SOSDigestVectorInit
;
172 struct SOSDigestVector dv2
= SOSDigestVectorInit
;
173 SOSDigestVectorAppend(&dv1
, (const uint8_t *)"sha1 hash that is 20 bytes long or so and stuff");
174 SOSDigestVectorAppend(&dv2
, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
175 SOSDigestVectorAppend(&dv1
, (const uint8_t *)"so much more is good to see here is another one for me");
176 SOSDigestVectorAppend(&dv2
, (const uint8_t *)"so much more is good to see here is another one for me");
177 SOSDigestVectorAppend(&dv1
, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff");
178 SOSDigestVectorAppend(&dv1
, (const uint8_t *)"sha1 hash that was 23 bytes long or so and stuff!");
179 SOSDigestVectorAppend(&dv2
, (const uint8_t *)"so much for is good to see here is another one for me");
180 SOSDigestVectorSort(&dv1
);
181 SOSDigestVectorSort(&dv2
);
182 base
= SOSManifestCreateWithBytes((const uint8_t *)dv1
.digest
, dv1
.count
* SOSDigestSize
, &error
);
184 proposed
= SOSManifestCreateWithBytes((const uint8_t *)dv2
.digest
, dv2
.count
* SOSDigestSize
, &error
);
185 CFReleaseNull(error
);
186 ok(sentMessage
= SOSMessageCreateWithManifests(kCFAllocatorDefault
, proposed
, base
, proposed
, true, &error
), "sentMessage create: %@", error
);
187 CFDataRef O0
, O1
, O2
, O3
;
188 CFDataRef o0
= CFDataCreate(kCFAllocatorDefault
, NULL
, 0);
189 O0
= testCopyAddedObject(sentMessage
, o0
);
190 CFDataRef o1
= CFDataCreate(kCFAllocatorDefault
, (const UInt8
*)"test", 4);
191 O1
= testCopyAddedObject(sentMessage
, o1
);
192 CFDataRef o2
= CFDataCreate(kCFAllocatorDefault
, (const UInt8
*)"what an object", 14);
193 O2
= testCopyAddedObject(sentMessage
, o2
);
194 CFDataRef o3
= CFDataCreate(kCFAllocatorDefault
, (const UInt8
*)"This one even has shiny stripe.", 31);
195 O3
= testCopyAddedObject(sentMessage
, o3
);
196 ok(data
= SOSMessageCreateData(sentMessage
, msgid
, &error
), "sentMessage data create: %@ .. %@", error
, sentMessage
);
197 CFReleaseNull(error
);
200 ok(rcvdMessage
= SOSMessageCreateWithData(kCFAllocatorDefault
, data
, &error
), "rcvdMessage create: %@", error
);
201 CFReleaseNull(error
);
202 __block
size_t numObjects
= 0;
203 __block
bool f0
, f1
, f2
, f3
;
204 f0
= f1
= f2
= f3
= false;
205 SOSMessageWithObjects(rcvdMessage
, &error
, ^(CFDataRef object
, bool *stop
) {
206 if (CFEqualSafe(object
, O0
)) f0
= true;
207 if (CFEqualSafe(object
, O1
)) f1
= true;
208 if (CFEqualSafe(object
, O2
)) f2
= true;
209 if (CFEqualSafe(object
, O3
)) f3
= true;
217 ok(sentMessage
&& rcvdMessage
&& CFEqual(sentMessage
, rcvdMessage
), "sent %@ == rcvd %@", sentMessage
, rcvdMessage
);
228 CFReleaseNull(sentMessage
);
229 CFReleaseNull(rcvdMessage
);
230 CFReleaseNull(sender
);
233 static void tests(void)
237 testNullMessage(0); // v0
240 todo("V2 doesn't work");
243 testNullMessage(++msgid
); // v2
244 testFlaggedMessage(++msgid
, 0x865);
245 testFlaggedMessage(++msgid
, 0xdeadbeef);
246 testDeltaManifestMessage(0);
247 testDeltaManifestMessage(++msgid
);
248 testObjectsMessage(0);
249 testObjectsMessage(++msgid
);
254 int sc_50_message(int argc
, char *const *argv
)
256 plan_tests(kTestTestCount
);