Mercurial > gnulib
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 |
rev | line source |
---|---|
11934 | 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 | 3 |
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. | |
8 | |
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. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
11934 | 16 |
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */ | |
18 | |
19 #include <config.h> | |
20 | |
21 #include <dirent.h> | |
22 | |
12489 | 23 #include "signature.h" |
24 SIGNATURE_CHECK (fdopendir, DIR *, (int)); | |
25 | |
11934 | 26 #include <errno.h> |
27 #include <fcntl.h> | |
28 #include <unistd.h> | |
29 | |
12496
a48d3d749ca5
Refactor common macros used in tests.
Bruno Haible <bruno@clisp.org>
parents:
12489
diff
changeset
|
30 #include "macros.h" |
11934 | 31 |
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 | 34 { |
35 DIR *d; | |
36 int fd; | |
37 | |
38 /* A non-directory cannot be turned into a directory stream. */ | |
12127 | 39 fd = open ("test-fdopendir.tmp", O_RDONLY | O_CREAT, 0600); |
11934 | 40 ASSERT (0 <= fd); |
41 errno = 0; | |
42 ASSERT (fdopendir (fd) == NULL); | |
43 ASSERT (errno == ENOTDIR); | |
44 ASSERT (close (fd) == 0); | |
12127 | 45 ASSERT (unlink ("test-fdopendir.tmp") == 0); |
11934 | 46 |
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 | 59 |
60 /* This should work. */ | |
61 fd = open (".", O_RDONLY); | |
62 ASSERT (0 <= fd); | |
63 d = fdopendir (fd); | |
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 | 73 ASSERT (closedir (d) == 0); |
74 /* Now we can guarantee that fd must be closed. */ | |
75 errno = 0; | |
76 ASSERT (dup2 (fd, fd) == -1); | |
77 ASSERT (errno == EBADF); | |
78 | |
79 return 0; | |
80 } |