Test more.
[gnulib.git] / tests / test-gc-md4.c
1 /*
2  * Copyright (C) 2005 Free Software Foundation
3  * Written by Simon Josefsson
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <string.h>
26 #include "gc.h"
27
28 int
29 main (int argc, char *argv[])
30 {
31   Gc_rc rc;
32   gc_hash_handle h;
33
34   rc = gc_init ();
35   if (rc != GC_OK)
36     {
37       printf ("gc_init() failed\n");
38       return 1;
39     }
40
41   /* Test vectors from RFC 1320. */
42
43   {
44     const char *in = "abc";
45     size_t inlen = strlen (in);
46     const char *expect =
47       "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d";
48     char out[16];
49     const char *p;
50
51     /* MD4 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b */
52
53     if (gc_md4 (in, inlen, out) != 0)
54       {
55         printf ("gc_md4 call failed\n");
56         return 1;
57       }
58
59     if (memcmp (out, expect, 16) != 0)
60       {
61         size_t i;
62         printf ("md4 1 missmatch. expected:\n");
63         for (i = 0; i < 16; i++)
64           printf ("%02x ", expect[i] & 0xFF);
65         printf ("\ncomputed:\n");
66         for (i = 0; i < 16; i++)
67           printf ("%02x ", out[i] & 0xFF);
68         printf ("\n");
69         return 1;
70       }
71
72     if (gc_hash_buffer (GC_MD4, in, inlen, out) != 0)
73       {
74         printf ("gc_hash_buffer(MD4) call failed\n");
75         return 1;
76       }
77
78     if (memcmp (out, expect, 16) != 0)
79       {
80         size_t i;
81         printf ("md4 1 missmatch. expected:\n");
82         for (i = 0; i < 16; i++)
83           printf ("%02x ", expect[i] & 0xFF);
84         printf ("\ncomputed:\n");
85         for (i = 0; i < 16; i++)
86           printf ("%02x ", out[i] & 0xFF);
87         printf ("\n");
88         return 1;
89       }
90
91     if (gc_hash_digest_length (GC_MD4) != 16)
92       {
93         printf ("gc_hash_digest_length (GC_MD4) failed\n");
94         return 1;
95       }
96
97     if ((rc = gc_hash_open (GC_MD4, 0, &h)) != GC_OK)
98       {
99         printf ("gc_hash_open(GC_MD4) failed (%d)\n", rc);
100         return 1;
101       }
102
103     gc_hash_write (h, inlen, in);
104
105     p = gc_hash_read (h);
106
107     if (!p)
108       {
109         printf ("gc_hash_read failed\n");
110         return 1;
111       }
112
113     if (memcmp (p, expect, 16) != 0)
114       {
115         size_t i;
116         printf ("md4 1 missmatch. expected:\n");
117         for (i = 0; i < 16; i++)
118           printf ("%02x ", expect[i] & 0xFF);
119         printf ("\ncomputed:\n");
120         for (i = 0; i < 16; i++)
121           printf ("%02x ", p[i] & 0xFF);
122         printf ("\n");
123         return 1;
124       }
125
126     gc_hash_close (h);
127   }
128
129   gc_done ();
130
131   return 0;
132 }