Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <string>
11 :
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>
17 :
18 : #define OUSTR_TO_STDSTR( oustr ) std::string( OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ).getStr() )
19 :
20 : CPPUNIT_NS_BEGIN
21 :
22 : template<> struct assertion_traits<INetProtocol>
23 : {
24 4 : static bool equal( const INetProtocol& x, const INetProtocol& y )
25 : {
26 4 : return x == y;
27 : }
28 :
29 0 : static std::string toString( const INetProtocol& x )
30 : {
31 0 : OStringStream ost;
32 0 : ost << static_cast<unsigned int>(x);
33 0 : return ost.str();
34 : }
35 : };
36 :
37 : CPPUNIT_NS_END
38 :
39 : namespace tools_urlobj
40 : {
41 :
42 27 : class urlobjTest:public CppUnit::TestFixture
43 : {
44 :
45 : public:
46 : // initialise your test code values here.
47 9 : void setUp( ) SAL_OVERRIDE
48 : {
49 9 : }
50 :
51 9 : void tearDown( ) SAL_OVERRIDE
52 : {
53 9 : }
54 :
55 : // insert your test code here.
56 : // this is only demonstration code
57 1 : void urlobjTest_001( )
58 : {
59 1 : INetURLObject aUrl( OUString( "file://10.10.1.1/sampledir/sample.file" ) );
60 : #ifdef LINUX
61 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
62 1 : == "smb://10.10.1.1/sampledir/sample.file" );
63 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
64 : #endif
65 : #ifdef WIN
66 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
67 : equalsAscii
68 : ( "file://10.10.1.1/sampledir/sample.file" ) );
69 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
70 : #endif
71 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
72 1 : == "10.10.1.1" );
73 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
74 1 : == "/sampledir/sample.file" );
75 2 : CPPUNIT_ASSERT( aUrl.getName( )
76 1 : == "sample.file" );
77 1 : CPPUNIT_ASSERT( aUrl.getBase( ) == "sample" );
78 1 : CPPUNIT_ASSERT( aUrl.getExtension( ) == "file" );
79 1 : }
80 :
81 1 : void urlobjTest_002( )
82 : {
83 1 : INetURLObject aUrl;
84 : aUrl.
85 : setFSysPath( OUString( "\\\\137.65.170.24\\c$\\Img0001.jpg" ),
86 1 : INetURLObject::FSYS_DETECT );
87 : #ifdef LINUX
88 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
89 1 : == "smb://137.65.170.24/c$/Img0001.jpg" );
90 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
91 : #endif
92 : #ifdef WIN
93 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
94 : equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
95 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
96 : #endif
97 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
98 1 : == "137.65.170.24" );
99 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
100 1 : == "/c$/Img0001.jpg" );
101 1 : CPPUNIT_ASSERT( aUrl.getName( ) == "Img0001.jpg" );
102 1 : CPPUNIT_ASSERT( aUrl.getBase( ) == "Img0001" );
103 1 : CPPUNIT_ASSERT( aUrl.getExtension( ) == "jpg" );
104 1 : }
105 :
106 :
107 1 : void urlobjTest_003( )
108 : {
109 1 : INetURLObject aUrl;
110 : aUrl.
111 : setFSysPath( OUString( "\\\\hive-winxp-x86\\pmladek\\test2.odt" ),
112 1 : INetURLObject::FSYS_DETECT );
113 : #ifdef LINUX
114 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
115 1 : == "smb://hive-winxp-x86/pmladek/test2.odt" );
116 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
117 : #endif
118 : #ifdef WIN
119 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
120 : equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
121 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
122 : #endif
123 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
124 1 : == "hive-winxp-x86" );
125 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
126 2 : == "/pmladek/test2.odt" );
127 1 : }
128 :
129 1 : void urlobjTest_004( )
130 : {
131 1 : INetURLObject aUrl( OUString( "smb://10.10.1.1/sampledir/sample.file" ) );
132 : #ifdef LINUX
133 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
134 1 : == "smb://10.10.1.1/sampledir/sample.file" );
135 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
136 : #endif
137 : #ifdef WIN
138 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
139 : equalsAscii( "file://10.10.1.1/sampledir/sample.file" ) );
140 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
141 : #endif
142 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
143 1 : == "10.10.1.1" );
144 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
145 1 : == "/sampledir/sample.file" );
146 1 : CPPUNIT_ASSERT( aUrl.getName( ) == "sample.file" );
147 1 : CPPUNIT_ASSERT( aUrl.getBase( ) == "sample" );
148 1 : CPPUNIT_ASSERT( aUrl.getExtension( ) == "file" );
149 1 : }
150 :
151 1 : void urlobjTest_005( )
152 : {
153 1 : INetURLObject aUrl;
154 : aUrl.setFSysPath( OUString( "//137.65.170.24/c$/Img0001.jpg" ),
155 1 : INetURLObject::FSYS_DETECT );
156 : #ifdef LINUX
157 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
158 1 : == "smb://137.65.170.24/c$/Img0001.jpg" );
159 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
160 : #endif
161 : #ifdef WIN
162 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
163 : equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
164 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
165 : #endif
166 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
167 1 : == "137.65.170.24" );
168 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
169 1 : == "/c$/Img0001.jpg" );
170 1 : CPPUNIT_ASSERT( aUrl.getName( ) == "Img0001.jpg" );
171 1 : CPPUNIT_ASSERT( aUrl.getBase( ) == "Img0001" );
172 1 : CPPUNIT_ASSERT( aUrl.getExtension( ) == "jpg" );
173 1 : }
174 :
175 :
176 1 : void urlobjTest_006( )
177 : {
178 1 : INetURLObject aUrl;
179 : aUrl.setFSysPath( OUString( "//hive-winxp-x86/pmladek/test2.odt" ),
180 1 : INetURLObject::FSYS_DETECT );
181 : #ifdef LINUX
182 2 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE )
183 1 : == "smb://hive-winxp-x86/pmladek/test2.odt" );
184 1 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::Smb );
185 : #endif
186 : #ifdef WIN
187 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
188 : equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
189 : CPPUNIT_ASSERT( aUrl.GetProtocol( ) == INetProtocol::File );
190 : #endif
191 2 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE )
192 1 : == "hive-winxp-x86" );
193 2 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE )
194 2 : == "/pmladek/test2.odt" );
195 1 : }
196 :
197 1 : void urlobjCmisTest( )
198 : {
199 : // Test with a username part
200 : {
201 1 : INetURLObject aUrl( OUString( "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
202 2 : CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
203 1 : OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
204 1 : CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
205 2 : CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
206 1 : OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
207 1 : CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis, aUrl.GetProtocol( ) );
208 : }
209 :
210 : // Test without a username part
211 : {
212 : INetURLObject aUrl( OUString(
213 1 : "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
214 2 : CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
215 1 : OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
216 1 : CPPUNIT_ASSERT( !aUrl.HasUserData() );
217 2 : CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
218 1 : OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
219 1 : CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis, aUrl.GetProtocol( ) );
220 : }
221 1 : }
222 :
223 1 : void urlobjTest_emptyPath() {
224 : {
225 1 : INetURLObject url(OUString("http://example.com"));
226 1 : CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, url.GetProtocol());
227 1 : CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
228 1 : CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
229 : }
230 : {
231 : // This is an invalid http URL per RFC 2616:
232 1 : INetURLObject url(OUString("http://example.com?query"));
233 1 : CPPUNIT_ASSERT(url.HasError());
234 : }
235 : {
236 1 : INetURLObject url(OUString("http://example.com#fragment"));
237 1 : CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, url.GetProtocol());
238 1 : CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
239 1 : CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
240 1 : CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url.GetMark());
241 : }
242 1 : }
243 :
244 1 : void urlobjTest_data() {
245 1 : INetURLObject url;
246 2 : std::unique_ptr<SvMemoryStream> strm;
247 : unsigned char const * buf;
248 :
249 1 : url = INetURLObject("data:");
250 : //TODO: CPPUNIT_ASSERT(url.HasError());
251 1 : strm = url.getData();
252 1 : CPPUNIT_ASSERT(strm == 0);
253 :
254 1 : url = INetURLObject("data:,");
255 1 : CPPUNIT_ASSERT(!url.HasError());
256 1 : strm = url.getData();
257 1 : CPPUNIT_ASSERT(strm != 0);
258 1 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
259 1 : strm.reset();
260 :
261 1 : url = INetURLObject("data:,,%C3%A4%90");
262 1 : CPPUNIT_ASSERT(!url.HasError());
263 1 : strm = url.getData();
264 1 : CPPUNIT_ASSERT(strm != 0);
265 1 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
266 1 : buf = static_cast<unsigned char const *>(strm->GetData());
267 1 : CPPUNIT_ASSERT_EQUAL(0x2C, int(buf[0]));
268 1 : CPPUNIT_ASSERT_EQUAL(0xC3, int(buf[1]));
269 1 : CPPUNIT_ASSERT_EQUAL(0xA4, int(buf[2]));
270 1 : CPPUNIT_ASSERT_EQUAL(0x90, int(buf[3]));
271 1 : strm.reset();
272 :
273 1 : url = INetURLObject("data:base64,");
274 : //TODO: CPPUNIT_ASSERT(url.HasError());
275 1 : strm = url.getData();
276 1 : CPPUNIT_ASSERT(strm == 0);
277 :
278 1 : url = INetURLObject("data:;base64,");
279 1 : CPPUNIT_ASSERT(!url.HasError());
280 1 : strm = url.getData();
281 1 : CPPUNIT_ASSERT(strm != 0);
282 1 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
283 1 : strm.reset();
284 :
285 1 : url = INetURLObject("data:;bAsE64,");
286 1 : CPPUNIT_ASSERT(!url.HasError());
287 1 : strm = url.getData();
288 1 : CPPUNIT_ASSERT(strm != 0);
289 1 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
290 1 : strm.reset();
291 :
292 1 : url = INetURLObject("data:;base64,YWJjCg==");
293 1 : CPPUNIT_ASSERT(!url.HasError());
294 1 : strm = url.getData();
295 1 : CPPUNIT_ASSERT(strm != 0);
296 1 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
297 1 : buf = static_cast<unsigned char const *>(strm->GetData());
298 1 : CPPUNIT_ASSERT_EQUAL(0x61, int(buf[0]));
299 1 : CPPUNIT_ASSERT_EQUAL(0x62, int(buf[1]));
300 1 : CPPUNIT_ASSERT_EQUAL(0x63, int(buf[2]));
301 1 : CPPUNIT_ASSERT_EQUAL(0x0A, int(buf[3]));
302 1 : strm.reset();
303 :
304 1 : url = INetURLObject("data:;base64,YWJjCg=");
305 1 : CPPUNIT_ASSERT(!url.HasError());
306 1 : strm = url.getData();
307 1 : CPPUNIT_ASSERT(strm == 0);
308 :
309 1 : url = INetURLObject("data:;base64,YWJ$Cg==");
310 1 : CPPUNIT_ASSERT(!url.HasError());
311 1 : strm = url.getData();
312 1 : CPPUNIT_ASSERT(strm == 0);
313 :
314 1 : url = INetURLObject("data:text/plain;param=%22;base64,%22,YQ==");
315 1 : CPPUNIT_ASSERT(!url.HasError());
316 1 : strm = url.getData();
317 1 : CPPUNIT_ASSERT(strm != 0);
318 1 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
319 1 : buf = static_cast<unsigned char const *>(strm->GetData());
320 1 : CPPUNIT_ASSERT_EQUAL(0x59, int(buf[0]));
321 1 : CPPUNIT_ASSERT_EQUAL(0x51, int(buf[1]));
322 1 : CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[2]));
323 1 : CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[3]));
324 1 : strm.reset();
325 :
326 1 : url = INetURLObject("http://example.com");
327 1 : CPPUNIT_ASSERT(!url.HasError());
328 1 : strm = url.getData();
329 2 : CPPUNIT_ASSERT(strm == 0);
330 1 : }
331 :
332 : // Change the following lines only, if you add, remove or rename
333 : // member functions of the current class,
334 : // because these macros are need by auto register mechanism.
335 :
336 2 : CPPUNIT_TEST_SUITE( urlobjTest );
337 1 : CPPUNIT_TEST( urlobjTest_001 );
338 1 : CPPUNIT_TEST( urlobjTest_002 );
339 1 : CPPUNIT_TEST( urlobjTest_003 );
340 1 : CPPUNIT_TEST( urlobjTest_004 );
341 1 : CPPUNIT_TEST( urlobjTest_005 );
342 1 : CPPUNIT_TEST( urlobjTest_006 );
343 1 : CPPUNIT_TEST( urlobjCmisTest );
344 1 : CPPUNIT_TEST( urlobjTest_emptyPath );
345 1 : CPPUNIT_TEST( urlobjTest_data );
346 5 : CPPUNIT_TEST_SUITE_END( );
347 : }; // class createPool
348 :
349 :
350 1 : CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
351 3 : } // namespace rtl_random
352 :
353 :
354 :
355 :
356 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|