aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/api/control/test/test_cpi_Listener.c
blob: 5bd5a5343be87aa956d53dbd299fb8f3e8285a81 (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
378
379
380
381
382
383
384
/*
 * 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 "../cpi_Listener.c"
#include <LongBow/unit-test.h>
#include <parc/algol/parc_SafeMemory.h>
#include <arpa/inet.h>
#include <errno.h>

typedef struct test_data {
    CPIListener *listener;

    // the truth values of the connection
    uint8_t macArray[6];
    CPIAddress *macAddress;
    uint16_t ethertype;
    char ifname[16];
    char symbolic[16];
} TestData;

static CPIListener *
_conjureIPObject(CPIInterfaceIPTunnelType type, const char *addressString, uint16_t port, const char *symbolic)
{
    struct sockaddr_in sin;
    memset(&sin, 0, sizeof(sin));
    sin.sin_family = AF_INET;
    sin.sin_port = htons(port);
    int result = inet_aton(addressString, &sin.sin_addr);
    assertTrue(result == 1, "failed inet_aton: (%d) %s", errno, strerror(errno));

    CPIAddress *address = cpiAddress_CreateFromInet(&sin);
    CPIListener *listener = cpiListener_CreateIP(type, address, symbolic);
    cpiAddress_Destroy(&address);

    return listener;
}

LONGBOW_TEST_RUNNER(cpi_ConnectionEthernet)
{
    // 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(Global);
}

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

LONGBOW_TEST_FIXTURE(Global)
{
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_CreateEther);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_CreateIP);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_Equals_Ether);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_Equals_IP);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_CreateAddMessage);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_CreateRemoveMessage);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_IsAddMessage);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_IsRemoveMessage);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_FromControl_Ether);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_FromControl_IP);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_IsEtherEncap);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_IsIPEncap);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_GetEtherType);

    LONGBOW_RUN_TEST_CASE(Global, cpiListener_GetInterfaceName);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_GetSymbolicName);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_GetAddress);

    LONGBOW_RUN_TEST_CASE(Global, cpiListener_IsProtocolUdp);
    LONGBOW_RUN_TEST_CASE(Global, cpiListener_IsProtocolTcp);
}

LONGBOW_TEST_FIXTURE_SETUP(Global)
{
    return LONGBOW_STATUS_SUCCEEDED;
}

LONGBOW_TEST_FIXTURE_TEARDOWN(Global)
{
    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, cpiListener_CreateEther)
{
    CPIListener *listener = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    assertNotNull(listener, "Got null IP based listener");
    cpiListener_Release(&listener);
}

LONGBOW_TEST_CASE(Global, cpiListener_CreateIP)
{
    CPIListener *listener = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");
    assertNotNull(listener, "Got null IP based listener");
    cpiListener_Release(&listener);
}

LONGBOW_TEST_CASE(Global, cpiListener_Equals_Ether)
{
    CPIListener *x = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    CPIListener *y = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    CPIListener *z = cpiListener_CreateEther("eth0", 0x0801, "puppy");

    CPIListener *t = cpiListener_CreateEther("eth1", 0x0801, "puppy");
    CPIListener *u = cpiListener_CreateEther("eth0", 0x0802, "puppy");
    CPIListener *v = cpiListener_CreateEther("eth0", 0x0801, "kitten");

    assertEqualsContract(cpiListener_Equals, x, y, z, t, u, v);

    cpiListener_Release(&x);
    cpiListener_Release(&y);
    cpiListener_Release(&z);
    cpiListener_Release(&t);
    cpiListener_Release(&u);
    cpiListener_Release(&v);
}

LONGBOW_TEST_CASE(Global, cpiListener_Equals_IP)
{
    CPIListener *x = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");
    CPIListener *y = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");
    CPIListener *z = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");

    CPIListener *t = _conjureIPObject(IPTUN_TCP, "127.0.0.1", 9596, "puppy");
    CPIListener *u = _conjureIPObject(IPTUN_UDP, "127.0.2.1", 9596, "puppy");
    CPIListener *v = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 1111, "puppy");
    CPIListener *w = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "kitten");

    assertEqualsContract(cpiListener_Equals, x, y, z, t, u, v, w);

    cpiListener_Release(&x);
    cpiListener_Release(&y);
    cpiListener_Release(&z);
    cpiListener_Release(&t);
    cpiListener_Release(&u);
    cpiListener_Release(&v);
    cpiListener_Release(&w);
}

LONGBOW_TEST_CASE(Global, cpiListener_CreateAddMessage)
{
    const char *truthFormat = "{\"CPI_REQUEST\":{\"SEQUENCE\":%d,\"%s\":{\"IFNAME\":\"eth0\",\"ETHERTYPE\":2049,\"SYMBOLIC\":\"puppy\"}}}";

    char buffer[1024];

    // Create the add message
    CPIListener *listener = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    CCNxControl *control = cpiListener_CreateAddMessage(listener);
    assertNotNull(control, "Got null control message");

    // extract the sequence number to put in the truth string
    PARCJSON *json = ccnxControl_GetJson(control);
    uint64_t seqnum = controlPlaneInterface_GetSequenceNumber(json);
    sprintf(buffer, truthFormat, (int) seqnum, KEY_ADDLISTENER);

    char *testString = parcJSON_ToCompactString(json);
    assertTrue(strcmp(buffer, testString) == 0, "Got wrong JSON, expected\n%s\nGot\n%s\n", buffer, testString);
    parcMemory_Deallocate((void **) &testString);

    ccnxControl_Release(&control);
    cpiListener_Release(&listener);
}

LONGBOW_TEST_CASE(Global, cpiListener_CreateRemoveMessage)
{
#if defined(__APPLE__)
    const char *truthFormat = "{\"CPI_REQUEST\":{\"SEQUENCE\":%d,\"%s\":{\"IPROTO\":\"UDP\",\"ADDR\":{\"ADDRESSTYPE\":\"INET\",\"DATA\":\"AAIlfH8AAAEAAAAAAAAAAA==\"},\"SYMBOLIC\":\"puppy\"}}}";
#elif defined(__linux__)
    const char *truthFormat = "{\"CPI_REQUEST\":{\"SEQUENCE\":%d,\"%s\":{\"IPROTO\":\"UDP\",\"ADDR\":{\"ADDRESSTYPE\":\"INET\",\"DATA\":\"AgAlfH8AAAEAAAAAAAAAAA==\"},\"SYMBOLIC\":\"puppy\"}}}";
#else
    // Case 1033
    testUnimplemented("Platform not supported");
    return;
#endif

    char buffer[1024];

    // Create the remove message
    CPIListener *listener = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");
    CCNxControl *control = cpiListener_CreateRemoveMessage(listener);
    assertNotNull(control, "Got null control message");

    // extract the sequence number to put in the truth string
    PARCJSON *json = ccnxControl_GetJson(control);
    uint64_t seqnum = controlPlaneInterface_GetSequenceNumber(json);
    sprintf(buffer, truthFormat, (int) seqnum, KEY_REMOVELISTENER);

    char *testString = parcJSON_ToCompactString(json);
    assertTrue(strcmp(buffer, testString) == 0, "Got wrong JSON, expected\n%s\nGot\n%s\n", buffer, testString);
    parcMemory_Deallocate((void **) &testString);

    ccnxControl_Release(&control);
    cpiListener_Release(&listener);
}

LONGBOW_TEST_CASE(Global, cpiListener_IsAddMessage)
{
    CPIListener *listener = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    CCNxControl *control = cpiListener_CreateAddMessage(listener);
    assertNotNull(control, "Got null control message");

    bool test = cpiListener_IsAddMessage(control);
    assertTrue(test, "Add message denies it is one.");

    ccnxControl_Release(&control);
    cpiListener_Release(&listener);
}

LONGBOW_TEST_CASE(Global, cpiListener_IsRemoveMessage)
{
    CPIListener *listener = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    CCNxControl *control = cpiListener_CreateRemoveMessage(listener);
    assertNotNull(control, "Got null control message");

    bool test = cpiListener_IsRemoveMessage(control);
    assertTrue(test, "Add message denies it is one.");

    ccnxControl_Release(&control);
    cpiListener_Release(&listener);
}

LONGBOW_TEST_CASE(Global, cpiListener_FromControl_Ether)
{
    CPIListener *listener = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    CCNxControl *control = cpiListener_CreateAddMessage(listener);

    CPIListener *test = cpiListener_FromControl(control);
    assertTrue(cpiListener_Equals(listener, test), "Listeners do not match")
    {
        printf("Expenected:\n");
        char *str = parcJSON_ToString(ccnxControl_GetJson(control));
        printf("   %s\n", str);
        parcMemory_Deallocate((void **) &str);

        printf("Got:\n");
        CCNxControl *testControl = cpiListener_CreateAddMessage(test);
        str = parcJSON_ToString(ccnxControl_GetJson(testControl));
        printf("   %s\n", str);
        parcMemory_Deallocate((void **) &str);
        ccnxControl_Release(&testControl);
    }

    ccnxControl_Release(&control);
    cpiListener_Release(&test);
    cpiListener_Release(&listener);
}


LONGBOW_TEST_CASE(Global, cpiListener_FromControl_IP)
{
    CPIListener *listener = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");
    CCNxControl *control = cpiListener_CreateAddMessage(listener);

    CPIListener *test = cpiListener_FromControl(control);
    assertTrue(cpiListener_Equals(listener, test), "Listeners do not match")
    {
        printf("Expenected:\n");
        char *str = parcJSON_ToString(ccnxControl_GetJson(control));
        printf("   %s\n", str);
        parcMemory_Deallocate((void **) &str);

        printf("Got:\n");
        CCNxControl *testControl = cpiListener_CreateAddMessage(test);
        str = parcJSON_ToString(ccnxControl_GetJson(testControl));
        printf("   %s\n", str);
        parcMemory_Deallocate((void **) &str);
        ccnxControl_Release(&testControl);
    }

    ccnxControl_Release(&control);
    cpiListener_Release(&test);
    cpiListener_Release(&listener);
}

LONGBOW_TEST_CASE(Global, cpiListener_IsEtherEncap)
{
    CPIListener *x = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    bool isTrue = cpiListener_IsEtherEncap(x);
    assertTrue(isTrue, "Ether listener says it is not ether");
    cpiListener_Release(&x);
}

LONGBOW_TEST_CASE(Global, cpiListener_IsIPEncap)
{
    CPIListener *x = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");
    bool isTrue = cpiListener_IsIPEncap(x);
    assertTrue(isTrue, "IP listener says it is not IP");
    cpiListener_Release(&x);
}

LONGBOW_TEST_CASE(Global, cpiListener_GetAddress)
{
    CPIListener *x = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");
    CPIAddress *test = cpiListener_GetAddress(x);
    assertNotNull(test, "Got null address for IP listener");

    struct sockaddr_in sin;
    cpiAddress_GetInet(test, &sin);
    assertTrue(htons(sin.sin_port) == 9596, "Wrong port expected %u got %u", 9695, htons(sin.sin_port));

    uint32_t testip = htonl(sin.sin_addr.s_addr);
    uint32_t truthip = 0x7F000001;

    assertTrue(testip == truthip, "Wrong IP address expected %#08x got %#08x", truthip, testip);

    cpiListener_Release(&x);
}

LONGBOW_TEST_CASE(Global, cpiListener_GetEtherType)
{
    CPIListener *x = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    uint16_t test = cpiListener_GetEtherType(x);
    assertTrue(test == 0x0801, "Wrong ethertype, got %04x expected %04x", test, 0x0801);
    cpiListener_Release(&x);
}

LONGBOW_TEST_CASE(Global, cpiListener_GetInterfaceName)
{
    CPIListener *x = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    const char *test = cpiListener_GetInterfaceName(x);
    assertTrue(strcmp(test, "eth0") == 0, "Wrong interface name, got '%s' expected '%s'", test, "eth0");
    cpiListener_Release(&x);
}

LONGBOW_TEST_CASE(Global, cpiListener_GetSymbolicName)
{
    CPIListener *x = cpiListener_CreateEther("eth0", 0x0801, "puppy");
    const char *test = cpiListener_GetSymbolicName(x);
    assertTrue(strcmp(test, "puppy") == 0, "Wrong symbolic name, got '%s' expected '%s'", test, "puppy");
    cpiListener_Release(&x);
}

LONGBOW_TEST_CASE(Global, cpiListener_IsProtocolUdp)
{
    CPIListener *x = _conjureIPObject(IPTUN_UDP, "127.0.0.1", 9596, "puppy");
    assertTrue(cpiListener_IsProtocolUdp(x), "UDP listener did not say it was UDP");
    cpiListener_Release(&x);
}

LONGBOW_TEST_CASE(Global, cpiListener_IsProtocolTcp)
{
    CPIListener *x = _conjureIPObject(IPTUN_TCP, "127.0.0.1", 9596, "puppy");
    assertTrue(cpiListener_IsProtocolTcp(x), "TCP listener did not say it was TCP");
    cpiListener_Release(&x);
}

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

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