From f7089859d339cfe2b33ab701c02e1f424e4bd248 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Sat, 15 Oct 2022 16:52:32 +0800
Subject: [PATCH 06/43] add UT for mainloop and network

Signed-off-by: zhangxiaoyu <zhangxiaoyu58@huawei.com>
---
 test/cutils/CMakeLists.txt                    |  3 +
 test/cutils/mainloop/CMakeLists.txt           | 27 ++++++
 test/cutils/mainloop/mainloop_ut.cc           | 55 ++++++++++++
 test/cutils/utils_network/CMakeLists.txt      | 27 ++++++
 test/cutils/utils_network/utils_network_ut.cc | 89 +++++++++++++++++++
 test/cutils/utils_string/utils_string_ut.cc   | 24 +++++
 test/mocks/mainloop_mock.cc                   | 58 ++++++++++++
 test/mocks/mainloop_mock.h                    | 33 +++++++
 test/mocks/utils_network_mock.cc              | 60 +++++++++++++
 test/mocks/utils_network_mock.h               | 34 +++++++
 10 files changed, 410 insertions(+)
 create mode 100644 test/cutils/mainloop/CMakeLists.txt
 create mode 100644 test/cutils/mainloop/mainloop_ut.cc
 create mode 100644 test/cutils/utils_network/CMakeLists.txt
 create mode 100644 test/cutils/utils_network/utils_network_ut.cc
 create mode 100644 test/mocks/mainloop_mock.cc
 create mode 100644 test/mocks/mainloop_mock.h
 create mode 100644 test/mocks/utils_network_mock.cc
 create mode 100644 test/mocks/utils_network_mock.h

diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 31408f18..724f2188 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -8,15 +8,18 @@ add_library(libutils_ut STATIC
     ${local_cutils_map_srcs}
     )
 set_target_properties(libutils_ut PROPERTIES PREFIX "")
+target_link_libraries(libutils_ut ${ISULA_LIBUTILS_LIBRARY})
 target_include_directories(libutils_ut
     PUBLIC ${CMAKE_SOURCE_DIR}/src/common
     PUBLIC ${CMAKE_SOURCE_DIR}/src/utils/cutils
     PUBLIC ${CMAKE_SOURCE_DIR}/src/utils/cutils/map
 )
 
+add_subdirectory(mainloop)
 add_subdirectory(utils_string)
 add_subdirectory(utils_convert)
 add_subdirectory(utils_array)
 add_subdirectory(utils_base64)
 add_subdirectory(utils_pwgr)
 add_subdirectory(utils_namespace)
