changeset 12014:63cfe4168142

test-canonicalize: consolidate into single C program * tests/test-canonicalize.sh: Delete; move setup into... * tests/test-canonicalize.c (main): ...the program, making it easier to run in debugger. Add some tests. * modules/canonicalize-tests (Files): Remove unused file. (Depends-on): Add progname. (configure.ac, Makefile.am): Simplify. Signed-off-by: Eric Blake <ebb9@byu.net>
author Eric Blake <ebb9@byu.net>
date Fri, 11 Sep 2009 09:53:44 -0600
parents 284fbf719a2a
children 9317d4a9ac96
files ChangeLog modules/canonicalize-tests tests/test-canonicalize.c tests/test-canonicalize.sh
diffstat 4 files changed, 135 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2009-09-17  Eric Blake  <ebb9@byu.net>
 
+	test-canonicalize: consolidate into single C program
+	* tests/test-canonicalize.sh: Delete; move setup into...
+	* tests/test-canonicalize.c (main): ...the program, making it
+	easier to run in debugger.  Add some tests.
+	* modules/canonicalize-tests (Files): Remove unused file.
+	(Depends-on): Add progname.
+	(configure.ac, Makefile.am): Simplify.
+
 	test-canonicalize-lgpl: consolidate into single C program
 	* tests/test-canonicalize-lgpl.sh: Delete; move setup into...
 	* tests/test-canonicalize-lgpl.c (main): ...the program, making it
--- a/modules/canonicalize-tests
+++ b/modules/canonicalize-tests
@@ -1,16 +1,13 @@
 Files:
-tests/test-canonicalize.sh
 tests/test-canonicalize.c
 
 Depends-on:
+progname
 
 configure.ac:
 AC_CHECK_FUNCS_ONCE([symlink])
-HAVE_SYMLINK=$ac_cv_func_symlink
-AC_SUBST([HAVE_SYMLINK])
 
 Makefile.am:
-TESTS += test-canonicalize.sh
-TESTS_ENVIRONMENT += EXEEXT='@EXEEXT@' HAVE_SYMLINK='$(HAVE_SYMLINK)'
+TESTS += test-canonicalize
 check_PROGRAMS += test-canonicalize
 test_canonicalize_LDADD = $(LDADD) @LIBINTL@
--- a/tests/test-canonicalize.c
+++ b/tests/test-canonicalize.c
@@ -1,5 +1,5 @@
 /* Test of execution of file name canonicalization.
-   Copyright (C) 2007-2008 Free Software Foundation, Inc.
+   Copyright (C) 2007-2009 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
@@ -20,9 +20,17 @@
 
 #include "canonicalize.h"
 
+#include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#if !HAVE_SYMLINK
+# define symlink(a,b) (-1)
+#endif
 
 #define ASSERT(expr) \
   do									     \
@@ -36,22 +44,85 @@
     }									     \
   while (0)
 
-const char *program_name = "test-canonicalize";
+#define BASE "t-can.tmp"
 
 int
 main ()
 {
+  /* Setup some hierarchy to be used by this test.  Start by removing
+     any leftovers from a previous partial run.  */
+  {
+    int fd;
+    ASSERT (system ("rm -rf " BASE " ise") == 0);
+    ASSERT (mkdir (BASE, 0700) == 0);
+    fd = creat (BASE "/tra", 0600);
+    ASSERT (0 <= fd);
+    ASSERT (close (fd) == 0);
+  }
+
+  /* Check for ., .., intermediate // handling, and for error cases.  */
+  {
+    char *result1 = canonicalize_file_name (BASE "//./..//" BASE "/tra");
+    char *result2 = canonicalize_filename_mode (BASE "//./..//" BASE "/tra",
+                                                CAN_EXISTING);
+    ASSERT (result1 != NULL);
+    ASSERT (result2 != NULL);
+    ASSERT (strcmp (result1, result2) == 0);
+    ASSERT (strstr (result1, "/" BASE "/tra")
+	    == result1 + strlen (result1) - strlen ("/" BASE "/tra"));
+    free (result1);
+    free (result2);
+    errno = 0;
+    result1 = canonicalize_file_name ("");
+    ASSERT (result1 == NULL);
+    ASSERT (errno == ENOENT);
+    errno = 0;
+    result2 = canonicalize_filename_mode ("", CAN_EXISTING);
+    ASSERT (result2 == NULL);
+    ASSERT (errno == ENOENT);
+    errno = 0;
+    result1 = canonicalize_file_name (NULL);
+    ASSERT (result1 == NULL);
+    ASSERT (errno == EINVAL);
+    errno = 0;
+    result2 = canonicalize_filename_mode (NULL, CAN_EXISTING);
+    ASSERT (result2 == NULL);
+    ASSERT (errno == EINVAL);
+  }
+
+  /* From here on out, tests involve symlinks.  */
+  if (symlink (BASE "/ket", "ise") != 0)
+    {
+      ASSERT (remove (BASE "/tra") == 0);
+      ASSERT (rmdir (BASE) == 0);
+      fputs ("skipping test: symlinks not supported on this filesystem\n",
+	     stderr);
+      return 77;
+    }
+  ASSERT (symlink ("bef", BASE "/plo") == 0);
+  ASSERT (symlink ("tra", BASE "/huk") == 0);
+  ASSERT (symlink ("lum", BASE "/bef") == 0);
+  ASSERT (symlink ("wum", BASE "/ouk") == 0);
+  ASSERT (symlink ("../ise", BASE "/ket") == 0);
+  ASSERT (mkdir (BASE "/lum", 0700) == 0);
+  ASSERT (symlink ("s", BASE "/p") == 0);
+  ASSERT (symlink ("d", BASE "/s") == 0);
+  ASSERT (mkdir (BASE "/d", 0700) == 0);
+  ASSERT (close (creat (BASE "/d/2", 0600)) == 0);
+  ASSERT (symlink ("../s/2", BASE "/d/1") == 0);
+
   /* Check that the symbolic link to a file can be resolved.  */
   {
-    char *result1 = canonicalize_file_name ("t-can.tmp/huk");
-    char *result2 = canonicalize_file_name ("t-can.tmp/tra");
-    char *result3 = canonicalize_filename_mode ("t-can.tmp/huk", CAN_EXISTING);
+    char *result1 = canonicalize_file_name (BASE "/huk");
+    char *result2 = canonicalize_file_name (BASE "/tra");
+    char *result3 = canonicalize_filename_mode (BASE "/huk", CAN_EXISTING);
     ASSERT (result1 != NULL);
     ASSERT (result2 != NULL);
     ASSERT (result3 != NULL);
     ASSERT (strcmp (result1, result2) == 0);
     ASSERT (strcmp (result2, result3) == 0);
-    ASSERT (strcmp (result1 + strlen (result1) - 14, "/t-can.tmp/tra") == 0);
+    ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/tra"),
+                    "/" BASE "/tra") == 0);
     free (result1);
     free (result2);
     free (result3);
