bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / test / testmd4.c
1 /* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * 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
32 #include "apr.h"
33 #include "apr_general.h"
34 #include "apr_file_io.h"
35 #include "apr_time.h"
36 #include "apr_md4.h"
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41
42 /*
43  * This is a MD4 test program based on the code published in RFC 1320.
44  * When run as ./testmd4 -x it should produce the following output:
45
46 MD4 test suite:
47 MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
48 MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
49 MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
50 MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
51 MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
52 MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = 043f8582f241db351ce627e153e7f0e4
53 MD4 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
54
55 */
56
57 /* Length of test block, number of test blocks.
58  */
59 #define TEST_BLOCK_LEN 1000
60 #define TEST_BLOCK_COUNT 1000
61
62 apr_pool_t *local_pool;
63 apr_file_t *in, *out, *err;
64
65 /* Prints a message digest in hexadecimal.
66  */
67 static void MDPrint (unsigned char digest[APR_MD4_DIGESTSIZE])
68 {
69     unsigned int i;
70
71     for (i = 0; i < APR_MD4_DIGESTSIZE; i++)
72         apr_file_printf(out, "%02x", digest[i]);
73 }
74
75 /* Digests a string and prints the result.
76  */
77 static void MDString(char *string)
78 {
79     apr_md4_ctx_t context;
80     unsigned char digest[APR_MD4_DIGESTSIZE];
81     unsigned int len = strlen(string);
82
83     apr_md4_init(&context);
84     apr_md4_update(&context, (unsigned char *)string, len);
85     apr_md4_final(digest, &context);
86
87     apr_file_printf (out, "MD4 (\"%s\") = ", string);
88     MDPrint(digest);
89     apr_file_printf (out, "\n");
90 }
91
92 /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
93      blocks.
94  */
95 static void MDTimeTrial(void)
96 {
97     apr_md4_ctx_t context;
98     apr_time_t endTime, startTime;
99     apr_interval_time_t timeTaken;
100     unsigned char block[TEST_BLOCK_LEN], digest[APR_MD4_DIGESTSIZE];
101     unsigned int i;
102
103     apr_file_printf(out, "MD4 time trial. Digesting %d %d-byte blocks ...", 
104                      TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
105
106     /* Initialize block */
107     for (i = 0; i < TEST_BLOCK_LEN; i++)
108         block[i] = (unsigned char)(i & 0xff);
109
110     /* Start timer */
111     startTime = apr_time_now();
112
113     /* Digest blocks */
114     apr_md4_init(&context);
115     for (i = 0; i < TEST_BLOCK_COUNT; i++)
116         apr_md4_update(&context, block, TEST_BLOCK_LEN);
117
118     apr_md4_final(digest, &context);
119
120     /* Stop timer */
121     endTime = apr_time_now();
122     timeTaken = endTime - startTime;
123
124     apr_file_printf(out, " done\n");
125     apr_file_printf(out, "Digest = ");
126     MDPrint(digest);
127
128     apr_file_printf(out, "\nTime = %" APR_TIME_T_FMT " seconds\n", timeTaken);
129     apr_file_printf(out, "Speed = % " APR_TIME_T_FMT " bytes/second\n",
130                     TEST_BLOCK_LEN * TEST_BLOCK_COUNT/timeTaken);
131 }
132
133 /* Digests a reference suite of strings and prints the results.
134  */
135 static void MDTestSuite(void)
136 {
137     apr_file_printf(out, "MD4 test suite:\n");
138
139     MDString("");
140     MDString("a");
141     MDString("abc");
142     MDString("message digest");
143     MDString("abcdefghijklmnopqrstuvwxyz");
144     MDString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
145     MDString("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
146 }
147
148 /* Digests a file and prints the result.
149  */
150 static void MDFile(char *filename)
151 {
152     apr_file_t *file;
153     apr_md4_ctx_t context;
154     apr_size_t len = 1024;
155     unsigned char buffer[1024], digest[APR_MD4_DIGESTSIZE];
156
157     if (apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, local_pool) 
158         != APR_SUCCESS)
159         apr_file_printf(err, "%s can't be opened\n", filename);
160     else {
161         apr_md4_init(&context);
162         while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
163         {
164             apr_md4_update(&context, buffer, len);
165             len = 1024;
166         }
167         apr_md4_final(digest, &context);
168
169         apr_file_close(file);
170
171         apr_file_printf(out, "MD4 (%s) = ", filename);
172         MDPrint(digest);
173         apr_file_printf(out, "\n");
174     }
175 }
176
177 /* Digests the standard input and prints the result.
178  */
179 static void MDFilter(void)
180 {
181     apr_md4_ctx_t context;
182     apr_size_t len = 16;
183     unsigned char buffer[16], digest[16];
184
185     apr_md4_init(&context);
186     while (apr_file_read(in, buffer, &len) != APR_SUCCESS)
187     {
188         apr_md4_update(&context, buffer, len);
189         len = 16;
190     }
191     apr_md4_update(&context, buffer, len);
192     apr_md4_final(digest, &context);
193
194     MDPrint(digest);
195     apr_file_printf(out, "\n");
196 }
197
198
199 /* Main driver.
200
201    Arguments (may be any combination):
202      -sstring - digests string
203      -t       - runs time trial
204      -x       - runs test script
205      filename - digests file
206      (none)   - digests standard input
207  */
208 int main (int argc, char **argv)
209 {
210     int i;
211
212     apr_initialize();
213     atexit(apr_terminate);
214
215     if (apr_pool_create(&local_pool, NULL) != APR_SUCCESS)
216         exit(-1);
217
218     apr_file_open_stdin(&in, local_pool); 
219     apr_file_open_stdout(&out, local_pool); 
220     apr_file_open_stderr(&err, local_pool); 
221
222     if (argc > 1)
223     {
224         for (i = 1; i < argc; i++)
225             if (argv[i][0] == '-' && argv[i][1] == 's')
226                 MDString(argv[i] + 2);
227             else if (strcmp(argv[i], "-t") == 0)
228                 MDTimeTrial();
229             else if (strcmp (argv[i], "-x") == 0)
230                 MDTestSuite();
231             else
232                 MDFile(argv[i]);
233     }
234     else
235         MDFilter();
236
237     return 0;
238 }