Branch libreoffice-5-0-4
[LibreOffice.git] / cppuhelper / qa / unourl / cppu_unourl.cxx
bloba54cf7cf40c9052f493a4abde1b310bcf126edbc
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/.
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/strbuf.hxx>
27 #include <rtl/string.h>
28 #include <rtl/textenc.h>
29 #include <rtl/ustring.hxx>
30 #include <sal/types.h>
32 namespace cppu_unourl
34 class UrlTest : public CppUnit::TestFixture
36 public:
37 void testDescriptorParsing()
39 struct Test
41 char const * pInput;
42 bool bValid;
44 static Test const aTests[]
45 = { { "", false },
46 { "abc", true },
47 { "Abc", true },
48 { "aBC", true },
49 { "ABC", true },
50 { "1abc", true },
51 { "123", true },
52 { "abc-1", false },
53 { "ab%63", false },
54 { "abc,", false },
55 { "abc,def=", true },
56 { "abc,Def=", true },
57 { "abc,DEF=", true },
58 { "abc,1def=", true },
59 { "abc,123=", true },
60 { "abc,def-1=", false },
61 { "abc,def", false },
62 { "abc,def=xxx,def=xxx", false },
63 { "abc,def=xxx,ghi=xxx", true },
64 { "abc,,def=xxx", false },
65 { "abc,def=xxx,,ghi=xxx", false },
66 { "abc,def=xxx,ghi=xxx,", false },
67 { "abc,def=%", true },
68 { "abc,def=%1", true },
69 { "abc,def=%00", true },
70 { "abc,def=%22", true },
71 { "abc,def=\"", true },
72 { "abc,def=%ed%a0%80", true } };
73 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
75 bool bValid = false;
76 try
78 cppu::UnoUrlDescriptor aDescriptor(rtl::OUString::createFromAscii(
79 aTests[i].pInput));
80 bValid = true;
82 catch (rtl::MalformedUriException &)
85 if (aTests[i].bValid)
87 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
89 else
91 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
96 void testDescriptorDescriptor()
98 struct Test
100 char const * pInput;
101 char const * pDescriptor;
103 static Test const aTests[]
104 = {{ "abc", "abc" },
105 { "Abc", "Abc" },
106 { "aBC", "aBC" },
107 { "ABC", "ABC" },
108 { "1abc", "1abc" },
109 { "123", "123" },
110 { "abc,def=", "abc,def=" },
111 { "abc,Def=", "abc,Def=" },
112 { "abc,DEF=", "abc,DEF=" },
113 { "abc,1def=", "abc,1def=" },
114 { "abc,123=", "abc,123=" },
115 { "abc,def=xxx,ghi=xxx", "abc,def=xxx,ghi=xxx" },
116 { "abc,def=%", "abc,def=%" },
117 { "abc,def=%1", "abc,def=%1" },
118 { "abc,def=%00", "abc,def=%00" },
119 { "abc,def=%22", "abc,def=%22" },
120 { "abc,def=\"", "abc,def=\"" },
121 { "abc,def=%ed%a0%80", "abc,def=%ed%a0%80" } };
122 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
124 bool bValid = false;
125 rtl::OUString aDescriptor;
128 aDescriptor = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
129 aTests[i].pInput)).
130 getDescriptor();
131 bValid = true;
133 catch (rtl::MalformedUriException &)
136 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
137 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
138 aDescriptor.equalsAscii(
139 aTests[i].pDescriptor));
144 void testDescriptorName()
146 struct Test
148 char const * pInput;
149 char const * pName;
151 static Test const aTests[]
152 = { { "abc", "abc" },
153 { "Abc", "abc" },
154 { "aBC", "abc" },
155 { "ABC", "abc" },
156 { "1abc", "1abc" },
157 { "123", "123" },
158 { "abc,def=", "abc" },
159 { "abc,Def=", "abc" },
160 { "abc,DEF=", "abc" },
161 { "abc,1def=", "abc" },
162 { "abc,123=", "abc" },
163 { "abc,def=xxx,ghi=xxx", "abc" },
164 { "abc,def=%", "abc" },
165 { "abc,def=%1", "abc" },
166 { "abc,def=%00", "abc" },
167 { "abc,def=%22", "abc" },
168 { "abc,def=\"", "abc" },
169 { "abc,def=%ed%a0%80", "abc" } };
170 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
172 bool bValid = false;
173 rtl::OUString aName;
176 aName = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
177 aTests[i].pInput)).getName();
178 bValid = true;
180 catch (rtl::MalformedUriException &)
183 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
184 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
185 aName.equalsAscii(aTests[i].pName));
189 void testDescriptorKey()
191 struct Test
193 char const * pInput;
194 char const * pKey;
195 bool bPresent;
197 static Test const aTests[]
198 = { { "abc", "abc", false },
199 { "abc", "def", false },
200 { "1abc", "def", false },
201 { "123", "def", false },
202 { "abc,def=", "abc", false },
203 { "abc,def=", "def", true },
204 { "abc,def=", "defg", false },
205 { "abc,def=", "de", false },
206 { "abc,def=", "ghi", false },
207 { "abc,Def=", "def", true },
208 { "abc,Def=", "Def", true },
209 { "abc,Def=", "dEF", true },
210 { "abc,Def=", "DEF", true },
211 { "abc,def=xxx,ghi=xxx", "abc", false },
212 { "abc,def=xxx,ghi=xxx", "def", true },
213 { "abc,def=xxx,ghi=xxx", "ghi", true },
214 { "abc,def=xxx,ghi=xxx", "jkl", false } };
215 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
217 bool bValid = false;
218 bool bPresent = false;
221 bPresent = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
222 aTests[i].pInput)).
223 hasParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
224 bValid = true;
226 catch (rtl::MalformedUriException &)
229 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
230 CPPUNIT_ASSERT_MESSAGE("Failed to detect parameter correctly",
231 bPresent == aTests[i].bPresent);
235 void testDescriptorValue()
237 struct Test
239 char const * pInput;
240 char const * pKey;
241 char const * pValue;
243 static Test const aTests[]
244 = { { "abc", "abc", "" },
245 { "abc", "def", "" },
246 { "1abc", "def", "" },
247 { "123", "def", "" },
248 { "abc,def=", "abc", "" },
249 { "abc,def=", "def", "" },
250 { "abc,def=", "defg", "" },
251 { "abc,def=", "de", "" },
252 { "abc,def=", "ghi", "" },
253 { "abc,Def=", "def", "" },
254 { "abc,Def=", "Def", "" },
255 { "abc,Def=", "dEF", "" },
256 { "abc,Def=", "DEF", "" },
257 { "abc,def=xxx,ghi=xxx", "abc", "" },
258 { "abc,def=xxx,ghi=xxx", "def", "xxx" },
259 { "abc,def=xxx,ghi=xxx", "ghi", "xxx" },
260 { "abc,def=xxx,ghi=xxx", "jkl", "" },
261 { "abc,def=%", "def", "%" },
262 { "abc,def=%1", "def", "%1" },
263 { "abc,def=%22", "def", "\"" },
264 { "abc,def=\"", "def", "\"" },
265 { "abc,def=abc", "def", "abc" },
266 { "abc,def=Abc", "def", "Abc" },
267 { "abc,def=aBC", "def", "aBC" },
268 { "abc,def=ABC", "def", "ABC" },
269 { "abc,def=%,ghi=", "def", "%" },
270 { "abc,def=%1,ghi=", "def", "%1" },
271 { "abc,def=%22,ghi=", "def", "\"" },
272 { "abc,def=\",ghi=", "def", "\"" },
273 { "abc,def=abc,ghi=", "def", "abc" },
274 { "abc,def=Abc,ghi=", "def", "Abc" },
275 { "abc,def=aBC,ghi=", "def", "aBC" },
276 { "abc,def=ABC,ghi=", "def", "ABC" },
277 { "abc,abc=,def=%", "def", "%" },
278 { "abc,abc=,def=%1", "def", "%1" },
279 { "abc,abc=,def=%22", "def", "\"" },
280 { "abc,abc=,def=\"", "def", "\"" },
281 { "abc,abc=,def=abc", "def", "abc" },
282 { "abc,abc=,def=Abc", "def", "Abc" },
283 { "abc,abc=,def=aBC", "def", "aBC" },
284 { "abc,abc=,def=ABC", "def", "ABC" } };
285 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
287 bool bValid = false;
288 rtl::OUString aValue;
291 aValue = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
292 aTests[i].pInput)).
293 getParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
294 bValid = true;
296 catch (rtl::MalformedUriException &)
298 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
299 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
300 aValue.equalsAscii(aTests[i].pValue));
304 void testUrlParsing()
306 struct Test
308 char const * pInput;
309 bool bValid;
311 static Test const aTests[]
312 = { { "", false },
313 { "abc", false },
314 { "uno", false },
315 { "uno:", false },
316 { "uno:abc;def;ghi", true },
317 { "Uno:abc;def;ghi", true },
318 { "uNO:abc;def;ghi", true },
319 { "UNO:abc;def;ghi", true },
320 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", true },
321 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx,;ghi", false },
322 { "uno:abc;def;", false },
323 { "uno:abc;def;a", true },
324 { "uno:abc;def;A", true },
325 { "uno:abc;def;1", true },
326 { "uno:abc;def;$&+,/:=?@", true },
327 { "uno:abc;def;%24&+,/:=?@", false } };
328 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
330 bool bValid = false;
333 cppu::UnoUrl aUrl(rtl::OUString::createFromAscii(aTests[i].pInput));
334 bValid = true;
336 catch (rtl::MalformedUriException &)
339 if (aTests[i].bValid)
341 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
343 else
345 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
351 void testUrlConnection()
353 struct Test
355 char const * pInput;
356 char const * pConnection;
358 static Test const aTests[]
359 = { { "uno:abc;def;ghi", "abc" },
360 { "uno:Abc;def;ghi", "Abc" },
361 { "uno:aBC;def;ghi", "aBC" },
362 { "uno:ABC;def;ghi", "ABC" },
363 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
364 "abc,def=xxx,ghi=xxx" } };
365 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
367 bool bValid = false;
368 rtl::OUString aConnection;
371 aConnection = cppu::UnoUrl(rtl::OUString::createFromAscii(
372 aTests[i].pInput)).
373 getConnection().getDescriptor();
374 bValid = true;
376 catch (rtl::MalformedUriException &)
378 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
379 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
380 aConnection.equalsAscii(
381 aTests[i].pConnection));
385 void testUrlProtocol()
387 struct Test
389 char const * pInput;
390 char const * pProtocol;
392 static Test const aTests[]
393 = { { "uno:abc;def;ghi", "def" },
394 { "uno:abc;Def;ghi", "Def" },
395 { "uno:abc;dEF;ghi", "dEF" },
396 { "uno:abc;DEF;ghi", "DEF" },
397 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
398 "def,ghi=xxx,jkl=xxx" } };
399 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
401 bool bValid = false;
402 rtl::OUString aProtocol;
405 aProtocol = cppu::UnoUrl(rtl::OUString::createFromAscii(
406 aTests[i].pInput)).
407 getProtocol().getDescriptor();
408 bValid = true;
410 catch (rtl::MalformedUriException &)
412 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
413 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
414 aProtocol.equalsAscii(
415 aTests[i].pProtocol));
419 void testUrlObjectName()
421 struct Test
423 char const * pInput;
424 char const * pObjectName;
426 static Test const aTests[]
427 = { { "uno:abc;def;ghi", "ghi" },
428 { "uno:abc;def;Ghi", "Ghi" },
429 { "uno:abc;def;gHI", "gHI" },
430 { "uno:abc;def;GHI", "GHI" },
431 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", "ghi" },
432 { "uno:abc;def;a", "a" },
433 { "uno:abc;def;A", "A" },
434 { "uno:abc;def;1", "1" },
435 { "uno:abc;def;$&+,/:=?@", "$&+,/:=?@" } };
436 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
438 bool bValid = false;
439 rtl::OUString aObjectName;
442 aObjectName = cppu::UnoUrl(rtl::OUString::createFromAscii(
443 aTests[i].pInput)).getObjectName();
444 bValid = true;
446 catch (rtl::MalformedUriException &)
448 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
449 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
450 aObjectName.equalsAscii(
451 aTests[i].pObjectName));
455 // Automatic registration code
456 CPPUNIT_TEST_SUITE(UrlTest);
457 CPPUNIT_TEST(testDescriptorParsing);
458 CPPUNIT_TEST(testDescriptorDescriptor);
459 CPPUNIT_TEST(testDescriptorName);
460 CPPUNIT_TEST(testDescriptorKey);
461 CPPUNIT_TEST(testDescriptorValue);
462 CPPUNIT_TEST(testUrlParsing);
463 CPPUNIT_TEST(testUrlConnection);
464 CPPUNIT_TEST(testUrlProtocol);
465 CPPUNIT_TEST(testUrlObjectName);
466 CPPUNIT_TEST_SUITE_END();
468 } // namespace cppu_ifcontainer
470 CPPUNIT_TEST_SUITE_REGISTRATION(cppu_unourl::UrlTest);
472 CPPUNIT_PLUGIN_IMPLEMENT();
474 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */