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 "SOSCircle_regressions.h"
27 #include "keychain/SecureObjectSync/SOSDigestVector.h"
29 #include <utilities/SecCFRelease.h>
31 #include "SOSRegressionUtilities.h"
33 static void testNullDigestVector(void)
35 struct SOSDigestVector dv
= SOSDigestVectorInit
;
36 is(dv
.count
, (size_t)0, "count is 0");
37 is(dv
.capacity
, (size_t)0, "capacity is 0");
38 ok(!dv
.unsorted
, "unsorted is false");
41 static CFStringRef
dvCopyString(const struct SOSDigestVector
*dv
)
43 CFMutableStringRef desc
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
44 SOSDigestVectorApplySorted(dv
, ^(const uint8_t *digest
, bool *stop
) {
47 CFStringAppendCString(desc
, buf
, kCFStringEncodingUTF8
);
52 static void testIntersectUnionDigestVector(void)
54 struct SOSDigestVector dv1
= SOSDigestVectorInit
;
55 struct SOSDigestVector dv2
= SOSDigestVectorInit
;
56 struct SOSDigestVector dvu
= SOSDigestVectorInit
;
57 SOSDigestVectorAppend(&dv1
, (void *)"a ");
58 SOSDigestVectorAppend(&dv1
, (void *)"b ");
59 SOSDigestVectorAppend(&dv1
, (void *)"d ");
60 SOSDigestVectorAppend(&dv1
, (void *)"f ");
61 SOSDigestVectorAppend(&dv1
, (void *)"h ");
63 SOSDigestVectorAppend(&dv2
, (void *)"c ");
64 SOSDigestVectorAppend(&dv2
, (void *)"d ");
65 SOSDigestVectorAppend(&dv2
, (void *)"e ");
66 SOSDigestVectorAppend(&dv2
, (void *)"f ");
67 SOSDigestVectorAppend(&dv2
, (void *)"g ");
69 SOSDigestVectorAppend(&dvu
, (void *)"a ");
70 SOSDigestVectorAppend(&dvu
, (void *)"b ");
71 SOSDigestVectorAppend(&dvu
, (void *)"b ");
72 SOSDigestVectorAppend(&dvu
, (void *)"b ");
73 SOSDigestVectorAppend(&dvu
, (void *)"b ");
74 SOSDigestVectorAppend(&dvu
, (void *)"c ");
75 SOSDigestVectorAppend(&dvu
, (void *)"d ");
76 SOSDigestVectorAppend(&dvu
, (void *)"f ");
77 SOSDigestVectorAppend(&dvu
, (void *)"h ");
79 SOSDigestVectorAppend(&dvu
, (void *)"c ");
80 SOSDigestVectorAppend(&dvu
, (void *)"d ");
81 SOSDigestVectorAppend(&dvu
, (void *)"e ");
82 SOSDigestVectorAppend(&dvu
, (void *)"f ");
83 SOSDigestVectorAppend(&dvu
, (void *)"g ");
85 struct SOSDigestVector dvintersect
= SOSDigestVectorInit
;
86 SOSDigestVectorIntersectSorted(&dv1
, &dv2
, &dvintersect
);
87 CFStringRef desc
= dvCopyString(&dvintersect
);
88 ok(CFEqual(CFSTR("df"), desc
), "intersection is %@", desc
);
91 struct SOSDigestVector dvunion
= SOSDigestVectorInit
;
92 SOSDigestVectorUnionSorted(&dv1
, &dv2
, &dvunion
);
93 desc
= dvCopyString(&dvunion
);
94 ok(CFEqual(CFSTR("abcdefgh"), desc
), "union is %@", desc
);
97 struct SOSDigestVector dvdels
= SOSDigestVectorInit
;
98 struct SOSDigestVector dvadds
= SOSDigestVectorInit
;
99 SOSDigestVectorDiffSorted(&dv1
, &dv2
, &dvdels
, &dvadds
);
100 desc
= dvCopyString(&dvdels
);
101 ok(CFEqual(CFSTR("abh"), desc
), "dels is %@", desc
);
103 desc
= dvCopyString(&dvadds
);
104 ok(CFEqual(CFSTR("ceg"), desc
), "adds is %@", desc
);
107 CFErrorRef localError
= NULL
;
108 struct SOSDigestVector dvpatched
= SOSDigestVectorInit
;
109 ok(SOSDigestVectorPatch(&dv1
, &dvdels
, &dvadds
, &dvpatched
, &localError
), "patch : %@", localError
);
110 CFReleaseNull(localError
);
111 desc
= dvCopyString(&dvpatched
);
112 ok(CFEqual(CFSTR("cdefg"), desc
), "patched dv1 - dels + adds is %@, should be: %@", desc
, CFSTR("cdefg"));
115 SOSDigestVectorFree(&dvpatched
);
116 ok(SOSDigestVectorPatch(&dv2
, &dvadds
, &dvdels
, &dvpatched
, &localError
), "patch : %@", localError
);
117 CFReleaseNull(localError
);
118 desc
= dvCopyString(&dvpatched
);
119 ok(CFEqual(CFSTR("abdfh"), desc
), "patched dv2 - adds + dels is is %@, should be: %@", desc
, CFSTR("abdfh"));
122 SOSDigestVectorAppend(&dvadds
, (void *)"c ");
123 SOSDigestVectorFree(&dvpatched
);
124 SOSDigestVectorUniqueSorted(&dvadds
);
125 ok(SOSDigestVectorPatch(&dv2
, &dvadds
, &dvdels
, &dvpatched
, &localError
), "patch failed: %@", localError
);
126 CFReleaseNull(localError
);
127 desc
= dvCopyString(&dvpatched
);
128 ok(CFEqual(CFSTR("abdfh"), desc
), "patched dv2 - adds + dels is is %@, should be: %@", desc
, CFSTR("abdfh"));
131 SOSDigestVectorUniqueSorted(&dvu
);
132 desc
= dvCopyString(&dvu
);
133 ok(CFEqual(CFSTR("abcdefgh"), desc
), "uniqued dvu is %@, should be: %@", desc
, CFSTR("abcdefgh"));
136 // This operation should be idempotent
137 SOSDigestVectorUniqueSorted(&dvu
);
138 desc
= dvCopyString(&dvu
);
139 ok(CFEqual(CFSTR("abcdefgh"), desc
), "uniqued dvu is %@, should be: %@", desc
, CFSTR("abcdefgh"));
142 SOSDigestVectorFree(&dv1
);
143 SOSDigestVectorFree(&dv2
);
144 SOSDigestVectorFree(&dvu
);
145 SOSDigestVectorFree(&dvintersect
);
146 SOSDigestVectorFree(&dvunion
);
147 SOSDigestVectorFree(&dvdels
);
148 SOSDigestVectorFree(&dvadds
);
149 SOSDigestVectorFree(&dvpatched
);
152 static void tests(void)
154 testNullDigestVector();
155 testIntersectUnionDigestVector();
158 int sc_45_digestvector(int argc
, char *const *argv
)