Mercurial > hg > octave-lojdl > gnulib-hg
annotate tests/test-readlink.h @ 17476:6057744acd2c default tip master
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Fri, 16 Aug 2013 06:32:22 -0700 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
12061 | 1 /* Tests of readlink. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16201
diff
changeset
|
2 Copyright (C) 2009-2013 Free Software Foundation, Inc. |
12061 | 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 | |
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
16 | |
17 /* Written by Eric Blake <ebb9@byu.net>, 2009. */ | |
18 | |
19 /* This file is designed to test both readlink(a,b,c) and | |
20 readlinkat(AT_FDCWD,a,b,c). FUNC is the function to test. Assumes | |
21 that BASE and ASSERT are already defined, and that appropriate | |
22 headers are already included. If PRINT, warn before skipping | |
23 symlink tests with status 77. */ | |
24 | |
25 static int | |
26 test_readlink (ssize_t (*func) (char const *, char *, size_t), bool print) | |
27 { | |
28 char buf[80]; | |
29 | |
30 /* Sanity checks of failures. Mingw lacks symlink, but readlink can | |
31 still distinguish between various errors. */ | |
32 memset (buf, 0xff, sizeof buf); | |
33 errno = 0; | |
34 ASSERT (func ("no_such", buf, sizeof buf) == -1); | |
35 ASSERT (errno == ENOENT); | |
36 errno = 0; | |
37 ASSERT (func ("no_such/", buf, sizeof buf) == -1); | |
38 ASSERT (errno == ENOENT); | |
39 errno = 0; | |
40 ASSERT (func ("", buf, sizeof buf) == -1); | |
14505
ece10d05a1cf
tests: readlink* ("",... fails with EINVAL on newer kernels
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
41 ASSERT (errno == ENOENT || errno == EINVAL); |
12061 | 42 errno = 0; |
43 ASSERT (func (".", buf, sizeof buf) == -1); | |
44 ASSERT (errno == EINVAL); | |
45 errno = 0; | |
46 ASSERT (func ("./", buf, sizeof buf) == -1); | |
47 ASSERT (errno == EINVAL); | |
48 ASSERT (close (creat (BASE "file", 0600)) == 0); | |
49 errno = 0; | |
50 ASSERT (func (BASE "file", buf, sizeof buf) == -1); | |
51 ASSERT (errno == EINVAL); | |
52 errno = 0; | |
53 ASSERT (func (BASE "file/", buf, sizeof buf) == -1); | |
13511
d81cdd1015af
readlink: Relax test a bit.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
54 ASSERT (errno == ENOTDIR || errno == EINVAL); /* AIX yields EINVAL */ |
12061 | 55 |
56 /* Now test actual symlinks. */ | |
57 if (symlink (BASE "dir", BASE "link")) | |
58 { | |
12265 | 59 ASSERT (unlink (BASE "file") == 0); |
12061 | 60 if (print) |
12146
e076a06bae48
maint: prefer 'file system' over 'filesystem'
Eric Blake <ebb9@byu.net>
parents:
12061
diff
changeset
|
61 fputs ("skipping test: symlinks not supported on this file system\n", |
12061 | 62 stderr); |
63 return 77; | |
64 } | |
65 ASSERT (mkdir (BASE "dir", 0700) == 0); | |
66 errno = 0; | |
67 ASSERT (func (BASE "link/", buf, sizeof buf) == -1); | |
68 ASSERT (errno == EINVAL); | |
12265 | 69 ASSERT (symlink (BASE "link", BASE "link2") == 0); |
70 errno = 0; | |
71 ASSERT (func (BASE "link2/", buf, sizeof buf) == -1); | |
72 ASSERT (errno == EINVAL); | |
73 ASSERT (unlink (BASE "link2") == 0); | |
74 ASSERT (symlink (BASE "file", BASE "link2") == 0); | |
75 errno = 0; | |
76 ASSERT (func (BASE "link2/", buf, sizeof buf) == -1); | |
13525
ac49cf58cb8b
readlink, areadlink: Relax test a bit.
Bruno Haible <bruno@clisp.org>
parents:
13511
diff
changeset
|
77 ASSERT (errno == ENOTDIR || errno == EINVAL); /* AIX yields EINVAL */ |
12265 | 78 ASSERT (unlink (BASE "file") == 0); |
79 ASSERT (unlink (BASE "link2") == 0); | |
12061 | 80 { |
81 /* Up till now, no readlink has been successful, so buf should be | |
82 unchanged. */ | |
83 int i; | |
84 for (i = 0; i < sizeof buf; i++) | |
85 ASSERT (buf[i] == (char) 0xff); | |
86 } | |
87 { | |
88 size_t len = strlen (BASE "dir"); | |
89 /* When passing too small of a buffer, expect the truncated | |
90 length, or an ERANGE failure. However, a size of 0 is not | |
91 portable enough to test. */ | |
92 ssize_t result; | |
93 errno = 0; | |
94 result = readlink (BASE "link", buf, 1); | |
95 if (result == -1) | |
96 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12265
diff
changeset
|
97 ASSERT (errno == ERANGE); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12265
diff
changeset
|
98 ASSERT (buf[0] == (char) 0xff); |
12061 | 99 } |
100 else | |
101 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12265
diff
changeset
|
102 ASSERT (result == 1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
12265
diff
changeset
|
103 ASSERT (buf[0] == BASE[0]); |
12061 | 104 } |
105 ASSERT (buf[1] == (char) 0xff); | |
106 ASSERT (func (BASE "link", buf, len) == len); | |
107 ASSERT (strncmp (buf, BASE "dir", len) == 0); | |
108 ASSERT (buf[len] == (char) 0xff); | |
109 ASSERT (func (BASE "link", buf, sizeof buf) == len); | |
110 ASSERT (strncmp (buf, BASE "dir", len) == 0); | |
111 /* POSIX says rest of buf is unspecified; but in practice, it is | |
112 either left alone, or NUL-terminated. */ | |
113 ASSERT (buf[len] == '\0' || buf[len] == (char) 0xff); | |
114 } | |
115 ASSERT (rmdir (BASE "dir") == 0); | |
116 ASSERT (unlink (BASE "link") == 0); | |
117 | |
118 return 0; | |
119 } |