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