xref: /AOO41X/main/sal/qa/rtl/uri/rtl_testuri.cxx (revision 87d2adbc9cadf14644c3679b041b9226f7630199)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "rtl/strbuf.hxx"
28cdf0e10cSrcweir #include "rtl/textenc.h"
29cdf0e10cSrcweir #include "rtl/uri.h"
30cdf0e10cSrcweir #include "rtl/uri.hxx"
31cdf0e10cSrcweir #include "rtl/ustrbuf.hxx"
32cdf0e10cSrcweir #include "rtl/ustring.h"
33cdf0e10cSrcweir #include "rtl/ustring.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include "testshl/simpleheader.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <cstddef>
38cdf0e10cSrcweir #include <stdio.h>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace {
41cdf0e10cSrcweir 
42cdf0e10cSrcweir struct Test: public CppUnit::TestFixture {
43cdf0e10cSrcweir     void test_Uri();
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     CPPUNIT_TEST_SUITE(Test);
46cdf0e10cSrcweir     CPPUNIT_TEST(test_Uri);
47cdf0e10cSrcweir     CPPUNIT_TEST_SUITE_END();
48cdf0e10cSrcweir };
49cdf0e10cSrcweir 
test_Uri()50cdf0e10cSrcweir void Test::test_Uri() {
51cdf0e10cSrcweir     rtl_UriCharClass const eFirstCharClass = rtl_UriCharClassNone;
52cdf0e10cSrcweir     rtl_UriCharClass const eLastCharClass = rtl_UriCharClassUnoParamValue;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     rtl::OUStringBuffer aBuffer;
55cdf0e10cSrcweir     rtl::OUString aText1;
56cdf0e10cSrcweir     rtl::OUString aText2;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     // Check that all characters map back to themselves when encoded/decoded:
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     aText1 = rtl::OUString(
61cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM(
62cdf0e10cSrcweir             "\0x00\0x01\0x02\0x03\0x04\0x05\0x06\0x07"
63cdf0e10cSrcweir             "\0x08\0x09\0x0A\0x0B\0x0C\0x0D\0x0E\0x0F"
64cdf0e10cSrcweir             "\0x10\0x11\0x12\0x13\0x14\0x15\0x16\0x17"
65cdf0e10cSrcweir             "\0x18\0x19\0x1A\0x1B\0x1C\0x1D\0x1E\0x1F"
66cdf0e10cSrcweir             "\0x20\0x21\0x22\0x23\0x24\0x25\0x26\0x27"
67cdf0e10cSrcweir             "\0x28\0x29\0x2A\0x2B\0x2C\0x2D\0x2E\0x2F"
68cdf0e10cSrcweir             "\0x30\0x31\0x32\0x33\0x34\0x35\0x36\0x37"
69cdf0e10cSrcweir             "\0x38\0x39\0x3A\0x3B\0x3C\0x3D\0x3E\0x3F"
70cdf0e10cSrcweir             "\0x40\0x41\0x42\0x43\0x44\0x45\0x46\0x47"
71cdf0e10cSrcweir             "\0x48\0x49\0x4A\0x4B\0x4C\0x4D\0x4E\0x4F"
72cdf0e10cSrcweir             "\0x50\0x51\0x52\0x53\0x54\0x55\0x56\0x57"
73cdf0e10cSrcweir             "\0x58\0x59\0x5A\0x5B\0x5C\0x5D\0x5E\0x5F"
74cdf0e10cSrcweir             "\0x60\0x61\0x62\0x63\0x64\0x65\0x66\0x67"
75cdf0e10cSrcweir             "\0x68\0x69\0x6A\0x6B\0x6C\0x6D\0x6E\0x6F"
76cdf0e10cSrcweir             "\0x70\0x71\0x72\0x73\0x74\0x75\0x76\0x77"
77cdf0e10cSrcweir             "\0x78\0x79\0x7A\0x7B\0x7C\0x7D\0x7E\0x7F"));
78cdf0e10cSrcweir     aText2 = aText1;
79cdf0e10cSrcweir     {for (rtl_UriCharClass eCharClass = eFirstCharClass;
80cdf0e10cSrcweir           eCharClass <= eLastCharClass;
81cdf0e10cSrcweir           eCharClass = static_cast< rtl_UriCharClass >(eCharClass + 1))
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
84cdf0e10cSrcweir             "failure 1",
85cdf0e10cSrcweir             (rtl::Uri::decode(
86cdf0e10cSrcweir                 rtl::Uri::encode(
87cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeKeepEscapes,
88cdf0e10cSrcweir                     RTL_TEXTENCODING_ISO_8859_1),
89cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ASCII_US)
90cdf0e10cSrcweir              == aText2));
91cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
92cdf0e10cSrcweir             "failure 2",
93cdf0e10cSrcweir             (rtl::Uri::decode(
94cdf0e10cSrcweir                 rtl::Uri::encode(
95cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeCheckEscapes,
96cdf0e10cSrcweir                     RTL_TEXTENCODING_ISO_8859_1),
97cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ASCII_US)
98cdf0e10cSrcweir              == aText2));
99cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
100cdf0e10cSrcweir             "failure 3",
101cdf0e10cSrcweir             (rtl::Uri::decode(
102cdf0e10cSrcweir                 rtl::Uri::encode(
103cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeKeepEscapes,
104cdf0e10cSrcweir                     RTL_TEXTENCODING_ISO_8859_1),
105cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ISO_8859_1)
106cdf0e10cSrcweir              == aText2));
107cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
108cdf0e10cSrcweir             "failure 4",
109cdf0e10cSrcweir             (rtl::Uri::decode(
110cdf0e10cSrcweir                 rtl::Uri::encode(
111cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeCheckEscapes,
112cdf0e10cSrcweir                     RTL_TEXTENCODING_ISO_8859_1),
113cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ISO_8859_1)
114cdf0e10cSrcweir              == aText2));
115cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
116cdf0e10cSrcweir             "failure 5",
117cdf0e10cSrcweir             (rtl::Uri::decode(
118cdf0e10cSrcweir                 rtl::Uri::encode(
119cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeKeepEscapes,
120cdf0e10cSrcweir                     RTL_TEXTENCODING_ISO_8859_1),
121cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8)
122cdf0e10cSrcweir              == aText2));
123cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
124cdf0e10cSrcweir             "failure 6",
125cdf0e10cSrcweir             (rtl::Uri::decode(
126cdf0e10cSrcweir                 rtl::Uri::encode(
127cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeCheckEscapes,
128cdf0e10cSrcweir                     RTL_TEXTENCODING_ISO_8859_1),
129cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8)
130cdf0e10cSrcweir              == aText2));
131cdf0e10cSrcweir     }}
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     aText1 = rtl::OUString(
134cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM(
135cdf0e10cSrcweir             "\0x00\0x01\0x02\0x03\0x04\0x05\0x06\0x07"
136cdf0e10cSrcweir             "\0x08\0x09\0x0A\0x0B\0x0C\0x0D\0x0E\0x0F"
137cdf0e10cSrcweir             "\0x10\0x11\0x12\0x13\0x14\0x15\0x16\0x17"
138cdf0e10cSrcweir             "\0x18\0x19\0x1A\0x1B\0x1C\0x1D\0x1E\0x1F"
139cdf0e10cSrcweir             "\0x20\0x21\0x22\0x23\0x24\0x25\0x26\0x27"
140cdf0e10cSrcweir             "\0x28\0x29\0x2A\0x2B\0x2C\0x2D\0x2E\0x2F"
141cdf0e10cSrcweir             "\0x30\0x31\0x32\0x33\0x34\0x35\0x36\0x37"
142cdf0e10cSrcweir             "\0x38\0x39\0x3A\0x3B\0x3C\0x3D\0x3E\0x3F"
143cdf0e10cSrcweir             "\0x40\0x41\0x42\0x43\0x44\0x45\0x46\0x47"
144cdf0e10cSrcweir             "\0x48\0x49\0x4A\0x4B\0x4C\0x4D\0x4E\0x4F"
145cdf0e10cSrcweir             "\0x50\0x51\0x52\0x53\0x54\0x55\0x56\0x57"
146cdf0e10cSrcweir             "\0x58\0x59\0x5A\0x5B\0x5C\0x5D\0x5E\0x5F"
147cdf0e10cSrcweir             "\0x60\0x61\0x62\0x63\0x64\0x65\0x66\0x67"
148cdf0e10cSrcweir             "\0x68\0x69\0x6A\0x6B\0x6C\0x6D\0x6E\0x6F"
149cdf0e10cSrcweir             "\0x70\0x71\0x72\0x73\0x74\0x75\0x76\0x77"
150cdf0e10cSrcweir             "\0x78\0x79\0x7A\0x7B\0x7C\0x7D\0x7E\0x7F"
151cdf0e10cSrcweir             "\0x80\0x81\0x82\0x83\0x84\0x85\0x86\0x87"
152cdf0e10cSrcweir             "\0x88\0x89\0x8A\0x8B\0x8C\0x8D\0x8E\0x8F"
153cdf0e10cSrcweir             "\0x90\0x91\0x92\0x93\0x94\0x95\0x96\0x97"
154cdf0e10cSrcweir             "\0x98\0x99\0x9A\0x9B\0x9C\0x9D\0x9E\0x9F"
155cdf0e10cSrcweir             "\0xA0\0xA1\0xA2\0xA3\0xA4\0xA5\0xA6\0xA7"
156cdf0e10cSrcweir             "\0xA8\0xA9\0xAA\0xAB\0xAC\0xAD\0xAE\0xAF"
157cdf0e10cSrcweir             "\0xB0\0xB1\0xB2\0xB3\0xB4\0xB5\0xB6\0xB7"
158cdf0e10cSrcweir             "\0xB8\0xB9\0xBA\0xBB\0xBC\0xBD\0xBE\0xBF"
159cdf0e10cSrcweir             "\0xC0\0xC1\0xC2\0xC3\0xC4\0xC5\0xC6\0xC7"
160cdf0e10cSrcweir             "\0xC8\0xC9\0xCA\0xCB\0xCC\0xCD\0xCE\0xCF"
161cdf0e10cSrcweir             "\0xD0\0xD1\0xD2\0xD3\0xD4\0xD5\0xD6\0xD7"
162cdf0e10cSrcweir             "\0xD8\0xD9\0xDA\0xDB\0xDC\0xDD\0xDE\0xDF"
163cdf0e10cSrcweir             "\0xE0\0xE1\0xE2\0xE3\0xE4\0xE5\0xE6\0xE7"
164cdf0e10cSrcweir             "\0xE8\0xE9\0xEA\0xEB\0xEC\0xED\0xEE\0xEF"
165cdf0e10cSrcweir             "\0xF0\0xF1\0xF2\0xF3\0xF4\0xF5\0xF6\0xF7"
166cdf0e10cSrcweir             "\0xF8\0xF9\0xFA\0xFB\0xFC\0xFD\0xFE\0xFF"));
167cdf0e10cSrcweir     aText2 = aText1;
168cdf0e10cSrcweir     {for (rtl_UriCharClass eCharClass = eFirstCharClass;
169cdf0e10cSrcweir           eCharClass <= eLastCharClass;
170cdf0e10cSrcweir           eCharClass = static_cast< rtl_UriCharClass >(eCharClass + 1))
171cdf0e10cSrcweir     {
172cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
173cdf0e10cSrcweir             "failure 7",
174cdf0e10cSrcweir             (rtl::Uri::decode(
175cdf0e10cSrcweir                 rtl::Uri::encode(
176cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeKeepEscapes,
177cdf0e10cSrcweir                     RTL_TEXTENCODING_ISO_8859_1),
178cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ISO_8859_1)
179cdf0e10cSrcweir              == aText2));
180cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
181cdf0e10cSrcweir             "failure 8",
182cdf0e10cSrcweir             (rtl::Uri::decode(
183cdf0e10cSrcweir                 rtl::Uri::encode(
184cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeCheckEscapes,
185cdf0e10cSrcweir                     RTL_TEXTENCODING_ISO_8859_1),
186cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ISO_8859_1)
187cdf0e10cSrcweir              == aText2));
188cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
189cdf0e10cSrcweir             "failure 9",
190cdf0e10cSrcweir             (rtl::Uri::decode(
191cdf0e10cSrcweir                 rtl::Uri::encode(
192cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeKeepEscapes,
193cdf0e10cSrcweir                     RTL_TEXTENCODING_UTF8),
194cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8)
195cdf0e10cSrcweir              == aText2));
196cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
197cdf0e10cSrcweir             "failure 10",
198cdf0e10cSrcweir             (rtl::Uri::decode(
199cdf0e10cSrcweir                 rtl::Uri::encode(
200cdf0e10cSrcweir                     aText1, eCharClass, rtl_UriEncodeCheckEscapes,
201cdf0e10cSrcweir                     RTL_TEXTENCODING_UTF8),
202cdf0e10cSrcweir                 rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8)
203cdf0e10cSrcweir              == aText2));
204cdf0e10cSrcweir     }}
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     // Check surrogate handling:
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >(0xD800)); // %ED%A0%80
209cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >(0xD800)); // %F0%90%8F%BF
210cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >(0xDFFF));
211cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >(0xDFFF)); // %ED%BF%BF
212cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >('A')); // A
213cdf0e10cSrcweir     aText1 = aBuffer.makeStringAndClear();
214cdf0e10cSrcweir     aText2 = rtl::OUString(
215cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM(
216cdf0e10cSrcweir             "%ED%A0%80" "%F0%90%8F%BF" "%ED%BF%BF" "A"));
217cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
218cdf0e10cSrcweir         "failure 11",
219cdf0e10cSrcweir         (rtl::Uri::encode(
220cdf0e10cSrcweir             aText1, rtl_UriCharClassUric, rtl_UriEncodeIgnoreEscapes,
221cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8)
222cdf0e10cSrcweir          == aText2));
223cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
224cdf0e10cSrcweir         "failure 12",
225cdf0e10cSrcweir         (rtl::Uri::encode(
226cdf0e10cSrcweir             aText1, rtl_UriCharClassUric, rtl_UriEncodeKeepEscapes,
227cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8)
228cdf0e10cSrcweir          == aText2));
229cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
230cdf0e10cSrcweir         "failure 13",
231cdf0e10cSrcweir         (rtl::Uri::encode(
232cdf0e10cSrcweir             aText1, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes,
233cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8)
234cdf0e10cSrcweir          == aText2));
235cdf0e10cSrcweir 
236cdf0e10cSrcweir     aText1 = rtl::OUString(
237cdf0e10cSrcweir         RTL_CONSTASCII_USTRINGPARAM(
238cdf0e10cSrcweir             "%ed%a0%80" "%f0%90%8f%bf" "%ed%bf%bf" "A"));
239cdf0e10cSrcweir     aBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("%ED%A0%80"));
240cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >(0xD800));
241cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >(0xDFFF));
242cdf0e10cSrcweir     aBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("%ED%BF%BF"));
243cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >('A'));
244cdf0e10cSrcweir     aText2 = aBuffer.makeStringAndClear();
245cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
246cdf0e10cSrcweir         "failure 14",
247cdf0e10cSrcweir         (rtl::Uri::decode(aText1, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8)
248cdf0e10cSrcweir          == aText2));
249cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
250cdf0e10cSrcweir         "failure 15",
251cdf0e10cSrcweir         (rtl::Uri::decode(
252cdf0e10cSrcweir             aText1, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8)
253cdf0e10cSrcweir          == aText2));
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     // Check UTF-8 handling:
256cdf0e10cSrcweir 
257cdf0e10cSrcweir     aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%E0%83%BF"));
258cdf0e10cSrcweir         // \U+00FF encoded with three instead of two bytes
259cdf0e10cSrcweir     aText2 = aText1;
260cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
261cdf0e10cSrcweir         "failure 16",
262cdf0e10cSrcweir         (rtl::Uri::encode(
263cdf0e10cSrcweir             aText1, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes,
264cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8)
265cdf0e10cSrcweir          == aText2));
266cdf0e10cSrcweir 
267cdf0e10cSrcweir     aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%EF%BF%BF"));
268cdf0e10cSrcweir         // \U+FFFF is no legal character
269cdf0e10cSrcweir     aText2 = aText1;
270cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
271cdf0e10cSrcweir         "failure 17",
272cdf0e10cSrcweir         (rtl::Uri::encode(
273cdf0e10cSrcweir             aText1, rtl_UriCharClassUric, rtl_UriEncodeCheckEscapes,
274cdf0e10cSrcweir             RTL_TEXTENCODING_UTF8)
275cdf0e10cSrcweir          == aText2));
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     // Check IURI handling:
278cdf0e10cSrcweir 
279cdf0e10cSrcweir     aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%30%C3%BF"));
280cdf0e10cSrcweir     aBuffer.appendAscii(RTL_CONSTASCII_STRINGPARAM("%30"));
281cdf0e10cSrcweir     aBuffer.append(static_cast< sal_Unicode >(0x00FF));
282cdf0e10cSrcweir     aText2 = aBuffer.makeStringAndClear();
283cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
284cdf0e10cSrcweir         "failure 18",
285cdf0e10cSrcweir         (rtl::Uri::decode(aText1, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8)
286cdf0e10cSrcweir          == aText2));
287cdf0e10cSrcweir 
288cdf0e10cSrcweir     // Check modified rtl_UriCharClassUnoParamValue (removed '[' and ']'):
289cdf0e10cSrcweir 
290cdf0e10cSrcweir     aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("[]%5B%5D"));
291cdf0e10cSrcweir     aText2 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%5B%5D%5B%5D"));
292cdf0e10cSrcweir     CPPUNIT_ASSERT_MESSAGE(
293cdf0e10cSrcweir         "failure 19",
294cdf0e10cSrcweir         (rtl::Uri::encode(
295cdf0e10cSrcweir             aText1, rtl_UriCharClassUnoParamValue, rtl_UriEncodeCheckEscapes,
296cdf0e10cSrcweir             RTL_TEXTENCODING_ASCII_US)
297cdf0e10cSrcweir          == aText2));
298cdf0e10cSrcweir 
299cdf0e10cSrcweir     // Check Uri::convertRelToAbs:
300cdf0e10cSrcweir 
301cdf0e10cSrcweir     struct RelToAbsTest
302cdf0e10cSrcweir     {
303cdf0e10cSrcweir         char const * pBase;
304cdf0e10cSrcweir         char const * pRel;
305cdf0e10cSrcweir         char const * pAbs;
306cdf0e10cSrcweir     };
307cdf0e10cSrcweir     static RelToAbsTest const aRelToAbsTest[]
308cdf0e10cSrcweir         = { // The following tests are taken from RFC 2396:
309cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g:h", "g:h" },
310cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g", "http://a/b/c/g" },
311cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "./g", "http://a/b/c/g" },
312cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g/", "http://a/b/c/g/" },
313cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "/g", "http://a/g" },
314cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "//g", "http://g" },
315cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "?y", "http://a/b/c/?y" },
316cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y" },
317cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "#s", "http://a/b/c/d;p?q#s" },
318cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s" },
319cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s" },
320cdf0e10cSrcweir             { "http://a/b/c/d;p?q", ";x", "http://a/b/c/;x" },
321cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x" },
322cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s" },
323cdf0e10cSrcweir             { "http://a/b/c/d;p?q", ".", "http://a/b/c/" },
324cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "./", "http://a/b/c/" },
325cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "..", "http://a/b/" },
326cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "../", "http://a/b/" },
327cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "../g", "http://a/b/g" },
328cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "../..", "http://a/" },
329cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "../../", "http://a/" },
330cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "../../g", "http://a/g" },
331cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "", "http://a/b/c/d;p?q" },
332cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "../../../g", "http://a/../g" },
333cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "../../../../g", "http://a/../../g" },
334cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "/./g", "http://a/./g" },
335cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "/../g", "http://a/../g" },
336cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g.", "http://a/b/c/g." },
337cdf0e10cSrcweir             { "http://a/b/c/d;p?q", ".g", "http://a/b/c/.g" },
338cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g..", "http://a/b/c/g.." },
339cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "..g", "http://a/b/c/..g" },
340cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "./../g", "http://a/b/g" },
341cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/" },
342cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h" },
343cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g/../h", "http://a/b/c/h" },
344cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y" },
345cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y" },
346cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g?y/./x", "http://a/b/c/g?y/./x" },
347cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g?y/../x", "http://a/b/c/g?y/../x" },
348cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g#s/./x", "http://a/b/c/g#s/./x" },
349cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "g#s/../x", "http://a/b/c/g#s/../x" },
350cdf0e10cSrcweir             { "http://a/b/c/d;p?q", "http:g", "http:g" },
351cdf0e10cSrcweir             { "http!://a/b/c/d;p?q", "g:h", "g:h" },
352cdf0e10cSrcweir             { "http!://a/b/c/d;p?q", "g", 0 },
353cdf0e10cSrcweir             { "http:b/c/d;p?q", "g:h", "g:h" },
354cdf0e10cSrcweir             { "http:b/c/d;p?q", "g", 0 },
355cdf0e10cSrcweir             { "http://a/b/../", "../c", "http://a/b/../../c" },
356cdf0e10cSrcweir             { "http://a/b/..", "../c", "http://a/c" },
357cdf0e10cSrcweir             { "http://a/./b/", ".././.././../c", "http://a/./../../c" } };
358cdf0e10cSrcweir     for (std::size_t i = 0; i < sizeof aRelToAbsTest / sizeof (RelToAbsTest); ++i)
359cdf0e10cSrcweir     {
360cdf0e10cSrcweir         rtl::OUString aAbs;
361cdf0e10cSrcweir         bool bMalformed = false;
362cdf0e10cSrcweir         try {
363cdf0e10cSrcweir             aAbs = rtl::Uri::convertRelToAbs(
364cdf0e10cSrcweir                 rtl::OUString::createFromAscii(aRelToAbsTest[i].pBase),
365cdf0e10cSrcweir                 rtl::OUString::createFromAscii(aRelToAbsTest[i].pRel));
366cdf0e10cSrcweir         } catch (rtl::MalformedUriException &) {
367cdf0e10cSrcweir             bMalformed = true;
368cdf0e10cSrcweir         }
369cdf0e10cSrcweir         if (bMalformed
370cdf0e10cSrcweir             ? aRelToAbsTest[i].pAbs != 0
371cdf0e10cSrcweir             : (aRelToAbsTest[i].pAbs == 0
372cdf0e10cSrcweir                || !aAbs.equalsAscii(aRelToAbsTest[i].pAbs)))
373cdf0e10cSrcweir         {
374cdf0e10cSrcweir             printf(
375cdf0e10cSrcweir                 "FAILED convertRelToAbs(%s, %s) -> %s != %s\n",
376cdf0e10cSrcweir                 aRelToAbsTest[i].pBase, aRelToAbsTest[i].pRel,
377cdf0e10cSrcweir                 (bMalformed
378cdf0e10cSrcweir                  ? "<MALFORMED>"
379cdf0e10cSrcweir                  : rtl::OUStringToOString(
380cdf0e10cSrcweir                      aAbs, RTL_TEXTENCODING_UTF8).getStr()),
381cdf0e10cSrcweir                 (aRelToAbsTest[i].pAbs == 0
382cdf0e10cSrcweir                  ? "<MALFORMED>" : aRelToAbsTest[i].pAbs));
383cdf0e10cSrcweir             CPPUNIT_ASSERT(false);
384cdf0e10cSrcweir         }
385cdf0e10cSrcweir     }
386cdf0e10cSrcweir 
387cdf0e10cSrcweir     // Check encode with unusual text encodings:
388cdf0e10cSrcweir 
389cdf0e10cSrcweir     {
390cdf0e10cSrcweir         sal_Unicode const aText1U[] = { ' ', '!', 0x0401, 0x045F, 0 };
391cdf0e10cSrcweir         aText1 = rtl::OUString(aText1U);
392cdf0e10cSrcweir         aText2 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%20!%A1%FF"));
393cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
394cdf0e10cSrcweir             "failure 20",
395cdf0e10cSrcweir             (rtl::Uri::encode(
396cdf0e10cSrcweir                 aText1, rtl_UriCharClassUric, rtl_UriEncodeIgnoreEscapes,
397cdf0e10cSrcweir                 RTL_TEXTENCODING_ISO_8859_5)
398cdf0e10cSrcweir              == aText2));
399cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
400cdf0e10cSrcweir             "failure 20a",
401cdf0e10cSrcweir             (rtl::Uri::decode(
402cdf0e10cSrcweir                 aText2, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ISO_8859_5)
403cdf0e10cSrcweir              == aText1));
404cdf0e10cSrcweir     }
405cdf0e10cSrcweir     {
406cdf0e10cSrcweir         sal_Unicode const aText1U[] = { ' ', '!', 0x0401, 0x0700, 0x045F, 0 };
407cdf0e10cSrcweir         aText1 = rtl::OUString(aText1U);
408cdf0e10cSrcweir         sal_Unicode const aText2U[] = {
409cdf0e10cSrcweir             '%', '2', '0', '!', '%', 'A', '1', 0x0700, '%', 'F', 'F', 0 };
410cdf0e10cSrcweir         aText2 = rtl::OUString(aText2U);
411cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
412cdf0e10cSrcweir             "failure 21",
413cdf0e10cSrcweir             (rtl::Uri::encode(
414cdf0e10cSrcweir                 aText1, rtl_UriCharClassUric, rtl_UriEncodeIgnoreEscapes,
415cdf0e10cSrcweir                 RTL_TEXTENCODING_ISO_8859_5)
416cdf0e10cSrcweir              == aText2));
417cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
418cdf0e10cSrcweir             "failure 21a",
419cdf0e10cSrcweir             (rtl::Uri::decode(
420cdf0e10cSrcweir                 aText2, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_ISO_8859_5)
421cdf0e10cSrcweir              == aText1));
422cdf0e10cSrcweir     }
423cdf0e10cSrcweir     {
424cdf0e10cSrcweir         sal_Unicode const aText1U[] = { ' ', '!', 0x028A, 0xD849, 0xDD13, 0 };
425cdf0e10cSrcweir         aText1 = rtl::OUString(aText1U);
426cdf0e10cSrcweir         aText2 = rtl::OUString(
427cdf0e10cSrcweir             RTL_CONSTASCII_USTRINGPARAM("%20!%81%30%B1%33%95%39%C5%37"));
428cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
429cdf0e10cSrcweir             "failure 22",
430cdf0e10cSrcweir             (rtl::Uri::encode(
431cdf0e10cSrcweir                 aText1, rtl_UriCharClassUric, rtl_UriEncodeIgnoreEscapes,
432cdf0e10cSrcweir                 RTL_TEXTENCODING_GB_18030)
433cdf0e10cSrcweir              == aText2));
434cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
435cdf0e10cSrcweir             "failure 22a",
436cdf0e10cSrcweir             (rtl::Uri::decode(
437cdf0e10cSrcweir                 aText2, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_GB_18030)
438cdf0e10cSrcweir              == aText1));
439cdf0e10cSrcweir     }
440cdf0e10cSrcweir 
441cdf0e10cSrcweir     // Check strict mode:
442cdf0e10cSrcweir 
443cdf0e10cSrcweir     {
444cdf0e10cSrcweir         sal_Unicode const aText1U[] = { ' ', '!', 0x0401, 0x0700, 0x045F, 0 };
445cdf0e10cSrcweir         aText1 = rtl::OUString(aText1U);
446cdf0e10cSrcweir         aText2 = rtl::OUString();
447cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
448cdf0e10cSrcweir             "failure 23",
449cdf0e10cSrcweir             (rtl::Uri::encode(
450cdf0e10cSrcweir                 aText1, rtl_UriCharClassUric, rtl_UriEncodeStrict,
451cdf0e10cSrcweir                 RTL_TEXTENCODING_ISO_8859_5)
452cdf0e10cSrcweir              == aText2));
453cdf0e10cSrcweir     }
454cdf0e10cSrcweir     {
455cdf0e10cSrcweir         aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%20%C4%80%FF"));
456cdf0e10cSrcweir         aText2 = rtl::OUString();
457cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
458cdf0e10cSrcweir             "failure 24",
459cdf0e10cSrcweir             (rtl::Uri::decode(
460cdf0e10cSrcweir                 aText1, rtl_UriDecodeStrict, RTL_TEXTENCODING_UTF8)
461cdf0e10cSrcweir              == aText2));
462cdf0e10cSrcweir     }
463cdf0e10cSrcweir     {
464cdf0e10cSrcweir         aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%81 "));
465cdf0e10cSrcweir         aText2 = rtl::OUString();
466cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
467cdf0e10cSrcweir             "failure 25",
468cdf0e10cSrcweir             (rtl::Uri::decode(
469cdf0e10cSrcweir                 aText1, rtl_UriDecodeStrict, RTL_TEXTENCODING_GB_18030)
470cdf0e10cSrcweir              == aText2));
471cdf0e10cSrcweir     }
472cdf0e10cSrcweir     {
473cdf0e10cSrcweir         aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%81%20"));
474cdf0e10cSrcweir         aText2 = rtl::OUString();
475cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
476cdf0e10cSrcweir             "failure 26",
477cdf0e10cSrcweir             (rtl::Uri::decode(
478cdf0e10cSrcweir                 aText1, rtl_UriDecodeStrict, RTL_TEXTENCODING_GB_18030)
479cdf0e10cSrcweir              == aText2));
480cdf0e10cSrcweir     }
481cdf0e10cSrcweir     {
482cdf0e10cSrcweir         aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%81%30%B1%33"));
483cdf0e10cSrcweir         sal_Unicode const aText2U[] = { 0x028A, 0 };
484cdf0e10cSrcweir         aText2 = rtl::OUString(aText2U);
485cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
486cdf0e10cSrcweir             "failure 27",
487cdf0e10cSrcweir             (rtl::Uri::decode(
488cdf0e10cSrcweir                 aText1, rtl_UriDecodeStrict, RTL_TEXTENCODING_GB_18030)
489cdf0e10cSrcweir              == aText2));
490cdf0e10cSrcweir     }
491cdf0e10cSrcweir     {
492cdf0e10cSrcweir         aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%810%B13"));
493cdf0e10cSrcweir         sal_Unicode const aText2U[] = { 0x028A, 0 };
494cdf0e10cSrcweir         aText2 = rtl::OUString(aText2U);
495cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
496cdf0e10cSrcweir             "failure 28",
497cdf0e10cSrcweir             (rtl::Uri::decode(
498cdf0e10cSrcweir                 aText1, rtl_UriDecodeStrict, RTL_TEXTENCODING_GB_18030)
499cdf0e10cSrcweir              == aText2));
500cdf0e10cSrcweir     }
501cdf0e10cSrcweir 
502cdf0e10cSrcweir     // Check rtl_UriEncodeStrictKeepEscapes mode:
503cdf0e10cSrcweir 
504cdf0e10cSrcweir     {
505cdf0e10cSrcweir         aText1 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%%ea%c3%aa"));
506cdf0e10cSrcweir         aText2 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%25%EA%C3%AA"));
507cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
508cdf0e10cSrcweir             "failure 29",
509cdf0e10cSrcweir             (rtl::Uri::encode(
510cdf0e10cSrcweir                 aText1, rtl_UriCharClassUric, rtl_UriEncodeStrictKeepEscapes,
511cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8)
512cdf0e10cSrcweir              == aText2));
513cdf0e10cSrcweir     }
514cdf0e10cSrcweir     {
515cdf0e10cSrcweir         sal_Unicode const aText1U[] = { 0x00EA, 0 };
516cdf0e10cSrcweir         aText1 = rtl::OUString(aText1U);
517cdf0e10cSrcweir         aText2 = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("%C3%AA"));
518cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
519cdf0e10cSrcweir             "failure 30",
520cdf0e10cSrcweir             (rtl::Uri::encode(
521cdf0e10cSrcweir                 aText1, rtl_UriCharClassUric, rtl_UriEncodeStrictKeepEscapes,
522cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8)
523cdf0e10cSrcweir              == aText2));
524cdf0e10cSrcweir     }
525cdf0e10cSrcweir     {
526cdf0e10cSrcweir         sal_Unicode const aText1U[] = { ' ', '!', 0x0401, 0x0700, 0x045F, 0 };
527cdf0e10cSrcweir         aText1 = rtl::OUString(aText1U);
528cdf0e10cSrcweir         aText2 = rtl::OUString();
529cdf0e10cSrcweir         CPPUNIT_ASSERT_MESSAGE(
530cdf0e10cSrcweir             "failure 23",
531cdf0e10cSrcweir             (rtl::Uri::encode(
532cdf0e10cSrcweir                 aText1, rtl_UriCharClassUric, rtl_UriEncodeStrictKeepEscapes,
533cdf0e10cSrcweir                 RTL_TEXTENCODING_ISO_8859_5)
534cdf0e10cSrcweir              == aText2));
535cdf0e10cSrcweir     }
536cdf0e10cSrcweir }
537cdf0e10cSrcweir 
538cdf0e10cSrcweir }
539cdf0e10cSrcweir 
540cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(Test, "rtl_testuri");
541cdf0e10cSrcweir NOADDITIONAL;
542