+add_subdirectory(utils_network)
diff --git a/test/cutils/mainloop/CMakeLists.txt b/test/cutils/mainloop/CMakeLists.txt
new file mode 100644
index 00000000..78e3f18d
--- /dev/null
+++ b/test/cutils/mainloop/CMakeLists.txt
@@ -0,0 +1,27 @@
+project(iSulad_UT)
+
+SET(EXE mainloop_ut)
+
+add_executable(${EXE}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/mainloop_mock.cc
+    mainloop_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+    ${GTEST_INCLUDE_DIR}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks
+    )
+
+target_link_libraries(${EXE}
+    ${GTEST_BOTH_LIBRARIES}
+    ${GMOCK_LIBRARY}
+    ${GMOCK_MAIN_LIBRARY}
+    ${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/cutils/mainloop/mainloop_ut.cc b/test/cutils/mainloop/mainloop_ut.cc
new file mode 100644
index 00000000..adba9ea1
--- /dev/null
+++ b/test/cutils/mainloop/mainloop_ut.cc
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: mainloop unit test
+ * Author: zhangxiaoyu
+ * Create: 2022-10-11
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <gtest/gtest.h>
+#include "mainloop.h"
+#include "mainloop_mock.h"
+
+using ::testing::NiceMock;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::_;
+
+class MainloopUnitTest : public testing::Test {
+protected:
+    void SetUp() override
+    {
+        Mainloop_SetMock(&m_mainloop_mock);
+        EXPECT_CALL(m_mainloop_mock, Close(_)).WillRepeatedly(Return(0));
+        EXPECT_CALL(m_mainloop_mock, EpollCreate1(_)).WillRepeatedly(Return(0));
+        EXPECT_CALL(m_mainloop_mock, EpollCtl(_, _, _, _)).WillRepeatedly(Return(0));
+        EXPECT_CALL(m_mainloop_mock, EpollWait(_, _, _, _)).WillRepeatedly(Return(0));
+    }
+
+    void TearDown() override
+    {
+        Mainloop_SetMock(nullptr);
+    }
+
+    NiceMock<MockMainloop> m_mainloop_mock;
+};
+
+TEST_F(MainloopUnitTest, test_mainloop)
+{
+    struct epoll_descr descr = { 0 };
+
+    ASSERT_EQ(epoll_loop_open(&descr), 0);
+    ASSERT_EQ(epoll_loop_add_handler(&descr, 111, nullptr, nullptr), 0);
+    ASSERT_EQ(epoll_loop(&descr, -1), 0);
+    ASSERT_EQ(epoll_loop_del_handler(&descr, 111), 0);
+    ASSERT_EQ(epoll_loop_close(&descr), 0);
+}
diff --git a/test/cutils/utils_network/CMakeLists.txt b/test/cutils/utils_network/CMakeLists.txt
new file mode 100644
index 00000000..7e2c84e7
--- /dev/null
+++ b/test/cutils/utils_network/CMakeLists.txt
@@ -0,0 +1,27 @@
+project(iSulad_UT)
+
+SET(EXE utils_network_ut)
+
+add_executable(${EXE}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks/utils_network_mock.cc
+    utils_network_ut.cc)
+
+target_include_directories(${EXE} PUBLIC
+    ${GTEST_INCLUDE_DIR}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../include
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/common
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils/map
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/sha256
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../src/utils/cutils
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../../test/mocks
+    )
+
+target_link_libraries(${EXE}
+    ${GTEST_BOTH_LIBRARIES}
+    ${GMOCK_LIBRARY}
+    ${GMOCK_MAIN_LIBRARY}
+    ${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/cutils/utils_network/utils_network_ut.cc b/test/cutils/utils_network/utils_network_ut.cc
new file mode 100644
index 00000000..532fd780
--- /dev/null
+++ b/test/cutils/utils_network/utils_network_ut.cc
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Description: utils_network unit test
+ * Author: zhangxiaoyu
+ * Create: 2022-10-11
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <gtest/gtest.h>
+#include "utils.h"
+#include "utils_network.h"
+#include "utils_network_mock.h"
+
+using ::testing::NiceMock;
+using ::testing::Invoke;
+using ::testing::Return;
+using ::testing::_;
+
+std::string GetLocalPath()
+{
+    char abs_path[PATH_MAX] { 0x00 };
+    int ret = readlink("/proc/self/exe", abs_path, sizeof(abs_path));
+    if (ret < 0 || static_cast<size_t>(ret) >= sizeof(abs_path)) {
+        return "";
+    }
+
+    for (int i { ret }; i >= 0; --i) {
+        if (abs_path[i] == '/') {
+            abs_path[i + 1] = '\0';
+            break;
+        }
+    }
+
+    return static_cast<std::string>(abs_path);
+}
+
+class UtilsNetworkUnitTest : public testing::Test {
+protected:
+    void SetUp() override
+    {
+        UtilsNetwork_SetMock(&m_utils_network_mock);
+        EXPECT_CALL(m_utils_network_mock, Mount(_, _, _, _, _)).WillRepeatedly(Return(0));
+        EXPECT_CALL(m_utils_network_mock, Umount2(_, _)).WillRepeatedly(Invoke(invokeUmont2));
+
+        EXPECT_CALL(m_utils_network_mock, PthreadCreate(_, _, _, _)).WillRepeatedly(Return(0));
+        EXPECT_CALL(m_utils_network_mock, PthreadJoin(_, _)).WillRepeatedly(Invoke(invokePthreadJoin));
+    }
+
+    void TearDown() override
+    {
+        UtilsNetwork_SetMock(nullptr);
+    }
+
+    NiceMock<MockUtilsNetwork> m_utils_network_mock;
+
+    static int invokeUmont2(const char *target, int flags)
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    static int invokePthreadJoin(pthread_t thread, void **retval)
+    {
+        void *status = (void *)calloc(1, sizeof(int));
+        *retval = status;
+
+        return 0;
+    }
+};
+
+TEST_F(UtilsNetworkUnitTest, test_network_namespace)
+{
+    int err = 0;
+    std::string netNS = GetLocalPath() + "test_namespace";
+
+    ASSERT_EQ(util_create_netns_file(netNS.c_str()), 0);
+    ASSERT_EQ(util_mount_namespace(netNS.c_str()), 0);
+    ASSERT_EQ(util_umount_namespace(netNS.c_str()), 0);
+    ASSERT_EQ(util_force_remove_file(netNS.c_str(), &err), true);
+}
diff --git a/test/cutils/utils_string/utils_string_ut.cc b/test/cutils/utils_string/utils_string_ut.cc
index 0cac212f..b488a09f 100644
--- a/test/cutils/utils_string/utils_string_ut.cc
+++ b/test/cutils/utils_string/utils_string_ut.cc
@@ -807,3 +807,27 @@ TEST(utils_string_ut, test_parse_percent_string)
     ret = util_parse_percent_string(wrong6, &converted);
     ASSERT_NE(ret, 0);
 }
+
+TEST(utils_string_ut, test_str_token)
+{
+    char *token = nullptr;
+    char *string = (char *)"abc:def:gh";
+    char *tmp = string;
+
+    token = util_str_token(&tmp, nullptr);
+    ASSERT_STREQ(token, nullptr);
+    token = util_str_token(nullptr, ":");
+    ASSERT_STREQ(token, nullptr);
+
+    token = util_str_token(&tmp, ",");
+    ASSERT_STREQ(tmp, nullptr);
+    ASSERT_STREQ(token, string);
+    tmp = string;
+    free(token);
+    token = nullptr;
+
+    token = util_str_token(&tmp, ":");
+    ASSERT_STREQ(tmp, "def:gh");
+    ASSERT_STREQ(token, "abc");
+    free(token);
+}
diff --git a/test/mocks/mainloop_mock.cc b/test/mocks/mainloop_mock.cc
new file mode 100644
index 00000000..508c957b
--- /dev/null
+++ b/test/mocks/mainloop_mock.cc
@@ -0,0 +1,58 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2022-10-13
+ * Description: provide mainloop mock
+ ******************************************************************************/
+
+#include "mainloop_mock.h"
+
+namespace {
+MockMainloop *g_mainloop_mock = nullptr;
+}
+
+void Mainloop_SetMock(MockMainloop* mock)
+{
+    g_mainloop_mock = mock;
+}
+
+int close(int fd)
+{
+    if (g_mainloop_mock != nullptr) {
+        return g_mainloop_mock->Close(fd);
+    }
+    return 0;
+}
+
+int epoll_create1(int flags)
+{
+    std::cout << "epoll_create1" << std::endl;
+    if (g_mainloop_mock != nullptr) {
+        return g_mainloop_mock->EpollCreate1(flags);
+    }
+    return 0;
+}
+
+int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
+{
+    if (g_mainloop_mock != nullptr) {
+        return g_mainloop_mock->EpollCtl(epfd, op, fd, event);
+    }
+    return 0;
+}
+
+int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
+{
+    if (g_mainloop_mock != nullptr) {
+        return g_mainloop_mock->EpollWait(epfd, events, maxevents, timeout);
+    }
+    return 0;
+}
diff --git a/test/mocks/mainloop_mock.h b/test/mocks/mainloop_mock.h
new file mode 100644
index 00000000..aab16d12
--- /dev/null
+++ b/test/mocks/mainloop_mock.h
@@ -0,0 +1,33 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2022-10-13
+ * Description: mainloop mock
+ ******************************************************************************/
+
+#ifndef _ISULAD_TEST_MOCKS_MAINLOOP_MOCK_H
+#define _ISULAD_TEST_MOCKS_MAINLOOP_MOCK_H
+
+#include <gmock/gmock.h>
+#include <sys/epoll.h>
+
+class MockMainloop {
+public:
+    virtual ~MockMainloop() = default;
+    MOCK_METHOD1(Close, int(int));
+    MOCK_METHOD1(EpollCreate1, int(int));
+    MOCK_METHOD4(EpollCtl, int(int, int, int, struct epoll_event *));
+    MOCK_METHOD4(EpollWait, int(int, struct epoll_event *, int, int));
+};
+
+void Mainloop_SetMock(MockMainloop* mock);
+
+#endif // _ISULAD_TEST_MOCKS_MAINLOOP_MOCK_H
diff --git a/test/mocks/utils_network_mock.cc b/test/mocks/utils_network_mock.cc
new file mode 100644
index 00000000..afa346b5
--- /dev/null
+++ b/test/mocks/utils_network_mock.cc
@@ -0,0 +1,60 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2022-10-15
+ * Description: provide utils_network mock
+ ******************************************************************************/
+
+#include "utils_network_mock.h"
+
+namespace {
+MockUtilsNetwork *g_utils_network_mock = nullptr;
+}
+
+void UtilsNetwork_SetMock(MockUtilsNetwork* mock)
+{
+    g_utils_network_mock = mock;
+}
+
+int mount(const char *source, const char *target, const char *filesystemtype,
+          unsigned long mountflags, const void *data)
+{
+    if (g_utils_network_mock != nullptr) {
+        return g_utils_network_mock->Mount(source, target, filesystemtype, mountflags, data);
+    }
+    return 0;
+}
+
+int umount2(const char *target, int flags)
+{
+    if (g_utils_network_mock != nullptr) {
+        return g_utils_network_mock->Umount2(target, flags);
+    }
+    return 0;
+}
+
+int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *),
+                   void *arg)
+{
+    if (g_utils_network_mock != nullptr) {
+        return g_utils_network_mock->PthreadCreate(thread, attr, start_routine, arg);
+    }
+    return 0;
+}
+
+
+int pthread_join(pthread_t thread, void **retval)
+{
+    if (g_utils_network_mock != nullptr) {
+        return g_utils_network_mock->PthreadJoin(thread, retval);
+    }
+    return 0;
+}
\ No newline at end of file
diff --git a/test/mocks/utils_network_mock.h b/test/mocks/utils_network_mock.h
new file mode 100644
index 00000000..fcae5664
--- /dev/null
+++ b/test/mocks/utils_network_mock.h
@@ -0,0 +1,34 @@
+/******************************************************************************
+ * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved.
+ * iSulad licensed under the Mulan PSL v2.
+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
+ * You may obtain a copy of Mulan PSL v2 at:
+ *     http://license.coscl.org.cn/MulanPSL2
+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+ * PURPOSE.
+ * See the Mulan PSL v2 for more details.
+ * Author: zhangxiaoyu
+ * Create: 2022-10-15
+ * Description: utils_network mock
+ ******************************************************************************/
+
+#ifndef _ISULAD_TEST_MOCKS_UTILS_NETWORK_MOCK_H
+#define _ISULAD_TEST_MOCKS_UTILS_NETWORK_MOCK_H
+
+#include <gmock/gmock.h>
+#include <sys/mount.h>
+#include <pthread.h>
+
+class MockUtilsNetwork {
+public:
+    virtual ~MockUtilsNetwork() = default;
+    MOCK_METHOD5(Mount, int(const char *, const char *, const char *, unsigned long, const void *));
+    MOCK_METHOD2(Umount2, int(const char *, int));
+    MOCK_METHOD4(PthreadCreate, int(pthread_t *, const pthread_attr_t *, void *(*start_routine)(void *), void *));
+    MOCK_METHOD2(PthreadJoin, int(pthread_t, void **));
+};
+
+void UtilsNetwork_SetMock(MockUtilsNetwork* mock);
+
+#endif // _ISULAD_TEST_MOCKS_UTILS_NETWORK_MOCK_H
-- 
2.25.1

