From 57b0b30499c39c1626503f67189dad4f528985bd Mon Sep 17 00:00:00 2001 From: "tran.tung" Date: Mon, 8 Jul 2024 10:32:24 +0700 Subject: [PATCH] Add unit-test for dpl old_style_log_provider Change-Id: Ie11f200d654bab48248a168710021abb1a48b938 Signed-off-by: tran.tung --- unit-tests/CMakeLists.txt | 3 +- unit-tests/test_dpl_old_style_log_provider.cpp | 109 +++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 unit-tests/test_dpl_old_style_log_provider.cpp diff --git a/unit-tests/CMakeLists.txt b/unit-tests/CMakeLists.txt index e98d594..65a1efc 100644 --- a/unit-tests/CMakeLists.txt +++ b/unit-tests/CMakeLists.txt @@ -61,6 +61,8 @@ SET(UNIT_TESTS_SOURCES test_main.cpp test_constant.cpp test_common.cpp + test_dpl_journal_log_provider.cpp + test_dpl_old_style_log_provider.cpp test_vcore_base64.cpp test_vcore_cert_store_type.cpp test_vcore_api.cpp @@ -73,7 +75,6 @@ SET(UNIT_TESTS_SOURCES test_vcore_signature_reader.cpp test_cert_server_db.cpp test_cert_server_logic.cpp - test_dpl_journal_log_provider.cpp colour_log_formatter.cpp ${PROJECT_SOURCE_DIR}/src/server/src/cert-server-logic.c ${PROJECT_SOURCE_DIR}/src/server/src/cert-server-db.c diff --git a/unit-tests/test_dpl_old_style_log_provider.cpp b/unit-tests/test_dpl_old_style_log_provider.cpp new file mode 100644 index 0000000..96792c3 --- /dev/null +++ b/unit-tests/test_dpl_old_style_log_provider.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 "test_macros.h" +#include "dpl/log/old_style_log_provider.h" +#include +#include +#include +#include + +using namespace VcoreDPL; +using namespace Log; +using namespace std; + +#define LEVEL AbstractLogProvider::LogLevel + +string getLogFromCmd( + LEVEL level, + const char *message, + int line, + const char *function) +{ + OldStyleLogProvider *oldStyleLogProvider = new OldStyleLogProvider(); + + int pipefd[2]; + + int ret = pipe(pipefd); + if (ret == -1) + return ""; + + int saved_stdout = dup(STDOUT_FILENO); + + dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[1]); + + oldStyleLogProvider->Log(level, message, __FILE__, line, function); + + dup2(saved_stdout, STDOUT_FILENO); + close(saved_stdout); + + char buffer[1 << 10]; + ssize_t count = read(pipefd[0], buffer, sizeof(buffer) - 1); + buffer[count] = '\0'; + + close(pipefd[0]); + + string output(buffer); + + delete oldStyleLogProvider; + + return output; +} + +BOOST_AUTO_TEST_SUITE(DPL_OLD_STYLE_LOG_PROVIDER_TEST) + +POSITIVE_TEST_CASE(T_old_style_log_provider_log) +{ + const char *str = "ABCDEF"; + string message = getLogFromCmd(LEVEL::Error, str, 1, "Log"); + + BOOST_CHECK(message.find(str) != string::npos); + + const char *str1 = "FUNCTION_TEST_AAA"; + message = getLogFromCmd(LEVEL::Warning, NULL, 1, str1); + + BOOST_CHECK(message.find(str1) != string::npos); + + const char *str2 = "454sfscx"; + message = getLogFromCmd(LEVEL::Info, str2, 1, "Log"); + + BOOST_CHECK(message.find(str2) != string::npos); + + const char *str3 = "789dfrs"; + message = getLogFromCmd(LEVEL::Debug, str3, -1, "Log"); + + BOOST_CHECK(message.find(str3) != string::npos); + + const char *str4 = "798fdsg"; + message = getLogFromCmd(LEVEL::Pedantic, str4, 1, "Log"); + + BOOST_CHECK(message.find(str4) != string::npos); +} + +NEGATIVE_TEST_CASE(T_old_style_log_provider_log_out_of_range_level) +{ + string message = getLogFromCmd(LEVEL::None, "This is a type none log", 1, "Log"); + + BOOST_CHECK(message.find("Unsupported log level: 0") != string::npos); + + LEVEL Level = static_cast(1 << 3); + message = getLogFromCmd(Level, "This is a type error log", 1, "Log"); + + BOOST_CHECK(message.find("Unsupported log level: 8") != string::npos); +} + +BOOST_AUTO_TEST_SUITE_END() -- 2.7.4