Branch libreoffice-5-0-4
[LibreOffice.git] / tools / qa / cppunit / test_urlobj.cxx
blobb591156b52c7c5dbe252e52a61c864b2fd73b590
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <string>
12 #include <sal/types.h>
13 #include "cppunit/TestFixture.h"
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <tools/stream.hxx>
16 #include <tools/urlobj.hxx>
18 #define OUSTR_TO_STDSTR( oustr ) std::string( OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ).getStr() )
20 CPPUNIT_NS_BEGIN
22 template<> struct assertion_traits<INetProtocol>
24 static bool equal( const INetProtocol& x, const INetProtocol& y )
26 return x == y;
29 static std::string toString( const INetProtocol& x )
31 OStringStream ost;
32 ost << static_cast<unsigned int>(x);
33 return ost.str();
37 CPPUNIT_NS_END
39 namespace tools_urlobj
42 class urlobjTest:public CppUnit::TestFixture
45 public:
46 // initialise your test code values here.
47 void setUp( ) SAL_OVERRIDE
51 void tearDown( ) SAL_OVERRIDE
55 // insert your test code here.
56 // this is only demonstration code
57 void urlobjTest_001( )
59 INetURLObject aUrl( OUString( "file://10.10.1.1/sampledir/sample.file" ) );
60 #ifdef LINUX
61 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
62 == "smb://10.10.1.1/sampledir/sample.file" );
63 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
64 #endif
65 #ifdef WIN
66 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
67 equalsAscii
68 ( "file://10.10.1.1/sampledir/sample.file" ) );
69 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
70 #endif
71 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
72 == "10.10.1.1" );
73 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
74 == "/sampledir/sample.file" );
75 CPPUNIT_ASSERT( aUrl.getName( )
76 == "sample.file" );
77 CPPUNIT_ASSERT( aUrl.getBase( ) == "sample" );
78 CPPUNIT_ASSERT( aUrl.getExtension( ) == "file" );
81 void urlobjTest_002( )
83 INetURLObject aUrl;
84 aUrl.
85 setFSysPath( OUString( "\\\\137.65.170.24\\c$\\Img0001.jpg" ),
86 INetURLObject::FSYS_DETECT );
87 #ifdef LINUX
88 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
89 == "smb://137.65.170.24/c$/Img0001.jpg" );
90 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
91 #endif
92 #ifdef WIN
93 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
94 equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
95 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
96 #endif
97 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
98 == "137.65.170.24" );
99 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
100 == "/c$/Img0001.jpg" );
101 CPPUNIT_ASSERT( aUrl.getName( ) == "Img0001.jpg" );
102 CPPUNIT_ASSERT( aUrl.getBase( ) == "Img0001" );
103 CPPUNIT_ASSERT( aUrl.getExtension( ) == "jpg" );
107 void urlobjTest_003( )
109 INetURLObject aUrl;
110 aUrl.
111 setFSysPath( OUString( "\\\\hive-winxp-x86\\pmladek\\test2.odt" ),
112 INetURLObject::FSYS_DETECT );
113 #ifdef LINUX
114 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
115 == "smb://hive-winxp-x86/pmladek/test2.odt" );
116 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
117 #endif
118 #ifdef WIN
119 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
120 equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
121 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
122 #endif
123 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
124 == "hive-winxp-x86" );
125 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
126 == "/pmladek/test2.odt" );
129 void urlobjTest_004( )
131 INetURLObject aUrl( OUString( "smb://10.10.1.1/sampledir/sample.file" ) );
132 #ifdef LINUX
133 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
134 == "smb://10.10.1.1/sampledir/sample.file" );
135 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
136 #endif
137 #ifdef WIN
138 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
139 equalsAscii( "file://10.10.1.1/sampledir/sample.file" ) );
140 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
141 #endif
142 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
143 == "10.10.1.1" );
144 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
145 == "/sampledir/sample.file" );
146 CPPUNIT_ASSERT( aUrl.getName( ) == "sample.file" );
147 CPPUNIT_ASSERT( aUrl.getBase( ) == "sample" );
148 CPPUNIT_ASSERT( aUrl.getExtension( ) == "file" );
151 void urlobjTest_005( )
153 INetURLObject aUrl;
154 aUrl.setFSysPath( OUString( "//137.65.170.24/c$/Img0001.jpg" ),
155 INetURLObject::FSYS_DETECT );
156 #ifdef LINUX
157 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
158 == "smb://137.65.170.24/c$/Img0001.jpg" );
159 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
160 #endif
161 #ifdef WIN
162 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
163 equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
164 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
165 #endif
166 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
167 == "137.65.170.24" );
168 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
169 == "/c$/Img0001.jpg" );
170 CPPUNIT_ASSERT( aUrl.getName( ) == "Img0001.jpg" );
171 CPPUNIT_ASSERT( aUrl.getBase( ) == "Img0001" );
172 CPPUNIT_ASSERT( aUrl.getExtension( ) == "jpg" );
176 void urlobjTest_006( )
178 INetURLObject aUrl;
179 aUrl.setFSysPath( OUString( "//hive-winxp-x86/pmladek/test2.odt" ),
180 INetURLObject::FSYS_DETECT );
181 #ifdef LINUX
182 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
183 == "smb://hive-winxp-x86/pmladek/test2.odt" );
184 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
185 #endif
186 #ifdef WIN
187 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
188 equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
189 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
190 #endif
191 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
192 == "hive-winxp-x86" );
193 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
194 == "/pmladek/test2.odt" );
197 void urlobjCmisTest( )
199 // Test with a username part
201 INetURLObject aUrl( OUString( "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
202 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
203 OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
204 CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
205 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
206 OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
207 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis, aUrl.GetProtocol( ) );
210 // Test without a username part
212 INetURLObject aUrl( OUString(
213 "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
214 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
215 OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
216 CPPUNIT_ASSERT( !aUrl.HasUserData() );
217 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
218 OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
219 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis, aUrl.GetProtocol( ) );
223 void urlobjTest_emptyPath() {
225 INetURLObject url(OUString("http://example.com"));
226 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, url.GetProtocol());
227 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
228 CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
231 // This is an invalid http URL per RFC 2616:
232 INetURLObject url(OUString("http://example.com?query"));
233 CPPUNIT_ASSERT(url.HasError());
236 INetURLObject url(OUString("http://example.com#fragment"));
237 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, url.GetProtocol());
238 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
239 CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
240 CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url.GetMark());
244 void urlobjTest_data() {
245 INetURLObject url;
246 std::unique_ptr<SvMemoryStream> strm;
247 unsigned char const * buf;
249 url = INetURLObject("data:");
250 //TODO: CPPUNIT_ASSERT(url.HasError());
251 strm = url.getData();
252 CPPUNIT_ASSERT(strm == 0);
254 url = INetURLObject("data:,");
255 CPPUNIT_ASSERT(!url.HasError());
256 strm = url.getData();
257 CPPUNIT_ASSERT(strm != 0);
258 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
259 strm.reset();
261 url = INetURLObject("data:,,%C3%A4%90");
262 CPPUNIT_ASSERT(!url.HasError());
263 strm = url.getData();
264 CPPUNIT_ASSERT(strm != 0);
265 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
266 buf = static_cast<unsigned char const *>(strm->GetData());
267 CPPUNIT_ASSERT_EQUAL(0x2C, int(buf[0]));
268 CPPUNIT_ASSERT_EQUAL(0xC3, int(buf[1]));
269 CPPUNIT_ASSERT_EQUAL(0xA4, int(buf[2]));
270 CPPUNIT_ASSERT_EQUAL(0x90, int(buf[3]));
271 strm.reset();
273 url = INetURLObject("data:base64,");
274 //TODO: CPPUNIT_ASSERT(url.HasError());
275 strm = url.getData();
276 CPPUNIT_ASSERT(strm == 0);
278 url = INetURLObject("data:;base64,");
279 CPPUNIT_ASSERT(!url.HasError());
280 strm = url.getData();
281 CPPUNIT_ASSERT(strm != 0);
282 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
283 strm.reset();
285 url = INetURLObject("data:;bAsE64,");
286 CPPUNIT_ASSERT(!url.HasError());
287 strm = url.getData();
288 CPPUNIT_ASSERT(strm != 0);
289 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
290 strm.reset();
292 url = INetURLObject("data:;base64,YWJjCg==");
293 CPPUNIT_ASSERT(!url.HasError());
294 strm = url.getData();
295 CPPUNIT_ASSERT(strm != 0);
296 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
297 buf = static_cast<unsigned char const *>(strm->GetData());
298 CPPUNIT_ASSERT_EQUAL(0x61, int(buf[0]));
299 CPPUNIT_ASSERT_EQUAL(0x62, int(buf[1]));
300 CPPUNIT_ASSERT_EQUAL(0x63, int(buf[2]));
301 CPPUNIT_ASSERT_EQUAL(0x0A, int(buf[3]));
302 strm.reset();
304 url = INetURLObject("data:;base64,YWJjCg=");
305 CPPUNIT_ASSERT(!url.HasError());
306 strm = url.getData();
307 CPPUNIT_ASSERT(strm == 0);
309 url = INetURLObject("data:;base64,YWJ$Cg==");
310 CPPUNIT_ASSERT(!url.HasError());
311 strm = url.getData();
312 CPPUNIT_ASSERT(strm == 0);
314 url = INetURLObject("data:text/plain;param=%22;base64,%22,YQ==");
315 CPPUNIT_ASSERT(!url.HasError());
316 strm = url.getData();
317 CPPUNIT_ASSERT(strm != 0);
318 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
319 buf = static_cast<unsigned char const *>(strm->GetData());
320 CPPUNIT_ASSERT_EQUAL(0x59, int(buf[0]));
321 CPPUNIT_ASSERT_EQUAL(0x51, int(buf[1]));
322 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[2]));
323 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[3]));
324 strm.reset();
326 url = INetURLObject("http://example.com");
327 CPPUNIT_ASSERT(!url.HasError());
328 strm = url.getData();
329 CPPUNIT_ASSERT(strm == 0);
332 // Change the following lines only, if you add, remove or rename
333 // member functions of the current class,
334 // because these macros are need by auto register mechanism.
336 CPPUNIT_TEST_SUITE( urlobjTest );
337 CPPUNIT_TEST( urlobjTest_001 );
338 CPPUNIT_TEST( urlobjTest_002 );
339 CPPUNIT_TEST( urlobjTest_003 );
340 CPPUNIT_TEST( urlobjTest_004 );
341 CPPUNIT_TEST( urlobjTest_005 );
342 CPPUNIT_TEST( urlobjTest_006 );
343 CPPUNIT_TEST( urlobjCmisTest );
344 CPPUNIT_TEST( urlobjTest_emptyPath );
345 CPPUNIT_TEST( urlobjTest_data );
346 CPPUNIT_TEST_SUITE_END( );
347 }; // class createPool
350 CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
351 } // namespace rtl_random
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */