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 .
20 #include <cppunit/TestFixture.h>
21 #include <cppunit/extensions/HelperMacros.h>
22 #include <cppunit/plugin/TestPlugIn.h>
24 #include <cppuhelper/unourl.hxx>
25 #include <rtl/malformeduriexception.hxx>
26 #include <rtl/ustring.hxx>
27 #include <sal/types.h>
31 class UrlTest
: public CppUnit::TestFixture
34 void testDescriptorParsing()
41 static Test
const aTests
[]
55 { "abc,1def=", true },
57 { "abc,def-1=", false },
59 { "abc,def=xxx,def=xxx", false },
60 { "abc,def=xxx,ghi=xxx", true },
61 { "abc,,def=xxx", false },
62 { "abc,def=xxx,,ghi=xxx", false },
63 { "abc,def=xxx,ghi=xxx,", false },
64 { "abc,def=%", true },
65 { "abc,def=%1", true },
66 { "abc,def=%00", true },
67 { "abc,def=%22", true },
68 { "abc,def=\"", true },
69 { "abc,def=%ed%a0%80", true } };
70 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
75 cppu::UnoUrlDescriptor
aDescriptor(OUString::createFromAscii(
80 catch (rtl::MalformedUriException
&)
85 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid
);
89 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid
);
94 void testDescriptorDescriptor()
99 char const * pDescriptor
;
101 static Test
const aTests
[]
108 { "abc,def=", "abc,def=" },
109 { "abc,Def=", "abc,Def=" },
110 { "abc,DEF=", "abc,DEF=" },
111 { "abc,1def=", "abc,1def=" },
112 { "abc,123=", "abc,123=" },
113 { "abc,def=xxx,ghi=xxx", "abc,def=xxx,ghi=xxx" },
114 { "abc,def=%", "abc,def=%" },
115 { "abc,def=%1", "abc,def=%1" },
116 { "abc,def=%00", "abc,def=%00" },
117 { "abc,def=%22", "abc,def=%22" },
118 { "abc,def=\"", "abc,def=\"" },
119 { "abc,def=%ed%a0%80", "abc,def=%ed%a0%80" } };
120 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
123 OUString aDescriptor
;
126 aDescriptor
= cppu::UnoUrlDescriptor(OUString::createFromAscii(
131 catch (rtl::MalformedUriException
&)
134 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
135 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
136 aDescriptor
.equalsAscii(
137 aTests
[i
].pDescriptor
));
142 void testDescriptorName()
149 static Test
const aTests
[]
150 = { { "abc", "abc" },
156 { "abc,def=", "abc" },
157 { "abc,Def=", "abc" },
158 { "abc,DEF=", "abc" },
159 { "abc,1def=", "abc" },
160 { "abc,123=", "abc" },
161 { "abc,def=xxx,ghi=xxx", "abc" },
162 { "abc,def=%", "abc" },
163 { "abc,def=%1", "abc" },
164 { "abc,def=%00", "abc" },
165 { "abc,def=%22", "abc" },
166 { "abc,def=\"", "abc" },
167 { "abc,def=%ed%a0%80", "abc" } };
168 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
174 aName
= cppu::UnoUrlDescriptor(OUString::createFromAscii(
175 aTests
[i
].pInput
)).getName();
178 catch (rtl::MalformedUriException
&)
181 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
182 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
183 aName
.equalsAscii(aTests
[i
].pName
));
187 void testDescriptorKey()
195 static Test
const aTests
[]
196 = { { "abc", "abc", false },
197 { "abc", "def", false },
198 { "1abc", "def", false },
199 { "123", "def", false },
200 { "abc,def=", "abc", false },
201 { "abc,def=", "def", true },
202 { "abc,def=", "defg", false },
203 { "abc,def=", "de", false },
204 { "abc,def=", "ghi", false },
205 { "abc,Def=", "def", true },
206 { "abc,Def=", "Def", true },
207 { "abc,Def=", "dEF", true },
208 { "abc,Def=", "DEF", true },
209 { "abc,def=xxx,ghi=xxx", "abc", false },
210 { "abc,def=xxx,ghi=xxx", "def", true },
211 { "abc,def=xxx,ghi=xxx", "ghi", true },
212 { "abc,def=xxx,ghi=xxx", "jkl", false } };
213 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
216 bool bPresent
= false;
219 bPresent
= cppu::UnoUrlDescriptor(OUString::createFromAscii(
221 hasParameter(OUString::createFromAscii(aTests
[i
].pKey
));
224 catch (rtl::MalformedUriException
&)
227 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
228 CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to detect parameter correctly",
229 aTests
[i
].bPresent
, bPresent
);
233 void testDescriptorValue()
241 static Test
const aTests
[]
242 = { { "abc", "abc", "" },
243 { "abc", "def", "" },
244 { "1abc", "def", "" },
245 { "123", "def", "" },
246 { "abc,def=", "abc", "" },
247 { "abc,def=", "def", "" },
248 { "abc,def=", "defg", "" },
249 { "abc,def=", "de", "" },
250 { "abc,def=", "ghi", "" },
251 { "abc,Def=", "def", "" },
252 { "abc,Def=", "Def", "" },
253 { "abc,Def=", "dEF", "" },
254 { "abc,Def=", "DEF", "" },
255 { "abc,def=xxx,ghi=xxx", "abc", "" },
256 { "abc,def=xxx,ghi=xxx", "def", "xxx" },
257 { "abc,def=xxx,ghi=xxx", "ghi", "xxx" },
258 { "abc,def=xxx,ghi=xxx", "jkl", "" },
259 { "abc,def=%", "def", "%" },
260 { "abc,def=%1", "def", "%1" },
261 { "abc,def=%22", "def", "\"" },
262 { "abc,def=\"", "def", "\"" },
263 { "abc,def=abc", "def", "abc" },
264 { "abc,def=Abc", "def", "Abc" },
265 { "abc,def=aBC", "def", "aBC" },
266 { "abc,def=ABC", "def", "ABC" },
267 { "abc,def=%,ghi=", "def", "%" },
268 { "abc,def=%1,ghi=", "def", "%1" },
269 { "abc,def=%22,ghi=", "def", "\"" },
270 { "abc,def=\",ghi=", "def", "\"" },
271 { "abc,def=abc,ghi=", "def", "abc" },
272 { "abc,def=Abc,ghi=", "def", "Abc" },
273 { "abc,def=aBC,ghi=", "def", "aBC" },
274 { "abc,def=ABC,ghi=", "def", "ABC" },
275 { "abc,abc=,def=%", "def", "%" },
276 { "abc,abc=,def=%1", "def", "%1" },
277 { "abc,abc=,def=%22", "def", "\"" },
278 { "abc,abc=,def=\"", "def", "\"" },
279 { "abc,abc=,def=abc", "def", "abc" },
280 { "abc,abc=,def=Abc", "def", "Abc" },
281 { "abc,abc=,def=aBC", "def", "aBC" },
282 { "abc,abc=,def=ABC", "def", "ABC" } };
283 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
289 aValue
= cppu::UnoUrlDescriptor(OUString::createFromAscii(
291 getParameter(OUString::createFromAscii(aTests
[i
].pKey
));
294 catch (rtl::MalformedUriException
&)
296 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
297 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
298 aValue
.equalsAscii(aTests
[i
].pValue
));
302 void testUrlParsing()
309 static Test
const aTests
[]
314 { "uno:abc;def;ghi", true },
315 { "Uno:abc;def;ghi", true },
316 { "uNO:abc;def;ghi", true },
317 { "UNO:abc;def;ghi", true },
318 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", true },
319 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx,;ghi", false },
320 { "uno:abc;def;", false },
321 { "uno:abc;def;a", true },
322 { "uno:abc;def;A", true },
323 { "uno:abc;def;1", true },
324 { "uno:abc;def;$&+,/:=?@", true },
325 { "uno:abc;def;%24&+,/:=?@", false } };
326 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
331 cppu::UnoUrl
aUrl(OUString::createFromAscii(aTests
[i
].pInput
));
335 catch (rtl::MalformedUriException
&)
338 if (aTests
[i
].bValid
)
340 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid
);
344 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid
);
350 void testUrlConnection()
355 char const * pConnection
;
357 static Test
const aTests
[]
358 = { { "uno:abc;def;ghi", "abc" },
359 { "uno:Abc;def;ghi", "Abc" },
360 { "uno:aBC;def;ghi", "aBC" },
361 { "uno:ABC;def;ghi", "ABC" },
362 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
363 "abc,def=xxx,ghi=xxx" } };
364 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
367 OUString aConnection
;
370 aConnection
= cppu::UnoUrl(OUString::createFromAscii(
372 getConnection().getDescriptor();
375 catch (rtl::MalformedUriException
&)
377 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
378 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
379 aConnection
.equalsAscii(
380 aTests
[i
].pConnection
));
384 void testUrlProtocol()
389 char const * pProtocol
;
391 static Test
const aTests
[]
392 = { { "uno:abc;def;ghi", "def" },
393 { "uno:abc;Def;ghi", "Def" },
394 { "uno:abc;dEF;ghi", "dEF" },
395 { "uno:abc;DEF;ghi", "DEF" },
396 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
397 "def,ghi=xxx,jkl=xxx" } };
398 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
404 aProtocol
= cppu::UnoUrl(OUString::createFromAscii(
406 getProtocol().getDescriptor();
409 catch (rtl::MalformedUriException
&)
411 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
412 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
413 aProtocol
.equalsAscii(
414 aTests
[i
].pProtocol
));
418 void testUrlObjectName()
423 char const * pObjectName
;
425 static Test
const aTests
[]
426 = { { "uno:abc;def;ghi", "ghi" },
427 { "uno:abc;def;Ghi", "Ghi" },
428 { "uno:abc;def;gHI", "gHI" },
429 { "uno:abc;def;GHI", "GHI" },
430 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", "ghi" },
431 { "uno:abc;def;a", "a" },
432 { "uno:abc;def;A", "A" },
433 { "uno:abc;def;1", "1" },
434 { "uno:abc;def;$&+,/:=?@", "$&+,/:=?@" } };
435 for (size_t i
= 0; i
< SAL_N_ELEMENTS(aTests
); ++i
)
438 OUString aObjectName
;
441 aObjectName
= cppu::UnoUrl(OUString::createFromAscii(
442 aTests
[i
].pInput
)).getObjectName();
445 catch (rtl::MalformedUriException
&)
447 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid
);
448 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
449 aObjectName
.equalsAscii(
450 aTests
[i
].pObjectName
));
454 // Automatic registration code
455 CPPUNIT_TEST_SUITE(UrlTest
);
456 CPPUNIT_TEST(testDescriptorParsing
);
457 CPPUNIT_TEST(testDescriptorDescriptor
);
458 CPPUNIT_TEST(testDescriptorName
);
459 CPPUNIT_TEST(testDescriptorKey
);
460 CPPUNIT_TEST(testDescriptorValue
);
461 CPPUNIT_TEST(testUrlParsing
);
462 CPPUNIT_TEST(testUrlConnection
);
463 CPPUNIT_TEST(testUrlProtocol
);
464 CPPUNIT_TEST(testUrlObjectName
);
465 CPPUNIT_TEST_SUITE_END();
467 } // namespace cppu_ifcontainer
469 CPPUNIT_TEST_SUITE_REGISTRATION(cppu_unourl::UrlTest
);
471 CPPUNIT_PLUGIN_IMPLEMENT();
473 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */