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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "apr_errno.h"
19 #include "apr_general.h"
20 #include "apr_strings.h"
21 #include "apr_xlate.h"
27 static const char test_utf8[] = "Edelwei\xc3\x9f";
28 static const char test_utf7[] = "Edelwei+AN8-";
29 static const char test_latin1[] = "Edelwei\xdf";
30 static const char test_latin2[] = "Edelwei\xdf";
32 static void test_conversion(abts_case *tc, apr_xlate_t *convset,
33 const char *inbuf, const char *expected)
35 static char buf[1024];
36 apr_size_t inbytes_left = strlen(inbuf);
37 apr_size_t outbytes_left = sizeof(buf) - 1;
40 rv = apr_xlate_conv_buffer(convset, inbuf, &inbytes_left, buf, &outbytes_left);
41 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
43 if (rv != APR_SUCCESS)
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);
50 buf[sizeof(buf) - outbytes_left - 1] = '\0';
52 ABTS_STR_EQUAL(tc, expected, buf);
55 static void one_test(abts_case *tc, const char *cs1, const char *cs2,
56 const char *str1, const char *str2,
62 rv = apr_xlate_open(&convset, cs2, cs1, pool);
63 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
65 if (rv != APR_SUCCESS)
68 test_conversion(tc, convset, str1, str2);
70 rv = apr_xlate_close(convset);
71 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
74 #if APU_HAVE_APR_ICONV
75 /* it is a bug if iconv_open() fails */
76 static int is_transform_supported(abts_case *tc, const char *cs1,
77 const char *cs2, apr_pool_t *pool) {
81 /* some iconv implementations don't support all tested transforms;
82 * example: 8859-1 <-> 8859-2 using native Solaris iconv
84 static int is_transform_supported(abts_case *tc, const char *cs1,
85 const char *cs2, apr_pool_t *pool) {
89 rv = apr_xlate_open(&convset, cs2, cs1, pool);
90 if (rv != APR_SUCCESS) {
94 rv = apr_xlate_close(convset);
95 ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
101 static void test_transformation(abts_case *tc, void *data)
103 /* 1. Identity transformation: UTF-8 -> UTF-8 */
104 one_test(tc, "UTF-8", "UTF-8", test_utf8, test_utf8, p);
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);
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);
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);
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);
123 #endif /* APR_HAS_XLATE */
125 abts_suite *testxlate(abts_suite *suite)
127 suite = ADD_SUITE(suite);
130 abts_run_test(suite, test_transformation, NULL);