annotate tests/test-fdopendir.c @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 10eb9086bea0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Test opening a directory stream from a file descriptor.
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 (at your option) any later version.
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19 #include <config.h>
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include <dirent.h>
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22
12489
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
23 #include "signature.h"
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
24 SIGNATURE_CHECK (fdopendir, DIR *, (int));
33ab12a7cea2 tests: add signature checks
Eric Blake <ebb9@byu.net>
parents: 12421
diff changeset
25
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26 #include <errno.h>
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27 #include <fcntl.h>
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28 #include <unistd.h>
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29
12496
a48d3d749ca5 Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents: 12489
diff changeset
30 #include "macros.h"
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 int
15287
223683be3038 openat, fdopendir tests: Fix link errors.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
33 main (int argc _GL_UNUSED, char *argv[])
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34 {
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
35 DIR *d;
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
36 int fd;
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
38 /* A non-directory cannot be turned into a directory stream. */
12127
bda9467a9d66 fdopendir: fix GNU/Hurd bug
Eric Blake <ebb9@byu.net>
parents: 11934
diff changeset
39 fd = open ("test-fdopendir.tmp", O_RDONLY | O_CREAT, 0600);
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40 ASSERT (0 <= fd);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
41 errno = 0;
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 ASSERT (fdopendir (fd) == NULL);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 ASSERT (errno == ENOTDIR);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
44 ASSERT (close (fd) == 0);
12127
bda9467a9d66 fdopendir: fix GNU/Hurd bug
Eric Blake <ebb9@byu.net>
parents: 11934
diff changeset
45 ASSERT (unlink ("test-fdopendir.tmp") == 0);
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
47 /* A bad fd cannot be turned into a stream. */
15701
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
48 {
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
49 errno = 0;
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
50 ASSERT (fdopendir (-1) == NULL);
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
51 ASSERT (errno == EBADF);
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
52 }
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
53 {
17292
3a09cc104f4c tests: don't assume fd 99 is closed
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
54 close (99);
15701
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
55 errno = 0;
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
56 ASSERT (fdopendir (99) == NULL);
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
57 ASSERT (errno == EBADF);
892fd4630714 fdopendir tests: EBADF tests.
Bruno Haible <bruno@clisp.org>
parents: 15287
diff changeset
58 }
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 /* This should work. */
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
61 fd = open (".", O_RDONLY);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
62 ASSERT (0 <= fd);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
63 d = fdopendir (fd);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
64 ASSERT (d);
17947
101ada775ae6 fdopendir-tests: test it does not close its arg
Paul Eggert <eggert@cs.ucla.edu>
parents: 17848
diff changeset
65 /* fdopendir should not close fd. */
101ada775ae6 fdopendir-tests: test it does not close its arg
Paul Eggert <eggert@cs.ucla.edu>
parents: 17848
diff changeset
66 ASSERT (dup2 (fd, fd) == fd);
101ada775ae6 fdopendir-tests: test it does not close its arg
Paul Eggert <eggert@cs.ucla.edu>
parents: 17848
diff changeset
67
101ada775ae6 fdopendir-tests: test it does not close its arg
Paul Eggert <eggert@cs.ucla.edu>
parents: 17848
diff changeset
68 /* Don't test dirfd here. dirfd (d) must return fd on current POSIX
101ada775ae6 fdopendir-tests: test it does not close its arg
Paul Eggert <eggert@cs.ucla.edu>
parents: 17848
diff changeset
69 platforms, but on pre-2008 platforms or on non-POSIX platforms
101ada775ae6 fdopendir-tests: test it does not close its arg
Paul Eggert <eggert@cs.ucla.edu>
parents: 17848
diff changeset
70 dirfd (fd) might return some other descriptor, or -1, and gnulib
101ada775ae6 fdopendir-tests: test it does not close its arg
Paul Eggert <eggert@cs.ucla.edu>
parents: 17848
diff changeset
71 does not work around this porting problem. */
101ada775ae6 fdopendir-tests: test it does not close its arg
Paul Eggert <eggert@cs.ucla.edu>
parents: 17848
diff changeset
72
11934
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
73 ASSERT (closedir (d) == 0);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 /* Now we can guarantee that fd must be closed. */
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
75 errno = 0;
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
76 ASSERT (dup2 (fd, fd) == -1);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
77 ASSERT (errno == EBADF);
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
78
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
79 return 0;
80567a8f98f8 fdopendir: split into its own module
Eric Blake <ebb9@byu.net>
parents:
diff changeset
80 }