1 /* Test glob danglin symlink match (BZ #866).
2 Copyright (C) 2017-2020 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 <https://www.gnu.org/licenses/>. */
28 #include <sys/types.h>
30 #include <support/check.h>
31 #include <support/temp_file.h>
33 static void do_prepare (int argc
, char *argv
[]);
34 #define PREPARE do_prepare
35 static int do_test (void);
36 #include <support/test-driver.c>
38 /* Maximum number of symlink calls for create_link function. */
39 #define MAX_CREATE_LINK_TRIES 10
42 create_link (const char *base
, const char *fname
, char *linkname
,
48 snprintf (linkname
, linknamesize
, "%s/%s%02d", test_dir
, base
,
50 if (symlink (fname
, linkname
) == 0)
53 FAIL_EXIT1 ("symlink failed: %m");
54 if (ntries
++ == MAX_CREATE_LINK_TRIES
)
55 FAIL_EXIT1 ("symlink failed with EEXIST too many times");
57 add_temp_file (linkname
);
61 # define PATH_MAX 1024
63 static char valid_link
[PATH_MAX
];
64 static char dangling_link
[PATH_MAX
];
65 static char dangling_dir
[PATH_MAX
];
68 do_prepare (int argc
, char *argv
[])
72 create_temp_file ("tst-glob_symlinks.", &fname
);
74 /* Create an existing symlink. */
75 create_link ("valid-symlink-tst-glob_symlinks", fname
, valid_link
,
78 /* Create a dangling symlink to a file. */
79 int fd
= create_temp_file ("dangling-tst-glob_file", &fname
);
80 TEST_VERIFY_EXIT (close (fd
) == 0);
81 /* It throws a warning at process end due 'add_temp_file' trying to
83 TEST_VERIFY_EXIT (unlink (fname
) == 0);
84 create_link ("dangling-symlink-file-tst-glob", fname
, dangling_link
,
85 sizeof dangling_link
);
87 /* Create a dangling symlink to a directory. */
88 char tmpdir
[PATH_MAX
];
89 snprintf (tmpdir
, sizeof tmpdir
, "%s/dangling-tst-glob_folder.XXXXXX",
91 TEST_VERIFY_EXIT (mkdtemp (tmpdir
) != NULL
);
92 create_link ("dangling-symlink-dir-tst-glob", tmpdir
, dangling_dir
,
94 TEST_VERIFY_EXIT (rmdir (tmpdir
) == 0);
100 char buf
[PATH_MAX
+ 1];
103 TEST_VERIFY_EXIT (glob (valid_link
, 0, NULL
, &gl
) == 0);
104 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
105 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], valid_link
) == 0);
108 TEST_VERIFY_EXIT (glob (dangling_link
, 0, NULL
, &gl
) == 0);
109 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
110 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], dangling_link
) == 0);
113 TEST_VERIFY_EXIT (glob (dangling_dir
, 0, NULL
, &gl
) == 0);
114 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
115 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], dangling_dir
) == 0);
118 snprintf (buf
, sizeof buf
, "%s", dangling_link
);
119 buf
[strlen(buf
) - 1] = '?';
120 TEST_VERIFY_EXIT (glob (buf
, 0, NULL
, &gl
) == 0);
121 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
122 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], dangling_link
) == 0);
125 /* glob should handle dangling symbol as normal file, so <file>? should
126 return an empty string. */
127 snprintf (buf
, sizeof buf
, "%s?", dangling_link
);
128 TEST_VERIFY_EXIT (glob (buf
, 0, NULL
, &gl
) != 0);
131 snprintf (buf
, sizeof buf
, "%s*", dangling_link
);
132 TEST_VERIFY_EXIT (glob (buf
, 0, NULL
, &gl
) == 0);
133 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
134 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], dangling_link
) == 0);