summaryrefslogtreecommitdiff
path: root/tools/qa/cppunit/test_urlobj.cxx
blob: abcd2fe1417bcc053505f649012589926cb52511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <memory>
#include <string>

#include <sal/types.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <tools/stream.hxx>
#include <tools/urlobj.hxx>

#define OUSTR_TO_STDSTR( oustr ) std::string( OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ).getStr() )

CPPUNIT_NS_BEGIN

template<> struct assertion_traits<INetProtocol>
{
    static bool equal( const INetProtocol& x, const INetProtocol& y )
    {
        return x == y;
    }

    static std::string toString( const INetProtocol& x )
    {
        OStringStream ost;
        ost << static_cast<unsigned int>(x);
        return ost.str();
    }
};

CPPUNIT_NS_END

namespace tools_urlobj
{

    class urlobjTest:public CppUnit::TestFixture
    {

      public:
        // insert your test code here.
        // this is only demonstration code
        void urlobjTest_001(  )
        {
            INetURLObject aUrl( OUString( "file://10.10.1.1/sampledir/sample.file" ) );
            CPPUNIT_ASSERT_EQUAL(INetProtocol::File, aUrl.GetProtocol());
            CPPUNIT_ASSERT_EQUAL(OUString("10.10.1.1"),
                                 aUrl.GetHost(INetURLObject::DecodeMechanism::NONE));
            CPPUNIT_ASSERT_EQUAL(OUString("/sampledir/sample.file"),
                                 aUrl.GetURLPath(INetURLObject::DecodeMechanism::NONE));
            CPPUNIT_ASSERT_EQUAL(OUString("sample.file"),
                                 aUrl.getName());
            CPPUNIT_ASSERT_EQUAL(OUString("sample"), aUrl.getBase());
            CPPUNIT_ASSERT_EQUAL(OUString("file"), aUrl.getExtension());
        }

        void urlobjTest_004(  )
        {
            INetURLObject aUrl( OUString( "smb://10.10.1.1/sampledir/sample.file" ) );
            CPPUNIT_ASSERT_EQUAL( INetProtocol::Smb, aUrl.GetProtocol(  ) );
            CPPUNIT_ASSERT_EQUAL(OUString("10.10.1.1"),
                                 aUrl.GetHost(INetURLObject::DecodeMechanism::NONE));
            CPPUNIT_ASSERT_EQUAL(OUString("/sampledir/sample.file"),
                                 aUrl.GetURLPath(INetURLObject::DecodeMechanism::NONE));
            CPPUNIT_ASSERT_EQUAL(OUString("sample.file"), aUrl.getName());
            CPPUNIT_ASSERT_EQUAL(OUString("sample"), aUrl.getBase());
            CPPUNIT_ASSERT_EQUAL(OUString("file"), aUrl.getExtension());
        }

        void urlobjCmisTest(  )
        {
            // Test with a username part
            {
                INetURLObject aUrl( OUString( "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
                CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
                        OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DecodeMechanism::WithCharset ) ) );
                CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
                CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
                        OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::DecodeMechanism::NONE ) ) );
                CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis, aUrl.GetProtocol(  ) );
            }

            // Test without a username part
            {
                INetURLObject aUrl( OUString(
                                "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
                CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
                        OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DecodeMechanism::WithCharset ) ) );
                CPPUNIT_ASSERT( !aUrl.HasUserData() );
                CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
                        OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::DecodeMechanism::NONE ) ) );
                CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INetProtocol::Cmis, aUrl.GetProtocol(  ) );
            }
        }

        void urlobjTest_emptyPath() {
            {
                INetURLObject url(OUString("http://example.com"));
                CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, url.GetProtocol());
                CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
                CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
            }
            {
                // This is an invalid http URL per RFC 2616:
                INetURLObject url(OUString("http://example.com?query"));
                CPPUNIT_ASSERT(url.HasError());
            }
            {
                INetURLObject url(OUString("http://example.com#fragment"));
                CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, url.GetProtocol());
                CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
                CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
                CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url.GetMark());
            }
        }

        void urlobjTest_data() {
            INetURLObject url;
            std::unique_ptr<SvMemoryStream> strm;
            unsigned char const * buf;

            url = INetURLObject("data:");
            //TODO: CPPUNIT_ASSERT(url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(!strm);

            url = INetURLObject("data:,");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(strm != nullptr);
            CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm->GetSize());
            strm.reset();

            url = INetURLObject("data:,,%C3%A4%90");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(strm != nullptr);
            CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm->GetSize());
            buf = static_cast<unsigned char const *>(strm->GetData());
            CPPUNIT_ASSERT_EQUAL(0x2C, int(buf[0]));
            CPPUNIT_ASSERT_EQUAL(0xC3, int(buf[1]));
            CPPUNIT_ASSERT_EQUAL(0xA4, int(buf[2]));
            CPPUNIT_ASSERT_EQUAL(0x90, int(buf[3]));
            strm.reset();

            url = INetURLObject("data:base64,");
            //TODO: CPPUNIT_ASSERT(url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(!strm);

            url = INetURLObject("data:;base64,");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(strm != nullptr);
            CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm->GetSize());
            strm.reset();

            url = INetURLObject("data:;bAsE64,");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(strm != nullptr);
            CPPUNIT_ASSERT_EQUAL(sal_uInt64(0), strm->GetSize());
            strm.reset();

            url = INetURLObject("data:;base64,YWJjCg==");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(strm != nullptr);
            CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm->GetSize());
            buf = static_cast<unsigned char const *>(strm->GetData());
            CPPUNIT_ASSERT_EQUAL(0x61, int(buf[0]));
            CPPUNIT_ASSERT_EQUAL(0x62, int(buf[1]));
            CPPUNIT_ASSERT_EQUAL(0x63, int(buf[2]));
            CPPUNIT_ASSERT_EQUAL(0x0A, int(buf[3]));
            strm.reset();

            url = INetURLObject("data:;base64,YWJjCg=");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(!strm);

            url = INetURLObject("data:;base64,YWJ$Cg==");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(!strm);

            url = INetURLObject("data:text/plain;param=%22;base64,%22,YQ==");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(strm != nullptr);
            CPPUNIT_ASSERT_EQUAL(sal_uInt64(4), strm->GetSize());
            buf = static_cast<unsigned char const *>(strm->GetData());
            CPPUNIT_ASSERT_EQUAL(0x59, int(buf[0]));
            CPPUNIT_ASSERT_EQUAL(0x51, int(buf[1]));
            CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[2]));
            CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[3]));
            strm.reset();

            url = INetURLObject("http://example.com");
            CPPUNIT_ASSERT(!url.HasError());
            strm = url.getData();
            CPPUNIT_ASSERT(!strm);
        }

        void urlobjTest_isSchemeEqualTo() {
            CPPUNIT_ASSERT(INetURLObject().isSchemeEqualTo(INetProtocol::NotValid));
            CPPUNIT_ASSERT(!INetURLObject().isSchemeEqualTo(u""));
            CPPUNIT_ASSERT(
                INetURLObject("http://example.org").isSchemeEqualTo(
                    INetProtocol::Http));
            CPPUNIT_ASSERT(
                !INetURLObject("http://example.org").isSchemeEqualTo(
                    INetProtocol::Https));
            CPPUNIT_ASSERT(
                INetURLObject("http://example.org").isSchemeEqualTo(u"Http"));
            CPPUNIT_ASSERT(
                !INetURLObject("http://example.org").isSchemeEqualTo(u"dav"));
            CPPUNIT_ASSERT(
                INetURLObject("dav://example.org").isSchemeEqualTo(u"dav"));
        }

        void urlobjTest_isAnyKnownWebDAVScheme() {
            CPPUNIT_ASSERT(
                INetURLObject("http://example.org").isAnyKnownWebDAVScheme());
            CPPUNIT_ASSERT(
                INetURLObject("https://example.org").isAnyKnownWebDAVScheme());
            CPPUNIT_ASSERT(
                INetURLObject("vnd.sun.star.webdav://example.org").isAnyKnownWebDAVScheme());
            CPPUNIT_ASSERT(
                INetURLObject("vnd.sun.star.webdavs://example.org").isAnyKnownWebDAVScheme());
            CPPUNIT_ASSERT(
                !INetURLObject("ftp://example.org").isAnyKnownWebDAVScheme());
            CPPUNIT_ASSERT(
                !INetURLObject("file://example.org").isAnyKnownWebDAVScheme());
            CPPUNIT_ASSERT(
                !INetURLObject("dav://example.org").isAnyKnownWebDAVScheme());
            CPPUNIT_ASSERT(
                !INetURLObject("davs://example.org").isAnyKnownWebDAVScheme());
            CPPUNIT_ASSERT(
                !INetURLObject("vnd.sun.star.pkg://example.org").isAnyKnownWebDAVScheme());
        }

        void testSetName() {
            {
                INetURLObject obj("file:///");
                bool ok = obj.setName(u"foo");
                CPPUNIT_ASSERT(!ok);
            }
            {
                INetURLObject obj("file:///foo");
                bool ok = obj.setName(u"bar");
                CPPUNIT_ASSERT(ok);
                CPPUNIT_ASSERT_EQUAL(
                    OUString("file:///bar"), obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
            }
            {
                INetURLObject obj("file:///foo/");
                bool ok = obj.setName(u"bar");
                CPPUNIT_ASSERT(ok);
                CPPUNIT_ASSERT_EQUAL(
                    OUString("file:///bar/"), obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
            }
            {
                INetURLObject obj("file:///foo/bar");
                bool ok = obj.setName(u"baz");
                CPPUNIT_ASSERT(ok);
                CPPUNIT_ASSERT_EQUAL(
                    OUString("file:///foo/baz"),
                    obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
            }
            {
                INetURLObject obj("file:///foo/bar/");
                bool ok = obj.setName(u"baz");
                CPPUNIT_ASSERT(ok);
                CPPUNIT_ASSERT_EQUAL(
                    OUString("file:///foo/baz/"),
                    obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
            }
        }

        void testSetExtension() {
            INetURLObject obj("file:///foo/bar.baz/");
            bool ok = obj.setExtension(
                u"other", INetURLObject::LAST_SEGMENT, false);
            CPPUNIT_ASSERT(ok);
            CPPUNIT_ASSERT_EQUAL(
                OUString("file:///foo/bar.baz/.other"),
                obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
        }

        void testChangeScheme() {
            INetURLObject obj("unknown://example.com/foo/bar");
            CPPUNIT_ASSERT(!obj.HasError());
            obj.changeScheme(INetProtocol::Http);
            CPPUNIT_ASSERT_EQUAL(
                OUString("http://example.com/foo/bar"),
                obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
            obj.changeScheme(INetProtocol::Https);
            CPPUNIT_ASSERT_EQUAL(
                OUString("https://example.com/foo/bar"),
                obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
            obj.changeScheme(INetProtocol::Ftp);
            CPPUNIT_ASSERT_EQUAL(
                OUString("ftp://example.com/foo/bar"),
                obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
        }

        void testTd146382() {
            INetURLObject obj("file://share.allotropia.de@SSL/DavWWWRoot/remote.php");
            CPPUNIT_ASSERT(!obj.HasError());
            CPPUNIT_ASSERT_EQUAL(
                OUString("file://share.allotropia.de@SSL/DavWWWRoot/remote.php"),
                obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
        }

        void testParseSmart()
        {
            {
                // host:port must not be misinterpreted as scheme:path
                INetURLObject obj("example.com:8080/foo", INetProtocol::Http);
                CPPUNIT_ASSERT(!obj.HasError());
                CPPUNIT_ASSERT_EQUAL(OUString("http://example.com:8080/foo"),
                    obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
                CPPUNIT_ASSERT_EQUAL(INetProtocol::Http, obj.GetProtocol());
                CPPUNIT_ASSERT_EQUAL(OUString("example.com"), obj.GetHost());
                CPPUNIT_ASSERT_EQUAL(sal_uInt32(8080), obj.GetPort());
                CPPUNIT_ASSERT_EQUAL(OUString("/foo"), obj.GetURLPath());
            }
            {
                // port may only contain decimal digits, so this must be treated as unknown scheme
                INetURLObject obj("example.com:80a0/foo", INetProtocol::Http);
                CPPUNIT_ASSERT(!obj.HasError());
                CPPUNIT_ASSERT_EQUAL(OUString("example.com:80a0/foo"),
                    obj.GetMainURL(INetURLObject::DecodeMechanism::NONE));
                CPPUNIT_ASSERT_EQUAL(INetProtocol::Generic, obj.GetProtocol());
                CPPUNIT_ASSERT(obj.isSchemeEqualTo(u"example.com"));
                CPPUNIT_ASSERT_EQUAL(OUString(""), obj.GetHost());
                CPPUNIT_ASSERT_EQUAL(OUString("80a0/foo"), obj.GetURLPath());
            }
        }

        // Change the following lines only, if you add, remove or rename
        // member functions of the current class,
        // because these macros are need by auto register mechanism.

        CPPUNIT_TEST_SUITE( urlobjTest );
        CPPUNIT_TEST( urlobjTest_001 );
        CPPUNIT_TEST( urlobjTest_004 );
        CPPUNIT_TEST( urlobjCmisTest );
        CPPUNIT_TEST( urlobjTest_emptyPath );
        CPPUNIT_TEST( urlobjTest_data );
        CPPUNIT_TEST( urlobjTest_isSchemeEqualTo );
        CPPUNIT_TEST( urlobjTest_isAnyKnownWebDAVScheme );
        CPPUNIT_TEST( testSetName );
        CPPUNIT_TEST( testSetExtension );
        CPPUNIT_TEST( testChangeScheme );
        CPPUNIT_TEST( testTd146382 );
        CPPUNIT_TEST( testParseSmart );
        CPPUNIT_TEST_SUITE_END(  );
    };                          // class createPool


    CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
}                               // namespace rtl_random


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */