1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
13 #include <sal/types.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <tools/stream.hxx>
17 #include <tools/urlobj.hxx>
19 #define OUSTR_TO_STDSTR( oustr ) std::string( OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ) )
21 template<> inline std::string
CPPUNIT_NS::assertion_traits
<INetProtocol
>::toString(
22 const INetProtocol
& x
)
25 ost
<< static_cast<unsigned int>(x
);
29 namespace tools_urlobj
32 class urlobjTest
:public CppUnit::TestFixture
36 // insert your test code here.
37 // this is only demonstration code
38 void urlobjTest_001( )
40 INetURLObject
aUrl( u
"file://10.10.1.1/sampledir/sample.file" );
41 CPPUNIT_ASSERT_EQUAL(INetProtocol::File
, aUrl
.GetProtocol());
42 CPPUNIT_ASSERT_EQUAL(u
"10.10.1.1"_ustr
,
43 aUrl
.GetHost(INetURLObject::DecodeMechanism::NONE
));
44 CPPUNIT_ASSERT_EQUAL(u
"/sampledir/sample.file"_ustr
,
45 aUrl
.GetURLPath(INetURLObject::DecodeMechanism::NONE
));
46 CPPUNIT_ASSERT_EQUAL(u
"sample.file"_ustr
,
48 CPPUNIT_ASSERT_EQUAL(u
"sample"_ustr
, aUrl
.getBase());
49 CPPUNIT_ASSERT_EQUAL(u
"file"_ustr
, aUrl
.getExtension());
52 void urlobjTest_004( )
54 INetURLObject
aUrl( u
"smb://10.10.1.1/sampledir/sample.file" );
55 CPPUNIT_ASSERT_EQUAL( INetProtocol::Smb
, aUrl
.GetProtocol( ) );
56 CPPUNIT_ASSERT_EQUAL(u
"10.10.1.1"_ustr
,
57 aUrl
.GetHost(INetURLObject::DecodeMechanism::NONE
));
58 CPPUNIT_ASSERT_EQUAL(u
"/sampledir/sample.file"_ustr
,
59 aUrl
.GetURLPath(INetURLObject::DecodeMechanism::NONE
));
60 CPPUNIT_ASSERT_EQUAL(u
"sample.file"_ustr
, aUrl
.getName());
61 CPPUNIT_ASSERT_EQUAL(u
"sample"_ustr
, aUrl
.getBase());
62 CPPUNIT_ASSERT_EQUAL(u
"file"_ustr
, aUrl
.getExtension());
65 void urlobjCmisTest( )
67 // Test with a username part
69 INetURLObject
aUrl( u
"vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" );
70 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
71 OUSTR_TO_STDSTR( aUrl
.GetHost( INetURLObject::DecodeMechanism::WithCharset
) ) );
72 CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl
.GetUser( ) ) );
73 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
74 OUSTR_TO_STDSTR( aUrl
.GetURLPath( INetURLObject::DecodeMechanism::NONE
) ) );
75 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis
, aUrl
.GetProtocol( ) );
78 // Test without a username part
81 u
"vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" );
82 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
83 OUSTR_TO_STDSTR( aUrl
.GetHost( INetURLObject::DecodeMechanism::WithCharset
) ) );
84 CPPUNIT_ASSERT( !aUrl
.HasUserData() );
85 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
86 OUSTR_TO_STDSTR( aUrl
.GetURLPath( INetURLObject::DecodeMechanism::NONE
) ) );
87 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis
, aUrl
.GetProtocol( ) );
91 void urlobjTest_emptyPath() {
93 INetURLObject
url(u
"http://example.com");
94 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http
, url
.GetProtocol());
95 CPPUNIT_ASSERT_EQUAL(u
"example.com"_ustr
, url
.GetHost());
96 CPPUNIT_ASSERT_EQUAL(u
"/"_ustr
, url
.GetURLPath());
99 // This is a valid http URL per RFC 7230:
100 INetURLObject
url(u
"http://example.com?query");
101 CPPUNIT_ASSERT(!url
.HasError());
104 INetURLObject
url(u
"http://example.com#fragment");
105 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http
, url
.GetProtocol());
106 CPPUNIT_ASSERT_EQUAL(u
"example.com"_ustr
, url
.GetHost());
107 CPPUNIT_ASSERT_EQUAL(u
"/"_ustr
, url
.GetURLPath());
108 CPPUNIT_ASSERT_EQUAL(u
"fragment"_ustr
, url
.GetMark());
112 void urlobjTest_data() {
114 std::unique_ptr
<SvMemoryStream
> strm
;
115 unsigned char const * buf
;
117 url
= INetURLObject(u
"data:");
118 //TODO: CPPUNIT_ASSERT(url.HasError());
119 strm
= url
.getData();
120 CPPUNIT_ASSERT(!strm
);
122 url
= INetURLObject(u
"data:,");
123 CPPUNIT_ASSERT(!url
.HasError());
124 strm
= url
.getData();
125 CPPUNIT_ASSERT(strm
!= nullptr);
126 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
129 url
= INetURLObject(u
"data:,,%C3%A4%90");
130 CPPUNIT_ASSERT(!url
.HasError());
131 strm
= url
.getData();
132 CPPUNIT_ASSERT(strm
!= nullptr);
133 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
134 buf
= static_cast<unsigned char const *>(strm
->GetData());
135 CPPUNIT_ASSERT_EQUAL(0x2C, int(buf
[0]));
136 CPPUNIT_ASSERT_EQUAL(0xC3, int(buf
[1]));
137 CPPUNIT_ASSERT_EQUAL(0xA4, int(buf
[2]));
138 CPPUNIT_ASSERT_EQUAL(0x90, int(buf
[3]));
141 url
= INetURLObject(u
"data:base64,");
142 //TODO: CPPUNIT_ASSERT(url.HasError());
143 strm
= url
.getData();
144 CPPUNIT_ASSERT(!strm
);
146 url
= INetURLObject(u
"data:;base64,");
147 CPPUNIT_ASSERT(!url
.HasError());
148 strm
= url
.getData();
149 CPPUNIT_ASSERT(strm
!= nullptr);
150 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
153 url
= INetURLObject(u
"data:;bAsE64,");
154 CPPUNIT_ASSERT(!url
.HasError());
155 strm
= url
.getData();
156 CPPUNIT_ASSERT(strm
!= nullptr);
157 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
160 url
= INetURLObject(u
"data:;base64,YWJjCg==");
161 CPPUNIT_ASSERT(!url
.HasError());
162 strm
= url
.getData();
163 CPPUNIT_ASSERT(strm
!= nullptr);
164 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
165 buf
= static_cast<unsigned char const *>(strm
->GetData());
166 CPPUNIT_ASSERT_EQUAL(0x61, int(buf
[0]));
167 CPPUNIT_ASSERT_EQUAL(0x62, int(buf
[1]));
168 CPPUNIT_ASSERT_EQUAL(0x63, int(buf
[2]));
169 CPPUNIT_ASSERT_EQUAL(0x0A, int(buf
[3]));
172 url
= INetURLObject(u
"data:;base64,YWJjCg=");
173 CPPUNIT_ASSERT(!url
.HasError());
174 strm
= url
.getData();
175 CPPUNIT_ASSERT(!strm
);
177 url
= INetURLObject(u
"data:;base64,YWJ$Cg==");
178 CPPUNIT_ASSERT(!url
.HasError());
179 strm
= url
.getData();
180 CPPUNIT_ASSERT(!strm
);
182 url
= INetURLObject(u
"data:text/plain;param=%22;base64,%22,YQ==");
183 CPPUNIT_ASSERT(!url
.HasError());
184 strm
= url
.getData();
185 CPPUNIT_ASSERT(strm
!= nullptr);
186 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
187 buf
= static_cast<unsigned char const *>(strm
->GetData());
188 CPPUNIT_ASSERT_EQUAL(0x59, int(buf
[0]));
189 CPPUNIT_ASSERT_EQUAL(0x51, int(buf
[1]));
190 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf
[2]));
191 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf
[3]));
194 url
= INetURLObject(u
"http://example.com");
195 CPPUNIT_ASSERT(!url
.HasError());
196 strm
= url
.getData();
197 CPPUNIT_ASSERT(!strm
);
200 void urlobjTest_isSchemeEqualTo() {
201 CPPUNIT_ASSERT(INetURLObject().isSchemeEqualTo(INetProtocol::NotValid
));
202 CPPUNIT_ASSERT(!INetURLObject().isSchemeEqualTo(u
""));
204 INetURLObject(u
"http://example.org").isSchemeEqualTo(
205 INetProtocol::Http
));
207 !INetURLObject(u
"http://example.org").isSchemeEqualTo(
208 INetProtocol::Https
));
210 INetURLObject(u
"http://example.org").isSchemeEqualTo(u
"Http"));
212 !INetURLObject(u
"http://example.org").isSchemeEqualTo(u
"dav"));
214 INetURLObject(u
"dav://example.org").isSchemeEqualTo(u
"dav"));
217 void urlobjTest_isAnyKnownWebDAVScheme() {
219 INetURLObject(u
"http://example.org").isAnyKnownWebDAVScheme());
221 INetURLObject(u
"https://example.org").isAnyKnownWebDAVScheme());
223 INetURLObject(u
"vnd.sun.star.webdav://example.org").isAnyKnownWebDAVScheme());
225 INetURLObject(u
"vnd.sun.star.webdavs://example.org").isAnyKnownWebDAVScheme());
227 !INetURLObject(u
"ftp://example.org").isAnyKnownWebDAVScheme());
229 !INetURLObject(u
"file://example.org").isAnyKnownWebDAVScheme());
231 !INetURLObject(u
"dav://example.org").isAnyKnownWebDAVScheme());
233 !INetURLObject(u
"davs://example.org").isAnyKnownWebDAVScheme());
235 !INetURLObject(u
"vnd.sun.star.pkg://example.org").isAnyKnownWebDAVScheme());
240 INetURLObject
obj(u
"file:///");
241 bool ok
= obj
.setName(u
"foo");
245 INetURLObject
obj(u
"file:///foo");
246 bool ok
= obj
.setName(u
"bar");
248 CPPUNIT_ASSERT_EQUAL(
249 u
"file:///bar"_ustr
, obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
252 INetURLObject
obj(u
"file:///foo/");
253 bool ok
= obj
.setName(u
"bar");
255 CPPUNIT_ASSERT_EQUAL(
256 u
"file:///bar/"_ustr
, obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
259 INetURLObject
obj(u
"file:///foo/bar");
260 bool ok
= obj
.setName(u
"baz");
262 CPPUNIT_ASSERT_EQUAL(
263 u
"file:///foo/baz"_ustr
,
264 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
267 INetURLObject
obj(u
"file:///foo/bar/");
268 bool ok
= obj
.setName(u
"baz");
270 CPPUNIT_ASSERT_EQUAL(
271 u
"file:///foo/baz/"_ustr
,
272 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
276 void testSetExtension() {
277 INetURLObject
obj(u
"file:///foo/bar.baz/");
278 bool ok
= obj
.setExtension(
279 u
"other", INetURLObject::LAST_SEGMENT
, false);
281 CPPUNIT_ASSERT_EQUAL(
282 u
"file:///foo/bar.baz/.other"_ustr
,
283 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
286 void testChangeScheme() {
287 INetURLObject
obj(u
"unknown://example.com/foo/bar");
288 CPPUNIT_ASSERT(!obj
.HasError());
289 obj
.changeScheme(INetProtocol::Http
);
290 CPPUNIT_ASSERT_EQUAL(
291 u
"http://example.com/foo/bar"_ustr
,
292 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
293 obj
.changeScheme(INetProtocol::Https
);
294 CPPUNIT_ASSERT_EQUAL(
295 u
"https://example.com/foo/bar"_ustr
,
296 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
297 obj
.changeScheme(INetProtocol::Ftp
);
298 CPPUNIT_ASSERT_EQUAL(
299 u
"ftp://example.com/foo/bar"_ustr
,
300 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
303 void testTd146382() {
304 INetURLObject
obj(u
"file://share.allotropia.de@SSL/DavWWWRoot/remote.php");
305 CPPUNIT_ASSERT(!obj
.HasError());
306 CPPUNIT_ASSERT_EQUAL(
307 u
"file://share.allotropia.de@SSL/DavWWWRoot/remote.php"_ustr
,
308 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
311 void testParseSmart()
314 // host:port must not be misinterpreted as scheme:path
315 INetURLObject
obj(u
"example.com:8080/foo", INetProtocol::Http
);
316 CPPUNIT_ASSERT(!obj
.HasError());
317 CPPUNIT_ASSERT_EQUAL(u
"http://example.com:8080/foo"_ustr
,
318 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
319 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http
, obj
.GetProtocol());
320 CPPUNIT_ASSERT_EQUAL(u
"example.com"_ustr
, obj
.GetHost());
321 CPPUNIT_ASSERT_EQUAL(sal_uInt32(8080), obj
.GetPort());
322 CPPUNIT_ASSERT_EQUAL(u
"/foo"_ustr
, obj
.GetURLPath());
325 // port may only contain decimal digits, so this must be treated as unknown scheme
326 INetURLObject
obj(u
"example.com:80a0/foo", INetProtocol::Http
);
327 CPPUNIT_ASSERT(!obj
.HasError());
328 CPPUNIT_ASSERT_EQUAL(u
"example.com:80a0/foo"_ustr
,
329 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
330 CPPUNIT_ASSERT_EQUAL(INetProtocol::Generic
, obj
.GetProtocol());
331 CPPUNIT_ASSERT(obj
.isSchemeEqualTo(u
"example.com"));
332 CPPUNIT_ASSERT_EQUAL(u
""_ustr
, obj
.GetHost());
333 CPPUNIT_ASSERT_EQUAL(u
"80a0/foo"_ustr
, obj
.GetURLPath());
336 // Test Windows \\?\C:... long path scheme
337 INetURLObject
base(u
"file:///C:/foo"); // set up base path
338 bool bWasAbsolute
= false;
340 = base
.smartRel2Abs(u
"\\\\?\\D:\\bar\\baz.ext"_ustr
, bWasAbsolute
);
341 CPPUNIT_ASSERT(bWasAbsolute
);
342 CPPUNIT_ASSERT_EQUAL(u
"file:///D:/bar/baz.ext"_ustr
,
343 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
346 // Test Windows \\?\UNC\Server... long path scheme
347 INetURLObject
base(u
"file://ServerFoo/fooShare"); // set up base path
348 bool bWasAbsolute
= false;
349 INetURLObject obj
= base
.smartRel2Abs(
350 u
"\\\\?\\UNC\\ServerBar\\barShare\\baz.ext"_ustr
, bWasAbsolute
);
351 CPPUNIT_ASSERT(bWasAbsolute
);
352 CPPUNIT_ASSERT_EQUAL(u
"file://ServerBar/barShare/baz.ext"_ustr
,
353 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
357 void testIsExoticProtocol() {
359 INetURLObject
url(u
"vnd.sun.star.pkg://slot%3A0");
360 CPPUNIT_ASSERT_EQUAL(INetProtocol::VndSunStarPkg
, url
.GetProtocol());
361 CPPUNIT_ASSERT(url
.IsExoticProtocol());
364 INetURLObject
url(u
"vnd.sun.star.pkg://vnd.sun.star.pkg%3A%2F%2Fslot%253A0");
365 CPPUNIT_ASSERT_EQUAL(INetProtocol::VndSunStarPkg
, url
.GetProtocol());
366 CPPUNIT_ASSERT(url
.IsExoticProtocol());
369 INetURLObject
url(u
"vnd.sun.star.pkg://http%3A%2F%2Fexample.net");
370 CPPUNIT_ASSERT_EQUAL(INetProtocol::VndSunStarPkg
, url
.GetProtocol());
371 CPPUNIT_ASSERT(!url
.IsExoticProtocol());
374 INetURLObject
url(u
"vnd.sun.star.zip://slot%3A0");
375 CPPUNIT_ASSERT_EQUAL(INetProtocol::Generic
, url
.GetProtocol());
376 CPPUNIT_ASSERT(url
.IsExoticProtocol());
379 INetURLObject
url(u
"vnd.sun.star.zip://slot%3A0/foo");
380 CPPUNIT_ASSERT_EQUAL(INetProtocol::Generic
, url
.GetProtocol());
381 CPPUNIT_ASSERT(url
.IsExoticProtocol());
384 INetURLObject
url(u
"vnd.sun.star.zip://slot%3A0?foo");
385 CPPUNIT_ASSERT_EQUAL(INetProtocol::Generic
, url
.GetProtocol());
386 CPPUNIT_ASSERT(url
.IsExoticProtocol());
389 INetURLObject
url(u
"vnd.sun.star.zip://slot%3A0#foo");
390 CPPUNIT_ASSERT_EQUAL(INetProtocol::Generic
, url
.GetProtocol());
391 CPPUNIT_ASSERT(url
.IsExoticProtocol());
394 INetURLObject
url(u
"vnd.sun.star.zip://http%3A%2F%2Fexample.net");
395 CPPUNIT_ASSERT_EQUAL(INetProtocol::Generic
, url
.GetProtocol());
396 CPPUNIT_ASSERT(!url
.IsExoticProtocol());
400 // Change the following lines only, if you add, remove or rename
401 // member functions of the current class,
402 // because these macros are need by auto register mechanism.
404 CPPUNIT_TEST_SUITE( urlobjTest
);
405 CPPUNIT_TEST( urlobjTest_001
);
406 CPPUNIT_TEST( urlobjTest_004
);
407 CPPUNIT_TEST( urlobjCmisTest
);
408 CPPUNIT_TEST( urlobjTest_emptyPath
);
409 CPPUNIT_TEST( urlobjTest_data
);
410 CPPUNIT_TEST( urlobjTest_isSchemeEqualTo
);
411 CPPUNIT_TEST( urlobjTest_isAnyKnownWebDAVScheme
);
412 CPPUNIT_TEST( testSetName
);
413 CPPUNIT_TEST( testSetExtension
);
414 CPPUNIT_TEST( testChangeScheme
);
415 CPPUNIT_TEST( testTd146382
);
416 CPPUNIT_TEST( testParseSmart
);
417 CPPUNIT_TEST( testIsExoticProtocol
);
418 CPPUNIT_TEST_SUITE_END( );
419 }; // class createPool
422 CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest
);
423 } // namespace rtl_random
426 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */