lcr/0006-add-unit-test-for-util-function.patch
jake 9f47321607 !272 sync from upstream
* sync from upstream
2023-12-11 10:35:06 +00:00

110 lines
3.6 KiB
Diff

From e072071325b04d362b1eee69aed5c75199799fa5 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Mon, 13 Nov 2023 15:20:05 +0800
Subject: [PATCH 06/13] add unit test for util function
Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
tests/utils_file_ut.cpp | 30 ++++++++++++++++++++++++++++++
tests/utils_string_ut.cpp | 21 +++++++++++++++++++++
tests/utils_utils_ut.cpp | 13 +++++++++++++
3 files changed, 64 insertions(+)
diff --git a/tests/utils_file_ut.cpp b/tests/utils_file_ut.cpp
index d9bd252..7e98bd8 100644
--- a/tests/utils_file_ut.cpp
+++ b/tests/utils_file_ut.cpp
@@ -210,4 +210,34 @@ TEST(utils_file_testcase, test_isula_read_write_nointr)
ASSERT_EQ(nread, 5);
isula_path_remove(test_file.c_str());
+}
+
+TEST(utils_file_testcase, test_isula_set_non_block)
+{
+ ASSERT_EQ(isula_set_non_block(-1), -1);
+
+ int pipefd[2];
+ ASSERT_EQ(0, pipe(pipefd));
+ ASSERT_EQ(0, isula_set_non_block(pipefd[0]));
+ int flag = fcntl(pipefd[0], F_GETFL, 0);
+ ASSERT_NE(-1, flag);
+ EXPECT_TRUE(flag & O_NONBLOCK);
+ close(pipefd[0]);
+ close(pipefd[1]);
+
+ int pipefd2[2];
+ ASSERT_EQ(0, pipe(pipefd2));
+ close(pipefd2[1]);
+ ASSERT_EQ(-1, isula_set_non_block(pipefd2[1]));
+ close(pipefd2[0]);
+}
+
+TEST(utils_file_testcase, test_util_validate_absolute_path)
+{
+ ASSERT_EQ(isula_validate_absolute_path("/etc/isulad"), 0);
+ ASSERT_EQ(isula_validate_absolute_path("/isulad/"), 0);
+
+ ASSERT_EQ(isula_validate_absolute_path(nullptr), -1);
+ ASSERT_EQ(isula_validate_absolute_path("./isulad"), -1);
+ ASSERT_EQ(isula_validate_absolute_path("isulad"), -1);
}
\ No newline at end of file
diff --git a/tests/utils_string_ut.cpp b/tests/utils_string_ut.cpp
index 5b93723..20fd619 100644
--- a/tests/utils_string_ut.cpp
+++ b/tests/utils_string_ut.cpp
@@ -286,4 +286,25 @@ TEST(utils_string_testcase, test_isula_string_split_to_multi)
ASSERT_STREQ(ret->items[2], "c");
ASSERT_STREQ(ret->items[3], "d");
isula_string_array_free(ret);
+}
+
+TEST(utils_string_testcase, test_isula_has_prefix)
+{
+ const char* prefix = "prefix";
+ EXPECT_FALSE(isula_has_prefix(NULL, prefix));
+
+ const char* str = "string";
+ EXPECT_FALSE(isula_has_prefix(str, NULL));
+
+ const char* str2 = "short";
+ const char* prefix2 = "longer";
+ EXPECT_FALSE(isula_has_prefix(str2, prefix2));
+
+ const char* str3 = "string";
+ const char* prefix3 = "prefix";
+ EXPECT_FALSE(isula_has_prefix(str3, prefix3));
+
+ const char* str4 = "prefix_string";
+ const char* prefix4 = "prefix";
+ EXPECT_TRUE(isula_has_prefix(str4, prefix4));
}
\ No newline at end of file
diff --git a/tests/utils_utils_ut.cpp b/tests/utils_utils_ut.cpp
index 843bcf4..7085f23 100644
--- a/tests/utils_utils_ut.cpp
+++ b/tests/utils_utils_ut.cpp
@@ -47,4 +47,17 @@ TEST(utils_utils_testcase, test_isula_usleep_nointerupt)
elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
ASSERT_GT(elapsed_time.count(), 800);
ASSERT_LT(elapsed_time.count(), 1200);
+}
+
+TEST(utils_utils_testcase, test_isula_reg_match)
+{
+ const char *pattern = "^[a-f0-9]{64}$";
+ const char *valid = "c8da28a6cea7443b648ec70a1c947b6cb920ee0ef3c4a691d4252ff6e1888036";
+ const char *invalid = "g8da28a6cea7443b648ec70a1c947b6cb920ee0ef3c4a691d4252ff6e1888036";
+
+ ASSERT_EQ(isula_reg_match(pattern, valid), 0);
+ ASSERT_EQ(isula_reg_match(pattern, invalid), 1);
+
+ ASSERT_EQ(isula_reg_match(pattern, nullptr), -1);
+ ASSERT_EQ(isula_reg_match(nullptr, pattern), -1);
}
\ No newline at end of file
--
2.33.0