Blame stdlib/test-canon.c

Packit 6c4009
/* Test program for returning the canonical absolute name of a given file.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by David Mosberger <davidm@azstarnet.com>.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* This file must be run from within a directory called "stdlib".  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <fcntl.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
#include <sys/stat.h>
Packit 6c4009
Packit 6c4009
/* Prototype for our test function.  */
Packit 6c4009
extern int do_test (int argc, char *argv[]);
Packit 6c4009
#include <test-skeleton.c>
Packit 6c4009
Packit 6c4009
#ifndef PATH_MAX
Packit 6c4009
# define PATH_MAX 4096
Packit 6c4009
#endif
Packit 6c4009
static char	cwd[PATH_MAX];
Packit 6c4009
static size_t	cwd_len;
Packit 6c4009
Packit 6c4009
struct {
Packit 6c4009
  const char *	name;
Packit 6c4009
  const char *	value;
Packit 6c4009
} symlinks[] = {
Packit 6c4009
  {"SYMLINK_LOOP",	"SYMLINK_LOOP"},
Packit 6c4009
  {"SYMLINK_1",		"."},
Packit 6c4009
  {"SYMLINK_2",		"//////./../../etc"},
Packit 6c4009
  {"SYMLINK_3",		"SYMLINK_1"},
Packit 6c4009
  {"SYMLINK_4",		"SYMLINK_2"},
Packit 6c4009
  {"SYMLINK_5",		"doesNotExist"},
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
struct {
Packit 6c4009
  const char * in, * out, * resolved;
Packit 6c4009
  int error;
Packit 6c4009
} tests[] = {
Packit 6c4009
  /*  0 */
Packit 6c4009
  {"/",					"/"},
Packit 6c4009
  {"/////////////////////////////////",	"/"},
Packit 6c4009
  {"/.././.././.././..///",		"/"},
Packit 6c4009
  {"/etc",				"/etc"},
Packit 6c4009
  {"/etc/../etc",			"/etc"},
Packit 6c4009
  /*  5 */
Packit 6c4009
  {"/doesNotExist/../etc",		0, "/doesNotExist", ENOENT},
Packit 6c4009
  {"./././././././././.",		"."},
Packit 6c4009
  {"/etc/.//doesNotExist",		0, "/etc/doesNotExist", ENOENT},
Packit 6c4009
  {"./doesExist",			"./doesExist"},
Packit 6c4009
  {"./doesExist/",			"./doesExist"},
Packit 6c4009
  /* 10 */
Packit 6c4009
  {"./doesExist/../doesExist",		"./doesExist"},
Packit 6c4009
  {"foobar",				0, "./foobar", ENOENT},
Packit 6c4009
  {".",					"."},
Packit 6c4009
  {"./foobar",				0, "./foobar", ENOENT},
Packit 6c4009
  {"SYMLINK_LOOP",			0, "./SYMLINK_LOOP", ELOOP},
Packit 6c4009
  /* 15 */
Packit 6c4009
  {"./SYMLINK_LOOP",			0, "./SYMLINK_LOOP", ELOOP},
Packit 6c4009
  {"SYMLINK_1",				"."},
Packit 6c4009
  {"SYMLINK_1/foobar",			0, "./foobar", ENOENT},
Packit 6c4009
  {"SYMLINK_2",				"/etc"},
Packit 6c4009
  {"SYMLINK_3",				"."},
Packit 6c4009
  /* 20 */
Packit 6c4009
  {"SYMLINK_4",				"/etc"},
Packit 6c4009
  {"../stdlib/SYMLINK_1",		"."},
Packit 6c4009
  {"../stdlib/SYMLINK_2",		"/etc"},
Packit 6c4009
  {"../stdlib/SYMLINK_3",		"."},
Packit 6c4009
  {"../stdlib/SYMLINK_4",		"/etc"},
Packit 6c4009
  /* 25 */
Packit 6c4009
  {"./SYMLINK_5",			0, "./doesNotExist", ENOENT},
Packit 6c4009
  {"SYMLINK_5",				0, "./doesNotExist", ENOENT},
Packit 6c4009
  {"SYMLINK_5/foobar",			0, "./doesNotExist", ENOENT},
Packit 6c4009
  {"doesExist/../../stdlib/doesExist",	"./doesExist"},
Packit 6c4009
  {"doesExist/.././../stdlib/.",	"."},
Packit 6c4009
  /* 30 */
Packit 6c4009
  {"./doesExist/someFile/",		0, "./doesExist/someFile", ENOTDIR},
Packit 6c4009
  {"./doesExist/someFile/..",		0, "./doesExist/someFile", ENOTDIR},
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
check_path (const char * result, const char * expected)
Packit 6c4009
{
Packit 6c4009
  int good;
Packit 6c4009
Packit 6c4009
  if (!result)
Packit 6c4009
    return (expected == NULL);
Packit 6c4009
Packit 6c4009
  if (!expected)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  if (expected[0] == '.' && (expected[1] == '/' || expected[1] == '\0'))
Packit 6c4009
    good = (strncmp (result, cwd, cwd_len) == 0
Packit 6c4009
	    && strcmp (result + cwd_len, expected + 1) == 0);
Packit 6c4009
  else
Packit 6c4009
    good = (strcmp (expected, result) == 0);
Packit 6c4009
Packit 6c4009
  return good;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (int argc, char ** argv)
Packit 6c4009
{
Packit 6c4009
  char * result;
Packit 6c4009
  int i, errors = 0;
Packit 6c4009
  char buf[PATH_MAX];
Packit 6c4009
Packit 6c4009
  getcwd (cwd, sizeof(buf));
Packit 6c4009
  cwd_len = strlen (cwd);
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (realpath (NULL, buf) != NULL || errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: expected return value NULL and errno set to EINVAL"
Packit 6c4009
	      " for realpath(NULL,...)\n", argv[0]);
Packit 6c4009
      ++errors;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
#if 0
Packit 6c4009
  /* This is now allowed.  The test is invalid.  */
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (realpath ("/", NULL) != NULL || errno != EINVAL)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: expected return value NULL and errno set to EINVAL"
Packit 6c4009
	      " for realpath(...,NULL)\n", argv[0]);
Packit 6c4009
      ++errors;
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  errno = 0;
Packit 6c4009
  if (realpath ("", buf) != NULL || errno != ENOENT)
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: expected return value NULL and set errno to ENOENT"
Packit 6c4009
	      " for realpath(\"\",...)\n", argv[0]);
Packit 6c4009
      ++errors;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
Packit 6c4009
    symlink (symlinks[i].value, symlinks[i].name);
Packit 6c4009
Packit 6c4009
  int has_dir = mkdir ("doesExist", 0777) == 0;
Packit 6c4009
Packit 6c4009
  int fd = has_dir ? creat ("doesExist/someFile", 0777) : -1;
Packit 6c4009
Packit 6c4009
  for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
Packit 6c4009
    {
Packit 6c4009
      buf[0] = '\0';
Packit 6c4009
      result = realpath (tests[i].in, buf);
Packit 6c4009
Packit 6c4009
      if (!check_path (result, tests[i].out))
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s: flunked test %d (expected `%s', got `%s')\n",
Packit 6c4009
		  argv[0], i, tests[i].out ? tests[i].out : "NULL",
Packit 6c4009
		  result ? result : "NULL");
Packit 6c4009
	  ++errors;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
Packit 6c4009
		  argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
Packit 6c4009
		  buf);
Packit 6c4009
	  ++errors;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (!tests[i].out && errno != tests[i].error)
Packit 6c4009
	{
Packit 6c4009
	  printf ("%s: flunked test %d (expected errno %d, got %d)\n",
Packit 6c4009
		  argv[0], i, tests[i].error, errno);
Packit 6c4009
	  ++errors;
Packit 6c4009
	  continue;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      char *result2 = realpath (tests[i].in, NULL);
Packit 6c4009
      if ((result2 == NULL && result != NULL)
Packit 6c4009
	  || (result2 != NULL && strcmp (result, result2) != 0))
Packit 6c4009
	{
Packit 6c4009
	  printf ("\
Packit 6c4009
%s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
Packit 6c4009
		  argv[0], result2, result);
Packit 6c4009
	  ++errors;
Packit 6c4009
	}
Packit 6c4009
      free (result2);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  getcwd (buf, sizeof(buf));
Packit 6c4009
  if (strcmp (buf, cwd))
Packit 6c4009
    {
Packit 6c4009
      printf ("%s: current working directory changed from %s to %s\n",
Packit 6c4009
	      argv[0], cwd, buf);
Packit 6c4009
      ++errors;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (fd >= 0)
Packit 6c4009
    {
Packit 6c4009
      close (fd);
Packit 6c4009
      unlink ("doesExist/someFile");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (has_dir)
Packit 6c4009
    rmdir ("doesExist");
Packit 6c4009
Packit 6c4009
  for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
Packit 6c4009
    unlink (symlinks[i].name);
Packit 6c4009
Packit 6c4009
  if (errors != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("%d errors.\n", errors);
Packit 6c4009
      return EXIT_FAILURE;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  puts ("No errors.");
Packit 6c4009
  return EXIT_SUCCESS;
Packit 6c4009
}