]> git.saurik.com Git - apple/security.git/blob - sec/Security/Regressions/otr/otr-packetdata.c
Security-55471.14.8.tar.gz
[apple/security.git] / sec / Security / Regressions / otr / otr-packetdata.c
1 //
2 // otr-packetdata.c
3 // OTR
4 //
5 // Created by Mitch Adler on 7/22/11.
6 // Copyright (c) 2011 Apple Inc. All rights reserved.
7 //
8
9 #include <CoreFoundation/CFData.h>
10 #include "SecOTRPacketData.h"
11
12 #include <corecrypto/ccn.h>
13
14 #include "security_regressions.h"
15
16 static bool CFDataMatches(CFDataRef data, size_t amount, const uint8_t* bytes)
17 {
18 if ((size_t) CFDataGetLength(data) != amount)
19 return false;
20
21 return 0 == memcmp(CFDataGetBytePtr(data), bytes, amount);
22
23 }
24
25
26 static void testAppendShort()
27 {
28 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
29
30 const uint8_t firstResult[] = { 1, 2 };
31 AppendShort(result, 0x0102);
32 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "Didn't insert correctly");
33
34 const uint8_t secondResult[] = { 1, 2, 3, 4};
35 AppendShort(result, 0x0304);
36 ok(CFDataMatches(result, sizeof(secondResult), secondResult), "Didn't append!");
37
38 CFRelease(result);
39
40 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
41
42 const uint8_t thirdResult[] = { 0, 0 };
43 AppendShort(result, 0x0);
44 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "Didn't insert zero");
45
46 CFRelease(result);
47
48 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
49
50 const uint8_t fourthResult[] = { 0xFF, 0xFF};
51 AppendShort(result, 0xFFFF);
52 ok(CFDataMatches(result, sizeof(thirdResult), fourthResult), "Didn't insert 0xFFFFFFFF");
53
54 CFRelease(result);
55 }
56
57 static void testAppendLong()
58 {
59 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
60
61 const uint8_t firstResult[] = { 1, 2, 3, 4 };
62 AppendLong(result, 0x01020304);
63 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "Didn't insert correctly");
64
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!");
68
69 CFRelease(result);
70
71 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
72
73 const uint8_t thirdResult[] = { 0, 0, 0, 0 };
74 AppendLong(result, 0x0);
75 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "Didn't insert zero");
76
77 CFRelease(result);
78
79 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
80
81 const uint8_t fourthResult[] = { 0xFF, 0xFF, 0xFF, 0xFF };
82 AppendLong(result, 0xFFFFFFFF);
83 ok(CFDataMatches(result, sizeof(thirdResult), fourthResult), "Didn't insert 0xFFFFFFFF");
84
85 CFRelease(result);
86 }
87
88 static void testAppendData()
89 {
90 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
91
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");
95
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!");
99
100 CFRelease(result);
101
102 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
103
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");
107
108 CFRelease(result);
109
110 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
111
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");
115
116 CFRelease(result);
117 }
118
119
120 static void testAppendMPI()
121 {
122 const size_t kUnitBufferN = 1024;
123 cc_unit unitBuffer[1024];
124
125 CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
126
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);
130
131 ok(CFDataMatches(result, sizeof(firstResult), firstResult), "Didn't insert zero");
132
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);
136
137 ok(CFDataMatches(result, sizeof(secondResult), secondResult), "Didn't append zero");
138
139 CFRelease(result);
140
141 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
142
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);
146
147 ok(CFDataMatches(result, sizeof(thirdResult), thirdResult), "Didn't insert zero");
148
149 CFRelease(result);
150
151 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
152
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);
156
157 ok(CFDataMatches(result, sizeof(fourthResult), fourthResult), "Didn't insert zero");
158
159 CFRelease(result);
160
161 result = CFDataCreateMutable(kCFAllocatorDefault, 0);
162
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);
167
168 ok(CFDataMatches(result, sizeof(shortenedResult), shortenedResult), "Didn't insert zero");
169
170 CFRelease(result);
171
172 }
173
174 int otr_packetdata(int argc, char *const * argv)
175 {
176 plan_tests(17);
177
178 testAppendShort();
179 testAppendLong();
180 testAppendData();
181 testAppendMPI();
182
183 return 0;
184 }
185