olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh1705899-1.patch

4c174f
commit 2afece36f6006844e87d7cb2fcb1ad8b220b2623
4c174f
Author: Florian Weimer <fweimer@redhat.com>
4c174f
Date:   Wed May 16 17:00:35 2018 +0200
4c174f
4c174f
    support: Add TEST_COMPARE_BLOB, support_quote_blob
4c174f
    
4c174f
    The declaration of support_test_compare_blob uses unsigned long int,
4c174f
    to avoid including <stddef.h>.
4c174f
    
4c174f
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
4c174f
4c174f
(Adjusted for minor conflict in support/Makefile.)
4c174f
4c174f
diff --git a/support/Makefile b/support/Makefile
4c174f
index 1bda81e55e519a57..fb9f4291d72156df 100644
4c174f
--- a/support/Makefile
4c174f
+++ b/support/Makefile
4c174f
@@ -52,9 +52,11 @@ libsupport-routines = \
4c174f
   support_format_hostent \
4c174f
   support_format_netent \
4c174f
   support_isolate_in_subprocess \
4c174f
+  support_quote_blob \
4c174f
   support_record_failure \
4c174f
   support_run_diff \
4c174f
   support_shared_allocate \
4c174f
+  support_test_compare_blob \
4c174f
   support_test_compare_failure \
4c174f
   support_write_file_string \
4c174f
   support_test_main \
4c174f
@@ -150,8 +152,10 @@ tests = \
4c174f
   tst-support-namespace \
4c174f
   tst-support_capture_subprocess \
4c174f
   tst-support_format_dns_packet \
4c174f
+  tst-support_quote_blob \
4c174f
   tst-support_record_failure \
4c174f
   tst-test_compare \
4c174f
+  tst-test_compare_blob \
4c174f
   tst-xreadlink \
4c174f
 
4c174f
 ifeq ($(run-built-tests),yes)
4c174f
diff --git a/support/check.h b/support/check.h
4c174f
index 2192f38941af2515..b3a4645e9255e90d 100644
4c174f
--- a/support/check.h
4c174f
+++ b/support/check.h
4c174f
@@ -64,6 +64,8 @@ __BEGIN_DECLS
4c174f
         (1, __FILE__, __LINE__, #expr);                         \
4c174f
   })
4c174f
 
4c174f
+
4c174f
+
4c174f
 int support_print_failure_impl (const char *file, int line,
4c174f
                                 const char *format, ...)
4c174f
   __attribute__ ((nonnull (1), format (printf, 3, 4)));
4c174f
@@ -141,6 +143,26 @@ void support_test_compare_failure (const char *file, int line,
4c174f
                                    int right_size);
4c174f
 
4c174f
 
