bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / test / testpass.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 <stdio.h>
19 #include <stdlib.h>
20
21 #include "apr_errno.h"
22 #include "apr_strings.h"
23 #include "apr_file_io.h"
24 #include "apr_thread_proc.h"
25 #include "apr_md5.h"
26
27 static struct {
28     const char *password;
29     const char *hash;
30 } passwords[] =
31 {
32 /*
33   passwords and hashes created with Apache's htpasswd utility like this:
34   
35   htpasswd -c -b passwords pass1 pass1
36   htpasswd -b passwords pass2 pass2
37   htpasswd -b passwords pass3 pass3
38   htpasswd -b passwords pass4 pass4
39   htpasswd -b passwords pass5 pass5
40   htpasswd -b passwords pass6 pass6
41   htpasswd -b passwords pass7 pass7
42   htpasswd -b passwords pass8 pass8
43   (insert Perl one-liner to convert to initializer :) )
44  */
45     {"pass1", "1fWDc9QWYCWrQ"},
46     {"pass2", "1fiGx3u7QoXaM"},
47     {"pass3", "1fzijMylTiwCs"},
48     {"pass4", "nHUYc8U2UOP7s"},
49     {"pass5", "nHpETGLGPwAmA"},
50     {"pass6", "nHbsbWmJ3uyhc"},
51     {"pass7", "nHQ3BbF0Y9vpI"},
52     {"pass8", "nHZA1rViSldQk"}
53 };
54 static int num_passwords = sizeof(passwords) / sizeof(passwords[0]);
55
56 static void check_rv(apr_status_t rv)
57 {
58     if (rv != APR_SUCCESS) {
59         fprintf(stderr, "bailing\n");
60         exit(1);
61     }
62 }
63
64 static void test(void)
65 {
66     int i;
67
68     for (i = 0; i < num_passwords; i++) {
69         apr_status_t rv = apr_password_validate(passwords[i].password,
70                                                 passwords[i].hash);
71         assert(rv == APR_SUCCESS);
72     }
73 }
74
75 #if APR_HAS_THREADS
76
77 static void * APR_THREAD_FUNC testing_thread(apr_thread_t *thd,
78                                              void *data)
79 {
80     int i;
81
82     for (i = 0; i < 100; i++) {
83         test();
84     }
85     return APR_SUCCESS;
86 }
87
88 static void thread_safe_test(apr_pool_t *p)
89 {
90 #define NUM_THR 20
91     apr_thread_t *my_threads[NUM_THR];
92     int i;
93     apr_status_t rv;
94     
95     for (i = 0; i < NUM_THR; i++) {
96         rv = apr_thread_create(&my_threads[i], NULL, testing_thread, NULL, p);
97         check_rv(rv);
98     }
99
100     for (i = 0; i < NUM_THR; i++) {
101         apr_thread_join(&rv, my_threads[i]);
102     }
103 }
104 #endif
105
106 int main(void)
107 {
108     apr_status_t rv;
109     apr_pool_t *p;
110
111     rv = apr_initialize();
112     check_rv(rv);
113     rv = apr_pool_create(&p, NULL);
114     check_rv(rv);
115     atexit(apr_terminate);
116
117     /* before creating any threads, test it first just to check
118      * for problems with the test driver
119      */
120     printf("dry run\n");
121     test();
122
123 #if APR_HAS_THREADS
124     printf("thread-safe test\n");
125     thread_safe_test(p);
126 #endif
127     
128     return 0;
129 }