From 04c7beb8788826063c19715b58e11c4eea7efbe6 Mon Sep 17 00:00:00 2001
From: "Neil.wrz" <wangrunze13@huawei.com>
Date: Wed, 30 Nov 2022 23:54:47 -0800
Subject: [PATCH 52/54] add console ut

Signed-off-by: Neil.wrz <wangrunze13@huawei.com>
---
 test/CMakeLists.txt         |   1 +
 test/console/CMakeLists.txt |  20 +++++++
 test/console/console_ut.cc  | 107 ++++++++++++++++++++++++++++++++++++
 3 files changed, 128 insertions(+)
 create mode 100644 test/console/CMakeLists.txt
 create mode 100644 test/console/console_ut.cc

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 8b927f91..06adb602 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -46,6 +46,7 @@ IF(ENABLE_UT)
     add_subdirectory(services)
     add_subdirectory(sha256)
     add_subdirectory(buffer)
+    add_subdirectory(console)
 ENDIF(ENABLE_UT)
 
 IF(ENABLE_FUZZ)
diff --git a/test/console/CMakeLists.txt b/test/console/CMakeLists.txt
new file mode 100644
index 00000000..acadc620
--- /dev/null
+++ b/test/console/CMakeLists.txt
@@ -0,0 +1,20 @@
+project(iSulad_UT)
+
+SET(EXE console_ut)
+
+add_executable(${EXE}
+    ${CMAKE_SOURCE_DIR}/src/utils/console/console.c
+    console_ut.cc)
+
+
+target_include_directories(${EXE} PUBLIC
+    ${GTEST_INCLUDE_DIR}
+    ${CMAKE_SOURCE_DIR}/src/utils/console
+    ${CMAKE_SOURCE_DIR}/src/utils/cutils
+    ${CMAKE_SOURCE_DIR}/src/common
+    )
+
+target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${ISULA_LIBUTILS_LIBRARY} libutils_ut -lcrypto -lyajl -lz)
+add_test(NAME ${EXE} COMMAND ${EXE} --gtest_output=xml:${EXE}-Results.xml)
+
+
diff --git a/test/console/console_ut.cc b/test/console/console_ut.cc
new file mode 100644
index 00000000..73479000
--- /dev/null
+++ b/test/console/console_ut.cc
@@ -0,0 +1,107 @@
+#include <sys/stat.h>
+#include <gtest/gtest.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <string.h>
+
+#include "console.h"
+
+#define FIFO_NAME "fifo1"
+#define PATH_NOT_EXIST "./path_not_found/"
+#define LONGER_PATH_MAX 4098
+
+TEST(utils_console, test_console_fifo_create)
+{
+    int ret = 0;
+    struct stat buf;
+
+    ret = console_fifo_create(FIFO_NAME);
+    if (ret != 0) {
+        return;
+    }
+
+    if (stat(FIFO_NAME, &buf) < 0) {
+        return;
+    }
+
+    ASSERT_EQ(S_ISFIFO(buf.st_mode), true);
+
+    ret = access(FIFO_NAME, R_OK|W_OK);
+    ASSERT_EQ(ret, 0);
+
+    remove(FIFO_NAME);
+}
+
+TEST(utils_console, test_console_fifo_create_failed)
+{
+    int ret = 0;
+
+    ret = console_fifo_create(PATH_NOT_EXIST FIFO_NAME);
+    ASSERT_EQ(ret, -1);
+}
+
+TEST(utils_console, test_console_fifo_delete)
+{
+    int ret = 0;
+    char path_buf[LONGER_PATH_MAX] = { 0x00 };
+
+    memset(path_buf, 'a', LONGER_PATH_MAX);
+    path_buf[LONGER_PATH_MAX - 1] = 0;
+    ASSERT_EQ(strlen(path_buf), LONGER_PATH_MAX-1)<< "strlen is " << strlen(path_buf);
+
+    ret = console_fifo_create(FIFO_NAME);
+    if (ret != 0) {
+        return;
+    }
+
+    // PATH TOO LONG
+    ret = console_fifo_delete(path_buf);
+    ASSERT_EQ(ret, -1) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+
+    // PATH NULL
+    ret = console_fifo_delete(NULL);
+    ASSERT_EQ(ret, -1) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+
+    // PATH LEN IS ZERO
+    ret = console_fifo_delete("");
+    ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+
+    // PATH NOT FOUND
+    ret = console_fifo_delete(PATH_NOT_EXIST FIFO_NAME);
+    ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+
+    ret = console_fifo_delete(FIFO_NAME);
+    ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+}
+
+TEST(utils_console, test_console_fifo_open)
+{
+    int ret = 0;
+    int fifooutfd = -1;
+
+    ret = console_fifo_create(FIFO_NAME);
+    if (ret != 0) {
+        return;
+    }
+
+    ret = console_fifo_open(FIFO_NAME, &fifooutfd, O_RDWR | O_NONBLOCK);
+    ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+    console_fifo_close(fifooutfd);
+    remove(FIFO_NAME);
+}
+
+TEST(utils_console, test_console_fifo_open_withlock)
+{
+    int ret = 0;
+    int fifooutfd = -1;
+
+    ret = console_fifo_create(FIFO_NAME);
+    if (ret != 0) {
+        return;
+    }
+
+    ret = console_fifo_open_withlock(FIFO_NAME, &fifooutfd, O_RDWR | O_NONBLOCK);
+    ASSERT_EQ(ret, 0) << []()->std::string { remove(FIFO_NAME); return "failed"; }();
+    console_fifo_close(fifooutfd);
+    remove(FIFO_NAME);
+}
-- 
2.25.1

