x86: Always include <dl-cet.h>/cet-tunables.h> for --enable-cet
[glibc.git] / misc / tst-preadvwritev-common.c
blobb59a3de4658d48c9be7772d587346c0a860b3e9e
1 /* Common definitions for preadv and pwritev.
2 Copyright (C) 2016-2018 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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <array_length.h>
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <sys/uio.h>
25 #include <sys/stat.h>
27 #include <support/check.h>
28 #include <support/temp_file.h>
29 #include <support/xunistd.h>
31 static char *temp_filename;
32 static int temp_fd;
34 static int do_test (void);
36 static void
37 do_prepare (int argc, char **argv)
39 temp_fd = create_temp_file ("tst-preadvwritev.", &temp_filename);
40 if (temp_fd == -1)
41 FAIL_EXIT1 ("cannot create temporary file");
43 #define PREPARE do_prepare
45 #ifndef PREADV
46 # define PREADV(__fd, __iov, __iovcnt, __offset) \
47 preadv (__fd, __iov, __iovcnt, __offset)
48 #endif
50 #ifndef PWRITEV
51 # define PWRITEV(__fd, __iov, __iovcnt, __offset) \
52 pwritev (__fd, __iov, __iovcnt, __offset)
53 #endif
55 static __attribute__ ((unused)) void
56 do_test_without_offset (void)
58 xftruncate (temp_fd, 0);
60 xwrite (temp_fd, "123", 3);
61 xlseek (temp_fd, 2, SEEK_SET);
63 struct iovec iov[] =
65 { (void *) "abc", 3 },
66 { (void *) "xyzt", 4 },
68 TEST_COMPARE (PWRITEV (temp_fd, iov, array_length (iov), -1), 7);
70 TEST_COMPARE (xlseek (temp_fd, 0, SEEK_CUR), 9);
72 xlseek (temp_fd, 1, SEEK_SET);
73 char buf1[3];
74 char buf2[2];
76 struct iovec iov[] =
78 { buf1, sizeof (buf1) },
79 { buf2, sizeof (buf2) },
81 TEST_COMPARE (PREADV (temp_fd, iov, array_length (iov), -1),
82 sizeof (buf1) + sizeof (buf2));
83 TEST_COMPARE (memcmp ("2ab", buf1, sizeof (buf1)), 0);
84 TEST_COMPARE (memcmp ("cx", buf2, sizeof (buf2)), 0);
85 TEST_COMPARE (xlseek (temp_fd, 0, SEEK_CUR), 6);
88 xftruncate (temp_fd, 0);
91 static int
92 do_test_with_offset (off_t offset)
94 struct iovec iov[2];
95 ssize_t ret;
97 char buf1[32];
98 char buf2[64];
100 memset (buf1, 0xf0, sizeof buf1);
101 memset (buf2, 0x0f, sizeof buf2);
103 /* Write two buffer with 32 and 64 bytes respectively. */
104 memset (iov, 0, sizeof iov);
105 iov[0].iov_base = buf1;
106 iov[0].iov_len = sizeof buf1;
107 iov[1].iov_base = buf2;
108 iov[1].iov_len = sizeof buf2;
110 ret = PWRITEV (temp_fd, iov, 2, offset);
111 if (ret == -1)
112 FAIL_RET ("first pwritev returned -1");
113 if (ret != (sizeof buf1 + sizeof buf2))
114 FAIL_RET ("first pwritev returned an unexpected value");
116 ret = PWRITEV (temp_fd, iov, 2, sizeof buf1 + sizeof buf2 + offset);
117 if (ret == -1)
118 FAIL_RET ("second pwritev returned -1");
119 if (ret != (sizeof buf1 + sizeof buf2))
120 FAIL_RET ("second pwritev returned an unexpected value");
122 char buf3[32];
123 char buf4[64];
125 memset (buf3, 0x0f, sizeof buf3);
126 memset (buf4, 0xf0, sizeof buf4);
128 iov[0].iov_base = buf3;
129 iov[0].iov_len = sizeof buf3;
130 iov[1].iov_base = buf4;
131 iov[1].iov_len = sizeof buf4;
133 /* Now read two buffer with 32 and 64 bytes respectively. */
134 ret = PREADV (temp_fd, iov, 2, offset);
135 if (ret == -1)
136 FAIL_RET ("first preadv returned -1");
137 if (ret != (sizeof buf3 + sizeof buf4))
138 FAIL_RET ("first preadv returned an unexpected value");
140 if (memcmp (buf1, buf3, sizeof buf1) != 0)
141 FAIL_RET ("first buffer from first preadv different than expected");
142 if (memcmp (buf2, buf4, sizeof buf2) != 0)
143 FAIL_RET ("second buffer from first preadv different than expected");
145 ret = PREADV (temp_fd, iov, 2, sizeof buf3 + sizeof buf4 + offset);
146 if (ret == -1)
147 FAIL_RET ("second preadv returned -1");
148 if (ret != (sizeof buf3 + sizeof buf4))
149 FAIL_RET ("second preadv returned an unexpected value");
151 /* And compare the buffers read and written to check if there are equal. */
152 if (memcmp (buf1, buf3, sizeof buf1) != 0)
153 FAIL_RET ("first buffer from second preadv different than expected");
154 if (memcmp (buf2, buf4, sizeof buf2) != 0)
155 FAIL_RET ("second buffer from second preadv different than expected");
157 return 0;
160 #include <support/test-driver.c>