chiark / gitweb /
more manpage, remove -h option
[innduct.git] / tests / lib / md5-t.c
1 /* $Id: md5-t.c 6128 2003-01-18 22:26:49Z rra $ */
2 /* MD5 hashing test suite. */
3
4 #include "config.h"
5 #include "clibrary.h"
6 #include "inn/md5.h"
7 #include "libinn.h"
8 #include "libtest.h"
9
10 /* Used to initialize strings of unsigned characters. */
11 #define U       (const unsigned char *)
12
13 /* An unsigned char version of strlen. */
14 #define ustrlen(s)      strlen((const char *) s)
15
16 /* Used for converting digests to hex to make them easier to deal with. */
17 static const char hex[] = "0123456789abcdef";
18
19 /* A set of data strings and resulting digests to check.  It's not easy to
20    get nulls into this data structure, so data containing nulls should be
21    checked separately. */
22 static const unsigned char * const testdata[] = {
23     /* First five tests of the MD5 test suite from RFC 1321. */
24     U"",
25     U"a",
26     U"abc",
27     U"message digest",
28     U"abcdefghijklmnopqrstuvwxyz",
29
30     /* Three real message IDs to ensure compatibility with old INN versions;
31        the corresponding MD5 hashes were taken directly out of the history
32        file of a server running INN 2.3. */
33     U"<J3Ds5.931$Vg6.7556@news01.chello.no>",
34     U"<sr5v7ooea6e17@corp.supernews.com>",
35     U"<cancel.Y2Ds5.26391$oH5.540535@news-east.usenetserver.com>",
36
37     /* Other random stuff, including high-bit characters. */
38     U"example.test",
39     U"||",
40     U"|||",
41     U"\375\277\277\277\277\276",
42     U"\377\277\277\277\277\277"
43 };
44
45 /* The hashes corresonding to the above data. */
46 static const char * const testhash[] = {
47     "d41d8cd98f00b204e9800998ecf8427e",
48     "0cc175b9c0f1b6a831c399e269772661",
49     "900150983cd24fb0d6963f7d28e17f72",
50     "f96b697d7cb7938d525a2f31aaf161d0",
51     "c3fcd3d76192e4007dfb496cca67e13b",
52     "c4a70fb19af37bed6b7c77f1e1187f00",
53     "7f70531c7027c20b0ddba0a649cf8691",
54     "9d9f0423f38b731c9bf69607cea6be76",
55     "09952be409a7d6464cd7661beeeb966e",
56     "7d010443693eec253a121e2aa2ba177c",
57     "2edf2958166561c5c08cd228e53bbcdc",
58     "c18293a6fe0a09720e841c8ebc697b97",
59     "ce23eb027c63215b999b9f86d6a4f9cb"
60 };
61
62 static void
63 digest2hex(const unsigned char *digest, char *result)
64 {
65     const unsigned char *p;
66     unsigned int i;
67
68     for (p = digest, i = 0; i < 32; i += 2, p++) {
69         result[i] = hex[(*p & 0xf0) >> 4];
70         result[i + 1] = hex[*p & 0x0f];
71     }
72     result[32] = '\0';
73 }
74
75 static void
76 test_md5(int n, const char *expected, const unsigned char *data,
77          size_t length)
78 {
79     unsigned char digest[16];
80     char hexdigest[33];
81
82     md5_hash(data, length, digest);
83     digest2hex(digest, hexdigest);
84     ok_string(n, expected, hexdigest);
85 }
86
87 int
88 main(void)
89 {
90     unsigned int i;
91     int j, n;
92     unsigned char *data;
93     struct md5_context context;
94     char hexdigest[33];
95
96     printf("%d\n", 12 + ARRAY_SIZE(testdata));
97
98     test_md5(1, "93b885adfe0da089cdf634904fd59f71", U"\0", 1);
99     test_md5(2, "e94a053c3fbfcfb22b4debaa11af7718", U"\0ab\n", 4);
100
101     data = xmalloc(64 * 1024);
102     memset(data, 0, 64 * 1024);
103     test_md5(3, "fcd6bcb56c1689fcef28b57c22475bad", data, 64 * 1024);
104     memset(data, 1, 32 * 1024);
105     test_md5(4, "3d8897b14254c9f86fbad3fe22f62edd", data, 64 * 1024);
106     test_md5(5, "25364962aa23b187942a24ae736c4e8c", data, 65000);
107     test_md5(6, "f9816b5d5363d15f14bb98d548309dcc", data, 55);
108     test_md5(7, "5e99dfddfb51c18cfc55911dee24ae7b", data, 56);
109     test_md5(8, "0871ffa021e2bc4da87eb93ac22d293c", data, 63);
110     test_md5(9, "784d68ba9112308689114a6816c628ce", data, 64);
111
112     /* Check the individual functions. */
113     md5_init(&context);
114     md5_update(&context, data, 32 * 1024);
115     md5_update(&context, data + 32 * 1024, 32 * 1024 - 42);
116     md5_update(&context, data + 64 * 1024 - 42, 42);
117     md5_final(&context);
118     digest2hex(context.digest, hexdigest);
119     ok_string(10, "3d8897b14254c9f86fbad3fe22f62edd", hexdigest);
120
121     /* Part of the MD5 test suite from RFC 1321. */
122     for (i = 0, n = 'A'; n <= 'Z'; i++, n++)
123         data[i] = n;
124     for (i = 26, n = 'a'; n <= 'z'; i++, n++)
125         data[i] = n;
126     for (i = 52, n = '0'; n <= '9'; i++, n++)
127         data[i] = n;
128     test_md5(11, "d174ab98d277d9f5a5611c2c9f419d9f", data, 62);
129     for (i = 0, j = 0; j < 8; j++) {
130         for (n = '1'; n <= '9'; i++, n++)
131             data[i] = n;
132         data[i++] = '0';
133     }
134     test_md5(12, "57edf4a22be3c955ac49da2e2107b67a", data, 80);
135
136     n = 13;
137     for (i = 0; i < ARRAY_SIZE(testdata); i++)
138         test_md5(n++, testhash[i], testdata[i], ustrlen(testdata[i]));
139
140     return 0;
141 }