manual: Add documentation for arc4random functions
[glibc.git] / login / tst-updwtmpx.c
blobebf2f8a2c0c75c7b4e37e38ed0041789cef83654
1 /* Basic test coverage for updwtmpx.
2 Copyright (C) 2019-2022 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. */
23 #include <errno.h>
24 #include <stdlib.h>
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>
31 #include <unistd.h>
32 #include <utmpx.h>
34 static int
35 do_test (void)
37 /* Two entries filled with an arbitrary bit pattern. */
38 struct utmpx entries[2];
39 unsigned char pad;
41 unsigned char *p = (unsigned char *) &entries[0];
42 for (size_t i = 0; i < sizeof (entries); ++i)
44 p[i] = i;
46 /* Make sure that the first and second entry and the padding are
47 different. */
48 p[sizeof (struct utmpx)] = p[0] + 1;
49 pad = p[0] + 2;
52 char *path;
53 int fd = create_temp_file ("tst-updwtmpx-", &path);
55 /* Used to check that updwtmpx does not leave an open file
56 descriptor around. */
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
61 the first entry). */
62 for (int misaligned_start = 0; misaligned_start < 2; ++misaligned_start)
63 for (int misaligned_middle = 0; misaligned_middle < 2; ++misaligned_middle)
65 if (test_verbose > 0)
66 printf ("info: misaligned_start=%d misaligned_middle=%d\n",
67 misaligned_start, misaligned_middle);
69 xftruncate (fd, 0);
70 TEST_COMPARE (pwrite64 (fd, &pad, misaligned_start, 0),
71 misaligned_start);
73 /* Write first entry and check it. */
74 errno = 0;
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));
79 struct utmpx buffer;
80 TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), 0),
81 sizeof (buffer));
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. */
90 errno = 0;
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),
96 sizeof (buffer));
97 TEST_COMPARE_BLOB (&entries[0], sizeof (entries[0]),
98 &buffer, sizeof (buffer));
99 TEST_COMPARE (pread64 (fd, &buffer, sizeof (buffer), sizeof (buffer)),
100 sizeof (buffer));
101 TEST_COMPARE_BLOB (&entries[1], sizeof (entries[1]),
102 &buffer, sizeof (buffer));
105 support_descriptors_free (descriptors);
106 free (path);
107 xclose (fd);
109 return 0;
112 #include <support/test-driver.c>