1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* Copyright (c) 2017 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Include the file(s) containing the functions to be tested.
// This permits internal static functions to be visible to this Test Framework.
#include "../parc_Key.c"
#include <LongBow/unit-test.h>
#include <parc/testing/parc_ObjectTesting.h>
LONGBOW_TEST_RUNNER(parc_Key)
{
LONGBOW_RUN_TEST_FIXTURE(Global);
LONGBOW_RUN_TEST_FIXTURE(Errors);
}
LONGBOW_TEST_RUNNER_SETUP(parc_Key)
{
return LONGBOW_STATUS_SUCCEEDED;
}
LONGBOW_TEST_RUNNER_TEARDOWN(parc_Key)
{
return LONGBOW_STATUS_SUCCEEDED;
}
LONGBOW_TEST_FIXTURE(Global)
{
LONGBOW_RUN_TEST_CASE(Global, parcKey_CreateFromDerEncodedPublicKey);
LONGBOW_RUN_TEST_CASE(Global, parcKey_CreateFromSymmetricKey);
LONGBOW_RUN_TEST_CASE(Global, parcKey_Equals);
LONGBOW_RUN_TEST_CASE(Global, parcKey_Copy);
LONGBOW_RUN_TEST_CASE(Global, parcKey_GetKey);
LONGBOW_RUN_TEST_CASE(Global, parcKey_GetKeyId);
LONGBOW_RUN_TEST_CASE(Global, parcKey_GetSigningAlgorithm);
LONGBOW_RUN_TEST_CASE(Global, parcKey_Acquire);
LONGBOW_RUN_TEST_CASE(Global, parcKey_ToString);
}
LONGBOW_TEST_FIXTURE_SETUP(Global)
{
return LONGBOW_STATUS_SUCCEEDED;
}
LONGBOW_TEST_FIXTURE_TEARDOWN(Global)
{
return LONGBOW_STATUS_SUCCEEDED;
}
LONGBOW_TEST_CASE(Global, parcKey_Copy)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromDerEncodedPublicKey(keyid, PARCSigningAlgorithm_RSA, bb_key);
parcKey_AssertValid(key);
PARCKey *copy = parcKey_Copy(key);
parcKey_AssertValid(copy);
assertTrue(parcKey_Equals(key, copy), "Expected the original key instance and its copy to be equal");
parcKey_Release(©);
parcKey_Release(&key);
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
LONGBOW_TEST_CASE(Global, parcKey_CreateFromDerEncodedPublicKey)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromDerEncodedPublicKey(keyid, PARCSigningAlgorithm_RSA, bb_key);
parcKey_AssertValid(key);
parcKey_Release(&key);
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
LONGBOW_TEST_CASE(Global, parcKey_CreateFromSymmetricKey)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromSymmetricKey(keyid, PARCSigningAlgorithm_HMAC, bb_key);
parcKey_AssertValid(key);
parcKey_Release(&key);
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
LONGBOW_TEST_CASE(Global, parcKey_Equals)
{
PARCBuffer *bb_id_1 = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid_1 = parcKeyId_Create(bb_id_1);
parcBuffer_Release(&bb_id_1);
PARCBuffer *bb_id_2 = parcBuffer_Wrap("chugga chugga", 13, 0, 13);
PARCKeyId *keyid_2 = parcKeyId_Create(bb_id_2);
parcBuffer_Release(&bb_id_2);
PARCBufferComposer *composer1 = parcBufferComposer_Create();
parcBufferComposer_PutString(composer1, "quack quack");
PARCBuffer *bb_key_1 = parcBufferComposer_ProduceBuffer(composer1);
PARCKey *x = parcKey_CreateFromDerEncodedPublicKey(keyid_1, PARCSigningAlgorithm_RSA, bb_key_1);
PARCKey *y = parcKey_CreateFromDerEncodedPublicKey(keyid_1, PARCSigningAlgorithm_RSA, bb_key_1);
PARCKey *z = parcKey_CreateFromDerEncodedPublicKey(keyid_1, PARCSigningAlgorithm_RSA, bb_key_1);
PARCBufferComposer *composer2 = parcBufferComposer_Create();
parcBufferComposer_PutString(composer2, "mew mew");
PARCBuffer *bb_key_2 = parcBufferComposer_ProduceBuffer(composer2);
PARCKey *u1 = parcKey_CreateFromDerEncodedPublicKey(keyid_1, PARCSigningAlgorithm_RSA, bb_key_2);
PARCKey *u2 = parcKey_CreateFromDerEncodedPublicKey(keyid_2, PARCSigningAlgorithm_RSA, bb_key_1);
parcObjectTesting_AssertEqualsFunction(parcKey_Equals, x, y, z, u1, u2);
parcBuffer_Release(&bb_key_1);
parcBufferComposer_Release(&composer1);
parcBuffer_Release(&bb_key_2);
parcBufferComposer_Release(&composer2);
parcKeyId_Release(&keyid_1);
parcKeyId_Release(&keyid_2);
parcKey_Release(&x);
parcKey_Release(&y);
parcKey_Release(&z);
parcKey_Release(&u1);
parcKey_Release(&u2);
}
LONGBOW_TEST_CASE(Global, parcKey_GetKey)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromDerEncodedPublicKey(keyid, PARCSigningAlgorithm_RSA, bb_key);
parcKey_AssertValid(key);
PARCBuffer *rawKey = parcKey_GetKey(key); // reference count is not incremented
assertTrue(parcBuffer_Equals(rawKey, bb_key), "Expected the raw key buffers to be equal");
parcKey_Release(&key);
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
LONGBOW_TEST_CASE(Global, parcKey_GetKeyId)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromDerEncodedPublicKey(keyid, PARCSigningAlgorithm_RSA, bb_key);
parcKey_AssertValid(key);
PARCKeyId *rawKeyId = parcKey_GetKeyId(key); // reference count is not incremented
assertTrue(parcKeyId_Equals(rawKeyId, keyid), "Expected the raw KeyID buffers to be equal");
parcKey_Release(&key);
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
LONGBOW_TEST_CASE(Global, parcKey_GetSigningAlgorithm)
{
// Check for PARCSigningAlgorithm_RSA value
PARCBuffer *bb_id_1 = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid_1 = parcKeyId_Create(bb_id_1);
parcBuffer_Release(&bb_id_1);
PARCBufferComposer *composer1 = parcBufferComposer_Create();
parcBufferComposer_PutString(composer1, "quack quack");
PARCBuffer *bb_key_1 = parcBufferComposer_ProduceBuffer(composer1);
PARCKey *key_1 = parcKey_CreateFromDerEncodedPublicKey(keyid_1, PARCSigningAlgorithm_RSA, bb_key_1);
parcKey_AssertValid(key_1);
assertTrue((parcKey_GetSigningAlgorithm(key_1) == PARCSigningAlgorithm_RSA), "Signing Algorithms don't match");
parcBuffer_Release(&bb_key_1);
parcBufferComposer_Release(&composer1);
parcKeyId_Release(&keyid_1);
parcKey_Release(&key_1);
// Check for PARCSigningAlgorithm_HMAC value
PARCBuffer *bb_id_2 = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid_2 = parcKeyId_Create(bb_id_2);
parcBuffer_Release(&bb_id_2);
PARCBufferComposer *composer2 = parcBufferComposer_Create();
parcBufferComposer_PutString(composer2, "quack quack");
PARCBuffer *bb_key_2 = parcBufferComposer_ProduceBuffer(composer2);
PARCKey *key_2 = parcKey_CreateFromSymmetricKey(keyid_2, PARCSigningAlgorithm_HMAC, bb_key_2);
parcKey_AssertValid(key_2);
assertTrue((parcKey_GetSigningAlgorithm(key_2) == PARCSigningAlgorithm_HMAC), "Signing Algorithms don't match");
parcBuffer_Release(&bb_key_2);
parcBufferComposer_Release(&composer2);
parcKeyId_Release(&keyid_2);
parcKey_Release(&key_2);
}
LONGBOW_TEST_CASE(Global, parcKey_Acquire)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromDerEncodedPublicKey(keyid, PARCSigningAlgorithm_RSA, bb_key);
parcKey_AssertValid(key);
parcObjectTesting_AssertAcquireReleaseContract(parcKey_Acquire, key);
parcKey_Release(&key);
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
LONGBOW_TEST_CASE(Global, parcKey_ToString)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromDerEncodedPublicKey(keyid, PARCSigningAlgorithm_RSA, bb_key);
char *keyString = parcKey_ToString(key);
assertNotNull(keyString, "Expected non-null key representation string");
assertTrue(strlen(keyString) > 0, "Expected non-null key representation string");
parcKey_Release(&key);
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
LONGBOW_TEST_FIXTURE(Errors)
{
LONGBOW_RUN_TEST_CASE(Errors, parcKey_CreateFromDerEncodedPublicKey_InvalidAlgorithm);
LONGBOW_RUN_TEST_CASE(Errors, parcKey_CreateFromSymmetricKey_InvalidAlgorithm);
}
LONGBOW_TEST_FIXTURE_SETUP(Errors)
{
return LONGBOW_STATUS_SUCCEEDED;
}
LONGBOW_TEST_FIXTURE_TEARDOWN(Errors)
{
return LONGBOW_STATUS_SUCCEEDED;
}
LONGBOW_TEST_CASE_EXPECTS(Errors, parcKey_CreateFromDerEncodedPublicKey_InvalidAlgorithm, .event = &LongBowTrapIllegalValue)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromDerEncodedPublicKey(keyid, PARCSigningAlgorithm_HMAC, bb_key);
assertNull(key, "This should not be reached"); //To avoid a warning
// HMAC is an illegal value for this constructor
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
LONGBOW_TEST_CASE_EXPECTS(Errors, parcKey_CreateFromSymmetricKey_InvalidAlgorithm, .event = &LongBowTrapIllegalValue)
{
PARCBuffer *bb_id = parcBuffer_Wrap("choo choo", 9, 0, 9);
PARCKeyId *keyid = parcKeyId_Create(bb_id);
parcBuffer_Release(&bb_id);
PARCBufferComposer *composer = parcBufferComposer_Create();
parcBufferComposer_PutString(composer, "quack quack");
PARCBuffer *bb_key = parcBufferComposer_ProduceBuffer(composer);
PARCKey *key = parcKey_CreateFromSymmetricKey(keyid, PARCSigningAlgorithm_RSA, bb_key);
assertNull(key, "This should not be reached"); //To avoid a warning
// RSA/DSA are illegal values for this constructor
parcBuffer_Release(&bb_key);
parcBufferComposer_Release(&composer);
parcKeyId_Release(&keyid);
}
int
main(int argc, char *argv[argc])
{
LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_Key);
int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner, NULL);
longBowTestRunner_Destroy(&testRunner);
exit(exitStatus);
}
|