aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-common/ccnx/common/test/test_ccnx_Manifest.c
blob: 583c9a792013b7813ff4caee03108a97c22fed6c (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
/*
 * 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_Manifest.c"

#include <LongBow/unit-test.h>
#include <parc/algol/parc_SafeMemory.h>

#include <parc/testing/parc_ObjectTesting.h>

#include <ccnx/common/ccnx_Interest.h>

typedef struct test_data {
    CCNxManifest *object;
    CCNxManifest *manifestWithNamelessGroup;
    CCNxManifest *nameless;
    PARCLinkedList *interestListFromGroupLocator;
    PARCLinkedList *interestListFromManifestLocator;
    PARCLinkedList *interestListFromOverrideLocator;
    CCNxName *overrideLocator;
} ManifestTestData;

static ManifestTestData *
_commonSetup(void)
{
    ManifestTestData *data = parcMemory_AllocateAndClear(sizeof(ManifestTestData));

    data->overrideLocator = ccnxName_CreateFromCString("ccnx:/override");

    CCNxName *name = ccnxName_CreateFromCString("ccnx:/my/manifest");
    CCNxManifest *manifest = ccnxManifest_Create(name);
    CCNxManifest *nameless = ccnxManifest_CreateNameless();
    CCNxManifest *manifestWithNamelessGroup = ccnxManifest_Create(name);

    data->interestListFromGroupLocator = parcLinkedList_Create();
    data->interestListFromManifestLocator = parcLinkedList_Create();
    data->interestListFromOverrideLocator = parcLinkedList_Create();
    data->object = manifest;
    data->nameless = nameless;
    data->manifestWithNamelessGroup = manifestWithNamelessGroup;

    CCNxManifestHashGroup *group = ccnxManifestHashGroup_Create();
    CCNxManifestHashGroup *namelessGroup = ccnxManifestHashGroup_Create();

    CCNxName *locator = ccnxName_CreateFromCString("ccnx:/locator");
    ccnxManifestHashGroup_SetLocator(group, locator);

    // Create pointers for the pieces of data
    PARCBuffer *digest1 = parcBuffer_Allocate(32);
    PARCBuffer *digest2 = parcBuffer_Allocate(32);
    ccnxManifestHashGroup_AppendPointer(group, CCNxManifestHashGroupPointerType_Data, digest1);
    ccnxManifestHashGroup_AppendPointer(namelessGroup, CCNxManifestHashGroupPointerType_Data, digest1);
    ccnxManifestHashGroup_AppendPointer(group, CCNxManifestHashGroupPointerType_Manifest, digest2);
    ccnxManifestHashGroup_AppendPointer(namelessGroup, CCNxManifestHashGroupPointerType_Data, digest2);

    // Create the corresponding interests based on the three locator cases
    // 1. The locator is inherited from the hash group
    // 2. The locator is inherited from the manifest
    // 3. The locator is overridden
    CCNxInterest *interest1 = ccnxInterest_CreateSimple(locator);
    ccnxInterest_SetContentObjectHashRestriction(interest1, digest1);
    parcLinkedList_Append(data->interestListFromGroupLocator, interest1);
    ccnxInterest_Release(&interest1);

    interest1 = ccnxInterest_CreateSimple(name);
    ccnxInterest_SetContentObjectHashRestriction(interest1, digest1);
    parcLinkedList_Append(data->interestListFromManifestLocator, interest1);
    ccnxInterest_Release(&interest1);

    interest1 = ccnxInterest_CreateSimple(data->overrideLocator);
    ccnxInterest_SetContentObjectHashRestriction(interest1, digest1);
    parcLinkedList_Append(data->interestListFromOverrideLocator, interest1);
    ccnxInterest_Release(&interest1);

    CCNxInterest *interest2 = ccnxInterest_CreateSimple(locator);
    ccnxInterest_SetContentObjectHashRestriction(interest2, digest2);
    parcLinkedList_Append(data->interestListFromGroupLocator, interest2);
    ccnxInterest_Release(&interest2);

    interest2 = ccnxInterest_CreateSimple(name);
    ccnxInterest_SetContentObjectHashRestriction(interest2, digest2);
    parcLinkedList_Append(data->interestListFromManifestLocator, interest2);
    ccnxInterest_Release(&interest2);

    interest2 = ccnxInterest_CreateSimple(data->overrideLocator);
    ccnxInterest_SetContentObjectHashRestriction(interest2, digest2);
    parcLinkedList_Append(data->interestListFromOverrideLocator, interest2);
    ccnxInterest_Release(&interest2);

    ccnxName_Release(&name);
    ccnxName_Release(&locator);
    parcBuffer_Release(&digest1);
    parcBuffer_Release(&digest2);

    ccnxManifest_AddHashGroup(manifest, group);
    ccnxManifest_AddHashGroup(manifestWithNamelessGroup, namelessGroup);
    ccnxManifest_AddHashGroup(nameless, namelessGroup);

    ccnxManifestHashGroup_Release(&group);
    ccnxManifestHashGroup_Release(&namelessGroup);

    return data;
}

static void
_commonTeardown(ManifestTestData *data)
{
    ccnxManifest_Release(&data->object);
    ccnxManifest_Release(&data->nameless);
    ccnxManifest_Release(&data->manifestWithNamelessGroup);

    parcLinkedList_Release(&data->interestListFromGroupLocator);
    parcLinkedList_Release(&data->interestListFromManifestLocator);
    parcLinkedList_Release(&data->interestListFromOverrideLocator);

    ccnxName_Release(&data->overrideLocator);
    parcMemory_Deallocate((void **) &data);
}

LONGBOW_TEST_RUNNER(ccnx_Manifest)
{
    LONGBOW_RUN_TEST_FIXTURE(Global);
}

// The Test Runner calls this function once before any Test Fixtures are run.
LONGBOW_TEST_RUNNER_SETUP(ccnx_Manifest)
{
    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_Manifest)
{
    return LONGBOW_STATUS_SUCCEEDED;
}

LONGBOW_TEST_FIXTURE(Global)
{
    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_AcquireRelease);
    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_Create);
    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_AddHashGroup);
    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_GetHashGroup);
    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_GetNumberOfHashGroups);
    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_GetName);
    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_Equals);
    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_ToString);

    LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_CreateInterestList_OverrideLocator);
    // LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_CreateInterestList_ManifestLocator);
    // LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_CreateInterestList_GroupLocator);
    // LONGBOW_RUN_TEST_CASE(Global, ccnxManifest_CreateInterestList_NoLocator);
}

LONGBOW_TEST_FIXTURE_SETUP(Global)
{
    longBowTestCase_SetClipBoardData(testCase, _commonSetup());

    return LONGBOW_STATUS_SUCCEEDED;
}

LONGBOW_TEST_FIXTURE_TEARDOWN(Global)
{
    _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(Global, ccnxManifest_AcquireRelease)
{
    CCNxName *name = ccnxName_CreateFromCString("ccnx:/my/manifest");
    CCNxManifest *manifest = ccnxManifest_Create(name);
    ccnxName_Release(&name);

    parcObjectTesting_AssertAcquireReleaseContract(ccnxManifest_Acquire, manifest);

    ccnxManifest_Release(&manifest);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_Create)
{
    CCNxName *name = ccnxName_CreateFromCString("ccnx:/my/manifest");
    CCNxManifest *manifest = ccnxManifest_Create(name);

    assertNotNull(manifest, "Expected the Manifest to be created without errors.");
    const CCNxName *copy = ccnxManifest_GetName(manifest);

    assertTrue(ccnxName_Equals(name, copy), "Expected name to equal %s, got %s", ccnxName_ToString(name), ccnxName_ToString(copy));

    ccnxName_Release(&name);
    ccnxManifest_Release(&manifest);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_AddHashGroup)
{
    ManifestTestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxManifest *manifest = data->object;

    size_t numGroups = ccnxManifest_GetNumberOfHashGroups(manifest);

    CCNxManifestHashGroup *group = ccnxManifestHashGroup_Create();
    ccnxManifest_AddHashGroup(manifest, group);

    size_t expected = numGroups + 1;
    size_t actual = ccnxManifest_GetNumberOfHashGroups(manifest);
    assertTrue(actual == expected, "Expected %zu, got %zu", expected, actual);

    ccnxManifestHashGroup_Release(&group);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_GetHashGroup)
{
    ManifestTestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxManifest *manifest = data->object;

    CCNxManifestHashGroup *group = ccnxManifest_GetHashGroupByIndex(manifest, 0);
    CCNxName *expected = ccnxName_CreateFromCString("ccnx:/locator");
    const CCNxName *actual = ccnxManifestHashGroup_GetLocator(group);
    assertTrue(ccnxName_Equals(expected, actual), "Expected %s, got %s", ccnxName_ToString(expected), ccnxName_ToString(actual));

    ccnxName_Release(&expected);
    ccnxManifestHashGroup_Release(&group);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_GetNumberOfHashGroups)
{
    ManifestTestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxManifest *manifest = data->object;

    size_t before = ccnxManifest_GetNumberOfHashGroups(manifest);
    CCNxManifestHashGroup *group = ccnxManifestHashGroup_Create();
    ccnxManifest_AddHashGroup(manifest, group);

    size_t actual = ccnxManifest_GetNumberOfHashGroups(manifest);
    size_t expected = before + 1;

    assertTrue(expected == actual, "Expected %zu, got %zu", expected, actual);

    ccnxManifestHashGroup_Release(&group);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_CreateInterestList_NoLocator)
{
    ManifestTestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxManifest *nameless = data->nameless;

    PARCLinkedList *interestList = ccnxManifest_CreateInterestList(nameless, NULL);
    assertTrue(parcLinkedList_Size(interestList) == 0, "Expected the interest list to be empty since there was no valid locator");
    parcLinkedList_Release(&interestList);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_CreateInterestList_GroupLocator)
{
    ManifestTestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxManifest *manifest = data->object;

    PARCLinkedList *interestList = ccnxManifest_CreateInterestList(manifest, NULL);
    assertTrue(parcLinkedList_Equals(interestList, data->interestListFromGroupLocator), "Expected the interest lists to be equal");
    parcLinkedList_Release(&interestList);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_CreateInterestList_ManifestLocator)
{
    ManifestTestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxManifest *manifestWithNamelessGroup = data->manifestWithNamelessGroup;

    PARCLinkedList *interestList = ccnxManifest_CreateInterestList(manifestWithNamelessGroup, NULL);
    assertTrue(parcLinkedList_Equals(interestList, data->interestListFromManifestLocator), "Expected the interest lists to be equal");
    parcLinkedList_Release(&interestList);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_CreateInterestList_OverrideLocator)
{
    ManifestTestData *data = longBowTestCase_GetClipBoardData(testCase);
    CCNxManifest *nameless = data->nameless;
    CCNxName *overrideLocator = data->overrideLocator;

    PARCLinkedList *interestList = ccnxManifest_CreateInterestList(nameless, overrideLocator);
    assertTrue(parcLinkedList_Equals(interestList, data->interestListFromOverrideLocator), "Expected the interest lists to be equal");
    parcLinkedList_Release(&interestList);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_GetName)
{
    CCNxName *name = ccnxName_CreateFromCString("ccnx:/my/manifest");
    CCNxManifest *manifest = ccnxManifest_Create(name);

    assertNotNull(manifest, "Expected the Manifest to be created without errors.");
    const CCNxName *copy = ccnxManifest_GetName(manifest);

    assertTrue(ccnxName_Equals(name, copy), "Expected name to equal %s, got %s", ccnxName_ToString(name), ccnxName_ToString(copy));

    ccnxName_Release(&name);
    ccnxManifest_Release(&manifest);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_Equals)
{
    CCNxName *name = ccnxName_CreateFromCString("ccnx:/my/manifest");
    CCNxManifest *x = ccnxManifest_Create(name);
    CCNxManifest *y = ccnxManifest_Create(name);
    CCNxManifest *z = ccnxManifest_Create(name);

    CCNxName *name1 = ccnxName_CreateFromCString("ccnx:/not/my/manifest");
    CCNxManifest *u1 = ccnxManifest_Create(name1);

    parcObjectTesting_AssertEqualsFunction(ccnxManifest_Equals, x, y, z, u1, NULL);

    ccnxName_Release(&name);
    ccnxName_Release(&name1);
    ccnxManifest_Release(&x);
    ccnxManifest_Release(&y);
    ccnxManifest_Release(&z);
    ccnxManifest_Release(&u1);
}

LONGBOW_TEST_CASE(Global, ccnxManifest_ToString)
{
    CCNxName *name = ccnxName_CreateFromCString("ccnx:/my/manifest");
    CCNxManifest *manifest = ccnxManifest_Create(name);

    assertNotNull(manifest, "Expected the Manifest to be created without errors.");
    const CCNxName *copy = ccnxManifest_GetName(manifest);

    assertTrue(ccnxName_Equals(name, copy), "Expected name to equal %s, got %s", ccnxName_ToString(name), ccnxName_ToString(copy));

    ccnxName_Release(&name);
    ccnxManifest_Release(&manifest);
}

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