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