aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/internal/test/test_ccnx_ContentObjectFacadeV1.c
blob: b3c6b343b458b5ce3a76676b55e5dd49510d21d0 (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * 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 "../ccnx_ContentObjectFacadeV1.c"
#include <parc/algol/parc_SafeMemory.h>
#include <LongBow/unit-test.h>

#include <ccnx/common/codec/schema_v1/ccnxCodecSchemaV1_TlvDictionary.h>
#include <inttypes.h>

typedef struct test_data {
    CCNxName *name;
    CCNxPayloadType payloadType;
    PARCBuffer *payload;

    // a V1 dictionary but with no values set
    CCNxTlvDictionary *contentObjectV1Empty;

    // a V1 nameless Content Object
    CCNxTlvDictionary *contentObjectV1Nameless;

    // a valid V1 Content Object
    CCNxTlvDictionary *contentObjectV1;
    CCNxTlvDictionary *contentObjectVFF;
} TestData;

static TestData *
_commonSetup(void)
{
    TestData *data = parcMemory_AllocateAndClear(sizeof(TestData));
    assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData));

    data->name = ccnxName_CreateFromCString("lci:/foo/bar");
    data->payloadType = CCNxPayloadType_DATA;

    data->payload = parcBuffer_Flip(parcBuffer_PutArray(parcBuffer_Allocate(20), 7, (uint8_t *) "payload"));

    data->contentObjectV1Empty = ccnxCodecSchemaV1TlvDictionary_CreateContentObject();
    data->contentObjectV1 = _ccnxContentObjectFacadeV1_CreateWithNameAndPayload(data->name, data->payloadType,
                                                                                data->payload);
    data->contentObjectV1Nameless = _ccnxContentObjectFacadeV1_CreateWithPayload(data->payloadType, data->payload);

    data->contentObjectVFF = ccnxTlvDictionary_Create(CCNxCodecSchemaV1TlvDictionary_MessageFastArray_END, CCNxCodecSchemaV1TlvDictionary_Lists_END);
    ccnxTlvDictionary_SetMessageType_ContentObject(data->contentObjectVFF, 0xFF);

    return data;
}

static void
_commonTeardown(TestData *data)
{
    ccnxName_Release(&data->name);
    parcBuffer_Release(&data->payload);

    ccnxTlvDictionary_Release(&data->contentObjectV1Empty);
    ccnxTlvDictionary_Release(&data->contentObjectV1);
    ccnxTlvDictionary_Release(&data->contentObjectV1Nameless);
    ccnxTlvDictionary_Release(&data->contentObjectVFF);
    parcMemory_Deallocate((void **) &data);
}


LONGBOW_TEST_RUNNER(ccnx_ContentObjectFacade)
{
    // The following Test Fixtures will run their corresponding Test Cases.
    // Test Fixtures are run in the order specified, but all tests should be idempotent.
    // Never rely on the execution order of tests or share state between them.
    LONGBOW_RUN_TEST_FIXTURE(ImplInterface);
}

// The Test Runner calls this function once before any Test Fixtures are run.
LONGBOW_TEST_RUNNER_SETUP(ccnx_ContentObjectFacade)
{
    parcMemory_SetInterface(&PARCSafeMemoryAsPARCMemory);
    return LONGBOW_STATUS_SUCCEEDED;
}

// The Test Runner calls this function once after all the Test Fixtures are run.
LONGBOW_TEST_RUNNER_TEARDOWN(ccnx_ContentObjectFacade)
{
    return LONGBOW_STATUS_SUCCEEDED;
}

// ======================================================================================

LONGBOW_TEST_FIXTURE(ImplInterface)
{
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_Init);

    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetName);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetPayload);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_SetPayload);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_SetPayload_Link);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetPayloadType);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetPayloadType_Unset);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetExpiryTime);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_SetExpiryTime);

    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_SetSignature);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetKeyId);

    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_ToString);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_Display);
    LONGBOW_RUN_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_Equals);
}

LONGBOW_TEST_FIXTURE_SETUP(ImplInterface)
{
    longBowTestCase_SetClipBoardData(testCase, _commonSetup());
    return LONGBOW_STATUS_SUCCEEDED;
}

LONGBOW_TEST_FIXTURE_TEARDOWN(ImplInterface)
{
    _commonTeardown(longBowTestCase_GetClipBoardData(testCase));

    uint32_t outstandingAllocations = parcSafeMemory_ReportAllocation(STDERR_FILENO);
    if (outstandingAllocations != 0) {
        printf("%s leaks memory by %d allocations\n", longBowTestCase_GetName(testCase), outstandingAllocations);
        return LONGBOW_STATUS_MEMORYLEAK;
    }
    return LONGBOW_STATUS_SUCCEEDED;
}

