summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Blake <ebb9@byu.net>2009-09-11 09:53:44 -0600
committerEric Blake <ebb9@byu.net>2009-09-17 19:16:21 -0600
commit8e7fa996cb189829ed7c9a4f2f489b9150a6571d (patch)
treecde3179d06d3a3146b4e31df0fef214f6c60cd0f
parent59a85b3ebaf70211eb568441a9d2c33619c97ae6 (diff)
downloadgnulib-8e7fa996cb189829ed7c9a4f2f489b9150a6571d.tar.gz
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>
-rw-r--r--ChangeLog8
-rw-r--r--modules/canonicalize-tests7
-rw-r--r--tests/test-canonicalize.c148
-rwxr-xr-xtests/test-canonicalize.sh42
4 files changed, 135 insertions, 70 deletions
diff --git a/ChangeLog b/ChangeLog
index 063a07bb07..301d75624e 100644
--- 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
diff --git a/modules/canonicalize-tests b/modules/canonicalize-tests
index e8dfec7204..f91c5f92e0 100644
--- 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@
diff --git a/tests/test-canonicalize.c b/tests/test-canonicalize.c
index d76c307365..66144221d5 100644
--- 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 @@ main ()
/* 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 @@ main ()
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 @@ main ()
/* 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 @@ main ()
/* 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 @@ main ()
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;
}
diff --git a/tests/test-canonicalize.sh b/tests/test-canonicalize.sh
deleted file mode 100755
index a4ab9626f4..0000000000
--- 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