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 8 : static bool equal( const INetProtocol& x, const INetProtocol& y )
25 : {
26 8 : 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 54 : class urlobjTest:public CppUnit::TestFixture
43 : {
44 :
45 : public:
46 : // initialise your test code values here.
47 18 : void setUp( ) SAL_OVERRIDE
48 : {
49 18 : }
50 :
51 18 : void tearDown( ) SAL_OVERRIDE
52 : {
53 18 : }
54 :
55 : // insert your test code here.
56 : // this is only demonstration code
57 2 : void urlobjTest_001( )
58 : {
59 2 : INetURLObject aUrl( OUString( "file://10.10.1.1/sampledir/sample.file" ) );
60 : #ifdef LINUX
61 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
62 : equalsAscii
63 2 : ( "smb://10.10.1.1/sampledir/sample.file" ) );
64 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
73 2 : equalsAscii( "10.10.1.1" ) );
74 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
75 2 : equalsAscii( "/sampledir/sample.file" ) );
76 4 : CPPUNIT_ASSERT( aUrl.getName( ).
77 2 : equalsAscii( "sample.file" ) );
78 2 : CPPUNIT_ASSERT( aUrl.getBase( ).equalsAscii( "sample" ) );
79 2 : CPPUNIT_ASSERT( aUrl.getExtension( ).equalsAscii( "file" ) );
80 2 : }
81 :
82 2 : void urlobjTest_002( )
83 : {
84 2 : INetURLObject aUrl;
85 : aUrl.
86 : setFSysPath( OUString( "\\\\137.65.170.24\\c$\\Img0001.jpg" ),
87 2 : INetURLObject::FSYS_DETECT );
88 : #ifdef LINUX
89 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
90 2 : equalsAscii( "smb://137.65.170.24/c$/Img0001.jpg" ) );
91 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
99 2 : equalsAscii( "137.65.170.24" ) );
100 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
101 2 : equalsAscii( "/c$/Img0001.jpg" ) );
102 4 : CPPUNIT_ASSERT( aUrl.getName( ).
103 2 : equalsAscii( "Img0001.jpg" ) );
104 2 : CPPUNIT_ASSERT( aUrl.getBase( ).equalsAscii( "Img0001" ) );
105 2 : CPPUNIT_ASSERT( aUrl.getExtension( ).equalsAscii( "jpg" ) );
106 2 : }
107 :
108 :
109 2 : void urlobjTest_003( )
110 : {
111 2 : INetURLObject aUrl;
112 : aUrl.
113 : setFSysPath( OUString( "\\\\hive-winxp-x86\\pmladek\\test2.odt" ),
114 2 : INetURLObject::FSYS_DETECT );
115 : #ifdef LINUX
116 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
117 2 : equalsAscii( "smb://hive-winxp-x86/pmladek/test2.odt" ) );
118 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
126 2 : equalsAscii( "hive-winxp-x86" ) );
127 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
128 4 : equalsAscii( "/pmladek/test2.odt" ) );
129 2 : }
130 :
131 2 : void urlobjTest_004( )
132 : {
133 2 : INetURLObject aUrl( OUString( "smb://10.10.1.1/sampledir/sample.file" ) );
134 : #ifdef LINUX
135 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
136 2 : equalsAscii( "smb://10.10.1.1/sampledir/sample.file" ) );
137 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
145 2 : equalsAscii( "10.10.1.1" ) );
146 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
147 2 : equalsAscii( "/sampledir/sample.file" ) );
148 4 : CPPUNIT_ASSERT( aUrl.getName( ).
149 2 : equalsAscii( "sample.file" ) );
150 2 : CPPUNIT_ASSERT( aUrl.getBase( ).equalsAscii( "sample" ) );
151 2 : CPPUNIT_ASSERT( aUrl.getExtension( ).equalsAscii( "file" ) );
152 2 : }
153 :
154 2 : void urlobjTest_005( )
155 : {
156 2 : INetURLObject aUrl;
157 : aUrl.setFSysPath( OUString( "//137.65.170.24/c$/Img0001.jpg" ),
158 2 : INetURLObject::FSYS_DETECT );
159 : #ifdef LINUX
160 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
161 2 : equalsAscii( "smb://137.65.170.24/c$/Img0001.jpg" ) );
162 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
170 2 : equalsAscii( "137.65.170.24" ) );
171 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
172 2 : equalsAscii( "/c$/Img0001.jpg" ) );
173 2 : CPPUNIT_ASSERT( aUrl.getName( ).equalsAscii( "Img0001.jpg" ) );
174 2 : CPPUNIT_ASSERT( aUrl.getBase( ).equalsAscii( "Img0001" ) );
175 2 : CPPUNIT_ASSERT( aUrl.getExtension( ).equalsAscii( "jpg" ) );
176 2 : }
177 :
178 :
179 2 : void urlobjTest_006( )
180 : {
181 2 : INetURLObject aUrl;
182 : aUrl.setFSysPath( OUString( "//hive-winxp-x86/pmladek/test2.odt" ),
183 2 : INetURLObject::FSYS_DETECT );
184 : #ifdef LINUX
185 4 : CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
186 2 : equalsAscii( "smb://hive-winxp-x86/pmladek/test2.odt" ) );
187 2 : 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 4 : CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
195 2 : equalsAscii( "hive-winxp-x86" ) );
196 4 : CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
197 4 : equalsAscii( "/pmladek/test2.odt" ) );
198 2 : }
199 :
200 2 : void urlobjCmisTest( )
201 : {
202 : // Test with a username part
203 : {
204 2 : INetURLObject aUrl( OUString( "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
205 4 : CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
206 2 : OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
207 2 : CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
208 4 : CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
209 2 : OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
210 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INET_PROT_CMIS, aUrl.GetProtocol( ) );
211 : }
212 :
213 : // Test without a username part
214 : {
215 : INetURLObject aUrl( OUString(
216 2 : "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
217 4 : CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
218 2 : OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
219 2 : CPPUNIT_ASSERT( !aUrl.HasUserData() );
220 4 : CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
221 2 : OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
222 2 : CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INET_PROT_CMIS, aUrl.GetProtocol( ) );
223 : }
224 2 : }
225 :
226 2 : void urlobjTest_emptyPath() {
227 : {
228 2 : INetURLObject url(OUString("http://example.com"));
229 2 : CPPUNIT_ASSERT_EQUAL(INET_PROT_HTTP, url.GetProtocol());
230 2 : CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
231 2 : CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
232 : }
233 : {
234 : // This is an invalid http URL per RFC 2616:
235 2 : INetURLObject url(OUString("http://example.com?query"));
236 2 : CPPUNIT_ASSERT(url.HasError());
237 : }
238 : {
239 2 : INetURLObject url(OUString("http://example.com#fragment"));
240 2 : CPPUNIT_ASSERT_EQUAL(INET_PROT_HTTP, url.GetProtocol());
241 2 : CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
242 2 : CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
243 2 : CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url.GetMark());
244 : }
245 2 : }
246 :
247 2 : void urlobjTest_data() {
248 2 : INetURLObject url;
249 4 : std::unique_ptr<SvMemoryStream> strm;
250 : unsigned char const * buf;
251 :
252 2 : url = INetURLObject("data:");
253 : //TODO: CPPUNIT_ASSERT(url.HasError());
254 2 : strm = url.getData();
255 2 : CPPUNIT_ASSERT(strm == 0);
256 :
257 2 : url = INetURLObject("data:,");
258 2 : CPPUNIT_ASSERT(!url.HasError());
259 2 : strm = url.getData();
260 2 : CPPUNIT_ASSERT(strm != 0);
261 2 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
262 2 : strm.reset();
263 :
264 2 : url = INetURLObject("data:,,%C3%A4%90");
265 2 : CPPUNIT_ASSERT(!url.HasError());
266 2 : strm = url.getData();
267 2 : CPPUNIT_ASSERT(strm != 0);
268 2 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
269 2 : buf = static_cast<unsigned char const *>(strm->GetData());
270 2 : CPPUNIT_ASSERT_EQUAL(0x2C, int(buf[0]));
271 2 : CPPUNIT_ASSERT_EQUAL(0xC3, int(buf[1]));
272 2 : CPPUNIT_ASSERT_EQUAL(0xA4, int(buf[2]));
273 2 : CPPUNIT_ASSERT_EQUAL(0x90, int(buf[3]));
274 2 : strm.reset();
275 :
276 2 : url = INetURLObject("data:base64,");
277 : //TODO: CPPUNIT_ASSERT(url.HasError());
278 2 : strm = url.getData();
279 2 : CPPUNIT_ASSERT(strm == 0);
280 :
281 2 : url = INetURLObject("data:;base64,");
282 2 : CPPUNIT_ASSERT(!url.HasError());
283 2 : strm = url.getData();
284 2 : CPPUNIT_ASSERT(strm != 0);
285 2 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
286 2 : strm.reset();
287 :
288 2 : url = INetURLObject("data:;bAsE64,");
289 2 : CPPUNIT_ASSERT(!url.HasError());
290 2 : strm = url.getData();
291 2 : CPPUNIT_ASSERT(strm != 0);
292 2 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
293 2 : strm.reset();
294 :
295 2 : url = INetURLObject("data:;base64,YWJjCg==");
296 2 : CPPUNIT_ASSERT(!url.HasError());
297 2 : strm = url.getData();
298 2 : CPPUNIT_ASSERT(strm != 0);
299 2 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
300 2 : buf = static_cast<unsigned char const *>(strm->GetData());
301 2 : CPPUNIT_ASSERT_EQUAL(0x61, int(buf[0]));
302 2 : CPPUNIT_ASSERT_EQUAL(0x62, int(buf[1]));
303 2 : CPPUNIT_ASSERT_EQUAL(0x63, int(buf[2]));
304 2 : CPPUNIT_ASSERT_EQUAL(0x0A, int(buf[3]));
305 2 : strm.reset();
306 :
307 2 : url = INetURLObject("data:;base64,YWJjCg=");
308 2 : CPPUNIT_ASSERT(!url.HasError());
309 2 : strm = url.getData();
310 2 : CPPUNIT_ASSERT(strm == 0);
311 :
312 2 : url = INetURLObject("data:;base64,YWJ$Cg==");
313 2 : CPPUNIT_ASSERT(!url.HasError());
314 2 : strm = url.getData();
315 2 : CPPUNIT_ASSERT(strm == 0);
316 :
317 2 : url = INetURLObject("data:text/plain;param=%22;base64,%22,YQ==");
318 2 : CPPUNIT_ASSERT(!url.HasError());
319 2 : strm = url.getData();
320 2 : CPPUNIT_ASSERT(strm != 0);
321 2 : CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
322 2 : buf = static_cast<unsigned char const *>(strm->GetData());
323 2 : CPPUNIT_ASSERT_EQUAL(0x59, int(buf[0]));
324 2 : CPPUNIT_ASSERT_EQUAL(0x51, int(buf[1]));
325 2 : CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[2]));
326 2 : CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[3]));
327 2 : strm.reset();
328 :
329 2 : url = INetURLObject("http://example.com");
330 2 : CPPUNIT_ASSERT(!url.HasError());
331 2 : strm = url.getData();
332 4 : CPPUNIT_ASSERT(strm == 0);
333 2 : }
334 :
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.
338 :
339 4 : CPPUNIT_TEST_SUITE( urlobjTest );
340 2 : CPPUNIT_TEST( urlobjTest_001 );
341 2 : CPPUNIT_TEST( urlobjTest_002 );
342 2 : CPPUNIT_TEST( urlobjTest_003 );
343 2 : CPPUNIT_TEST( urlobjTest_004 );
344 2 : CPPUNIT_TEST( urlobjTest_005 );
345 2 : CPPUNIT_TEST( urlobjTest_006 );
346 2 : CPPUNIT_TEST( urlobjCmisTest );
347 2 : CPPUNIT_TEST( urlobjTest_emptyPath );
348 2 : CPPUNIT_TEST( urlobjTest_data );
349 4 : CPPUNIT_TEST_SUITE_END( );
350 : }; // class createPool
351 :
352 :
353 2 : CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
354 6 : } // namespace rtl_random
355 :
356 :
357 :
358 :
359 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|