// ======================================================================================

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_Init)
{
    CCNxContentObjectInterface *impl = &CCNxContentObjectFacadeV1_Implementation;
    assertNotNull(impl->createWithNameAndPayload, "Expected CreateWithNameAndPayload to be set");
    assertNotNull(impl->createWithPayload, "Expected CreateWithPayload to be set");

    assertNotNull(impl->getName, "Expected GetName to be set");
    assertNotNull(impl->setSignature, "Expected SetSignature to be set");
    assertNotNull(impl->getKeyId, "Expected GetKeyID to be set");
    assertNotNull(impl->getPayload, "Expected GetPayload to be set");
    assertNotNull(impl->getPayloadType, "Expected GetPayloadType to be set");

    assertNotNull(impl->hasFinalChunkNumber, "Expected HasFinalChunkNumber to be set");
    assertNotNull(impl->getFinalChunkNumber, "Expected GetFinalChunkNumber to be set");
    assertNotNull(impl->setFinalChunkNumber, "Expected SetFinalChunkNumber to be set");

    assertNotNull(impl->hasExpiryTime, "Expected HasExpiryTime to be set");
    assertNotNull(impl->getExpiryTime, "Expected GetExpiryTime to be set");
    assertNotNull(impl->setExpiryTime, "Expected SetExpiryTime to be set");

    assertNotNull(impl->toString, "Expected ToString to be set");
    assertNotNull(impl->display, "Expected Display to be set");
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetName)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxName *test = _ccnxContentObjectFacadeV1_GetName(data->contentObjectV1);
    assertTrue(ccnxName_Equals(test, data->name), "Names do not match")
    {
        ccnxName_Display(test, 0);
        ccnxName_Display(data->name, 0);
    }
    test = _ccnxContentObjectFacadeV1_GetName(data->contentObjectV1Nameless);
    assertNull(test, "A nameless Content Object has no name.");
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_SetPayload)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    PARCBuffer *newPayload = parcBuffer_WrapCString("this is a new payload");

    // Shouldn't be able to set it on an already initialized ContentObject.
    // (Though this may change...)
    assertFalse(_ccnxContentObjectFacadeV1_SetPayload(data->contentObjectV1, CCNxPayloadType_DATA, newPayload),
                "Expected to not be able to re-assign a payload on an already initialized ContentObject");

    CCNxTlvDictionary *contentObject =
        _ccnxContentObjectFacadeV1_CreateWithNameAndPayload(data->name,
                                                            CCNxPayloadType_DATA,
                                                            NULL);


    bool status = _ccnxContentObjectFacadeV1_SetPayload(contentObject, CCNxPayloadType_KEY, newPayload);
    assertTrue(status, "Expected to be able to set the buffer");

    PARCBuffer *testPayload = _ccnxContentObjectFacadeV1_GetPayload(contentObject);
    assertTrue(parcBuffer_Equals(newPayload, testPayload), "payloads do not match")
    {
        parcBuffer_Display(newPayload, 0);
        parcBuffer_Display(data->payload, 0);
    }

    CCNxPayloadType testType = _ccnxContentObjectFacadeV1_GetPayloadType(contentObject);
    assertTrue(testType == CCNxPayloadType_KEY, "Expected type KEY");

    parcBuffer_Release(&newPayload);
    ccnxTlvDictionary_Release(&contentObject);
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_SetPayload_Link)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    PARCBuffer *payload = parcBuffer_WrapCString("this is a payload");

    CCNxTlvDictionary *contentObject =
        _ccnxContentObjectFacadeV1_CreateWithNameAndPayload(data->name,
                                                            CCNxPayloadType_LINK,
                                                            payload);

    PARCBuffer *testPayload = _ccnxContentObjectFacadeV1_GetPayload(contentObject);
    assertTrue(parcBuffer_Equals(payload, testPayload), "payloads do not match")
    {
        parcBuffer_Display(payload, 0);
        parcBuffer_Display(testPayload, 0);
    }

    CCNxPayloadType testType = _ccnxContentObjectFacadeV1_GetPayloadType(contentObject);
    assertTrue(testType == CCNxPayloadType_LINK, "Expected type LINK");

    parcBuffer_Release(&payload);
    ccnxTlvDictionary_Release(&contentObject);
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetPayload)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    PARCBuffer *test = _ccnxContentObjectFacadeV1_GetPayload(data->contentObjectV1);
    assertTrue(parcBuffer_Equals(test, data->payload), "payloads do not match")
    {
        parcBuffer_Display(test, 0);
        parcBuffer_Display(data->payload, 0);
    }
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetPayloadType)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxPayloadType test = _ccnxContentObjectFacadeV1_GetPayloadType(data->contentObjectV1);
    assertTrue(test == data->payloadType, "payloads do not match, got %d expected %d", test, data->payloadType)
    {
        ccnxTlvDictionary_Display(data->contentObjectV1, 0);
    }
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetPayloadType_Unset)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxPayloadType test = _ccnxContentObjectFacadeV1_GetPayloadType(data->contentObjectV1Empty);
    assertTrue(test == CCNxPayloadType_DATA, "payloads do not match, got %d expected %d", test, CCNxPayloadType_DATA)
    {
        ccnxTlvDictionary_Display(data->contentObjectV1, 0);
    }
}

