Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / tools / qa / cppunit / test_urlobj.cxx
blob79f20b117ad8cde8ab06c1a16f98a55e875d39af
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 // insert your test code here.
47 // this is only demonstration code
48 void urlobjTest_001( )
50 INetURLObject aUrl( OUString( "file://10.10.1.1/sampledir/sample.file" ) );
51 #ifdef LINUX
52 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
53 == "smb://10.10.1.1/sampledir/sample.file" );
54 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
55 #endif
56 #ifdef _WIN32
57 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
58 equalsAscii
59 ( "file://10.10.1.1/sampledir/sample.file" ) );
60 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
61 #endif
62 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
63 == "10.10.1.1" );
64 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
65 == "/sampledir/sample.file" );
66 CPPUNIT_ASSERT( aUrl.getName( )
67 == "sample.file" );
68 CPPUNIT_ASSERT( aUrl.getBase( ) == "sample" );
69 CPPUNIT_ASSERT( aUrl.getExtension( ) == "file" );
72 void urlobjTest_002( )
74 INetURLObject aUrl;
75 aUrl.
76 setFSysPath( "\\\\137.65.170.24\\c$\\Img0001.jpg",
77 INetURLObject::FSYS_DETECT );
78 #ifdef LINUX
79 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
80 == "smb://137.65.170.24/c$/Img0001.jpg" );
81 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
82 #endif
83 #ifdef _WIN32
84 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
85 equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
86 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
87 #endif
88 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
89 == "137.65.170.24" );
90 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
91 == "/c$/Img0001.jpg" );
92 CPPUNIT_ASSERT( aUrl.getName( ) == "Img0001.jpg" );
93 CPPUNIT_ASSERT( aUrl.getBase( ) == "Img0001" );
94 CPPUNIT_ASSERT( aUrl.getExtension( ) == "jpg" );
98 void urlobjTest_003( )
100 INetURLObject aUrl;
101 aUrl.
102 setFSysPath( "\\\\hive-winxp-x86\\pmladek\\test2.odt",
103 INetURLObject::FSYS_DETECT );
104 #ifdef LINUX
105 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
106 == "smb://hive-winxp-x86/pmladek/test2.odt" );
107 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
108 #endif
109 #ifdef _WIN32
110 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
111 equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
112 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
113 #endif
114 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
115 == "hive-winxp-x86" );
116 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
117 == "/pmladek/test2.odt" );
120 void urlobjTest_004( )
122 INetURLObject aUrl( OUString( "smb://10.10.1.1/sampledir/sample.file" ) );
123 #ifdef LINUX
124 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
125 == "smb://10.10.1.1/sampledir/sample.file" );
126 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
127 #endif
128 #ifdef _WIN32
129 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
130 equalsAscii( "file://10.10.1.1/sampledir/sample.file" ) );
131 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
132 #endif
133 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
134 == "10.10.1.1" );
135 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
136 == "/sampledir/sample.file" );
137 CPPUNIT_ASSERT( aUrl.getName( ) == "sample.file" );
138 CPPUNIT_ASSERT( aUrl.getBase( ) == "sample" );
139 CPPUNIT_ASSERT( aUrl.getExtension( ) == "file" );
142 void urlobjTest_005( )
144 INetURLObject aUrl;
145 aUrl.setFSysPath( "//137.65.170.24/c$/Img0001.jpg",
146 INetURLObject::FSYS_DETECT );
147 #ifdef LINUX
148 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
149 == "smb://137.65.170.24/c$/Img0001.jpg" );
150 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
151 #endif
152 #ifdef _WIN32
153 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
154 equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
155 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
156 #endif
157 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
158 == "137.65.170.24" );
159 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
160 == "/c$/Img0001.jpg" );
161 CPPUNIT_ASSERT( aUrl.getName( ) == "Img0001.jpg" );
162 CPPUNIT_ASSERT( aUrl.getBase( ) == "Img0001" );
163 CPPUNIT_ASSERT( aUrl.getExtension( ) == "jpg" );
167 void urlobjTest_006( )
169 INetURLObject aUrl;
170 aUrl.setFSysPath( "//hive-winxp-x86/pmladek/test2.odt",
171 INetURLObject::FSYS_DETECT );
172 #ifdef LINUX
173 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
174 == "smb://hive-winxp-x86/pmladek/test2.odt" );
175 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
176 #endif
177 #ifdef _WIN32
178 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
179 equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
180 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
181 #endif
182 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
183 == "hive-winxp-x86" );
184 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
185 == "/pmladek/test2.odt" );
188 void urlobjCmisTest( )
190 // Test with a username part
192 INetURLObject aUrl( OUString( "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
193 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
194 OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
195 CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
196 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
197 OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
198 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis, aUrl.GetProtocol( ) );
201 // Test without a username part
203 INetURLObject aUrl( OUString(
204 "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
205 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
206 OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
207 CPPUNIT_ASSERT( !aUrl.HasUserData() );
208 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
209 OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
210 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis, aUrl.GetProtocol( ) );
214 void urlobjTest_emptyPath() {
216 INetURLObject url(OUString("http://example.com"));
217 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, url.GetProtocol());
218 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
219 CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
222 // This is an invalid http URL per RFC 2616:
223 INetURLObject url(OUString("http://example.com?query"));
224 CPPUNIT_ASSERT(url.HasError());
227 INetURLObject url(OUString("http://example.com#fragment"));
228 CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, url.GetProtocol());
229 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
230 CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
231 CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url.GetMark());
235 void urlobjTest_data() {
236 INetURLObject url;
237 std::unique_ptr<SvMemoryStream> strm;
238 unsigned char const * buf;
240 url = INetURLObject("data:");
241 //TODO: CPPUNIT_ASSERT(url.HasError());
242 strm = url.getData();
243 CPPUNIT_ASSERT(strm == nullptr);
245 url = INetURLObject("data:,");
246 CPPUNIT_ASSERT(!url.HasError());
247 strm = url.getData();
248 CPPUNIT_ASSERT(strm != nullptr);
249 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm->GetSize());
250 strm.reset();
252 url = INetURLObject("data:,,%C3%A4%90");
253 CPPUNIT_ASSERT(!url.HasError());
254 strm = url.getData();
255 CPPUNIT_ASSERT(strm != nullptr);
256 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm->GetSize());
257 buf = static_cast<unsigned char const *>(strm->GetData());
258 CPPUNIT_ASSERT_EQUAL(0x2C, int(buf[0]));
259 CPPUNIT_ASSERT_EQUAL(0xC3, int(buf[1]));
260 CPPUNIT_ASSERT_EQUAL(0xA4, int(buf[2]));
261 CPPUNIT_ASSERT_EQUAL(0x90, int(buf[3]));
262 strm.reset();
264 url = INetURLObject("data:base64,");
265 //TODO: CPPUNIT_ASSERT(url.HasError());
266 strm = url.getData();
267 CPPUNIT_ASSERT(strm == nullptr);
269 url = INetURLObject("data:;base64,");
270 CPPUNIT_ASSERT(!url.HasError());
271 strm = url.getData();
272 CPPUNIT_ASSERT(strm != nullptr);
273 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm->GetSize());
274 strm.reset();
276 url = INetURLObject("data:;bAsE64,");
277 CPPUNIT_ASSERT(!url.HasError());
278 strm = url.getData();
279 CPPUNIT_ASSERT(strm != nullptr);
280 CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm->GetSize());
281 strm.reset();
283 url = INetURLObject("data:;base64,YWJjCg==");
284 CPPUNIT_ASSERT(!url.HasError());
285 strm = url.getData();
286 CPPUNIT_ASSERT(strm != nullptr);
287 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm->GetSize());
288 buf = static_cast<unsigned char const *>(strm->GetData());
289 CPPUNIT_ASSERT_EQUAL(0x61, int(buf[0]));
290 CPPUNIT_ASSERT_EQUAL(0x62, int(buf[1]));
291 CPPUNIT_ASSERT_EQUAL(0x63, int(buf[2]));
292 CPPUNIT_ASSERT_EQUAL(0x0A, int(buf[3]));
293 strm.reset();
295 url = INetURLObject("data:;base64,YWJjCg=");
296 CPPUNIT_ASSERT(!url.HasError());
297 strm = url.getData();
298 CPPUNIT_ASSERT(strm == nullptr);
300 url = INetURLObject("data:;base64,YWJ$Cg==");
301 CPPUNIT_ASSERT(!url.HasError());
302 strm = url.getData();
303 CPPUNIT_ASSERT(strm == nullptr);
305 url = INetURLObject("data:text/plain;param=%22;base64,%22,YQ==");
306 CPPUNIT_ASSERT(!url.HasError());
307 strm = url.getData();
308 CPPUNIT_ASSERT(strm != nullptr);
309 CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm->GetSize());
310 buf = static_cast<unsigned char const *>(strm->GetData());
311 CPPUNIT_ASSERT_EQUAL(0x59, int(buf[0]));
312 CPPUNIT_ASSERT_EQUAL(0x51, int(buf[1]));
313 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[2]));
314 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[3]));
315 strm.reset();
317 url = INetURLObject("http://example.com");
318 CPPUNIT_ASSERT(!url.HasError());
319 strm = url.getData();
320 CPPUNIT_ASSERT(strm == nullptr);
323 void urlobjTest_isSchemeEqualTo() {
324 CPPUNIT_ASSERT(INetURLObject().isSchemeEqualTo(INetProtocol::NotValid));
325 CPPUNIT_ASSERT(!INetURLObject().isSchemeEqualTo(""));
326 CPPUNIT_ASSERT(
327 INetURLObject("http://example.org").isSchemeEqualTo(
328 INetProtocol::Http));
329 CPPUNIT_ASSERT(
330 !INetURLObject("http://example.org").isSchemeEqualTo(
331 INetProtocol::Https));
332 CPPUNIT_ASSERT(
333 INetURLObject("http://example.org").isSchemeEqualTo("Http"));
334 CPPUNIT_ASSERT(
335 !INetURLObject("http://example.org").isSchemeEqualTo("dav"));
336 CPPUNIT_ASSERT(
337 INetURLObject("dav://example.org").isSchemeEqualTo("dav"));
340 void urlobjTest_isAnyKnownWebDAVScheme() {
341 CPPUNIT_ASSERT(
342 INetURLObject("http://example.org").isAnyKnownWebDAVScheme());
343 CPPUNIT_ASSERT(
344 INetURLObject("https://example.org").isAnyKnownWebDAVScheme());
345 CPPUNIT_ASSERT(
346 INetURLObject("vnd.sun.star.webdav://example.org").isAnyKnownWebDAVScheme());
347 CPPUNIT_ASSERT(
348 INetURLObject("vnd.sun.star.webdavs://example.org").isAnyKnownWebDAVScheme());
349 CPPUNIT_ASSERT(
350 !INetURLObject("ftp://example.org").isAnyKnownWebDAVScheme());
351 CPPUNIT_ASSERT(
352 !INetURLObject("file://example.org").isAnyKnownWebDAVScheme());
353 CPPUNIT_ASSERT(
354 !INetURLObject("dav://example.org").isAnyKnownWebDAVScheme());
355 CPPUNIT_ASSERT(
356 !INetURLObject("davs://example.org").isAnyKnownWebDAVScheme());
357 CPPUNIT_ASSERT(
358 !INetURLObject("vnd.sun.star.pkg://example.org").isAnyKnownWebDAVScheme());
361 // Change the following lines only, if you add, remove or rename
362 // member functions of the current class,
363 // because these macros are need by auto register mechanism.
365 CPPUNIT_TEST_SUITE( urlobjTest );
366 CPPUNIT_TEST( urlobjTest_001 );
367 CPPUNIT_TEST( urlobjTest_002 );
368 CPPUNIT_TEST( urlobjTest_003 );
369 CPPUNIT_TEST( urlobjTest_004 );
370 CPPUNIT_TEST( urlobjTest_005 );
371 CPPUNIT_TEST( urlobjTest_006 );
372 CPPUNIT_TEST( urlobjCmisTest );
373 CPPUNIT_TEST( urlobjTest_emptyPath );
374 CPPUNIT_TEST( urlobjTest_data );
375 CPPUNIT_TEST( urlobjTest_isSchemeEqualTo );
376 CPPUNIT_TEST( urlobjTest_isAnyKnownWebDAVScheme );
377 CPPUNIT_TEST_SUITE_END( );
378 }; // class createPool
381 CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
382 } // namespace rtl_random
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */