1a8f6409peter/* Licensed to the Apache Software Foundation (ASF) under one or more
2a8f6409peter * contributor license agreements.  See the NOTICE file distributed with
3a8f6409peter * this work for additional information regarding copyright ownership.
4a8f6409peter * The ASF licenses this file to You under the Apache License, Version 2.0
5a8f6409peter * (the "License"); you may not use this file except in compliance with
6a8f6409peter * the License.  You may obtain a copy of the License at
7a8f6409peter *
8a8f6409peter *     http://www.apache.org/licenses/LICENSE-2.0
9a8f6409peter *
10a8f6409peter * Unless required by applicable law or agreed to in writing, software
11a8f6409peter * distributed under the License is distributed on an "AS IS" BASIS,
12a8f6409peter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8f6409peter * See the License for the specific language governing permissions and
14a8f6409peter * limitations under the License.
15a8f6409peter */
16a8f6409peter
17a8f6409peter#include <assert.h>
18a8f6409peter#include <stdio.h>
19a8f6409peter#include <stdlib.h>
20a8f6409peter
21a8f6409peter#include "apr_md5.h"
22a8f6409peter#include "apr_xlate.h"
23a8f6409peter#include "apr_general.h"
24a8f6409peter
25a8f6409peter#include "abts.h"
26a8f6409peter#include "testutil.h"
27a8f6409peter
28a8f6409peterstatic struct {
29a8f6409peter    const char *string;
30a8f6409peter    const char *digest;
31a8f6409peter} md5sums[] =
32a8f6409peter{
33a8f6409peter    {"Jeff was here!",
34a8f6409peter     "\xa5\x25\x8a\x89\x11\xb2\x9d\x1f\x81\x75\x96\x3b\x60\x94\x49\xc0"},
35a8f6409peter    {"01234567890aBcDeFASDFGHJKLPOIUYTR"
36a8f6409peter     "POIUYTREWQZXCVBN  LLLLLLLLLLLLLLL",
37a8f6409peter     "\xd4\x1a\x06\x2c\xc5\xfd\x6f\x24\x67\x68\x56\x7c\x40\x8a\xd5\x69"},
38a8f6409peter    {"111111118888888888888888*******%%%%%%%%%%#####"
39a8f6409peter     "142134u8097289720432098409289nkjlfkjlmn,m..   ",
40a8f6409peter     "\xb6\xea\x5b\xe8\xca\x45\x8a\x33\xf0\xf1\x84\x6f\xf9\x65\xa8\xe1"},
41a8f6409peter    {"01234567890aBcDeFASDFGHJKLPOIUYTR"
42a8f6409peter     "POIUYTREWQZXCVBN  LLLLLLLLLLLLLLL"
43a8f6409peter     "01234567890aBcDeFASDFGHJKLPOIUYTR"
44a8f6409peter     "POIUYTREWQZXCVBN  LLLLLLLLLLLLLLL"
45a8f6409peter     "1",
46a8f6409peter     "\xd1\xa1\xc0\x97\x8a\x60\xbb\xfb\x2a\x25\x46\x9d\xa5\xae\xd0\xb0"}
47a8f6409peter};
48a8f6409peter
49a8f6409peterstatic int num_sums = sizeof(md5sums) / sizeof(md5sums[0]);
50a8f6409peterstatic int count;
51a8f6409peter
52a8f6409peterstatic void test_md5sum(abts_case *tc, void *data)
53a8f6409peter{
54a8f6409peter        apr_md5_ctx_t context;
55a8f6409peter        unsigned char digest[APR_MD5_DIGESTSIZE];
56a8f6409peter        const void *string = md5sums[count].string;
57a8f6409peter        const void *sum = md5sums[count].digest;
58a8f6409peter        unsigned int len = strlen(string);
59a8f6409peter
60a8f6409peter        ABTS_ASSERT(tc, "apr_md5_init", (apr_md5_init(&context) == 0));
61a8f6409peter        ABTS_ASSERT(tc, "apr_md5_update",
62a8f6409peter                    (apr_md5_update(&context, string, len) == 0));
63a8f6409peter        ABTS_ASSERT(tc, "apr_md5_final", (apr_md5_final(digest, &context)
64a8f6409peter                                          == 0));
65a8f6409peter        ABTS_ASSERT(tc, "check for correct md5 digest",
66a8f6409peter                    (memcmp(digest, sum, APR_MD5_DIGESTSIZE) == 0));
67a8f6409peter}
68a8f6409peter
69b910f82peterstatic void test_md5sum_unaligned(abts_case *tc, void *data)
70b910f82peter{
71b910f82peter        apr_md5_ctx_t context;
72b910f82peter        const char *string = "abcdefghijklmnopqrstuvwxyz01234"
73b910f82peter                             "abcdefghijklmnopqrstuvwxyz01234"
74b910f82peter                             "abcdefghijklmnopqrstuvwxyz01234"
75b910f82peter                             "abcdefghijklmnopqrstuvwxyz01234_";
76b910f82peter        const char *sum =
77b910f82peter            "\x93\x17\x22\x78\xee\x30\x82\xb3\xeb\x95\x33\xec\xea\x78\xb7\x89";
78b910f82peter        unsigned char digest[APR_MD5_DIGESTSIZE];
79b910f82peter        unsigned int i;
80b910f82peter
81b910f82peter        ABTS_ASSERT(tc, "apr_md5_init", (apr_md5_init(&context) == 0));
82b910f82peter        for (i = 0; i < 10; i++) {
83b910f82peter                ABTS_ASSERT(tc, "apr_md5_update",
84b910f82peter                    (apr_md5_update(&context, string, strlen(string)) == 0));
85b910f82peter                string++;
86b910f82peter        }
87b910f82peter        ABTS_ASSERT(tc, "apr_md5_final", (apr_md5_final(digest, &context)
88b910f82peter                                          == 0));
89b910f82peter        ABTS_ASSERT(tc, "check for correct md5 digest of unaligned data",
90b910f82peter                    (memcmp(digest, sum, APR_MD5_DIGESTSIZE) == 0));
91b910f82peter}
92b910f82peter
93a8f6409peterabts_suite *testmd5(abts_suite *suite)
94a8f6409peter{
95a8f6409peter        suite = ADD_SUITE(suite);
96a8f6409peter
97a8f6409peter        for (count=0; count < num_sums; count++) {
98a8f6409peter            abts_run_test(suite, test_md5sum, NULL);
99a8f6409peter        }
100b910f82peter        abts_run_test(suite, test_md5sum_unaligned, NULL);
101a8f6409peter
102a8f6409peter        return suite;
103a8f6409peter}
104