diff options
author | Luca Muscariello <lumuscar+fdio@cisco.com> | 2017-02-23 17:01:02 +0100 |
---|---|---|
committer | Luca Muscariello <lumuscar+fdio@cisco.com> | 2017-02-23 17:21:02 +0100 |
commit | ec688b4723a041044226358bcd4dd6e2da39da49 (patch) | |
tree | 3a244c48d1eb9e4d90f9050fd1a61ae5c0327526 /libparc/parc/logging/test | |
parent | 9b30fc10fb1cbebe651e5a107e8ca5b24de54675 (diff) |
Initial commit: cframework. Longbow and Libparc
Change-Id: I90378dbd30da6033b20fb1f829b3b822cf366c59
Signed-off-by: Luca Muscariello <lumuscar+fdio@cisco.com>
Diffstat (limited to 'libparc/parc/logging/test')
-rw-r--r-- | libparc/parc/logging/test/.gitignore | 12 | ||||
-rw-r--r-- | libparc/parc/logging/test/CMakeLists.txt | 20 | ||||
-rw-r--r-- | libparc/parc/logging/test/test_parc_Log.c | 345 | ||||
-rw-r--r-- | libparc/parc/logging/test/test_parc_LogEntry.c | 465 | ||||
-rwxr-xr-x | libparc/parc/logging/test/test_parc_LogFormatSyslog.c | 97 | ||||
-rw-r--r-- | libparc/parc/logging/test/test_parc_LogFormatText.c | 95 | ||||
-rwxr-xr-x | libparc/parc/logging/test/test_parc_LogLevel.c | 138 | ||||
-rw-r--r-- | libparc/parc/logging/test/test_parc_LogReporter.c | 164 | ||||
-rwxr-xr-x | libparc/parc/logging/test/test_parc_LogReporterFile.c | 149 | ||||
-rwxr-xr-x | libparc/parc/logging/test/test_parc_LogReporterTextStdout.c | 128 |
10 files changed, 1613 insertions, 0 deletions
diff --git a/libparc/parc/logging/test/.gitignore b/libparc/parc/logging/test/.gitignore new file mode 100644 index 00000000..9b66ee0e --- /dev/null +++ b/libparc/parc/logging/test/.gitignore @@ -0,0 +1,12 @@ +*.gcov +*.gcda +*.gcno +*.log +test_parc_LogEntry +test_parc_Log +test_parc_LogReporterFile +test_parc_LogLevel +test_parc_LogReporter +test_parc_LogReporterTextStdout +test_parc_LogFormatSyslog +test_parc_LogFormatText diff --git a/libparc/parc/logging/test/CMakeLists.txt b/libparc/parc/logging/test/CMakeLists.txt new file mode 100644 index 00000000..060a0ba1 --- /dev/null +++ b/libparc/parc/logging/test/CMakeLists.txt @@ -0,0 +1,20 @@ +set(TestsExpectedToPass + test_parc_Log + test_parc_LogEntry + test_parc_LogFormatSyslog + test_parc_LogFormatText + test_parc_LogLevel + test_parc_LogReporter + test_parc_LogReporterFile + test_parc_LogReporterTextStdout + ) + +# Enable gcov output for the tests +add_definitions(--coverage) +set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} " --coverage") + +foreach(test ${TestsExpectedToPass}) + AddTest(${test}) +endforeach() + + diff --git a/libparc/parc/logging/test/test_parc_Log.c b/libparc/parc/logging/test/test_parc_Log.c new file mode 100644 index 00000000..af9f65a5 --- /dev/null +++ b/libparc/parc/logging/test/test_parc_Log.c @@ -0,0 +1,345 @@ +/* + * 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 "../parc_Log.c" + +#include <parc/logging/parc_LogLevel.h> +#include <parc/logging/parc_LogReporterFile.h> + +#include <parc/algol/parc_FileOutputStream.h> +#include <parc/algol/parc_SafeMemory.h> + +#include <LongBow/testing.h> +#include <LongBow/debugging.h> + +LONGBOW_TEST_RUNNER(test_parc_Log) +{ + // 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(CreateDestroy); + LONGBOW_RUN_TEST_FIXTURE(Global); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(test_parc_Log) +{ + 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(test_parc_Log) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +uint32_t CreationInitialMemoryOutstanding = 0; + +LONGBOW_TEST_FIXTURE(CreateDestroy) +{ + LONGBOW_RUN_TEST_CASE(CreateDestroy, parcLog_Create); + LONGBOW_RUN_TEST_CASE(CreateDestroy, parcLog_Create_DefaultValues); +} + +LONGBOW_TEST_FIXTURE_SETUP(CreateDestroy) +{ + CreationInitialMemoryOutstanding = parcMemory_Outstanding(); + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(CreateDestroy) +{ + if (parcMemory_Outstanding() != CreationInitialMemoryOutstanding) { + parcSafeMemory_ReportAllocation(STDOUT_FILENO); + printf("'%s' leaks memory by %u\n", + longBowTestCase_GetName(testCase), parcMemory_Outstanding() - CreationInitialMemoryOutstanding); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(CreateDestroy, parcLog_Create) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(output); + parcOutputStream_Release(&output); + + PARCLog *log = parcLog_Create("localhost", "test_parc_Log", NULL, reporter); + parcLogReporter_Release(&reporter); + + assertTrue(parcLogLevel_Equals(parcLog_GetLevel(log), PARCLogLevel_Off), "Expected initial log level to be OFF"); + + parcLog_Release(&log); +} + +LONGBOW_TEST_CASE(CreateDestroy, parcLog_Create_DefaultValues) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(output); + parcOutputStream_Release(&output); + + PARCLog *log = parcLog_Create(NULL, NULL, NULL, reporter); + parcLogReporter_Release(&reporter); + + assertTrue(parcLogLevel_Equals(parcLog_GetLevel(log), PARCLogLevel_Off), "Expected initial log level to be OFF"); + + parcLog_Release(&log); +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, parcLog_Emergency); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Warning); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Alert); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Critical); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Error); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Notice); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Debug); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Info); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Message); + LONGBOW_RUN_TEST_CASE(Global, parcLog_IsLoggable_True); + LONGBOW_RUN_TEST_CASE(Global, parcLog_IsLoggable_False); + + LONGBOW_RUN_TEST_CASE(Global, parcLog_Emergency_WrongLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Warning_WrongLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Alert_WrongLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Critical_WrongLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Error_WrongLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Notice_WrongLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Debug_WrongLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLog_Info_WrongLevel); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + CreationInitialMemoryOutstanding = parcMemory_Outstanding(); + + { + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(output); + parcOutputStream_Release(&output); + + PARCLog *log = parcLog_Create("localhost", "test_parc_Log", NULL, reporter); + parcLogReporter_Release(&reporter); + + parcLog_SetLevel(log, PARCLogLevel_All); + + longBowTestCase_SetClipBoardData(testCase, log); + } + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + { + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_Release(&log); + } + + if (parcMemory_Outstanding() != CreationInitialMemoryOutstanding) { + parcSafeMemory_ReportAllocation(STDOUT_FILENO); + printf("'%s' leaks memory by %u\n", + longBowTestCase_GetName(testCase), parcMemory_Outstanding() - CreationInitialMemoryOutstanding); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Global, parcLog_IsLoggable_True) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_IsLoggable(log, PARCLogLevel_Alert), "Expected parcLog_IsLoggable to be true."); +} + +LONGBOW_TEST_CASE(Global, parcLog_IsLoggable_False) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + parcLog_SetLevel(log, PARCLogLevel_Off); + + assertFalse(parcLog_IsLoggable(log, PARCLogLevel_Alert), "Expected parcLog_IsLoggable to be true."); +} + +LONGBOW_TEST_CASE(Global, parcLog_Info) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_Info(log, "This is a info message"), + "Expected message to be logged successfully"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Warning) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_Warning(log, "This is a warning message"), + "Expected message to be logged successfully"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Emergency) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_Emergency(log, "This is an emergency message"), + "Expected message to be logged successfully"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Alert) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_Alert(log, "This is a alert message"), + "Expected message to be logged successfully"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Critical) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_Critical(log, "This is a critical message"), + "Expected message to be logged successfully"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Notice) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_Notice(log, "This is a notice message"), + "Expected message to be logged successfully"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Error) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_Error(log, "This is a error message"), + "Expected message to be logged successfully"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Debug) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + + assertTrue(parcLog_Debug(log, "This is a debug message"), + "Expected message to be logged successfully"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Warning_WrongLevel) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Off); + + assertFalse(parcLog_Warning(log, "This is a warning message"), + "Expected message to not be logged"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Emergency_WrongLevel) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Off); + + // Even if the log level is set to off, you cannot block an emergency message. + assertTrue(parcLog_Emergency(log, "This is an emergency message"), + "Expected message to not be logged"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Alert_WrongLevel) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Off); + + assertFalse(parcLog_Alert(log, "This is a finest message"), + "Expected message to not be logged"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Critical_WrongLevel) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Off); + + assertFalse(parcLog_Critical(log, "This is a finer message"), + "Expected message to not be logged"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Notice_WrongLevel) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Off); + + assertFalse(parcLog_Notice(log, "This is a fine message"), + "Expected message to not be logged"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Debug_WrongLevel) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Off); + + assertFalse(parcLog_Debug(log, "This is a debug message"), + "Expected message to not be logged"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Error_WrongLevel) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Off); + + assertFalse(parcLog_Error(log, "This is a debug message"), + "Expected message to not be logged"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Info_WrongLevel) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Off); + + assertFalse(parcLog_Info(log, "This is a debug message"), + "Expected message to not be logged"); +} + +LONGBOW_TEST_CASE(Global, parcLog_Message) +{ + PARCLog *log = (PARCLog *) longBowTestCase_GetClipBoardData(testCase); + parcLog_SetLevel(log, PARCLogLevel_Alert); + + assertTrue(parcLog_Message(log, PARCLogLevel_Alert, 0, "This is an alert message"), + "Expected message to be logged"); +} + +int +main(int argc, char *argv[argc]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(test_parc_Log); + int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libparc/parc/logging/test/test_parc_LogEntry.c b/libparc/parc/logging/test/test_parc_LogEntry.c new file mode 100644 index 00000000..56a07c0d --- /dev/null +++ b/libparc/parc/logging/test/test_parc_LogEntry.c @@ -0,0 +1,465 @@ +/* + * 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 Runner. +#include "../parc_LogEntry.c" + +#include <LongBow/unit-test.h> + +#include <stdio.h> +#include <pthread.h> + +#include <parc/algol/parc_Memory.h> +#include <parc/algol/parc_SafeMemory.h> +#include <parc/testing/parc_ObjectTesting.h> + +#include <parc/logging/parc_LogReporter.h> +#include <parc/logging/parc_LogReporterTextStdout.h> +#include <parc/logging/parc_LogFormatText.h> + +LONGBOW_TEST_RUNNER(parc_LogEntry) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified here, but every test must be idempotent. + // Never rely on the execution order of tests or share state between them. + LONGBOW_RUN_TEST_FIXTURE(Creation); + LONGBOW_RUN_TEST_FIXTURE(Static); + LONGBOW_RUN_TEST_FIXTURE(Global); + + LONGBOW_RUN_TEST_FIXTURE(MultiThreaded); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(parc_LogEntry) +{ + 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(parc_LogEntry) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Creation) +{ + LONGBOW_RUN_TEST_CASE(Creation, parcLogEntry_CreateRelease); +} + +uint32_t CreationInitialMemoryOutstanding = 0; + +LONGBOW_TEST_FIXTURE_SETUP(Creation) +{ + CreationInitialMemoryOutstanding = parcMemory_Outstanding(); + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Creation) +{ + if (parcMemory_Outstanding() != CreationInitialMemoryOutstanding) { + parcSafeMemory_ReportAllocation(STDOUT_FILENO); + printf("'%s' leaks memory by %u\n", + longBowTestCase_GetName(testCase), parcMemory_Outstanding() - CreationInitialMemoryOutstanding); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Creation, parcLogEntry_CreateRelease) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 12345, timeStamp, payload); + parcBuffer_Release(&payload); + + parcObjectTesting_AssertAcquireReleaseContract(parcLogEntry_Acquire, entry); + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_AcquireRelease); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetBuffer); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetTimeStamp); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_ToString); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetMessageId); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetApplicationName); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetHostName); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetLevel); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetProcessName); + LONGBOW_RUN_TEST_CASE(Global, parcLogEntry_GetVersion); +} + +uint32_t GlobalInitialMemoryOutstanding = 0; + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + GlobalInitialMemoryOutstanding = parcMemory_Outstanding(); + + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + if (parcMemory_Outstanding() != GlobalInitialMemoryOutstanding) { + parcSafeMemory_ReportAllocation(STDOUT_FILENO); + printf("'%s' leaks memory by %u\n", + longBowTestCase_GetName(testCase), parcMemory_Outstanding() - GlobalInitialMemoryOutstanding); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_AcquireRelease) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + parcBuffer_Release(&payload); + + parcObjectTesting_AssertAcquireReleaseContract(parcLogEntry_Acquire, entry); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_GetBuffer) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + PARCBuffer *actual = parcLogEntry_GetPayload(entry); + + assertTrue(payload == actual, "Expected %p, actual %p", (void *) payload, (void *) actual); + parcBuffer_Release(&payload); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_GetTimeStamp) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + const struct timeval *actual = parcLogEntry_GetTimeStamp(entry); + + assertTrue(memcmp(&timeStamp, actual, sizeof(struct timeval)) == 0, "Expected timeStamp to be identical"); + parcBuffer_Release(&payload); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_GetLevel) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + const PARCLogLevel actual = parcLogEntry_GetLevel(entry); + + assertTrue(PARCLogLevel_Info == actual, "Expected %d, actual %d", PARCLogLevel_Info, actual); + parcBuffer_Release(&payload); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_GetVersion) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + const PARCLogLevel actual = parcLogEntry_GetVersion(entry); + + assertTrue(_parcLog_Version == actual, "Expected %d, actual %d", _parcLog_Version, actual); + parcBuffer_Release(&payload); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_GetHostName) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + char *expected = "hostname"; + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, expected, "applicationname", "processid", 1234, timeStamp, payload); + const char *actual = parcLogEntry_GetHostName(entry); + + assertTrue(strcmp(expected, actual) == 0, "Expected %s, actual %s", expected, actual); + parcBuffer_Release(&payload); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_GetApplicationName) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + char *expected = "applicationname"; + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + const char *actual = parcLogEntry_GetApplicationName(entry); + + assertTrue(strcmp(expected, actual) == 0, "Expected %s, actual %s", expected, actual); + parcBuffer_Release(&payload); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_GetProcessName) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + char *expected = "processid"; + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", expected, 1234, timeStamp, payload); + const char *actual = parcLogEntry_GetProcessName(entry); + + assertTrue(strcmp(expected, actual) == 0, "Expected %s, actual %s", expected, actual); + parcBuffer_Release(&payload); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_GetMessageId) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + + uint64_t expected = 1234; + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", expected, timeStamp, payload); + const uint64_t actual = parcLogEntry_GetMessageId(entry); + + assertTrue(expected == actual, "Expected %" PRId64 " actual %" PRId64 "", expected, actual); + parcBuffer_Release(&payload); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_CASE(Global, parcLogEntry_ToString) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + + parcBuffer_Release(&payload); + + char *actual = parcLogEntry_ToString(entry); + + parcMemory_Deallocate((void **) &actual); + + parcLogEntry_Release(&entry); +} + +LONGBOW_TEST_FIXTURE(Static) +{ + LONGBOW_RUN_TEST_CASE(Static, _parcLogEntry_Destroy); + LONGBOW_RUN_TEST_CASE(Static, _toString); +} + +LONGBOW_TEST_FIXTURE_SETUP(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Static, _parcLogEntry_Destroy) +{ + testUnimplemented(""); +} + +LONGBOW_TEST_CASE(Static, _toString) +{ + testUnimplemented(""); +} + +// Multi-threaded test + +LONGBOW_TEST_FIXTURE(MultiThreaded) +{ + LONGBOW_RUN_TEST_CASE(MultiThreaded, fgThreadTest); + LONGBOW_RUN_TEST_CASE(MultiThreaded, bgThreadTest); +} + +LONGBOW_TEST_FIXTURE_SETUP(MultiThreaded) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(MultiThreaded) +{ + if (parcSafeMemory_ReportAllocation(STDOUT_FILENO) != 0) { + //parcSafeMemory_ReportAllocation(STDOUT_FILENO); + printf("'%s' leaks memory by %u allocations\n", + longBowTestCase_GetName(testCase), parcMemory_Outstanding() - CreationInitialMemoryOutstanding); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + + +void *_runInThread(void *threadLabel); +void *_runInThread_Stripped(void *threadLabel); + +static int _loopCount = INT32_MAX; + +void * +_runInThread(void *threadLabel) // Look at _runInThread_Stripped(), below, instead of this one. +{ + PARCLogReporter *reporter = parcLogReporterTextStdout_Create(); + PARCBuffer *payload = parcBuffer_AllocateCString(threadLabel); + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + + PARCLogEntry *entry = parcLogEntry_Create(PARCLogLevel_Info, "hostName", "applicationName", "processName", _loopCount, timeStamp, payload); + + while (_loopCount > 0) { + PARCBuffer *buf = parcLogFormatText_FormatEntry(entry); + + parcLogReporterTextStdout_Report(reporter, entry); + parcBuffer_Release(&buf); + + _loopCount--; + + usleep(10 * 1000); // yield for a bit to let another thread have at it. + } + + parcLogEntry_Release(&entry); + + parcBuffer_Release(&payload); + parcLogReporter_Release(&reporter); + + return threadLabel; // Unchanged from what was passed in. +} + + +void * +_runInThread_Stripped(void *threadLabel) +{ + PARCBuffer *payload = parcBuffer_AllocateCString(threadLabel); + + while (_loopCount > 0) { + // + // The code below taken from parcLogReporterTextStdout_Report(). I stripped it down some. + // If you switch the thread's job from _runInThread_Stripped to _runInThread you can run + // the original, which shows the same thing. + // + + PARCBufferComposer *composer = parcBufferComposer_Allocate(128); + + parcBufferComposer_Format(composer, "%d [ ", _loopCount); + parcBufferComposer_PutBuffer(composer, payload); + parcBufferComposer_PutStrings(composer, " ]\n", NULL); + + PARCBuffer *result = parcBuffer_Flip(parcBuffer_Acquire(parcBufferComposer_GetBuffer(composer))); + parcBufferComposer_Release(&composer); + + char *string = parcBuffer_ToString(result); + parcBuffer_Release(&result); + + printf("%s", string); + + parcMemory_Deallocate((void **) &string); + + _loopCount--; + + usleep(10 * 1000); // yield for a bit to let another thread have at it. + } + + parcBuffer_Release(&payload); + + return threadLabel; // Unchanged from what was passed in. +} + + +LONGBOW_TEST_CASE(MultiThreaded, bgThreadTest) +{ + int numThreads = 2; + pthread_t workerThreads[numThreads]; + char *threadLabel[numThreads]; + + _loopCount = INT32_MAX; // We'll set it to 0 after a second + for (int i = 0; i < numThreads; i++) { + if (asprintf(&threadLabel[i], "bg thread #%d", i) > 0) { + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + pthread_create(&workerThreads[i], &attr, _runInThread_Stripped, threadLabel[i]); + } + //pthread_create(&workerThreads[i], &attr, _runInThread, threadLabel[i]); + } + + sleep(2); // Let the bg threads run + + _loopCount = 0; // tell the bg threads to stop + + for (int i = 0; i < numThreads; i++) { + int status = pthread_join(workerThreads[i], NULL); + printf("Child %d (out of %d) joined with status %d\n", i, numThreads, status); + free(threadLabel[i]); + } +} + +LONGBOW_TEST_CASE(MultiThreaded, fgThreadTest) +{ + _loopCount = 10; + //_runInThread("main thread"); // Run the same logging loop, but in a single thread + _runInThread_Stripped("main thread"); // Run the same logging loop, but in a single thread +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_LogEntry); + int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libparc/parc/logging/test/test_parc_LogFormatSyslog.c b/libparc/parc/logging/test/test_parc_LogFormatSyslog.c new file mode 100755 index 00000000..b5d1f4b5 --- /dev/null +++ b/libparc/parc/logging/test/test_parc_LogFormatSyslog.c @@ -0,0 +1,97 @@ +/* + * 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 Runner. +#include "../parc_LogFormatSyslog.c" + +#include <parc/logging/parc_LogEntry.h> + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(parc_LogFormatSyslog) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified here, but every test must be idempotent. + // Never rely on the execution order of tests or share state between them. + LONGBOW_RUN_TEST_FIXTURE(Global); + LONGBOW_RUN_TEST_FIXTURE(Static); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(parc_LogFormatSyslog) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(parc_LogFormatSyslog) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, parcLogFormatSyslog_FormatEntry); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Global, parcLogFormatSyslog_FormatEntry) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + + PARCBuffer *actual = parcLogFormatSyslog_FormatEntry(entry); + assertTrue(parcBuffer_Remaining(actual) > 0, "Expected formatter to return non-zero length buffer"); + parcLogEntry_Release(&entry); + parcBuffer_Release(&actual); +} + +LONGBOW_TEST_FIXTURE(Static) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_LogFormatSyslog); + int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libparc/parc/logging/test/test_parc_LogFormatText.c b/libparc/parc/logging/test/test_parc_LogFormatText.c new file mode 100644 index 00000000..0d294d36 --- /dev/null +++ b/libparc/parc/logging/test/test_parc_LogFormatText.c @@ -0,0 +1,95 @@ +/* + * 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 Runner. +#include "../parc_LogFormatText.c" + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(parc_LogFormatText) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified here, but every test must be idempotent. + // Never rely on the execution order of tests or share state between them. + LONGBOW_RUN_TEST_FIXTURE(Global); + LONGBOW_RUN_TEST_FIXTURE(Static); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(parc_LogFormatText) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(parc_LogFormatText) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, parcLogFormatText_FormatEntry); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Global, parcLogFormatText_FormatEntry) +{ + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + + PARCBuffer *actual = parcLogFormatText_FormatEntry(entry); + assertTrue(parcBuffer_Remaining(actual) > 0, "Expected formatter to return non-zero length buffer"); + parcLogEntry_Release(&entry); + parcBuffer_Release(&actual); +} + +LONGBOW_TEST_FIXTURE(Static) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_LogFormatText); + int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libparc/parc/logging/test/test_parc_LogLevel.c b/libparc/parc/logging/test/test_parc_LogLevel.c new file mode 100755 index 00000000..ca05cc5c --- /dev/null +++ b/libparc/parc/logging/test/test_parc_LogLevel.c @@ -0,0 +1,138 @@ +/* + * 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 Runner. +#include "../parc_LogLevel.c" + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(parc_LogLevel) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified here, but every test must be idempotent. + // Never rely on the execution order of tests or share state between them. + LONGBOW_RUN_TEST_FIXTURE(Global); + LONGBOW_RUN_TEST_FIXTURE(Static); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(parc_LogLevel) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(parc_LogLevel) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, parcLogLevel_Compare); + LONGBOW_RUN_TEST_CASE(Global, parcLogLevel_Equals); + LONGBOW_RUN_TEST_CASE(Global, parcLogLevel_ToString); + LONGBOW_RUN_TEST_CASE(Global, parcLogLevel_ToString_All); + LONGBOW_RUN_TEST_CASE(Global, parcLogLevel_ToString_Off); + LONGBOW_RUN_TEST_CASE(Global, parcLogLevel_FromString_Debug); + LONGBOW_RUN_TEST_CASE(Global, parcLogLevel_FromString_All); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Global, parcLogLevel_Compare) +{ + assertTrue(parcLogLevel_Compare(PARCLogLevel_Off, PARCLogLevel_All) < 0, "Expected PARCLogLevel_Off to be less that All"); +} + +LONGBOW_TEST_CASE(Global, parcLogLevel_Equals) +{ + assertTrue(parcLogLevel_Equals(PARCLogLevel_Emergency, PARCLogLevel_Emergency), "Expected equality"); + assertFalse(parcLogLevel_Equals(PARCLogLevel_Emergency, PARCLogLevel_Debug), "Expected inequality"); +} + +LONGBOW_TEST_CASE(Global, parcLogLevel_ToString) +{ + char *expected = "Debug"; + const char *actual = parcLogLevel_ToString(PARCLogLevel_Debug); + + assertTrue(strcmp(expected, actual) == 0, "Expected '%s', actual '%s'", expected, actual); +} + +LONGBOW_TEST_CASE(Global, parcLogLevel_ToString_Off) +{ + char *expected = "Off"; + const char *actual = parcLogLevel_ToString(PARCLogLevel_Off); + + assertTrue(strcmp(expected, actual) == 0, "Expected '%s', actual '%s'", expected, actual); +} + +LONGBOW_TEST_CASE(Global, parcLogLevel_ToString_All) +{ + char *expected = "All"; + const char *actual = parcLogLevel_ToString(PARCLogLevel_All); + + assertTrue(strcmp(expected, actual) == 0, "Expected '%s', actual '%s'", expected, actual); +} + +LONGBOW_TEST_CASE(Global, parcLogLevel_FromString_Debug) +{ + PARCLogLevel expected = PARCLogLevel_Debug; + PARCLogLevel actual = parcLogLevel_FromString("DEBUG"); + + assertTrue(expected == actual, "Expected '%d', actual '%d'", expected, actual); +} + +LONGBOW_TEST_CASE(Global, parcLogLevel_FromString_All) +{ + PARCLogLevel expected = PARCLogLevel_All; + PARCLogLevel actual = parcLogLevel_FromString("AlL"); + + assertTrue(expected == actual, "Expected '%d', actual '%d'", expected, actual); +} + + +LONGBOW_TEST_FIXTURE(Static) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_LogLevel); + int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libparc/parc/logging/test/test_parc_LogReporter.c b/libparc/parc/logging/test/test_parc_LogReporter.c new file mode 100644 index 00000000..5a9b0baa --- /dev/null +++ b/libparc/parc/logging/test/test_parc_LogReporter.c @@ -0,0 +1,164 @@ +/* + * 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 Runner. +#include "../parc_LogReporter.c" + +#include <LongBow/unit-test.h> + +#include <stdio.h> + +#include <parc/algol/parc_SafeMemory.h> +#include <parc/algol/parc_FileOutputStream.h> +#include <parc/logging/parc_LogReporterFile.h> +#include <parc/testing/parc_ObjectTesting.h> + +LONGBOW_TEST_RUNNER(parc_LogReporter) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified here, but every test must 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(parc_LogReporter) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +// The Test Runner calls this function once after all the Test Fixtures are run. +LONGBOW_TEST_RUNNER_TEARDOWN(parc_LogReporter) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, parcLogReporter_Create); + LONGBOW_RUN_TEST_CASE(Global, parcLogReporter_Create_NULLObject); + LONGBOW_RUN_TEST_CASE(Global, parcLogReporter_AcquireRelease); + LONGBOW_RUN_TEST_CASE(Global, parcLogReporter_GetPrivateObject); + LONGBOW_RUN_TEST_CASE(Global, parcLogReporter_Report); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + uint32_t outstandingAllocations = parcSafeMemory_ReportAllocation(STDOUT_FILENO); + if (outstandingAllocations != 0) { + printf("%s leaks %d memory allocations\n", longBowTestCase_GetName(testCase), outstandingAllocations); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Global, parcLogReporter_AcquireRelease) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *out = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(out); + + parcObjectTesting_AssertAcquireReleaseContract(parcLogReporter_Acquire, reporter); + + parcOutputStream_Release(&out); + + assertNull(out, "Expected null value."); + + parcLogReporter_Release(&reporter); +} + +LONGBOW_TEST_CASE(Global, parcLogReporter_Create) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *out = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(out); + + parcOutputStream_Release(&out); + + assertNull(out, "Expected null value."); + + parcLogReporter_Release(&reporter); +} + +void +testReporter(PARCLogReporter *reporter, const PARCLogEntry *entry) +{ +} + +LONGBOW_TEST_CASE(Global, parcLogReporter_Create_NULLObject) +{ + PARCLogReporter *reporter = parcLogReporter_Create(NULL, NULL, testReporter, NULL); + + parcLogReporter_Release(&reporter); +} + +LONGBOW_TEST_CASE(Global, parcLogReporter_GetPrivateObject) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *out = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(out); + parcOutputStream_Release(&out); + + void *instance = parcLogReporter_GetPrivateObject(reporter); + assertNotNull(instance, "Expected the instance pointer to be non-NULL"); + + parcLogReporter_Release(&reporter); +} + +LONGBOW_TEST_CASE(Global, parcLogReporter_Report) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *out = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(out); + parcOutputStream_Release(&out); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + + parcLogReporter_Report(reporter, entry); + parcLogEntry_Release(&entry); + parcBuffer_Release(&payload); + + assertNull(out, "Expected null value."); + + parcLogReporter_Release(&reporter); +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_LogReporter); + int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libparc/parc/logging/test/test_parc_LogReporterFile.c b/libparc/parc/logging/test/test_parc_LogReporterFile.c new file mode 100755 index 00000000..9739c715 --- /dev/null +++ b/libparc/parc/logging/test/test_parc_LogReporterFile.c @@ -0,0 +1,149 @@ +/* + * 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 Runner. +#include "../parc_LogReporterFile.c" + +#include <parc/algol/parc_OutputStream.h> +#include <parc/algol/parc_FileOutputStream.h> +#include <parc/testing/parc_ObjectTesting.h> + +#include <parc/algol/parc_SafeMemory.h> + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(parc_LogReporterFile) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified here, but every test must be idempotent. + // Never rely on the execution order of tests or share state between them. + LONGBOW_RUN_TEST_FIXTURE(Global); + LONGBOW_RUN_TEST_FIXTURE(Static); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(parc_LogReporterFile) +{ + 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(parc_LogReporterFile) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, parcLogReporterFile_AcquireRelease); + LONGBOW_RUN_TEST_CASE(Global, parcLogReporterFile_Create); + LONGBOW_RUN_TEST_CASE(Global, parcLogReporterFile_Report); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + uint32_t outstandingAllocations = parcSafeMemory_ReportAllocation(STDOUT_FILENO); + if (outstandingAllocations != 0) { + printf("%s leaks %d memory allocations\n", longBowTestCase_GetName(testCase), outstandingAllocations); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Global, parcLogReporterFile_Create) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *out = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(out); + parcOutputStream_Release(&out); + + assertNull(out, "Expected null value."); + + parcLogReporter_Release(&reporter); +} + +LONGBOW_TEST_CASE(Global, parcLogReporterFile_AcquireRelease) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *out = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(out); + + parcObjectTesting_AssertAcquireReleaseContract(parcLogReporterFile_Acquire, reporter); + + parcOutputStream_Release(&out); + + assertNull(out, "Expected null value."); + + parcLogReporterFile_Release(&reporter); +} + +LONGBOW_TEST_CASE(Global, parcLogReporterFile_Report) +{ + PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(dup(STDOUT_FILENO)); + PARCOutputStream *out = parcFileOutputStream_AsOutputStream(fileOutput); + parcFileOutputStream_Release(&fileOutput); + + PARCLogReporter *reporter = parcLogReporterFile_Create(out); + parcOutputStream_Release(&out); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + + parcLogReporter_Report(reporter, entry); + parcLogEntry_Release(&entry); + parcBuffer_Release(&payload); + + assertNull(out, "Expected null value."); + + parcLogReporter_Release(&reporter); +} + +LONGBOW_TEST_FIXTURE(Static) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_LogReporterFile); + int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} diff --git a/libparc/parc/logging/test/test_parc_LogReporterTextStdout.c b/libparc/parc/logging/test/test_parc_LogReporterTextStdout.c new file mode 100755 index 00000000..004fbd58 --- /dev/null +++ b/libparc/parc/logging/test/test_parc_LogReporterTextStdout.c @@ -0,0 +1,128 @@ +/* + * 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 Runner. +#include "../parc_LogReporterTextStdout.c" + +#include <stdio.h> + +#include <parc/algol/parc_OutputStream.h> +#include <parc/algol/parc_FileOutputStream.h> +#include <parc/testing/parc_ObjectTesting.h> + +#include <parc/algol/parc_SafeMemory.h> + +#include <LongBow/unit-test.h> + +LONGBOW_TEST_RUNNER(parc_LogReporterTextStdout) +{ + // The following Test Fixtures will run their corresponding Test Cases. + // Test Fixtures are run in the order specified here, but every test must be idempotent. + // Never rely on the execution order of tests or share state between them. + LONGBOW_RUN_TEST_FIXTURE(Global); + LONGBOW_RUN_TEST_FIXTURE(Static); +} + +// The Test Runner calls this function once before any Test Fixtures are run. +LONGBOW_TEST_RUNNER_SETUP(parc_LogReporterTextStdout) +{ + 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(parc_LogReporterTextStdout) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE(Global) +{ + LONGBOW_RUN_TEST_CASE(Global, parc_LogReporterTextStdout_AcquireRelease); + LONGBOW_RUN_TEST_CASE(Global, parc_LogReporterTextStdout_Create); + LONGBOW_RUN_TEST_CASE(Global, parc_LogReporterTextStdout_Report); +} + +LONGBOW_TEST_FIXTURE_SETUP(Global) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Global) +{ + uint32_t outstandingAllocations = parcSafeMemory_ReportAllocation(STDOUT_FILENO); + if (outstandingAllocations != 0) { + printf("%s leaks %d memory allocations\n", longBowTestCase_GetName(testCase), outstandingAllocations); + return LONGBOW_STATUS_MEMORYLEAK; + } + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_CASE(Global, parc_LogReporterTextStdout_Create) +{ + PARCLogReporter *reporter = parcLogReporterTextStdout_Create(); + + parcLogReporter_Release(&reporter); +} + +LONGBOW_TEST_CASE(Global, parc_LogReporterTextStdout_AcquireRelease) +{ + PARCLogReporter *reporter = parcLogReporterTextStdout_Create(); + + parcObjectTesting_AssertAcquireReleaseContract(parcLogReporterTextStdout_Acquire, reporter); + parcLogReporterTextStdout_Release(&reporter); +} + +LONGBOW_TEST_CASE(Global, parc_LogReporterTextStdout_Report) +{ + PARCLogReporter *reporter = parcLogReporterTextStdout_Create(); + + struct timeval timeStamp; + gettimeofday(&timeStamp, NULL); + PARCBuffer *payload = parcBuffer_AllocateCString("hello"); + PARCLogEntry *entry = + parcLogEntry_Create(PARCLogLevel_Info, "hostname", "applicationname", "processid", 1234, timeStamp, payload); + + parcLogReporter_Report(reporter, entry); + parcLogEntry_Release(&entry); + parcBuffer_Release(&payload); + + parcLogReporter_Release(&reporter); +} + +LONGBOW_TEST_FIXTURE(Static) +{ +} + +LONGBOW_TEST_FIXTURE_SETUP(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +LONGBOW_TEST_FIXTURE_TEARDOWN(Static) +{ + return LONGBOW_STATUS_SUCCEEDED; +} + +int +main(int argc, char *argv[argc]) +{ + LongBowRunner *testRunner = LONGBOW_TEST_RUNNER_CREATE(parc_LogReporterTextStdout); + int exitStatus = LONGBOW_TEST_MAIN(argc, argv, testRunner); + longBowTestRunner_Destroy(&testRunner); + exit(exitStatus); +} |