testpass.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include <assert.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include "apr_errno.h"
  20. #include "apr_strings.h"
  21. #include "apr_file_io.h"
  22. #include "apr_thread_proc.h"
  23. #include "apr_md5.h"
  24. #include "apr_sha1.h"
  25. #include "abts.h"
  26. #include "testutil.h"
  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. htpasswd -c -b passwords pass1 pass1
  35. htpasswd -b passwords pass2 pass2
  36. htpasswd -b passwords pass3 pass3
  37. htpasswd -b passwords pass4 pass4
  38. htpasswd -b passwords pass5 pass5
  39. htpasswd -b passwords pass6 pass6
  40. htpasswd -b passwords pass7 pass7
  41. htpasswd -b passwords pass8 pass8
  42. (insert Perl one-liner to convert to initializer :) )
  43. */
  44. {"pass1", "1fWDc9QWYCWrQ"},
  45. {"pass2", "1fiGx3u7QoXaM"},
  46. {"pass3", "1fzijMylTiwCs"},
  47. {"pass4", "nHUYc8U2UOP7s"},
  48. {"pass5", "nHpETGLGPwAmA"},
  49. {"pass6", "nHbsbWmJ3uyhc"},
  50. {"pass7", "nHQ3BbF0Y9vpI"},
  51. {"pass8", "nHZA1rViSldQk"}
  52. };
  53. static int num_passwords = sizeof(passwords) / sizeof(passwords[0]);
  54. static void test_crypt(abts_case *tc, void *data)
  55. {
  56. int i;
  57. for (i = 0; i < num_passwords; i++) {
  58. apr_assert_success(tc, "check for valid password",
  59. apr_password_validate(passwords[i].password,
  60. passwords[i].hash));
  61. }
  62. }
  63. #if APR_HAS_THREADS
  64. static void * APR_THREAD_FUNC testing_thread(apr_thread_t *thd,
  65. void *data)
  66. {
  67. abts_case *tc = data;
  68. int i;
  69. for (i = 0; i < 100; i++) {
  70. test_crypt(tc, NULL);
  71. }
  72. return APR_SUCCESS;
  73. }
  74. /* test for threadsafe crypt() */
  75. static void test_threadsafe(abts_case *tc, void *data)
  76. {
  77. #define NUM_THR 20
  78. apr_thread_t *my_threads[NUM_THR];
  79. int i;
  80. apr_status_t rv;
  81. for (i = 0; i < NUM_THR; i++) {
  82. apr_assert_success(tc, "create test thread",
  83. apr_thread_create(&my_threads[i], NULL,
  84. testing_thread, tc, p));
  85. }
  86. for (i = 0; i < NUM_THR; i++) {
  87. apr_thread_join(&rv, my_threads[i]);
  88. }
  89. }
  90. #endif
  91. static void test_shapass(abts_case *tc, void *data)
  92. {
  93. const char *pass = "hellojed";
  94. char hash[100];
  95. apr_sha1_base64(pass, strlen(pass), hash);
  96. apr_assert_success(tc, "SHA1 password validated",
  97. apr_password_validate(pass, hash));
  98. }
  99. static void test_md5pass(abts_case *tc, void *data)
  100. {
  101. const char *pass = "hellojed", *salt = "sardine";
  102. char hash[100];
  103. apr_md5_encode(pass, salt, hash, sizeof hash);
  104. apr_assert_success(tc, "MD5 password validated",
  105. apr_password_validate(pass, hash));
  106. }
  107. abts_suite *testpass(abts_suite *suite)
  108. {
  109. suite = ADD_SUITE(suite);
  110. abts_run_test(suite, test_crypt, NULL);
  111. #if APR_HAS_THREADS
  112. abts_run_test(suite, test_threadsafe, NULL);
  113. #endif
  114. abts_run_test(suite, test_shapass, NULL);
  115. abts_run_test(suite, test_md5pass, NULL);
  116. return suite;
  117. }