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. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
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. */ |
22 | |
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> |
33 | |
34 | static int |
35 | do_test (void) |
36 | { |
37 | /* Two entries filled with an arbitrary bit pattern. */ |
38 | struct utmpx entries[2]; |
39 | unsigned char pad; |
40 | { |
41 | unsigned char *p = (unsigned char *) &entries[0]; |
42 | for (size_t i = 0; i < sizeof (entries); ++i) |
43 | { |
44 | p[i] = i; |
45 | } |
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; |
50 | } |
51 | |
52 | char *path; |
53 | int fd = create_temp_file (base: "tst-updwtmpx-" , filename: &path); |
54 | |
55 | /* Used to check that updwtmpx does not leave an open file |
56 | descriptor around. */ |
57 | struct support_descriptors *descriptors = support_descriptors_list (); |
58 | |
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) |
64 | { |
65 | if (test_verbose > 0) |
66 | printf (format: "info: misaligned_start=%d misaligned_middle=%d\n" , |
67 | misaligned_start, misaligned_middle); |
68 | |
69 | xftruncate (fd, length: 0); |
70 | TEST_COMPARE (pwrite64 (fd, &pad, misaligned_start, 0), |
71 | misaligned_start); |
72 | |
73 | /* Write first entry and check it. */ |
74 | errno = 0; |
75 | updwtmpx (wtmpx_file: path, utmpx: &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)); |
84 | |
85 | /* Middle mis-alignmet. */ |
86 | TEST_COMPARE (pwrite64 (fd, &pad, misaligned_middle, |
87 | sizeof (struct utmpx)), misaligned_middle); |
88 | |
89 | /* Write second entry and check both entries. */ |
90 | errno = 0; |
91 | updwtmpx (wtmpx_file: path, utmpx: &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)); |
103 | } |
104 | |
105 | support_descriptors_free (descriptors); |
106 | free (ptr: path); |
107 | xclose (fd); |
108 | |
109 | return 0; |
110 | } |
111 | |
112 | #include <support/test-driver.c> |
113 | |