]>
git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/otr/otr-packetdata.c
2 * Copyright (c) 2011-2012,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 <CoreFoundation/CFData.h>
26 #include "SecOTRPacketData.h"
28 #include <corecrypto/ccn.h>
30 #include "Security_regressions.h"
32 static bool CFDataMatches(CFDataRef data
, size_t amount
, const uint8_t* bytes
)
34 if ((size_t) CFDataGetLength(data
) != amount
)
37 return 0 == memcmp(CFDataGetBytePtr(data
), bytes
, amount
);
42 static void testAppendShort()
44 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
46 const uint8_t firstResult
[] = { 1, 2 };
47 AppendShort(result
, 0x0102);
48 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "Didn't insert correctly");
50 const uint8_t secondResult
[] = { 1, 2, 3, 4};
51 AppendShort(result
, 0x0304);
52 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "Didn't append!");
56 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
58 const uint8_t thirdResult
[] = { 0, 0 };
59 AppendShort(result
, 0x0);
60 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "Didn't insert zero");
64 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
66 const uint8_t fourthResult
[] = { 0xFF, 0xFF};
67 AppendShort(result
, 0xFFFF);
68 ok(CFDataMatches(result
, sizeof(fourthResult
), fourthResult
), "Didn't insert 0xFFFFFFFF");
73 static void testAppendLong()
75 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
77 const uint8_t firstResult
[] = { 1, 2, 3, 4 };
78 AppendLong(result
, 0x01020304);
79 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "Didn't insert correctly");
81 const uint8_t secondResult
[] = { 1, 2, 3, 4, 5, 6, 7, 8};
82 AppendLong(result
, 0x05060708);
83 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "Didn't append!");
87 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
89 const uint8_t thirdResult
[] = { 0, 0, 0, 0 };
90 AppendLong(result
, 0x0);
91 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "Didn't insert zero");
95 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
97 const uint8_t fourthResult
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
98 AppendLong(result
, 0xFFFFFFFF);
99 ok(CFDataMatches(result
, sizeof(fourthResult
), fourthResult
), "Didn't insert 0xFFFFFFFF");
104 static void testAppendLongLong()
106 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
108 const uint8_t firstResult
[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
109 AppendLongLong(result
, 0x0102030405060708);
110 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "insert correctly");
112 const uint8_t secondResult
[] = { 1, 2, 3, 4, 5, 6, 7, 8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x10};
113 AppendLongLong(result
, 0x090A0B0C0D0E0F10);
114 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "append!");
118 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
120 const uint8_t thirdResult
[] = { 0, 0, 0, 0, 0, 0, 0, 0};
121 AppendLongLong(result
, 0x0);
122 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "insert zero");
126 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
128 const uint8_t fourthResult
[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
129 AppendLongLong(result
, 0xFFFFFFFFFFFFFFFF);
130 ok(CFDataMatches(result
, sizeof(fourthResult
), fourthResult
), "insert 0xFFFFFFFFFFFFFFF");
135 static void testAppendLongLongCompact()
137 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
139 const uint64_t firstValue
= 0x000001FC07F;
140 const uint8_t firstResult
[] = { 0xFF,0x80,0x7F };
141 AppendLongLongCompact(result
, firstValue
);
142 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "insert correctly");
145 const uint8_t *readPtr
= firstResult
;
146 size_t size
= sizeof(firstResult
);
147 ReadLongLongCompact(&readPtr
, &size
, &readback
);
149 is(readback
, firstValue
, "read back");
151 const uint8_t secondResult
[] = { 0xFF,0x80,0x7F, 0x82, 0x80, 0x81, 0x80, 0xff, 0x80, 0x7f };
152 AppendLongLongCompact(result
, 0x800101FC07F);
153 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "append!");
157 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
159 const uint8_t thirdResult
[] = { 0 };
160 AppendLongLongCompact(result
, 0x0);
161 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "insert zero");
163 readPtr
= thirdResult
;
164 size
= sizeof(thirdResult
);
165 ReadLongLongCompact(&readPtr
, &size
, &readback
);
166 is(readback
, 0ULL, "read back");
170 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
172 const uint8_t fourthResult
[] = { 0x81, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F };
173 AppendLongLongCompact(result
, 0xFFFFFFFFFFFFFFFF);
174 ok(CFDataMatches(result
, sizeof(fourthResult
), fourthResult
), "insert 0xFFFFFFFFFFFFFFF");
176 readPtr
= fourthResult
;
177 size
= sizeof(fourthResult
);
178 ReadLongLongCompact(&readPtr
, &size
, &readback
);
179 is(readback
, 0xFFFFFFFFFFFFFFFF, "read back");
184 static void testAppendData()
186 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
188 const uint8_t firstResult
[] = { 0, 0, 0, 4, 1, 2, 3, 4 };
189 AppendDATA(result
, sizeof(firstResult
) - 4, firstResult
+ 4);
190 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "Didn't insert correctly");
192 const uint8_t secondResult
[] = { 0, 0, 0, 4, 1, 2, 3, 4, 0, 0, 0, 1, 0 };
193 AppendDATA(result
, sizeof(secondResult
) - 12, secondResult
+ 12);
194 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "Didn't append!");
198 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
200 const uint8_t thirdResult
[] = { 0, 0, 0, 2, 0, 0 };
201 AppendDATA(result
, sizeof(thirdResult
) - 4, thirdResult
+ 4);
202 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "Didn't insert correctly");
206 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
208 const uint8_t fourthResult
[] = { 0, 0, 0, 5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
209 AppendDATA(result
, sizeof(fourthResult
) - 4, fourthResult
+ 4);
210 ok(CFDataMatches(result
, sizeof(fourthResult
), fourthResult
), "Didn't insert correctly");
216 static void testAppendMPI()
218 const size_t kUnitBufferN
= 1024;
219 cc_unit unitBuffer
[1024];
221 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
223 const uint8_t firstResult
[] = { 0, 0, 0, 2, 1, 2 };
224 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(firstResult
) - 4, firstResult
+ 4);
225 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
227 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "Didn't insert zero");
229 const uint8_t secondResult
[] = { 0, 0, 0, 2, 1, 2, 0, 0, 0, 3, 5, 6, 7 };
230 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(secondResult
) - 10, secondResult
+ 10);
231 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
233 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "Didn't append zero");
237 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
239 const uint8_t thirdResult
[] = { 0, 0, 0, 1, 1 };
240 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(thirdResult
) - 4, thirdResult
+ 4);
241 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
243 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "Didn't insert zero");
247 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
249 const uint8_t fourthResult
[] = { 0, 0, 0, 7, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
250 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(fourthResult
) - 4, fourthResult
+ 4);
251 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
253 ok(CFDataMatches(result
, sizeof(fourthResult
), fourthResult
), "Didn't insert zero");
257 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
259 const uint8_t paddedData
[] = { 0, 0xCC, 0xDD, 0xEE, 0xFF, 0xAA };
260 const uint8_t shortenedResult
[] = { 0, 0, 0, 5, 0xCC, 0xDD, 0xEE, 0xFF, 0xAA };
261 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(paddedData
), paddedData
);
262 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
264 ok(CFDataMatches(result
, sizeof(shortenedResult
), shortenedResult
), "Didn't insert zero");
270 int otr_packetdata(int argc
, char *const * argv
)
276 testAppendLongLong();
277 testAppendLongLongCompact();