bump product version to 4.1.6.2
[LibreOffice.git] / cppuhelper / qa / unourl / cppu_unourl.cxx
blob10fbebeade9169f48cdabd6356aa28806a5e1c36
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 <sal/types.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <cppunit/plugin/TestPlugIn.h>
25 #include "cppuhelper/unourl.hxx"
26 #include "rtl/malformeduriexception.hxx"
27 #include "rtl/strbuf.hxx"
28 #include "rtl/string.h"
29 #include "rtl/textenc.h"
30 #include "rtl/ustring.hxx"
31 #include "sal/types.h"
33 namespace cppu_unourl
35 class UrlTest : public CppUnit::TestFixture
37 public:
38 void testDescriptorParsing()
40 struct Test
42 char const * pInput;
43 bool bValid;
45 static Test const aTests[]
46 = { { "", false },
47 { "abc", true },
48 { "Abc", true },
49 { "aBC", true },
50 { "ABC", true },
51 { "1abc", true },
52 { "123", true },
53 { "abc-1", false },
54 { "ab%63", false },
55 { "abc,", false },
56 { "abc,def=", true },
57 { "abc,Def=", true },
58 { "abc,DEF=", true },
59 { "abc,1def=", true },
60 { "abc,123=", true },
61 { "abc,def-1=", false },
62 { "abc,def", false },
63 { "abc,def=xxx,def=xxx", false },
64 { "abc,def=xxx,ghi=xxx", true },
65 { "abc,,def=xxx", false },
66 { "abc,def=xxx,,ghi=xxx", false },
67 { "abc,def=xxx,ghi=xxx,", false },
68 { "abc,def=%", true },
69 { "abc,def=%1", true },
70 { "abc,def=%00", true },
71 { "abc,def=%22", true },
72 { "abc,def=\"", true },
73 { "abc,def=%ed%a0%80", true } };
74 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
76 bool bValid = false;
77 try
79 cppu::UnoUrlDescriptor aDescriptor(rtl::OUString::createFromAscii(
80 aTests[i].pInput));
81 bValid = true;
83 catch (rtl::MalformedUriException &)
86 if (aTests[i].bValid)
88 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
90 else
92 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
97 void testDescriptorDescriptor()
99 struct Test
101 char const * pInput;
102 char const * pDescriptor;
104 static Test const aTests[]
105 = {{ "abc", "abc" },
106 { "Abc", "Abc" },
107 { "aBC", "aBC" },
108 { "ABC", "ABC" },
109 { "1abc", "1abc" },
110 { "123", "123" },
111 { "abc,def=", "abc,def=" },
112 { "abc,Def=", "abc,Def=" },
113 { "abc,DEF=", "abc,DEF=" },
114 { "abc,1def=", "abc,1def=" },
115 { "abc,123=", "abc,123=" },
116 { "abc,def=xxx,ghi=xxx", "abc,def=xxx,ghi=xxx" },
117 { "abc,def=%", "abc,def=%" },
118 { "abc,def=%1", "abc,def=%1" },
119 { "abc,def=%00", "abc,def=%00" },
120 { "abc,def=%22", "abc,def=%22" },
121 { "abc,def=\"", "abc,def=\"" },
122 { "abc,def=%ed%a0%80", "abc,def=%ed%a0%80" } };
123 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
125 bool bValid = false;
126 rtl::OUString aDescriptor;
129 aDescriptor = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
130 aTests[i].pInput)).
131 getDescriptor();
132 bValid = true;
134 catch (rtl::MalformedUriException &)
137 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
138 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
139 aDescriptor.equalsAscii(
140 aTests[i].pDescriptor));
145 void testDescriptorName()
147 struct Test
149 char const * pInput;
150 char const * pName;
152 static Test const aTests[]
153 = { { "abc", "abc" },
154 { "Abc", "abc" },
155 { "aBC", "abc" },
156 { "ABC", "abc" },
157 { "1abc", "1abc" },
158 { "123", "123" },
159 { "abc,def=", "abc" },
160 { "abc,Def=", "abc" },
161 { "abc,DEF=", "abc" },
162 { "abc,1def=", "abc" },
163 { "abc,123=", "abc" },
164 { "abc,def=xxx,ghi=xxx", "abc" },
165 { "abc,def=%", "abc" },
166 { "abc,def=%1", "abc" },
167 { "abc,def=%00", "abc" },
168 { "abc,def=%22", "abc" },
169 { "abc,def=\"", "abc" },
170 { "abc,def=%ed%a0%80", "abc" } };
171 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
173 bool bValid = false;
174 rtl::OUString aName;
177 aName = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
178 aTests[i].pInput)).getName();
179 bValid = true;
181 catch (rtl::MalformedUriException &)
184 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
185 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
186 aName.equalsAscii(aTests[i].pName));
190 void testDescriptorKey(void)
192 struct Test
194 char const * pInput;
195 char const * pKey;
196 bool bPresent;
198 static Test const aTests[]
199 = { { "abc", "abc", false },
200 { "abc", "def", false },
201 { "1abc", "def", false },
202 { "123", "def", false },
203 { "abc,def=", "abc", false },
204 { "abc,def=", "def", true },
205 { "abc,def=", "defg", false },
206 { "abc,def=", "de", false },
207 { "abc,def=", "ghi", false },
208 { "abc,Def=", "def", true },
209 { "abc,Def=", "Def", true },
210 { "abc,Def=", "dEF", true },
211 { "abc,Def=", "DEF", true },
212 { "abc,def=xxx,ghi=xxx", "abc", false },
213 { "abc,def=xxx,ghi=xxx", "def", true },
214 { "abc,def=xxx,ghi=xxx", "ghi", true },
215 { "abc,def=xxx,ghi=xxx", "jkl", false } };
216 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
218 bool bValid = false;
219 bool bPresent = false;
222 bPresent = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
223 aTests[i].pInput)).
224 hasParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
225 bValid = true;
227 catch (rtl::MalformedUriException &)
230 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
231 CPPUNIT_ASSERT_MESSAGE("Failed to detect parameter correctly",
232 bPresent == aTests[i].bPresent);
236 void testDescriptorValue()
238 struct Test
240 char const * pInput;
241 char const * pKey;
242 char const * pValue;
244 static Test const aTests[]
245 = { { "abc", "abc", "" },
246 { "abc", "def", "" },
247 { "1abc", "def", "" },
248 { "123", "def", "" },
249 { "abc,def=", "abc", "" },
250 { "abc,def=", "def", "" },
251 { "abc,def=", "defg", "" },
252 { "abc,def=", "de", "" },
253 { "abc,def=", "ghi", "" },
254 { "abc,Def=", "def", "" },
255 { "abc,Def=", "Def", "" },
256 { "abc,Def=", "dEF", "" },
257 { "abc,Def=", "DEF", "" },
258 { "abc,def=xxx,ghi=xxx", "abc", "" },
259 { "abc,def=xxx,ghi=xxx", "def", "xxx" },
260 { "abc,def=xxx,ghi=xxx", "ghi", "xxx" },
261 { "abc,def=xxx,ghi=xxx", "jkl", "" },
262 { "abc,def=%", "def", "%" },
263 { "abc,def=%1", "def", "%1" },
264 { "abc,def=%22", "def", "\"" },
265 { "abc,def=\"", "def", "\"" },
266 { "abc,def=abc", "def", "abc" },
267 { "abc,def=Abc", "def", "Abc" },
268 { "abc,def=aBC", "def", "aBC" },
269 { "abc,def=ABC", "def", "ABC" },
270 { "abc,def=%,ghi=", "def", "%" },
271 { "abc,def=%1,ghi=", "def", "%1" },
272 { "abc,def=%22,ghi=", "def", "\"" },
273 { "abc,def=\",ghi=", "def", "\"" },
274 { "abc,def=abc,ghi=", "def", "abc" },
275 { "abc,def=Abc,ghi=", "def", "Abc" },
276 { "abc,def=aBC,ghi=", "def", "aBC" },
277 { "abc,def=ABC,ghi=", "def", "ABC" },
278 { "abc,abc=,def=%", "def", "%" },
279 { "abc,abc=,def=%1", "def", "%1" },
280 { "abc,abc=,def=%22", "def", "\"" },
281 { "abc,abc=,def=\"", "def", "\"" },
282 { "abc,abc=,def=abc", "def", "abc" },
283 { "abc,abc=,def=Abc", "def", "Abc" },
284 { "abc,abc=,def=aBC", "def", "aBC" },
285 { "abc,abc=,def=ABC", "def", "ABC" } };
286 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
288 bool bValid = false;
289 rtl::OUString aValue;
292 aValue = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
293 aTests[i].pInput)).
294 getParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
295 bValid = true;
297 catch (rtl::MalformedUriException &)
299 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
300 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
301 aValue.equalsAscii(aTests[i].pValue));
305 void testUrlParsing()
307 struct Test
309 char const * pInput;
310 bool bValid;
312 static Test const aTests[]
313 = { { "", false },
314 { "abc", false },
315 { "uno", false },
316 { "uno:", false },
317 { "uno:abc;def;ghi", true },
318 { "Uno:abc;def;ghi", true },
319 { "uNO:abc;def;ghi", true },
320 { "UNO:abc;def;ghi", true },
321 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", true },
322 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx,;ghi", false },
323 { "uno:abc;def;", false },
324 { "uno:abc;def;a", true },
325 { "uno:abc;def;A", true },
326 { "uno:abc;def;1", true },
327 { "uno:abc;def;$&+,/:=?@", true },
328 { "uno:abc;def;%24&+,/:=?@", false } };
329 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
331 bool bValid = false;
334 cppu::UnoUrl aUrl(rtl::OUString::createFromAscii(aTests[i].pInput));
335 bValid = true;
337 catch (rtl::MalformedUriException &)
340 if (aTests[i].bValid)
342 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
344 else
346 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
352 void testUrlConnection()
354 struct Test
356 char const * pInput;
357 char const * pConnection;
359 static Test const aTests[]
360 = { { "uno:abc;def;ghi", "abc" },
361 { "uno:Abc;def;ghi", "Abc" },
362 { "uno:aBC;def;ghi", "aBC" },
363 { "uno:ABC;def;ghi", "ABC" },
364 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
365 "abc,def=xxx,ghi=xxx" } };
366 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
368 bool bValid = false;
369 rtl::OUString aConnection;
372 aConnection = cppu::UnoUrl(rtl::OUString::createFromAscii(
373 aTests[i].pInput)).
374 getConnection().getDescriptor();
375 bValid = true;
377 catch (rtl::MalformedUriException &)
379 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
380 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
381 aConnection.equalsAscii(
382 aTests[i].pConnection));
386 void testUrlProtocol()
388 struct Test
390 char const * pInput;
391 char const * pProtocol;
393 static Test const aTests[]
394 = { { "uno:abc;def;ghi", "def" },
395 { "uno:abc;Def;ghi", "Def" },
396 { "uno:abc;dEF;ghi", "dEF" },
397 { "uno:abc;DEF;ghi", "DEF" },
398 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
399 "def,ghi=xxx,jkl=xxx" } };
400 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
402 bool bValid = false;
403 rtl::OUString aProtocol;
406 aProtocol = cppu::UnoUrl(rtl::OUString::createFromAscii(
407 aTests[i].pInput)).
408 getProtocol().getDescriptor();
409 bValid = true;
411 catch (rtl::MalformedUriException &)
413 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
414 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
415 aProtocol.equalsAscii(
416 aTests[i].pProtocol));
420 void testUrlObjectName()
422 struct Test
424 char const * pInput;
425 char const * pObjectName;
427 static Test const aTests[]
428 = { { "uno:abc;def;ghi", "ghi" },
429 { "uno:abc;def;Ghi", "Ghi" },
430 { "uno:abc;def;gHI", "gHI" },
431 { "uno:abc;def;GHI", "GHI" },
432 { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", "ghi" },
433 { "uno:abc;def;a", "a" },
434 { "uno:abc;def;A", "A" },
435 { "uno:abc;def;1", "1" },
436 { "uno:abc;def;$&+,/:=?@", "$&+,/:=?@" } };
437 for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
439 bool bValid = false;
440 rtl::OUString aObjectName;
443 aObjectName = cppu::UnoUrl(rtl::OUString::createFromAscii(
444 aTests[i].pInput)).getObjectName();
445 bValid = true;
447 catch (rtl::MalformedUriException &)
449 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
450 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
451 aObjectName.equalsAscii(
452 aTests[i].pObjectName));
456 // Automatic registration code
457 CPPUNIT_TEST_SUITE(UrlTest);
458 CPPUNIT_TEST(testDescriptorParsing);
459 CPPUNIT_TEST(testDescriptorDescriptor);
460 CPPUNIT_TEST(testDescriptorName);
461 CPPUNIT_TEST(testDescriptorKey);
462 CPPUNIT_TEST(testDescriptorValue);
463 CPPUNIT_TEST(testUrlParsing);
464 CPPUNIT_TEST(testUrlConnection);
465 CPPUNIT_TEST(testUrlProtocol);
466 CPPUNIT_TEST(testUrlObjectName);
467 CPPUNIT_TEST_SUITE_END();
469 } // namespace cppu_ifcontainer
471 CPPUNIT_TEST_SUITE_REGISTRATION(cppu_unourl::UrlTest);
473 CPPUNIT_PLUGIN_IMPLEMENT();
475 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */