Bump version to 6.4-15
[LibreOffice.git] / tools / qa / cppunit / test_urlobj.cxx
blob127e01067ec8b83a9b10cfe970947c03b6dc0a65
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 <memory>
11 #include <string>
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() )
21 CPPUNIT_NS_BEGIN
23 template<> struct assertion_traits<INetProtocol>
25 static bool equal( const INetProtocol& x, const INetProtocol& y )
27 return x == y;
30 static std::string toString( const INetProtocol& x )
32 OStringStream ost;
33 ost << static_cast<unsigned int>(x);
34 return ost.str();
38 CPPUNIT_NS_END
40 namespace tools_urlobj
43 class urlobjTest:public CppUnit::TestFixture
46 public:
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" ) );
52 #ifdef LINUX
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());
56 #endif
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"),
62 aUrl.getName());
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" ) );
70 #ifdef LINUX
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( ) );
74 #endif
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() {
132 INetURLObject url;
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());
146 strm.reset();
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]));
158 strm.reset();
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());
170 strm.reset();
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());
177 strm.reset();
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]));
189 strm.reset();
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]));
211 strm.reset();
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""));
222 CPPUNIT_ASSERT(
223 INetURLObject("http://example.org").isSchemeEqualTo(
224 INetProtocol::Http));
225 CPPUNIT_ASSERT(
226 !INetURLObject("http://example.org").isSchemeEqualTo(
227 INetProtocol::Https));
228 CPPUNIT_ASSERT(
229 INetURLObject("http://example.org").isSchemeEqualTo(u"Http"));
230 CPPUNIT_ASSERT(
231 !INetURLObject("http://example.org").isSchemeEqualTo(u"dav"));
232 CPPUNIT_ASSERT(
233 INetURLObject("dav://example.org").isSchemeEqualTo(u"dav"));
236 void urlobjTest_isAnyKnownWebDAVScheme() {
237 CPPUNIT_ASSERT(
238 INetURLObject("http://example.org").isAnyKnownWebDAVScheme());
239 CPPUNIT_ASSERT(
240 INetURLObject("https://example.org").isAnyKnownWebDAVScheme());
241 CPPUNIT_ASSERT(
242 INetURLObject("vnd.sun.star.webdav://example.org").isAnyKnownWebDAVScheme());
243 CPPUNIT_ASSERT(
244 INetURLObject("vnd.sun.star.webdavs://example.org").isAnyKnownWebDAVScheme());
245 CPPUNIT_ASSERT(
246 !INetURLObject("ftp://example.org").isAnyKnownWebDAVScheme());
247 CPPUNIT_ASSERT(
248 !INetURLObject("file://example.org").isAnyKnownWebDAVScheme());
249 CPPUNIT_ASSERT(
250 !INetURLObject("dav://example.org").isAnyKnownWebDAVScheme());
251 CPPUNIT_ASSERT(
252 !INetURLObject("davs://example.org").isAnyKnownWebDAVScheme());
253 CPPUNIT_ASSERT(
254 !INetURLObject("vnd.sun.star.pkg://example.org").isAnyKnownWebDAVScheme());
257 void testSetName() {
259 INetURLObject obj("file:///");
260 bool ok = obj.setName("foo");
261 CPPUNIT_ASSERT(!ok);
264 INetURLObject obj("file:///foo");
265 bool ok = obj.setName("bar");
266 CPPUNIT_ASSERT(ok);
267 CPPUNIT_ASSERT_EQUAL(
268 OUString("file:///bar"), obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
271 INetURLObject obj("file:///foo/");
272 bool ok = obj.setName("bar");
273 CPPUNIT_ASSERT(ok);
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");
280 CPPUNIT_ASSERT(ok);
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");
288 CPPUNIT_ASSERT(ok);
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);
299 CPPUNIT_ASSERT(ok);
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: */