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 "apr.h"
18#include "apr_errno.h"
19#include "apr_general.h"
20#include "apr_strings.h"
21#include "apr_xlate.h"
22#include "abts.h"
23#include "testutil.h"
24
25#if APR_HAS_XLATE
26
27static const char test_utf8[] = "Edelwei\xc3\x9f";
28static const char test_utf7[] = "Edelwei+AN8-";
29static const char test_latin1[] = "Edelwei\xdf";
30static const char test_latin2[] = "Edelwei\xdf";
31
32static void test_conversion(abts_case *tc, apr_xlate_t *convset,
33                            const char *inbuf, const char *expected)
34{
35    static char buf[1024];
36    apr_size_t inbytes_left = strlen(inbuf);
37    apr_size_t outbytes_left = sizeof(buf) - 1;
38    apr_status_t rv;
39
40    rv = apr_xlate_conv_buffer(convset, inbuf, &inbytes_left, buf, &outbytes_left);
41    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
42
43    if (rv != APR_SUCCESS)
44        return;
45
46    rv = apr_xlate_conv_buffer(convset, NULL, NULL, buf + sizeof(buf) -
47                               outbytes_left - 1, &outbytes_left);
48    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
49
50    buf[sizeof(buf) - outbytes_left - 1] = '\0';
51
52    ABTS_STR_EQUAL(tc, expected, buf);
53}
54
55static void one_test(abts_case *tc, const char *cs1, const char *cs2,
56                     const char *str1, const char *str2,
57                     apr_pool_t *pool)
58{
59    apr_status_t rv;
60    apr_xlate_t *convset;
61
62    rv = apr_xlate_open(&convset, cs2, cs1, pool);
63    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
64
65    if (rv != APR_SUCCESS)
66        return;
67
68    test_conversion(tc, convset, str1, str2);
69
70    rv = apr_xlate_close(convset);
71    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
72}
73
74#if APU_HAVE_APR_ICONV
75/* it is a bug if iconv_open() fails */
76static int is_transform_supported(abts_case *tc, const char *cs1,
77                                  const char *cs2, apr_pool_t *pool) {
78    return 1;
79}
80#else
81/* some iconv implementations don't support all tested transforms;
82 * example: 8859-1 <-> 8859-2 using native Solaris iconv
83 */
84static int is_transform_supported(abts_case *tc, const char *cs1,
85                                  const char *cs2, apr_pool_t *pool) {
86    apr_status_t rv;
87    apr_xlate_t *convset;
88
89    rv = apr_xlate_open(&convset, cs2, cs1, pool);
90    if (rv != APR_SUCCESS) {
91        return 0;
92    }
93
94    rv = apr_xlate_close(convset);
95    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
96
97    return 1;
98}
99#endif
100
101static void test_transformation(abts_case *tc, void *data)
102{
103    /* 1. Identity transformation: UTF-8 -> UTF-8 */
104    one_test(tc, "UTF-8", "UTF-8", test_utf8, test_utf8, p);
105
106    /* 2. UTF-8 <-> ISO-8859-1 */
107    one_test(tc, "UTF-8", "ISO-8859-1", test_utf8, test_latin1, p);
108    one_test(tc, "ISO-8859-1", "UTF-8", test_latin1, test_utf8, p);
109
110    /* 3. ISO-8859-1 <-> ISO-8859-2, identity */
111    if (is_transform_supported(tc, "ISO-8859-1", "ISO-8859-2", p)) {
112        one_test(tc, "ISO-8859-1", "ISO-8859-2", test_latin1, test_latin2, p);
113    }
114    if (is_transform_supported(tc, "ISO-8859-2", "ISO-8859-1", p)) {
115        one_test(tc, "ISO-8859-2", "ISO-8859-1", test_latin2, test_latin1, p);
116    }
117
118    /* 4. Transformation using charset aliases */
119    one_test(tc, "UTF-8", "UTF-7", test_utf8, test_utf7, p);
120    one_test(tc, "UTF-7", "UTF-8", test_utf7, test_utf8, p);
121}
122
123#endif /* APR_HAS_XLATE */
124
125abts_suite *testxlate(abts_suite *suite)
126{
127    suite = ADD_SUITE(suite);
128
129#if APR_HAS_XLATE
130    abts_run_test(suite, test_transformation, NULL);
131#endif
132
133    return suite;
134}
135