Back to index

glibc  2.9
Defines | Functions | Variables
test-canon.c File Reference
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>
#include <test-skeleton.c>

Go to the source code of this file.

Defines

#define PATH_MAX   4096

Functions

int do_test (int argc, char *argv[])
static int check_path (const char *result, const char *expected)
int do_test (int argc, char **argv)

Variables

static char cwd [PATH_MAX]
static size_t cwd_len
struct {
const char * name
const char * value
symlinks []
struct {
const char * in
const char * out
const char * resolved
int error
tests []

Define Documentation

#define PATH_MAX   4096

Definition at line 37 of file test-canon.c.


Function Documentation

static int check_path ( const char *  result,
const char *  expected 
) [static]

Definition at line 101 of file test-canon.c.

{
  int good;

  if (!result)
    return (expected == NULL);

  if (!expected)
    return 0;

  if (expected[0] == '.' && (expected[1] == '/' || expected[1] == '\0'))
    good = (strncmp (result, cwd, cwd_len) == 0
           && strcmp (result + cwd_len, expected + 1) == 0);
  else
    good = (strcmp (expected, result) == 0);

  return good;
}

Here is the caller graph for this function:

int do_test ( int  argc,
char *  argv[] 
)

Definition at line 146 of file test-lfs.c.

{
  int ret;
  struct stat64 statbuf;

  ret = lseek64 (fd, TWO_GB+100, SEEK_SET);
  if (ret == -1 && errno == ENOSYS)
    {
      error (0, 0, "lseek64 is not supported.");
      exit (EXIT_SUCCESS);
    }
  if (ret == -1 && errno == EINVAL)
    {
      error (0, 0, "LFS seems not to be supported.");
      exit (EXIT_SUCCESS);
    }
  if (ret == -1)
    {
      error (0, errno, "lseek64 failed with error");
      exit (EXIT_FAILURE);
    }

  ret = write (fd, "Hello", 5);
  if (ret == -1 && errno == EFBIG)
    {
      error (0, 0, "LFS seems not to be supported.");
      exit (EXIT_SUCCESS);
    }

  if (ret == -1 && errno == ENOSPC)
    {
      error (0, 0, "Not enough space to write file.");
      exit (EXIT_SUCCESS);
    }

  if (ret != 5)
    error (EXIT_FAILURE, errno, "cannot write test string to large file");

  ret = close (fd);

  if (ret == -1)
    error (EXIT_FAILURE, errno, "error closing file");

  ret = stat64 (name, &statbuf);

  if (ret == -1 && (errno == ENOSYS || errno == EOVERFLOW))
    error (0, 0, "stat64 is not supported.");
  else if (ret == -1)
    error (EXIT_FAILURE, errno, "cannot stat file `%s'", name);
  else if (statbuf.st_size != (TWO_GB + 100 + 5))
    error (EXIT_FAILURE, 0, "stat reported size %lld instead of %lld.",
          (long long int) statbuf.st_size, (TWO_GB + 100 + 5));

  test_ftello ();

  return 0;
}

Here is the call graph for this function:

int do_test ( int  argc,
char **  argv 
)

Definition at line 122 of file test-canon.c.

{
  char * result;
  int i, errors = 0;
  char buf[PATH_MAX];

  getcwd (cwd, sizeof(buf));
  cwd_len = strlen (cwd);

  errno = 0;
  if (realpath (NULL, buf) != NULL || errno != EINVAL)
    {
      printf ("%s: expected return value NULL and errno set to EINVAL"
             " for realpath(NULL,...)\n", argv[0]);
      ++errors;
    }

#if 0
  /* This is now allowed.  The test is invalid.  */
  errno = 0;
  if (realpath ("/", NULL) != NULL || errno != EINVAL)
    {
      printf ("%s: expected return value NULL and errno set to EINVAL"
             " for realpath(...,NULL)\n", argv[0]);
      ++errors;
    }
#endif

  errno = 0;
  if (realpath ("", buf) != NULL || errno != ENOENT)
    {
      printf ("%s: expected return value NULL and set errno to ENOENT"
             " for realpath(\"\",...)\n", argv[0]);
      ++errors;
    }

  for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
    symlink (symlinks[i].value, symlinks[i].name);

  int has_dir = mkdir ("doesExist", 0777) == 0;

  int fd = has_dir ? creat ("doesExist/someFile", 0777) : -1;

  for (i = 0; i < (int) (sizeof (tests) / sizeof (tests[0])); ++i)
    {
      buf[0] = '\0';
      result = realpath (tests[i].in, buf);

      if (!check_path (result, tests[i].out))
       {
         printf ("%s: flunked test %d (expected `%s', got `%s')\n",
                argv[0], i, tests[i].out ? tests[i].out : "NULL",
                result ? result : "NULL");
         ++errors;
         continue;
       }

      if (!check_path (buf, tests[i].out ? tests[i].out : tests[i].resolved))
       {
         printf ("%s: flunked test %d (expected resolved `%s', got `%s')\n",
                argv[0], i, tests[i].out ? tests[i].out : tests[i].resolved,
                buf);
         ++errors;
         continue;
       }

      if (!tests[i].out && errno != tests[i].error)
       {
         printf ("%s: flunked test %d (expected errno %d, got %d)\n",
                argv[0], i, tests[i].error, errno);
         ++errors;
         continue;
       }

      char *result2 = realpath (tests[i].in, NULL);
      if ((result2 == NULL && result != NULL)
         || (result2 != NULL && strcmp (result, result2) != 0))
       {
         printf ("\
%s: realpath(..., NULL) produced different result than realpath(..., buf): '%s' vs '%s'\n",
                argv[0], result2, result);
         ++errors;
       }
      free (result2);
    }

  getcwd (buf, sizeof(buf));
  if (strcmp (buf, cwd))
    {
      printf ("%s: current working directory changed from %s to %s\n",
             argv[0], cwd, buf);
      ++errors;
    }

  if (fd >= 0)
    {
      close (fd);
      unlink ("doesExist/someFile");
    }

  if (has_dir)
    rmdir ("doesExist");

  for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
    unlink (symlinks[i].name);

  if (errors != 0)
    {
      printf ("%d errors.\n", errors);
      return EXIT_FAILURE;
    }

  puts ("No errors.");
  return EXIT_SUCCESS;
}

Here is the call graph for this function:


Variable Documentation

char cwd[PATH_MAX] [static]

Definition at line 39 of file test-canon.c.

size_t cwd_len [static]

Definition at line 40 of file test-canon.c.

struct { ... } symlinks[]
struct { ... } tests[]