From afad1f4da9a5411280e094e121cba18180d60958 Mon Sep 17 00:00:00 2001
From: chengzrz <czrzrichard@gmail.com>
Date: Fri, 25 Nov 2022 17:15:22 +0800
Subject: [PATCH 50/54] add unit test for buffer

Signed-off-by: chengzrz <czrzrichard@gmail.com>
---
 test/CMakeLists.txt        |  1 +
 test/buffer/CMakeLists.txt | 17 ++++++++
 test/buffer/buffer_ut.cc   | 89 ++++++++++++++++++++++++++++++++++++++
 test/tar/CMakeLists.txt    | 18 ++++++++
 test/tar/tar_ut.cc         |  0
 5 files changed, 125 insertions(+)
 create mode 100644 test/buffer/CMakeLists.txt
 create mode 100644 test/buffer/buffer_ut.cc
 create mode 100644 test/tar/CMakeLists.txt
 create mode 100644 test/tar/tar_ut.cc

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 27201100..8b927f91 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -45,6 +45,7 @@ IF(ENABLE_UT)
     add_subdirectory(specs)
     add_subdirectory(services)
     add_subdirectory(sha256)
+    add_subdirectory(buffer)
 ENDIF(ENABLE_UT)
 
 IF(ENABLE_FUZZ)
diff --git a/test/buffer/CMakeLists.txt b/test/buffer/CMakeLists.txt
new file mode 100644
index 00000000..f900b592
--- /dev/null
+++ b/test/buffer/CMakeLists.txt
@@ -0,0 +1,17 @@
+project(iSulad_UT)
+
+SET(EXE buffer_ut)
+
+add_executable(${EXE}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/buffer/buffer.c
+    buffer_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/buffer
+    )
+
+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/buffer/buffer_ut.cc b/test/buffer/buffer_ut.cc
new file mode 100644
index 00000000..9c5630e0
--- /dev/null
+++ b/test/buffer/buffer_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: buffer unit test
+ * Author: chengzeruizhi
+ * Create: 2022-11-29
+ */
+
+#include <gtest/gtest.h>
+
+#include "buffer.h"
+
+TEST(buffer, test_buffer_alloc)
+{
+    Buffer *buffer = buffer_alloc(0);
+    EXPECT_EQ(buffer, nullptr);
+
+    buffer = buffer_alloc(-1);
+    EXPECT_EQ(buffer, nullptr);
+
+    buffer = buffer_alloc(SIZE_MAX + 1);
+    EXPECT_EQ(buffer, nullptr);
+
+    buffer = buffer_alloc(10);
+    ASSERT_NE(buffer, nullptr);
+    EXPECT_EQ(buffer->total_size, 10);
+    EXPECT_EQ(buffer->bytes_used, 0);
+    EXPECT_NE(buffer->contents, nullptr);
+    buffer_free(buffer);
+}
+
+TEST(buffer, test_buffer_strlen)
+{
+    Buffer *buffer = buffer_alloc(0);
+    EXPECT_EQ(buffer_strlen(buffer), 0);
+    buffer = buffer_alloc(-1);
+    EXPECT_EQ(buffer_strlen(buffer), 0);
+    buffer = buffer_alloc(SIZE_MAX + 1);
+    EXPECT_EQ(buffer_strlen(buffer), 0);
+    buffer = buffer_alloc(10);
+    ASSERT_NE(buffer, nullptr);
+    EXPECT_EQ(buffer_strlen(buffer), 0);
+    ASSERT_EQ(buffer_append(buffer, "append", 6), 0);
+    EXPECT_EQ(buffer_strlen(buffer), 6);
+    buffer_free(buffer);
+}
+
+TEST(buffer, test_buffer_free)
+{
+    Buffer *buffer = nullptr;
+    buffer_free(buffer);
+    EXPECT_EQ(buffer, nullptr);
+}
+
+TEST(buffer, test_buffer_append)
+{
+    EXPECT_EQ(buffer_append(nullptr, "append", 6), -1);
+    Buffer *buffer = buffer_alloc(5);
+    EXPECT_EQ(buffer_append(buffer, "buffer needs to grow", 20), 0);
+    EXPECT_STREQ(buffer->contents, "buffer needs to grow");
+    EXPECT_EQ(buffer->bytes_used, 20);
+    EXPECT_EQ(buffer->total_size, 42);
+    buffer_free(buffer);
+
+    buffer = buffer_alloc(20);
+    EXPECT_EQ(buffer_append(buffer, "first", 5), 0);
+    EXPECT_EQ(buffer->bytes_used, 5);
+    EXPECT_STREQ(buffer->contents, "first");
+    EXPECT_EQ(buffer_append(buffer, "second", 6), 0);
+    EXPECT_EQ(buffer->bytes_used, 11);
+    EXPECT_EQ(buffer->total_size, 20);
+    EXPECT_STREQ(buffer->contents, "firstsecond");
+}
+
+TEST(buffer, test_buffer_empty)
+{
+    Buffer *buffer = buffer_alloc(10);
+    buffer_append(buffer, "content", 7);
+    buffer_empty(buffer);
+    EXPECT_EQ(buffer->total_size, 10);
+    EXPECT_EQ(buffer->bytes_used, 0);
+}
diff --git a/test/tar/CMakeLists.txt b/test/tar/CMakeLists.txt
new file mode 100644
index 00000000..10779f4c
--- /dev/null
+++ b/test/tar/CMakeLists.txt
@@ -0,0 +1,18 @@
+project(iSulad_UT)
+
+SET(EXE sha256_ut)
+
+add_executable(${EXE}
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/tar/util_gzip.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/sha256/sha256.c
+    sha256_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/tar
+    ${CMAKE_CURRENT_SOURCE_DIR}/../../src/utils/sha256
+    )
+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/tar/tar_ut.cc b/test/tar/tar_ut.cc
new file mode 100644
index 00000000..e69de29b
-- 
2.25.1