4c174f
+/* Compare [LEFT, LEFT + LEFT_LENGTH) with [RIGHT, RIGHT +
4c174f
+   RIGHT_LENGTH) and report a test failure if the arrays are
4c174f
+   different.  LEFT_LENGTH and RIGHT_LENGTH are measured in bytes.  If
4c174f
+   the length is null, the corresponding pointer is ignored (i.e., it
4c174f
+   can be NULL).  The blobs should be reasonably short because on
4c174f
+   mismatch, both are printed.  */
4c174f
+#define TEST_COMPARE_BLOB(left, left_length, right, right_length)       \
4c174f
+  (support_test_compare_blob (left, left_length, right, right_length,   \
4c174f
+                              __FILE__, __LINE__,                       \
4c174f
+                              #left, #left_length, #right, #right_length))
4c174f
+
4c174f
+void support_test_compare_blob (const void *left,
4c174f
+                                unsigned long int left_length,
4c174f
+                                const void *right,
4c174f
+                                unsigned long int right_length,
4c174f
+                                const char *file, int line,
4c174f
+                                const char *left_exp, const char *left_len_exp,
4c174f
+                                const char *right_exp,
4c174f
+                                const char *right_len_exp);
4c174f
+
4c174f
 /* Internal function called by the test driver.  */
4c174f
 int support_report_failure (int status)
4c174f
   __attribute__ ((weak, warn_unused_result));
4c174f
diff --git a/support/support.h b/support/support.h
4c174f
index bc5827ed87d0d96c..b61fe0735c9204de 100644
4c174f
--- a/support/support.h
4c174f
+++ b/support/support.h
4c174f
@@ -59,6 +59,12 @@ void support_shared_free (void *);
4c174f
    process on error.  */
4c174f
 void support_write_file_string (const char *path, const char *contents);
4c174f
 
4c174f
+/* Quote the contents of the byte array starting at BLOB, of LENGTH
4c174f
+   bytes, in such a way that the result string can be included in a C
4c174f
+   literal (in single/double quotes, without putting the quotes into
4c174f
+   the result).  */
4c174f
+char *support_quote_blob (const void *blob, size_t length);
4c174f
+
4c174f
 /* Error-checking wrapper functions which terminate the process on
4c174f
    error.  */
4c174f
 
4c174f
diff --git a/support/support_quote_blob.c b/support/support_quote_blob.c
4c174f
new file mode 100644
4c174f
index 0000000000000000..d6a678d8d69160a8
4c174f
--- /dev/null
4c174f
+++ b/support/support_quote_blob.c
4c174f
@@ -0,0 +1,83 @@
4c174f
+/* Quote a blob so that it can be used in C literals.
4c174f
+   Copyright (C) 2018 Free Software Foundation, Inc.
4c174f
+   This file is part of the GNU C Library.
4c174f
+
4c174f
+   The GNU C Library is free software; you can redistribute it and/or
4c174f
+   modify it under the terms of the GNU Lesser General Public
4c174f
+   License as published by the Free Software Foundation; either
4c174f
+   version 2.1 of the License, or (at your option) any later version.
4c174f
+
4c174f
+   The GNU C Library is distributed in the hope that it will be useful,
4c174f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c174f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c174f
+   Lesser General Public License for more details.
4c174f
+
4c174f
+   You should have received a copy of the GNU Lesser General Public
4c174f
+   License along with the GNU C Library; if not, see
4c174f
+   <http://www.gnu.org/licenses/>.  */
4c174f
+
4c174f
+#include <support/support.h>
4c174f
+#include <support/xmemstream.h>
4c174f
+
4c174f
+char *
4c174f
+support_quote_blob (const void *blob, size_t length)
4c174f
+{
4c174f
+  struct xmemstream out;
4c174f
+  xopen_memstream (&out;;
4c174f
+
4c174f
+  const unsigned char *p = blob;
4c174f
+  for (size_t i = 0; i < length; ++i)
4c174f
+    {
4c174f
+      unsigned char ch = p[i];
4c174f
+
4c174f
+      /* Use C backslash escapes for those control characters for
4c174f
+         which they are defined.  */
4c174f
+      switch (ch)
4c174f
+        {
4c174f
+          case '\a':
4c174f
+            putc_unlocked ('\\', out.out);
4c174f
+            putc_unlocked ('a', out.out);
4c174f
+            break;
4c174f
+          case '\b':
4c174f
+            putc_unlocked ('\\', out.out);
4c174f
+            putc_unlocked ('b', out.out);
4c174f
+            break;
4c174f
+          case '\f':
4c174f
+            putc_unlocked ('\\', out.out);
4c174f
+            putc_unlocked ('f', out.out);
4c174f
+            break;
4c174f
+          case '\n':
4c174f
+            putc_unlocked ('\\', out.out);
4c174f
+            putc_unlocked ('n', out.out);
4c174f
+            break;
4c174f
+          case '\r':
4c174f
+            putc_unlocked ('\\', out.out);
4c174f
+            putc_unlocked ('r', out.out);
4c174f
+            break;
4c174f
+          case '\t':
4c174f
+            putc_unlocked ('\\', out.out);
4c174f
+            putc_unlocked ('t', out.out);
4c174f
+            break;
4c174f
+          case '\v':
4c174f
+            putc_unlocked ('\\', out.out);
4c174f
+            putc_unlocked ('v', out.out);
4c174f
+            break;
4c174f
+          case '\\':
4c174f
+          case '\'':
4c174f
+          case '\"':
4c174f
+            putc_unlocked ('\\', out.out);
4c174f
+            putc_unlocked (ch, out.out);
4c174f
+            break;
4c174f
+        default:
4c174f
+          if (ch < ' ' || ch > '~')
4c174f
+            /* Use octal sequences because they are fixed width,
4c174f
+               unlike hexadecimal sequences.  */
4c174f
+            fprintf (out.out, "\\%03o", ch);
4c174f
+          else
4c174f
+            putc_unlocked (ch, out.out);
4c174f
+        }
4c174f
+    }
4c174f
+
4c174f
+  xfclose_memstream (&out;;
4c174f
+  return out.buffer;
4c174f
+}
4c174f
diff --git a/support/support_test_compare_blob.c b/support/support_test_compare_blob.c
4c174f
new file mode 100644
4c174f
index 0000000000000000..c5e63d1b9327c9fe
4c174f
--- /dev/null
4c174f
+++ b/support/support_test_compare_blob.c
4c174f
@@ -0,0 +1,76 @@
4c174f
+/* Check two binary blobs for equality.
4c174f
+   Copyright (C) 2018 Free Software Foundation, Inc.
4c174f
+   This file is part of the GNU C Library.
4c174f
+
4c174f
+   The GNU C Library is free software; you can redistribute it and/or
4c174f
+   modify it under the terms of the GNU Lesser General Public
4c174f
+   License as published by the Free Software Foundation; either
4c174f
+   version 2.1 of the License, or (at your option) any later version.
4c174f
+
4c174f
+   The GNU C Library is distributed in the hope that it will be useful,
4c174f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c174f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c174f
+   Lesser General Public License for more details.
4c174f
+
4c174f
+   You should have received a copy of the GNU Lesser General Public
4c174f
+   License along with the GNU C Library; if not, see
4c174f
+   <http://www.gnu.org/licenses/>.  */
4c174f
+
4c174f
+#include <stdio.h>
4c174f
+#include <stdlib.h>
4c174f
+#include <string.h>
4c174f
+#include <support/check.h>
4c174f
+#include <support/support.h>
4c174f
+#include <support/xmemstream.h>
4c174f
+
4c174f
+static void
4c174f
+report_length (const char *what, unsigned long int length, const char *expr)
4c174f
+{
4c174f
+  printf ("  %s %lu bytes (from %s)\n", what, length, expr);
4c174f
+}
4c174f
+
4c174f
+static void
4c174f
+report_blob (const char *what, const unsigned char *blob,
4c174f
+             unsigned long int length, const char *expr)
4c174f
+{
4c174f
+  if (length > 0)
4c174f
+    {
4c174f
+      printf ("  %s (evaluated from %s):\n", what, expr);
4c174f
+      char *quoted = support_quote_blob (blob, length);
4c174f
+      printf ("      \"%s\"\n", quoted);
4c174f
+      free (quoted);
4c174f
+
4c174f
+      fputs ("     ", stdout);
4c174f
+      for (unsigned long i = 0; i < length; ++i)
4c174f
+        printf (" %02X", blob[i]);
4c174f
+      putc ('\n', stdout);
4c174f
+    }
4c174f
+}
4c174f
+
4c174f
+void
4c174f
+support_test_compare_blob (const void *left, unsigned long int left_length,
4c174f
+                           const void *right, unsigned long int right_length,
4c174f
+                           const char *file, int line,
4c174f
+                           const char *left_expr, const char *left_len_expr,
4c174f
+                           const char *right_expr, const char *right_len_expr)
4c174f
+{
4c174f
+  /* No differences are possible if both lengths are null.  */
4c174f
+  if (left_length == 0 && right_length == 0)
4c174f
+    return;
4c174f
+
4c174f
+  if (left_length != right_length || left == NULL || right == NULL
4c174f
+      || memcmp (left, right, left_length) != 0)
4c174f
+    {
4c174f
+      support_record_failure ();
4c174f
+      printf ("%s:%d: error: blob comparison failed\n", file, line);
4c174f
+      if (left_length == right_length)
4c174f
+        printf ("  blob length: %lu bytes\n", left_length);
4c174f
+      else
4c174f
+        {
4c174f
+          report_length ("left length: ", left_length, left_len_expr);
4c174f
+          report_length ("right length:", right_length, right_len_expr);
4c174f
+        }
4c174f
+      report_blob ("left", left, left_length, left_expr);
4c174f
+      report_blob ("right", right, right_length, right_expr);
4c174f
+    }
4c174f
+}
4c174f
diff --git a/support/tst-support_quote_blob.c b/support/tst-support_quote_blob.c
4c174f
new file mode 100644
4c174f
index 0000000000000000..5467a190a6e0725c
4c174f
--- /dev/null
4c174f
+++ b/support/tst-support_quote_blob.c
4c174f
@@ -0,0 +1,61 @@
4c174f
+/* Test the support_quote_blob function.
4c174f
+   Copyright (C) 2018 Free Software Foundation, Inc.
4c174f
+   This file is part of the GNU C Library.
4c174f
+
4c174f
+   The GNU C Library is free software; you can redistribute it and/or
4c174f
+   modify it under the terms of the GNU Lesser General Public
4c174f
+   License as published by the Free Software Foundation; either
4c174f
+   version 2.1 of the License, or (at your option) any later version.
4c174f
+
4c174f
+   The GNU C Library is distributed in the hope that it will be useful,
4c174f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c174f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c174f
+   Lesser General Public License for more details.
4c174f
+
4c174f
+   You should have received a copy of the GNU Lesser General Public
4c174f
+   License along with the GNU C Library; if not, see
4c174f
+   <http://www.gnu.org/licenses/>.  */
4c174f
+
4c174f
+#include <support/check.h>
4c174f
+#include <support/support.h>
4c174f
+#include <string.h>
4c174f
+#include <stdlib.h>
4c174f
+
4c174f
+static int
4c174f
+do_test (void)
4c174f
+{
4c174f
+  /* Check handling of the empty blob, both with and without trailing
4c174f
+     NUL byte.  */
4c174f
+  char *p = support_quote_blob ("", 0);
4c174f
+  TEST_COMPARE (strlen (p), 0);
4c174f
+  free (p);
4c174f
+  p = support_quote_blob ("X", 0);
4c174f
+  TEST_COMPARE (strlen (p), 0);
4c174f
+  free (p);
4c174f
+
4c174f
+  /* Check escaping of backslash-escaped characters, and lack of
4c174f
+     escaping for other shell meta-characters.  */
4c174f
+  p = support_quote_blob ("$()*?`@[]{}~\'\"X", 14);
4c174f
+  TEST_COMPARE (strcmp (p, "$()*?`@[]{}~\\'\\\""), 0);
4c174f
+  free (p);
4c174f
+
4c174f
+  /* Check lack of escaping for letters and digits.  */
4c174f
+#define LETTERS_AND_DIGTS                       \
4c174f
+  "abcdefghijklmnopqrstuvwxyz"                  \
4c174f
+  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"                  \
4c174f
+  "0123456789"
4c174f
+  p = support_quote_blob (LETTERS_AND_DIGTS "@", 2 * 26 + 10);
4c174f
+  TEST_COMPARE (strcmp (p, LETTERS_AND_DIGTS), 0);
4c174f
+  free (p);
4c174f
+
4c174f
+  /* Check escaping of control characters and other non-printable
4c174f
+     characters.  */
4c174f
+  p = support_quote_blob ("\r\n\t\a\b\f\v\1\177\200\377\0@", 14);
4c174f
+  TEST_COMPARE (strcmp (p, "\\r\\n\\t\\a\\b\\f\\v\\001"
4c174f
+                        "\\177\\200\\377\\000@\\000"), 0);
4c174f
+  free (p);
4c174f
+
4c174f
+  return 0;
4c174f
+}
4c174f
+
4c174f
+#include <support/test-driver.c>
4c174f
diff --git a/support/tst-test_compare_blob.c b/support/tst-test_compare_blob.c
4c174f
new file mode 100644
4c174f
index 0000000000000000..aa8643e18227da85
4c174f
--- /dev/null
4c174f
+++ b/support/tst-test_compare_blob.c
4c174f
@@ -0,0 +1,125 @@
4c174f
+/* Basic test for the TEST_COMPARE_BLOB macro.
4c174f
+   Copyright (C) 2018 Free Software Foundation, Inc.
4c174f
+   This file is part of the GNU C Library.
4c174f
+
4c174f
+   The GNU C Library is free software; you can redistribute it and/or
4c174f
+   modify it under the terms of the GNU Lesser General Public
4c174f
+   License as published by the Free Software Foundation; either
4c174f
+   version 2.1 of the License, or (at your option) any later version.
4c174f
+
4c174f
+   The GNU C Library is distributed in the hope that it will be useful,
4c174f
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c174f
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c174f
+   Lesser General Public License for more details.
4c174f
+
4c174f
+   You should have received a copy of the GNU Lesser General Public
4c174f
+   License along with the GNU C Library; if not, see
4c174f
+   <http://www.gnu.org/licenses/>.  */
4c174f
+
4c174f
+#include <string.h>
4c174f
+#include <support/check.h>
4c174f
+#include <support/capture_subprocess.h>
4c174f
+
4c174f
+static void
4c174f
+subprocess (void *closure)
4c174f
+{
4c174f
+  /* These tests should fail.  They were chosen to cover differences
4c174f
+     in length (with the same contents), single-bit mismatches, and
4c174f
+     mismatching null pointers.  */
4c174f
+  TEST_COMPARE_BLOB ("", 0, "", 1);    /* Line 29.  */
4c174f
+  TEST_COMPARE_BLOB ("X", 1, "", 1);   /* Line 30.  */
4c174f
+  TEST_COMPARE_BLOB ("abcd", 3, "abcd", 4); /* Line 31.  */
4c174f
+  TEST_COMPARE_BLOB ("abcd", 4, "abcD", 4); /* Line 32.  */
4c174f
+  TEST_COMPARE_BLOB ("abcd", 4, NULL, 0); /* Line 33.  */
4c174f
+  TEST_COMPARE_BLOB (NULL, 0, "abcd", 4); /* Line 34.  */
4c174f
+}
4c174f
+
4c174f
+/* Same contents, different addresses.  */
4c174f
+char buffer_abc_1[] = "abc";
4c174f
+char buffer_abc_2[] = "abc";
4c174f
+
4c174f
+static int
4c174f
+do_test (void)
4c174f
+{
4c174f
+  /* This should succeed.  Even if the pointers and array contents are
4c174f
+     different, zero-length inputs are not different.  */
4c174f
+  TEST_COMPARE_BLOB ("", 0, "", 0);
4c174f
+  TEST_COMPARE_BLOB ("", 0, buffer_abc_1, 0);
4c174f
+  TEST_COMPARE_BLOB (buffer_abc_1, 0, "", 0);
4c174f
+  TEST_COMPARE_BLOB (NULL, 0, "", 0);
4c174f
+  TEST_COMPARE_BLOB ("", 0, NULL, 0);
4c174f
+  TEST_COMPARE_BLOB (NULL, 0, NULL, 0);
4c174f
+
4c174f
+  /* Check equality of blobs containing a single NUL byte.  */
4c174f
+  TEST_COMPARE_BLOB ("", 1, "", 1);
4c174f
+  TEST_COMPARE_BLOB ("", 1, &buffer_abc_1[3], 1);
4c174f
+
4c174f
+  /* Check equality of blobs of varying lengths.  */
4c174f
+  for (size_t i = 0; i <= sizeof (buffer_abc_1); ++i)
4c174f
+    TEST_COMPARE_BLOB (buffer_abc_1, i, buffer_abc_2, i);
4c174f
+
4c174f
+  struct support_capture_subprocess proc = support_capture_subprocess
4c174f
+    (&subprocess, NULL);
4c174f
+
4c174f
+  /* Discard the reported error.  */
4c174f
+  support_record_failure_reset ();
4c174f
+
4c174f
+  puts ("info: *** subprocess output starts ***");
4c174f
+  fputs (proc.out.buffer, stdout);
4c174f
+  puts ("info: *** subprocess output ends ***");
4c174f
+
4c174f
+  TEST_VERIFY
4c174f
+    (strcmp (proc.out.buffer,
4c174f
+"tst-test_compare_blob.c:29: error: blob comparison failed\n"
4c174f
+"  left length:  0 bytes (from 0)\n"
4c174f
+"  right length: 1 bytes (from 1)\n"
4c174f
+"  right (evaluated from \"\"):\n"
4c174f
+"      \"\\000\"\n"
4c174f
+"      00\n"
4c174f
+"tst-test_compare_blob.c:30: error: blob comparison failed\n"
4c174f
+"  blob length: 1 bytes\n"
4c174f
+"  left (evaluated from \"X\"):\n"
4c174f
+"      \"X\"\n"
4c174f
+"      58\n"
4c174f
+"  right (evaluated from \"\"):\n"
4c174f
+"      \"\\000\"\n"
4c174f
+"      00\n"
4c174f
+"tst-test_compare_blob.c:31: error: blob comparison failed\n"
4c174f
+"  left length:  3 bytes (from 3)\n"
4c174f
+"  right length: 4 bytes (from 4)\n"
4c174f
+"  left (evaluated from \"abcd\"):\n"
4c174f
+"      \"abc\"\n"
4c174f
+"      61 62 63\n"
4c174f
+"  right (evaluated from \"abcd\"):\n"
4c174f
+"      \"abcd\"\n"
4c174f
+"      61 62 63 64\n"
4c174f
+"tst-test_compare_blob.c:32: error: blob comparison failed\n"
4c174f
+"  blob length: 4 bytes\n"
4c174f
+"  left (evaluated from \"abcd\"):\n"
4c174f
+"      \"abcd\"\n"
4c174f
+"      61 62 63 64\n"
4c174f
+"  right (evaluated from \"abcD\"):\n"
4c174f
+"      \"abcD\"\n"
4c174f
+"      61 62 63 44\n"
4c174f
+"tst-test_compare_blob.c:33: error: blob comparison failed\n"
4c174f
+"  left length:  4 bytes (from 4)\n"
4c174f
+"  right length: 0 bytes (from 0)\n"
4c174f
+"  left (evaluated from \"abcd\"):\n"
4c174f
+"      \"abcd\"\n"
4c174f
+"      61 62 63 64\n"
4c174f
+"tst-test_compare_blob.c:34: error: blob comparison failed\n"
4c174f
+"  left length:  0 bytes (from 0)\n"
4c174f
+"  right length: 4 bytes (from 4)\n"
4c174f
+"  right (evaluated from \"abcd\"):\n"
4c174f
+"      \"abcd\"\n"
4c174f
+"      61 62 63 64\n"
4c174f
+             ) == 0);
4c174f
+
4c174f
+  /* Check that there is no output on standard error.  */
4c174f
+  support_capture_subprocess_check (&proc, "TEST_COMPARE_BLOB",
4c174f
+                                    0, sc_allow_stdout);
4c174f
+
4c174f
+  return 0;
4c174f
+}
4c174f
+
4c174f
+#include <support/test-driver.c>