From 3909ddbc369c69202308e77beda6553b7d95d79b Mon Sep 17 00:00:00 2001
From: haozi007 <liuhao27@huawei.com>
Date: Sat, 15 Oct 2022 15:04:56 +0800
Subject: [PATCH 08/43] add ut for cutils aes

Signed-off-by: haozi007 <liuhao27@huawei.com>
---
 test/CMakeLists.txt                   |  1 +
 test/cutils/CMakeLists.txt            |  1 +
 test/cutils/utils_aes/CMakeLists.txt  | 16 ++++++
 test/cutils/utils_aes/utils_aes_ut.cc | 74 +++++++++++++++++++++++++++
 4 files changed, 92 insertions(+)
 create mode 100644 test/cutils/utils_aes/CMakeLists.txt
 create mode 100644 test/cutils/utils_aes/utils_aes_ut.cc

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index a36f68c5..034aaf97 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -62,6 +62,7 @@ IF(ENABLE_COVERAGE)
 
         COMMAND lcov --directory . --capture --output-file coverage.info
         COMMAND lcov -a coverage.base -a coverage.info --output-file coverage.total
+        COMMAND lcov --remove coverage.total '/usr/include/*' --output-file coverage.total
         COMMAND lcov --remove coverage.total ${COVERAGE_EXCLUDES} --output-file ${PROJECT_BINARY_DIR}/coverage.info.cleaned
         COMMAND genhtml -o coverage ${PROJECT_BINARY_DIR}/coverage.info.cleaned
         COMMAND ${CMAKE_COMMAND} -E remove coverage.base coverage.total ${PROJECT_BINARY_DIR}/coverage.info.cleaned
diff --git a/test/cutils/CMakeLists.txt b/test/cutils/CMakeLists.txt
index 724f2188..93c73fb8 100644
--- a/test/cutils/CMakeLists.txt
+++ b/test/cutils/CMakeLists.txt
@@ -23,3 +23,4 @@ add_subdirectory(utils_base64)
 add_subdirectory(utils_pwgr)
 add_subdirectory(utils_namespace)
 add_subdirectory(utils_network)
+add_subdirectory(utils_aes)
diff --git a/test/cutils/utils_aes/CMakeLists.txt b/test/cutils/utils_aes/CMakeLists.txt
new file mode 100644
index 00000000..f7535bb3
--- /dev/null
+++ b/test/cutils/utils_aes/CMakeLists.txt
@@ -0,0 +1,16 @@
+project(iSulad_UT)
+
+SET(EXE utils_aes_ut)
+
+add_executable(${EXE}
+    utils_aes_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_aes/utils_aes_ut.cc b/test/cutils/utils_aes/utils_aes_ut.cc
new file mode 100644
index 00000000..e564428d
--- /dev/null
+++ b/test/cutils/utils_aes/utils_aes_ut.cc
@@ -0,0 +1,74 @@
+/******************************************************************************
+ * 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: haozi007
+ * Create: 2022-10-13
+ * Description: utils namespace unit test
+ *******************************************************************************/
+
+#include <gtest/gtest.h>
+#include "utils_aes.h"
+
+TEST(utils_aes, test_util_aes_key)
+{
+    std::string key_file = "./aes_key";
+    unsigned char key_val[AES_256_CFB_KEY_LEN] = { 0 };
+
+    unlink(key_file.c_str());
+    ASSERT_NE(util_aes_key(key_file.c_str(), false, key_val), 0);
+    ASSERT_NE(util_aes_key(nullptr, true, key_val), 0);
+    ASSERT_NE(util_aes_key(nullptr, false, key_val), 0);
+    ASSERT_NE(util_aes_key(key_file.c_str(), true, nullptr), 0);
+    ASSERT_NE(util_aes_key(key_file.c_str(), false, nullptr), 0);
+    ASSERT_NE(util_aes_key(nullptr, true, nullptr), 0);
+    ASSERT_NE(util_aes_key(nullptr, false, nullptr), 0);
+}
+
+TEST(utils_aes, test_util_aes_encode)
+{
+    std::string key_file = "./aes_key";
+    unsigned char key_val[AES_256_CFB_KEY_LEN] = { 0 };
+    std::string test_data = "hello world";
+    unsigned char *out = nullptr;
+
+    ASSERT_EQ(util_aes_key(key_file.c_str(), true, key_val), 0);
+    ASSERT_EQ(util_aes_encode(key_val, (unsigned char *)test_data.c_str(), test_data.size(), &out), 0);
+
+    ASSERT_NE(util_aes_encode(nullptr, (unsigned char *)test_data.c_str(), test_data.size(), &out), 0);
+    ASSERT_NE(util_aes_encode(key_val, nullptr, 0, &out), 0);
+    ASSERT_NE(util_aes_encode(key_val, (unsigned char *)test_data.c_str(), 0, &out), 0);
+    ASSERT_NE(util_aes_encode(key_val, (unsigned char *)test_data.c_str(), test_data.size(), nullptr), 0);
+
+    unlink(key_file.c_str());
+}
+
+TEST(utils_aes, test_util_aes_decode)
+{
+    std::string key_file = "./aes_key";
+    unsigned char key_val[AES_256_CFB_KEY_LEN] = { 0 };
+    std::string test_data = "hello world";
+    unsigned char *encode_data = nullptr;
+    unsigned char *decode_data = nullptr;
+    size_t aes_len = AES_256_CFB_IV_LEN;
+
+    ASSERT_EQ(util_aes_key(key_file.c_str(), true, key_val), 0);
+    ASSERT_EQ(util_aes_encode(key_val, (unsigned char *)test_data.c_str(), test_data.size(), &encode_data), 0);
+    aes_len += test_data.size();
+    ASSERT_EQ(util_aes_decode(key_val, encode_data, aes_len, &decode_data), 0);
+    printf("get decode value = %s\n", (const char *)decode_data);
+    ASSERT_EQ(strcmp(test_data.c_str(), (const char *)decode_data), 0);
+
+    ASSERT_NE(util_aes_decode(nullptr, encode_data, aes_len, &decode_data), 0);
+    ASSERT_NE(util_aes_decode(key_val, nullptr, 0, &decode_data), 0);
+    ASSERT_NE(util_aes_decode(key_val, encode_data, 0, &decode_data), 0);
+    ASSERT_NE(util_aes_decode(key_val, encode_data, aes_len, nullptr), 0);
+
+    unlink(key_file.c_str());
+}
\ No newline at end of file
-- 
2.25.1

