upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / test / testmd5.c
1 /* Copyright 2000-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
17 #include <assert.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 #include "apr_md5.h"
22 #include "apr_xlate.h"
23 #include "apr_general.h"
24 #include "test_apu.h"
25
26 int cur; 
27
28 struct testcase {
29     const char *s;
30     const char *digest;
31 };
32
33 struct testcase testcases[] =
34 {
35     {"Jeff was here!",
36      "\xa5\x25\x8a\x89\x11\xb2\x9d\x1f\x81\x75\x96\x3b\x60\x94\x49\xc0"},
37     {"01234567890aBcDeFASDFGHJKLPOIUYTR"
38      "POIUYTREWQZXCVBN  LLLLLLLLLLLLLLL",
39      "\xd4\x1a\x06\x2c\xc5\xfd\x6f\x24\x67\x68\x56\x7c\x40\x8a\xd5\x69"},
40     {"111111118888888888888888*******%%%%%%%%%%#####"
41      "142134u8097289720432098409289nkjlfkjlmn,m..   ",
42      "\xb6\xea\x5b\xe8\xca\x45\x8a\x33\xf0\xf1\x84\x6f\xf9\x65\xa8\xe1"},
43     {"01234567890aBcDeFASDFGHJKLPOIUYTR"
44      "POIUYTREWQZXCVBN  LLLLLLLLLLLLLLL"
45      "01234567890aBcDeFASDFGHJKLPOIUYTR"
46      "POIUYTREWQZXCVBN  LLLLLLLLLLLLLLL"
47      "1",
48      "\xd1\xa1\xc0\x97\x8a\x60\xbb\xfb\x2a\x25\x46\x9d\xa5\xae\xd0\xb0"}
49 };
50
51 static void try(const void *buf, apr_size_t bufLen, apr_xlate_t *xlate,
52                 const void *digest)
53 {
54     int i;
55     apr_md5_ctx_t context;
56     unsigned char hash[APR_MD5_DIGESTSIZE];
57     
58     printf("Trying translation %d\n", cur + 1);
59
60     STD_TEST_NEQ("    apr_md5_init", apr_md5_init(&context))
61
62     if (xlate) {
63 #if APR_HAS_XLATE
64         STD_TEST_NEQ("    apr_md5_set_xlate", 
65                      apr_md5_set_xlate(&context, xlate))
66 #else
67         printf("    Didn't expect a translation handle! Not fatal.\n");
68 #endif
69     }
70     
71     STD_TEST_NEQ("    apr_md5_update", apr_md5_update(&context, buf, bufLen))
72     STD_TEST_NEQ("    apr_md5_final", apr_md5_final(hash, &context))
73
74     printf("     (MD5 hash : ");
75     for (i = 0; i < APR_MD5_DIGESTSIZE; i++) {
76         printf("%02x",hash[i]);
77     }
78     
79     printf(")\n");
80
81     printf("%-60s", "    Checking hash against expected");
82     if (memcmp(hash, digest, APR_MD5_DIGESTSIZE)) {
83         /* This is a fatal error...report on stderr */
84         fprintf(stderr, "The digest is not as expected!\n");
85 #if 'A' != 0x41
86         fprintf(stderr,
87                 "Maybe you didn't tell me what character sets "
88                 "to translate between?\n"
89                 "The expected digest is based on the string "
90                 "being in ASCII.\n");
91 #endif
92     }
93     printf("OK\n");
94 }
95
96 int main(int argc, char **argv)
97 {
98     apr_status_t rv;
99     apr_xlate_t *xlate = NULL;
100     apr_pool_t *pool;
101     const char *src = NULL, *dst = NULL;
102
103     switch(argc) {
104     case 1:
105         break;
106     case 3:
107         src = argv[1];
108         dst = argv[2];
109         break;
110     default:
111         fprintf(stderr,
112                 "Usage: %s [src-charset dst-charset]\n",
113                 argv[0]);
114         exit(1);
115     }
116
117     rv = apr_initialize();
118     assert(!rv);
119     atexit(apr_terminate);
120
121     printf("APR MD5 Test\n============\n\n");
122     STD_TEST_NEQ("Creating pool", apr_pool_create(&pool, NULL))
123
124     if (src) {
125 #if APR_HAS_XLATE
126         STD_TEST_NEQ("Opening xlate functions", 
127                      apr_xlate_open(&xlate, dst, src, pool))
128 #else
129         /* This isn't a fatal error, so just report it... */
130         printf("APR doesn't implement translation for this "
131                "configuration.\n");
132 #endif
133     }
134
135     for (cur = 0; cur < sizeof(testcases) / sizeof(testcases[0]); cur++) {
136         try(testcases[cur].s, strlen(testcases[cur].s), xlate,
137             testcases[cur].digest);
138     }
139
140     printf("\nMD5 Test passed.\n");    
141     return 0;
142 }