diff options
Diffstat (limited to 'libccnx-transport-rta/ccnx/transport/transport_rta/core/test')
14 files changed, 2461 insertions, 0 deletions
diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/.gitignore b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/.gitignore new file mode 100644 index 00000000..8763938d --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/.gitignore @@ -0,0 +1,10 @@ +test_rta_Component +test_rta_Connection +test_rta_Framework_NonThreaded +test_rta_Framework_Services +test_rta_Framework_Threaded +test_rta_ProtocolStack +test_rta_Logger +test_rta_Stats +output.txt +core diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/CMakeLists.txt b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/CMakeLists.txt new file mode 100644 index 00000000..b57c2afd --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/CMakeLists.txt @@ -0,0 +1,23 @@ +# Enable gcov output for the tests +add_definitions(--coverage) +set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} " --coverage") + +set(TestsExpectedToPass + test_rta_ConnectionTable + test_rta_Framework + test_rta_Framework_Commands + test_rta_Component + test_rta_Connection + test_rta_Framework_NonThreaded + test_rta_Framework_Services + test_rta_Framework_Threaded + test_rta_Logger + test_rta_ProtocolStack + test_rta_ComponentStats +) + + +foreach(test ${TestsExpectedToPass}) + AddTest(${test}) +endforeach() + diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Component.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Component.c new file mode 100644 index 00000000..a1de01bf --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Component.c @@ -0,0 +1,247 @@ +/* + * 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. + */ + +/** + * Create a non-threaded framework to test comopnent functions. + * + */ +#define DEBUG_OUTPUT 1 +#include "../rta_Component.c" + +#include <parc/algol/parc_SafeMemory.h> +#include <LongBow/unit-test.h> + +#include <ccnx/transport/transport_rta/core/rta_ProtocolStack.h> +#include <ccnx/transport/transport_rta/config/config_All.h> +#include <ccnx/transport/transport_rta/core/rta_Framework_Commands.c> +#include <ccnx/transport/test_tools/traffic_tools.h> + +#include <sys/socket.h> +#include <errno.h> + +#define PAIR_OTHER 0 +#define PAIR_TRANSPORT 1 + +typedef struct test_data { + PARCRingBuffer1x1 *commandRingBuffer; + PARCNotifier *commandNotifier; + + int api_fds[2]; + RtaFramework *framework; + RtaProtocolStack *stack; + RtaConnection *connection; +} TestData; + +static TestData * +_commonSetup(void) +{ + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); + + int error = socketpair(AF_UNIX, SOCK_STREAM, 0, data->api_fds); + assertFalse(error, "Error creating socket pair: (%d) %s", errno, strerror(errno)); + + data->commandRingBuffer = parcRingBuffer1x1_Create(128, NULL); + data->commandNotifier = parcNotifier_Create(); + data->framework = rtaFramework_Create(data->commandRingBuffer, data->commandNotifier); + + assertNotNull(data->framework, "rtaFramework_Create returned null"); + + CCNxStackConfig *stackConfig = ccnxStackConfig_Create(); + apiConnector_ProtocolStackConfig(stackConfig); + testingLower_ProtocolStackConfig(stackConfig); + protocolStack_ComponentsConfigArgs(stackConfig, apiConnector_GetName(), testingLower_GetName(), NULL); + + rtaFramework_NonThreadedStepCount(data->framework, 10); + + int stackId = 1; + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, stackConfig); + _rtaFramework_ExecuteCreateStack(data->framework, createStack); + rtaCommandCreateProtocolStack_Release(&createStack); + + rtaFramework_NonThreadedStepCount(data->framework, 10); + data->stack = (rtaFramework_GetProtocolStackByStackId(data->framework, stackId))->stack; + + CCNxConnectionConfig *connConfig = ccnxConnectionConfig_Create(); + apiConnector_ConnectionConfig(connConfig); + + tlvCodec_ConnectionConfig(connConfig); + + testingLower_ConnectionConfig(connConfig); + + RtaCommandOpenConnection *openConnection = rtaCommandOpenConnection_Create(stackId, + data->api_fds[PAIR_OTHER], + data->api_fds[PAIR_TRANSPORT], + ccnxConnectionConfig_GetJson(connConfig)); + + rtaFramework_NonThreadedStepCount(data->framework, 10); + _rtaFramework_ExecuteOpenConnection(data->framework, openConnection); + rtaCommandOpenConnection_Release(&openConnection); + + rtaFramework_NonThreadedStepCount(data->framework, 10); + data->connection = rtaConnectionTable_GetByApiFd(data->framework->connectionTable, data->api_fds[PAIR_OTHER]); + + ccnxConnectionConfig_Destroy(&connConfig); + ccnxStackConfig_Release(&stackConfig); + + return data; +} + +static void +_commonTeardown(TestData *data) +{ + rtaFramework_Teardown(data->framework); + + parcRingBuffer1x1_Release(&data->commandRingBuffer); + parcNotifier_Release(&data->commandNotifier); + rtaFramework_Destroy(&data->framework); + + close(data->api_fds[0]); + close(data->api_fds[1]); + parcMemory_Deallocate((void **) &data); +} + +LONGBOW_TEST_RUNNER(rta_Component) +{ + // 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); + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Component) +{ + 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(rta_Component) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaComponent_GetOutputQueue); + + LONGBOW_RUN_TEST_CASE(Global, rtaComponent_PutMessage_ClosedConnection); + LONGBOW_RUN_TEST_CASE(Global, rtaComponent_PutMessage_OpenConnection); +} + +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, rtaComponent_GetOutputQueue) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + PARCEventQueue *queue = rtaComponent_GetOutputQueue(data->connection, API_CONNECTOR, RTA_DOWN); + assertNotNull(queue, "Got null queue for API_CONNECTOR DOWN queue"); +} + +LONGBOW_TEST_CASE(Global, rtaComponent_PutMessage_ClosedConnection) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + rtaConnection_SetState(data->connection, CONN_CLOSED); + + // Create the TransportMessage to put on the queue + TransportMessage *tm = trafficTools_CreateTransportMessageWithDictionaryControl(data->connection, CCNxTlvDictionary_SchemaVersion_V1); + + // Send it down from the API connector to the Testing Lower component + PARCEventQueue *outputQueue = rtaComponent_GetOutputQueue(data->connection, API_CONNECTOR, RTA_DOWN); + + int success = rtaComponent_PutMessage(outputQueue, tm); + assertFalse(success, "Error putting message on API Connector's down queue"); + + // check that we got it + PARCEventQueue *inputQueue = rtaComponent_GetOutputQueue(data->connection, TESTING_LOWER, RTA_UP); + + TransportMessage *test_tm = rtaComponent_GetMessage(inputQueue); + assertNull(test_tm, "Should have returned NULL on a closed connection"); + + // The transport message was destroyed by PutMessage because the connection + // was closed. Don't need to destroy the transport message. + + // set state back to OPEN so the connection is properly disposed of + rtaConnection_SetState(data->connection, CONN_OPEN); +} + +LONGBOW_TEST_CASE(Global, rtaComponent_PutMessage_OpenConnection) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + // Create the TransportMessage to put on the queue + TransportMessage *tm = trafficTools_CreateTransportMessageWithDictionaryControl(data->connection, CCNxTlvDictionary_SchemaVersion_V1); + + // Send it down from the API connector to the Testing Lower component + PARCEventQueue *outputQueue = rtaComponent_GetOutputQueue(data->connection, API_CONNECTOR, RTA_DOWN); + + int success = rtaComponent_PutMessage(outputQueue, tm); + assertTrue(success, "Error putting message on API Connector's down queue"); + + // check that we got it + PARCEventQueue *inputQueue = rtaComponent_GetOutputQueue(data->connection, TESTING_LOWER, RTA_UP); + + TransportMessage *test_tm = rtaComponent_GetMessage(inputQueue); + assertTrue(test_tm == tm, "Got wrong message, got %p expected %p", (void *) test_tm, (void *) tm); + + transportMessage_Destroy(&tm); +} + +LONGBOW_TEST_FIXTURE(Local) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + 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; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Component); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ComponentStats.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ComponentStats.c new file mode 100644 index 00000000..1da0b4fc --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ComponentStats.c @@ -0,0 +1,189 @@ +/* + * 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 "../rta_ComponentStats.c" +#include <parc/algol/parc_SafeMemory.h> + +#include <ccnx/transport/transport_rta/core/rta_ProtocolStack.h> +#include <ccnx/transport/transport_rta/config/config_All.h> + +#include <inttypes.h> +#include <sys/socket.h> +#include <errno.h> + +#define PAIR_OTHER 0 +#define PAIR_TRANSPORT 1 + +#include <LongBow/unit-test.h> + +typedef struct test_data { + PARCRingBuffer1x1 *commandRingBuffer; + PARCNotifier *commandNotifier; + + int api_fds[2]; + RtaFramework *framework; + RtaProtocolStack *stack; +} TestData; + +static TestData * +_commonSetup(void) +{ + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); + + int error = socketpair(AF_UNIX, SOCK_STREAM, 0, data->api_fds); + assertTrue(error == 0, "Error creating socket pair: (%d) %s", errno, strerror(errno)); + + data->commandRingBuffer = parcRingBuffer1x1_Create(128, NULL); + data->commandNotifier = parcNotifier_Create(); + data->framework = rtaFramework_Create(data->commandRingBuffer, data->commandNotifier); + assertNotNull(data->framework, "rtaFramework_Create returned null"); + + rtaFramework_Start(data->framework); + + CCNxStackConfig *stackConfig = ccnxStackConfig_Create(); + apiConnector_ProtocolStackConfig(stackConfig); + testingLower_ProtocolStackConfig(stackConfig); + protocolStack_ComponentsConfigArgs(stackConfig, apiConnector_GetName(), testingLower_GetName(), NULL); + data->stack = rtaProtocolStack_Create(data->framework, ccnxStackConfig_GetJson(stackConfig), 1); + + ccnxStackConfig_Release(&stackConfig); + return data; +} + +static void +_commonTeardown(TestData *data) +{ + rtaProtocolStack_Destroy(&data->stack); + + // blocks until done + rtaFramework_Shutdown(data->framework); + + parcRingBuffer1x1_Release(&data->commandRingBuffer); + parcNotifier_Release(&data->commandNotifier); + rtaFramework_Destroy(&data->framework); + + close(data->api_fds[0]); + close(data->api_fds[1]); + parcMemory_Deallocate((void **) &data); +} + +LONGBOW_TEST_RUNNER(rta_Stats) +{ + LONGBOW_RUN_TEST_FIXTURE(Global); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Stats) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_Stats) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, stats_Create_Destroy); + LONGBOW_RUN_TEST_CASE(Global, stats_Dump); + LONGBOW_RUN_TEST_CASE(Global, stats_Get); + LONGBOW_RUN_TEST_CASE(Global, stats_Increment); +} + +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, stats_Create_Destroy) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + RtaComponentStats *stats = rtaComponentStats_Create(data->stack, API_CONNECTOR); + + assertNotNull(stats, "Got null stats from rtaComponentStats_Create"); + assertTrue(stats->stack == data->stack, + "Bad stack pointer, got %p expected %p", + (void *) stats->stack, (void *) data->stack); + + rtaComponentStats_Destroy(&stats); +} + +LONGBOW_TEST_CASE(Global, stats_Dump) +{ + for (int i = 0; i < STATS_LAST; i++) { + char *test = rtaComponentStatType_ToString(i); + assertNotNull(test, "Got null string for stat type %d", i); + } +} + +LONGBOW_TEST_CASE(Global, stats_Get) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + RtaComponentStats *stats = rtaComponentStats_Create(data->stack, API_CONNECTOR); + + for (int i = 0; i < STATS_LAST; i++) { + // set each stat to a value + uint64_t value = i + 5; + stats->stats[i] = value; + + uint64_t counter = stats->stats[i]; + assertTrue(counter == value, "Counter %d wrong value, got %" PRIu64 " expected %" PRIu64, i, counter, value); + } + + rtaComponentStats_Destroy(&stats); +} + +LONGBOW_TEST_CASE(Global, stats_Increment) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + RtaComponentStats *stats = rtaComponentStats_Create(data->stack, API_CONNECTOR); + + for (int i = 0; i < STATS_LAST; i++) { + rtaComponentStats_Increment(stats, (RtaComponentStatType) i); + } + + // now make sure they are all "1" + for (int i = 0; i < STATS_LAST; i++) { + uint64_t counter = stats->stats[i]; + assertTrue(counter == 1, "Counter %d wrong value, got %" PRIu64 "expected 1", i, counter); + } + + rtaComponentStats_Destroy(&stats); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Stats); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Connection.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Connection.c new file mode 100644 index 00000000..4b80d7e1 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Connection.c @@ -0,0 +1,82 @@ +/* + * 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 "../rta_Connection.c" +#include <parc/algol/parc_SafeMemory.h> +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(rta_Connection) +{ + // 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); + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Connection) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_Connection) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ +} + +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_FIXTURE(Local) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Connection); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ConnectionTable.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ConnectionTable.c new file mode 100644 index 00000000..d77f47c0 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ConnectionTable.c @@ -0,0 +1,309 @@ +/* + * 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 "../rta_ConnectionTable.c" +#include "../rta_ProtocolStack.c" +#include <LongBow/unit-test.h> + +#include <parc/algol/parc_SafeMemory.h> + +LONGBOW_TEST_RUNNER(rta_ConnectionTable) +{ + // 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(rta_ConnectionTable) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_ConnectionTable) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaConnectionTable_AddConnection); + LONGBOW_RUN_TEST_CASE(Global, rtaConnectionTable_AddConnection_TooMany); + LONGBOW_RUN_TEST_CASE(Global, rtaConnectionTable_Create_Destroy); + LONGBOW_RUN_TEST_CASE(Global, rtaConnectionTable_GetByApiFd); + LONGBOW_RUN_TEST_CASE(Global, rtaConnectionTable_GetByTransportFd); + LONGBOW_RUN_TEST_CASE(Global, rtaConnectionTable_Remove); + LONGBOW_RUN_TEST_CASE(Global, rtaConnectionTable_RemoveByStack); +} + +typedef struct test_data { + PARCRingBuffer1x1 *commandRingBuffer; + PARCNotifier *commandNotifier; + + RtaFramework *framework; + + // in some tests we use two protocol stacks + RtaProtocolStack *stack_a; + RtaProtocolStack *stack_b; +} TestData; + +static RtaConnection * +createConnection(RtaProtocolStack *stack, int api_fd, int transport_fd) +{ + // ------- + // Create a connection to use in the table + PARCJSON *params = parcJSON_ParseString("{}"); + RtaCommandOpenConnection *openConnection = rtaCommandOpenConnection_Create(stack->stack_id, api_fd, transport_fd, params); + + // Create a connection that goes in the connection table + RtaConnection *conn = rtaConnection_Create(stack, openConnection); + assertNotNull(conn, "Got null connection from rtaConnection_Create"); + + rtaCommandOpenConnection_Release(&openConnection); + parcJSON_Release(¶ms); + return conn; +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); + + // --------------------------- + // To test a connection table, we need to create a Framework and a Protocol stack + + data->commandRingBuffer = parcRingBuffer1x1_Create(128, NULL); + data->commandNotifier = parcNotifier_Create(); + + data->framework = rtaFramework_Create(data->commandRingBuffer, data->commandNotifier); + + // fake out a protocol stack + data->stack_a = parcMemory_AllocateAndClear(sizeof(RtaProtocolStack)); + assertNotNull(data->stack_a, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(RtaProtocolStack)); + data->stack_a->stack_id = 1; + data->stack_a->framework = data->framework; + + // fake out a protocol stack + data->stack_b = parcMemory_AllocateAndClear(sizeof(RtaProtocolStack)); + assertNotNull(data->stack_b, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(RtaProtocolStack)); + data->stack_b->stack_id = 2; + data->stack_b->framework = data->framework; + + longBowTestCase_SetClipBoardData(testCase, data); + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + // now cleanup everything + rtaFramework_Destroy(&data->framework); + parcNotifier_Release(&data->commandNotifier); + parcRingBuffer1x1_Release(&data->commandRingBuffer); + + parcMemory_Deallocate((void **) &(data->stack_a)); + parcMemory_Deallocate((void **) &(data->stack_b)); + parcMemory_Deallocate((void **) &data); + + 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; +} + +/** + * Destroy the table before destroying the connection + */ +LONGBOW_TEST_CASE(Global, rtaConnectionTable_AddConnection) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + RtaConnection *conn = createConnection(data->stack_a, 2, 3); + + // This is the part we want to test. + RtaConnectionTable *table = rtaConnectionTable_Create(1000, rtaConnection_Destroy); + rtaConnectionTable_AddConnection(table, conn); + + assertTrue(table->count_elements == 1, "Incorrect table size, expected %d got %zu", 1, table->count_elements); + rtaConnectionTable_Destroy(&table); +} + + +/** + * Create a connection table with just 1 connection and make sure table + * does the right thing on overflow + */ +LONGBOW_TEST_CASE(Global, rtaConnectionTable_AddConnection_TooMany) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + RtaConnection *conn = createConnection(data->stack_a, 2, 3); + + int res; + + // create the table with size 1 + RtaConnectionTable *table = rtaConnectionTable_Create(1, rtaConnection_Destroy); + res = rtaConnectionTable_AddConnection(table, conn); + assertTrue(res == 0, "Got non-zero return %d", res); + assertTrue(table->count_elements == 1, "Incorrect table size, expected %d got %zu", 1, table->count_elements); + + // add the second connection, should return failure + res = rtaConnectionTable_AddConnection(table, conn); + assertTrue(res == -1, "Should have failed, expecting -1, got %d", res); + + rtaConnectionTable_Destroy(&table); +} + + +LONGBOW_TEST_CASE(Global, rtaConnectionTable_Create_Destroy) +{ + size_t beforeBalance = parcMemory_Outstanding(); + RtaConnectionTable *table = rtaConnectionTable_Create(1000, rtaConnection_Destroy); + assertTrue(table->max_elements == 1000, "Initialized with wrong number of elements"); + rtaConnectionTable_Destroy(&table); + size_t afterBalance = parcMemory_Outstanding(); + assertTrue(beforeBalance == afterBalance, "Memory imbalance after create/destroy"); +} + +LONGBOW_TEST_CASE(Global, rtaConnectionTable_GetByApiFd) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + RtaConnection *conn = createConnection(data->stack_a, 2, 3); + + RtaConnectionTable *table = rtaConnectionTable_Create(1000, rtaConnection_Destroy); + rtaConnectionTable_AddConnection(table, conn); + + RtaConnection *test; + test = rtaConnectionTable_GetByApiFd(table, 2); + assertTrue(test == conn, "Got wrong connection, expecting %p got %p", (void *) conn, (void *) test); + + test = rtaConnectionTable_GetByApiFd(table, 3); + assertTrue(test == NULL, "Got wrong connection, expecting %p got %p", NULL, (void *) test); + + test = rtaConnectionTable_GetByApiFd(table, 4); + assertTrue(test == NULL, "Got wrong connection, expecting %p got %p", NULL, (void *) test); + + rtaConnectionTable_Destroy(&table); +} + +LONGBOW_TEST_CASE(Global, rtaConnectionTable_GetByTransportFd) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + RtaConnection *conn = createConnection(data->stack_a, 2, 3); + + RtaConnectionTable *table = rtaConnectionTable_Create(1000, rtaConnection_Destroy); + rtaConnectionTable_AddConnection(table, conn); + + RtaConnection *test; + test = rtaConnectionTable_GetByTransportFd(table, 2); + assertTrue(test == NULL, "Got wrong connection, expecting %p got %p", NULL, (void *) test); + + test = rtaConnectionTable_GetByTransportFd(table, 3); + assertTrue(test == conn, "Got wrong connection, expecting %p got %p", (void *) conn, (void *) test); + + test = rtaConnectionTable_GetByTransportFd(table, 4); + assertTrue(test == NULL, "Got wrong connection, expecting %p got %p", NULL, (void *) test); + + + rtaConnectionTable_Destroy(&table); +} + +/** + * We create two connections and make sure that when we remove one the other + * is still in the table + */ +LONGBOW_TEST_CASE(Global, rtaConnectionTable_Remove) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + int res; + int a_pair[2]; + int b_pair[2]; + + // we have to use actual socket pairs in this test because Remove will destroy + // the last copy of the connection and call close() on the sockets. + socketpair(PF_LOCAL, SOCK_STREAM, 0, a_pair); + socketpair(PF_LOCAL, SOCK_STREAM, 0, b_pair); + + RtaConnectionTable *table = rtaConnectionTable_Create(1000, rtaConnection_Destroy); + + RtaConnection *conn_a = createConnection(data->stack_a, a_pair[0], a_pair[1]); + rtaConnectionTable_AddConnection(table, conn_a); + + RtaConnection *conn_b = createConnection(data->stack_b, b_pair[0], b_pair[1]); + rtaConnectionTable_AddConnection(table, conn_b); + + assertTrue(table->count_elements == 2, "Wrong element count"); + + res = rtaConnectionTable_Remove(table, conn_b); + assertTrue(res == 0, "Got error from rtaConnectionTable_Remove: %d", res); + assertTrue(table->count_elements == 1, "Wrong element count"); + + RtaConnection *test = rtaConnectionTable_GetByApiFd(table, a_pair[0]); + assertNotNull(test, "Could not retrieve connection that was supposed to still be there"); + + rtaConnectionTable_Destroy(&table); +} + +/** + * Create two connections, they are in different protocol stacks. Remove one by + * stack id and make sure the other is still in the table + */ +LONGBOW_TEST_CASE(Global, rtaConnectionTable_RemoveByStack) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + int res; + int a_pair[2]; + int b_pair[2]; + + // we have to use actual socket pairs in this test because Remove will destroy + // the last copy of the connection and call close() on the sockets. + socketpair(PF_LOCAL, SOCK_STREAM, 0, a_pair); + socketpair(PF_LOCAL, SOCK_STREAM, 0, b_pair); + + RtaConnectionTable *table = rtaConnectionTable_Create(1000, rtaConnection_Destroy); + + RtaConnection *conn_a = createConnection(data->stack_a, a_pair[0], a_pair[1]); + rtaConnectionTable_AddConnection(table, conn_a); + + RtaConnection *conn_b = createConnection(data->stack_b, b_pair[0], b_pair[1]); + rtaConnectionTable_AddConnection(table, conn_b); + + // now remove a connection by stack id + + res = rtaConnectionTable_RemoveByStack(table, data->stack_a->stack_id); + assertTrue(res == 0, "Got error from rtaConnectionTable_RemoveByStack: %d", res); + assertTrue(table->count_elements == 1, "Wrong element count"); + + RtaConnection *test = rtaConnectionTable_GetByApiFd(table, b_pair[0]); + assertNotNull(test, "Could not retrieve connection that was supposed to still be there"); + + rtaConnectionTable_Destroy(&table); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_ConnectionTable); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework.c new file mode 100644 index 00000000..715908f3 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework.c @@ -0,0 +1,298 @@ +/* + * 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 "../rta_Framework.c" +#include <ccnx/transport/transport_rta/commands/rta_Command.h> + +#include <parc/algol/parc_SafeMemory.h> +#include <LongBow/unit-test.h> +#include <math.h> + +typedef struct test_data { + PARCRingBuffer1x1 *commandRingBuffer; + PARCNotifier *commandNotifier; + RtaFramework *framework; +} TestData; + + +static TestData * +_createTestData(void) +{ + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); + data->commandRingBuffer = parcRingBuffer1x1_Create(128, NULL); + data->commandNotifier = parcNotifier_Create(); + data->framework = rtaFramework_Create(data->commandRingBuffer, data->commandNotifier); + rtaLogger_SetLogLevel(data->framework->logger, RtaLoggerFacility_Framework, PARCLogLevel_Debug); + return data; +} + +static void +_destroyTestData(TestData *data) +{ + parcRingBuffer1x1_Release(&data->commandRingBuffer); + parcNotifier_Release(&data->commandNotifier); + rtaFramework_Destroy(&data->framework); + parcMemory_Deallocate((void **) &data); +} + +LONGBOW_TEST_RUNNER(rta_Framework) +{ + LONGBOW_RUN_TEST_FIXTURE(Global); + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Framework) +{ + 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(rta_Framework) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// =================================================================== + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaFramework_Create_Destroy); + LONGBOW_RUN_TEST_CASE(Global, rtaFramework_GetEventScheduler); + LONGBOW_RUN_TEST_CASE(Global, rtaFramework_GetNextConnectionId); + LONGBOW_RUN_TEST_CASE(Global, rtaFramework_GetStatus); + LONGBOW_RUN_TEST_CASE(Global, rtaFramework_Start_Shutdown); + LONGBOW_RUN_TEST_CASE(Global, tick_cb); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + longBowTestCase_SetClipBoardData(testCase, _createTestData()); + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + _destroyTestData(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, rtaFramework_Create_Destroy) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + assertNotNull(data->framework, "rtaFramework_Create returned null"); + assertTrue(data->framework->commandRingBuffer == data->commandRingBuffer, "framework commandRingBuffer incorrect"); + assertTrue(data->framework->commandNotifier == data->commandNotifier, "framework commandNotifier incorrect"); + assertNotNull(data->framework->commandEvent, "framework commandEvent is null"); +} + +LONGBOW_TEST_CASE(Global, rtaFramework_GetEventScheduler) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + assertTrue(rtaFramework_GetEventScheduler(data->framework) == data->framework->base, "getEventScheduler broken"); +} + +LONGBOW_TEST_CASE(Global, rtaFramework_GetNextConnectionId) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + assertTrue(rtaFramework_GetNextConnectionId(data->framework) == 1, "GetNextConnetionId not starting at 1"); + assertTrue(rtaFramework_GetNextConnectionId(data->framework) == 2, "GetNextConnetionId first increment not 2"); +} + +LONGBOW_TEST_CASE(Global, rtaFramework_GetStatus) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + assertTrue(rtaFramework_GetStatus(data->framework) == FRAMEWORK_INIT, "Wrong initial status"); +} + +LONGBOW_TEST_CASE(Global, rtaFramework_Start_Shutdown) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + rtaFramework_Start(data->framework); + assertTrue(rtaFramework_WaitForStatus(data->framework, FRAMEWORK_RUNNING) == FRAMEWORK_RUNNING, "Status not RUNNING"); + + // blocks until done + rtaFramework_Shutdown(data->framework); +} + +LONGBOW_TEST_CASE(Global, tick_cb) +{ + ticks tic0, tic1; + struct timeval t0, t1; + double delta_tic, delta_t, delta_abs; + + TestData *data = longBowTestCase_GetClipBoardData(testCase); + rtaFramework_Start(data->framework); + assertTrue(rtaFramework_WaitForStatus(data->framework, FRAMEWORK_RUNNING) == FRAMEWORK_RUNNING, "Status not RUNNING"); + + gettimeofday(&t0, NULL); + tic0 = data->framework->clock_ticks; + sleep(2); + gettimeofday(&t1, NULL); + tic1 = data->framework->clock_ticks; + + delta_t = (t1.tv_sec + t1.tv_usec * 1E-6) - (t0.tv_sec + t0.tv_usec * 1E-6); + delta_tic = ((tic1 - tic0) * FC_USEC_PER_TICK) * 1E-6; + delta_abs = fabs(delta_tic - delta_t); + + printf("over 2 seconds, absolute clock error is %.6f seconds\n", delta_abs); + + + // blocks until done + rtaFramework_Shutdown(data->framework); +} + +// =================================================== + +LONGBOW_TEST_FIXTURE(Local) +{ + LONGBOW_RUN_TEST_CASE(Local, _setLogLevels_All); + LONGBOW_RUN_TEST_CASE(Local, _setLogLevels_All_Framework); + LONGBOW_RUN_TEST_CASE(Local, _setLogLevels_Framework); + LONGBOW_RUN_TEST_CASE(Local, _setLogLevels_ApiConnector); + LONGBOW_RUN_TEST_CASE(Local, _setLogLevels_FlowController); + LONGBOW_RUN_TEST_CASE(Local, _setLogLevels_Codec); + LONGBOW_RUN_TEST_CASE(Local, _setLogLevels_ForwarderConnector); +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + longBowTestCase_SetClipBoardData(testCase, _createTestData()); + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + _destroyTestData(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(Local, _setLogLevels_All) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + setenv("RtaFacility_All", "Warning", 1); + _setLogLevels(data->framework); + + for (int i = 0; i < RtaLoggerFacility_END; i++) { + bool isLoggable = rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), i, PARCLogLevel_Warning); + assertTrue(isLoggable, "Facility %s not set to Warning", rtaLogger_FacilityString(i)); + } + + unsetenv("RtaFacility_All"); +} + +LONGBOW_TEST_CASE(Local, _setLogLevels_All_Framework) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + setenv("RtaFacility_All", "Info", 1); + setenv("RtaFacility_Framework", "Warning", 1); + _setLogLevels(data->framework); + + assertTrue(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_ApiConnector, PARCLogLevel_Info), "Api facility not Info"); + assertTrue(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_Framework, PARCLogLevel_Warning), "Framework not Warning"); + + unsetenv("RtaFacility_All"); + unsetenv("RtaFacility_Framework"); +} + +LONGBOW_TEST_CASE(Local, _setLogLevels_Framework) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + setenv("RtaFacility_Framework", "Warning", 1); + _setLogLevels(data->framework); + + assertFalse(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_Framework, PARCLogLevel_Info), "Info should not be loggable"); + assertTrue(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_Framework, PARCLogLevel_Warning), "Warning should be loggable"); + unsetenv("RtaFacility_Framework"); +} + +LONGBOW_TEST_CASE(Local, _setLogLevels_ApiConnector) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + setenv("RtaFacility_Api", "Warning", 1); + _setLogLevels(data->framework); + + assertFalse(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_ApiConnector, PARCLogLevel_Info), "Info should not be loggable"); + assertTrue(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_ApiConnector, PARCLogLevel_Warning), "Warning should be loggable"); + unsetenv("RtaFacility_Api"); +} + +LONGBOW_TEST_CASE(Local, _setLogLevels_FlowController) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + setenv("RtaFacility_Flowcontrol", "Warning", 1); + _setLogLevels(data->framework); + + assertFalse(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_Flowcontrol, PARCLogLevel_Info), "Info should not be loggable"); + assertTrue(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_Flowcontrol, PARCLogLevel_Warning), "Warning should be loggable"); + unsetenv("RtaFacility_Flowcontrol"); +} + +LONGBOW_TEST_CASE(Local, _setLogLevels_Codec) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + setenv("RtaFacility_Codec", "Warning", 1); + _setLogLevels(data->framework); + + assertFalse(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_Codec, PARCLogLevel_Info), "Info should not be loggable"); + assertTrue(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_Codec, PARCLogLevel_Warning), "Warning should be loggable"); + unsetenv("RtaFacility_Codec"); +} + +LONGBOW_TEST_CASE(Local, _setLogLevels_ForwarderConnector) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + setenv("RtaFacility_Forwarder", "Warning", 1); + _setLogLevels(data->framework); + + assertFalse(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_ForwarderConnector, PARCLogLevel_Info), "Info should not be loggable"); + assertTrue(rtaLogger_IsLoggable(rtaFramework_GetLogger(data->framework), RtaLoggerFacility_ForwarderConnector, PARCLogLevel_Warning), "Warning should be loggable"); + unsetenv("RtaFacility_Forwarder"); +} + +// =================================================== + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Framework); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Commands.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Commands.c new file mode 100644 index 00000000..d19d680b --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Commands.c @@ -0,0 +1,449 @@ +/* + * 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. + */ + +/** + * Creates a bentpipe forwarder and then creates and runs in non-threaded Transport in + * the commonSetup() function. The function commonTeardown() undoes all that. + * + */ + +#include "../rta_Framework_Commands.c" +#include <sys/param.h> + +#include <LongBow/unit-test.h> + +#include <parc/security/parc_Pkcs12KeyStore.h> +#include <parc/security/parc_Security.h> + +#include <ccnx/api/control/cpi_ControlMessage.h> + +#include <parc/algol/parc_SafeMemory.h> + +#include <ccnx/transport/transport_rta/config/config_All.h> +#include <ccnx/transport/transport_rta/rta_Transport.h> +#include <ccnx/transport/common/transport_private.h> +#include <ccnx/transport/test_tools/traffic_tools.h> + +#include <ccnx/transport/test_tools/bent_pipe.h> + +// ============================================== +typedef struct test_data { + PARCRingBuffer1x1 *commandRingBuffer; + PARCNotifier *commandNotifier; + RtaFramework *framework; + + char bentpipe_Directory[MAXPATHLEN]; + char bentpipe_LocalName[MAXPATHLEN]; + BentPipeState *bentpipe; + char keystoreName[MAXPATHLEN]; + char keystorePassword[MAXPATHLEN]; +} TestData; + +static CCNxTransportConfig * +_createParams(const char *local_name, const char *keystore_name, const char *keystore_passwd) +{ + assertNotNull(local_name, "Got null local name\n"); + assertNotNull(keystore_name, "Got null keystore name\n"); + assertNotNull(keystore_passwd, "Got null keystore passwd\n"); + + CCNxStackConfig *stackConfig = apiConnector_ProtocolStackConfig( + tlvCodec_ProtocolStackConfig( + localForwarder_ProtocolStackConfig( + protocolStack_ComponentsConfigArgs(ccnxStackConfig_Create(), + apiConnector_GetName(), + tlvCodec_GetName(), + localForwarder_GetName(), + NULL)))); + + CCNxConnectionConfig *connConfig = apiConnector_ConnectionConfig( + localForwarder_ConnectionConfig(ccnxConnectionConfig_Create(), local_name)); + + connConfig = tlvCodec_ConnectionConfig(connConfig); + + publicKeySigner_ConnectionConfig(connConfig, keystore_name, keystore_passwd); + + CCNxTransportConfig *result = ccnxTransportConfig_Create(stackConfig, connConfig); + ccnxStackConfig_Release(&stackConfig); + return result; +} + +static void +_runNonThreaded(TestData *data) +{ + rtaFramework_NonThreadedStepTimed(data->framework, &((struct timeval) { 0, 100000 })); +} + +static void +_stopThreaded(TestData *data) +{ + printf("Beginning shutdown pid %d\n", getpid()); + // blocks until done + rtaFramework_Shutdown(data->framework); + printf("Finished shutdown pid %d\n", getpid()); +} + +static void +_stopNonThreaded(TestData *data) +{ + printf("Beginning shutdown pid %d\n", getpid()); + rtaFramework_Teardown(data->framework); + printf("Finished shutdown pid %d\n", getpid()); +} + +static TestData * +_commonSetup(void) +{ + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); + + snprintf(data->bentpipe_Directory, MAXPATHLEN, "/tmp/bentpipe_XXXXXX"); + char *p = mkdtemp(data->bentpipe_Directory); + assertNotNull(p, "Got null from mkdtemp(%s)", data->bentpipe_Directory); + snprintf(data->bentpipe_LocalName, MAXPATHLEN, "%s/bentpipe.sock", data->bentpipe_Directory); + + data->bentpipe = bentpipe_Create(data->bentpipe_LocalName); + bentpipe_SetChattyOutput(data->bentpipe, false); + + printf("Staring bent pipe pid %d\n", getpid()); + bentpipe_Start(data->bentpipe); + printf("Started bent pipe\n"); + + snprintf(data->keystoreName, MAXPATHLEN, "/tmp/keystore_p12_XXXXXX"); + int fd = mkstemp(data->keystoreName); + assertTrue(fd != -1, "Error from mkstemp(%s)", data->keystoreName); + + sprintf(data->keystorePassword, "23439429"); + + bool success = parcPkcs12KeyStore_CreateFile(data->keystoreName, data->keystorePassword, "user", 1024, 30); + assertTrue(success, "parcPublicKeySignerPkcs12Store_CreateFile() failed."); + close(fd); + + data->commandRingBuffer = parcRingBuffer1x1_Create(128, NULL); + data->commandNotifier = parcNotifier_Create(); + data->framework = rtaFramework_Create(data->commandRingBuffer, data->commandNotifier); + return data; +} + +static void +_commonTeardown(TestData *data) +{ + if (rtaFramework_GetStatus(data->framework) == FRAMEWORK_RUNNING) { + _stopThreaded(data); + } else { + _stopNonThreaded(data); + } + + parcRingBuffer1x1_Release(&data->commandRingBuffer); + parcNotifier_Release(&data->commandNotifier); + + printf("Destroying framework pid %d\n", getpid()); + rtaFramework_Destroy(&data->framework); + + bentpipe_Stop(data->bentpipe); + bentpipe_Destroy(&data->bentpipe); + unlink(data->keystoreName); + unlink(data->bentpipe_LocalName); + rmdir(data->bentpipe_Directory); + + parcMemory_Deallocate((void **) &data); +} + + +/** + * @function assertConnectionOpen + * @abstract Block on reading the 1st message out of the socket. It's the connection ready message. + * @discussion + * <#Discussion#> + * + * @param <#param1#> + * @return <#return#> + */ +static void +_assertConnectionOpen(int fd) +{ + CCNxMetaMessage *firstMessage; + + rtaTransport_Recv(NULL, fd, &firstMessage, CCNxStackTimeout_Never); + + assertTrue(ccnxMetaMessage_IsControl(firstMessage), "not a control message"); + + CCNxControl *control = ccnxMetaMessage_GetControl(firstMessage); + + NotifyStatus *status = notifyStatus_ParseJSON(ccnxControl_GetJson(control)); + ccnxMetaMessage_Release(&firstMessage); + + assertTrue(notifyStatus_IsConnectionOpen(status), "Expected notifyStatus_IsConnectionOpen to be true"); + notifyStatus_Release(&status); +} + + +/** + * @function openConnection + * @abstract Opens a connection and fills in the socket pair + * @discussion + * uses rtaFramework_ExecuteOpen to directly create, does not go over the command pair + * + * @param <#param1#> + * @return <#return#> + */ +static void +_openConnection(RtaFramework *framework, CCNxTransportConfig *transportConfig, int stack_id, int socketPairOutput[]) +{ + socketpair(PF_LOCAL, SOCK_STREAM, 0, socketPairOutput); + + struct timeval timeout = { .tv_sec = 10, .tv_usec = 0 }; + + setsockopt(socketPairOutput[0], SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); + setsockopt(socketPairOutput[0], SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + setsockopt(socketPairOutput[1], SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); + setsockopt(socketPairOutput[1], SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); + + RtaCommandOpenConnection *openConnection = rtaCommandOpenConnection_Create(stack_id, socketPairOutput[1], socketPairOutput[0], + ccnxConnectionConfig_GetJson(ccnxTransportConfig_GetConnectionConfig(transportConfig))); + + _rtaFramework_ExecuteOpenConnection(framework, openConnection); + rtaCommandOpenConnection_Release(&openConnection); + + rtaFramework_NonThreadedStepCount(framework, 10); + _assertConnectionOpen(socketPairOutput[1]); +} + +static bool +_readAndCompareName(int fd, CCNxName *truthName) +{ + CCNxMetaMessage *test_msg; + + int res = rtaTransport_Recv(NULL, fd, &test_msg, CCNxStackTimeout_Never); + assertTrue(res == 0, "Got error receiving on bob's socket: %s (%d)", strerror(errno), errno); + + assertNotNull(test_msg, "Got null message from Bob"); + + assertTrue(ccnxMetaMessage_IsInterest(test_msg), "Got wrong type, expected Interest but got other"); + + CCNxInterest *interest = ccnxMetaMessage_GetInterest(test_msg); + + assertTrue(ccnxName_Compare(truthName, ccnxInterest_GetName(interest)) == 0, "Names did not compare") + { + ccnxName_Display(ccnxInterest_GetName(interest), 3); + ccnxName_Display(truthName, 3); + } + + ccnxMetaMessage_Release(&test_msg); + + return true; +} + +// ========================== + +LONGBOW_TEST_RUNNER(rta_Framework_Commands) +{ + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Framework_Commands) +{ + printf("\n********\n%s starting\n\n", __func__); + + srandom((int) time(NULL)); + 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(rta_Framework_Commands) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Local) +{ + LONGBOW_RUN_TEST_CASE(Local, _rtaFramework_ExecuteCloseConnection); + LONGBOW_RUN_TEST_CASE(Local, _rtaFramework_ExecuteCreateStack); + LONGBOW_RUN_TEST_CASE(Local, _rtaFramework_ExecuteOpenConnection); +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + parcSecurity_Init(); + +#if __APPLE__ + pthread_setname_np(longBowTestCase_GetName(testCase)); +#else + pthread_setname_np(pthread_self(), longBowTestCase_GetName(testCase)); +#endif + + TestData *data = _commonSetup(); + _runNonThreaded(data); + + longBowTestCase_SetClipBoardData(testCase, data); + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + _commonTeardown(data); + parcSecurity_Fini(); + + 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(Local, _rtaFramework_ExecuteCloseConnection) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + int stack_id = 5; + + CCNxTransportConfig *params = _createParams(data->bentpipe_LocalName, data->keystoreName, data->keystorePassword); + + RtaCommandCreateProtocolStack *createStack = + rtaCommandCreateProtocolStack_Create(stack_id, ccnxTransportConfig_GetStackConfig(params)); + + _rtaFramework_ExecuteCreateStack(data->framework, createStack); + rtaCommandCreateProtocolStack_Release(&createStack); + + // now use three connections, then close 1 and make sure other 2 still ok + { + int alice_pair[2], bob_pair[2], charlie_pair[2]; + + _openConnection(data->framework, params, stack_id, alice_pair); + _openConnection(data->framework, params, stack_id, bob_pair); + _openConnection(data->framework, params, stack_id, charlie_pair); + + CCNxInterest *firstInterest = trafficTools_CreateInterest(); + + // send will consume the message, so copy out the name + CCNxName *truth_name = ccnxName_Copy(ccnxInterest_GetName(firstInterest)); + + CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(firstInterest); + bool success = rtaTransport_Send(NULL, alice_pair[1], message, CCNxStackTimeout_Never); + assertTrue(success, "Got error sending on alice's socket: %s (%d)", strerror(errno), errno); + ccnxMetaMessage_Release(&message); + + // *** Read bob + rtaFramework_NonThreadedStepCount(data->framework, 10); + _readAndCompareName(bob_pair[1], truth_name); + + // *** Read Charlie + rtaFramework_NonThreadedStepCount(data->framework, 10); + _readAndCompareName(charlie_pair[1], truth_name); + + // Close charlie and make sure alice + bob still happy + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(charlie_pair[1]); + _rtaFramework_ExecuteCloseConnection(data->framework, closeConnection); + rtaCommandCloseConnection_Release(&closeConnection); + rtaFramework_NonThreadedStepCount(data->framework, 10); + + // send another interest + CCNxInterest *secondInterest = trafficTools_CreateInterest(); + message = ccnxMetaMessage_CreateFromInterest(secondInterest); + + success = rtaTransport_Send(NULL, alice_pair[1], message, CCNxStackTimeout_Never); + assertTrue(success, "Got error sending on alice's socket: %s (%d)", strerror(errno), errno); + ccnxMetaMessage_Release(&message); + + // make sure bob gets it + rtaFramework_NonThreadedStepCount(data->framework, 10); + _readAndCompareName(bob_pair[1], truth_name); + + ccnxName_Release(&truth_name); + ccnxInterest_Release(&firstInterest); + ccnxInterest_Release(&secondInterest); + } + + ccnxTransportConfig_Destroy(¶ms); +} + +LONGBOW_TEST_CASE(Local, _rtaFramework_ExecuteCreateStack) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + int stack_id = 4; + CCNxTransportConfig *params = _createParams(data->bentpipe_LocalName, data->keystoreName, data->keystorePassword); + RtaCommandCreateProtocolStack *createStack = + rtaCommandCreateProtocolStack_Create(stack_id, ccnxTransportConfig_GetStackConfig(params)); + + + // this call skirts around threading + _rtaFramework_ExecuteCreateStack(data->framework, createStack); + + FrameworkProtocolHolder *holder; + holder = rtaFramework_GetProtocolStackByStackId(data->framework, stack_id); + assertNotNull(holder, "There is no protocol holder for this stack, not created?"); + + ccnxTransportConfig_Destroy(¶ms); + rtaCommandCreateProtocolStack_Release(&createStack); +} + +LONGBOW_TEST_CASE(Local, _rtaFramework_ExecuteOpenConnection) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + int stack_id = 4; + CCNxTransportConfig *params = _createParams(data->bentpipe_LocalName, data->keystoreName, data->keystorePassword); + + RtaCommandCreateProtocolStack *createStack = + rtaCommandCreateProtocolStack_Create(stack_id, ccnxTransportConfig_GetStackConfig(params)); + _rtaFramework_ExecuteCreateStack(data->framework, createStack); + rtaCommandCreateProtocolStack_Release(&createStack); + + // now create two connections and make sure they work + { + // now create + int alice_pair[2], bob_pair[2]; + socketpair(PF_LOCAL, SOCK_STREAM, 0, alice_pair); + socketpair(PF_LOCAL, SOCK_STREAM, 0, bob_pair); + + _openConnection(data->framework, params, stack_id, alice_pair); + _openConnection(data->framework, params, stack_id, bob_pair); + + CCNxInterest *interest = trafficTools_CreateInterest(); + + //ccnxInterest_Display(interest, 0); + + // send will consume the message, so copy out the name + CCNxName *truth_name = ccnxName_Copy(ccnxInterest_GetName(interest)); + + //ccnxName_Display(truth_name, 0); + + // now send it down the stack + CCNxMetaMessage *message = ccnxMetaMessage_CreateFromInterest(interest); + bool success = rtaTransport_Send(NULL, alice_pair[1], message, CCNxStackTimeout_Never); + assertTrue(success, "Got error sending on alice's socket: %s (%d)", strerror(errno), errno); + ccnxMetaMessage_Release(&message); + + rtaFramework_NonThreadedStepCount(data->framework, 10); + _readAndCompareName(bob_pair[1], truth_name); + + ccnxName_Release(&truth_name); + ccnxInterest_Release(&interest); + } + + ccnxTransportConfig_Destroy(¶ms); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Framework_Commands); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_NonThreaded.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_NonThreaded.c new file mode 100644 index 00000000..fb73e15c --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_NonThreaded.c @@ -0,0 +1,83 @@ +/* + * 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 "../rta_Framework_NonThreaded.c" +#include <parc/algol/parc_SafeMemory.h> + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(rta_Framework_NonThreaded) +{ + // 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); + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Framework_NonThreaded) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_Framework_NonThreaded) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ +} + +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_FIXTURE(Local) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Framework_NonThreaded); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Services.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Services.c new file mode 100644 index 00000000..6bb51f0f --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Services.c @@ -0,0 +1,84 @@ +/* + * 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 "../rta_Framework_Services.c" +#include <parc/algol/parc_SafeMemory.h> + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(rta_Framework_Services) +{ + // 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); + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Framework_Services) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_Framework_Services) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ +} + +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_FIXTURE(Local) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Framework_Services); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Threaded.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Threaded.c new file mode 100644 index 00000000..74ea2f64 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Framework_Threaded.c @@ -0,0 +1,83 @@ +/* + * 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 "../rta_Framework_Threaded.c" +#include <parc/algol/parc_SafeMemory.h> + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(rta_Framework_Threaded) +{ + // 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); + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Framework_Threaded) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_Framework_Threaded) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ +} + +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_FIXTURE(Local) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Framework_Threaded); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Logger.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Logger.c new file mode 100644 index 00000000..240293fc --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_Logger.c @@ -0,0 +1,222 @@ +/* + * 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 "../rta_Logger.c" +#include <stdio.h> +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> + +LONGBOW_TEST_RUNNER(rta_Logger) +{ + LONGBOW_RUN_TEST_FIXTURE(Global); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_Logger) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_Logger) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// ========================================================== + +/* + * _testWritter will vsprintf to this buffer + */ +#define _logLength 1024 +static char _lastLogMessage[_logLength]; + +static int +_testWriter(const char *message) +{ + int written = 0; + written = snprintf(_lastLogMessage, _logLength, "%s", message); + return written; +} + +static PARCLogReporter * +_testWriter_Acquire(const PARCLogReporter *reporter) +{ + return parcObject_Acquire(reporter); +} + +static void +_testWriter_Release(PARCLogReporter **reporterPtr) +{ + parcObject_Release((void **) reporterPtr); +} + +static void +_testWriter_Report(PARCLogReporter *reporter, const PARCLogEntry *entry) +{ + char *string = parcLogEntry_ToString(entry); + _testWriter(string); + parcMemory_Deallocate((void **) &string); +} + +static PARCLogReporter * +_testWriter_Create(void) +{ + return parcLogReporter_Create(_testWriter_Acquire, _testWriter_Release, _testWriter_Report, NULL); +} + +// ========================================================== + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_FacilityString_Found); + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_FacilityString_NotFound); + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_Create); + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_Acquire); + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_SetLogLevel); + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_IsLoggable_True); + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_IsLoggable_False); + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_Log_IsLoggable); + LONGBOW_RUN_TEST_CASE(Global, rtaLogger_Log_IsNotLoggable); +} + +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, rtaLogger_FacilityString_Found) +{ + for (RtaLoggerFacility i = 0; i < RtaLoggerFacility_END; i++) { + const char *test = rtaLogger_FacilityString(i); + assertNotNull(test, "Got null string for facility %d", i); + } +} + +LONGBOW_TEST_CASE(Global, rtaLogger_FacilityString_NotFound) +{ + const char *test = rtaLogger_FacilityString(1000); + assertTrue(strcmp(test, "Unknown") == 0, "Got wrong string for unknown facility"); +} + +LONGBOW_TEST_CASE(Global, rtaLogger_Create) +{ + PARCLogReporter *reporter = _testWriter_Create(); + RtaLogger *logger = rtaLogger_Create(reporter, parcClock_Wallclock()); + parcLogReporter_Release(&reporter); + + rtaLogger_Release(&logger); +} + +LONGBOW_TEST_CASE(Global, rtaLogger_Acquire) +{ + PARCLogReporter *reporter = _testWriter_Create(); + RtaLogger *logger = rtaLogger_Create(reporter, parcClock_Wallclock()); + parcLogReporter_Release(&reporter); + + RtaLogger *copy = rtaLogger_Acquire(logger); + rtaLogger_Release(&logger); + rtaLogger_Release(©); +} + +LONGBOW_TEST_CASE(Global, rtaLogger_SetLogLevel) +{ + PARCLogReporter *reporter = _testWriter_Create(); + RtaLogger *logger = rtaLogger_Create(reporter, parcClock_Wallclock()); + parcLogReporter_Release(&reporter); + + rtaLogger_SetLogLevel(logger, RtaLoggerFacility_Framework, PARCLogLevel_Off); + + PARCLogLevel test = parcLog_GetLevel(logger->loggerArray[RtaLoggerFacility_Framework]); + assertTrue(test == PARCLogLevel_Off, "wrong log level, expected %d got %d", PARCLogLevel_Off, test); + rtaLogger_Release(&logger); +} + +LONGBOW_TEST_CASE(Global, rtaLogger_IsLoggable_True) +{ + PARCLogReporter *reporter = _testWriter_Create(); + RtaLogger *logger = rtaLogger_Create(reporter, parcClock_Wallclock()); + parcLogReporter_Release(&reporter); + + rtaLogger_SetLogLevel(logger, RtaLoggerFacility_Framework, PARCLogLevel_Warning); + bool isLoggable = rtaLogger_IsLoggable(logger, RtaLoggerFacility_Framework, PARCLogLevel_Warning); + assertTrue(isLoggable, "Did not get true for isLoggable when expecting true"); + rtaLogger_Release(&logger); +} + +LONGBOW_TEST_CASE(Global, rtaLogger_IsLoggable_False) +{ + PARCLogReporter *reporter = _testWriter_Create(); + RtaLogger *logger = rtaLogger_Create(reporter, parcClock_Wallclock()); + parcLogReporter_Release(&reporter); + + rtaLogger_SetLogLevel(logger, RtaLoggerFacility_Framework, PARCLogLevel_Warning); + bool isLoggable = rtaLogger_IsLoggable(logger, RtaLoggerFacility_Framework, PARCLogLevel_Debug); + assertFalse(isLoggable, "Logging debug to warning facility should have been false"); + rtaLogger_Release(&logger); +} + +LONGBOW_TEST_CASE(Global, rtaLogger_Log_IsLoggable) +{ + PARCLogReporter *reporter = _testWriter_Create(); + RtaLogger *logger = rtaLogger_Create(reporter, parcClock_Wallclock()); + parcLogReporter_Release(&reporter); + + rtaLogger_SetLogLevel(logger, RtaLoggerFacility_Framework, PARCLogLevel_Warning); + memset(_lastLogMessage, 0, _logLength); + + rtaLogger_Log(logger, RtaLoggerFacility_Framework, PARCLogLevel_Warning, __func__, "hello"); + assertTrue(strlen(_lastLogMessage) > 0, "Did not write to log message"); + rtaLogger_Release(&logger); +} + +LONGBOW_TEST_CASE(Global, rtaLogger_Log_IsNotLoggable) +{ + PARCLogReporter *reporter = _testWriter_Create(); + RtaLogger *logger = rtaLogger_Create(reporter, parcClock_Wallclock()); + parcLogReporter_Release(&reporter); + + rtaLogger_SetLogLevel(logger, RtaLoggerFacility_Framework, PARCLogLevel_Warning); + memset(_lastLogMessage, 0, _logLength); + + rtaLogger_Log(logger, RtaLoggerFacility_Framework, PARCLogLevel_Debug, __func__, "hello"); + assertTrue(strlen(_lastLogMessage) == 0, "Should not have written to log message"); + rtaLogger_Release(&logger); +} + + +// ========================================================== + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Logger); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} + diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ProtocolStack.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ProtocolStack.c new file mode 100644 index 00000000..c14d799a --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_ProtocolStack.c @@ -0,0 +1,81 @@ +/* + * 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 <stdio.h> + +#include <parc/algol/parc_SafeMemory.h> + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(rta_ProtocolStack) +{ + LONGBOW_RUN_TEST_FIXTURE(Global); + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_ProtocolStack) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_ProtocolStack) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ +} + +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_FIXTURE(Local) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_ProtocolStack); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_WebService.c b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_WebService.c new file mode 100644 index 00000000..4b2fb015 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/core/test/test_rta_WebService.c @@ -0,0 +1,301 @@ +/* + * 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 "../rta_WebService.c" +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> + +#include <signal.h> +#include <pthread.h> +#include <errno.h> +#include <arpa/inet.h> + +LONGBOW_TEST_RUNNER(rta_WebService) +{ + // 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); + LONGBOW_RUN_TEST_FIXTURE(Local); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(rta_WebService) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_WebService) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaWebService_Create_Destroy); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + if (parcSafeMemory_ReportAllocation(STDOUT_FILENO) != 0) { + printf("('%s' leaks memory by %d (allocs - frees)) ", longBowTestCase_GetName(testCase), parcMemory_Outstanding()); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +struct sigaction save_sigchld; +struct sigaction save_sigpipe; + +static void +blockSigChild() +{ + struct sigaction ignore_action; + ignore_action.sa_handler = SIG_IGN; + sigemptyset(&ignore_action.sa_mask); + ignore_action.sa_flags = 0; + + sigaction(SIGCHLD, NULL, &save_sigchld); + sigaction(SIGPIPE, NULL, &save_sigpipe); + + sigaction(SIGCHLD, &ignore_action, NULL); + sigaction(SIGPIPE, &ignore_action, NULL); +} + +static void +unblockSigChild() +{ + sigaction(SIGCHLD, &save_sigchld, NULL); + sigaction(SIGPIPE, &save_sigpipe, NULL); +} + +LONGBOW_TEST_CASE(Global, rtaWebService_Create_Destroy) +{ + int fds[2]; + int failure = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds); + assertFalse(failure, "error on socketpair: (%d) %s", errno, strerror(errno)); + + RtaFramework *framework = rtaFramework_Create(fds[1]); + + // we should be runing on port 9090, so the string popen() gets + // will look like this: + // tcp4 0 0 127.0.0.1.9090 *.* LISTEN + + blockSigChild(); + FILE *fp = popen("netstat -an -p tcp", "r"); + assertNotNull(fp, "Got null opening netstat for reading"); + + char str[1035]; + bool found = false; + while (fgets(str, sizeof(str) - 1, fp) != NULL) { + if (strstr(str, "127.0.0.1.9090") != NULL) { + found = true; + break; + } + + if (strstr(str, "127.0.0.1:9090") != NULL) { + found = true; + break; + } + } + + pclose(fp); + + rtaFramework_Destroy(&framework); + + close(fds[0]); + close(fds[1]); + unblockSigChild(); + + assertTrue(found, "Did not find 127.0.0.1.9090 in netstat output"); +} + + +LONGBOW_TEST_FIXTURE(Local) +{ + LONGBOW_RUN_TEST_CASE(Local, rtaWebService_ProcessHelloRequest); + LONGBOW_RUN_TEST_CASE(Local, rtaWebService_ProcessRequest); +} + +LONGBOW_TEST_FIXTURE_SETUP(Local) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Local) +{ + if (parcSafeMemory_ReportAllocation(STDOUT_FILENO) != 0) { + printf("('%s' leaks memory by %d (allocs - frees)) ", longBowTestCase_GetName(testCase), parcMemory_Outstanding()); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Local, rtaWebService_ProcessHelloRequest) +{ +#ifndef __APPLE__ + testSkip("Test broken on non-darwin"); +#endif + + blockSigChild(); + int fds[2]; + int failure = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds); + assertFalse(failure, "error on socketpair: (%d) %s", errno, strerror(errno)); + + RtaFramework *framework = rtaFramework_Create(fds[0]); + rtaFramework_Start(framework); + rtaFramework_WaitForStatus(framework, FRAMEWORK_RUNNING); + + int fd = socket(AF_INET, SOCK_STREAM, 0); + + struct sockaddr_in sin; + sin.sin_addr.s_addr = inet_addr("127.0.0.1"); + sin.sin_port = htons(9090); + + failure = connect(fd, (struct sockaddr *) &sin, sizeof(sin)); + assertFalse(failure, "error on connect: (%d) %s", errno, strerror(errno)); + + char request[] = "GET /hello HTTP/1.1\r\n\r\n"; + ssize_t write_length = write(fd, request, sizeof(request)); + assertFalse(write_length < 0, "Error writing: (%d) %s", errno, strerror(errno)); + + + struct truth_s { + char *line; + } truth[] = { + { .line = "HTTP/1.1 200 OK\r\n" }, + { .line = "" }, // do not care line for Date + { .line = "Content-Length: 18\r\n" }, + { .line = "Content-Type: text/html; charset=ISO-8859-1\r\n" }, + { .line = "\r\n" }, + { .line = "Requested: /hello\n" }, + { .line = NULL } + }; + + // read response line by line + FILE *fh = fdopen(fd, "r"); + int count = 0; + while (!feof(fh) && truth[count].line != NULL) { + assertNotNull(truth[count].line, "read too many lines: %d", count); + + char response[16384]; + fgets(response, sizeof(response), fh); + if (truth[count].line[0] != '\0') { + bool result = strcmp(truth[count].line, response) == 0; + + if (!result) { + // we need to cleanup the server or the next test will fail + rtaFramework_Shutdown(framework, fds[1]); + rtaFramework_Destroy(&framework); + close(fds[0]); + close(fds[1]); + unblockSigChild(); + assertTrue(result, "mismatched lines, expected '%s' got '%s'", truth[count].line, response); + } + } + count++; + } + fclose(fh); + + rtaFramework_Shutdown(framework, fds[1]); + rtaFramework_Destroy(&framework); + close(fds[0]); + close(fds[1]); + + unblockSigChild(); +} + +LONGBOW_TEST_CASE(Local, rtaWebService_ProcessRequest) +{ +#ifndef __APPLE__ + testSkip("Test broken on non-darwin"); +#endif + + blockSigChild(); + int fds[2]; + int failure = socketpair(AF_LOCAL, SOCK_STREAM, 0, fds); + assertFalse(failure, "error on socketpair: (%d) %s", errno, strerror(errno)); + + RtaFramework *framework = rtaFramework_Create(fds[0]); + rtaFramework_Start(framework); + rtaFramework_WaitForStatus(framework, FRAMEWORK_RUNNING); + + int fd = socket(AF_INET, SOCK_STREAM, 0); + + struct sockaddr_in sin; + sin.sin_addr.s_addr = inet_addr("127.0.0.1"); + sin.sin_port = htons(9090); + + failure = connect(fd, (struct sockaddr *) &sin, sizeof(sin)); + assertFalse(failure, "error on connect: (%d) %s", errno, strerror(errno)); + + char request[] = "GET /foo HTTP/1.1\r\n\r\n"; + write(fd, request, sizeof(request)); + + struct truth_s { + char *line; + } truth[] = { + { .line = "HTTP/1.1 404 Document was not found\r\n" }, + { .line = "Content-Type: text/html\r\n" }, + { .line = "Connection: close\r\n" }, + { .line = "" }, // do not care line for Date + { .line = "Content-Length: 116\r\n" }, + { .line = "\r\n" }, + { .line = "<HTML><HEAD>\n" }, + { .line = "<TITLE>404 Document was not found</TITLE>\n" }, + { .line = "</HEAD><BODY>\n" }, + { .line = "<H1>Document was not found</H1>\n" }, + { .line = "</BODY></HTML>\n" }, + { .line = NULL } + }; + + // read response line by line + FILE *fh = fdopen(fd, "r"); + int count = 0; + while (!feof(fh) && truth[count].line != NULL) { + assertNotNull(truth[count].line, "read too many lines: %d", count); + + char response[16384]; + fgets(response, sizeof(response), fh); + if (truth[count].line[0] != '\0') { + assertTrue(strcmp(truth[count].line, response) == 0, "mismatched lines, expected '%s' got '%s'", truth[count].line, response); + } + count++; + } + fclose(fh); + + rtaFramework_Shutdown(framework, fds[1]); + rtaFramework_Destroy(&framework); + close(fds[0]); + close(fds[1]); + unblockSigChild(); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_WebService); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} |