1 /* Test glob danglin symlink match (BZ #866).
2 Copyright (C) 2017 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 <http://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
);
60 static char valid_link
[PATH_MAX
];
61 static char dangling_link
[PATH_MAX
];
62 static char dangling_dir
[PATH_MAX
];
65 do_prepare (int argc
, char *argv
[])
69 create_temp_file ("tst-glob_symlinks.", &fname
);
71 /* Create an existing symlink. */
72 create_link ("valid-symlink-tst-glob_symlinks", fname
, valid_link
,
75 /* Create a dangling symlink to a file. */
76 int fd
= create_temp_file ("dangling-tst-glob_file", &fname
);
77 TEST_VERIFY_EXIT (close (fd
) == 0);
78 /* It throws a warning at process end due 'add_temp_file' trying to
80 TEST_VERIFY_EXIT (unlink (fname
) == 0);
81 create_link ("dangling-symlink-file-tst-glob", fname
, dangling_link
,
82 sizeof dangling_link
);
84 /* Create a dangling symlink to a directory. */
85 char tmpdir
[PATH_MAX
];
86 snprintf (tmpdir
, sizeof tmpdir
, "%s/dangling-tst-glob_folder.XXXXXX",
88 TEST_VERIFY_EXIT (mkdtemp (tmpdir
) != NULL
);
89 create_link ("dangling-symlink-dir-tst-glob", tmpdir
, dangling_dir
,
91 TEST_VERIFY_EXIT (rmdir (tmpdir
) == 0);
100 TEST_VERIFY_EXIT (glob (valid_link
, 0, NULL
, &gl
) == 0);
101 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
102 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], valid_link
) == 0);
105 TEST_VERIFY_EXIT (glob (dangling_link
, 0, NULL
, &gl
) == 0);
106 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
107 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], dangling_link
) == 0);
110 TEST_VERIFY_EXIT (glob (dangling_dir
, 0, NULL
, &gl
) == 0);
111 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
112 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], dangling_dir
) == 0);
115 snprintf (buf
, sizeof buf
, "%s", dangling_link
);
116 buf
[strlen(buf
) - 1] = '?';
117 TEST_VERIFY_EXIT (glob (buf
, 0, NULL
, &gl
) == 0);
118 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
119 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], dangling_link
) == 0);
122 /* glob should handle dangling symbol as normal file, so <file>? should
123 return an empty string. */
124 snprintf (buf
, sizeof buf
, "%s?", dangling_link
);
125 TEST_VERIFY_EXIT (glob (buf
, 0, NULL
, &gl
) != 0);
128 snprintf (buf
, sizeof buf
, "%s*", dangling_link
);
129 TEST_VERIFY_EXIT (glob (buf
, 0, NULL
, &gl
) == 0);
130 TEST_VERIFY_EXIT (gl
.gl_pathc
== 1);
131 TEST_VERIFY_EXIT (strcmp (gl
.gl_pathv
[0], dangling_link
) == 0);