gettimeofday: port recent C++ fix to Emacs
[gnulib.git] / tests / test-lstat.c
index 8182738..d520193 100644 (file)
@@ -1,5 +1,5 @@
 /* Test of lstat() function.
-   Copyright (C) 2008, 2009 Free Software Foundation, Inc.
+   Copyright (C) 2008-2013 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <sys/stat.h>
 
+/* Caution: lstat may be a function-like macro.  Although this
+   signature check must pass, it may be the signature of the real (and
+   broken) lstat rather than rpl_lstat.  Most code should not use the
+   address of lstat.  */
+#include "signature.h"
+SIGNATURE_CHECK (lstat, int, (char const *, struct stat *));
+
 #include <fcntl.h>
 #include <errno.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 
 #include "same-inode.h"
+#include "ignore-value.h"
+#include "macros.h"
 
-#if !HAVE_SYMLINK
-# define symlink(a,b) (-1)
-#endif
+#define BASE "test-lstat.t"
 
-#define ASSERT(expr) \
-  do                                                                         \
-    {                                                                        \
-      if (!(expr))                                                           \
-       {                                                                    \
-         fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__);  \
-         fflush (stderr);                                                   \
-         abort ();                                                          \
-       }                                                                    \
-    }                                                                        \
-  while (0)
+#include "test-lstat.h"
 
-#define BASE "test-lstat.t"
+/* Wrapper around lstat, which works even if lstat is a function-like
+   macro, where test_lstat_func(lstat) would do the wrong thing.  */
+static int
+do_lstat (char const *name, struct stat *st)
+{
+  return lstat (name, st);
+}
 
 int
-main ()
+main (void)
 {
-  struct stat st1;
-  struct stat st2;
-
   /* Remove any leftovers from a previous partial run.  */
-  ASSERT (system ("rm -rf " BASE "*") == 0);
-
-  /* Test for common directories.  */
-  ASSERT (lstat (".", &st1) == 0);
-  ASSERT (lstat ("./", &st2) == 0);
-  ASSERT (SAME_INODE (st1, st2));
-  ASSERT (S_ISDIR (st1.st_mode));
-  ASSERT (S_ISDIR (st2.st_mode));
-  ASSERT (lstat ("/", &st1) == 0);
-  ASSERT (lstat ("///", &st2) == 0);
-  ASSERT (SAME_INODE (st1, st2));
-  ASSERT (S_ISDIR (st1.st_mode));
-  ASSERT (S_ISDIR (st2.st_mode));
-  ASSERT (lstat ("..", &st1) == 0);
-  ASSERT (S_ISDIR (st1.st_mode));
-
-  /* Test for error conditions.  */
-  errno = 0;
-  ASSERT (lstat ("", &st1) == -1);
-  ASSERT (errno == ENOENT);
-  errno = 0;
-  ASSERT (lstat ("nosuch", &st1) == -1);
-  ASSERT (errno == ENOENT);
-  errno = 0;
-  ASSERT (lstat ("nosuch/", &st1) == -1);
-  ASSERT (errno == ENOENT);
-
-  ASSERT (close (creat (BASE "file", 0600)) == 0);
-  ASSERT (lstat (BASE "file", &st1) == 0);
-  ASSERT (S_ISREG (st1.st_mode));
-  errno = 0;
-  ASSERT (lstat (BASE "file/", &st1) == -1);
-  ASSERT (errno == ENOTDIR);
-
-  /* Now for some symlink tests, where supported.  We set up:
-     link1 -> directory
-     link2 -> file
-     link3 -> dangling
-     link4 -> loop
-     then test behavior both with and without trailing slash.
-  */
-  if (symlink (".", BASE "link1") != 0)
-    {
-      ASSERT (unlink (BASE "file") == 0);
-      fputs ("skipping test: symlinks not supported on this filesystem\n",
-             stderr);
-      return 77;
-    }
-  ASSERT (symlink (BASE "file", BASE "link2") == 0);
-  ASSERT (symlink (BASE "nosuch", BASE "link3") == 0);
-  ASSERT (symlink (BASE "link4", BASE "link4") == 0);
-
-  ASSERT (lstat (BASE "link1", &st1) == 0);
-  ASSERT (S_ISLNK (st1.st_mode));
-  ASSERT (lstat (BASE "link1/", &st1) == 0);
-  ASSERT (stat (BASE "link1", &st2) == 0);
-  ASSERT (S_ISDIR (st1.st_mode));
-  ASSERT (S_ISDIR (st2.st_mode));
-  ASSERT (SAME_INODE (st1, st2));
-
-  ASSERT (lstat (BASE "link2", &st1) == 0);
-  ASSERT (S_ISLNK (st1.st_mode));
-  errno = 0;
-  ASSERT (lstat (BASE "link2/", &st1) == -1);
-  ASSERT (errno == ENOTDIR);
-
-  ASSERT (lstat (BASE "link3", &st1) == 0);
-  ASSERT (S_ISLNK (st1.st_mode));
-  errno = 0;
-  ASSERT (lstat (BASE "link3/", &st1) == -1);
-  ASSERT (errno == ENOENT);
-
-  ASSERT (lstat (BASE "link4", &st1) == 0);
-  ASSERT (S_ISLNK (st1.st_mode));
-  errno = 0;
-  ASSERT (lstat (BASE "link4/", &st1) == -1);
-  ASSERT (errno == ELOOP);
-
-  /* Cleanup.  */
-  ASSERT (unlink (BASE "file") == 0);
-  ASSERT (unlink (BASE "link1") == 0);
-  ASSERT (unlink (BASE "link2") == 0);
-  ASSERT (unlink (BASE "link3") == 0);
-  ASSERT (unlink (BASE "link4") == 0);
+  ignore_value (system ("rm -rf " BASE "*"));
 
-  return 0;
+  return test_lstat_func (do_lstat, true);
 }