From: mwilck@suse.com
To: Christophe Varoqui <christophe.varoqui@opensvc.com>,
Benjamin Marzinski <bmarzins@redhat.com>
Cc: dm-devel@redhat.com, Martin Wilck <mwilck@suse.com>
Subject: [PATCH 11/35] multipath-tools tests: add unit tests for strlcat
Date: Thu, 9 Jul 2020 12:15:56 +0200 [thread overview]
Message-ID: <20200709101620.6786-12-mwilck@suse.com> (raw)
In-Reply-To: <20200709101620.6786-1-mwilck@suse.com>
From: Martin Wilck <mwilck@suse.com>
Also, use some constants for both strlcpy and strlcat tests.
Signed-off-by: Martin Wilck <mwilck@suse.com>
---
tests/util.c | 222 ++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 210 insertions(+), 12 deletions(-)
diff --git a/tests/util.c b/tests/util.c
index db7c05f..3c4113e 100644
--- a/tests/util.c
+++ b/tests/util.c
@@ -476,48 +476,54 @@ static int test_bitmasks(void)
return cmocka_run_group_tests(tests, NULL, NULL);
}
-static const char src_str[] = "Hello";
+#define DST_STR "Hello"
+static const char dst_str[] = DST_STR;
+/* length of src_str and dst_str should be different */
+static const char src_str[] = " World";
+/* Must be big enough to hold dst_str and src_str */
+#define ARRSZ 16
+#define FILL '@'
/* strlcpy with length 0 */
static void test_strlcpy_0(void **state)
{
- char tst[] = "word";
+ char tst[] = DST_STR;
int rc;
rc = strlcpy(tst, src_str, 0);
assert_int_equal(rc, strlen(src_str));
- assert_string_equal(tst, "word");
+ assert_string_equal(tst, dst_str);
}
/* strlcpy with length 1 */
static void test_strlcpy_1(void **state)
{
- char tst[] = "word";
+ char tst[] = DST_STR;
int rc;
rc = strlcpy(tst, src_str, 1);
assert_int_equal(rc, strlen(src_str));
assert_int_equal(tst[0], '\0');
- assert_string_equal(tst + 1, "ord");
+ assert_string_equal(tst + 1, dst_str + 1);
}
/* strlcpy with length 2 */
static void test_strlcpy_2(void **state)
{
- char tst[] = "word";
+ char tst[] = DST_STR;
int rc;
rc = strlcpy(tst, src_str, 2);
assert_int_equal(rc, strlen(src_str));
assert_int_equal(tst[0], src_str[0]);
assert_int_equal(tst[1], '\0');
- assert_string_equal(tst + 2, "rd");
+ assert_string_equal(tst + 2, dst_str + 2);
}
/* strlcpy with dst length < src length */
static void test_strlcpy_3(void **state)
{
- char tst[] = "word";
+ char tst[] = DST_STR;
int rc;
rc = strlcpy(tst, src_str, sizeof(tst));
@@ -580,26 +586,26 @@ static void test_strlcpy_6(void **state)
/* strlcpy with empty src */
static void test_strlcpy_7(void **state)
{
- char tst[] = "word";
+ char tst[] = DST_STR;
static const char empty[] = "";
int rc;
rc = strlcpy(tst, empty, sizeof(tst));
assert_int_equal(rc, strlen(empty));
assert_string_equal(empty, tst);
- assert_string_equal(tst + 1, "ord");
+ assert_string_equal(tst + 1, dst_str + 1);
}
/* strlcpy with empty src, length 0 */
static void test_strlcpy_8(void **state)
{
- char tst[] = "word";
+ char tst[] = DST_STR;
static const char empty[] = "";
int rc;
rc = strlcpy(tst, empty, 0);
assert_int_equal(rc, strlen(empty));
- assert_string_equal("word", tst);
+ assert_string_equal(dst_str, tst);
}
static int test_strlcpy(void)
@@ -619,6 +625,197 @@ static int test_strlcpy(void)
return cmocka_run_group_tests(tests, NULL, NULL);
}
+
+/* 0-terminated string, filled with non-0 after the terminator */
+static void prep_buf(char *buf, size_t size, const char *word)
+{
+ memset(buf, FILL, size);
+ assert_in_range(strlen(word), 0, size - 1);
+ memcpy(buf, word, strlen(word) + 1);
+}
+
+/* strlcat with size 0, dst not 0-terminated */
+static void test_strlcat_0(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, src_str, 0);
+ assert_int_equal(rc, strlen(src_str));
+ assert_string_equal(tst, dst_str);
+ assert_int_equal(tst[sizeof(dst_str)], FILL);
+}
+
+/* strlcat with length 1, dst not 0-terminated */
+static void test_strlcat_1(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, src_str, 1);
+ assert_int_equal(rc, 1 + strlen(src_str));
+ assert_string_equal(tst, dst_str);
+ assert_int_equal(tst[sizeof(dst_str)], FILL);
+}
+
+/* strlcat with length = dst - 1 */
+static void test_strlcat_2(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, src_str, strlen(dst_str));
+ assert_int_equal(rc, strlen(src_str) + strlen(dst_str));
+ assert_string_equal(tst, dst_str);
+ assert_int_equal(tst[sizeof(dst_str)], FILL);
+}
+
+/* strlcat with length = dst */
+static void test_strlcat_3(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, src_str, strlen(dst_str) + 1);
+ assert_int_equal(rc, strlen(src_str) + strlen(dst_str));
+ assert_string_equal(tst, dst_str);
+ assert_int_equal(tst[sizeof(dst_str)], FILL);
+}
+
+/* strlcat with len = dst + 1 */
+static void test_strlcat_4(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, src_str, strlen(dst_str) + 2);
+ assert_int_equal(rc, strlen(src_str) + strlen(dst_str));
+ assert_false(strncmp(tst, dst_str, strlen(dst_str)));
+ assert_int_equal(tst[strlen(dst_str)], src_str[0]);
+ assert_int_equal(tst[strlen(dst_str) + 1], '\0');
+ assert_int_equal(tst[strlen(dst_str) + 2], FILL);
+}
+
+/* strlcat with len = needed - 1 */
+static void test_strlcat_5(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, src_str, strlen(dst_str) + strlen(src_str));
+ assert_int_equal(rc, strlen(src_str) + strlen(dst_str));
+ assert_false(strncmp(tst, dst_str, strlen(dst_str)));
+ assert_false(strncmp(tst + strlen(dst_str), src_str,
+ strlen(src_str) - 1));
+ assert_int_equal(tst[strlen(dst_str) + strlen(src_str) - 1], '\0');
+ assert_int_equal(tst[strlen(dst_str) + strlen(src_str)], FILL);
+}
+
+/* strlcat with exactly sufficient space */
+static void test_strlcat_6(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, src_str, strlen(dst_str) + strlen(src_str) + 1);
+ assert_int_equal(rc, strlen(src_str) + strlen(dst_str));
+ assert_false(strncmp(tst, dst_str, strlen(dst_str)));
+ assert_string_equal(tst + strlen(dst_str), src_str);
+ assert_int_equal(tst[strlen(dst_str) + strlen(src_str) + 1], FILL);
+}
+
+/* strlcat with sufficient space */
+static void test_strlcat_7(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, src_str, sizeof(tst));
+ assert_int_equal(rc, strlen(src_str) + strlen(dst_str));
+ assert_false(strncmp(tst, dst_str, strlen(dst_str)));
+ assert_string_equal(tst + strlen(dst_str), src_str);
+}
+
+/* strlcat with 0-length string */
+static void test_strlcat_8(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), dst_str);
+ rc = strlcat(tst, "", sizeof(tst));
+ assert_int_equal(rc, strlen(dst_str));
+ assert_string_equal(tst, dst_str);
+ assert_int_equal(tst[sizeof(dst_str)], FILL);
+}
+
+/* strlcat with empty dst */
+static void test_strlcat_9(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), "");
+ rc = strlcat(tst, src_str, ARRSZ);
+ assert_int_equal(rc, strlen(src_str));
+ assert_string_equal(tst, src_str);
+ assert_int_equal(tst[sizeof(src_str)], FILL);
+}
+
+/* strlcat with empty dst and src */
+static void test_strlcat_10(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), "");
+ rc = strlcat(tst, "", ARRSZ);
+ assert_int_equal(rc, 0);
+ assert_string_equal(tst, "");
+ assert_int_equal(tst[1], FILL);
+}
+
+/* strlcat with no space to store 0 */
+static void test_strlcat_11(void **state)
+{
+ char tst[ARRSZ];
+ int rc;
+
+ prep_buf(tst, sizeof(tst), "");
+ tst[0] = FILL;
+ rc = strlcat(tst, src_str, 0);
+ assert_int_equal(rc, strlen(src_str));
+ assert_int_equal(tst[0], FILL);
+}
+
+static int test_strlcat(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_strlcat_0),
+ cmocka_unit_test(test_strlcat_1),
+ cmocka_unit_test(test_strlcat_2),
+ cmocka_unit_test(test_strlcat_3),
+ cmocka_unit_test(test_strlcat_4),
+ cmocka_unit_test(test_strlcat_5),
+ cmocka_unit_test(test_strlcat_6),
+ cmocka_unit_test(test_strlcat_7),
+ cmocka_unit_test(test_strlcat_8),
+ cmocka_unit_test(test_strlcat_9),
+ cmocka_unit_test(test_strlcat_10),
+ cmocka_unit_test(test_strlcat_11),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
+
static void test_strchop_nochop(void **state)
{
char hello[] = "hello";
@@ -688,6 +885,7 @@ int main(void)
ret += test_basenamecpy();
ret += test_bitmasks();
ret += test_strlcpy();
+ ret += test_strlcat();
ret += test_strchop();
return ret;
}
--
2.26.2
next prev parent reply other threads:[~2020-07-09 10:15 UTC|newest]
Thread overview: 48+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-09 10:15 [PATCH 00/35] multipath-tools series part I: minor changes mwilck
2020-07-09 10:15 ` [PATCH 01/35] multipath-tools tests/util: separate group for bitmask tests mwilck
2020-07-09 10:15 ` [PATCH 02/35] multipath-tools tests/directio: fix missing initializers mwilck
2020-07-09 10:15 ` [PATCH 03/35] tests: __wrap_dlog: use check_expected() mwilck
2020-07-09 10:15 ` [PATCH 04/35] multipath tools tests: add strchop() test mwilck
2020-07-09 10:15 ` [PATCH 05/35] libmultipath: improve strchop() mwilck
2020-07-09 10:15 ` [PATCH 06/35] multipath-tools tests: add test for devt2devname mwilck
2020-07-09 10:15 ` [PATCH 07/35] libmultipath: devt2devname(): simplify using libudev mwilck
2020-07-09 10:15 ` [PATCH 08/35] libmultipath: create bitfield abstraction mwilck
2020-07-16 21:17 ` Benjamin Marzinski
2020-08-04 15:04 ` Martin Wilck
2020-08-04 15:18 ` Martin Wilck
2020-08-04 16:26 ` Benjamin Marzinski
2020-08-04 19:35 ` Martin Wilck
2020-08-10 18:05 ` Benjamin Marzinski
2020-08-10 18:59 ` Martin Wilck
2020-07-09 10:15 ` [PATCH 09/35] libmultipath: use bitfields in group_by_match() mwilck
2020-07-09 10:15 ` [PATCH 10/35] libmultipath: util: constify function arguments mwilck
2020-07-09 10:15 ` mwilck [this message]
2020-07-09 10:15 ` [PATCH 12/35] libmultipath: strlcpy()/strlcat(): use restrict qualifier mwilck
2020-07-16 22:18 ` Benjamin Marzinski
2020-08-04 15:36 ` Martin Wilck
2020-08-04 17:29 ` Benjamin Marzinski
2020-08-04 20:15 ` Martin Wilck
2020-07-09 10:15 ` [PATCH 13/35] libmultipath: constify blacklist code mwilck
2020-07-09 10:15 ` [PATCH 14/35] libmultipath: rlookup_binding(): remove newline in log message mwilck
2020-07-09 10:16 ` [PATCH 15/35] libmultipath: fix missing initializer warning from clang 3.9 mwilck
2020-07-09 10:16 ` [PATCH 16/35] libmultipath: fix gcc -Wstringop-overflow warning mwilck
2020-07-09 10:16 ` [PATCH 17/35] libmultipath: remove uevent listener failback mwilck
2020-07-09 10:16 ` [PATCH 18/35] libmultipath: uevent: use static linkage where possible mwilck
2020-07-09 10:16 ` [PATCH 19/35] libmultipath: uevent: inline trivial functions mwilck
2020-07-09 10:16 ` [PATCH 20/35] libmultipath: decrease log level of "SCSI target" log message mwilck
2020-07-09 10:16 ` [PATCH 21/35] libmultipath: get_udev_uid(): more appropriate error code mwilck
2020-07-09 10:16 ` [PATCH 22/35] libmultipath: get_uid(): improve log message on udev failure mwilck
2020-07-09 10:16 ` [PATCH 23/35] libmultipath: make sysfs_pathinfo() static and use const mwilck
2020-07-09 10:16 ` [PATCH 24/35] libmultipath: pathinfo(): improve a log message mwilck
2020-07-09 10:16 ` [PATCH 25/35] libmultipath: pathinfo(): don't filter emtpy devnode names mwilck
2020-07-09 10:16 ` [PATCH 26/35] libmultipath: io_err_stat_handle_pathfail(): less error conditions mwilck
2020-07-09 10:16 ` [PATCH 27/35] libmultipath: improve libdm logging mwilck
2020-07-09 10:16 ` [PATCH 28/35] libmultipath: snprint_devices(): use udev_enumerate mwilck
2020-07-09 10:16 ` [PATCH 29/35] libmultipath: snprint_devices(): print hidden/foreign status mwilck
2020-07-09 10:16 ` [PATCH 30/35] libmultipath: alloc_path(): initialize pp->initialized mwilck
2020-07-09 10:16 ` [PATCH 31/35] libmultipath: alloc_path_with_pathinfo(): treat devname overflow as error mwilck
2020-07-09 10:16 ` [PATCH 32/35] libmultipath: log table params in addmap() mwilck
2020-07-09 10:16 ` [PATCH 33/35] multipathd: remove set_multipath_wwid() mwilck
2020-07-09 10:16 ` [PATCH 34/35] kpartx: print an error message if removing a partition fails mwilck
2020-07-09 10:16 ` [PATCH 35/35] kpartx: add missing newline mwilck
2020-07-20 21:09 ` [PATCH 00/35] multipath-tools series part I: minor changes Benjamin Marzinski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200709101620.6786-12-mwilck@suse.com \
--to=mwilck@suse.com \
--cc=bmarzins@redhat.com \
--cc=christophe.varoqui@opensvc.com \
--cc=dm-devel@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.