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( OUString( "file://10.10.1.1/sampledir/sample.file" ) );
53 CPPUNIT_ASSERT_EQUAL(OUString("smb://10.10.1.1/sampledir/sample.file"),
54 aUrl
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
55 CPPUNIT_ASSERT_EQUAL(INetProtocol::Smb
, aUrl
.GetProtocol());
57 CPPUNIT_ASSERT_EQUAL(OUString("10.10.1.1"),
58 aUrl
.GetHost(INetURLObject::DecodeMechanism::NONE
));
59 CPPUNIT_ASSERT_EQUAL(OUString("/sampledir/sample.file"),
60 aUrl
.GetURLPath(INetURLObject::DecodeMechanism::NONE
));
61 CPPUNIT_ASSERT_EQUAL(OUString("sample.file"),
63 CPPUNIT_ASSERT_EQUAL(OUString("sample"), aUrl
.getBase());
64 CPPUNIT_ASSERT_EQUAL(OUString("file"), aUrl
.getExtension());
67 void urlobjTest_004( )
69 INetURLObject
aUrl( OUString( "smb://10.10.1.1/sampledir/sample.file" ) );
71 CPPUNIT_ASSERT_EQUAL(OUString("smb://10.10.1.1/sampledir/sample.file"),
72 aUrl
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
73 CPPUNIT_ASSERT_EQUAL( INetProtocol::Smb
, aUrl
.GetProtocol( ) );
75 CPPUNIT_ASSERT_EQUAL(OUString("10.10.1.1"),
76 aUrl
.GetHost(INetURLObject::DecodeMechanism::NONE
));
77 CPPUNIT_ASSERT_EQUAL(OUString("/sampledir/sample.file"),
78 aUrl
.GetURLPath(INetURLObject::DecodeMechanism::NONE
));
79 CPPUNIT_ASSERT_EQUAL(OUString("sample.file"), aUrl
.getName());
80 CPPUNIT_ASSERT_EQUAL(OUString("sample"), aUrl
.getBase());
81 CPPUNIT_ASSERT_EQUAL(OUString("file"), aUrl
.getExtension());
84 void urlobjCmisTest( )
86 // Test with a username part
88 INetURLObject
aUrl( OUString( "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
89 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
90 OUSTR_TO_STDSTR( aUrl
.GetHost( INetURLObject::DecodeMechanism::WithCharset
) ) );
91 CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl
.GetUser( ) ) );
92 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
93 OUSTR_TO_STDSTR( aUrl
.GetURLPath( INetURLObject::DecodeMechanism::NONE
) ) );
94 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis
, aUrl
.GetProtocol( ) );
97 // Test without a username part
99 INetURLObject
aUrl( OUString(
100 "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
101 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
102 OUSTR_TO_STDSTR( aUrl
.GetHost( INetURLObject::DecodeMechanism::WithCharset
) ) );
103 CPPUNIT_ASSERT( !aUrl
.HasUserData() );
104 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
105 OUSTR_TO_STDSTR( aUrl
.GetURLPath( INetURLObject::DecodeMechanism::NONE
) ) );
106 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis
, aUrl
.GetProtocol( ) );
110 void urlobjTest_emptyPath() {
112 INetURLObject
url(OUString("http://example.com"));
113 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http
, url
.GetProtocol());
114 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url
.GetHost());
115 CPPUNIT_ASSERT_EQUAL(OUString("/"), url
.GetURLPath());
118 // This is an invalid http URL per RFC 2616:
119 INetURLObject
url(OUString("http://example.com?query"));
120 CPPUNIT_ASSERT(url
.HasError());
123 INetURLObject
url(OUString("http://example.com#fragment"));
124 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http
, url
.GetProtocol());
125 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url
.GetHost());
126 CPPUNIT_ASSERT_EQUAL(OUString("/"), url
.GetURLPath());
127 CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url
.GetMark());
131 void urlobjTest_data() {
133 std::unique_ptr
<SvMemoryStream
> strm
;
134 unsigned char const * buf
;
136 url
= INetURLObject("data:");
137 //TODO: CPPUNIT_ASSERT(url.HasError());
138 strm
= url
.getData();
139 CPPUNIT_ASSERT(!strm
);
141 url
= INetURLObject("data:,");
142 CPPUNIT_ASSERT(!url
.HasError());
143 strm
= url
.getData();
144 CPPUNIT_ASSERT(strm
!= nullptr);
145 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
148 url
= INetURLObject("data:,,%C3%A4%90");
149 CPPUNIT_ASSERT(!url
.HasError());
150 strm
= url
.getData();
151 CPPUNIT_ASSERT(strm
!= nullptr);
152 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
153 buf
= static_cast<unsigned char const *>(strm
->GetData());
154 CPPUNIT_ASSERT_EQUAL(0x2C, int(buf
[0]));
155 CPPUNIT_ASSERT_EQUAL(0xC3, int(buf
[1]));
156 CPPUNIT_ASSERT_EQUAL(0xA4, int(buf
[2]));
157 CPPUNIT_ASSERT_EQUAL(0x90, int(buf
[3]));
160 url
= INetURLObject("data:base64,");
161 //TODO: CPPUNIT_ASSERT(url.HasError());
162 strm
= url
.getData();
163 CPPUNIT_ASSERT(!strm
);
165 url
= INetURLObject("data:;base64,");
166 CPPUNIT_ASSERT(!url
.HasError());
167 strm
= url
.getData();
168 CPPUNIT_ASSERT(strm
!= nullptr);
169 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
172 url
= INetURLObject("data:;bAsE64,");
173 CPPUNIT_ASSERT(!url
.HasError());
174 strm
= url
.getData();
175 CPPUNIT_ASSERT(strm
!= nullptr);
176 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm
->GetSize());
179 url
= INetURLObject("data:;base64,YWJjCg==");
180 CPPUNIT_ASSERT(!url
.HasError());
181 strm
= url
.getData();
182 CPPUNIT_ASSERT(strm
!= nullptr);
183 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
184 buf
= static_cast<unsigned char const *>(strm
->GetData());
185 CPPUNIT_ASSERT_EQUAL(0x61, int(buf
[0]));
186 CPPUNIT_ASSERT_EQUAL(0x62, int(buf
[1]));
187 CPPUNIT_ASSERT_EQUAL(0x63, int(buf
[2]));
188 CPPUNIT_ASSERT_EQUAL(0x0A, int(buf
[3]));
191 url
= INetURLObject("data:;base64,YWJjCg=");
192 CPPUNIT_ASSERT(!url
.HasError());
193 strm
= url
.getData();
194 CPPUNIT_ASSERT(!strm
);
196 url
= INetURLObject("data:;base64,YWJ$Cg==");
197 CPPUNIT_ASSERT(!url
.HasError());
198 strm
= url
.getData();
199 CPPUNIT_ASSERT(!strm
);
201 url
= INetURLObject("data:text/plain;param=%22;base64,%22,YQ==");
202 CPPUNIT_ASSERT(!url
.HasError());
203 strm
= url
.getData();
204 CPPUNIT_ASSERT(strm
!= nullptr);
205 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm
->GetSize());
206 buf
= static_cast<unsigned char const *>(strm
->GetData());
207 CPPUNIT_ASSERT_EQUAL(0x59, int(buf
[0]));
208 CPPUNIT_ASSERT_EQUAL(0x51, int(buf
[1]));
209 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf
[2]));
210 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf
[3]));
213 url
= INetURLObject("http://example.com");
214 CPPUNIT_ASSERT(!url
.HasError());
215 strm
= url
.getData();
216 CPPUNIT_ASSERT(!strm
);
219 void urlobjTest_isSchemeEqualTo() {
220 CPPUNIT_ASSERT(INetURLObject().isSchemeEqualTo(INetProtocol::NotValid
));
221 CPPUNIT_ASSERT(!INetURLObject().isSchemeEqualTo(u
""));
223 INetURLObject("http://example.org").isSchemeEqualTo(
224 INetProtocol::Http
));
226 !INetURLObject("http://example.org").isSchemeEqualTo(
227 INetProtocol::Https
));
229 INetURLObject("http://example.org").isSchemeEqualTo(u
"Http"));
231 !INetURLObject("http://example.org").isSchemeEqualTo(u
"dav"));
233 INetURLObject("dav://example.org").isSchemeEqualTo(u
"dav"));
236 void urlobjTest_isAnyKnownWebDAVScheme() {
238 INetURLObject("http://example.org").isAnyKnownWebDAVScheme());
240 INetURLObject("https://example.org").isAnyKnownWebDAVScheme());
242 INetURLObject("vnd.sun.star.webdav://example.org").isAnyKnownWebDAVScheme());
244 INetURLObject("vnd.sun.star.webdavs://example.org").isAnyKnownWebDAVScheme());
246 !INetURLObject("ftp://example.org").isAnyKnownWebDAVScheme());
248 !INetURLObject("file://example.org").isAnyKnownWebDAVScheme());
250 !INetURLObject("dav://example.org").isAnyKnownWebDAVScheme());
252 !INetURLObject("davs://example.org").isAnyKnownWebDAVScheme());
254 !INetURLObject("vnd.sun.star.pkg://example.org").isAnyKnownWebDAVScheme());
259 INetURLObject
obj("file:///");
260 bool ok
= obj
.setName("foo");
264 INetURLObject
obj("file:///foo");
265 bool ok
= obj
.setName("bar");
267 CPPUNIT_ASSERT_EQUAL(
268 OUString("file:///bar"), obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
271 INetURLObject
obj("file:///foo/");
272 bool ok
= obj
.setName("bar");
274 CPPUNIT_ASSERT_EQUAL(
275 OUString("file:///bar/"), obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
278 INetURLObject
obj("file:///foo/bar");
279 bool ok
= obj
.setName("baz");
281 CPPUNIT_ASSERT_EQUAL(
282 OUString("file:///foo/baz"),
283 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
286 INetURLObject
obj("file:///foo/bar/");
287 bool ok
= obj
.setName("baz");
289 CPPUNIT_ASSERT_EQUAL(
290 OUString("file:///foo/baz/"),
291 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
295 void testSetExtension() {
296 INetURLObject
obj("file:///foo/bar.baz/");
297 bool ok
= obj
.setExtension(
298 "other", INetURLObject::LAST_SEGMENT
, false);
300 CPPUNIT_ASSERT_EQUAL(
301 OUString("file:///foo/bar.baz/.other"),
302 obj
.GetMainURL(INetURLObject::DecodeMechanism::NONE
));
305 // Change the following lines only, if you add, remove or rename
306 // member functions of the current class,
307 // because these macros are need by auto register mechanism.
309 CPPUNIT_TEST_SUITE( urlobjTest
);
310 CPPUNIT_TEST( urlobjTest_001
);
311 CPPUNIT_TEST( urlobjTest_004
);
312 CPPUNIT_TEST( urlobjCmisTest
);
313 CPPUNIT_TEST( urlobjTest_emptyPath
);
314 CPPUNIT_TEST( urlobjTest_data
);
315 CPPUNIT_TEST( urlobjTest_isSchemeEqualTo
);
316 CPPUNIT_TEST( urlobjTest_isAnyKnownWebDAVScheme
);
317 CPPUNIT_TEST( testSetName
);
318 CPPUNIT_TEST( testSetExtension
);
319 CPPUNIT_TEST_SUITE_END( );
320 }; // class createPool
323 CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest
);
324 } // namespace rtl_random
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */