]>
Commit | Line | Data |
---|---|---|
46870067 | 1 | /* Common definitions for preadv and pwritev. |
688903eb | 2 | Copyright (C) 2016-2018 Free Software Foundation, Inc. |
46870067 AZ |
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 | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the 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; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
d4b4a00a | 19 | #include <array_length.h> |
ffd60238 AZ |
20 | #include <stdio.h> |
21 | #include <stdint.h> | |
52bd9381 | 22 | #include <errno.h> |
ffd60238 | 23 | #include <string.h> |
46870067 | 24 | #include <sys/uio.h> |
ceaa9889 | 25 | #include <sys/stat.h> |
46870067 | 26 | |
ffd60238 AZ |
27 | #include <support/check.h> |
28 | #include <support/temp_file.h> | |
d4b4a00a | 29 | #include <support/xunistd.h> |
46870067 AZ |
30 | |
31 | static char *temp_filename; | |
32 | static int temp_fd; | |
33 | ||
ffd60238 AZ |
34 | static int do_test (void); |
35 | ||
46870067 | 36 | static void |
ffd60238 | 37 | do_prepare (int argc, char **argv) |
46870067 AZ |
38 | { |
39 | temp_fd = create_temp_file ("tst-preadvwritev.", &temp_filename); | |
40 | if (temp_fd == -1) | |
ffd60238 | 41 | FAIL_EXIT1 ("cannot create temporary file"); |
46870067 | 42 | } |
ffd60238 | 43 | #define PREPARE do_prepare |
46870067 | 44 | |
52bd9381 AZ |
45 | #ifndef PREADV |
46 | # define PREADV(__fd, __iov, __iovcnt, __offset) \ | |
47 | preadv (__fd, __iov, __iovcnt, __offset) | |
48 | #endif | |
49 | ||
50 | #ifndef PWRITEV | |
51 | # define PWRITEV(__fd, __iov, __iovcnt, __offset) \ | |
52 | pwritev (__fd, __iov, __iovcnt, __offset) | |
53 | #endif | |
54 | ||
d4b4a00a FW |
55 | static __attribute__ ((unused)) void |
56 | do_test_without_offset (void) | |
57 | { | |
58 | xftruncate (temp_fd, 0); | |
59 | ||
60 | xwrite (temp_fd, "123", 3); | |
61 | xlseek (temp_fd, 2, SEEK_SET); | |
62 | { | |
63 | struct iovec iov[] = | |
64 | { | |
65 | { (void *) "abc", 3 }, | |
66 | { (void *) "xyzt", 4 }, | |
67 | }; | |
68 | TEST_COMPARE (PWRITEV (temp_fd, iov, array_length (iov), -1), 7); | |
69 | } | |
70 | TEST_COMPARE (xlseek (temp_fd, 0, SEEK_CUR), 9); | |
71 | ||
72 | xlseek (temp_fd, 1, SEEK_SET); | |
73 | char buf1[3]; | |
74 | char buf2[2]; | |
75 | { | |
76 | struct iovec iov[] = | |
77 | { | |
78 | { buf1, sizeof (buf1) }, | |
79 | { buf2, sizeof (buf2) }, | |
80 | }; | |
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); | |
86 | } | |
87 | ||
88 | xftruncate (temp_fd, 0); | |
89 | } | |
90 | ||
46870067 AZ |
91 | static int |
92 | do_test_with_offset (off_t offset) | |
93 | { | |
94 | struct iovec iov[2]; | |
95 | ssize_t ret; | |
96 | ||
97 | char buf1[32]; | |
98 | char buf2[64]; | |
99 | ||
100 | memset (buf1, 0xf0, sizeof buf1); | |
101 | memset (buf2, 0x0f, sizeof buf2); | |
102 | ||
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; | |
109 | ||
52bd9381 | 110 | ret = PWRITEV (temp_fd, iov, 2, offset); |
46870067 | 111 | if (ret == -1) |
ffd60238 | 112 | FAIL_RET ("first pwritev returned -1"); |
46870067 | 113 | if (ret != (sizeof buf1 + sizeof buf2)) |
ffd60238 | 114 | FAIL_RET ("first pwritev returned an unexpected value"); |
46870067 | 115 | |
52bd9381 | 116 | ret = PWRITEV (temp_fd, iov, 2, sizeof buf1 + sizeof buf2 + offset); |
46870067 | 117 | if (ret == -1) |
ffd60238 | 118 | FAIL_RET ("second pwritev returned -1"); |
46870067 | 119 | if (ret != (sizeof buf1 + sizeof buf2)) |
ffd60238 | 120 | FAIL_RET ("second pwritev returned an unexpected value"); |
46870067 AZ |
121 | |
122 | char buf3[32]; | |
123 | char buf4[64]; | |
124 | ||
125 | memset (buf3, 0x0f, sizeof buf3); | |
126 | memset (buf4, 0xf0, sizeof buf4); | |
127 | ||
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; | |
132 | ||
133 | /* Now read two buffer with 32 and 64 bytes respectively. */ | |
52bd9381 | 134 | ret = PREADV (temp_fd, iov, 2, offset); |
46870067 | 135 | if (ret == -1) |
ffd60238 | 136 | FAIL_RET ("first preadv returned -1"); |
46870067 | 137 | if (ret != (sizeof buf3 + sizeof buf4)) |
ffd60238 | 138 | FAIL_RET ("first preadv returned an unexpected value"); |
46870067 AZ |
139 | |
140 | if (memcmp (buf1, buf3, sizeof buf1) != 0) | |
ffd60238 | 141 | FAIL_RET ("first buffer from first preadv different than expected"); |
46870067 | 142 | if (memcmp (buf2, buf4, sizeof buf2) != 0) |
ffd60238 | 143 | FAIL_RET ("second buffer from first preadv different than expected"); |
46870067 | 144 | |
52bd9381 | 145 | ret = PREADV (temp_fd, iov, 2, sizeof buf3 + sizeof buf4 + offset); |
46870067 | 146 | if (ret == -1) |
ffd60238 | 147 | FAIL_RET ("second preadv returned -1"); |
46870067 | 148 | if (ret != (sizeof buf3 + sizeof buf4)) |
ffd60238 | 149 | FAIL_RET ("second preadv returned an unexpected value"); |
46870067 AZ |
150 | |
151 | /* And compare the buffers read and written to check if there are equal. */ | |
152 | if (memcmp (buf1, buf3, sizeof buf1) != 0) | |
ffd60238 | 153 | FAIL_RET ("first buffer from second preadv different than expected"); |
46870067 | 154 | if (memcmp (buf2, buf4, sizeof buf2) != 0) |
ffd60238 | 155 | FAIL_RET ("second buffer from second preadv different than expected"); |
46870067 AZ |
156 | |
157 | return 0; | |
158 | } | |
ffd60238 AZ |
159 | |
160 | #include <support/test-driver.c> |