maint.mk: Update system header list for #include syntax checks.
[gnulib.git] / tests / test-truncate.c
blob07ad289056f060f33924903b0119f51a42e36587
1 /* Test truncating a file.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 #include <config.h>
19 #include <unistd.h>
21 #include "signature.h"
22 SIGNATURE_CHECK (truncate, int, (const char *, off_t));
24 #include <errno.h>
25 #include <fcntl.h>
27 #include "ignore-value.h"
28 #include "macros.h"
30 #define BASE "test-truncate.t"
32 int
33 main (int argc, char *argv[])
35 /* Clean up any trash from prior testsuite runs. */
36 ignore_value (system ("rm -rf " BASE "*"));
39 int fd = open (BASE "file", O_RDWR | O_TRUNC | O_CREAT, 0600);
40 ASSERT (fd >= 0);
41 ASSERT (write (fd, "Hello", 5) == 5);
42 close (fd);
46 int fd = open (BASE "file", O_RDONLY);
47 ASSERT (fd >= 0);
48 ASSERT (lseek (fd, 0, SEEK_END) == 5);
49 close (fd);
52 /* Test increasing the size. */
53 ASSERT (truncate (BASE "file", 314159) == 0);
55 int fd = open (BASE "file", O_RDONLY);
56 ASSERT (fd >= 0);
57 ASSERT (lseek (fd, 0, SEEK_END) == 314159);
58 close (fd);
61 /* Test reducing the size. */
62 ASSERT (truncate (BASE "file", 3) == 0);
64 int fd = open (BASE "file", O_RDONLY);
65 ASSERT (fd >= 0);
66 ASSERT (lseek (fd, 0, SEEK_END) == 3);
67 close (fd);
70 /* Test reducing the size to 0. */
71 ASSERT (truncate (BASE "file", 0) == 0);
73 int fd = open (BASE "file", O_RDONLY);
74 ASSERT (fd >= 0);
75 ASSERT (lseek (fd, 0, SEEK_END) == 0);
76 close (fd);
79 /* Test behaviour for nonexistent files. */
81 errno = 0;
82 ASSERT (truncate ("/nonexistent", 0) == -1);
83 ASSERT (errno == ENOENT);
85 /* Test behaviour for directories. */
87 errno = 0;
88 ASSERT (truncate (".", 0) == -1);
89 ASSERT (errno == EISDIR
90 || /* on native Windows */ errno == EACCES);
92 /* Test behaviour for trailing slashes. */
94 errno = 0;
95 ASSERT (truncate (BASE "file/", 0) == -1);
96 ASSERT (errno == ENOTDIR
97 || /* on native Windows */ errno == EINVAL);
99 /* Test behaviour for invalid lengths. */
101 errno = 0;
102 ASSERT (truncate (BASE "file", -3) == -1);
103 ASSERT (errno == EINVAL);
106 /* Cleanup. */
107 ASSERT (unlink (BASE "file") == 0);
109 return test_exit_status;