Blame crypt/md5test-giant.c

Packit 6c4009
/* Testcase for https://sourceware.org/bugzilla/show_bug.cgi?id=14090.
Packit 6c4009
   Copyright (C) 2012-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or modify
Packit 6c4009
   it under the terms of the GNU General Public License as published
Packit 6c4009
   by the Free Software Foundation; version 2 of the License, or
Packit 6c4009
   (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program 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
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
Packit 6c4009
#include "md5.h"
Packit 6c4009
Packit 6c4009
/* This test will not work with 32-bit size_t, so let it succeed
Packit 6c4009
   there.  */
Packit 6c4009
#if SIZE_MAX <= UINT32_MAX
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
#else
Packit 6c4009
Packit 6c4009
# define CONST_2G  0x080000000
Packit 6c4009
# define CONST_10G 0x280000000
Packit 6c4009
Packit 6c4009
/* MD5 sum values of zero-filled blocks of specified sizes.  */
Packit 6c4009
static const struct test_data_s
Packit 6c4009
{
Packit 6c4009
  const char ref[16];
Packit 6c4009
  size_t len;
Packit 6c4009
} test_data[] =
Packit 6c4009
  {
Packit 6c4009
    { "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e",
Packit 6c4009
      0x000000000 },
Packit 6c4009
    { "\xa9\x81\x13\x0c\xf2\xb7\xe0\x9f\x46\x86\xdc\x27\x3c\xf7\x18\x7e",
Packit 6c4009
      0x080000000 },
Packit 6c4009
    { "\xc9\xa5\xa6\x87\x8d\x97\xb4\x8c\xc9\x65\xc1\xe4\x18\x59\xf0\x34",
Packit 6c4009
      0x100000000 },
Packit 6c4009
    { "\x58\xcf\x63\x8a\x73\x3f\x91\x90\x07\xb4\x28\x7c\xf5\x39\x6d\x0c",
Packit 6c4009
      0x180000000 },
Packit 6c4009
    { "\xb7\x70\x35\x1f\xad\xae\x5a\x96\xbb\xaf\x97\x02\xed\x97\xd2\x8d",
Packit 6c4009
      0x200000000 },
Packit 6c4009
    { "\x2d\xd2\x6c\x4d\x47\x99\xeb\xd2\x9f\xa3\x1e\x48\xd4\x9e\x8e\x53",
Packit 6c4009
      0x280000000 },
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
report (const char *id, const char *md5, size_t len, const char *ref)
Packit 6c4009
{
Packit 6c4009
  if (memcmp (md5, ref, 16))
Packit 6c4009
    {
Packit 6c4009
      printf ("test %s with size %zd failed\n", id, len);
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Test md5 in a single md5_process_bytes call.  */
Packit 6c4009
static int
Packit 6c4009
test_single (void *buf, size_t len, const char *ref)
Packit 6c4009
{
Packit 6c4009
  char sum[16];
Packit 6c4009
  struct md5_ctx ctx;
Packit 6c4009
Packit 6c4009
  __md5_init_ctx (&ctx;;
Packit 6c4009
  __md5_process_bytes (buf, len, &ctx;;
Packit 6c4009
  __md5_finish_ctx (&ctx, sum);
Packit 6c4009
Packit 6c4009
  return report ("single", sum, len, ref);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Test md5 with two md5_process_bytes calls to trigger a
Packit 6c4009
   different path in md5_process_block for sizes > 2 GB.  */
Packit 6c4009
static int
Packit 6c4009
test_double (void *buf, size_t len, const char *ref)
Packit 6c4009
{
Packit 6c4009
  char sum[16];
Packit 6c4009
  struct md5_ctx ctx;
Packit 6c4009
Packit 6c4009
  __md5_init_ctx (&ctx;;
Packit 6c4009
  if (len >= CONST_2G)
Packit 6c4009
    {
Packit 6c4009
      __md5_process_bytes (buf, CONST_2G, &ctx;;
Packit 6c4009
      __md5_process_bytes (buf + CONST_2G, len - CONST_2G, &ctx;;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    __md5_process_bytes (buf, len, &ctx;;
Packit 6c4009
Packit 6c4009
  __md5_finish_ctx (&ctx, sum);
Packit 6c4009
Packit 6c4009
  return report ("double", sum, len, ref);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  void *buf;
Packit 6c4009
  unsigned int j;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  buf = mmap64 (0, CONST_10G, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Packit 6c4009
  if (buf == MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      puts ("Could not allocate 10 GB via mmap, skipping test.");
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  for (j = 0; j < sizeof (test_data) / sizeof (struct test_data_s); j++)
Packit 6c4009
    {
Packit 6c4009
      if (test_single (buf, test_data[j].len, test_data[j].ref))
Packit 6c4009
	result = 1;
Packit 6c4009
      if (test_double (buf, test_data[j].len, test_data[j].ref))
Packit 6c4009
	result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* This needs on a fast machine 90s.  */
Packit 6c4009
#define TIMEOUT 480
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"