]> git.saurik.com Git - apple/security.git/blob - keychain/SecureObjectSync/Regressions/sc-45-digestvector.c
Security-59754.41.1.tar.gz
[apple/security.git] / keychain / SecureObjectSync / Regressions / sc-45-digestvector.c
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #include "SOSCircle_regressions.h"
26
27 #include "keychain/SecureObjectSync/SOSDigestVector.h"
28
29 #include <utilities/SecCFRelease.h>
30 #include <stdlib.h>
31 #include "SOSRegressionUtilities.h"
32
33 static void testNullDigestVector(void)
34 {
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");
39 }
40
41 static CFStringRef dvCopyString(const struct SOSDigestVector *dv)
42 {
43 CFMutableStringRef desc = CFStringCreateMutable(kCFAllocatorDefault, 0);
44 SOSDigestVectorApplySorted(dv, ^(const uint8_t *digest, bool *stop) {
45 char buf[2] = {};
46 buf[0] = digest[0];
47 CFStringAppendCString(desc, buf, kCFStringEncodingUTF8);
48 });
49 return desc;
50 }
51
52 static void testIntersectUnionDigestVector(void)
53 {
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 ");
62
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 ");
68
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 ");
78
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 ");
84
85 struct SOSDigestVector dvintersect = SOSDigestVectorInit;
86 SOSDigestVectorIntersectSorted(&dv1, &dv2, &dvintersect);
87 CFStringRef desc = dvCopyString(&dvintersect);
88 ok(CFEqual(CFSTR("df"), desc), "intersection is %@", desc);
89 CFReleaseNull(desc);
90
91 struct SOSDigestVector dvunion = SOSDigestVectorInit;
92 SOSDigestVectorUnionSorted(&dv1, &dv2, &dvunion);
93 desc = dvCopyString(&dvunion);
94 ok(CFEqual(CFSTR("abcdefgh"), desc), "union is %@", desc);
95 CFReleaseNull(desc);
96
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);
102 CFReleaseNull(desc);
103 desc = dvCopyString(&dvadds);
104 ok(CFEqual(CFSTR("ceg"), desc), "adds is %@", desc);
105 CFReleaseNull(desc);
106
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"));
113 CFReleaseNull(desc);
114
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"));
120 CFReleaseNull(desc);
121
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"));
129 CFReleaseNull(desc);
130
131 SOSDigestVectorUniqueSorted(&dvu);
132 desc = dvCopyString(&dvu);
133 ok(CFEqual(CFSTR("abcdefgh"), desc), "uniqued dvu is %@, should be: %@", desc, CFSTR("abcdefgh"));
134 CFReleaseNull(desc);
135
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"));
140 CFReleaseNull(desc);
141
142 SOSDigestVectorFree(&dv1);
143 SOSDigestVectorFree(&dv2);
144 SOSDigestVectorFree(&dvu);
145 SOSDigestVectorFree(&dvintersect);
146 SOSDigestVectorFree(&dvunion);
147 SOSDigestVectorFree(&dvdels);
148 SOSDigestVectorFree(&dvadds);
149 SOSDigestVectorFree(&dvpatched);
150 }
151
152 static void tests(void)
153 {
154 testNullDigestVector();
155 testIntersectUnionDigestVector();
156 }
157
158 int sc_45_digestvector(int argc, char *const *argv)
159 {
160 #if SOS_ENABLED
161 plan_tests(15);
162 tests();
163 #else
164 plan_tests(0);
165 #endif
166 return 0;
167 }