]> Savannah Git Hosting - gnulib.git/commitdiff
canonicalize[-lgpl] tests: Add more tests.
authorBruno Haible <bruno@clisp.org>
Sun, 17 Jan 2021 16:22:25 +0000 (17:22 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 17 Jan 2021 21:02:29 +0000 (22:02 +0100)
* tests/test-canonicalize.c (main): Add detailed tests for // handling.
* tests/test-canonicalize-lgpl.c (main): Likewise.

ChangeLog
tests/test-canonicalize-lgpl.c
tests/test-canonicalize.c

index 3c4a5133bef6ca07b8a010479c3847f1970710ce..dad3119bfbdbcde5ea3d7e7dbb0b62c6c1022357 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2021-01-17  Bruno Haible  <bruno@clisp.org>
+
+       canonicalize[-lgpl] tests: Add more tests.
+       * tests/test-canonicalize.c (main): Add detailed tests for // handling.
+       * tests/test-canonicalize-lgpl.c (main): Likewise.
+
 2021-01-17  Bruno Haible  <bruno@clisp.org>
 
        argp tests: Avoid test failures on Alpine Linux.
index d86f7262a6d90155ae8b10167d7e77a03265baa6..c0a5a551509fc668cf1d27ada20e2322e351768f 100644 (file)
@@ -67,6 +67,48 @@ main (void)
     ASSERT (close (fd) == 0);
   }
 
+  /* Check // handling (the easy cases, without symlinks).
+     This // handling is not mandated by POSIX.  However, many applications
+     expect that canonicalize_file_name "canonicalizes" the file name,
+     that is, that different results of canonicalize_file_name correspond
+     to different files (except for hard links).  */
+  {
+    char *result0 = canonicalize_file_name ("/etc/passwd");
+    if (result0 != NULL) /* This file does not exist on native Windows.  */
+      {
+        char *result;
+
+        result = canonicalize_file_name ("/etc//passwd");
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_file_name ("/etc///passwd");
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        /* On Windows, the syntax //host/share/filename denotes a file
+           in a directory named 'share', exported from host 'host'.
+           See also m4/double-slash-root.m4.  */
+#if !(defined _WIN32 || defined __CYGWIN__)
+        result = canonicalize_file_name ("//etc/passwd");
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_file_name ("//etc//passwd");
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_file_name ("//etc///passwd");
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+#endif
+
+        result = canonicalize_file_name ("///etc/passwd");
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_file_name ("///etc//passwd");
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_file_name ("///etc///passwd");
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+      }
+  }
+
   /* Check for ., .., intermediate // handling, and for error cases.  */
   {
     char *result = canonicalize_file_name (BASE "//./..//" BASE "/tra");
@@ -209,7 +251,7 @@ main (void)
     ASSERT (errno == ELOOP);
   }
 
-  /* Check that leading // is honored correctly.  */
+  /* Check that leading // within symlinks is honored correctly.  */
   {
     struct stat st1;
     struct stat st2;
index e85ca4557dc8af08d568d1cbac6dea994186521a..dbde6f2e98c88aea7c4343cc702a66a3f2887c06 100644 (file)
@@ -58,6 +58,51 @@ main (void)
     ASSERT (close (fd) == 0);
   }
 
+  /* Check // handling (the easy cases, without symlinks).
+     This // handling is not mandated by POSIX.  However, many applications
+     expect that canonicalize_filename_mode "canonicalizes" the file name,
+     that is, that different results of canonicalize_filename_mode correspond
+     to different files (except for hard links).  */
+  {
+    char *result0 = canonicalize_file_name ("/etc/passwd");
+    if (result0 != NULL) /* This file does not exist on native Windows.  */
+      {
+        char *result;
+
+        result = canonicalize_filename_mode ("/etc/passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_filename_mode ("/etc//passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_filename_mode ("/etc///passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        /* On Windows, the syntax //host/share/filename denotes a file
+           in a directory named 'share', exported from host 'host'.
+           See also m4/double-slash-root.m4.  */
+#if !(defined _WIN32 || defined __CYGWIN__)
+        result = canonicalize_filename_mode ("//etc/passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_filename_mode ("//etc//passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_filename_mode ("//etc///passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+#endif
+
+        result = canonicalize_filename_mode ("///etc/passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_filename_mode ("///etc//passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+
+        result = canonicalize_filename_mode ("///etc///passwd", CAN_MISSING);
+        ASSERT (result != NULL && strcmp (result, result0) == 0);
+      }
+  }
+
   /* Check for ., .., intermediate // handling, and for error cases.  */
   {
     char *result1 = canonicalize_file_name (BASE "//./..//" BASE "/tra");
@@ -334,7 +379,7 @@ main (void)
     free (result2);
   }
 
-  /* Check that leading // is honored correctly.  */
+  /* Check that leading // within symlinks is honored correctly.  */
   {
     struct stat st1;
     struct stat st2;