1 // SPDX-License-Identifier: GPL-2.1+
3 * Copyright (C) 2021 Sean Anderson <seanga2@gmail.com>
4 * Copyright (C) 2011-2021 Free Software Foundation, Inc.
6 * These tests adapted from glibc's string/test-strncat.c
11 #include <test/test.h>
15 char buf1[BUF_SIZE], buf2[BUF_SIZE];
17 static int do_test_strlcat(struct unit_test_state *uts, int line, size_t align1,
18 size_t align2, size_t len1, size_t len2, size_t n)
21 size_t i, len, expected, actual;
24 if (align1 + len1 >= BUF_SIZE)
26 if (align1 + n > BUF_SIZE)
30 if (align2 + len1 + len2 >= BUF_SIZE)
32 if (align2 + len1 + n > BUF_SIZE)
38 for (i = 0; i < len1 - 1; i++)
39 s1[i] = 32 + 23 * i % (127 - 32);
42 for (i = 0; i < len2 - 1; i++)
43 s2[i] = 32 + 23 * i % (127 - 32);
46 expected = len2 < n ? min(len1 + len2 - 1, n) : n;
47 actual = strlcat(s2, s1, n);
48 if (expected != actual) {
49 ut_failf(uts, __FILE__, line, __func__,
50 "strlcat(s2, s1, 2) == len2 < n ? min(len1 + len2, n) : n",
51 "Expected %#zx (%zd), got %#zx (%zd)",
52 expected, expected, actual, actual);
53 return CMD_RET_FAILURE;
56 len = min3(len1, n - len2, (size_t)0);
57 if (memcmp(s2 + len2, s1, len)) {
58 ut_failf(uts, __FILE__, line, __func__,
60 "Expected \"%.*s\", got \"%.*s\"",
61 (int)len, s1, (int)len, s2 + len2);
62 return CMD_RET_FAILURE;
65 i = min(n, len1 + len2 - 1) - 1;
66 if (len2 < n && s2[i] != '\0') {
67 ut_failf(uts, __FILE__, line, __func__,
68 "n < len1 && s2[len2 + n] == '\\0'",
69 "Expected s2[%zd] = '\\0', got %d ('%c')",
71 return CMD_RET_FAILURE;
77 #define test_strlcat(align1, align2, len1, len2, n) do { \
78 int ret = do_test_strlcat(uts, __LINE__, align1, align2, len1, len2, \
84 static int lib_test_strlcat(struct unit_test_state *uts)
88 test_strlcat(0, 2, 2, 2, SIZE_MAX);
89 test_strlcat(0, 0, 4, 4, SIZE_MAX);
90 test_strlcat(4, 0, 4, 4, SIZE_MAX);
91 test_strlcat(0, 0, 8, 8, SIZE_MAX);
92 test_strlcat(0, 8, 8, 8, SIZE_MAX);
94 for (i = 1; i < 8; i++) {
95 test_strlcat(0, 0, 8 << i, 8 << i, SIZE_MAX);
96 test_strlcat(8 - i, 2 * i, 8 << i, 8 << i, SIZE_MAX);
97 test_strlcat(0, 0, 8 << i, 2 << i, SIZE_MAX);
98 test_strlcat(8 - i, 2 * i, 8 << i, 2 << i, SIZE_MAX);
100 test_strlcat(i, 2 * i, 8 << i, 1, SIZE_MAX);
101 test_strlcat(2 * i, i, 8 << i, 1, SIZE_MAX);
102 test_strlcat(i, i, 8 << i, 10, SIZE_MAX);
105 for (n = 2; n <= 2048; n *= 4) {
106 test_strlcat(0, 2, 2, 2, n);
107 test_strlcat(0, 0, 4, 4, n);
108 test_strlcat(4, 0, 4, 4, n);
109 test_strlcat(0, 0, 8, 8, n);
110 test_strlcat(0, 8, 8, 8, n);
112 for (i = 1; i < 8; i++) {
113 test_strlcat(0, 0, 8 << i, 8 << i, n);
114 test_strlcat(8 - i, 2 * i, 8 << i, 8 << i, n);
115 test_strlcat(0, 0, 8 << i, 2 << i, n);
116 test_strlcat(8 - i, 2 * i, 8 << i, 2 << i, n);
118 test_strlcat(i, 2 * i, 8 << i, 1, n);
119 test_strlcat(2 * i, i, 8 << i, 1, n);
120 test_strlcat(i, i, 8 << i, 10, n);
126 LIB_TEST(lib_test_strlcat, 0);