Blame support/tst-test_compare_blob.c

Packit 6c4009
/* Basic test for the TEST_COMPARE_BLOB macro.
Packit 6c4009
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/capture_subprocess.h>
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
subprocess (void *closure)
Packit 6c4009
{
Packit 6c4009
  /* These tests should fail.  They were chosen to cover differences
Packit 6c4009
     in length (with the same contents), single-bit mismatches, and
Packit 6c4009
     mismatching null pointers.  */
Packit 6c4009
  TEST_COMPARE_BLOB ("", 0, "", 1);    /* Line 29.  */
Packit 6c4009
  TEST_COMPARE_BLOB ("X", 1, "", 1);   /* Line 30.  */
Packit 6c4009
  TEST_COMPARE_BLOB ("abcd", 3, "abcd", 4); /* Line 31.  */
Packit 6c4009
  TEST_COMPARE_BLOB ("abcd", 4, "abcD", 4); /* Line 32.  */
Packit 6c4009
  TEST_COMPARE_BLOB ("abcd", 4, NULL, 0); /* Line 33.  */
Packit 6c4009
  TEST_COMPARE_BLOB (NULL, 0, "abcd", 4); /* Line 34.  */
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Same contents, different addresses.  */
Packit 6c4009
char buffer_abc_1[] = "abc";
Packit 6c4009
char buffer_abc_2[] = "abc";
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* This should succeed.  Even if the pointers and array contents are
Packit 6c4009
     different, zero-length inputs are not different.  */
Packit 6c4009
  TEST_COMPARE_BLOB ("", 0, "", 0);
Packit 6c4009
  TEST_COMPARE_BLOB ("", 0, buffer_abc_1, 0);
Packit 6c4009
  TEST_COMPARE_BLOB (buffer_abc_1, 0, "", 0);
Packit 6c4009
  TEST_COMPARE_BLOB (NULL, 0, "", 0);
Packit 6c4009
  TEST_COMPARE_BLOB ("", 0, NULL, 0);
Packit 6c4009
  TEST_COMPARE_BLOB (NULL, 0, NULL, 0);
Packit 6c4009
Packit 6c4009
  /* Check equality of blobs containing a single NUL byte.  */
Packit 6c4009
  TEST_COMPARE_BLOB ("", 1, "", 1);
Packit 6c4009
  TEST_COMPARE_BLOB ("", 1, &buffer_abc_1[3], 1);
Packit 6c4009
Packit 6c4009
  /* Check equality of blobs of varying lengths.  */
Packit 6c4009
  for (size_t i = 0; i <= sizeof (buffer_abc_1); ++i)
Packit 6c4009
    TEST_COMPARE_BLOB (buffer_abc_1, i, buffer_abc_2, i);
Packit 6c4009
Packit 6c4009
  struct support_capture_subprocess proc = support_capture_subprocess
Packit 6c4009
    (&subprocess, NULL);
Packit 6c4009
Packit 6c4009
  /* Discard the reported error.  */
Packit 6c4009
  support_record_failure_reset ();
Packit 6c4009
Packit 6c4009
  puts ("info: *** subprocess output starts ***");
Packit 6c4009
  fputs (proc.out.buffer, stdout);
Packit 6c4009
  puts ("info: *** subprocess output ends ***");
Packit 6c4009
Packit 6c4009
  TEST_VERIFY
Packit 6c4009
    (strcmp (proc.out.buffer,
Packit 6c4009
"tst-test_compare_blob.c:29: error: blob comparison failed\n"
Packit 6c4009
"  left length:  0 bytes (from 0)\n"
Packit 6c4009
"  right length: 1 bytes (from 1)\n"
Packit 6c4009
"  right (evaluated from \"\"):\n"
Packit 6c4009
"      \"\\000\"\n"
Packit 6c4009
"      00\n"
Packit 6c4009
"tst-test_compare_blob.c:30: error: blob comparison failed\n"
Packit 6c4009
"  blob length: 1 bytes\n"
Packit 6c4009
"  left (evaluated from \"X\"):\n"
Packit 6c4009
"      \"X\"\n"
Packit 6c4009
"      58\n"
Packit 6c4009
"  right (evaluated from \"\"):\n"
Packit 6c4009
"      \"\\000\"\n"
Packit 6c4009
"      00\n"
Packit 6c4009
"tst-test_compare_blob.c:31: error: blob comparison failed\n"
Packit 6c4009
"  left length:  3 bytes (from 3)\n"
Packit 6c4009
"  right length: 4 bytes (from 4)\n"
Packit 6c4009
"  left (evaluated from \"abcd\"):\n"
Packit 6c4009
"      \"abc\"\n"
Packit 6c4009
"      61 62 63\n"
Packit 6c4009
"  right (evaluated from \"abcd\"):\n"
Packit 6c4009
"      \"abcd\"\n"
Packit 6c4009
"      61 62 63 64\n"
Packit 6c4009
"tst-test_compare_blob.c:32: error: blob comparison failed\n"
Packit 6c4009
"  blob length: 4 bytes\n"
Packit 6c4009
"  left (evaluated from \"abcd\"):\n"
Packit 6c4009
"      \"abcd\"\n"
Packit 6c4009
"      61 62 63 64\n"
Packit 6c4009
"  right (evaluated from \"abcD\"):\n"
Packit 6c4009
"      \"abcD\"\n"
Packit 6c4009
"      61 62 63 44\n"
Packit 6c4009
"tst-test_compare_blob.c:33: error: blob comparison failed\n"
Packit 6c4009
"  left length:  4 bytes (from 4)\n"
Packit 6c4009
"  right length: 0 bytes (from 0)\n"
Packit 6c4009
"  left (evaluated from \"abcd\"):\n"
Packit 6c4009
"      \"abcd\"\n"
Packit 6c4009
"      61 62 63 64\n"
Packit 6c4009
"tst-test_compare_blob.c:34: error: blob comparison failed\n"
Packit 6c4009
"  left length:  0 bytes (from 0)\n"
Packit 6c4009
"  right length: 4 bytes (from 4)\n"
Packit 6c4009
"  right (evaluated from \"abcd\"):\n"
Packit 6c4009
"      \"abcd\"\n"
Packit 6c4009
"      61 62 63 64\n"
Packit 6c4009
             ) == 0);
Packit 6c4009
Packit 6c4009
  /* Check that there is no output on standard error.  */
Packit 6c4009
  support_capture_subprocess_check (&proc, "TEST_COMPARE_BLOB",
Packit 6c4009
                                    0, sc_allow_stdout);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>