lwp build fixes
[LibreOffice.git] / tools / qa / cppunit / test_urlobj.cxx
blobdf866bc85e2cde486cd50180a67a11dacb3726f4
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 equalsAscii
63 ( "smb://10.10.1.1/sampledir/sample.file" ) );
64 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
65 #endif
66 #ifdef WIN
67 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
68 equalsAscii
69 ( "file://10.10.1.1/sampledir/sample.file" ) );
70 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
71 #endif
72 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
73 equalsAscii( "10.10.1.1" ) );
74 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
75 equalsAscii( "/sampledir/sample.file" ) );
76 CPPUNIT_ASSERT( aUrl.getName( ).
77 equalsAscii( "sample.file" ) );
78 CPPUNIT_ASSERT( aUrl.getBase( ).equalsAscii( "sample" ) );
79 CPPUNIT_ASSERT( aUrl.getExtension( ).equalsAscii( "file" ) );
82 void urlobjTest_002( )
84 INetURLObject aUrl;
85 aUrl.
86 setFSysPath( OUString( "\\\\137.65.170.24\\c$\\Img0001.jpg" ),
87 INetURLObject::FSYS_DETECT );
88 #ifdef LINUX
89 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
90 equalsAscii( "smb://137.65.170.24/c$/Img0001.jpg" ) );
91 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
92 #endif
93 #ifdef WIN
94 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
95 equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
96 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
97 #endif
98 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
99 equalsAscii( "137.65.170.24" ) );
100 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
101 equalsAscii( "/c$/Img0001.jpg" ) );
102 CPPUNIT_ASSERT( aUrl.getName( ).
103 equalsAscii( "Img0001.jpg" ) );
104 CPPUNIT_ASSERT( aUrl.getBase( ).equalsAscii( "Img0001" ) );
105 CPPUNIT_ASSERT( aUrl.getExtension( ).equalsAscii( "jpg" ) );
109 void urlobjTest_003( )
111 INetURLObject aUrl;
112 aUrl.
113 setFSysPath( OUString( "\\\\hive-winxp-x86\\pmladek\\test2.odt" ),
114 INetURLObject::FSYS_DETECT );
115 #ifdef LINUX
116 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
117 equalsAscii( "smb://hive-winxp-x86/pmladek/test2.odt" ) );
118 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
119 #endif
120 #ifdef WIN
121 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
122 equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
123 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
124 #endif
125 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
126 equalsAscii( "hive-winxp-x86" ) );
127 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
128 equalsAscii( "/pmladek/test2.odt" ) );
131 void urlobjTest_004( )
133 INetURLObject aUrl( OUString( "smb://10.10.1.1/sampledir/sample.file" ) );
134 #ifdef LINUX
135 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
136 equalsAscii( "smb://10.10.1.1/sampledir/sample.file" ) );
137 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
138 #endif
139 #ifdef WIN
140 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
141 equalsAscii( "file://10.10.1.1/sampledir/sample.file" ) );
142 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
143 #endif
144 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
145 equalsAscii( "10.10.1.1" ) );
146 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
147 equalsAscii( "/sampledir/sample.file" ) );
148 CPPUNIT_ASSERT( aUrl.getName( ).
149 equalsAscii( "sample.file" ) );
150 CPPUNIT_ASSERT( aUrl.getBase( ).equalsAscii( "sample" ) );
151 CPPUNIT_ASSERT( aUrl.getExtension( ).equalsAscii( "file" ) );
154 void urlobjTest_005( )
156 INetURLObject aUrl;
157 aUrl.setFSysPath( OUString( "//137.65.170.24/c$/Img0001.jpg" ),
158 INetURLObject::FSYS_DETECT );
159 #ifdef LINUX
160 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
161 equalsAscii( "smb://137.65.170.24/c$/Img0001.jpg" ) );
162 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
163 #endif
164 #ifdef WIN
165 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
166 equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
167 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
168 #endif
169 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
170 equalsAscii( "137.65.170.24" ) );
171 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
172 equalsAscii( "/c$/Img0001.jpg" ) );
173 CPPUNIT_ASSERT( aUrl.getName( ).equalsAscii( "Img0001.jpg" ) );
174 CPPUNIT_ASSERT( aUrl.getBase( ).equalsAscii( "Img0001" ) );
175 CPPUNIT_ASSERT( aUrl.getExtension( ).equalsAscii( "jpg" ) );
179 void urlobjTest_006( )
181 INetURLObject aUrl;
182 aUrl.setFSysPath( OUString( "//hive-winxp-x86/pmladek/test2.odt" ),
183 INetURLObject::FSYS_DETECT );
184 #ifdef LINUX
185 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
186 equalsAscii( "smb://hive-winxp-x86/pmladek/test2.odt" ) );
187 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_SMB );
188 #endif
189 #ifdef WIN
190 CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
191 equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
192 CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INET_PROT_FILE );
193 #endif
194 CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
195 equalsAscii( "hive-winxp-x86" ) );
196 CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
197 equalsAscii( "/pmladek/test2.odt" ) );
200 void urlobjCmisTest( )
202 // Test with a username part
204 INetURLObject aUrl( OUString( "vnd.libreoffice.cmis://username@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_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
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", INET_PROT_CMIS, aUrl.GetProtocol( ) );
213 // Test without a username part
215 INetURLObject aUrl( OUString(
216 "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
217 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
218 OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
219 CPPUNIT_ASSERT( !aUrl.HasUserData() );
220 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
221 OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
222 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INET_PROT_CMIS, aUrl.GetProtocol( ) );
226 void urlobjTest_emptyPath() {
228 INetURLObject url(OUString("http://example.com"));
229 CPPUNIT_ASSERT_EQUAL(INET_PROT_HTTP, url.GetProtocol());
230 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
231 CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
234 // This is an invalid http URL per RFC 2616:
235 INetURLObject url(OUString("http://example.com?query"));
236 CPPUNIT_ASSERT(url.HasError());
239 INetURLObject url(OUString("http://example.com#fragment"));
240 CPPUNIT_ASSERT_EQUAL(INET_PROT_HTTP, url.GetProtocol());
241 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
242 CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
243 CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url.GetMark());
247 void urlobjTest_data() {
248 INetURLObject url;
249 std::unique_ptr<SvMemoryStream> strm;
250 unsigned char const * buf;
252 url = INetURLObject("data:");
253 //TODO: CPPUNIT_ASSERT(url.HasError());
254 strm = url.getData();
255 CPPUNIT_ASSERT(strm == 0);
257 url = INetURLObject("data:,");
258 CPPUNIT_ASSERT(!url.HasError());
259 strm = url.getData();
260 CPPUNIT_ASSERT(strm != 0);
261 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
262 strm.reset();
264 url = INetURLObject("data:,,%C3%A4%90");
265 CPPUNIT_ASSERT(!url.HasError());
266 strm = url.getData();
267 CPPUNIT_ASSERT(strm != 0);
268 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
269 buf = static_cast<unsigned char const *>(strm->GetData());
270 CPPUNIT_ASSERT_EQUAL(0x2C, int(buf[0]));
271 CPPUNIT_ASSERT_EQUAL(0xC3, int(buf[1]));
272 CPPUNIT_ASSERT_EQUAL(0xA4, int(buf[2]));
273 CPPUNIT_ASSERT_EQUAL(0x90, int(buf[3]));
274 strm.reset();
276 url = INetURLObject("data:base64,");
277 //TODO: CPPUNIT_ASSERT(url.HasError());
278 strm = url.getData();
279 CPPUNIT_ASSERT(strm == 0);
281 url = INetURLObject("data:;base64,");
282 CPPUNIT_ASSERT(!url.HasError());
283 strm = url.getData();
284 CPPUNIT_ASSERT(strm != 0);
285 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
286 strm.reset();
288 url = INetURLObject("data:;bAsE64,");
289 CPPUNIT_ASSERT(!url.HasError());
290 strm = url.getData();
291 CPPUNIT_ASSERT(strm != 0);
292 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
293 strm.reset();
295 url = INetURLObject("data:;base64,YWJjCg==");
296 CPPUNIT_ASSERT(!url.HasError());
297 strm = url.getData();
298 CPPUNIT_ASSERT(strm != 0);
299 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
300 buf = static_cast<unsigned char const *>(strm->GetData());
301 CPPUNIT_ASSERT_EQUAL(0x61, int(buf[0]));
302 CPPUNIT_ASSERT_EQUAL(0x62, int(buf[1]));
303 CPPUNIT_ASSERT_EQUAL(0x63, int(buf[2]));
304 CPPUNIT_ASSERT_EQUAL(0x0A, int(buf[3]));
305 strm.reset();
307 url = INetURLObject("data:;base64,YWJjCg=");
308 CPPUNIT_ASSERT(!url.HasError());
309 strm = url.getData();
310 CPPUNIT_ASSERT(strm == 0);
312 url = INetURLObject("data:;base64,YWJ$Cg==");
313 CPPUNIT_ASSERT(!url.HasError());
314 strm = url.getData();
315 CPPUNIT_ASSERT(strm == 0);
317 url = INetURLObject("data:text/plain;param=%22;base64,%22,YQ==");
318 CPPUNIT_ASSERT(!url.HasError());
319 strm = url.getData();
320 CPPUNIT_ASSERT(strm != 0);
321 CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
322 buf = static_cast<unsigned char const *>(strm->GetData());
323 CPPUNIT_ASSERT_EQUAL(0x59, int(buf[0]));
324 CPPUNIT_ASSERT_EQUAL(0x51, int(buf[1]));
325 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[2]));
326 CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[3]));
327 strm.reset();
329 url = INetURLObject("http://example.com");
330 CPPUNIT_ASSERT(!url.HasError());
331 strm = url.getData();
332 CPPUNIT_ASSERT(strm == 0);
335 // Change the following lines only, if you add, remove or rename
336 // member functions of the current class,
337 // because these macros are need by auto register mechanism.
339 CPPUNIT_TEST_SUITE( urlobjTest );
340 CPPUNIT_TEST( urlobjTest_001 );
341 CPPUNIT_TEST( urlobjTest_002 );
342 CPPUNIT_TEST( urlobjTest_003 );
343 CPPUNIT_TEST( urlobjTest_004 );
344 CPPUNIT_TEST( urlobjTest_005 );
345 CPPUNIT_TEST( urlobjTest_006 );
346 CPPUNIT_TEST( urlobjCmisTest );
347 CPPUNIT_TEST( urlobjTest_emptyPath );
348 CPPUNIT_TEST( urlobjTest_data );
349 CPPUNIT_TEST_SUITE_END( );
350 }; // class createPool
353 CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
354 } // namespace rtl_random
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */