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