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>
32 static int kTestTestCount
= 15;
34 static void testNullDigestVector(void)
36 struct SOSDigestVector dv
= SOSDigestVectorInit
;
37 is(dv
.count
, (size_t)0, "count is 0");
38 is(dv
.capacity
, (size_t)0, "capacity is 0");
39 ok(!dv
.unsorted
, "unsorted is false");
42 static CFStringRef
dvCopyString(const struct SOSDigestVector
*dv
)
44 CFMutableStringRef desc
= CFStringCreateMutable(kCFAllocatorDefault
, 0);
45 SOSDigestVectorApplySorted(dv
, ^(const uint8_t *digest
, bool *stop
) {
48 CFStringAppendCString(desc
, buf
, kCFStringEncodingUTF8
);
53 static void testIntersectUnionDigestVector(void)
55 struct SOSDigestVector dv1
= SOSDigestVectorInit
;
56 struct SOSDigestVector dv2
= SOSDigestVectorInit
;
57 struct SOSDigestVector dvu
= SOSDigestVectorInit
;
58 SOSDigestVectorAppend(&dv1
, (void *)"a ");
59 SOSDigestVectorAppend(&dv1
, (void *)"b ");
60 SOSDigestVectorAppend(&dv1
, (void *)"d ");
61 SOSDigestVectorAppend(&dv1
, (void *)"f ");
62 SOSDigestVectorAppend(&dv1
, (void *)"h ");
64 SOSDigestVectorAppend(&dv2
, (void *)"c ");
65 SOSDigestVectorAppend(&dv2
, (void *)"d ");
66 SOSDigestVectorAppend(&dv2
, (void *)"e ");
67 SOSDigestVectorAppend(&dv2
, (void *)"f ");
68 SOSDigestVectorAppend(&dv2
, (void *)"g ");
70 SOSDigestVectorAppend(&dvu
, (void *)"a ");
71 SOSDigestVectorAppend(&dvu
, (void *)"b ");
72 SOSDigestVectorAppend(&dvu
, (void *)"b ");
73 SOSDigestVectorAppend(&dvu
, (void *)"b ");
74 SOSDigestVectorAppend(&dvu
, (void *)"b ");
75 SOSDigestVectorAppend(&dvu
, (void *)"c ");
76 SOSDigestVectorAppend(&dvu
, (void *)"d ");
77 SOSDigestVectorAppend(&dvu
, (void *)"f ");
78 SOSDigestVectorAppend(&dvu
, (void *)"h ");
80 SOSDigestVectorAppend(&dvu
, (void *)"c ");
81 SOSDigestVectorAppend(&dvu
, (void *)"d ");
82 SOSDigestVectorAppend(&dvu
, (void *)"e ");
83 SOSDigestVectorAppend(&dvu
, (void *)"f ");
84 SOSDigestVectorAppend(&dvu
, (void *)"g ");
86 struct SOSDigestVector dvintersect
= SOSDigestVectorInit
;
87 SOSDigestVectorIntersectSorted(&dv1
, &dv2
, &dvintersect
);
88 CFStringRef desc
= dvCopyString(&dvintersect
);
89 ok(CFEqual(CFSTR("df"), desc
), "intersection is %@", desc
);
92 struct SOSDigestVector dvunion
= SOSDigestVectorInit
;
93 SOSDigestVectorUnionSorted(&dv1
, &dv2
, &dvunion
);
94 desc
= dvCopyString(&dvunion
);
95 ok(CFEqual(CFSTR("abcdefgh"), desc
), "union is %@", desc
);
98 struct SOSDigestVector dvdels
= SOSDigestVectorInit
;
99 struct SOSDigestVector dvadds
= SOSDigestVectorInit
;
100 SOSDigestVectorDiffSorted(&dv1
, &dv2
, &dvdels
, &dvadds
);
101 desc
= dvCopyString(&dvdels
);
102 ok(CFEqual(CFSTR("abh"), desc
), "dels is %@", desc
);
104 desc
= dvCopyString(&dvadds
);
105 ok(CFEqual(CFSTR("ceg"), desc
), "adds is %@", desc
);
108 CFErrorRef localError
= NULL
;
109 struct SOSDigestVector dvpatched
= SOSDigestVectorInit
;
110 ok(SOSDigestVectorPatch(&dv1
, &dvdels
, &dvadds
, &dvpatched
, &localError
), "patch : %@", localError
);
111 CFReleaseNull(localError
);
112 desc
= dvCopyString(&dvpatched
);
113 ok(CFEqual(CFSTR("cdefg"), desc
), "patched dv1 - dels + adds is %@, should be: %@", desc
, CFSTR("cdefg"));
116 SOSDigestVectorFree(&dvpatched
);
117 ok(SOSDigestVectorPatch(&dv2
, &dvadds
, &dvdels
, &dvpatched
, &localError
), "patch : %@", localError
);
118 CFReleaseNull(localError
);
119 desc
= dvCopyString(&dvpatched
);
120 ok(CFEqual(CFSTR("abdfh"), desc
), "patched dv2 - adds + dels is is %@, should be: %@", desc
, CFSTR("abdfh"));
123 SOSDigestVectorAppend(&dvadds
, (void *)"c ");
124 SOSDigestVectorFree(&dvpatched
);
125 SOSDigestVectorUniqueSorted(&dvadds
);
126 ok(SOSDigestVectorPatch(&dv2
, &dvadds
, &dvdels
, &dvpatched
, &localError
), "patch failed: %@", localError
);
127 CFReleaseNull(localError
);
128 desc
= dvCopyString(&dvpatched
);
129 ok(CFEqual(CFSTR("abdfh"), desc
), "patched dv2 - adds + dels is is %@, should be: %@", desc
, CFSTR("abdfh"));
132 SOSDigestVectorUniqueSorted(&dvu
);
133 desc
= dvCopyString(&dvu
);
134 ok(CFEqual(CFSTR("abcdefgh"), desc
), "uniqued dvu is %@, should be: %@", desc
, CFSTR("abcdefgh"));
137 // This operation should be idempotent
138 SOSDigestVectorUniqueSorted(&dvu
);
139 desc
= dvCopyString(&dvu
);
140 ok(CFEqual(CFSTR("abcdefgh"), desc
), "uniqued dvu is %@, should be: %@", desc
, CFSTR("abcdefgh"));
143 SOSDigestVectorFree(&dv1
);
144 SOSDigestVectorFree(&dv2
);
145 SOSDigestVectorFree(&dvu
);
146 SOSDigestVectorFree(&dvintersect
);
147 SOSDigestVectorFree(&dvunion
);
148 SOSDigestVectorFree(&dvdels
);
149 SOSDigestVectorFree(&dvadds
);
150 SOSDigestVectorFree(&dvpatched
);
153 static void tests(void)
155 testNullDigestVector();
156 testIntersectUnionDigestVector();
159 int sc_45_digestvector(int argc
, char *const *argv
)
161 plan_tests(kTestTestCount
);