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  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <cassert>
21 #include <cstddef>
22 
23 #include <com/sun/star/lang/Locale.hpp>
24 #include <com/sun/star/lang/XComponent.hpp>
25 #include <com/sun/star/ucb/Command.hpp>
26 #include <com/sun/star/ucb/IllegalIdentifierException.hpp>
27 #include <com/sun/star/ucb/UniversalContentBroker.hpp>
28 #include <com/sun/star/ucb/XCommandProcessor.hpp>
29 #include <com/sun/star/ucb/XContent.hpp>
30 #include <com/sun/star/ucb/XContentIdentifier.hpp>
31 #include <com/sun/star/ucb/XContentProvider.hpp>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <com/sun/star/uno/Exception.hpp>
34 #include <com/sun/star/uno/Reference.hxx>
35 #include <com/sun/star/uno/RuntimeException.hpp>
36 #include <com/sun/star/uno/XComponentContext.hpp>
37 #include <com/sun/star/uri/XUriReference.hpp>
38 #include <cppuhelper/bootstrap.hxx>
39 #include <cppuhelper/implbase.hxx>
40 #include <cppunit/TestFixture.h>
41 #include <cppunit/extensions/HelperMacros.h>
42 #include <cppunit/plugin/TestPlugIn.h>
43 #include <rtl/strbuf.hxx>
44 #include <rtl/string.h>
45 #include <rtl/string.hxx>
46 #include <rtl/textenc.h>
47 #include <rtl/ustring.hxx>
48 #include <sal/macros.h>
49 #include <sal/types.h>
50 #include <svl/urihelper.hxx>
51 #include <unotools/charclass.hxx>
52 
53 namespace com { namespace sun { namespace star { namespace ucb {
54     class XCommandEnvironment;
55     class XContentEventListener;
56 } } } }
57 
58 namespace {
59 
60 // This class only implements that subset of functionality of a proper
61 // css::ucb::Content that is known to be needed here:
62 class Content:
63     public cppu::WeakImplHelper<
64         css::ucb::XContent, css::ucb::XCommandProcessor >
65 {
66 public:
67     explicit Content(
68         css::uno::Reference< css::ucb::XContentIdentifier > const & identifier);
69 
70     virtual css::uno::Reference< css::ucb::XContentIdentifier > SAL_CALL
getIdentifier()71     getIdentifier() override {
72         return m_identifier;
73     }
74 
getContentType()75     virtual OUString SAL_CALL getContentType() override
76     {
77         return OUString();
78     }
79 
addContentEventListener(css::uno::Reference<css::ucb::XContentEventListener> const &)80     virtual void SAL_CALL addContentEventListener(
81         css::uno::Reference< css::ucb::XContentEventListener > const &) override
82     {}
83 
removeContentEventListener(css::uno::Reference<css::ucb::XContentEventListener> const &)84     virtual void SAL_CALL removeContentEventListener(
85         css::uno::Reference< css::ucb::XContentEventListener > const &) override
86     {}
87 
createCommandIdentifier()88     virtual sal_Int32 SAL_CALL createCommandIdentifier() override
89     {
90         return 0;
91     }
92 
93     virtual css::uno::Any SAL_CALL execute(
94         css::ucb::Command const & command, sal_Int32 commandId,
95         css::uno::Reference< css::ucb::XCommandEnvironment > const &) override;
96 
abort(sal_Int32)97     virtual void SAL_CALL abort(sal_Int32) override {}
98 
99 private:
100     static char const m_prefix[];
101 
102     css::uno::Reference< css::ucb::XContentIdentifier > m_identifier;
103 };
104 
105 char const Content::m_prefix[] = "test:";
106 
Content(css::uno::Reference<css::ucb::XContentIdentifier> const & identifier)107 Content::Content(
108     css::uno::Reference< css::ucb::XContentIdentifier > const & identifier):
109     m_identifier(identifier)
110 {
111     assert(m_identifier.is());
112     OUString uri(m_identifier->getContentIdentifier());
113     if (!uri.matchIgnoreAsciiCase(m_prefix)
114         || uri.indexOf('#', RTL_CONSTASCII_LENGTH(m_prefix)) != -1)
115     {
116         throw css::ucb::IllegalIdentifierException();
117     }
118 }
119 
execute(css::ucb::Command const & command,sal_Int32,css::uno::Reference<css::ucb::XCommandEnvironment> const &)120 css::uno::Any Content::execute(
121     css::ucb::Command const & command, sal_Int32,
122     css::uno::Reference< css::ucb::XCommandEnvironment > const &)
123 {
124     if ( command.Name != "getCasePreservingURL" )
125     {
126         throw css::uno::RuntimeException();
127     }
128     // If any non-empty segment starts with anything but '0', '1', or '2', fail;
129     // otherwise, if the last non-empty segment starts with '1', add a final
130     // slash, and if the last non-empty segment starts with '2', remove a final
131     // slash (if any); also, turn the given uri into all-lowercase:
132     OUString uri(m_identifier->getContentIdentifier());
133     sal_Unicode c = '0';
134     for (sal_Int32 i = RTL_CONSTASCII_LENGTH(m_prefix); i != -1;) {
135         OUString seg(uri.getToken(0, '/', i));
136         if (seg.getLength() > 0) {
137             c = seg[0];
138             if (c < '0' || c > '2') {
139                 throw css::uno::Exception();
140             }
141         }
142     }
143     switch (c) {
144     case '1':
145         uri += "/";
146         break;
147     case '2':
148         if (uri.endsWith("/")) {
149             uri = uri.copy(0, uri.getLength() -1);
150         }
151         break;
152     }
153     return css::uno::makeAny(uri.toAsciiLowerCase());
154 }
155 
156 class Provider: public cppu::WeakImplHelper< css::ucb::XContentProvider > {
157 public:
queryContent(css::uno::Reference<css::ucb::XContentIdentifier> const & identifier)158     virtual css::uno::Reference< css::ucb::XContent > SAL_CALL queryContent(
159         css::uno::Reference< css::ucb::XContentIdentifier > const & identifier) override
160     {
161         return new Content(identifier);
162     }
163 
compareContentIds(css::uno::Reference<css::ucb::XContentIdentifier> const & id1,css::uno::Reference<css::ucb::XContentIdentifier> const & id2)164     virtual sal_Int32 SAL_CALL compareContentIds(
165         css::uno::Reference< css::ucb::XContentIdentifier > const & id1,
166         css::uno::Reference< css::ucb::XContentIdentifier > const & id2) override
167     {
168         assert(id1.is() && id2.is());
169         return
170             id1->getContentIdentifier().compareTo(id2->getContentIdentifier());
171     }
172 };
173 
174 class Test: public CppUnit::TestFixture {
175 public:
176     virtual void setUp() override;
177 
178     void finish();
179 
180     void testNormalizedMakeRelative();
181 
182     void testFindFirstURLInText();
183 
184     void testResolveIdnaHost();
185 
186     CPPUNIT_TEST_SUITE(Test);
187     CPPUNIT_TEST(testNormalizedMakeRelative);
188     CPPUNIT_TEST(testFindFirstURLInText);
189     CPPUNIT_TEST(testResolveIdnaHost);
190     CPPUNIT_TEST(finish);
191     CPPUNIT_TEST_SUITE_END();
192 
193 private:
194     static css::uno::Reference< css::uno::XComponentContext > m_context;
195 };
196 
setUp()197 void Test::setUp() {
198     // For whatever reason, on W32 it does not work to create/destroy a fresh
199     // component context for each test in Test::setUp/tearDown; therefore, a
200     // single component context is used for all tests and destroyed in the last
201     // pseudo-test "finish":
202     if (!m_context.is()) {
203         m_context = cppu::defaultBootstrap_InitialComponentContext();
204     }
205 }
206 
finish()207 void Test::finish() {
208     css::uno::Reference< css::lang::XComponent >(
209         m_context, css::uno::UNO_QUERY_THROW)->dispose();
210 }
211 
testNormalizedMakeRelative()212 void Test::testNormalizedMakeRelative() {
213     auto ucb(css::ucb::UniversalContentBroker::create(m_context));
214     ucb->registerContentProvider(new Provider, "test", true);
215     ucb->registerContentProvider(
216         css::uno::Reference<css::ucb::XContentProvider>(
217             m_context->getServiceManager()->createInstanceWithContext(
218                 "com.sun.star.comp.ucb.FileProvider", m_context),
219             css::uno::UNO_QUERY_THROW),
220         "file", true);
221     struct Data {
222         char const * base;
223         char const * absolute;
224         char const * relative;
225     };
226     static Data const tests[] = {
227         { "hierarchical:/", "mailto:def@a.b.c.", "mailto:def@a.b.c." },
228         { "hierarchical:/", "a/b/c", "a/b/c" },
229         { "hierarchical:/a", "hierarchical:/a/b/c?d#e", "/a/b/c?d#e" },
230         { "hierarchical:/a/", "hierarchical:/a/b/c?d#e", "b/c?d#e" },
231         { "test:/0/0/a", "test:/0/b", "../b" },
232         { "test:/1/1/a", "test:/1/b", "../b" },
233         { "test:/2/2//a", "test:/2/b", "../../b" },
234         { "test:/0a/b", "test:/0A/c#f", "c#f" },
235         { "file:///usr/bin/nonex1/nonex2",
236           "file:///usr/bin/nonex1/nonex3/nonex4", "nonex3/nonex4" },
237         { "file:///usr/bin/nonex1/nonex2#fragmentA",
238           "file:///usr/bin/nonex1/nonex3/nonex4#fragmentB",
239           "nonex3/nonex4#fragmentB" },
240         { "file:///usr/nonex1/nonex2", "file:///usr/nonex3", "../nonex3" },
241         { "file:///c:/windows/nonex1", "file:///c:/nonex2", "../nonex2" },
242 #if defined(_WIN32)
243         { "file:///c:/nonex1/nonex2", "file:///C:/nonex1/nonex3/nonex4",
244           "nonex3/nonex4" }
245 #endif
246     };
247     for (std::size_t i = 0; i < SAL_N_ELEMENTS(tests); ++i) {
248         css::uno::Reference< css::uri::XUriReference > ref(
249             URIHelper::normalizedMakeRelative(
250                 m_context, OUString::createFromAscii(tests[i].base),
251                 OUString::createFromAscii(tests[i].absolute)));
252         bool ok = tests[i].relative == nullptr
253             ? !ref.is()
254             : ref.is() && ref->getUriReference().equalsAscii(tests[i].relative);
255         OString msg;
256         if (!ok) {
257             OStringBuffer buf;
258             buf.append('<');
259             buf.append(tests[i].base);
260             buf.append(">, <");
261             buf.append(tests[i].absolute);
262             buf.append(">: ");
263             if (ref.is()) {
264                 buf.append('<');
265                 buf.append(
266                     OUStringToOString(
267                         ref->getUriReference(), RTL_TEXTENCODING_UTF8));
268                 buf.append('>');
269             } else {
270                 buf.append("none");
271             }
272             buf.append(" instead of ");
273             if (tests[i].relative == nullptr) {
274                 buf.append("none");
275             } else {
276                 buf.append('<');
277                 buf.append(tests[i].relative);
278                 buf.append('>');
279             }
280             msg = buf.makeStringAndClear();
281         }
282         CPPUNIT_ASSERT_MESSAGE(msg.getStr(), ok);
283     }
284 }
285 
testFindFirstURLInText()286 void Test::testFindFirstURLInText() {
287     struct Data {
288         char const * input;
289         char const * result;
290         sal_Int32 begin;
291         sal_Int32 end;
292     };
293     static Data const tests[] = {
294         { "...ftp://bla.bla.bla/blubber/...",
295           "ftp://bla.bla.bla/blubber/", 3, 29 },
296         { "..\\ftp://bla.bla.bla/blubber/...", nullptr, 0, 0 },
297         { "..\\ftp:\\\\bla.bla.bla\\blubber/...",
298 //Sync with tools/source/fsys/urlobj.cxx and changeScheme
299 #ifdef LINUX
300           "smb://bla.bla.bla/blubber%2F", 7, 29 },
301 #else
302           "file://bla.bla.bla/blubber%2F", 7, 29 },
303 #endif
304         { "http://sun.com", "http://sun.com/", 0, 14 },
305         { "http://sun.com/", "http://sun.com/", 0, 15 },
306         { "http://www.xerox.com@www.pcworld.com/go/3990332.htm", nullptr, 0, 0 },
307         { "ftp://www.xerox.com@www.pcworld.com/go/3990332.htm",
308           "ftp://www.xerox.com@www.pcworld.com/go/3990332.htm", 0, 50 },
309         { "Version.1.2.3", nullptr, 0, 0 },
310         { "Version:1.2.3", nullptr, 0, 0 },
311         { "a.b.c", nullptr, 0, 0 },
312         { "file:///a|...", "file:///a:", 0, 10 },
313         { "file:///a||...", "file:///a%7C%7C", 0, 11 },
314         { "file:///a|/bc#...", "file:///a:/bc", 0, 13 },
315         { "file:///a|/bc#de...", "file:///a:/bc#de", 0, 16 },
316         { "abc.def.ghi,ftp.xxx.yyy/zzz...", "ftp://ftp.xxx.yyy/zzz", 12, 27 },
317         { "abc.def.ghi,Ftp.xxx.yyy/zzz...", "ftp://Ftp.xxx.yyy/zzz", 12, 27 },
318         { "abc.def.ghi,www.xxx.yyy...", "http://www.xxx.yyy/", 12, 23 },
319         { "abc.def.ghi,wwww.xxx.yyy...", nullptr, 0, 0 },
320         { "abc.def.ghi,wWW.xxx.yyy...", "http://wWW.xxx.yyy/", 12, 23 },
321         { "Bla {mailto.me@abc.def.g.h.i}...",
322           "mailto:%7Bmailto.me@abc.def.g.h.i", 4, 28 },
323         { "abc@def@ghi", nullptr, 0, 0 },
324         { "lala@sun.com", "mailto:lala@sun.com", 0, 12 },
325         { "1lala@sun.com", "mailto:1lala@sun.com", 0, 13 },
326         { "aaa_bbb@xxx.yy", "mailto:aaa_bbb@xxx.yy", 0, 14 },
327         { "{a:\\bla/bla/bla...}", "file:///a:/bla/bla/bla", 1, 15 },
328         { "#b:/c/d#e#f#", "file:///b:/c/d", 1, 7 },
329         { "a:/", "file:///a:/", 0, 3 },
330         { ".component:", nullptr, 0, 0 },
331         { ".uno:", nullptr, 0, 0 },
332         { "cid:", nullptr, 0, 0 },
333         { "data:", nullptr, 0, 0 },
334         { "db:", nullptr, 0, 0 },
335         { "file:", nullptr, 0, 0 },
336         { "ftp:", nullptr, 0, 0 },
337         { "http:", nullptr, 0, 0 },
338         { "https:", nullptr, 0, 0 },
339         { "imap:", nullptr, 0, 0 },
340         { "javascript:", nullptr, 0, 0 },
341         { "ldap:", nullptr, 0, 0 },
342         { "macro:", nullptr, 0, 0 },
343         { "mailto:", nullptr, 0, 0 },
344         { "news:", nullptr, 0, 0 },
345         { "out:", nullptr, 0, 0 },
346         { "pop3:", nullptr, 0, 0 },
347         { "private:", nullptr, 0, 0 },
348         { "slot:", nullptr, 0, 0 },
349         { "staroffice.component:", nullptr, 0, 0 },
350         { "staroffice.db:", nullptr, 0, 0 },
351         { "staroffice.factory:", nullptr, 0, 0 },
352         { "staroffice.helpid:", nullptr, 0, 0 },
353         { "staroffice.java:", nullptr, 0, 0 },
354         { "staroffice.macro:", nullptr, 0, 0 },
355         { "staroffice.out:", nullptr, 0, 0 },
356         { "staroffice.pop3:", nullptr, 0, 0 },
357         { "staroffice.private:", nullptr, 0, 0 },
358         { "staroffice.searchfolder:", nullptr, 0, 0 },
359         { "staroffice.slot:", nullptr, 0, 0 },
360         { "staroffice.trashcan:", nullptr, 0, 0 },
361         { "staroffice.uno:", nullptr, 0, 0 },
362         { "staroffice.vim:", nullptr, 0, 0 },
363         { "staroffice:", nullptr, 0, 0 },
364         { "vim:", nullptr, 0, 0 },
365         { "vnd.sun.star.cmd:", nullptr, 0, 0 },
366         { "vnd.sun.star.help:", nullptr, 0, 0 },
367         { "vnd.sun.star.hier:", nullptr, 0, 0 },
368         { "vnd.sun.star.pkg:", nullptr, 0, 0 },
369         { "vnd.sun.star.script:", nullptr, 0, 0 },
370         { "vnd.sun.star.webdav:", nullptr, 0, 0 },
371         { "vnd.sun.star.wfs:", nullptr, 0, 0 },
372         { "generic:path", nullptr, 0, 0 },
373         { "wfs:", nullptr, 0, 0 }
374     };
375     CharClass charClass( m_context, LanguageTag( css::lang::Locale("en", "US", "")));
376     for (std::size_t i = 0; i < SAL_N_ELEMENTS(tests); ++i) {
377         OUString input(OUString::createFromAscii(tests[i].input));
378         sal_Int32 begin = 0;
379         sal_Int32 end = input.getLength();
380         OUString result(
381             URIHelper::FindFirstURLInText(input, begin, end, charClass));
382         bool ok = tests[i].result == nullptr
383             ? (result.getLength() == 0 && begin == input.getLength()
384                && end == input.getLength())
385             : (result.equalsAscii(tests[i].result) && begin == tests[i].begin
386                && end == tests[i].end);
387         OString msg;
388         if (!ok) {
389             OStringBuffer buf;
390             buf.append('"');
391             buf.append(tests[i].input);
392             buf.append("\" -> ");
393             buf.append(tests[i].result == nullptr ? "none" : tests[i].result);
394             buf.append(" (");
395             buf.append(tests[i].begin);
396             buf.append(", ");
397             buf.append(tests[i].end);
398             buf.append(')');
399             buf.append(" != ");
400             buf.append(OUStringToOString(result, RTL_TEXTENCODING_UTF8));
401             buf.append(" (");
402             buf.append(begin);
403             buf.append(", ");
404             buf.append(end);
405             buf.append(')');
406             msg = buf.makeStringAndClear();
407         }
408         CPPUNIT_ASSERT_MESSAGE(msg.getStr(), ok);
409     }
410 }
411 
testResolveIdnaHost()412 void Test::testResolveIdnaHost() {
413     OUString input;
414 
415     input.clear();
416     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
417 
418     input = u"Foo.M\u00FCnchen.de";
419     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
420 
421     input = "foo://Muenchen.de";
422     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
423 
424     input = u"foo://-M\u00FCnchen.de";
425     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
426 
427     input = u"foo://M\u00FCnchen-.de";
428     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
429 
430     input = u"foo://xn--M\u00FCnchen.de";
431     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
432 
433     input = u"foo://xy--M\u00FCnchen.de";
434     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
435 
436     input = u"foo://.M\u00FCnchen.de";
437     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
438 
439     input = u"foo://-bar.M\u00FCnchen.de";
440     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
441 
442     input = u"foo://bar-.M\u00FCnchen.de";
443     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
444 
445     input = u"foo://xn--bar.M\u00FCnchen.de";
446     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
447 
448     input = u"foo://xy--bar.M\u00FCnchen.de";
449     CPPUNIT_ASSERT_EQUAL(input, URIHelper::resolveIdnaHost(input));
450 
451     CPPUNIT_ASSERT_EQUAL(
452         OUString(u"foo://M\u00FCnchen@xn--mnchen-3ya.de"),
453         URIHelper::resolveIdnaHost(u"foo://M\u00FCnchen@M\u00FCnchen.de"));
454 
455     CPPUNIT_ASSERT_EQUAL(
456         OUString("foo://xn--mnchen-3ya.de."),
457         URIHelper::resolveIdnaHost(u"foo://M\u00FCnchen.de."));
458 
459     CPPUNIT_ASSERT_EQUAL(
460         OUString("Foo://bar@xn--mnchen-3ya.de:123/?bar#baz"),
461         URIHelper::resolveIdnaHost(u"Foo://bar@M\u00FCnchen.de:123/?bar#baz"));
462 
463     CPPUNIT_ASSERT_EQUAL(
464         OUString("foo://xn--mnchen-3ya.de"),
465         URIHelper::resolveIdnaHost(u"foo://Mu\u0308nchen.de"));
466 
467     CPPUNIT_ASSERT_EQUAL(
468         OUString("foo://example.xn--m-eha"), URIHelper::resolveIdnaHost(u"foo://example.mü"));
469 
470     CPPUNIT_ASSERT_EQUAL(
471         OUString("foo://example.xn--m-eha:0"), URIHelper::resolveIdnaHost(u"foo://example.mü:0"));
472 
473     CPPUNIT_ASSERT_EQUAL(
474         OUString("foo://xn--e1afmkfd.xn--p1ai"), URIHelper::resolveIdnaHost(u"foo://пример.рф"));
475 
476     CPPUNIT_ASSERT_EQUAL(
477         OUString("foo://xn--e1afmkfd.xn--p1ai:0"),
478         URIHelper::resolveIdnaHost(u"foo://пример.рф:0"));
479 }
480 
481 css::uno::Reference< css::uno::XComponentContext > Test::m_context;
482 
483 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
484 
485 }
486 
487 CPPUNIT_PLUGIN_IMPLEMENT();
488 
489 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
490