1 /* Basic test coverage for updwtmpx.
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <https://www.gnu.org/licenses/>. */
19 /* This program runs a series of tests. Each one calls updwtmpx
20 twice, to write two records, optionally with misalignment in the
21 file, and reads back the results. */
25 #include <support/check.h>
26 #include <support/descriptors.h>
27 #include <support/support.h>
28 #include <support/temp_file.h>
29 #include <support/test-driver.h>
30 #include <support/xunistd.h>
37 /* Two entries filled with an arbitrary bit pattern. */
38 struct utmpx entries
[2];
41 unsigned char *p
= (unsigned char *) &entries
[0];
42 for (size_t i
= 0; i
< sizeof (entries
); ++i
)
46 /* Make sure that the first and second entry and the padding are
48 p
[sizeof (struct utmpx
)] = p
[0] + 1;
53 int fd
= create_temp_file ("tst-updwtmpx-", &path
);
55 /* Used to check that updwtmpx does not leave an open file
57 struct support_descriptors
*descriptors
= support_descriptors_list ();
59 /* updwtmpx is expected to remove misalignment. Optionally insert
60 one byte of misalignment at the start and in the middle (after
62 for (int misaligned_start
= 0; misaligned_start
< 2; ++misaligned_start
)
63 for (int misaligned_middle
= 0; misaligned_middle
< 2; ++misaligned_middle
)
66 printf ("info: misaligned_start=%d misaligned_middle=%d\n",
67 misaligned_start
, misaligned_middle
);
70 TEST_COMPARE (pwrite64 (fd
, &pad
, misaligned_start
, 0),
73 /* Write first entry and check it. */
75 updwtmpx (path
, &entries
[0]);
76 TEST_COMPARE (errno
, 0);
77 support_descriptors_check (descriptors
);
78 TEST_COMPARE (xlseek (fd
, 0, SEEK_END
), sizeof (struct utmpx
));
80 TEST_COMPARE (pread64 (fd
, &buffer
, sizeof (buffer
), 0),
82 TEST_COMPARE_BLOB (&entries
[0], sizeof (entries
[0]),
83 &buffer
, sizeof (buffer
));
85 /* Middle mis-alignmet. */
86 TEST_COMPARE (pwrite64 (fd
, &pad
, misaligned_middle
,
87 sizeof (struct utmpx
)), misaligned_middle
);
89 /* Write second entry and check both entries. */
91 updwtmpx (path
, &entries
[1]);
92 TEST_COMPARE (errno
, 0);
93 support_descriptors_check (descriptors
);
94 TEST_COMPARE (xlseek (fd
, 0, SEEK_END
), 2 * sizeof (struct utmpx
));
95 TEST_COMPARE (pread64 (fd
, &buffer
, sizeof (buffer
), 0),
97 TEST_COMPARE_BLOB (&entries
[0], sizeof (entries
[0]),
98 &buffer
, sizeof (buffer
));
99 TEST_COMPARE (pread64 (fd
, &buffer
, sizeof (buffer
), sizeof (buffer
)),
101 TEST_COMPARE_BLOB (&entries
[1], sizeof (entries
[1]),
102 &buffer
, sizeof (buffer
));
105 support_descriptors_free (descriptors
);
112 #include <support/test-driver.c>