aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/transport/test_tools/traffic_tools.c
blob: 73b321e400cafa7b2a565fffaad9e1f295b9dba6 (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
/*
 * 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 <config.h>
#include <stdio.h>
#include <string.h>

#include <LongBow/runtime.h>
#include <parc/algol/parc_Memory.h>

#include "traffic_tools.h"
#include <ccnx/common/ccnx_NameSegmentNumber.h>
#include <ccnx/transport/transport_rta/core/rta_Component.h>

#include <ccnx/common/codec/schema_v1/testdata/v1_interest_nameA.h>

#include <ccnx/common/internal/ccnx_InterestDefault.h>

#include <ccnx/api/control/cpi_ControlFacade.h>

/**
 * @function decode_last_component_as_segment
 * @abstract Returns true is last name component is a segment, and returns the segment
 * @discussion
 *   The outputSegment may be null, in which case this function is just a true/false for the
 *   last path segment being an object segment number.
 *
 * @param outputSegment is an output parameter of the segment number, if returns true.  May be null.
 * @return <#return#>
 */
bool
trafficTools_GetObjectSegmentFromName(CCNxName *name, uint64_t *outputSegment)
{
    assertNotNull(name, "Name must be non-null");
    bool success = false;
    size_t segmentCount = ccnxName_GetSegmentCount(name);
    if (segmentCount > 0) {
        CCNxNameSegment *lastSegment = ccnxName_GetSegment(name, segmentCount - 1);
        if (ccnxNameSegment_GetType(lastSegment) == CCNxNameLabelType_CHUNK) {
            if (outputSegment) {
                *outputSegment = ccnxNameSegmentNumber_Value(lastSegment);
            }
            success = true;
        }
    }
    return success;
}

bool
trafficTools_ReadAndVerifySegment(PARCEventQueue *queue, CCNxName *basename, uint64_t expected, PARCBuffer *expectedPayload)
{
    TransportMessage *test_tm;
    CCNxName         *test_name;
    uint64_t segnum;
    CCNxName         *name_copy;

    test_tm = rtaComponent_GetMessage(queue);
    assertNotNull(test_tm, "got null transport message down the stack, expecting interest\n");

    assertTrue(transportMessage_IsInterest(test_tm),
               "Got wrong transport message pointer, is not an interest");

    CCNxTlvDictionary *interestDictionary = transportMessage_GetDictionary(test_tm);
    test_name = ccnxInterest_GetName(interestDictionary);

    bool success = trafficTools_GetObjectSegmentFromName(test_name, &segnum);
    assertTrue(success, "got error decoding last component as segnum: %s", ccnxName_ToString(test_name));
    assertTrue(expected == segnum, "Got wrong segnum, expected %" PRIu64 ", got %" PRIu64 "\n",
               expected, segnum);

    name_copy = ccnxName_Copy(test_name);
    ccnxName_Trim(name_copy, 1);
    assertTrue(ccnxName_Compare(basename, name_copy) == 0,
               "\nName '%s'\ndid not match\nexpected '%s'\nInterest name '%s'\n\n",
               ccnxName_ToString(name_copy),
               ccnxName_ToString(basename),
               ccnxName_ToString(test_name));

    ccnxName_Release(&name_copy);

    if (expectedPayload != NULL) {
        assertTrue(parcBuffer_Equals(expectedPayload, ccnxInterest_GetPayload(interestDictionary)),
                   "Expected the same Interest payload out as was sent in originally.");
    }

    transportMessage_Destroy(&test_tm);

    return true;
}

CCNxContentObject *
trafficTools_CreateSignedContentObject()
{
    CCNxName *name = ccnxName_CreateFromCString("lci:/hello/dolly");
    PARCBuffer *payload = parcBuffer_WrapCString("hello");

    CCNxContentObject *result = ccnxContentObject_CreateWithNameAndPayload(name, payload);

    PARCBuffer *keyId = parcBuffer_WrapCString("keyhash");
    PARCBuffer *sigbits = parcBuffer_WrapCString("siggybits");

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

    ccnxContentObject_SetSignature(result, keyId, signature, NULL);

    parcBuffer_Release(&payload);
    parcBuffer_Release(&keyId);
    parcSignature_Release(&signature);
    ccnxName_Release(&name);

    return result;
}

CCNxContentObject *
trafficTools_CreateContentObjectWithPayload(PARCBuffer *contents)
{
    CCNxName *name = ccnxName_CreateFromCString("lci:/hello/dolly");

    CCNxContentObject *result = ccnxContentObject_CreateWithNameAndPayload(name, contents);

    ccnxName_Release(&name);

    return result;
}

TransportMessage *
trafficTools_CreateTransportMessageWithSignedContentObject(RtaConnection *connection)
{
    CCNxContentObject *unsignedObject = trafficTools_CreateSignedContentObject();
    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromContentObject(unsignedObject);
    TransportMessage *tm = transportMessage_CreateFromDictionary(message);

    transportMessage_SetInfo(tm, connection, NULL);

    ccnxContentObject_Release(&unsignedObject);
    ccnxMetaMessage_Release(&message);

    return tm;
}

TransportMessage *
trafficTools_CreateTransportMessageWithSignedContentObjectWithName(RtaConnection *connection, CCNxName *name, const char *keystorePath, const char *keystorePassword)
{
    PARCBuffer *payload = parcBuffer_WrapCString("hello");

    CCNxContentObject *contentObject = ccnxContentObject_CreateWithNameAndPayload(name, payload);
    PARCBuffer *keyId = parcBuffer_WrapCString("hash of key");
    PARCBuffer *sigbits = parcBuffer_WrapCString("sig bits");
    PARCSignature *signature = parcSignature_Create(PARCSigningAlgorithm_RSA, PARCCryptoHashType_SHA256, sigbits);
    parcBuffer_Release(&sigbits);

    ccnxContentObject_SetSignature(contentObject, keyId, signature, NULL);

    CCNxMetaMessage *message = ccnxMetaMessage_CreateFromContentObject(contentObject);
    TransportMessage *tm = transportMessage_CreateFromDictionary(message);
    transportMessage_SetInfo(tm, connection, NULL);

    ccnxMetaMessage_Release(&message);
    ccnxContentObject_Release(&contentObject);
    parcSignature_Release(&signature);
    parcBuffer_Release(&keyId);
    return tm;
}

CCNxInterest *
trafficTools_CreateInterest(void)
{
    CCNxName *name = ccnxName_CreateFromCString("lci:/there/were/bells/on/the/hill");
    CCNxInterest *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);

    return interest;
}

CCNxTlvDictionary *
trafficTools_CreateDictionaryInterest(void)
{
    CCNxName *name = ccnxName_CreateFromCString("lci:/there/were/bells/on/the/hill");
    CCNxTlvDictionary *interest = ccnxInterest_CreateSimple(name);
    ccnxName_Release(&name);

    return interest;
}

TransportMessage *
trafficTools_CreateTransportMessageWithInterest(RtaConnection *connection)
{
    return trafficTools_CreateTransportMessageWithDictionaryInterest(connection, CCNxTlvDictionary_SchemaVersion_V1);
}

TransportMessage *
trafficTools_CreateTransportMessageWithControlMessage(RtaConnection *connection)
{
    return trafficTools_CreateTransportMessageWithDictionaryControl(connection, CCNxTlvDictionary_SchemaVersion_V1);
}

TransportMessage *
trafficTools_CreateTransportMessageWithRaw(RtaConnection *connection)
{
    return trafficTools_CreateTransportMessageWithDictionaryRaw(connection, CCNxTlvDictionary_SchemaVersion_V1);
}

TransportMessage *
trafficTools_CreateTransportMessageWithDictionaryInterest(RtaConnection *connection, CCNxTlvDictionary_SchemaVersion schema)
{
    CCNxTlvDictionary *interest;
    CCNxName *name = ccnxName_CreateFromCString("lci:/lost/in/space");

    CCNxInterestInterface *impl = NULL;

    switch (schema) {
        case CCNxTlvDictionary_SchemaVersion_V1:
            impl = &CCNxInterestFacadeV1_Implementation;
            break;

        default:
            trapIllegalValue(schema, "Unsupported schema version");
    }

    // impl should be set if we get here.
    interest = ccnxInterest_CreateWithImpl(impl,
                                           name,
                                           CCNxInterestDefault_LifetimeMilliseconds,
                                           NULL,
                                           NULL,
                                           CCNxInterestDefault_HopLimit);


    TransportMessage *tm = transportMessage_CreateFromDictionary(interest);
    ccnxTlvDictionary_Release(&interest);

    transportMessage_SetInfo(tm, connection, NULL);
    ccnxName_Release(&name);
    return tm;
}

TransportMessage *
trafficTools_CreateTransportMessageWithDictionaryRaw(RtaConnection *connection, unsigned schema)
{
    PARCBuffer *buffer = parcBuffer_Allocate(sizeof(v1_interest_nameA));
    parcBuffer_PutArray(buffer, sizeof(v1_interest_nameA), v1_interest_nameA);
    parcBuffer_Flip(buffer);
    CCNxTlvDictionary *wireformat = ccnxWireFormatMessage_FromInterestPacketType(schema, buffer);

    TransportMessage *tm = transportMessage_CreateFromDictionary(wireformat);
    ccnxTlvDictionary_Release(&wireformat);
    parcBuffer_Release(&buffer);

    transportMessage_SetInfo(tm, connection, NULL);
    return tm;
}

TransportMessage *
trafficTools_CreateTransportMessageWithDictionaryControl(RtaConnection *connection, unsigned schema)
{
    char *jsonstring = "{\"CPI_REQUEST\":{\"SEQUENCE\":22,\"REGISTER\":{\"PREFIX\":\"lci:/howdie/stranger\",\"INTERFACE\":55,\"FLAGS\":0,\"PROTOCOL\":\"STATIC\",\"ROUTETYPE\":\"LONGEST\",\"COST\":200}}}";

    PARCJSON *json = parcJSON_ParseString(jsonstring);
    CCNxTlvDictionary *control = NULL;

    switch (schema) {
        case 1:
            control = ccnxControlFacade_CreateCPI(json);

            break;

        default:
            break;
    }


    TransportMessage *tm = transportMessage_CreateFromDictionary(control);
    transportMessage_SetInfo(tm, connection, NULL);

    parcJSON_Release(&json);
    ccnxTlvDictionary_Release(&control);

    return tm;
}