Blame posix/tst-glob_symlinks.c

Packit Service 82fcde
/* Test glob danglin symlink match (BZ #866).
Packit Service 82fcde
   Copyright (C) 2017-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <string.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <limits.h>
Packit Service 82fcde
#include <stddef.h>
Packit Service 82fcde
#include <glob.h>
Packit Service 82fcde
#include <sys/stat.h>
Packit Service 82fcde
#include <sys/types.h>
Packit Service 82fcde
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
#include <support/temp_file.h>
Packit Service 82fcde
Packit Service 82fcde
static void do_prepare (int argc, char *argv[]);
Packit Service 82fcde
#define PREPARE do_prepare
Packit Service 82fcde
static int do_test (void);
Packit Service 82fcde
#include <support/test-driver.c>
Packit Service 82fcde
Packit Service 82fcde
/* Maximum number of symlink calls for create_link function.  */
Packit Service 82fcde
#define MAX_CREATE_LINK_TRIES 10
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
create_link (const char *base, const char *fname, char *linkname,
Packit Service 82fcde
	     size_t linknamesize)
Packit Service 82fcde
{
Packit Service 82fcde
  int ntries = 0;
Packit Service 82fcde
  while (1)
Packit Service 82fcde
    {
Packit Service 82fcde
      snprintf (linkname, linknamesize, "%s/%s%02d", test_dir, base,
Packit Service 82fcde
		ntries);
Packit Service 82fcde
      if (symlink (fname, linkname) == 0)
Packit Service 82fcde
	break;
Packit Service 82fcde
      if (errno != EEXIST)
Packit Service 82fcde
	FAIL_EXIT1 ("symlink failed: %m");
Packit Service 82fcde
      if (ntries++ == MAX_CREATE_LINK_TRIES)
Packit Service 82fcde
	FAIL_EXIT1 ("symlink failed with EEXIST too many times");
Packit Service 82fcde
    }
Packit Service 82fcde
  add_temp_file (linkname);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#ifndef PATH_MAX
Packit Service 82fcde
# define PATH_MAX 1024
Packit Service 82fcde
#endif
Packit Service 82fcde
static char valid_link[PATH_MAX];
Packit Service 82fcde
static char dangling_link[PATH_MAX];
Packit Service 82fcde
static char dangling_dir[PATH_MAX];
Packit Service 82fcde
Packit Service 82fcde
static void
Packit Service 82fcde
do_prepare (int argc, char *argv[])
Packit Service 82fcde
{
Packit Service 82fcde
  char *fname;
Packit Service 82fcde
Packit Service 82fcde
  create_temp_file ("tst-glob_symlinks.", &fname);
Packit Service 82fcde
Packit Service 82fcde
  /* Create an existing symlink.  */
Packit Service 82fcde
  create_link ("valid-symlink-tst-glob_symlinks", fname, valid_link,
Packit Service 82fcde
	       sizeof valid_link);
Packit Service 82fcde
Packit Service 82fcde
  /* Create a dangling symlink to a file.  */
Packit Service 82fcde
  int fd = create_temp_file ("dangling-tst-glob_file", &fname);
Packit Service 82fcde
  TEST_VERIFY_EXIT (close (fd) == 0);
Packit Service 82fcde
  /* It throws a warning at process end due 'add_temp_file' trying to
Packit Service 82fcde
     unlink it again.  */
Packit Service 82fcde
  TEST_VERIFY_EXIT (unlink (fname) == 0);
Packit Service 82fcde
  create_link ("dangling-symlink-file-tst-glob", fname, dangling_link,
Packit Service 82fcde
	       sizeof dangling_link);
Packit Service 82fcde
Packit Service 82fcde
  /* Create a dangling symlink to a directory.  */
Packit Service 82fcde
  char tmpdir[PATH_MAX];
Packit Service 82fcde
  snprintf (tmpdir, sizeof tmpdir, "%s/dangling-tst-glob_folder.XXXXXX",
Packit Service 82fcde
	    test_dir);
Packit Service 82fcde
  TEST_VERIFY_EXIT (mkdtemp (tmpdir) != NULL);
Packit Service 82fcde
  create_link ("dangling-symlink-dir-tst-glob", tmpdir, dangling_dir,
Packit Service 82fcde
	       sizeof dangling_dir);
Packit Service 82fcde
  TEST_VERIFY_EXIT (rmdir (tmpdir) == 0);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  char buf[PATH_MAX + 1];
Packit Service 82fcde
  glob_t gl;
Packit Service 82fcde
Packit Service 82fcde
  TEST_VERIFY_EXIT (glob (valid_link, 0, NULL, &gl) == 0);
Packit Service 82fcde
  TEST_VERIFY_EXIT (gl.gl_pathc == 1);
Packit Service 82fcde
  TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], valid_link) == 0);
Packit Service 82fcde
  globfree (&gl);
Packit Service 82fcde
Packit Service 82fcde
  TEST_VERIFY_EXIT (glob (dangling_link, 0, NULL, &gl) == 0);
Packit Service 82fcde
  TEST_VERIFY_EXIT (gl.gl_pathc == 1);
Packit Service 82fcde
  TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0);
Packit Service 82fcde
  globfree (&gl);
Packit Service 82fcde
Packit Service 82fcde
  TEST_VERIFY_EXIT (glob (dangling_dir, 0, NULL, &gl) == 0);
Packit Service 82fcde
  TEST_VERIFY_EXIT (gl.gl_pathc == 1);
Packit Service 82fcde
  TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_dir) == 0);
Packit Service 82fcde
  globfree (&gl);
Packit Service 82fcde
Packit Service 82fcde
  snprintf (buf, sizeof buf, "%s", dangling_link);
Packit Service 82fcde
  buf[strlen(buf) - 1] = '?';
Packit Service 82fcde
  TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) == 0);
Packit Service 82fcde
  TEST_VERIFY_EXIT (gl.gl_pathc == 1);
Packit Service 82fcde
  TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0);
Packit Service 82fcde
  globfree (&gl);
Packit Service 82fcde
Packit Service 82fcde
  /* glob should handle dangling symbol as normal file, so <file>? should
Packit Service 82fcde
     return an empty string.  */
Packit Service 82fcde
  snprintf (buf, sizeof buf, "%s?", dangling_link);
Packit Service 82fcde
  TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) != 0);
Packit Service 82fcde
  globfree (&gl);
Packit Service 82fcde
Packit Service 82fcde
  snprintf (buf, sizeof buf, "%s*", dangling_link);
Packit Service 82fcde
  TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) == 0);
Packit Service 82fcde
  TEST_VERIFY_EXIT (gl.gl_pathc == 1);
Packit Service 82fcde
  TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0);
Packit Service 82fcde
  globfree (&gl);
Packit Service 82fcde
Packit Service 82fcde
  return 0;
Packit Service 82fcde
}