summaryrefslogtreecommitdiff
path: root/test/testbase64.c
blob: 7e0d7d8ed8deae1ea18d24ad21763c2087c4b384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "apr_base64.h"

#include "abts.h"
#include "testutil.h"

static struct {
   const char *orig;
   const char *enc;
} base64_tbl[] =
{
    {"", ""},
    {"H", "SA=="},
    {"He", "SGU="},
    {"Hel", "SGVs"},
    {"Hell", "SGVsbA=="},
    {"Hello", "SGVsbG8="},
    {"Hello World", "SGVsbG8gV29ybGQ="},
    {"\xff\xff\xff\xff", "/////w=="},
};
static int num_base64 = sizeof(base64_tbl) / sizeof(base64_tbl[0]);

static void test_base64(abts_case *tc, void *data)
{
    apr_pool_t *pool;
    int i;

    apr_pool_create(&pool, NULL);

    for (i = 0; i < num_base64; i++) {
        char *enc;
        int orig_len, enc_len, b64_len, b64_enc_len;

        apr_pool_clear(pool);

        orig_len = (int)strlen(base64_tbl[i].orig);
        enc_len = (int)strlen(base64_tbl[i].enc);

        /* includes + 1 for term null */
        b64_enc_len = apr_base64_encode_len(orig_len);
        ABTS_ASSERT(tc, "base 64 exp. length", (enc_len == (b64_enc_len - 1)));

        enc = apr_palloc(pool, b64_enc_len);

        b64_len = apr_base64_encode(enc, base64_tbl[i].orig, orig_len);

        ABTS_ASSERT(tc, "base 64 encoded length", (b64_enc_len == b64_len));
        ABTS_ASSERT(tc, "base 64 encoded matches expected output",
                    (memcmp(enc, base64_tbl[i].enc, b64_enc_len) == 0));

        enc = apr_pbase64_encode(pool, base64_tbl[i].orig);
        ABTS_ASSERT(tc, "base 64 encoded from pool matches expected output",
                (strcmp(enc, base64_tbl[i].enc) == 0));
        ABTS_ASSERT(tc, "base 64 length", strlen(enc) == strlen(base64_tbl[i].enc));
    }
}

abts_suite *testbase64(abts_suite *suite)
{
    suite = ADD_SUITE(suite);

    abts_run_test(suite, test_base64, NULL);

    return suite;
}