Fix memory leak when maintaining resources. When extra resources are
[apr-util.git] / test / testpass.c
blob58ac489bd07d801f03d9ce4c4f89e22abea79ce5
1 /* Copyright 2000-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.
16 #include <assert.h>
17 #include <stdio.h>
18 #include <stdlib.h>
20 #include "apr_errno.h"
21 #include "apr_strings.h"
22 #include "apr_file_io.h"
23 #include "apr_thread_proc.h"
24 #include "apr_md5.h"
25 #include "apr_sha1.h"
27 #include "abts.h"
28 #include "testutil.h"
30 static struct {
31 const char *password;
32 const char *hash;
33 } passwords[] =
36 passwords and hashes created with Apache's htpasswd utility like this:
38 htpasswd -c -b passwords pass1 pass1
39 htpasswd -b passwords pass2 pass2
40 htpasswd -b passwords pass3 pass3
41 htpasswd -b passwords pass4 pass4
42 htpasswd -b passwords pass5 pass5
43 htpasswd -b passwords pass6 pass6
44 htpasswd -b passwords pass7 pass7
45 htpasswd -b passwords pass8 pass8
46 (insert Perl one-liner to convert to initializer :) )
48 {"pass1", "1fWDc9QWYCWrQ"},
49 {"pass2", "1fiGx3u7QoXaM"},
50 {"pass3", "1fzijMylTiwCs"},
51 {"pass4", "nHUYc8U2UOP7s"},
52 {"pass5", "nHpETGLGPwAmA"},
53 {"pass6", "nHbsbWmJ3uyhc"},
54 {"pass7", "nHQ3BbF0Y9vpI"},
55 {"pass8", "nHZA1rViSldQk"}
57 static int num_passwords = sizeof(passwords) / sizeof(passwords[0]);
59 static void test_crypt(abts_case *tc, void *data)
61 int i;
63 for (i = 0; i < num_passwords; i++) {
64 apr_assert_success(tc, "check for valid password",
65 apr_password_validate(passwords[i].password,
66 passwords[i].hash));
70 #if APR_HAS_THREADS
72 static void * APR_THREAD_FUNC testing_thread(apr_thread_t *thd,
73 void *data)
75 abts_case *tc = data;
76 int i;
78 for (i = 0; i < 100; i++) {
79 test_crypt(tc, NULL);
82 return APR_SUCCESS;
85 /* test for threadsafe crypt() */
86 static void test_threadsafe(abts_case *tc, void *data)
88 #define NUM_THR 20
89 apr_thread_t *my_threads[NUM_THR];
90 int i;
91 apr_status_t rv;
93 for (i = 0; i < NUM_THR; i++) {
94 apr_assert_success(tc, "create test thread",
95 apr_thread_create(&my_threads[i], NULL,
96 testing_thread, tc, p));
99 for (i = 0; i < NUM_THR; i++) {
100 apr_thread_join(&rv, my_threads[i]);
103 #endif
105 static void test_shapass(abts_case *tc, void *data)
107 const char *pass = "hellojed";
108 char hash[100];
110 apr_sha1_base64(pass, strlen(pass), hash);
112 apr_assert_success(tc, "SHA1 password validated",
113 apr_password_validate(pass, hash));
116 static void test_md5pass(abts_case *tc, void *data)
118 const char *pass = "hellojed", *salt = "sardine";
119 char hash[100];
121 apr_md5_encode(pass, salt, hash, sizeof hash);
123 apr_assert_success(tc, "MD5 password validated",
124 apr_password_validate(pass, hash));
127 abts_suite *testpass(abts_suite *suite)
129 suite = ADD_SUITE(suite);
131 abts_run_test(suite, test_crypt, NULL);
132 #if APR_HAS_THREADS
133 abts_run_test(suite, test_threadsafe, NULL);
134 #endif
135 abts_run_test(suite, test_shapass, NULL);
136 abts_run_test(suite, test_md5pass, NULL);
138 return suite;