Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / tst-sync_file_range.c
blobb0a6104fd0fdd83f2d35699cb200a25d2c68cd64
1 /* Basic sync_file_range (not specific flag is checked).
2 Copyright (C) 2016-2017 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 /* sync_file_range is only define for LFS. */
20 #define _FILE_OFFSET_BITS 64
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <sys/stat.h>
27 #include <support/temp_file.h>
28 #include <support/check.h>
30 #define XSTR(s) STR(S)
31 #define STR(s) #s
33 static char *temp_filename;
34 static int temp_fd;
36 static char fifoname[] = "/tmp/tst-posix_fadvise-fifo-XXXXXX";
37 static int fifofd;
39 void
40 do_prepare (int argc, char **argv)
42 temp_fd = create_temp_file ("tst-file_sync_range.", &temp_filename);
43 if (temp_fd == -1)
44 FAIL_EXIT1 ("cannot create temporary file: %m");
46 if (mktemp (fifoname) == NULL)
47 FAIL_EXIT1 ("cannot generate temp file name: %m");
48 add_temp_file (fifoname);
50 if (mkfifo (fifoname, S_IWUSR | S_IRUSR) != 0)
51 FAIL_EXIT1 ("cannot create fifo: %m");
53 fifofd = open (fifoname, O_RDONLY | O_NONBLOCK);
54 if (fifofd == -1)
55 FAIL_EXIT1 ("cannot open fifo: %m");
57 #define PREPARE do_prepare
59 static int
60 do_test (void)
62 int ret;
64 /* This tests first check for some invalid usage and then check for
65 a simple usage. It does not cover for all possible issue since for
66 EIO/ENOMEM/ENOSPC would require to create very specific scenarios that
67 are outside the current test coverage (basically correct kernel argument
68 passing. */
70 /* Check for invalid file descriptor. */
71 if ((ret = sync_file_range (-1, 0, 0, 0)) != -1)
72 FAIL_EXIT1 ("sync_file_range did not fail on an invalid descriptor "
73 "(returned %d, expected -1)", ret);
74 if (errno != EBADF)
75 FAIL_EXIT1 ("sync_file_range on an invalid descriptor did not set errno to "
76 "EBADF (%d)", errno);
78 if ((ret = sync_file_range (fifofd, 0, 0, 0)) != -1)
79 FAIL_EXIT1 ("sync_file_range did not fail on an invalid descriptor "
80 "(returned %d, expected -1)", ret);
81 if (errno != ESPIPE)
82 FAIL_EXIT1 ("sync_file_range on an invalid descriptor did not set errno to "
83 "EBADF (%d)", errno);
85 /* Check for invalid flags (it must be
86 SYNC_FILE_RANGE_{WAIT_BEFORE,WRITE,WAIT_AFTER) or a 'or' combination of
87 them. */
88 if ((ret = sync_file_range (temp_fd, 0, 0, -1)) != -1)
89 FAIL_EXIT1 ("sync_file_range did not failed with invalid flags "
90 "(returned %d, " "expected -1)", ret);
91 if (errno != EINVAL)
92 FAIL_EXIT1 ("sync_file_range with invalid flag did not set errno to "
93 "EINVAL (%d)", errno);
95 /* Check for negative offset. */
96 if ((ret = sync_file_range (temp_fd, -1, 1, 0)) != -1)
97 FAIL_EXIT1 ("sync_file_range did not failed with invalid offset "
98 "(returned %d, expected -1)", ret);
99 if (errno != EINVAL)
100 FAIL_EXIT1 ("sync_file_range with invalid offset did not set errno to "
101 "EINVAL (%d)", errno);
103 /* offset + nbytes must be a positive value. */
104 if ((ret = sync_file_range (temp_fd, 1024, -2048, 0)) != -1)
105 FAIL_EXIT1 ("sync_file_range did not failed with invalid nbytes (returned %d, "
106 "expected -1)", ret);
107 if (errno != EINVAL)
108 FAIL_EXIT1 ("sync_file_range with invalid offset did not set errno to "
109 "EINVAL (%d)", errno);
111 /* offset + nbytes must be larger or equal than offset */
112 if ((ret = sync_file_range (temp_fd, -1024, 1024, 0)) != -1)
113 FAIL_EXIT1 ("sync_file_range did not failed with invalid offset "
114 "(returned %d, expected -1)", ret);
115 if (errno != EINVAL)
116 FAIL_EXIT1 ("sync_file_range with invalid offset did not set errno to "
117 "EINVAL (%d)", errno);
119 /* Check simple successful case. */
120 if ((ret = sync_file_range (temp_fd, 0, 1024, 0)) == -1)
121 FAIL_EXIT1 ("sync_file_range failed (errno = %d)", errno);
123 /* Finally check also a successful case with a 64-bit offset. */
124 off_t large_offset = UINT32_MAX + 2048LL;
125 if ((ret = sync_file_range (temp_fd, large_offset, 1024, 0)) == -1)
126 FAIL_EXIT1 ("sync_file_range failed (errno = %d)", errno);
128 return 0;
131 #include <support/test-driver.c>