LONGBOW_TEST_CASE_EXPECTS(ImplInterface, ccnxContentObjectFacadeV1_ToString, .event = &LongBowTrapNotImplemented)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    _ccnxContentObjectFacadeV1_ToString(data->contentObjectV1);
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_Display)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    _ccnxContentObjectFacadeV1_Display(data->contentObjectV1, 0);
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_Equals)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);
    assertTrue(_ccnxContentObjectFacadeV1_Equals(data->contentObjectV1, data->contentObjectV1),
               "Expected equals to be true");
    assertFalse(_ccnxContentObjectFacadeV1_Equals(data->contentObjectV1, data->contentObjectV1Empty),
                "Expected equals to be false");
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetExpiryTime)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);

    uint64_t truth = 12345;
    ccnxTlvDictionary_PutInteger(data->contentObjectV1, CCNxCodecSchemaV1TlvDictionary_MessageFastArray_EXPIRY_TIME, truth);

    assertTrue(_ccnxContentObjectFacadeV1_HasExpiryTime(data->contentObjectV1), "Expected HasExpiryTime() to return true");
    uint64_t expiryTime = _ccnxContentObjectFacadeV1_GetExpiryTime(data->contentObjectV1);
    assertTrue(truth == expiryTime, "times do not match, expected %" PRIu64 ", got %" PRIu64, truth, expiryTime);
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_SetExpiryTime)
{
    TestData *data = longBowTestCase_GetClipBoardData(testCase);

    uint64_t truth = 12345;
    _ccnxContentObjectFacadeV1_SetExpiryTime(data->contentObjectV1, truth);
    uint64_t expiryTime = _ccnxContentObjectFacadeV1_GetExpiryTime(data->contentObjectV1);
    assertTrue(truth == expiryTime, "times do not match, expected %" PRIu64 ", got %" PRIu64, truth, expiryTime);
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_SetSignature)
{
    CCNxName *name = ccnxName_CreateFromCString("lci:/foo/bar/baz");
    PARCBuffer *payload = parcBuffer_Allocate(100);

    CCNxTlvDictionary *contentObject = _ccnxContentObjectFacadeV1_CreateWithNameAndPayload(name, CCNxPayloadType_DATA,
                                                                                           payload);

    PARCBuffer *keyId = parcBuffer_WrapCString("keyhash");
    char *rawsig = "siggybits";
    PARCBuffer *sigbits = parcBuffer_CreateFromArray((void *) rawsig, strlen(rawsig));

    PARCSignature *signature = parcSignature_Create(PARCSigningAlgorithm_RSA, PARCCryptoHashType_SHA256, parcBuffer_Flip(sigbits));
    parcBuffer_Release(&sigbits);

    assertTrue(_ccnxContentObjectFacadeV1_SetSignature(contentObject, keyId, signature, NULL),
               "Expected to be able to set the signature");

    ccnxName_Release(&name);
    parcBuffer_Release(&payload);
    parcSignature_Release(&signature);
    parcBuffer_Release(&keyId);
    ccnxTlvDictionary_Release(&contentObject);
}

LONGBOW_TEST_CASE(ImplInterface, ccnxContentObjectFacadeV1_GetKeyId)
{
    CCNxName *name = ccnxName_CreateFromCString("lci:/foo/bar/baz");
    PARCBuffer *payload = parcBuffer_Allocate(100);

    CCNxTlvDictionary *contentObject = _ccnxContentObjectFacadeV1_CreateWithNameAndPayload(name, CCNxPayloadType_DATA,
                                                                                           payload);

    assertNull(_ccnxContentObjectFacadeV1_GetKeyId(contentObject), "Expect key ID to be NULL");

    PARCBuffer *testKeyId = parcBuffer_WrapCString("keyhash");
    char *rawsig = "siggybits";
    PARCBuffer *sigbits = parcBuffer_CreateFromArray((void *) rawsig, strlen(rawsig));

    PARCSignature *signature = parcSignature_Create(PARCSigningAlgorithm_RSA, PARCCryptoHashType_SHA256, parcBuffer_Flip(sigbits));
    parcBuffer_Release(&sigbits);

    assertTrue(_ccnxContentObjectFacadeV1_SetSignature(contentObject, testKeyId, signature, NULL),
               "Expected to be able to set the signature");

    PARCBuffer *keyId = _ccnxContentObjectFacadeV1_GetKeyId(contentObject);

    assertTrue(parcBuffer_Equals(keyId, testKeyId), "Expect key ID's to be the same");

    ccnxName_Release(&name);
    parcBuffer_Release(&payload);
    parcSignature_Release(&signature);
    parcBuffer_Release(&keyId);
    ccnxTlvDictionary_Release(&contentObject);
}

// ======================================================================================

int
main(int argc, char *argv[])
{
    LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(ccnx_ContentObjectFacade);
    int exitStatus = longBowMain(argc, argv, testRunner, NULL);
    longBowTestRunner_Destroy(&testRunner);
    exit(exitStatus);
}