tst-strlcat.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Test the strlcat function.
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <support/check.h>
  19. static int
  20. do_test (void)
  21. {
  22. struct {
  23. char buf1[16];
  24. char buf2[16];
  25. } s;
  26. /* Nothing is written to the destination if its size is 0. */
  27. memset (&s, '@', sizeof (s));
  28. TEST_COMPARE (strlcat (s.buf1, "", 0), 0);
  29. TEST_COMPARE_BLOB (&s, sizeof (s), "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", 32);
  30. TEST_COMPARE (strlcat (s.buf1, "Hello!", 0), 6);
  31. TEST_COMPARE_BLOB (&s, sizeof (s), "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", 32);
  32. /* No bytes are are modified in the target buffer if the source
  33. string is short enough. */
  34. memset (&s, '@', sizeof (s));
  35. strcpy (s.buf1, "He");
  36. TEST_COMPARE (strlcat (s.buf1, "llo!", sizeof (s.buf1)), 6);
  37. TEST_COMPARE_BLOB (&s, sizeof (s), "Hello!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 32);
  38. /* A source string which fits exactly into the destination buffer is
  39. not truncated. */
  40. memset (&s, '@', sizeof (s));
  41. strcpy (s.buf1, "H");
  42. TEST_COMPARE (strlcat (s.buf1, "ello, world!!!", sizeof (s.buf1)), 15);
  43. TEST_COMPARE_BLOB (&s, sizeof (s),
  44. "Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 32);
  45. /* A source string one character longer than the destination buffer
  46. is truncated by one character. The total length is returned. */
  47. memset (&s, '@', sizeof (s));
  48. strcpy (s.buf1, "Hello");
  49. TEST_COMPARE (strlcat (s.buf1, ", world!!!!", sizeof (s.buf1)), 16);
  50. TEST_COMPARE_BLOB (&s, sizeof (s),
  51. "Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 32);
  52. /* An even longer source string is truncated as well, and the total
  53. length is returned. */
  54. memset (&s, '@', sizeof (s));
  55. strcpy (s.buf1, "Hello,");
  56. TEST_COMPARE (strlcat (s.buf1, " world!!!!!!!!", sizeof (s.buf1)), 20);
  57. TEST_COMPARE_BLOB (&s, sizeof (s),
  58. "Hello, world!!!\0@@@@@@@@@@@@@@@@@@@@@@@@@", 32);
  59. /* A destination string which is not NUL-terminated does not result
  60. in any changes to the buffer. */
  61. memset (&s, '@', sizeof (s));
  62. memset (s.buf1, '$', sizeof (s.buf1));
  63. TEST_COMPARE (strlcat (s.buf1, "", sizeof (s.buf1)), 16);
  64. TEST_COMPARE_BLOB (&s, sizeof (s), "$$$$$$$$$$$$$$$$@@@@@@@@@@@@@@@@", 32);
  65. TEST_COMPARE (strlcat (s.buf1, "Hello!", sizeof (s.buf1)), 22);
  66. TEST_COMPARE_BLOB (&s, sizeof (s), "$$$$$$$$$$$$$$$$@@@@@@@@@@@@@@@@", 32);
  67. TEST_COMPARE (strlcat (s.buf1, "Hello, world!!!!!!!!", sizeof (s.buf1)), 36);
  68. TEST_COMPARE_BLOB (&s, sizeof (s), "$$$$$$$$$$$$$$$$@@@@@@@@@@@@@@@@", 32);
  69. return 0;
  70. }
  71. #include <support/test-driver.c>