Add unit-test for dpl old_style_log_provider 63/314163/11
authortran.tung <tran.tung@samsung.com>
Mon, 8 Jul 2024 03:32:24 +0000 (10:32 +0700)
committerDariusz Michaluk <d.michaluk@samsung.com>
Mon, 15 Jul 2024 10:37:32 +0000 (10:37 +0000)
Change-Id: Ie11f200d654bab48248a168710021abb1a48b938
Signed-off-by: tran.tung <tran.tung@samsung.com>
unit-tests/CMakeLists.txt
unit-tests/test_dpl_old_style_log_provider.cpp [new file with mode: 0644]

index e98d594d7369139cd1825e2f5ebfee1d0d09826b..65a1efcfa5b84d9081acad115be2b79f38e33880 100644 (file)
@@ -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 (file)
index 0000000..96792c3
--- /dev/null
@@ -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 <string>
+#include <iostream>
+#include <unistd.h>
+#include <fcntl.h>
+
+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<LEVEL>(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()