tst-cmsghdr-skeleton.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Test ancillary data header creation.
  2. Copyright (C) 2022-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. /* We use the preprocessor to generate the function/macro tests instead of
  16. using indirection because having all the macro expansions alongside
  17. each other lets the compiler warn us about suspicious pointer
  18. arithmetic across subsequent CMSG_{FIRST,NXT}HDR expansions. */
  19. #include <stdint.h>
  20. #define RUN_TEST_CONCAT(suffix) run_test_##suffix
  21. #define RUN_TEST_FUNCNAME(suffix) RUN_TEST_CONCAT (suffix)
  22. static void
  23. RUN_TEST_FUNCNAME (CMSG_NXTHDR_IMPL) (void)
  24. {
  25. struct msghdr m = {0};
  26. struct cmsghdr *cmsg;
  27. char cmsgbuf[3 * CMSG_SPACE (sizeof (PAYLOAD))] = {0};
  28. m.msg_control = cmsgbuf;
  29. m.msg_controllen = sizeof (cmsgbuf);
  30. /* First header should point to the start of the buffer. */
  31. cmsg = CMSG_FIRSTHDR (&m);
  32. TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
  33. /* If the first header length consumes the entire buffer, there is no
  34. space remaining for additional headers. */
  35. cmsg->cmsg_len = sizeof (cmsgbuf);
  36. cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
  37. TEST_VERIFY_EXIT (cmsg == NULL);
  38. /* The first header length is so big, using it would cause an overflow. */
  39. cmsg = CMSG_FIRSTHDR (&m);
  40. TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
  41. cmsg->cmsg_len = (__typeof (cmsg->cmsg_len)) SIZE_MAX;
  42. cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
  43. TEST_VERIFY_EXIT (cmsg == NULL);
  44. /* The first header leaves just enough space to hold another header. */
  45. cmsg = CMSG_FIRSTHDR (&m);
  46. TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
  47. cmsg->cmsg_len = sizeof (cmsgbuf) - CMSG_ALIGN (sizeof (struct cmsghdr));
  48. cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
  49. TEST_VERIFY_EXIT (cmsg != NULL);
  50. /* The first header leaves space but not enough for another header. */
  51. cmsg = CMSG_FIRSTHDR (&m);
  52. TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
  53. cmsg->cmsg_len ++;
  54. cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
  55. TEST_VERIFY_EXIT (cmsg == NULL);
  56. /* The second header leaves just enough space to hold another header. */
  57. cmsg = CMSG_FIRSTHDR (&m);
  58. TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
  59. cmsg->cmsg_len = CMSG_LEN (sizeof (PAYLOAD));
  60. cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
  61. TEST_VERIFY_EXIT (cmsg != NULL);
  62. cmsg->cmsg_len = sizeof (cmsgbuf)
  63. - CMSG_SPACE (sizeof (PAYLOAD)) /* First header. */
  64. - CMSG_ALIGN (sizeof (struct cmsghdr));
  65. cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
  66. TEST_VERIFY_EXIT (cmsg != NULL);
  67. /* The second header leaves space but not enough for another header. */
  68. cmsg = CMSG_FIRSTHDR (&m);
  69. TEST_VERIFY_EXIT ((char *) cmsg == cmsgbuf);
  70. cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
  71. TEST_VERIFY_EXIT (cmsg != NULL);
  72. cmsg->cmsg_len ++;
  73. cmsg = CMSG_NXTHDR_IMPL (&m, cmsg);
  74. TEST_VERIFY_EXIT (cmsg == NULL);
  75. return;
  76. }