lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test and measure strncpy functions. |
| 2 | Copyright (C) 1999-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Written by Jakub Jelinek <jakub@redhat.com>, 1999. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #ifndef STRNCPY_RESULT |
| 21 | # define STRNCPY_RESULT(dst, len, n) dst |
| 22 | # define TEST_MAIN |
| 23 | # define TEST_NAME "strncpy" |
| 24 | # include "test-string.h" |
| 25 | |
| 26 | char *simple_strncpy (char *, const char *, size_t); |
| 27 | char *stupid_strncpy (char *, const char *, size_t); |
| 28 | |
| 29 | IMPL (stupid_strncpy, 0) |
| 30 | IMPL (simple_strncpy, 0) |
| 31 | IMPL (strncpy, 1) |
| 32 | |
| 33 | char * |
| 34 | simple_strncpy (char *dst, const char *src, size_t n) |
| 35 | { |
| 36 | char *ret = dst; |
| 37 | while (n--) |
| 38 | if ((*dst++ = *src++) == '\0') |
| 39 | { |
| 40 | while (n--) |
| 41 | *dst++ = '\0'; |
| 42 | return ret; |
| 43 | } |
| 44 | return ret; |
| 45 | } |
| 46 | |
| 47 | char * |
| 48 | stupid_strncpy (char *dst, const char *src, size_t n) |
| 49 | { |
| 50 | size_t nc = strnlen (src, n); |
| 51 | size_t i; |
| 52 | |
| 53 | for (i = 0; i < nc; ++i) |
| 54 | dst[i] = src[i]; |
| 55 | for (; i < n; ++i) |
| 56 | dst[i] = '\0'; |
| 57 | return dst; |
| 58 | } |
| 59 | #endif |
| 60 | |
| 61 | typedef char *(*proto_t) (char *, const char *, size_t); |
| 62 | |
| 63 | static void |
| 64 | do_one_test (impl_t *impl, char *dst, const char *src, size_t len, size_t n) |
| 65 | { |
| 66 | if (CALL (impl, dst, src, n) != STRNCPY_RESULT (dst, len, n)) |
| 67 | { |
| 68 | error (0, 0, "Wrong result in function %s %p %p", impl->name, |
| 69 | CALL (impl, dst, src, n), dst); |
| 70 | ret = 1; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if (memcmp (dst, src, len > n ? n : len) != 0) |
| 75 | { |
| 76 | error (0, 0, "Wrong result in function %s", impl->name); |
| 77 | ret = 1; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (n > len) |
| 82 | { |
| 83 | size_t i; |
| 84 | |
| 85 | for (i = len; i < n; ++i) |
| 86 | if (dst [i] != '\0') |
| 87 | { |
| 88 | error (0, 0, "Wrong result in function %s", impl->name); |
| 89 | ret = 1; |
| 90 | return; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char) |
| 97 | { |
| 98 | size_t i; |
| 99 | char *s1, *s2; |
| 100 | |
| 101 | align1 &= 7; |
| 102 | if (align1 + len >= page_size) |
| 103 | return; |
| 104 | |
| 105 | align2 &= 7; |
| 106 | if (align2 + len >= page_size) |
| 107 | return; |
| 108 | |
| 109 | s1 = (char *) (buf1 + align1); |
| 110 | s2 = (char *) (buf2 + align2); |
| 111 | |
| 112 | for (i = 0; i < len; ++i) |
| 113 | s1[i] = 32 + 23 * i % (max_char - 32); |
| 114 | s1[len] = 0; |
| 115 | for (i = len + 1; i + align1 < page_size && i < len + 64; ++i) |
| 116 | s1[i] = 32 + 32 * i % (max_char - 32); |
| 117 | |
| 118 | FOR_EACH_IMPL (impl, 0) |
| 119 | do_one_test (impl, s2, s1, len, n); |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | do_random_tests (void) |
| 124 | { |
| 125 | size_t i, j, n, align1, align2, len, size, mode; |
| 126 | unsigned char *p1 = buf1 + page_size - 512; |
| 127 | unsigned char *p2 = buf2 + page_size - 512; |
| 128 | unsigned char *res; |
| 129 | |
| 130 | for (n = 0; n < ITERATIONS; n++) |
| 131 | { |
| 132 | mode = random (); |
| 133 | if (mode & 1) |
| 134 | { |
| 135 | size = random () & 255; |
| 136 | align1 = 512 - size - (random () & 15); |
| 137 | if (mode & 2) |
| 138 | align2 = align1 - (random () & 24); |
| 139 | else |
| 140 | align2 = align1 - (random () & 31); |
| 141 | if (mode & 4) |
| 142 | { |
| 143 | j = align1; |
| 144 | align1 = align2; |
| 145 | align2 = j; |
| 146 | } |
| 147 | if (mode & 8) |
| 148 | len = size - (random () & 31); |
| 149 | else |
| 150 | len = 512; |
| 151 | if (len >= 512) |
| 152 | len = random () & 511; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | align1 = random () & 31; |
| 157 | if (mode & 2) |
| 158 | align2 = random () & 31; |
| 159 | else |
| 160 | align2 = align1 + (random () & 24); |
| 161 | len = random () & 511; |
| 162 | j = align1; |
| 163 | if (align2 > j) |
| 164 | j = align2; |
| 165 | if (mode & 4) |
| 166 | { |
| 167 | size = random () & 511; |
| 168 | if (size + j > 512) |
| 169 | size = 512 - j - (random() & 31); |
| 170 | } |
| 171 | else |
| 172 | size = 512 - j; |
| 173 | if ((mode & 8) && len + j >= 512) |
| 174 | len = 512 - j - (random () & 7); |
| 175 | } |
| 176 | j = len + align1 + 64; |
| 177 | if (j > 512) |
| 178 | j = 512; |
| 179 | for (i = 0; i < j; i++) |
| 180 | { |
| 181 | if (i == len + align1) |
| 182 | p1[i] = 0; |
| 183 | else |
| 184 | { |
| 185 | p1[i] = random () & 255; |
| 186 | if (i >= align1 && i < len + align1 && !p1[i]) |
| 187 | p1[i] = (random () & 127) + 3; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | FOR_EACH_IMPL (impl, 1) |
| 192 | { |
| 193 | memset (p2 - 64, '\1', 512 + 64); |
| 194 | res = (unsigned char *) CALL (impl, |
| 195 | (char *) (p2 + align2), |
| 196 | (char *) (p1 + align1), size); |
| 197 | if (res != STRNCPY_RESULT (p2 + align2, len, size)) |
| 198 | { |
| 199 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p", |
| 200 | n, impl->name, align1, align2, len, res, |
| 201 | STRNCPY_RESULT (p2 + align2, len, size)); |
| 202 | ret = 1; |
| 203 | } |
| 204 | for (j = 0; j < align2 + 64; ++j) |
| 205 | { |
| 206 | if (p2[j - 64] != '\1') |
| 207 | { |
| 208 | error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)", |
| 209 | n, impl->name, align1, align2, len); |
| 210 | ret = 1; |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | j = align2 + len + 1; |
| 215 | if (size + align2 > j) |
| 216 | j = size + align2; |
| 217 | for (; j < 512; ++j) |
| 218 | { |
| 219 | if (p2[j] != '\1') |
| 220 | { |
| 221 | error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)", |
| 222 | n, impl->name, align1, align2, len); |
| 223 | ret = 1; |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | for (j = align2 + len + 1; j < align2 + size; ++j) |
| 228 | if (p2[j]) |
| 229 | { |
| 230 | error (0, 0, "Iteration %zd - garbage after size, %s (%zd, %zd, %zd)", |
| 231 | n, impl->name, align1, align2, len); |
| 232 | ret = 1; |
| 233 | break; |
| 234 | } |
| 235 | j = len + 1; |
| 236 | if (size < j) |
| 237 | j = size; |
| 238 | if (memcmp (p1 + align1, p2 + align2, j)) |
| 239 | { |
| 240 | error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)", |
| 241 | n, impl->name, align1, align2, len); |
| 242 | ret = 1; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | int |
| 249 | test_main (void) |
| 250 | { |
| 251 | size_t i; |
| 252 | |
| 253 | test_init (); |
| 254 | |
| 255 | printf ("%28s", ""); |
| 256 | FOR_EACH_IMPL (impl, 0) |
| 257 | printf ("\t%s", impl->name); |
| 258 | putchar ('\n'); |
| 259 | |
| 260 | for (i = 1; i < 8; ++i) |
| 261 | { |
| 262 | do_test (i, i, 16, 16, 127); |
| 263 | do_test (i, i, 16, 16, 255); |
| 264 | do_test (i, 2 * i, 16, 16, 127); |
| 265 | do_test (2 * i, i, 16, 16, 255); |
| 266 | do_test (8 - i, 2 * i, 1 << i, 2 << i, 127); |
| 267 | do_test (2 * i, 8 - i, 2 << i, 1 << i, 127); |
| 268 | do_test (8 - i, 2 * i, 1 << i, 2 << i, 255); |
| 269 | do_test (2 * i, 8 - i, 2 << i, 1 << i, 255); |
| 270 | } |
| 271 | |
| 272 | for (i = 1; i < 8; ++i) |
| 273 | { |
| 274 | do_test (0, 0, 4 << i, 8 << i, 127); |
| 275 | do_test (0, 0, 16 << i, 8 << i, 127); |
| 276 | do_test (8 - i, 2 * i, 4 << i, 8 << i, 127); |
| 277 | do_test (8 - i, 2 * i, 16 << i, 8 << i, 127); |
| 278 | } |
| 279 | |
| 280 | do_random_tests (); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | #include "../test-skeleton.c" |