]>
git.saurik.com Git - apple/security.git/blob - sec/Security/Regressions/otr/otr-packetdata.c
5 // Created by Mitch Adler on 7/22/11.
6 // Copyright (c) 2011 Apple Inc. All rights reserved.
9 #include <CoreFoundation/CFData.h>
10 #include "SecOTRPacketData.h"
12 #include <corecrypto/ccn.h>
14 #include "security_regressions.h"
16 static bool CFDataMatches(CFDataRef data
, size_t amount
, const uint8_t* bytes
)
18 if ((size_t) CFDataGetLength(data
) != amount
)
21 return 0 == memcmp(CFDataGetBytePtr(data
), bytes
, amount
);
26 static void testAppendShort()
28 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
30 const uint8_t firstResult
[] = { 1, 2 };
31 AppendShort(result
, 0x0102);
32 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "Didn't insert correctly");
34 const uint8_t secondResult
[] = { 1, 2, 3, 4};
35 AppendShort(result
, 0x0304);
36 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "Didn't append!");
40 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
42 const uint8_t thirdResult
[] = { 0, 0 };
43 AppendShort(result
, 0x0);
44 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "Didn't insert zero");
48 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
50 const uint8_t fourthResult
[] = { 0xFF, 0xFF};
51 AppendShort(result
, 0xFFFF);
52 ok(CFDataMatches(result
, sizeof(thirdResult
), fourthResult
), "Didn't insert 0xFFFFFFFF");
57 static void testAppendLong()
59 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
61 const uint8_t firstResult
[] = { 1, 2, 3, 4 };
62 AppendLong(result
, 0x01020304);
63 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "Didn't insert correctly");
65 const uint8_t secondResult
[] = { 1, 2, 3, 4, 5, 6, 7, 8};
66 AppendLong(result
, 0x05060708);
67 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "Didn't append!");
71 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
73 const uint8_t thirdResult
[] = { 0, 0, 0, 0 };
74 AppendLong(result
, 0x0);
75 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "Didn't insert zero");
79 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
81 const uint8_t fourthResult
[] = { 0xFF, 0xFF, 0xFF, 0xFF };
82 AppendLong(result
, 0xFFFFFFFF);
83 ok(CFDataMatches(result
, sizeof(thirdResult
), fourthResult
), "Didn't insert 0xFFFFFFFF");
88 static void testAppendData()
90 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
92 const uint8_t firstResult
[] = { 0, 0, 0, 4, 1, 2, 3, 4 };
93 AppendDATA(result
, sizeof(firstResult
) - 4, firstResult
+ 4);
94 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "Didn't insert correctly");
96 const uint8_t secondResult
[] = { 0, 0, 0, 4, 1, 2, 3, 4, 0, 0, 0, 1, 0 };
97 AppendDATA(result
, sizeof(secondResult
) - 12, secondResult
+ 12);
98 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "Didn't append!");
102 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
104 const uint8_t thirdResult
[] = { 0, 0, 0, 2, 0, 0 };
105 AppendDATA(result
, sizeof(thirdResult
) - 4, thirdResult
+ 4);
106 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "Didn't insert correctly");
110 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
112 const uint8_t fourthResult
[] = { 0, 0, 0, 5, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
113 AppendDATA(result
, sizeof(fourthResult
) - 4, fourthResult
+ 4);
114 ok(CFDataMatches(result
, sizeof(fourthResult
), fourthResult
), "Didn't insert correctly");
120 static void testAppendMPI()
122 const size_t kUnitBufferN
= 1024;
123 cc_unit unitBuffer
[1024];
125 CFMutableDataRef result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
127 const uint8_t firstResult
[] = { 0, 0, 0, 2, 1, 2 };
128 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(firstResult
) - 4, firstResult
+ 4);
129 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
131 ok(CFDataMatches(result
, sizeof(firstResult
), firstResult
), "Didn't insert zero");
133 const uint8_t secondResult
[] = { 0, 0, 0, 2, 1, 2, 0, 0, 0, 3, 5, 6, 7 };
134 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(secondResult
) - 10, secondResult
+ 10);
135 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
137 ok(CFDataMatches(result
, sizeof(secondResult
), secondResult
), "Didn't append zero");
141 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
143 const uint8_t thirdResult
[] = { 0, 0, 0, 1, 1 };
144 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(thirdResult
) - 4, thirdResult
+ 4);
145 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
147 ok(CFDataMatches(result
, sizeof(thirdResult
), thirdResult
), "Didn't insert zero");
151 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
153 const uint8_t fourthResult
[] = { 0, 0, 0, 7, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
154 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(fourthResult
) - 4, fourthResult
+ 4);
155 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
157 ok(CFDataMatches(result
, sizeof(fourthResult
), fourthResult
), "Didn't insert zero");
161 result
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
163 const uint8_t paddedData
[] = { 0, 0xCC, 0xDD, 0xEE, 0xFF, 0xAA };
164 const uint8_t shortenedResult
[] = { 0, 0, 0, 5, 0xCC, 0xDD, 0xEE, 0xFF, 0xAA };
165 ccn_read_uint(kUnitBufferN
, unitBuffer
, sizeof(paddedData
), paddedData
);
166 AppendMPI(result
, kUnitBufferN
, unitBuffer
);
168 ok(CFDataMatches(result
, sizeof(shortenedResult
), shortenedResult
), "Didn't insert zero");
174 int otr_packetdata(int argc
, char *const * argv
)