xref: /aoo4110/main/sal/qa/rtl/uri/rtl_Uri.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 
25 // MARKER(update_precomp.py): autogen include statement, do not remove
26 #include "precompiled_sal.hxx"
27 #include <stdlib.h>
28 #include <rtl/ustring.hxx>
29 #include <rtl/strbuf.hxx>
30 #include <rtl/uri.hxx>
31 #include <osl/thread.h>
32 #include <osl/file.hxx>
33 
34 #include <testshl/simpleheader.hxx>
35 
36 // -----------------------------------------------------------------------------
37 
38 namespace Stringtest
39 {
toHex(unsigned char _c)40     rtl::OString toHex(unsigned char _c)
41     {
42         rtl::OStringBuffer sStrBuf;
43         static char cHex[] = "0123456789ABCDEF";
44 
45         int nhigh = int(_c) >> 4 & 0xf;
46         int nlow  = int(_c) & 0xf;
47         sStrBuf.append( cHex[nhigh] );
48         sStrBuf.append( cHex[nlow] );
49         return sStrBuf.makeStringAndClear();
50     }
51 
escapeString(rtl::OString const & _sStr)52     rtl::OString escapeString(rtl::OString const& _sStr)
53     {
54         rtl::OStringBuffer sStrBuf;
55         sal_Int32 nLength = _sStr.getLength();
56         for(int i=0;i<nLength;++i)
57         {
58             unsigned char c = (unsigned char)_sStr[i];
59             if (c > 127)
60             {
61                 sStrBuf.append("%");
62                 sStrBuf.append(toHex(c));
63             }
64             else
65             {
66                 sStrBuf.append((char)c);
67             }
68         }
69         return sStrBuf.makeStringAndClear();
70     }
71 
72     // -----------------------------------------------------------------------------
73 
74     class Convert : public CppUnit::TestFixture
75     {
76         rtl::OUString m_aStr;
77     public:
78         /*
79           rtl::OString toUTF8(rtl::OUString const& _suStr)
80             {
81                 rtl::OString sStrAsUTF8 = rtl::OUStringToOString(_suStr, RTL_TEXTENCODING_UTF8);
82                 t_print("%s\n", escapeString(sStrAsUTF8).getStr());
83                 return sStrAsUTF8;
84             }
85         */
fromUTF8(rtl::OString const & _suStr)86         rtl::OUString fromUTF8(rtl::OString const& _suStr)
87             {
88                 rtl::OUString suStr = rtl::OStringToOUString(_suStr, RTL_TEXTENCODING_UTF8);
89                 return suStr;
90             }
91 
convertToOString(rtl::OUString const & _suStr)92         rtl::OString convertToOString(rtl::OUString const& _suStr)
93             {
94                 return rtl::OUStringToOString(_suStr, osl_getThreadTextEncoding()/*RTL_TEXTENCODING_ASCII_US*/);
95             }
96 
showContent(rtl::OUString const & _suStr)97         void showContent(rtl::OUString const& _suStr)
98             {
99                 rtl::OString sStr = convertToOString(_suStr);
100                 t_print("%s\n", sStr.getStr());
101             }
102 
toUTF8_mech(rtl::OUString const & _suStr,rtl_UriEncodeMechanism _eMechanism)103         void toUTF8_mech(rtl::OUString const& _suStr, rtl_UriEncodeMechanism _eMechanism)
104             {
105                 rtl::OUString suStr;
106                 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassNone,          _eMechanism, RTL_TEXTENCODING_UTF8);
107                 showContent(suStr);
108                 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUric,          _eMechanism, RTL_TEXTENCODING_UTF8);
109                 showContent(suStr);
110                 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUricNoSlash,   _eMechanism, RTL_TEXTENCODING_UTF8);
111                 showContent(suStr);
112                 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassRelSegment,    _eMechanism, RTL_TEXTENCODING_UTF8);
113                 showContent(suStr);
114                 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassRegName,       _eMechanism, RTL_TEXTENCODING_UTF8);
115                 showContent(suStr);
116                 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUserinfo,      _eMechanism, RTL_TEXTENCODING_UTF8);
117                 showContent(suStr);
118                 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassPchar,         _eMechanism, RTL_TEXTENCODING_UTF8);
119                 showContent(suStr);
120                 suStr = rtl::Uri::encode(_suStr, rtl_UriCharClassUnoParamValue, _eMechanism, RTL_TEXTENCODING_UTF8);
121                 showContent(suStr);
122             }
123 
toUTF8(rtl::OUString const & _suStr)124         void toUTF8(rtl::OUString const& _suStr)
125             {
126                 t_print("rtl_UriEncodeIgnoreEscapes \n");
127                 toUTF8_mech(_suStr, rtl_UriEncodeIgnoreEscapes);
128                 t_print("\n");
129                 t_print("# rtl_UriEncodeKeepEscapes\n");
130                 toUTF8_mech(_suStr, rtl_UriEncodeKeepEscapes);
131                 t_print("\n");
132                 t_print("# rtl_UriEncodeCheckEscapes\n");
133                 toUTF8_mech(_suStr, rtl_UriEncodeCheckEscapes);
134                 t_print("\n");
135             }
136 
test_FromUTF8_001()137         void test_FromUTF8_001()
138             {
139                 // string --> ustring
140                 rtl::OString sStrUTF8("h%C3%A4llo");
141                 rtl::OUString suStrUTF8 = rtl::OStringToOUString(sStrUTF8, RTL_TEXTENCODING_ASCII_US);
142 
143                 // UTF8 --> real ustring
144                 rtl::OUString suStr_UriDecodeToIuri      = rtl::Uri::decode(suStrUTF8, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
145                 showContent(suStr_UriDecodeToIuri);
146 
147                 // string --> ustring
148                 rtl::OString sStr("h\xE4llo");
149                 rtl::OUString suString = rtl::OStringToOUString(sStr, RTL_TEXTENCODING_ISO_8859_15);
150 
151                 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suString.equals(suStr_UriDecodeToIuri) == sal_True);
152 
153                 // ustring --> ustring (UTF8)
154                 rtl::OUString suStr2 = rtl::Uri::encode(suStr_UriDecodeToIuri, rtl_UriCharClassUnoParamValue, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
155                 showContent(suStr2);
156 
157                 CPPUNIT_ASSERT_MESSAGE("Strings must be equal", suStr2.equals(suStrUTF8) == sal_True);
158                 // suStr should be equal to suStr2
159             }
160 
161         // "%C3%84qypten";
162         // testshl2 ../../../unxlngi4.pro/lib/libConvert.so "-onlyerrors"
163         // # Type: 'Directory' file name '%E6%89%8B%E6%9C%BA%E5%8F%B7%E7%A0%81'
164         // # Type: 'Directory' file name '%E6%9C%AA%E5%91%BD%E5%90%8Dzhgb18030'
165         // # Type: 'Regular file' file name '%E5%B7%A5%E4%BD%9C'
166         // # Type: 'Regular file' file name '%E4%BA%8C%E6%89%8B%E6%88%BF%E4%B9%B0%E5%8D%96%E5%90%88%E5%90%8C%E8%8D%89%E7%A8%BF.doc'
167         // ls
getFileTypeName(osl::FileStatus const & _aStatus)168         rtl::OString getFileTypeName(osl::FileStatus const& _aStatus)
169             {
170                 rtl::OString sType;
171                 if (_aStatus.isValid(osl_FileStatus_Mask_Type))
172                 {
173                     osl::FileStatus::Type aType = _aStatus.getFileType();
174                     if (aType == osl::FileStatus::Directory)
175                     {
176                         sType = "Directory";
177                     }
178                     else if (aType == osl::FileStatus::Regular)
179                     {
180                         sType = "Regular file";
181                     }
182                     else if (aType == osl::FileStatus::Volume)
183                     {
184                         sType = "Volume";
185                     }
186                     else if (aType == osl::FileStatus::Fifo)
187                     {
188                         sType = "Fifo";
189                     }
190                     else if (aType == osl::FileStatus::Socket)
191                     {
192                         sType = "Socket";
193                     }
194                     else if (aType == osl::FileStatus::Link)
195                     {
196                         sType = "Link";
197                     }
198                     else if (aType == osl::FileStatus::Special)
199                     {
200                         sType = "Special";
201                     }
202                     else if (aType == osl::FileStatus::Unknown)
203                     {
204                         sType = "Unknown";
205                     }
206                     else
207                     {
208                         sType = "Not handled yet";
209                     }
210                 }
211                 else
212                 {
213                     sType = "ERROR: osl_FileStatus_Mask_Type not set for FileStatus!";
214                 }
215                 return sType;
216             }
217 
218 
test_UTF8_files()219         void test_UTF8_files()
220             {
221 #ifdef UNX
222                 rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///tmp/atestdir"));
223 #else /* Windows */
224                 rtl::OUString suDirURL(rtl::OUString::createFromAscii("file:///c:/temp/atestdir"));
225 #endif
226                 osl::Directory aDir(suDirURL);
227                 aDir.open();
228                 if (aDir.isOpen())
229                 {
230                     osl::DirectoryItem aItem;
231                     osl::FileStatus aStatus(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes | osl_FileStatus_Mask_Type);
232                     while (aDir.getNextItem(aItem) == ::osl::FileBase::E_None)
233                     {
234                         if (osl::FileBase::E_None == aItem.getFileStatus(aStatus) &&
235                             aStatus.isValid(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Attributes))
236                         {
237                             rtl::OString sType = getFileTypeName(aStatus);
238 
239                             rtl::OUString suFilename = aStatus.getFileName();
240                             // rtl::OUString suFullFileURL;
241 
242                             rtl::OUString suStrUTF8 = rtl::Uri::encode(suFilename, rtl_UriCharClassUnoParamValue, rtl_UriEncodeKeepEscapes, RTL_TEXTENCODING_UTF8);
243                             rtl::OString sStrUTF8 = convertToOString(suStrUTF8);
244                             t_print("Type: '%s' file name '%s'\n", sType.getStr(), sStrUTF8.getStr());
245                         }
246                     }
247                     aDir.close();
248                 }
249                 else
250                 {
251                     rtl::OString sStr;
252                     sStr = rtl::OUStringToOString(suDirURL, osl_getThreadTextEncoding());
253                     t_print("can't open dir:'%s'\n", sStr.getStr());
254                 }
255             }
256 
test_FromUTF8()257         void test_FromUTF8()
258             {
259                 rtl::OString sStr("h%C3%A4llo");
260                 rtl::OUString suStr = rtl::OStringToOUString(sStr, osl_getThreadTextEncoding());
261 
262 //    rtl_UriEncodeIgnoreEscapes,
263 //    rtl_UriEncodeKeepEscapes,
264 //     rtl_UriEncodeCheckEscapes,
265 //                rtl::OUString suStr2 = rtl::Uri::encode(suStr, rtl_UriCharClassRegName, rtl_UriEncodeCheckEscapes, RTL_TEXTENCODING_UTF8);
266                 rtl::OUString suStr_UriDecodeNone        = rtl::Uri::decode(suStr, rtl_UriDecodeNone, RTL_TEXTENCODING_UTF8);
267                 showContent(suStr_UriDecodeNone);
268                 toUTF8(suStr_UriDecodeNone);
269 
270                 rtl::OUString suStr_UriDecodeToIuri      = rtl::Uri::decode(suStr, rtl_UriDecodeToIuri, RTL_TEXTENCODING_UTF8);
271                 showContent(suStr_UriDecodeToIuri);
272                 toUTF8(suStr_UriDecodeToIuri);
273 
274                 rtl::OUString suStr_UriDecodeWithCharset = rtl::Uri::decode(suStr, rtl_UriDecodeWithCharset, RTL_TEXTENCODING_UTF8);
275                 showContent(suStr_UriDecodeWithCharset);
276                 toUTF8(suStr_UriDecodeWithCharset);
277             }
278 
279         CPPUNIT_TEST_SUITE( Convert );
280         CPPUNIT_TEST( test_FromUTF8_001 );
281 //        CPPUNIT_TEST( test_UTF8_files );
282 //      CPPUNIT_TEST( test_FromUTF8 );
283         CPPUNIT_TEST_SUITE_END( );
284 	};
285 
286 }
287 
288 
289 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( Stringtest::Convert, "Stringtest" );
290 
291 // LLA: doku anpassen!!!
292 
293 NOADDITIONAL;
294