From 864a500f18d56aebeaa71960f10791386212b18b Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Thu, 13 Oct 2022 15:55:08 +0800
Subject: [PATCH 02/43] add namespace util UT

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 test/cutils/CMakeLists.txt                    | 15 ++++
 test/cutils/utils_namespace/CMakeLists.txt    | 16 ++++
 .../utils_namespace/utils_namespace_ut.cc     | 82 +++++++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 test/cutils/utils_namespace/CMakeLists.txt
 create mode 100644 test/cutils/utils_namespace/utils_namespace_ut.cc

diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index b549f844..31408f18 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -1,7 +1,22 @@
 project(iSulad_UT)
 
+aux_source_directory(${CMAKE_SOURCE_DIR}/src/utils/cutils local_cutils_srcs)
+aux_source_directory(${CMAKE_SOURCE_DIR}/src/utils/cutils/map local_cutils_map_srcs)
+
+add_library(libutils_ut STATIC
+    ${local_cutils_srcs}
+    ${local_cutils_map_srcs}
+    )
+set_target_properties(libutils_ut PROPERTIES PREFIX "")
+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(utils_string)
 add_subdirectory(utils_convert)
 add_subdirectory(utils_array)
 add_subdirectory(utils_base64)
 add_subdirectory(utils_pwgr)
+add_subdirectory(utils_namespace)
diff --git a/test/cutils/utils_namespace/CMakeLists.txt b/test/cutils/utils_namespace/CMakeLists.txt
new file mode 100644
index 00000000..8add4a71
--- /dev/null
+++ b/test/cutils/utils_namespace/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_namespace_ut)
+
+add_executable(${EXE}
+    utils_namespace_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/cutils
+    )
+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/cutils/utils_namespace/utils_namespace_ut.cc b/test/cutils/utils_namespace/utils_namespace_ut.cc
new file mode 100644
index 00000000..da50d503
--- /dev/null
+++ b/test/cutils/utils_namespace/utils_namespace_ut.cc
@@ -0,0 +1,82 @@
+/******************************************************************************
+ * 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.
+ * Author: hejunjie
+ * Create: 2022-04-08
+ * Description: utils_pwgr unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "namespace.h"
+
+TEST(utils_namespace, test_namespace_is_host)
+{
+    ASSERT_EQ(namespace_is_host(SHARE_NAMESPACE_HOST), true);
+    ASSERT_EQ(namespace_is_host(SHARE_NAMESPACE_NONE), false);
+    ASSERT_EQ(namespace_is_host(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_none)
+{
+    ASSERT_EQ(namespace_is_none(SHARE_NAMESPACE_HOST), false);
+    ASSERT_EQ(namespace_is_none(SHARE_NAMESPACE_NONE), true);
+    ASSERT_EQ(namespace_is_none(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_container)
+{
+    std::string con = "container:test";
+    ASSERT_EQ(namespace_is_container(SHARE_NAMESPACE_HOST), false);
+    ASSERT_EQ(namespace_is_container(con.c_str()), true);
+    ASSERT_EQ(namespace_is_container(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_bridge)
+{
+    ASSERT_EQ(namespace_is_bridge(SHARE_NAMESPACE_HOST), false);
+    ASSERT_EQ(namespace_is_bridge(SHARE_NAMESPACE_BRIDGE), true);
+    ASSERT_EQ(namespace_is_bridge(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_file)
+{
+    ASSERT_EQ(namespace_is_file(SHARE_NAMESPACE_HOST), false);
+    ASSERT_EQ(namespace_is_file(SHARE_NAMESPACE_FILE), true);
+    ASSERT_EQ(namespace_is_file(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_is_shareable)
+{
+    ASSERT_EQ(namespace_is_shareable(SHARE_NAMESPACE_HOST), false);
+    ASSERT_EQ(namespace_is_shareable(SHARE_NAMESPACE_SHAREABLE), true);
+    ASSERT_EQ(namespace_is_shareable(nullptr), false);
+}
+
+TEST(utils_namespace, test_namespace_get_connected_container)
+{
+    std::string con = "container:test";
+    char *ret = nullptr;
+    ret = namespace_get_connected_container(con.c_str());
+    ASSERT_STREQ(ret, "test");
+    ASSERT_EQ(namespace_get_connected_container(SHARE_NAMESPACE_SHAREABLE), nullptr);
+    ASSERT_EQ(namespace_get_connected_container(nullptr), nullptr);
+}
+
+TEST(utils_namespace, test_namespace_get_host_namespace_path)
+{
+    ASSERT_EQ(namespace_get_host_namespace_path(nullptr), nullptr);
+    ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_PID), SHARE_NAMESPACE_PID_HOST_PATH);
+    ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_NETWORK), SHARE_NAMESPACE_NET_HOST_PATH);
+    ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_IPC), SHARE_NAMESPACE_IPC_HOST_PATH);
+    ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_UTS), SHARE_NAMESPACE_UTS_HOST_PATH);
+    ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_MOUNT), SHARE_NAMESPACE_MNT_HOST_PATH);
+    ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_USER), SHARE_NAMESPACE_USER_HOST_PATH);
+    ASSERT_STREQ(namespace_get_host_namespace_path(TYPE_NAMESPACE_CGROUP), SHARE_NAMESPACE_CGROUP_HOST_PATH);
+}
\ No newline at end of file
-- 
2.25.1

