Fix memory leak when maintaining resources. When extra resources are
[apr-util.git] / test / testmd4.c
blob1e0199c507c6c27efa966abe5cf96637806de6b6
1 /* Copyright 2001-2004 The Apache Software Foundation
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 /* This is derived from material copyright RSA Data Security, Inc.
16 * Their notice is reproduced below in its entirety.
18 * Copyright (C) 1990-2, RSA Data Security, Inc. Created 1990. All
19 * rights reserved.
21 * RSA Data Security, Inc. makes no representations concerning either
22 * the merchantability of this software or the suitability of this
23 * software for any particular purpose. It is provided "as is"
24 * without express or implied warranty of any kind.
26 * These notices must be retained in any copies of any part of this
27 * documentation and/or software.
31 #include "apr.h"
32 #include "apr_general.h"
33 #include "apr_file_io.h"
34 #include "apr_time.h"
35 #include "apr_md4.h"
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
42 * This is a MD4 test program based on the code published in RFC 1320.
43 * When run as ./testmd4 -x it should produce the following output:
45 MD4 test suite:
46 MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
47 MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
48 MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
49 MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
50 MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
51 MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") = 043f8582f241db351ce627e153e7f0e4
52 MD4 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
56 /* Length of test block, number of test blocks.
58 #define TEST_BLOCK_LEN 1000
59 #define TEST_BLOCK_COUNT 1000
61 apr_pool_t *local_pool;
62 apr_file_t *in, *out, *err;
64 /* Prints a message digest in hexadecimal.
66 static void MDPrint (unsigned char digest[APR_MD4_DIGESTSIZE])
68 unsigned int i;
70 for (i = 0; i < APR_MD4_DIGESTSIZE; i++)
71 apr_file_printf(out, "%02x", digest[i]);
74 /* Digests a string and prints the result.
76 static void MDString(char *string)
78 apr_md4_ctx_t context;
79 unsigned char digest[APR_MD4_DIGESTSIZE];
80 unsigned int len = strlen(string);
82 apr_md4_init(&context);
83 apr_md4_update(&context, (unsigned char *)string, len);
84 apr_md4_final(digest, &context);
86 apr_file_printf (out, "MD4 (\"%s\") = ", string);
87 MDPrint(digest);
88 apr_file_printf (out, "\n");
91 /* Measures the time to digest TEST_BLOCK_COUNT TEST_BLOCK_LEN-byte
92 blocks.
94 static void MDTimeTrial(void)
96 apr_md4_ctx_t context;
97 apr_time_t endTime, startTime;
98 apr_interval_time_t timeTaken;
99 unsigned char block[TEST_BLOCK_LEN], digest[APR_MD4_DIGESTSIZE];
100 unsigned int i;
102 apr_file_printf(out, "MD4 time trial. Digesting %d %d-byte blocks ...",
103 TEST_BLOCK_LEN, TEST_BLOCK_COUNT);
105 /* Initialize block */
106 for (i = 0; i < TEST_BLOCK_LEN; i++)
107 block[i] = (unsigned char)(i & 0xff);
109 /* Start timer */
110 startTime = apr_time_now();
112 /* Digest blocks */
113 apr_md4_init(&context);
114 for (i = 0; i < TEST_BLOCK_COUNT; i++)
115 apr_md4_update(&context, block, TEST_BLOCK_LEN);
117 apr_md4_final(digest, &context);
119 /* Stop timer */
120 endTime = apr_time_now();
121 timeTaken = endTime - startTime;
123 apr_file_printf(out, " done\n");
124 apr_file_printf(out, "Digest = ");
125 MDPrint(digest);
127 apr_file_printf(out, "\nTime = %" APR_TIME_T_FMT " seconds\n", timeTaken);
128 apr_file_printf(out, "Speed = % " APR_TIME_T_FMT " bytes/second\n",
129 TEST_BLOCK_LEN * TEST_BLOCK_COUNT/timeTaken);
132 /* Digests a reference suite of strings and prints the results.
134 static void MDTestSuite(void)
136 apr_file_printf(out, "MD4 test suite:\n");
138 MDString("");
139 MDString("a");
140 MDString("abc");
141 MDString("message digest");
142 MDString("abcdefghijklmnopqrstuvwxyz");
143 MDString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
144 MDString("12345678901234567890123456789012345678901234567890123456789012345678901234567890");
147 /* Digests a file and prints the result.
149 static void MDFile(char *filename)
151 apr_file_t *file;
152 apr_md4_ctx_t context;
153 apr_size_t len = 1024;
154 unsigned char buffer[1024], digest[APR_MD4_DIGESTSIZE];
156 if (apr_file_open(&file, filename, APR_READ, APR_OS_DEFAULT, local_pool)
157 != APR_SUCCESS)
158 apr_file_printf(err, "%s can't be opened\n", filename);
159 else {
160 apr_md4_init(&context);
161 while (apr_file_read(file, buffer, &len) != APR_SUCCESS)
163 apr_md4_update(&context, buffer, len);
164 len = 1024;
166 apr_md4_final(digest, &context);
168 apr_file_close(file);
170 apr_file_printf(out, "MD4 (%s) = ", filename);
171 MDPrint(digest);
172 apr_file_printf(out, "\n");
176 /* Digests the standard input and prints the result.
178 static void MDFilter(void)
180 apr_md4_ctx_t context;
181 apr_size_t len = 16;
182 unsigned char buffer[16], digest[16];
184 apr_md4_init(&context);
185 while (apr_file_read(in, buffer, &len) != APR_SUCCESS)
187 apr_md4_update(&context, buffer, len);
188 len = 16;
190 apr_md4_update(&context, buffer, len);
191 apr_md4_final(digest, &context);
193 MDPrint(digest);
194 apr_file_printf(out, "\n");
198 /* Main driver.
200 Arguments (may be any combination):
201 -sstring - digests string
202 -t - runs time trial
203 -x - runs test script
204 filename - digests file
205 (none) - digests standard input
207 int main (int argc, char **argv)
209 int i;
211 apr_initialize();
212 atexit(apr_terminate);
214 if (apr_pool_create(&local_pool, NULL) != APR_SUCCESS)
215 exit(-1);
217 apr_file_open_stdin(&in, local_pool);
218 apr_file_open_stdout(&out, local_pool);
219 apr_file_open_stderr(&err, local_pool);
221 if (argc > 1)
223 for (i = 1; i < argc; i++)
224 if (argv[i][0] == '-' && argv[i][1] == 's')
225 MDString(argv[i] + 2);
226 else if (strcmp(argv[i], "-t") == 0)
227 MDTimeTrial();
228 else if (strcmp (argv[i], "-x") == 0)
229 MDTestSuite();
230 else
231 MDFile(argv[i]);
233 else
234 MDFilter();
236 return 0;