1/* Copyright (C) 2011 Red Hat, Inc.
2   This file is part of elfutils.
3
4   This file is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 3 of the License, or
7   (at your option) any later version.
8
9   elfutils is distributed in the hope that it will be useful, but
10   WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20
21#include <string.h>
22#include <error.h>
23
24#include "md5.h"
25#include "sha1.h"
26
27static const struct expected
28{
29  const char *sample;
30  const char *md5_expected;
31  const char *sha1_expected;
32} tests[] =
33  {
34    {
35      "abc",
36      "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72",
37      "\xa9\x99\x3e\x36\x47\x06\x81\x6a\xba\x3e"
38      "\x25\x71\x78\x50\xc2\x6c\x9c\xd0\xd8\x9d"
39    },
40    {
41      "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
42      "\x82\x15\xef\x07\x96\xa2\x0b\xca\xaa\xe1\x16\xd3\x87\x6c\x66\x4a",
43      "\x84\x98\x3e\x44\x1c\x3b\xd2\x6e\xba\xae"
44      "\x4a\xa1\xf9\x51\x29\xe5\xe5\x46\x70\xf1"
45    },
46    {
47      "\0a",
48      "\x77\x07\xd6\xae\x4e\x02\x7c\x70\xee\xa2\xa9\x35\xc2\x29\x6f\x21",
49      "\x34\xaa\x97\x3c\xd4\xc4\xda\xa4\xf6\x1e"
50      "\xeb\x2b\xdb\xad\x27\x31\x65\x34\x01\x6f",
51    },
52    {
53      "When in the Course of human events it becomes necessary",
54      "\x62\x6b\x5e\x22\xcd\x3d\x02\xea\x07\xde\xd4\x50\x62\x3d\xb9\x96",
55      "\x66\xc3\xc6\x8d\x62\x91\xc5\x1e\x63\x0c"
56      "\x85\xc8\x6c\xc4\x4b\x3a\x79\x3e\x07\x28",
57    },
58  };
59#define NTESTS (sizeof tests / sizeof tests[0])
60
61#define md5_size	16
62#define sha1_size	20
63
64#define TEST_HASH(ALGO, I)						      \
65  {									      \
66    struct ALGO##_ctx ctx;						      \
67    uint32_t result_buffer[(ALGO##_size + 3) / 4];			      \
68    ALGO##_init_ctx (&ctx);						      \
69    if (tests[I].sample[0] == '\0')					      \
70      {									      \
71	char input_buffer[1000];					      \
72	memset (input_buffer, tests[I].sample[1], sizeof input_buffer);	      \
73	for (int rept = 0; rept < 1000; ++rept)				      \
74	  ALGO##_process_bytes (input_buffer, sizeof input_buffer, &ctx);     \
75      }									      \
76    else								      \
77      ALGO##_process_bytes (tests[I].sample, strlen (tests[I].sample), &ctx); \
78    char *result = ALGO##_finish_ctx (&ctx, result_buffer);		      \
79    if (result != (void *) result_buffer				      \
80	|| memcmp (result, tests[I].ALGO##_expected, ALGO##_size) != 0)	      \
81      error (0, 0, #ALGO " test %zu failed", 1 + I);			      \
82  }
83
84int
85main (void)
86{
87  for (size_t i = 0; i < NTESTS; ++i)
88    {
89      TEST_HASH (md5, i);
90      TEST_HASH (sha1, i);
91    }
92  return error_message_count;
93}
94