tst-updwtmpx.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Basic test coverage for updwtmpx.
  2. Copyright (C) 2019-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 License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. 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; see the file COPYING.LIB. If
  14. not, see <https://www.gnu.org/licenses/>. */
  15. /* This program runs a series of tests. Each one calls updwtmpx
  16. twice, to write two records, optionally with misalignment in the
  17. file, and reads back the results. */
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <support/check.h>
  21. #include <support/descriptors.h>
  22. #include <support/support.h>
  23. #include <support/temp_file.h>
  24. #include <support/test-driver.h>
  25. #include <support/xunistd.h>
  26. #include <unistd.h>
  27. #include <utmpx.h>
  28. static int
  29. do_test (void)
  30. {
  31. /* Two entries filled with an arbitrary bit pattern. */
  32. struct utmpx entries[2];
  33. unsigned char pad;
  34. {
  35. unsigned char *p = (unsigned char *) &entries[0];
  36. for (size_t i = 0; i < sizeof (entries); ++i)
  37. {
  38. p[i] = i;
  39. }
  40. /* Make sure that the first and second entry and the padding are
  41. different. */
  42. p[sizeof (struct utmpx)] = p[0] + 1;
  43. pad = p[0] + 2;
  44. }
  45. char *path;
  46. int fd = create_temp_file ("tst-updwtmpx-", &path);
  47. /* Used to check that updwtmpx does not leave an open file
  48. descriptor around. */
  49. struct support_descriptors *descriptors = support_descriptors_list ();
  50. /* updwtmpx is expected to remove misalignment. Optionally insert
  51. one byte of misalignment at the start and in the middle (after
  52. the first entry). */
  53. for (int misaligned_start = 0; misaligned_start < 2; ++misaligned_start)
  54. for (int misaligned_middle = 0; misaligned_middle < 2; ++misaligned_middle)
  55. {
  56. if (test_verbose > 0)
  57. printf ("info: misaligned_start=%d misaligned_middle=%d\n",
  58. misaligned_start, misaligned_middle);
  59. xftruncate (fd, 0);
  60. TEST_COMPARE (pwrite64 (fd, &pad, misaligned_start, 0),
  61. misaligned_start);
  62. /* Write first entry and check it. */
  63. errno = 0;
  64. updwtmpx (path, &entries[0]);
  65. TEST_COMPARE (errno, 0);
  66. support_descriptors_check (descriptors);
  67. TEST_COMPARE (xlseek (fd, 0, SEEK_END), sizeof (struct utmpx));
  68. struct utmpx buffer;
  69. TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
  70. sizeof (buffer));
  71. TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
  72. &buffer, sizeof (buffer));
  73. /* Middle mis-alignmet. */
  74. TEST_COMPARE (pwrite64 (fd, &pad, misaligned_middle,
  75. sizeof (struct utmpx)), misaligned_middle);
  76. /* Write second entry and check both entries. */
  77. errno = 0;
  78. updwtmpx (path, &entries[1]);
  79. TEST_COMPARE (errno, 0);
  80. support_descriptors_check (descriptors);
  81. TEST_COMPARE (xlseek (fd, 0, SEEK_END), 2 * sizeof (struct utmpx));
  82. TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
  83. sizeof (buffer));
  84. TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
  85. &buffer, sizeof (buffer));
  86. TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), sizeof (buffer)),
  87. sizeof (buffer));
  88. TEST_COMPARE_BLOB (&entries[1], sizeof (entries[1]),
  89. &buffer, sizeof (buffer));
  90. }
  91. support_descriptors_free (descriptors);
  92. free (path);
  93. xclose (fd);
  94. return 0;
  95. }
  96. #include <support/test-driver.c>