1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
53 namespace com::sun::star::ucb
{
54 class XCommandEnvironment
;
55 class XContentEventListener
;
60 // This class only implements that subset of functionality of a proper
61 // css::ucb::Content that is known to be needed here:
63 public cppu::WeakImplHelper
<
64 css::ucb::XContent
, css::ucb::XCommandProcessor
>
68 css::uno::Reference
< css::ucb::XContentIdentifier
> const & identifier
);
70 virtual css::uno::Reference
< css::ucb::XContentIdentifier
> SAL_CALL
71 getIdentifier() override
{
75 virtual OUString SAL_CALL
getContentType() override
80 virtual void SAL_CALL
addContentEventListener(
81 css::uno::Reference
< css::ucb::XContentEventListener
> const &) override
84 virtual void SAL_CALL
removeContentEventListener(
85 css::uno::Reference
< css::ucb::XContentEventListener
> const &) override
88 virtual sal_Int32 SAL_CALL
createCommandIdentifier() override
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
;
97 virtual void SAL_CALL
abort(sal_Int32
) override
{}
100 static char const m_prefix
[];
102 css::uno::Reference
< css::ucb::XContentIdentifier
> m_identifier
;
105 char const Content::m_prefix
[] = "test:";
108 css::uno::Reference
< css::ucb::XContentIdentifier
> const & identifier
):
109 m_identifier(identifier
)
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)
116 throw css::ucb::IllegalIdentifierException();
120 css::uno::Any
Content::execute(
121 css::ucb::Command
const & command
, sal_Int32
,
122 css::uno::Reference
< css::ucb::XCommandEnvironment
> const &)
124 if ( command
.Name
!= "getCasePreservingURL" )
126 throw css::uno::RuntimeException();
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());
134 for (sal_Int32 i
= RTL_CONSTASCII_LENGTH(m_prefix
); i
!= -1;) {
135 OUString
seg(uri
.getToken(0, '/', i
));
136 if (seg
.getLength() > 0) {
138 if (c
< '0' || c
> '2') {
139 throw css::uno::Exception();
148 if (uri
.endsWith("/")) {
149 uri
= uri
.copy(0, uri
.getLength() -1);
153 return css::uno::Any(uri
.toAsciiLowerCase());
156 class Provider
: public cppu::WeakImplHelper
< css::ucb::XContentProvider
> {
158 virtual css::uno::Reference
< css::ucb::XContent
> SAL_CALL
queryContent(
159 css::uno::Reference
< css::ucb::XContentIdentifier
> const & identifier
) override
161 return new Content(identifier
);
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
168 assert(id1
.is() && id2
.is());
170 id1
->getContentIdentifier().compareTo(id2
->getContentIdentifier());
174 class Test
: public CppUnit::TestFixture
{
176 virtual void setUp() override
;
180 void testNormalizedMakeRelative();
182 void testFindFirstURLInText();
184 void testFindFirstDOIInText();
186 void testResolveIdnaHost();
188 CPPUNIT_TEST_SUITE(Test
);
189 CPPUNIT_TEST(testNormalizedMakeRelative
);
190 CPPUNIT_TEST(testFindFirstURLInText
);
191 CPPUNIT_TEST(testFindFirstDOIInText
);
192 CPPUNIT_TEST(testResolveIdnaHost
);
193 CPPUNIT_TEST(finish
);
194 CPPUNIT_TEST_SUITE_END();
197 static css::uno::Reference
< css::uno::XComponentContext
> m_context
;
201 // For whatever reason, on W32 it does not work to create/destroy a fresh
202 // component context for each test in Test::setUp/tearDown; therefore, a
203 // single component context is used for all tests and destroyed in the last
204 // pseudo-test "finish":
205 if (!m_context
.is()) {
206 m_context
= cppu::defaultBootstrap_InitialComponentContext();
210 void Test::finish() {
211 css::uno::Reference
< css::lang::XComponent
>(
212 m_context
, css::uno::UNO_QUERY_THROW
)->dispose();
215 void Test::testNormalizedMakeRelative() {
216 auto ucb(css::ucb::UniversalContentBroker::create(m_context
));
217 ucb
->registerContentProvider(new Provider
, u
"test"_ustr
, true);
218 ucb
->registerContentProvider(
219 css::uno::Reference
<css::ucb::XContentProvider
>(
220 m_context
->getServiceManager()->createInstanceWithContext(
221 u
"com.sun.star.comp.ucb.FileProvider"_ustr
, m_context
),
222 css::uno::UNO_QUERY_THROW
),
226 char const * absolute
;
227 char const * relative
;
229 static Data
const tests
[] = {
230 { "hierarchical:/", "mailto:def@a.b.c.", "mailto:def@a.b.c." },
231 { "hierarchical:/", "a/b/c", "a/b/c" },
232 { "hierarchical:/a", "hierarchical:/a/b/c?d#e", "/a/b/c?d#e" },
233 { "hierarchical:/a/", "hierarchical:/a/b/c?d#e", "b/c?d#e" },
234 { "test:/0/0/a", "test:/0/b", "../b" },
235 { "test:/1/1/a", "test:/1/b", "../b" },
236 { "test:/2/2//a", "test:/2/b", "../../b" },
237 { "test:/0a/b", "test:/0A/c#f", "c#f" },
238 { "file:///usr/bin/nonex1/nonex2",
239 "file:///usr/bin/nonex1/nonex3/nonex4", "nonex3/nonex4" },
240 { "file:///usr/bin/nonex1/nonex2#fragmentA",
241 "file:///usr/bin/nonex1/nonex3/nonex4#fragmentB",
242 "nonex3/nonex4#fragmentB" },
243 { "file:///usr/nonex1/nonex2", "file:///usr/nonex3", "../nonex3" },
244 { "file:///c:/windows/nonex1", "file:///c:/nonex2", "../nonex2" },
246 { "file:///c:/nonex1/nonex2", "file:///C:/nonex1/nonex3/nonex4",
250 for (auto const[base
, absolute
, relative
] : tests
)
252 css::uno::Reference
< css::uri::XUriReference
> ref(URIHelper::normalizedMakeRelative(
253 m_context
, OUString::createFromAscii(base
), OUString::createFromAscii(absolute
)));
254 bool ok
= relative
== nullptr ? !ref
.is()
255 : ref
.is() && ref
->getUriReference().equalsAscii(relative
);
259 OStringBuffer
buf(OString::Concat("<") + base
+ ">, <" + absolute
+ ">: ");
265 ref
->getUriReference(), RTL_TEXTENCODING_UTF8
));
272 buf
.append(" instead of ");
273 if (relative
== nullptr)
279 buf
.append(OString::Concat("<") + relative
+ ">");
281 msg
= buf
.makeStringAndClear();
283 CPPUNIT_ASSERT_MESSAGE(msg
.getStr(), ok
);
287 void Test::testFindFirstURLInText() {
294 static Data
const tests
[] = {
295 { "...ftp://bla.bla.bla/blubber/...",
296 "ftp://bla.bla.bla/blubber/", 3, 29 },
297 { "..\\ftp://bla.bla.bla/blubber/...", nullptr, 0, 0 },
298 { "..\\ftp:\\\\bla.bla.bla\\blubber/...",
299 "file://bla.bla.bla/blubber%2F", 7, 29 },
300 { "http://sun.com", "http://sun.com/", 0, 14 },
301 { "http://sun.com/", "http://sun.com/", 0, 15 },
302 { "http://www.xerox.com@www.pcworld.com/go/3990332.htm", nullptr, 0, 0 },
303 { "ftp://www.xerox.com@www.pcworld.com/go/3990332.htm",
304 "ftp://www.xerox.com@www.pcworld.com/go/3990332.htm", 0, 50 },
305 { "Version.1.2.3", nullptr, 0, 0 },
306 { "Version:1.2.3", nullptr, 0, 0 },
307 { "a.b.c", nullptr, 0, 0 },
308 { "file:///a|...", "file:///a:", 0, 10 },
309 { "file:///a||...", "file:///a%7C%7C", 0, 11 },
310 { "file:///a|/bc#...", "file:///a:/bc", 0, 13 },
311 { "file:///a|/bc#de...", "file:///a:/bc#de", 0, 16 },
312 { "abc.def.ghi,ftp.xxx.yyy/zzz...", "ftp://ftp.xxx.yyy/zzz", 12, 27 },
313 { "abc.def.ghi,Ftp.xxx.yyy/zzz...", "ftp://Ftp.xxx.yyy/zzz", 12, 27 },
314 { "abc.def.ghi,www.xxx.yyy...", "http://www.xxx.yyy/", 12, 23 },
315 { "abc.def.ghi,wwww.xxx.yyy...", nullptr, 0, 0 },
316 { "abc.def.ghi,wWW.xxx.yyy...", "http://wWW.xxx.yyy/", 12, 23 },
317 { "Bla {mailto.me@abc.def.g.h.i}...",
318 "mailto:%7Bmailto.me@abc.def.g.h.i", 4, 28 },
319 { "abc@def@ghi", nullptr, 0, 0 },
320 { "lala@sun.com", "mailto:lala@sun.com", 0, 12 },
321 { "1lala@sun.com", "mailto:1lala@sun.com", 0, 13 },
322 { "aaa_bbb@xxx.yy", "mailto:aaa_bbb@xxx.yy", 0, 14 },
323 { "{a:\\bla/bla/bla...}", "file:///a:/bla/bla/bla", 1, 15 },
324 { "#b:/c/d#e#f#", "file:///b:/c/d", 1, 7 },
325 { "a:/", "file:///a:/", 0, 3 },
326 { "http://sun.com/R_(l_a)", "http://sun.com/R_(l_a)", 0, 22 },
327 { ".component:", nullptr, 0, 0 },
328 { ".uno:", nullptr, 0, 0 },
329 { "cid:", nullptr, 0, 0 },
330 { "data:", nullptr, 0, 0 },
331 { "db:", nullptr, 0, 0 },
332 { "file:", nullptr, 0, 0 },
333 { "ftp:", nullptr, 0, 0 },
334 { "http:", nullptr, 0, 0 },
335 { "https:", nullptr, 0, 0 },
336 { "imap:", nullptr, 0, 0 },
337 { "javascript:", nullptr, 0, 0 },
338 { "ldap:", nullptr, 0, 0 },
339 { "macro:", nullptr, 0, 0 },
340 { "mailto:", nullptr, 0, 0 },
341 { "news:", nullptr, 0, 0 },
342 { "out:", nullptr, 0, 0 },
343 { "pop3:", nullptr, 0, 0 },
344 { "private:", nullptr, 0, 0 },
345 { "slot:", nullptr, 0, 0 },
346 { "staroffice.component:", nullptr, 0, 0 },
347 { "staroffice.db:", nullptr, 0, 0 },
348 { "staroffice.factory:", nullptr, 0, 0 },
349 { "staroffice.helpid:", nullptr, 0, 0 },
350 { "staroffice.java:", nullptr, 0, 0 },
351 { "staroffice.macro:", nullptr, 0, 0 },
352 { "staroffice.out:", nullptr, 0, 0 },
353 { "staroffice.pop3:", nullptr, 0, 0 },
354 { "staroffice.private:", nullptr, 0, 0 },
355 { "staroffice.searchfolder:", nullptr, 0, 0 },
356 { "staroffice.slot:", nullptr, 0, 0 },
357 { "staroffice.trashcan:", nullptr, 0, 0 },
358 { "staroffice.uno:", nullptr, 0, 0 },
359 { "staroffice.vim:", nullptr, 0, 0 },
360 { "staroffice:", nullptr, 0, 0 },
361 { "vim:", nullptr, 0, 0 },
362 { "vnd.sun.star.cmd:", nullptr, 0, 0 },
363 { "vnd.sun.star.help:", nullptr, 0, 0 },
364 { "vnd.sun.star.hier:", nullptr, 0, 0 },
365 { "vnd.sun.star.pkg:", nullptr, 0, 0 },
366 { "vnd.sun.star.script:", nullptr, 0, 0 },
367 { "vnd.sun.star.webdav:", nullptr, 0, 0 },
368 { "vnd.sun.star.wfs:", nullptr, 0, 0 },
369 { "generic:path", nullptr, 0, 0 },
370 { "wfs:", nullptr, 0, 0 }
372 CharClass
charClass( m_context
, LanguageTag( css::lang::Locale(u
"en"_ustr
, u
"US"_ustr
, u
""_ustr
)));
373 for (auto const[pInput
, pResult
, nBegin
, nEnd
] : tests
)
375 OUString
input(OUString::createFromAscii(pInput
));
377 sal_Int32 end
= input
.getLength();
378 OUString
result(URIHelper::FindFirstURLInText(input
, begin
, end
, charClass
));
379 bool ok
= pResult
== nullptr
380 ? (result
.getLength() == 0 && begin
== input
.getLength()
381 && end
== input
.getLength())
382 : (result
.equalsAscii(pResult
) && begin
== nBegin
&& end
== nEnd
);
387 buf
.append(OString::Concat("\"") + pInput
+ "\" -> ");
388 buf
.append(pResult
== nullptr ? "none" : pResult
);
389 buf
.append(" (" + OString::number(nBegin
) + ", " + OString::number(nEnd
)
392 + OUStringToOString(result
, RTL_TEXTENCODING_UTF8
) + " ("
393 + OString::number(begin
) + ", " + OString::number(end
) +")");
394 msg
= buf
.makeStringAndClear();
396 CPPUNIT_ASSERT_MESSAGE(msg
.getStr(), ok
);
400 void Test::testFindFirstDOIInText() {
407 static Data
const tests
[] = {
408 { "doi:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with only digits
409 { "Doi:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
410 { "DoI:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
411 { "DOI:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
412 { "dOI:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
413 { "dOi:10.1000/182", "https://doi.org/10.1000/182", 0, 15 }, // valid doi suffix with some of the first three characters being capitalized
414 { "doi:10.1038/nature03001", "https://doi.org/10.1038/nature03001", 0, 23 }, // valid doi suffix with alphanumeric characters
415 { "doi:10.1093/ajae/aaq063", "https://doi.org/10.1093/ajae/aaq063", 0, 23 }, // valid doi suffix with multiple slash
416 { "doi:10.1016/S0735-1097(98)00347-7", "https://doi.org/10.1016/S0735-1097(98)00347-7", 0, 33 }, // valid doi suffix with characters apart from alphanumeric
417 { "doi:10.109/ajae/aaq063", nullptr, 0, 0 }, // # of digits after doi;10. is not between 4 and 9
418 { "doi:10.1234567890/ajae/aaq063", nullptr, 0, 0 }, // # of digits after doi;10. is not between 4 and 9
419 { "doi:10.1093/ajae/aaq063/", nullptr, 0, 0 }, // nothing after slash
420 { "doi:10.1093", nullptr, 0, 0 }, // no slash
421 { "doi:11.1093/ajae/aaq063", nullptr, 0, 0 }, // doesn't begin with doi:10.
423 CharClass
charClass( m_context
, LanguageTag( css::lang::Locale(u
"en"_ustr
, u
"US"_ustr
, u
""_ustr
)));
424 for (auto const[pInput
, pResult
, nBegin
, nEnd
] : tests
)
426 OUString
input(OUString::createFromAscii(pInput
));
428 sal_Int32 end
= input
.getLength();
430 URIHelper::FindFirstDOIInText(input
, begin
, end
, charClass
));
431 bool ok
= pResult
== nullptr
432 ? (result
.getLength() == 0 && begin
== input
.getLength() && end
== input
.getLength())
433 : (result
.equalsAscii(pResult
) && begin
== nBegin
&& end
== nEnd
);
438 buf
.append(OString::Concat("\"") + pInput
+ "\" -> ");
439 buf
.append(pResult
== nullptr ? "none" : pResult
);
440 buf
.append(" (" + OString::number(nBegin
) + ", " + OString::number(nEnd
)
443 + OUStringToOString(result
, RTL_TEXTENCODING_UTF8
) + " ("
444 + OString::number(begin
) + ", " + OString::number(end
) +")");
445 msg
= buf
.makeStringAndClear();
447 CPPUNIT_ASSERT_MESSAGE(msg
.getStr(), ok
);
451 void Test::testResolveIdnaHost() {
455 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
457 input
= u
"Foo.M\u00FCnchen.de"_ustr
;
458 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
460 input
= "foo://Muenchen.de";
461 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
463 input
= u
"foo://-M\u00FCnchen.de"_ustr
;
464 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
466 input
= u
"foo://M\u00FCnchen-.de"_ustr
;
467 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
469 input
= u
"foo://xn--M\u00FCnchen.de"_ustr
;
470 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
472 input
= u
"foo://xy--M\u00FCnchen.de"_ustr
;
473 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
475 input
= u
"foo://.M\u00FCnchen.de"_ustr
;
476 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
478 input
= u
"foo://-bar.M\u00FCnchen.de"_ustr
;
479 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
481 input
= u
"foo://bar-.M\u00FCnchen.de"_ustr
;
482 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
484 input
= u
"foo://xn--bar.M\u00FCnchen.de"_ustr
;
485 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
487 input
= u
"foo://xy--bar.M\u00FCnchen.de"_ustr
;
488 CPPUNIT_ASSERT_EQUAL(input
, URIHelper::resolveIdnaHost(input
));
490 CPPUNIT_ASSERT_EQUAL(
491 u
"foo://M\u00FCnchen@xn--mnchen-3ya.de"_ustr
,
492 URIHelper::resolveIdnaHost(u
"foo://M\u00FCnchen@M\u00FCnchen.de"_ustr
));
494 CPPUNIT_ASSERT_EQUAL(
495 u
"foo://xn--mnchen-3ya.de."_ustr
,
496 URIHelper::resolveIdnaHost(u
"foo://M\u00FCnchen.de."_ustr
));
498 CPPUNIT_ASSERT_EQUAL(
499 u
"Foo://bar@xn--mnchen-3ya.de:123/?bar#baz"_ustr
,
500 URIHelper::resolveIdnaHost(u
"Foo://bar@M\u00FCnchen.de:123/?bar#baz"_ustr
));
502 CPPUNIT_ASSERT_EQUAL(
503 u
"foo://xn--mnchen-3ya.de"_ustr
,
504 URIHelper::resolveIdnaHost(u
"foo://Mu\u0308nchen.de"_ustr
));
506 CPPUNIT_ASSERT_EQUAL(
507 u
"foo://example.xn--m-eha"_ustr
, URIHelper::resolveIdnaHost(u
"foo://example.mü"_ustr
));
509 CPPUNIT_ASSERT_EQUAL(
510 u
"foo://example.xn--m-eha:0"_ustr
, URIHelper::resolveIdnaHost(u
"foo://example.mü:0"_ustr
));
512 CPPUNIT_ASSERT_EQUAL(
513 u
"foo://xn--e1afmkfd.xn--p1ai"_ustr
, URIHelper::resolveIdnaHost(u
"foo://пример.рф"_ustr
));
515 CPPUNIT_ASSERT_EQUAL(
516 u
"foo://xn--e1afmkfd.xn--p1ai:0"_ustr
,
517 URIHelper::resolveIdnaHost(u
"foo://пример.рф:0"_ustr
));
520 css::uno::Reference
< css::uno::XComponentContext
> Test::m_context
;
522 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
526 CPPUNIT_PLUGIN_IMPLEMENT();
528 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */