aboutsummaryrefslogtreecommitdiffstats
path: root/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/testrig_MockFramework.c
blob: 0ca07ab1be24e1bb31ff5f3745b2ad9bdd006dc9 (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
/*
 * 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.
 */

/**
 * This test rig sets up a mock RTA Framework for testing Components and Connectors.
 *
 *
 */

#include <ccnx/transport/transport_rta/core/rta_Framework.h>
#include <ccnx/transport/transport_rta/core/rta_Framework_Commands.c>
#include <ccnx/transport/transport_rta/core/rta_Framework_NonThreaded.h>

#ifndef MAXPATH
#define MAXPATH 1024
#endif

typedef struct mock_framework {
    PARCRingBuffer1x1*commandRingBuffer;
    PARCNotifier     *commandNotifier;
    RtaFramework     *framework;

    int stackId;
    RtaProtocolStack *stack;

    int connection_fds[2];
    RtaConnection    *connection;

    CCNxTransportConfig  *transport_config;
} MockFramework;

static MockFramework *
mockFramework_Create(CCNxTransportConfig *config)
{
    MockFramework *mock = parcMemory_AllocateAndClear(sizeof(MockFramework));
    assertNotNull(mock, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MockFramework));

    mock->transport_config = ccnxTransportConfig_Copy(config);
    assertNotNull(mock->transport_config, "%s got null params from createParams\n", __func__);

    mock->commandRingBuffer = parcRingBuffer1x1_Create(128, NULL);
    mock->commandNotifier = parcNotifier_Create();
    mock->framework = rtaFramework_Create(mock->commandRingBuffer, mock->commandNotifier);

    // Create the protocol stack

    mock->stackId = 1;
    RtaCommandCreateProtocolStack *createStack =
        rtaCommandCreateProtocolStack_Create(mock->stackId, ccnxTransportConfig_GetStackConfig(mock->transport_config));
    _rtaFramework_ExecuteCreateStack(mock->framework, createStack);
    rtaCommandCreateProtocolStack_Release(&createStack);

    // peek inside and get the protocol stack reference
    FrameworkProtocolHolder *fph = rtaFramework_GetProtocolStackByStackId(mock->framework, mock->stackId);
    mock->stack = fph->stack;

    int error = socketpair(AF_UNIX, SOCK_STREAM, 0, mock->connection_fds);
    assertFalse(error, "Error creating socket pair: (%d) %s", errno, strerror(errno));

    RtaCommandOpenConnection *openConnection = rtaCommandOpenConnection_Create(mock->stackId, mock->connection_fds[0], mock->connection_fds[1],
                                                                               ccnxConnectionConfig_GetJson(ccnxTransportConfig_GetConnectionConfig(mock->transport_config)));
    _rtaFramework_ExecuteOpenConnection(mock->framework, openConnection);
    rtaCommandOpenConnection_Release(&openConnection);

    mock->connection = rtaConnectionTable_GetByApiFd(mock->framework->connectionTable, mock->connection_fds[0]);

    // Uses the non-threaded forwarder, make sure we step at least once
    rtaFramework_NonThreadedStep(mock->framework);

    return mock;
}

static void
mockFramework_Destroy(MockFramework **mockPtr)
{
    MockFramework *mock = *mockPtr;

    rtaFramework_Teardown(mock->framework);

    parcRingBuffer1x1_Release(&mock->commandRingBuffer);
    parcNotifier_Release(&mock->commandNotifier);

    rtaFramework_Destroy(&mock->framework);
    ccnxTransportConfig_Destroy(&mock->transport_config);

    parcMemory_Deallocate((void **) &mock);
}