]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/apr-util/test/testmd4.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / apr-util / test / testmd4.c
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /* This is derived from material copyright RSA Data Security, Inc.
17  * Their notice is reproduced below in its entirety.
18  *
19  * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
20  * rights reserved.
21  *
22  * RSA Data Security, Inc. makes no representations concerning either
23  * the merchantability of this software or the suitability of this
24  * software for any particular purpose. It is provided "as is"
25  * without express or implied warranty of any kind.
26  *
27  * These notices must be retained in any copies of any part of this
28  * documentation and/or software.
29  */
30
31 #include <assert.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include "apr_errno.h"
36 #include "apr_md4.h"
37 #include "apr_file_io.h"
38
39 #include "abts.h"
40 #include "testutil.h"
41
42 static struct {
43         const char *string;
44         const char *md4sum;
45 } md4sums[] = 
46 {
47 /* 
48 * Taken from the old md4 test suite.
49 * MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
50 * MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
51 * MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
52 * MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
53 * MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
54 * MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
55 * MD4 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
56
57 */
58         {"", 
59          "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89\xc0"},
60         {"a", 
61          "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb\x24"},
62         {"abc", 
63          "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72\x9d"},
64         {"message digest", 
65          "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01\x4b"},
66         {"abcdefghijklmnopqrstuvwxyz", 
67          "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d\xa9"},
68         {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 
69          "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0\xe4"},
70         {"12345678901234567890123456789012345678901234567890123456789012345678901234567890", 
71          "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05\x36"}
72 };
73
74 static int num_sums = sizeof(md4sums) / sizeof(md4sums[0]); 
75 static int count;
76
77 #if 0
78 static int MDStringComp(const void *string, const void *sum)
79 {
80         apr_md4_ctx_t context;
81         unsigned char digest[APR_MD4_DIGESTSIZE];
82         unsigned int len = strlen(string);
83
84         apr_md4_init(&context);
85         apr_md4_update(&context, (unsigned char *)string, len);
86         apr_md4_final(digest, &context);
87         return (memcmp(digest, sum, APR_MD4_DIGESTSIZE));
88
89 }
90 #endif
91
92 static void test_md4sum(abts_case *tc, void *data)
93 {
94         apr_md4_ctx_t context;
95         unsigned char digest[APR_MD4_DIGESTSIZE];
96         const void *string = md4sums[count].string;
97         const void *sum = md4sums[count].md4sum;
98         unsigned int len = strlen(string);
99
100         ABTS_ASSERT(tc, "apr_md4_init", (apr_md4_init(&context) == 0));
101         ABTS_ASSERT(tc, "apr_md4_update", 
102                     (apr_md4_update(&context, 
103                                     (unsigned char *)string, len) == 0));
104         
105         ABTS_ASSERT(tc, "apr_md4_final", (apr_md4_final(digest, &context) ==0));
106         ABTS_ASSERT(tc, "check for correct md4 digest", 
107                     (memcmp(digest, sum, APR_MD4_DIGESTSIZE) == 0));
108 }
109
110 abts_suite *testmd4(abts_suite *suite)
111 {
112         suite = ADD_SUITE(suite);
113
114         for (count=0; count < num_sums; count++) {
115             abts_run_test(suite, test_md4sum, NULL);
116         }
117
118         return suite;
119 }