diff options
Diffstat (limited to 'libccnx-transport-rta/ccnx/transport/transport_rta/components/test')
6 files changed, 738 insertions, 0 deletions
diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/CMakeLists.txt b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/CMakeLists.txt new file mode 100644 index 00000000..0b4416f7 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/CMakeLists.txt @@ -0,0 +1,16 @@ +# Enable gcov output for the tests +add_definitions(--coverage) +set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} " --coverage") + +set(TestsExpectedToPass + test_codec_Signing + test_component_Codec_Tlv + test_component_Codec_Tlv_Hmac + test_component_Testing +) + + +foreach(test ${TestsExpectedToPass}) + AddTest(${test}) +endforeach() + diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_codec_Signing.c b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_codec_Signing.c new file mode 100644 index 00000000..3740994b --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_codec_Signing.c @@ -0,0 +1,61 @@ +/* + * 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 "../codec_Signing.c" + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(codec_Signing) +{ + LONGBOW_RUN_TEST_FIXTURE(Global); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(codec_Signing) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(codec_Signing) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(codec_Signing); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Codec_Tlv.c b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Codec_Tlv.c new file mode 100644 index 00000000..1990d8ae --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Codec_Tlv.c @@ -0,0 +1,276 @@ +/* + * 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 "../component_Codec_Tlv.c" +#include <LongBow/unit-test.h> +#include <parc/algol/parc_SafeMemory.h> +#include <ccnx/api/control/cpi_ControlFacade.h> + +#include <parc/security/parc_Pkcs12KeyStore.h> +#include <parc/security/parc_Security.h> +#include <ccnx/transport/transport_rta/config/config_All.h> + +#include <ccnx/transport/test_tools/traffic_tools.h> +#include <ccnx/common/codec/schema_v1/testdata/v1_interest_nameA.h> +#include <ccnx/common/codec/schema_v1/testdata/v1_cpi_add_route_crc32c.h> + + +#include <ccnx/common/ccnx_WireFormatMessage.h> + +#include "testrig_MockFramework.c" + +typedef struct test_data { + MockFramework *mock; + char keystore_filename[MAXPATH]; + char keystore_password[MAXPATH]; +} TestData; + +static CCNxTransportConfig * +codecTlv_CreateParams(const char *keystore_filename, const char *keystore_password) +{ + assertNotNull(keystore_filename, "Got null keystore name\n"); + assertNotNull(keystore_password, "Got null keystore passwd\n"); + + CCNxStackConfig *stackConfig = ccnxStackConfig_Create(); + + apiConnector_ProtocolStackConfig(stackConfig); + testingUpper_ProtocolStackConfig(stackConfig); + tlvCodec_ProtocolStackConfig(stackConfig); + testingLower_ProtocolStackConfig(stackConfig); + protocolStack_ComponentsConfigArgs(stackConfig, apiConnector_GetName(), testingUpper_GetName(), tlvCodec_GetName(), testingLower_GetName(), NULL); + + CCNxConnectionConfig *connConfig = apiConnector_ConnectionConfig(ccnxConnectionConfig_Create()); + testingUpper_ConnectionConfig(connConfig); + tlvCodec_ConnectionConfig(connConfig); + testingLower_ConnectionConfig(connConfig); + + unlink(keystore_filename); + + bool success = parcPkcs12KeyStore_CreateFile(keystore_filename, keystore_password, "alice", 1024, 30); + assertTrue(success, "parcPkcs12KeyStore_CreateFile() failed."); + + publicKeySigner_ConnectionConfig(connConfig, keystore_filename, keystore_password); + + CCNxTransportConfig *result = ccnxTransportConfig_Create(stackConfig, connConfig); + ccnxStackConfig_Release(&stackConfig); + return result; +} + +static TestData * +_commonSetup(void) +{ + parcSecurity_Init(); + + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); + + sprintf(data->keystore_filename, "/tmp/alice_keystore.p12.XXXXXX"); + mktemp(data->keystore_filename); + sprintf(data->keystore_password, "12345"); + + CCNxTransportConfig *config = codecTlv_CreateParams(data->keystore_filename, data->keystore_password); + data->mock = mockFramework_Create(config); + ccnxTransportConfig_Destroy(&config); + return data; +} + +static void +_commonTeardown(TestData *data) +{ + mockFramework_Destroy(&data->mock); + unlink(data->keystore_filename); + parcMemory_Deallocate((void **) &data); + + parcSecurity_Fini(); +} + +LONGBOW_TEST_RUNNER(component_Codec_Tlv) +{ + LONGBOW_RUN_TEST_FIXTURE(Global); + LONGBOW_RUN_TEST_FIXTURE(Dictionary); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(component_Codec_Tlv) +{ + 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(component_Codec_Tlv) +{ + 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; +} + +// ================================================================================== + +static TransportMessage * +sendDown(TestData *data, TransportMessage *tm_going_down) +{ + PARCEventQueue *in = rtaProtocolStack_GetPutQueue(data->mock->stack, TESTING_UPPER, RTA_DOWN); + PARCEventQueue *out = rtaProtocolStack_GetPutQueue(data->mock->stack, TESTING_LOWER, RTA_UP); + + rtaComponent_PutMessage(in, tm_going_down); + // turn the handle enough times, the message will pass all the way out the bottom + rtaFramework_NonThreadedStepCount(data->mock->framework, 10); + return rtaComponent_GetMessage(out); +} + +static TransportMessage * +sendUp(TestData *data, TransportMessage *tm_going_down) +{ + PARCEventQueue *out = rtaProtocolStack_GetPutQueue(data->mock->stack, TESTING_UPPER, RTA_DOWN); + PARCEventQueue *in = rtaProtocolStack_GetPutQueue(data->mock->stack, TESTING_LOWER, RTA_UP); + + rtaComponent_PutMessage(in, tm_going_down); + // turn the handle enough times, the message will pass all the way out the bottom + rtaFramework_NonThreadedStepCount(data->mock->framework, 10); + return rtaComponent_GetMessage(out); +} + + +// ================================================================================== + +LONGBOW_TEST_FIXTURE(Dictionary) +{ + LONGBOW_RUN_TEST_CASE(Dictionary, component_Codec_Tlv_Downcall_Read_Interest); + LONGBOW_RUN_TEST_CASE(Dictionary, component_Codec_Tlv_Downcall_Read_Control); + LONGBOW_RUN_TEST_CASE(Dictionary, component_Codec_Tlv_Downcall_Read_Raw); + + LONGBOW_RUN_TEST_CASE(Dictionary, component_Codec_Tlv_Upcall_Read_Control); +} + +LONGBOW_TEST_FIXTURE_SETUP(Dictionary) +{ + longBowTestCase_SetClipBoardData(testCase, _commonSetup()); + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Dictionary) +{ + _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; +} + +/** + * Makes sure an interest going down the stack gets encoded. Does not test + * the actual wire format -- that's the job of the tlv unit tests. + */ +LONGBOW_TEST_CASE(Dictionary, component_Codec_Tlv_Downcall_Read_Interest) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + TransportMessage *tm = trafficTools_CreateTransportMessageWithDictionaryInterest(data->mock->connection, CCNxTlvDictionary_SchemaVersion_V1); + TransportMessage *test_tm = sendDown(data, tm); + CCNxCodecNetworkBufferIoVec *vec = + ccnxTlvDictionary_GetIoVec(transportMessage_GetDictionary(test_tm), CCNxCodecSchemaV1TlvDictionary_HeadersFastArray_WireFormat); + assertNotNull(vec, "Output of coded did not have a raw format message"); + transportMessage_Destroy(&test_tm); +} + +/** + * control message should be passed through + */ +LONGBOW_TEST_CASE(Dictionary, component_Codec_Tlv_Downcall_Read_Control) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + TransportMessage *tm = trafficTools_CreateTransportMessageWithDictionaryControl(data->mock->connection, + CCNxTlvDictionary_SchemaVersion_V1); + TransportMessage *test_tm = sendDown(data, tm); + PARCJSON *json = ccnxControlFacade_GetJson(transportMessage_GetDictionary(test_tm)); + assertNotNull(json, "Output of codec did not have a control message"); + transportMessage_Destroy(&test_tm); +} + +LONGBOW_TEST_CASE(Dictionary, component_Codec_Tlv_Downcall_Read_Raw) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + TransportMessage *tm = trafficTools_CreateTransportMessageWithDictionaryRaw(data->mock->connection, + CCNxTlvDictionary_SchemaVersion_V1); + TransportMessage *test_tm = sendDown(data, tm); + PARCBuffer *buffer = ccnxWireFormatMessage_GetWireFormatBuffer(transportMessage_GetDictionary(test_tm)); + assertNotNull(buffer, "Output of codec did not have a raw format message"); + transportMessage_Destroy(&test_tm); +} + +LONGBOW_TEST_CASE(Dictionary, component_Codec_Tlv_Upcall_Read_Control) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + + PARCBuffer *wireFormat = parcBuffer_Wrap(v1_cpi_add_route_crc32c, sizeof(v1_cpi_add_route_crc32c), + 0, sizeof(v1_cpi_add_route_crc32c)); + CCNxTlvDictionary *dictionary = ccnxWireFormatMessage_FromControlPacketType(CCNxTlvDictionary_SchemaVersion_V1, wireFormat); + parcBuffer_Release(&wireFormat); + + // We have not set the message type or schema + TransportMessage *tm = transportMessage_CreateFromDictionary(dictionary); + transportMessage_SetInfo(tm, data->mock->connection, NULL); + ccnxTlvDictionary_Release(&dictionary); + + // ------ + // Now do the actual test of sending the transport message up the stack + + TransportMessage *test_tm = sendUp(data, tm); + + // It should now be parsed into an control message + CCNxTlvDictionary *testdict = transportMessage_GetDictionary(test_tm); + assertNotNull(testdict, "Failed to get dictionary from the transport message"); + + assertTrue(ccnxTlvDictionary_IsControl(testdict), "Dictionary says it is not a Control"); + assertTrue(ccnxTlvDictionary_GetSchemaVersion(testdict) == CCNxTlvDictionary_SchemaVersion_V1, + "Wrong schema, got %d expected %d", + ccnxTlvDictionary_GetSchemaVersion(testdict), CCNxTlvDictionary_SchemaVersion_V1); + + transportMessage_Destroy(&tm); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(component_Codec_Tlv); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Codec_Tlv_Hmac.c b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Codec_Tlv_Hmac.c new file mode 100644 index 00000000..4934a266 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Codec_Tlv_Hmac.c @@ -0,0 +1,204 @@ +/* + * 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 "../component_Codec_Tlv.c" +#include <parc/algol/parc_SafeMemory.h> + +#include <stdio.h> +#include <sys/un.h> +#include <strings.h> + +#include <LongBow/unit-test.h> +#include <LongBow/runtime.h> + +#define DEBUG_OUTPUT 0 + +#include <parc/security/parc_Security.h> +#include <parc/security/parc_SymmetricKeyStore.h> + +#include <ccnx/transport/transport_rta/config/config_All.h> +#include <ccnx/transport/transport_rta/core/rta_Framework.h> +#include <ccnx/transport/transport_rta/core/rta_Framework_NonThreaded.h> +#include <ccnx/transport/transport_rta/core/rta_Component.h> + +#include <ccnx/transport/test_tools/traffic_tools.h> + +#include "testrig_MockFramework.c" + +#ifndef MAXPATH +#define MAXPATH 1024 +#endif + +typedef struct test_data { + MockFramework *mock; + char keystore_filename[MAXPATH]; + char keystore_password[MAXPATH]; +} TestData; + +LONGBOW_TEST_RUNNER(System) +{ + LONGBOW_RUN_TEST_FIXTURE(Component_Codec_Tlv_Hmac); +} + +LONGBOW_TEST_RUNNER_SETUP(System) +{ + parcMemory_SetInterface(&PARCSafeMemoryAsPARCMemory); + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_RUNNER_TEARDOWN(System) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// ================================ + +static CCNxTransportConfig * +codecTlv_CreateParams(const char *keystore_name, const char *keystore_passwd) +{ + assertNotNull(keystore_name, "Got null keystore name\n"); + assertNotNull(keystore_passwd, "Got null keystore passwd\n"); + + CCNxStackConfig *stackConfig = ccnxStackConfig_Create(); + + apiConnector_ProtocolStackConfig(stackConfig); + testingUpper_ProtocolStackConfig(stackConfig); + tlvCodec_ProtocolStackConfig(stackConfig); + testingLower_ProtocolStackConfig(stackConfig); + protocolStack_ComponentsConfigArgs(stackConfig, + apiConnector_GetName(), + testingUpper_GetName(), + tlvCodec_GetName(), + testingLower_GetName(), + NULL); + + CCNxConnectionConfig *connConfig = apiConnector_ConnectionConfig(ccnxConnectionConfig_Create()); + testingUpper_ConnectionConfig(connConfig); + tlvCodec_ConnectionConfig(connConfig); + testingLower_ConnectionConfig(connConfig); + + symmetricKeySigner_ConnectionConfig(connConfig, keystore_name, keystore_passwd); + CCNxTransportConfig *result = ccnxTransportConfig_Create(stackConfig, connConfig); + ccnxStackConfig_Release(&stackConfig); + return result; +} + +static TestData * +_commonSetup(void) +{ + parcSecurity_Init(); + + TestData *data = parcMemory_AllocateAndClear(sizeof(TestData)); + assertNotNull(data, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(TestData)); + + sprintf(data->keystore_filename, "/tmp/alice_keystore.p12.XXXXXX"); + mktemp(data->keystore_filename); + sprintf(data->keystore_password, "12345"); + + unlink(data->keystore_filename); + PARCBuffer *secret_key = parcSymmetricKeyStore_CreateKey(256); + parcSymmetricKeyStore_CreateFile(data->keystore_filename, data->keystore_password, secret_key); + parcBuffer_Release(&secret_key); + + CCNxTransportConfig *config = codecTlv_CreateParams(data->keystore_filename, data->keystore_password); + data->mock = mockFramework_Create(config); + ccnxTransportConfig_Destroy(&config); + + return data; +} + +static void +_commonTeardown(TestData *data) +{ + mockFramework_Destroy(&data->mock); + unlink(data->keystore_filename); + parcMemory_Deallocate((void **) &data); + + parcSecurity_Fini(); +} + +// ====================================== + + +LONGBOW_TEST_FIXTURE(Component_Codec_Tlv_Hmac) +{ + LONGBOW_RUN_TEST_CASE(Component_Codec_Tlv_Hmac, open_close); + LONGBOW_RUN_TEST_CASE(Component_Codec_Tlv_Hmac, Component_Codec_Tlv_Hmac_Downcall_Read); +} + +LONGBOW_TEST_FIXTURE_SETUP(Component_Codec_Tlv_Hmac) +{ + longBowTestCase_SetClipBoardData(testCase, _commonSetup()); + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Component_Codec_Tlv_Hmac) +{ + _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(Component_Codec_Tlv_Hmac, open_close) +{ + // dont actually do anything. make sure no memory leaks in setup and teardown. +} + +// ============================================ +// TLV tests + +static TransportMessage * +sendDown(TestData *data, TransportMessage *tm_going_down) +{ + PARCEventQueue *in = rtaProtocolStack_GetPutQueue(data->mock->stack, TESTING_UPPER, RTA_DOWN); + PARCEventQueue *out = rtaProtocolStack_GetPutQueue(data->mock->stack, TESTING_LOWER, RTA_UP); + + rtaComponent_PutMessage(in, tm_going_down); + // turn the handle enough times, the message will pass all the way out the bottom + rtaFramework_NonThreadedStepCount(data->mock->framework, 5); + return rtaComponent_GetMessage(out); +} + +LONGBOW_TEST_CASE(Component_Codec_Tlv_Hmac, Component_Codec_Tlv_Hmac_Downcall_Read) +{ + TestData *data = longBowTestCase_GetClipBoardData(testCase); + TransportMessage *tm = trafficTools_CreateTransportMessageWithSignedContentObject(data->mock->connection); + + TransportMessage *test_tm = sendDown(data, tm); + + // we should now have a RawFormat message + CCNxCodecNetworkBufferIoVec *vec = ccnxWireFormatMessage_GetIoVec(transportMessage_GetDictionary(test_tm)); + assertNotNull(vec, "Output of coded message did not have a raw format message") + { + ccnxTlvDictionary_Display(transportMessage_GetDictionary(test_tm), 0); + } + + transportMessage_Destroy(&test_tm); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(System); + exit(longBowMain(argc, argv, testRunner, NULL)); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Testing.c b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Testing.c new file mode 100644 index 00000000..6593972f --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/test_component_Testing.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 the file(s) containing the functions to be tested. +// This permits internal static functions to be visible to this Test Framework. +#include "../component_Testing.c" + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(component_Testing) +{ + // 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(component_Testing) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(component_Testing) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + 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(component_Testing); + int exitStatus = longBowMain(argc, argv, testRunner, NULL); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/testrig_MockFramework.c b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/testrig_MockFramework.c new file mode 100644 index 00000000..0ca07ab1 --- /dev/null +++ b/libccnx-transport-rta/ccnx/transport/transport_rta/components/test/testrig_MockFramework.c @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This test rig sets up a mock RTA Framework for testing Components and Connectors. + * + * + */ + +#include <ccnx/transport/transport_rta/core/rta_Framework.h> +#include <ccnx/transport/transport_rta/core/rta_Framework_Commands.c> +#include <ccnx/transport/transport_rta/core/rta_Framework_NonThreaded.h> + +#ifndef MAXPATH +#define MAXPATH 1024 +#endif + +typedef struct mock_framework { + PARCRingBuffer1x1*commandRingBuffer; + PARCNotifier *commandNotifier; + RtaFramework *framework; + + int stackId; + RtaProtocolStack *stack; + + int connection_fds[2]; + RtaConnection *connection; + + CCNxTransportConfig *transport_config; +} MockFramework; + +static MockFramework * +mockFramework_Create(CCNxTransportConfig *config) +{ + MockFramework *mock = parcMemory_AllocateAndClear(sizeof(MockFramework)); + assertNotNull(mock, "parcMemory_AllocateAndClear(%zu) returned NULL", sizeof(MockFramework)); + + mock->transport_config = ccnxTransportConfig_Copy(config); + assertNotNull(mock->transport_config, "%s got null params from createParams\n", __func__); + + mock->commandRingBuffer = parcRingBuffer1x1_Create(128, NULL); + mock->commandNotifier = parcNotifier_Create(); + mock->framework = rtaFramework_Create(mock->commandRingBuffer, mock->commandNotifier); + + // Create the protocol stack + + mock->stackId = 1; + RtaCommandCreateProtocolStack *createStack = + rtaCommandCreateProtocolStack_Create(mock->stackId, ccnxTransportConfig_GetStackConfig(mock->transport_config)); + _rtaFramework_ExecuteCreateStack(mock->framework, createStack); + rtaCommandCreateProtocolStack_Release(&createStack); + + // peek inside and get the protocol stack reference + FrameworkProtocolHolder *fph = rtaFramework_GetProtocolStackByStackId(mock->framework, mock->stackId); + mock->stack = fph->stack; + + int error = socketpair(AF_UNIX, SOCK_STREAM, 0, mock->connection_fds); + assertFalse(error, "Error creating socket pair: (%d) %s", errno, strerror(errno)); + + RtaCommandOpenConnection *openConnection = rtaCommandOpenConnection_Create(mock->stackId, mock->connection_fds[0], mock->connection_fds[1], + ccnxConnectionConfig_GetJson(ccnxTransportConfig_GetConnectionConfig(mock->transport_config))); + _rtaFramework_ExecuteOpenConnection(mock->framework, openConnection); + rtaCommandOpenConnection_Release(&openConnection); + + mock->connection = rtaConnectionTable_GetByApiFd(mock->framework->connectionTable, mock->connection_fds[0]); + + // Uses the non-threaded forwarder, make sure we step at least once + rtaFramework_NonThreadedStep(mock->framework); + + return mock; +} + +static void +mockFramework_Destroy(MockFramework **mockPtr) +{ + MockFramework *mock = *mockPtr; + + rtaFramework_Teardown(mock->framework); + + parcRingBuffer1x1_Release(&mock->commandRingBuffer); + parcNotifier_Release(&mock->commandNotifier); + + rtaFramework_Destroy(&mock->framework); + ccnxTransportConfig_Destroy(&mock->transport_config); + + parcMemory_Deallocate((void **) &mock); +} |