@@ -59,10 +130,10 @@
 
   /* Check that the symbolic link to a directory can be resolved.  */
   {
-    char *result1 = canonicalize_file_name ("t-can.tmp/plo");
-    char *result2 = canonicalize_file_name ("t-can.tmp/bef");
-    char *result3 = canonicalize_file_name ("t-can.tmp/lum");
-    char *result4 = canonicalize_filename_mode ("t-can.tmp/plo", CAN_EXISTING);
+    char *result1 = canonicalize_file_name (BASE "/plo");
+    char *result2 = canonicalize_file_name (BASE "/bef");
+    char *result3 = canonicalize_file_name (BASE "/lum");
+    char *result4 = canonicalize_filename_mode (BASE "/plo", CAN_EXISTING);
     ASSERT (result1 != NULL);
     ASSERT (result2 != NULL);
     ASSERT (result3 != NULL);
@@ -70,7 +141,8 @@
     ASSERT (strcmp (result1, result2) == 0);
     ASSERT (strcmp (result2, result3) == 0);
     ASSERT (strcmp (result3, result4) == 0);
-    ASSERT (strcmp (result1 + strlen (result1) - 14, "/t-can.tmp/lum") == 0);
+    ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/lum"),
+                    "/" BASE "/lum") == 0);
     free (result1);
     free (result2);
     free (result3);
@@ -79,40 +151,54 @@
 
   /* Check that a symbolic link to a nonexistent file yields NULL.  */
   {
-    char *result1 = canonicalize_file_name ("t-can.tmp/ouk");
-    char *result2 = canonicalize_filename_mode ("t-can.tmp/ouk", CAN_EXISTING);
+    char *result1;
+    char *result2;
+    errno = 0;
+    result1 = canonicalize_file_name (BASE "/ouk");
     ASSERT (result1 == NULL);
+    ASSERT (errno == ENOENT);
+    errno = 0;
+    result2 = canonicalize_filename_mode (BASE "/ouk", CAN_EXISTING);
     ASSERT (result2 == NULL);
+    ASSERT (errno == ENOENT);
   }
 
   /* Check that a loop of symbolic links is detected.  */
   {
-    char *result1 = canonicalize_file_name ("ise");
-    char *result2 = canonicalize_filename_mode ("ise", CAN_EXISTING);
+    char *result1;
+    char *result2;
+    errno = 0;
+    result1 = canonicalize_file_name ("ise");
     ASSERT (result1 == NULL);
+    ASSERT (errno == ELOOP);
+    errno = 0;
+    result2 = canonicalize_filename_mode ("ise", CAN_EXISTING);
     ASSERT (result2 == NULL);
+    ASSERT (errno == ELOOP);
   }
 
   /* Check that alternate modes can resolve missing basenames.  */
   {
-    char *result1 = canonicalize_filename_mode ("t-can.tmp/zzz", CAN_ALL_BUT_LAST);
-    char *result2 = canonicalize_filename_mode ("t-can.tmp/zzz", CAN_MISSING);
+    char *result1 = canonicalize_filename_mode (BASE "/zzz", CAN_ALL_BUT_LAST);
+    char *result2 = canonicalize_filename_mode (BASE "/zzz", CAN_MISSING);
     ASSERT (result1 != NULL);
     ASSERT (result2 != NULL);
     ASSERT (strcmp (result1, result2) == 0);
-    ASSERT (strcmp (result1 + strlen (result1) - 14, "/t-can.tmp/zzz") == 0);
+    ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/zzz"),
+                    "/" BASE "/zzz") == 0);
     free (result1);
     free (result2);
   }
 
   /* Check that alternate modes can resolve broken symlink basenames.  */
   {
-    char *result1 = canonicalize_filename_mode ("t-can.tmp/ouk", CAN_ALL_BUT_LAST);
-    char *result2 = canonicalize_filename_mode ("t-can.tmp/ouk", CAN_MISSING);
+    char *result1 = canonicalize_filename_mode (BASE "/ouk", CAN_ALL_BUT_LAST);
+    char *result2 = canonicalize_filename_mode (BASE "/ouk", CAN_MISSING);
     ASSERT (result1 != NULL);
     ASSERT (result2 != NULL);
     ASSERT (strcmp (result1, result2) == 0);
-    ASSERT (strcmp (result1 + strlen (result1) - 14, "/t-can.tmp/wum") == 0);
+    ASSERT (strcmp (result1 + strlen (result1) - strlen ("/" BASE "/wum"),
+                    "/" BASE "/wum") == 0);
     free (result1);
     free (result2);
   }
@@ -130,8 +216,8 @@
   /* Ensure that the following is resolved properly.
      Before 2007-09-27, it would mistakenly report a loop.  */
   {
-    char *result1 = canonicalize_filename_mode ("t-can.tmp", CAN_EXISTING);
-    char *result2 = canonicalize_filename_mode ("t-can.tmp/p/1", CAN_EXISTING);
+    char *result1 = canonicalize_filename_mode (BASE, CAN_EXISTING);
+    char *result2 = canonicalize_filename_mode (BASE "/p/1", CAN_EXISTING);
     ASSERT (result1 != NULL);
     ASSERT (result2 != NULL);
     ASSERT (strcmp (result2 + strlen (result1), "/d/2") == 0);
@@ -139,5 +225,21 @@
     free (result2);
   }
 
+  /* Cleanup.  */
+  ASSERT (remove (BASE "/d/1") == 0);
+  ASSERT (remove (BASE "/d/2") == 0);
+  ASSERT (remove (BASE "/d") == 0);
+  ASSERT (remove (BASE "/s") == 0);
+  ASSERT (remove (BASE "/p") == 0);
+  ASSERT (remove (BASE "/plo") == 0);
+  ASSERT (remove (BASE "/huk") == 0);
+  ASSERT (remove (BASE "/bef") == 0);
+  ASSERT (remove (BASE "/ouk") == 0);
+  ASSERT (remove (BASE "/ket") == 0);
+  ASSERT (remove (BASE "/lum") == 0);
+  ASSERT (remove (BASE "/tra") == 0);
+  ASSERT (remove (BASE) == 0);
+  ASSERT (remove ("ise") == 0);
+
   return 0;
 }
deleted file mode 100755
--- a/tests/test-canonicalize.sh
+++ /dev/null
@@ -1,42 +0,0 @@
-#!/bin/sh
-
-tmpfiles=""
-trap 'rm -fr $tmpfiles' 1 2 3 15
-
-tmpfiles="$tmpfiles t-can.tmp ise"
-mkdir t-can.tmp
-test "x$HAVE_SYMLINK" = xyes \
-  && ln -s t-can.tmp/ket ise \
-  || { echo "Skipping test: symbolic links not supported on this filesystem"
-       rm -fr $tmpfiles
-       exit 77
-     }
-(cd t-can.tmp \
- && ln -s bef plo \
- && ln -s tra huk \
- && ln -s lum bef \
- && ln -s wum ouk \
- && ln -s ../ise ket \
- && echo > tra \
- && mkdir lum
-) || exit 1
-
-# Trigger a bug that would make the function mistakenly report a loop.
-# To trigger it, we have to construct a name/situation during the
-# resolution of which the code dereferences the same symlink (S)
-# two different times with no actual loop.  In addition, the
-# second and fourth calls to readlink must operate on S.
-(cd t-can.tmp \
- && ln -s s p \
- && ln -s d s \
- && mkdir d \
- && echo > d/2 \
- && ln -s ../s/2 d/1
-) || exit 1
-
-./test-canonicalize${EXEEXT}
-result=$?
-
-rm -fr $tmpfiles
-
-exit $result