diff options
Diffstat (limited to 'libccnx-transport-rta/ccnx/transport/transport_rta/commands/test')
8 files changed, 1334 insertions, 0 deletions
diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/.gitignore b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/.gitignore new file mode 100644 index 00000000..73fd1137 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/.gitignore @@ -0,0 +1,7 @@ +test_rta_Command +test_rta_CommandCloseConnection +test_rta_CommandCreateProtocolStack +test_rta_CommandDestroyProtocolStack +test_rta_CommandOpenConnection +test_rta_CommandTransmitStatistics + diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/CMakeLists.txt b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/CMakeLists.txt new file mode 100644 index 00000000..d3a5f009 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/CMakeLists.txt @@ -0,0 +1,18 @@ +# Enable gcov output for the tests +add_definitions(--coverage) +set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} " --coverage") + +set(TestsExpectedToPass + test_rta_Command + test_rta_CommandCreateProtocolStack + test_rta_CommandOpenConnection + test_rta_CommandCloseConnection + test_rta_CommandDestroyProtocolStack + test_rta_CommandTransmitStatistics +) + + +foreach(test ${TestsExpectedToPass}) + AddTest(${test}) +endforeach() + diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_Command.c b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_Command.c new file mode 100644 index 00000000..c27cb41d --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_Command.c @@ -0,0 +1,475 @@ +/* + * 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_Command.c" + +#include <inttypes.h> +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> + +LONGBOW_TEST_RUNNER(rta_Command) +{ + // 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_Command) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_Command) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_Acquire); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_Release); + + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_CreateShutdownFramework); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_CreateCloseConnection); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_CreateCreateProtocolStack); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_CreateDestroyProtocolStack); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_CreateOpenConnection); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_CreateTransmitStatistics); + + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_GetCloseConnection); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_GetCreateProtocolStack); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_GetDestroyProtocolStack); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_GetOpenConnection); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_GetTransmitStatistics); + + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsCloseConnection_True); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsCreateProtocolStack_True); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsDestroyProtocolStack_True); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsOpenConnection_True); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsShutdownFramework_True); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsTransmitStatistics_True); + + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsCloseConnection_False); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsCreateProtocolStack_False); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsDestroyProtocolStack_False); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsOpenConnection_False); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsShutdownFramework_False); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_IsTransmitStatistics_False); + + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_Read_Single); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_Write_Single); + + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_Read_Underflow); + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_Write_Overflow); + + // miscellaneous functions + LONGBOW_RUN_TEST_CASE(Global, rtaCommand_Display); +} + +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, rtaCommand_Acquire) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + size_t firstRefCount = parcObject_GetReferenceCount(command); + + RtaCommand *second = rtaCommand_Acquire(command); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + assertTrue(secondRefCount == firstRefCount + 1, "Wrong refcount after acquire, got %zu expected %zu", secondRefCount, firstRefCount + 1); + + rtaCommand_Release(&command); + rtaCommand_Release(&second); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_Release) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + + RtaCommand *second = rtaCommand_Acquire(command); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + rtaCommand_Release(&command); + size_t thirdRefCount = parcObject_GetReferenceCount(second); + + assertTrue(thirdRefCount == secondRefCount - 1, "Wrong refcount after release, got %zu expected %zu", thirdRefCount, secondRefCount - 1); + + rtaCommand_Release(&second); +} + +// ======================= +// Create/From operations + +LONGBOW_TEST_CASE(Global, rtaCommand_CreateShutdownFramework) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + assertNotNull(command, "Got null command from create"); + assertTrue(command->type == RtaCommandType_ShutdownFramework, "Command is not shutdown framework"); + rtaCommand_Release(&command); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_CreateCloseConnection) +{ + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(77); + RtaCommand *command = rtaCommand_CreateCloseConnection(closeConnection); + assertNotNull(command, "Got null command from create"); + assertTrue(command->type == RtaCommandType_CloseConnection, "Command is not CloseConnection"); + rtaCommand_Release(&command); + rtaCommandCloseConnection_Release(&closeConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_CreateCreateProtocolStack) +{ + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(111, config); + + RtaCommand *command = rtaCommand_CreateCreateProtocolStack(createStack); + assertNotNull(command, "Got null command from create"); + assertTrue(command->type == RtaCommandType_CreateProtocolStack, "Command is not CreateProtocolStack"); + + rtaCommand_Release(&command); + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_CreateDestroyProtocolStack) +{ + RtaCommandDestroyProtocolStack *destroyStack = rtaCommandDestroyProtocolStack_Create(77); + RtaCommand *command = rtaCommand_CreateDestroyProtocolStack(destroyStack); + assertNotNull(command, "Got null command from create"); + assertTrue(command->type == RtaCommandType_DestroyProtocolStack, "Command is not DestroyProtocolStack"); + rtaCommand_Release(&command); + rtaCommandDestroyProtocolStack_Release(&destroyStack); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_CreateOpenConnection) +{ + RtaCommandOpenConnection *openConnection = rtaCommandOpenConnection_Create(111, 2341, 2450987, NULL); + RtaCommand *command = rtaCommand_CreateOpenConnection(openConnection); + assertNotNull(command, "Got null command from create"); + assertTrue(command->type == RtaCommandType_OpenConnection, "Command is not OpenConnection"); + rtaCommand_Release(&command); + rtaCommandOpenConnection_Release(&openConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_CreateTransmitStatistics) +{ + RtaCommandTransmitStatistics *transmitStats = rtaCommandTransmitStatistics_Create((struct timeval) { 1, 2 }, "filename"); + RtaCommand *command = rtaCommand_CreateTransmitStatistics(transmitStats); + assertNotNull(command, "Got null command from create"); + assertTrue(command->type == RtaCommandType_TransmitStatistics, "Command is not TransmitStatistics"); + rtaCommand_Release(&command); + rtaCommandTransmitStatistics_Release(&transmitStats); +} + +// ======================= +// GET operations + +LONGBOW_TEST_CASE(Global, rtaCommand_GetCloseConnection) +{ + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(77); + RtaCommand *command = rtaCommand_CreateCloseConnection(closeConnection); + + const RtaCommandCloseConnection *test = rtaCommand_GetCloseConnection(command); + assertTrue(test == closeConnection, "Wrong pointers, got %p expected %p", (void *) test, (void *) closeConnection); + + rtaCommand_Release(&command); + rtaCommandCloseConnection_Release(&closeConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_GetCreateProtocolStack) +{ + CCNxStackConfig *config = ccnxStackConfig_Create(); + + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(111, config); + RtaCommand *command = rtaCommand_CreateCreateProtocolStack(createStack); + + const RtaCommandCreateProtocolStack *test = rtaCommand_GetCreateProtocolStack(command); + assertTrue(test == createStack, "Wrong pointers, got %p expected %p", (void *) test, (void *) createStack); + + rtaCommand_Release(&command); + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_GetDestroyProtocolStack) +{ + RtaCommandDestroyProtocolStack *destroyStack = rtaCommandDestroyProtocolStack_Create(77); + RtaCommand *command = rtaCommand_CreateDestroyProtocolStack(destroyStack); + + const RtaCommandDestroyProtocolStack *test = rtaCommand_GetDestroyProtocolStack(command); + assertTrue(test == destroyStack, "Wrong pointers, got %p expected %p", (void *) test, (void *) destroyStack); + + rtaCommand_Release(&command); + rtaCommandDestroyProtocolStack_Release(&destroyStack); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_GetOpenConnection) +{ + RtaCommandOpenConnection *openConnection = rtaCommandOpenConnection_Create(111, 2341, 2450987, NULL); + RtaCommand *command = rtaCommand_CreateOpenConnection(openConnection); + + const RtaCommandOpenConnection *test = rtaCommand_GetOpenConnection(command); + assertTrue(test == openConnection, "Wrong pointers, got %p expected %p", (void *) test, (void *) openConnection); + + rtaCommand_Release(&command); + rtaCommandOpenConnection_Release(&openConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_GetTransmitStatistics) +{ + RtaCommandTransmitStatistics *transmitStats = rtaCommandTransmitStatistics_Create((struct timeval) { 1, 2 }, "filename"); + RtaCommand *command = rtaCommand_CreateTransmitStatistics(transmitStats); + + const RtaCommandTransmitStatistics *test = rtaCommand_GetTransmitStatistics(command); + assertTrue(test == transmitStats, "Wrong pointers, got %p expected %p", (void *) test, (void *) transmitStats); + + rtaCommand_Release(&command); + rtaCommandTransmitStatistics_Release(&transmitStats); +} + +// ======================= +// IsX operations + +LONGBOW_TEST_CASE(Global, rtaCommand_IsCloseConnection_True) +{ + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(77); + RtaCommand *command = rtaCommand_CreateCloseConnection(closeConnection); + assertTrue(rtaCommand_IsCloseConnection(command), "Command is not CloseConnection"); + rtaCommand_Release(&command); + rtaCommandCloseConnection_Release(&closeConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsCreateProtocolStack_True) +{ + CCNxStackConfig *config = ccnxStackConfig_Create(); + + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(111, config); + RtaCommand *command = rtaCommand_CreateCreateProtocolStack(createStack); + assertTrue(rtaCommand_IsCreateProtocolStack(command), "Command is not CreateProtocolStack"); + rtaCommand_Release(&command); + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsDestroyProtocolStack_True) +{ + RtaCommandDestroyProtocolStack *destroyStack = rtaCommandDestroyProtocolStack_Create(77); + RtaCommand *command = rtaCommand_CreateDestroyProtocolStack(destroyStack); + assertTrue(rtaCommand_IsDestroyProtocolStack(command), "Command is not DestroyProtocolStack"); + rtaCommand_Release(&command); + rtaCommandDestroyProtocolStack_Release(&destroyStack); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsOpenConnection_True) +{ + RtaCommandOpenConnection *openConnection = rtaCommandOpenConnection_Create(111, 2341, 2450987, NULL); + RtaCommand *command = rtaCommand_CreateOpenConnection(openConnection); + assertTrue(rtaCommand_IsOpenConnection(command), "Command is not OpenConnection"); + rtaCommand_Release(&command); + rtaCommandOpenConnection_Release(&openConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsShutdownFramework_True) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + assertTrue(rtaCommand_IsShutdownFramework(command), "Command is not shutdown framework"); + rtaCommand_Release(&command); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsTransmitStatistics_True) +{ + RtaCommandTransmitStatistics *transmitStats = rtaCommandTransmitStatistics_Create((struct timeval) { 1, 2 }, "filename"); + RtaCommand *command = rtaCommand_CreateTransmitStatistics(transmitStats); + assertTrue(rtaCommand_IsTransmitStatistics(command), "Command is not TransmitStatistics"); + rtaCommand_Release(&command); + rtaCommandTransmitStatistics_Release(&transmitStats); +} + + +LONGBOW_TEST_CASE(Global, rtaCommand_IsCloseConnection_False) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + assertFalse(rtaCommand_IsCloseConnection(command), "Command is not CloseConnection, should be false"); + rtaCommand_Release(&command); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsCreateProtocolStack_False) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + assertFalse(rtaCommand_IsCreateProtocolStack(command), "Command is not CreateProtocolStack, should be false"); + rtaCommand_Release(&command); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsDestroyProtocolStack_False) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + assertFalse(rtaCommand_IsDestroyProtocolStack(command), "Command is not DestroyProtocolStack, should be false"); + rtaCommand_Release(&command); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsOpenConnection_False) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + assertFalse(rtaCommand_IsOpenConnection(command), "Command is not OpenConnection, should be false"); + rtaCommand_Release(&command); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsShutdownFramework_False) +{ + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(77); + RtaCommand *command = rtaCommand_CreateCloseConnection(closeConnection); + assertFalse(rtaCommand_IsShutdownFramework(command), "Command is not ShutdownFramework, should be false"); + rtaCommand_Release(&command); + rtaCommandCloseConnection_Release(&closeConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_IsTransmitStatistics_False) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + assertFalse(rtaCommand_IsTransmitStatistics(command), "Command is not TransmitStatistics, should be false"); + rtaCommand_Release(&command); +} + +// =========================== +// IO operations + +/* + * Read a single command from a ring buffer + */ +LONGBOW_TEST_CASE(Global, rtaCommand_Read_Single) +{ + PARCRingBuffer1x1 *ring = parcRingBuffer1x1_Create(4, NULL); + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + + bool success = parcRingBuffer1x1_Put(ring, command); + assertTrue(success, "Failed to put command in to ring buffer"); + + RtaCommand *test = rtaCommand_Read(ring); + assertTrue(test == command, "Wrong pointers, got %p expected %p", (void *) test, (void *) command); + + rtaCommand_Release(&command); + parcRingBuffer1x1_Release(&ring); +} + +/* + * Write a single command to a ring buffer and make sure it works + */ +LONGBOW_TEST_CASE(Global, rtaCommand_Write_Single) +{ + PARCRingBuffer1x1 *ring = parcRingBuffer1x1_Create(4, NULL); + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + + bool success = rtaCommand_Write(command, ring); + assertTrue(success, "Failed to put command in to ring buffer"); + + // We should now have two references + assertTrue(parcObject_GetReferenceCount(command) == 2, "Wrong refernce count, got %" PRIu64 " expected %u", parcObject_GetReferenceCount(command), 2); + + RtaCommand *test = rtaCommand_Read(ring); + assertTrue(test == command, "Wrong pointers, got %p expected %p", (void *) test, (void *) command); + + rtaCommand_Release(&command); + rtaCommand_Release(&test); + parcRingBuffer1x1_Release(&ring); +} + +/* + * Read from an empty ring buffer + */ +LONGBOW_TEST_CASE(Global, rtaCommand_Read_Underflow) +{ + PARCRingBuffer1x1 *ring = parcRingBuffer1x1_Create(4, NULL); + + RtaCommand *test = rtaCommand_Read(ring); + assertNull(test, "Should have gotten NULL read from an empty ring buffer"); + + parcRingBuffer1x1_Release(&ring); +} + +/* + * Write beyond the capacity of the ring buffer + */ +LONGBOW_TEST_CASE(Global, rtaCommand_Write_Overflow) +{ + // The ring will store up to (ringSize-1) elements, so we can only + // have 3 items in a ring of size 4 + unsigned ringSize = 4; + RtaCommand *commandArray[ringSize]; + + PARCRingBuffer1x1 *ring = parcRingBuffer1x1_Create(ringSize, NULL); + + for (int i = 0; i < ringSize; i++) { + commandArray[i] = rtaCommand_CreateShutdownFramework(); + } + + for (int i = 0; i < ringSize - 1; i++) { + bool success = rtaCommand_Write(commandArray[i], ring); + assertTrue(success, "Failed to put command in to ring buffer"); + } + + // now put the one that will not fit + bool shouldFail = rtaCommand_Write(commandArray[ringSize - 1], ring); + assertFalse(shouldFail, "Writing overflow item should have failed"); + + // now make sure we read off all the right items + for (int i = 0; i < ringSize - 1; i++) { + RtaCommand *test = rtaCommand_Read(ring); + assertTrue(test == commandArray[i], "Wrong pointers, got %p expected %p", (void *) test, (void *) commandArray[i]); + rtaCommand_Release(&test); + } + + for (int i = 0; i < ringSize; i++) { + rtaCommand_Release(&commandArray[i]); + } + parcRingBuffer1x1_Release(&ring); +} + +LONGBOW_TEST_CASE(Global, rtaCommand_Display) +{ + RtaCommand *command = rtaCommand_CreateShutdownFramework(); + rtaCommand_Display(command, 3); + rtaCommand_Release(&command); +} + +// =================================================================== + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_Command); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandCloseConnection.c b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandCloseConnection.c new file mode 100644 index 00000000..f6c0a3a3 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandCloseConnection.c @@ -0,0 +1,125 @@ +/* + * 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_CommandCloseConnection.c" + +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> + +LONGBOW_TEST_RUNNER(rta_CommandCloseConnection) +{ + // 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_CommandCloseConnection) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_CommandCloseConnection) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCloseConnection_Acquire); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCloseConnection_Create); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCloseConnection_GetApiNotifierFd); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCloseConnection_Release); +} + +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, rtaCommandCloseConnection_Acquire) +{ + int apiNotifierFd = 7; + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(apiNotifierFd); + size_t firstRefCount = parcObject_GetReferenceCount(closeConnection); + + RtaCommandCloseConnection *second = rtaCommandCloseConnection_Acquire(closeConnection); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + assertTrue(secondRefCount == firstRefCount + 1, "Wrong refcount after acquire, got %zu expected %zu", secondRefCount, firstRefCount + 1); + + rtaCommandCloseConnection_Release(&closeConnection); + rtaCommandCloseConnection_Release(&second); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCloseConnection_Create) +{ + int apiNotifierFd = 7; + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(apiNotifierFd); + assertNotNull(closeConnection, "Got null from create"); + assertTrue(closeConnection->apiNotifierFd == apiNotifierFd, "Internal apiSocket wrong, got %d expected %d", closeConnection->apiNotifierFd, apiNotifierFd); + rtaCommandCloseConnection_Release(&closeConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCloseConnection_GetApiNotifierFd) +{ + int apiNotifierFd = 7; + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(apiNotifierFd); + + int testFd = rtaCommandCloseConnection_GetApiNotifierFd(closeConnection); + assertTrue(testFd == apiNotifierFd, "Wrong value, got %d expected %d", testFd, apiNotifierFd); + + rtaCommandCloseConnection_Release(&closeConnection); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCloseConnection_Release) +{ + int apiNotifierFd = 7; + RtaCommandCloseConnection *closeConnection = rtaCommandCloseConnection_Create(apiNotifierFd); + + RtaCommandCloseConnection *second = rtaCommandCloseConnection_Acquire(closeConnection); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + rtaCommandCloseConnection_Release(&closeConnection); + size_t thirdRefCount = parcObject_GetReferenceCount(second); + + assertTrue(thirdRefCount == secondRefCount - 1, "Wrong refcount after release, got %zu expected %zu", thirdRefCount, secondRefCount - 1); + + rtaCommandCloseConnection_Release(&second); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_CommandCloseConnection); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandCreateProtocolStack.c b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandCreateProtocolStack.c new file mode 100644 index 00000000..948477f8 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandCreateProtocolStack.c @@ -0,0 +1,215 @@ +/* + * 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_CommandCreateProtocolStack.c" + +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> +#include <parc/testing/parc_ObjectTesting.h> + +LONGBOW_TEST_RUNNER(rta_CommandCreateProtocolStack) +{ + // 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_CommandCreateProtocolStack) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_CommandCreateProtocolStack) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_Acquire); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_Create); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_IsValid); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_IsValid_NULL); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_IsValid_BadCCNxStackConfig); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_AssertValid); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_GetConfig); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_GetStackId); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_GetStackConfig); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandCreateProtocolStack_Release); +} + +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, rtaCommandCreateProtocolStack_Acquire) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + + parcObjectTesting_AssertAcquire(createStack); + + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_Create) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + assertNotNull(createStack, "Expected rtaCommandCreateProtocolStack_Create to return non-NULL."); + + assertTrue(createStack->stackId == stackId, "Expected stackId %d, actual %d", stackId, createStack->stackId); + + assertTrue(ccnxStackConfig_Equals(config, createStack->config), + "ProtocolStackConfig instances are not equal"); + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_IsValid) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + + assertTrue(rtaCommandCreateProtocolStack_IsValid(createStack), + "Expected rtaCommandCreateProtocolStack_Create to return a valid instance."); + + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_IsValid_NULL) +{ + RtaCommandCreateProtocolStack *createStack = NULL; + + assertFalse(rtaCommandCreateProtocolStack_IsValid(createStack), + "Expected rtaCommandCreateProtocolStack_Create to return a valid instance."); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_IsValid_BadCCNxStackConfig) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + CCNxStackConfig *original = createStack->config; + createStack->config = NULL; // Make it bad. + assertFalse(rtaCommandCreateProtocolStack_IsValid(createStack), + "Expected rtaCommandCreateProtocolStack_Create to return a valid instance."); + createStack->config = original; + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_AssertValid) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + + rtaCommandCreateProtocolStack_AssertValid(createStack); + + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_GetStackConfig) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + + CCNxStackConfig *actual = rtaCommandCreateProtocolStack_GetStackConfig(createStack); + + assertTrue(ccnxStackConfig_Equals(config, actual), + "CCNxStackConfig instances are not equal"); + + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_GetConfig) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + + assertTrue(ccnxStackConfig_Equals(config, createStack->config), + "ProtocolStackConfig instances are not equal"); + + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_GetStackId) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + + int testStackId = rtaCommandCreateProtocolStack_GetStackId(createStack); + assertTrue(testStackId == stackId, "Wrong value, got %d expected %d", testStackId, stackId); + + rtaCommandCreateProtocolStack_Release(&createStack); + ccnxStackConfig_Release(&config); +} + +LONGBOW_TEST_CASE(Global, rtaCommandCreateProtocolStack_Release) +{ + int stackId = 7; + CCNxStackConfig *config = ccnxStackConfig_Create(); + RtaCommandCreateProtocolStack *createStack = rtaCommandCreateProtocolStack_Create(stackId, config); + + RtaCommandCreateProtocolStack *second = rtaCommandCreateProtocolStack_Acquire(createStack); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + rtaCommandCreateProtocolStack_Release(&createStack); + size_t thirdRefCount = parcObject_GetReferenceCount(second); + + assertTrue(thirdRefCount == secondRefCount - 1, + "Wrong refcount after release, got %zu expected %zu", thirdRefCount, secondRefCount - 1); + + rtaCommandCreateProtocolStack_Release(&second); + ccnxStackConfig_Release(&config); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_CommandCreateProtocolStack); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandDestroyProtocolStack.c b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandDestroyProtocolStack.c new file mode 100644 index 00000000..37e1128d --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandDestroyProtocolStack.c @@ -0,0 +1,125 @@ +/* + * 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_CommandDestroyProtocolStack.c" + +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> + +LONGBOW_TEST_RUNNER(rta_CommandDestroyProtocolStack) +{ + // 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_CommandDestroyProtocolStack) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_CommandDestroyProtocolStack) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaCommandDestroyProtocolStack_Acquire); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandDestroyProtocolStack_Create); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandDestroyProtocolStack_GetStackId); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandDestroyProtocolStack_Release); +} + +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, rtaCommandDestroyProtocolStack_Acquire) +{ + int stackId = 7; + RtaCommandDestroyProtocolStack *destroyStack = rtaCommandDestroyProtocolStack_Create(stackId); + size_t firstRefCount = parcObject_GetReferenceCount(destroyStack); + + RtaCommandDestroyProtocolStack *second = rtaCommandDestroyProtocolStack_Acquire(destroyStack); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + assertTrue(secondRefCount == firstRefCount + 1, "Wrong refcount after acquire, got %zu expected %zu", secondRefCount, firstRefCount + 1); + + rtaCommandDestroyProtocolStack_Release(&destroyStack); + rtaCommandDestroyProtocolStack_Release(&second); +} + +LONGBOW_TEST_CASE(Global, rtaCommandDestroyProtocolStack_Create) +{ + int stackId = 7; + RtaCommandDestroyProtocolStack *destroyStack = rtaCommandDestroyProtocolStack_Create(stackId); + assertNotNull(destroyStack, "Got null from create"); + assertTrue(destroyStack->stackId == stackId, "Internal stackId wrong, got %d expected %d", destroyStack->stackId, stackId); + rtaCommandDestroyProtocolStack_Release(&destroyStack); +} + +LONGBOW_TEST_CASE(Global, rtaCommandDestroyProtocolStack_GetStackId) +{ + int stackId = 7; + RtaCommandDestroyProtocolStack *destroyStack = rtaCommandDestroyProtocolStack_Create(stackId); + + int testStackId = rtaCommandDestroyProtocolStack_GetStackId(destroyStack); + assertTrue(testStackId == stackId, "Wrong value, got %d expected %d", testStackId, stackId); + + rtaCommandDestroyProtocolStack_Release(&destroyStack); +} + +LONGBOW_TEST_CASE(Global, rtaCommandDestroyProtocolStack_Release) +{ + int stackId = 7; + RtaCommandDestroyProtocolStack *destroyStack = rtaCommandDestroyProtocolStack_Create(stackId); + + RtaCommandDestroyProtocolStack *second = rtaCommandDestroyProtocolStack_Acquire(destroyStack); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + rtaCommandDestroyProtocolStack_Release(&destroyStack); + size_t thirdRefCount = parcObject_GetReferenceCount(second); + + assertTrue(thirdRefCount == secondRefCount - 1, "Wrong refcount after release, got %zu expected %zu", thirdRefCount, secondRefCount - 1); + + rtaCommandDestroyProtocolStack_Release(&second); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_CommandDestroyProtocolStack); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandOpenConnection.c b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandOpenConnection.c new file mode 100644 index 00000000..30d77931 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandOpenConnection.c @@ -0,0 +1,199 @@ +/* + * 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_CommandOpenConnection.c" + +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> + +typedef struct test_data { + int stackId; + int apiNotifierFd; + int transportNotifierFd; + PARCJSON *config; + + RtaCommandOpenConnection *openConnection; +} TestData; + +static TestData * +_createTestData(void) +{ + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + + data->stackId = 7; + data->apiNotifierFd = 11; + data->transportNotifierFd = 10029; + data->config = parcJSON_Create(); + + data->openConnection = rtaCommandOpenConnection_Create(data->stackId, data->apiNotifierFd, data->transportNotifierFd, data->config); + + return data; +} + +static void +_destroyTestData(TestData **dataPtr) +{ + TestData *data = *dataPtr; + + rtaCommandOpenConnection_Release(&data->openConnection); + parcJSON_Release(&data->config); + parcMemory_Deallocate((void **) &data); + + *dataPtr = NULL; +} + +// ============================================================= + +LONGBOW_TEST_RUNNER(rta_CommandOpenConnection) +{ + // 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_CommandOpenConnection) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_CommandOpenConnection) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaCommandOpenConnection_Acquire); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandOpenConnection_Create); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandOpenConnection_GetApiNotifierFd); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandOpenConnection_GetConfig); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandOpenConnection_GetStackId); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandOpenConnection_GetTransportNotifierFd); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandOpenConnection_Release); +} + +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, rtaCommandOpenConnection_Acquire) +{ + TestData *data = _createTestData(); + + size_t firstRefCount = parcObject_GetReferenceCount(data->openConnection); + + RtaCommandOpenConnection *second = rtaCommandOpenConnection_Acquire(data->openConnection); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + assertTrue(secondRefCount == firstRefCount + 1, "Wrong refcount after acquire, got %zu expected %zu", secondRefCount, firstRefCount + 1); + + rtaCommandOpenConnection_Release(&second); + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandOpenConnection_Create) +{ + TestData *data = _createTestData(); + assertNotNull(data->openConnection, "Got null from create"); + assertTrue(data->openConnection->stackId == data->stackId, "Internal stackId wrong, got %d expected %d", + data->openConnection->stackId, data->stackId); + assertTrue(data->openConnection->apiNotifierFd == data->apiNotifierFd, "Internal apiNotifierFd wrong, got %d expected %d", + data->openConnection->apiNotifierFd, data->apiNotifierFd); + assertTrue(data->openConnection->transportNotifierFd == data->transportNotifierFd, "Internal transportNotifierFd wrong, got %d expected %d", + data->openConnection->transportNotifierFd, data->transportNotifierFd); + assertTrue(parcJSON_Equals(data->openConnection->config, data->config), "JSON configs are not equal"); + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandOpenConnection_GetApiNotifierFd) +{ + TestData *data = _createTestData(); + + int testApiFd = rtaCommandOpenConnection_GetApiNotifierFd(data->openConnection); + assertTrue(testApiFd == data->apiNotifierFd, "Wrong value, got %d expected %d", testApiFd, data->apiNotifierFd); + + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandOpenConnection_GetConfig) +{ + TestData *data = _createTestData(); + + PARCJSON *testConfig = rtaCommandOpenConnection_GetConfig(data->openConnection); + assertTrue(parcJSON_Equals(data->config, testConfig), "Configurations do not match"); + + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandOpenConnection_GetStackId) +{ + TestData *data = _createTestData(); + + int testStackId = rtaCommandOpenConnection_GetStackId(data->openConnection); + assertTrue(testStackId == data->stackId, "Wrong value, got %d expected %d", testStackId, data->stackId); + + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandOpenConnection_GetTransportNotifierFd) +{ + TestData *data = _createTestData(); + + int testTransportFd = rtaCommandOpenConnection_GetTransportNotifierFd(data->openConnection); + assertTrue(testTransportFd == data->transportNotifierFd, "Wrong value, got %d expected %d", testTransportFd, data->transportNotifierFd); + + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandOpenConnection_Release) +{ + TestData *data = _createTestData(); + + RtaCommandOpenConnection *second = rtaCommandOpenConnection_Acquire(data->openConnection); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + rtaCommandOpenConnection_Release(&second); + size_t thirdRefCount = parcObject_GetReferenceCount(data->openConnection); + + assertTrue(thirdRefCount == secondRefCount - 1, "Wrong refcount after release, got %zu expected %zu", thirdRefCount, secondRefCount - 1); + + _destroyTestData(&data); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_CommandOpenConnection); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandTransmitStatistics.c b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandTransmitStatistics.c new file mode 100644 index 00000000..cc312e5a --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/commands/test/test_rta_CommandTransmitStatistics.c @@ -0,0 +1,170 @@ +/* + * 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_CommandTransmitStatistics.c" + +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> + +#include <sys/time.h> + +#define MAX_FILENAME 1024 + +typedef struct test_data { + struct timeval period; + char filename[MAX_FILENAME]; + + RtaCommandTransmitStatistics *transmitStats; +} TestData; + +static TestData * +_createTestData(void) +{ + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); + + data->period = (struct timeval) { .tv_sec = 1234, .tv_usec = 2389484 }; + snprintf(data->filename, MAX_FILENAME, "Miss Piggy"); + + data->transmitStats = rtaCommandTransmitStatistics_Create(data->period, data->filename); + + return data; +} + +static void +_destroyTestData(TestData **dataPtr) +{ + TestData *data = *dataPtr; + rtaCommandTransmitStatistics_Release(&data->transmitStats); + parcMemory_Deallocate((void **) &data); + *dataPtr = NULL; +} + +// ============================================================= +LONGBOW_TEST_RUNNER(rta_CommandTransmitStatistics) +{ + // 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_CommandTransmitStatistics) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(rta_CommandTransmitStatistics) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, rtaCommandTransmitStatistics_Acquire); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandTransmitStatistics_Create); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandTransmitStatistics_GetFilename); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandTransmitStatistics_GetPeriod); + LONGBOW_RUN_TEST_CASE(Global, rtaCommandTransmitStatistics_Release); +} + +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, rtaCommandTransmitStatistics_Acquire) +{ + TestData *data = _createTestData(); + + size_t firstRefCount = parcObject_GetReferenceCount(data->transmitStats); + + RtaCommandTransmitStatistics *second = rtaCommandTransmitStatistics_Acquire(data->transmitStats); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + assertTrue(secondRefCount == firstRefCount + 1, "Wrong refcount after acquire, got %zu expected %zu", secondRefCount, firstRefCount + 1); + + rtaCommandTransmitStatistics_Release(&second); + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandTransmitStatistics_Create) +{ + TestData *data = _createTestData(); + assertNotNull(data->transmitStats, "Got null from create"); + + assertTrue(timercmp(&data->period, &data->transmitStats->period, ==), "Period values not equal"); + assertTrue(strcmp(data->filename, data->transmitStats->filename) == 0, "Filenames not equal"); + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandTransmitStatistics_GetFilename) +{ + TestData *data = _createTestData(); + + const char *testFilename = rtaCommandTransmitStatistics_GetFilename(data->transmitStats); + assertTrue(strcmp(data->filename, testFilename) == 0, "Filenames not equal"); + + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandTransmitStatistics_GetPeriod) +{ + TestData *data = _createTestData(); + + struct timeval testPeriod = rtaCommandTransmitStatistics_GetPeriod(data->transmitStats); + assertTrue(timercmp(&data->period, &testPeriod, ==), "Period values not equal"); + + _destroyTestData(&data); +} + +LONGBOW_TEST_CASE(Global, rtaCommandTransmitStatistics_Release) +{ + TestData *data = _createTestData(); + + RtaCommandTransmitStatistics *second = rtaCommandTransmitStatistics_Acquire(data->transmitStats); + size_t secondRefCount = parcObject_GetReferenceCount(second); + + rtaCommandTransmitStatistics_Release(&second); + size_t thirdRefCount = parcObject_GetReferenceCount(data->transmitStats); + + assertTrue(thirdRefCount == secondRefCount - 1, "Wrong refcount after release, got %zu expected %zu", thirdRefCount, secondRefCount - 1); + + _destroyTestData(&data); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(rta_CommandTransmitStatistics); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} |