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 ).getStr() )
23 template<> struct assertion_traits
<INetProtocol
>
25 static bool equal( const INetProtocol
& x
, const INetProtocol
& y
)
30 static std::string
toString( const INetProtocol
& x
)
33 ost
<< static_cast<unsigned int>(x
);
40 namespace tools_urlobj
43 class urlobjTest
:public CppUnit::TestFixture
47 // insert your test code here.
48 // this is only demonstration code
49 void urlobjTest_001( )
51 INetURLObject
aUrl( u
"file://10.10.1.1/sampledir/sample.file" );
52 CPPUNIT_ASSERT_EQUAL(INetProtocol::File
, aUrl
.GetProtocol());
53 CPPUNIT_ASSERT_EQUAL(OUString("10.10.1.1"),
54 aUrl
.GetHost(INetURLObject::DecodeMechanism::NONE
));
55 CPPUNIT_ASSERT_EQUAL(OUString("/sampledir/sample.file"),
56 aUrl
.GetURLPath(INetURLObject::DecodeMechanism::NONE
));
57 CPPUNIT_ASSERT_EQUAL(OUString("sample.file"),
59 CPPUNIT_ASSERT_EQUAL(OUString("sample"), aUrl
.getBase());
60 CPPUNIT_ASSERT_EQUAL(OUString("file"), aUrl
.getExtension());
63 void urlobjTest_004( )
65 INetURLObject
aUrl( u
"smb://10.10.1.1/sampledir/sample.file" );
66 CPPUNIT_ASSERT_EQUAL( INetProtocol::Smb
, aUrl
.GetProtocol( ) );
67 CPPUNIT_ASSERT_EQUAL(OUString("10.10.1.1"),
68 aUrl
.GetHost(INetURLObject::DecodeMechanism::NONE
));
69 CPPUNIT_ASSERT_EQUAL(OUString("/sampledir/sample.file"),
70 aUrl
.GetURLPath(INetURLObject::DecodeMechanism::NONE
));
71 CPPUNIT_ASSERT_EQUAL(OUString("sample.file"), aUrl
.getName());
72 CPPUNIT_ASSERT_EQUAL(OUString("sample"), aUrl
.getBase());
73 CPPUNIT_ASSERT_EQUAL(OUString("file"), aUrl
.getExtension());
76 void urlobjCmisTest( )
78 // Test with a username part
80 INetURLObject
aUrl( u
"vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" );
81 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
82 OUSTR_TO_STDSTR( aUrl
.GetHost( INetURLObject::DecodeMechanism::WithCharset
) ) );
83 CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl
.GetUser( ) ) );
84 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
85 OUSTR_TO_STDSTR( aUrl
.GetURLPath( INetURLObject::DecodeMechanism::NONE
) ) );
86 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis
, aUrl
.GetProtocol( ) );
89 // Test without a username part
92 u
"vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" );
93 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
94 OUSTR_TO_STDSTR( aUrl
.GetHost( INetURLObject::DecodeMechanism::WithCharset
) ) );
95 CPPUNIT_ASSERT( !aUrl
.HasUserData() );
96 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
97 OUSTR_TO_STDSTR( aUrl
.GetURLPath( INetURLObject::DecodeMechanism::NONE
) ) );
98 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis
, aUrl
.GetProtocol( ) );
102 void urlobjTest_emptyPath() {
104 INetURLObject
url(u
"http://example.com");
105 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http
, url
.GetProtocol());
106 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url
.GetHost());
107 CPPUNIT_ASSERT_EQUAL(OUString("/"), url
.GetURLPath());
110 // This is an invalid http URL per RFC 2616:
111 INetURLObject
url(u
"http://example.com?query");
112 CPPUNIT_ASSERT(url
.HasError());
115 INetURLObject
url(u
"http://example.com#fragment");
116 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http
, url
.GetProtocol());
117 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url
.GetHost());
118 CPPUNIT_ASSERT_EQUAL(OUString("/"), url
.GetURLPath());
119 CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url
.GetMark());
123 void urlobjTest_data() {
125 std::unique_ptr
<SvMemoryStream
> strm
;
126 unsigned char const * buf
;
128 url
= INetURLObject(u
"data:");
129 //TODO: CPPUNIT_ASSERT(url.HasError());
130 strm
= url
.getData();
131 CPPUNIT_ASSERT(!strm
);
133 url
= INetURLObject(u
"data:,");
134 CPPUNIT_ASSERT(!url
.HasError());
135 strm
= url
.getData();
136 CPPUNIT_ASSERT(strm
!= nullptr);
137 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
140 url
= INetURLObject(u
"data:,,%C3%A4%90");
141 CPPUNIT_ASSERT(!url
.HasError());
142 strm
= url
.getData();
143 CPPUNIT_ASSERT(strm
!= nullptr);
144 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
145 buf
= static_cast<unsigned char const *>(strm
->GetData());
146 CPPUNIT_ASSERT_EQUAL(0x2C, int(buf
[0]));
147 CPPUNIT_ASSERT_EQUAL(0xC3, int(buf
[1]));
148 CPPUNIT_ASSERT_EQUAL(0xA4, int(buf
[2]));
149 CPPUNIT_ASSERT_EQUAL(0x90, int(buf
[3]));
152 url
= INetURLObject(u
"data:base64,");
153 //TODO: CPPUNIT_ASSERT(url.HasError());
154 strm
= url
.getData();
155 CPPUNIT_ASSERT(!strm
);
157 url
= INetURLObject(u
"data:;base64,");
158 CPPUNIT_ASSERT(!url
.HasError());
159 strm
= url
.getData();
160 CPPUNIT_ASSERT(strm
!= nullptr);
161 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
164 url
= INetURLObject(u
"data:;bAsE64,");
165 CPPUNIT_ASSERT(!url
.HasError());
166 strm
= url
.getData();
167 CPPUNIT_ASSERT(strm
!= nullptr);
168 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
171 url
= INetURLObject(u
"data:;base64,YWJjCg==");
172 CPPUNIT_ASSERT(!url
.HasError());
173 strm
= url
.getData();
174 CPPUNIT_ASSERT(strm
!= nullptr);
175 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
176 buf
= static_cast<unsigned char const *>(strm
->GetData());
177 CPPUNIT_ASSERT_EQUAL(0x61, int(buf
[0]));
178 CPPUNIT_ASSERT_EQUAL(0x62, int(buf
[1]));
179 CPPUNIT_ASSERT_EQUAL(0x63, int(buf
[2]));
180 CPPUNIT_ASSERT_EQUAL(0x0A, int(buf
[3]));
183 url
= INetURLObject(u
"data:;base64,YWJjCg=");
184 CPPUNIT_ASSERT(!url
.HasError());
185 strm
= url
.getData();
186 CPPUNIT_ASSERT(!strm
);
188 url
= INetURLObject(u
"data:;base64,YWJ$Cg==");
189 CPPUNIT_ASSERT(!url
.HasError());
190 strm
= url
.getData();
191 CPPUNIT_ASSERT(!strm
);
193 url
= INetURLObject(u
"data:text/plain;param=%22;base64,%22,YQ==");
194 CPPUNIT_ASSERT(!url
.HasError());
195 strm
= url
.getData();
196 CPPUNIT_ASSERT(strm
!= nullptr);
197 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
198 buf
= static_cast<unsigned char const *>(strm
->GetData());
199 CPPUNIT_ASSERT_EQUAL(0x59, int(buf
[0]));
200 CPPUNIT_ASSERT_EQUAL(0x51, int(buf
[1]));
201 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf
[2]));
202 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf
[3]));
205 url
= INetURLObject(u
"http://example.com");
206 CPPUNIT_ASSERT(!url
.HasError());
207 strm
= url
.getData();
208 CPPUNIT_ASSERT(!strm
);
211 void urlobjTest_isSchemeEqualTo() {
212 CPPUNIT_ASSERT(INetURLObject().isSchemeEqualTo(INetProtocol::NotValid
));
213 CPPUNIT_ASSERT(!INetURLObject().isSchemeEqualTo(u
""));
215 INetURLObject(u
"http://example.org").isSchemeEqualTo(
216 INetProtocol::Http
));
218 !INetURLObject(u
"http://example.org").isSchemeEqualTo(
219 INetProtocol::Https
));
221 INetURLObject(u
"http://example.org").isSchemeEqualTo(u
"Http"));
223 !INetURLObject(u
"http://example.org").isSchemeEqualTo(u
"dav"));
225 INetURLObject(u
"dav://example.org").isSchemeEqualTo(u
"dav"));
228 void urlobjTest_isAnyKnownWebDAVScheme() {
230 INetURLObject(u
"http://example.org").isAnyKnownWebDAVScheme());
232 INetURLObject(u
"https://example.org").isAnyKnownWebDAVScheme());
234 INetURLObject(u
"vnd.sun.star.webdav://example.org").isAnyKnownWebDAVScheme());
236 INetURLObject(u
"vnd.sun.star.webdavs://example.org").isAnyKnownWebDAVScheme());
238 !INetURLObject(u
"ftp://example.org").isAnyKnownWebDAVScheme());
240 !INetURLObject(u
"file://example.org").isAnyKnownWebDAVScheme());
242 !INetURLObject(u
"dav://example.org").isAnyKnownWebDAVScheme());
244 !INetURLObject(u
"davs://example.org").isAnyKnownWebDAVScheme());
246 !INetURLObject(u
"vnd.sun.star.pkg://example.org").isAnyKnownWebDAVScheme());
251 INetURLObject
obj(u
"file:///");
252 bool ok
= obj
.setName(u
"foo");
256 INetURLObject
obj(u
"file:///foo");
257 bool ok
= obj
.setName(u
"bar");
259 CPPUNIT_ASSERT_EQUAL(
260 OUString("file:///bar"), obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
263 INetURLObject
obj(u
"file:///foo/");
264 bool ok
= obj
.setName(u
"bar");
266 CPPUNIT_ASSERT_EQUAL(
267 OUString("file:///bar/"), obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
270 INetURLObject
obj(u
"file:///foo/bar");
271 bool ok
= obj
.setName(u
"baz");
273 CPPUNIT_ASSERT_EQUAL(
274 OUString("file:///foo/baz"),
275 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
278 INetURLObject
obj(u
"file:///foo/bar/");
279 bool ok
= obj
.setName(u
"baz");
281 CPPUNIT_ASSERT_EQUAL(
282 OUString("file:///foo/baz/"),
283 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
287 void testSetExtension() {
288 INetURLObject
obj(u
"file:///foo/bar.baz/");
289 bool ok
= obj
.setExtension(
290 u
"other", INetURLObject::LAST_SEGMENT
, false);
292 CPPUNIT_ASSERT_EQUAL(
293 OUString("file:///foo/bar.baz/.other"),
294 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
297 void testChangeScheme() {
298 INetURLObject
obj(u
"unknown://example.com/foo/bar");
299 CPPUNIT_ASSERT(!obj
.HasError());
300 obj
.changeScheme(INetProtocol::Http
);
301 CPPUNIT_ASSERT_EQUAL(
302 OUString("http://example.com/foo/bar"),
303 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
304 obj
.changeScheme(INetProtocol::Https
);
305 CPPUNIT_ASSERT_EQUAL(
306 OUString("https://example.com/foo/bar"),
307 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
308 obj
.changeScheme(INetProtocol::Ftp
);
309 CPPUNIT_ASSERT_EQUAL(
310 OUString("ftp://example.com/foo/bar"),
311 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
314 void testTd146382() {
315 INetURLObject
obj(u
"file://share.allotropia.de@SSL/DavWWWRoot/remote.php");
316 CPPUNIT_ASSERT(!obj
.HasError());
317 CPPUNIT_ASSERT_EQUAL(
318 OUString("file://share.allotropia.de@SSL/DavWWWRoot/remote.php"),
319 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
322 void testParseSmart()
325 // host:port must not be misinterpreted as scheme:path
326 INetURLObject
obj(u
"example.com:8080/foo", INetProtocol::Http
);
327 CPPUNIT_ASSERT(!obj
.HasError());
328 CPPUNIT_ASSERT_EQUAL(OUString("http://example.com:8080/foo"),
329 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
330 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http
, obj
.GetProtocol());
331 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), obj
.GetHost());
332 CPPUNIT_ASSERT_EQUAL(sal_uInt32(8080), obj
.GetPort());
333 CPPUNIT_ASSERT_EQUAL(OUString("/foo"), obj
.GetURLPath());
336 // port may only contain decimal digits, so this must be treated as unknown scheme
337 INetURLObject
obj(u
"example.com:80a0/foo", INetProtocol::Http
);
338 CPPUNIT_ASSERT(!obj
.HasError());
339 CPPUNIT_ASSERT_EQUAL(OUString("example.com:80a0/foo"),
340 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
341 CPPUNIT_ASSERT_EQUAL(INetProtocol::Generic
, obj
.GetProtocol());
342 CPPUNIT_ASSERT(obj
.isSchemeEqualTo(u
"example.com"));
343 CPPUNIT_ASSERT_EQUAL(OUString(""), obj
.GetHost());
344 CPPUNIT_ASSERT_EQUAL(OUString("80a0/foo"), obj
.GetURLPath());
348 // Change the following lines only, if you add, remove or rename
349 // member functions of the current class,
350 // because these macros are need by auto register mechanism.
352 CPPUNIT_TEST_SUITE( urlobjTest
);
353 CPPUNIT_TEST( urlobjTest_001
);
354 CPPUNIT_TEST( urlobjTest_004
);
355 CPPUNIT_TEST( urlobjCmisTest
);
356 CPPUNIT_TEST( urlobjTest_emptyPath
);
357 CPPUNIT_TEST( urlobjTest_data
);
358 CPPUNIT_TEST( urlobjTest_isSchemeEqualTo
);
359 CPPUNIT_TEST( urlobjTest_isAnyKnownWebDAVScheme
);
360 CPPUNIT_TEST( testSetName
);
361 CPPUNIT_TEST( testSetExtension
);
362 CPPUNIT_TEST( testChangeScheme
);
363 CPPUNIT_TEST( testTd146382
);
364 CPPUNIT_TEST( testParseSmart
);
365 CPPUNIT_TEST_SUITE_END( );
366 }; // class createPool
369 CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest
);
370 } // namespace rtl_random
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */