Avoid potential negative array index access to cached text.
[LibreOffice.git] / cppuhelper / qa / unourl / cppu_unourl.cxx
blob01261724828b2418e55973b94f18dd9dbe09b6fc
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/ustring.hxx>
27 #include <sal/types.h>
29 namespace cppu_unourl
31 class UrlTest : public CppUnit::TestFixture
33 public:
34 void testDescriptorParsing()
36 struct Test
38 char const * pInput;
39 bool bValid;
41 static Test const aTests[]
42 = { { "", false },
43 { "abc", true },
44 { "Abc", true },
45 { "aBC", true },
46 { "ABC", true },
47 { "1abc", true },
48 { "123", true },
49 { "abc-1", false },
50 { "ab%63", false },
51 { "abc,", false },
52 { "abc,def=", true },
53 { "abc,Def=", true },
54 { "abc,DEF=", true },
55 { "abc,1def=", true },
56 { "abc,123=", true },
57 { "abc,def-1=", false },
58 { "abc,def", 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 < std::size(aTests); ++i)
72 bool bValid = false;
73 try
75 cppu::UnoUrlDescriptor aDescriptor(OUString::createFromAscii(
76 aTests[i].pInput));
77 (void)aDescriptor;
78 bValid = true;
80 catch (rtl::MalformedUriException &)
83 if (aTests[i].bValid)
85 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
87 else
89 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
94 void testDescriptorDescriptor()
96 struct Test
98 char const * pInput;
99 char const * pDescriptor;
101 static Test const aTests[]
102 = {{ "abc", "abc" },
103 { "Abc", "Abc" },
104 { "aBC", "aBC" },
105 { "ABC", "ABC" },
106 { "1abc", "1abc" },
107 { "123", "123" },
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 < std::size(aTests); ++i)
122 bool bValid = false;
123 OUString aDescriptor;
126 aDescriptor = cppu::UnoUrlDescriptor(OUString::createFromAscii(
127 aTests[i].pInput)).
128 getDescriptor();
129 bValid = true;
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()
144 struct Test
146 char const * pInput;
147 char const * pName;
149 static Test const aTests[]
150 = { { "abc", "abc" },
151 { "Abc", "abc" },
152 { "aBC", "abc" },
153 { "ABC", "abc" },
154 { "1abc", "1abc" },
155 { "123", "123" },
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 < std::size(aTests); ++i)
170 bool bValid = false;
171 OUString aName;
174 aName = cppu::UnoUrlDescriptor(OUString::createFromAscii(
175 aTests[i].pInput)).getName();
176 bValid = true;
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()
189 struct Test
191 char const * pInput;
192 char const * pKey;
193 bool bPresent;
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 < std::size(aTests); ++i)
215 bool bValid = false;
216 bool bPresent = false;
219 bPresent = cppu::UnoUrlDescriptor(OUString::createFromAscii(
220 aTests[i].pInput)).
221 hasParameter(OUString::createFromAscii(aTests[i].pKey));
222 bValid = true;
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()
235 struct Test
237 char const * pInput;
238 char const * pKey;
239 char const * pValue;
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 < std::size(aTests); ++i)
285 bool bValid = false;
286 OUString aValue;
289 aValue = cppu::UnoUrlDescriptor(OUString::createFromAscii(
290 aTests[i].pInput)).
291 getParameter(OUString::createFromAscii(aTests[i].pKey));
292 bValid = true;
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()
304 struct Test
306 char const * pInput;
307 bool bValid;
309 static Test const aTests[]
310 = { { "", false },
311 { "abc", false },
312 { "uno", false },
313 { "uno:", false },
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 < std::size(aTests); ++i)
328 bool bValid = false;
331 cppu::UnoUrl aUrl(OUString::createFromAscii(aTests[i].pInput));
332 (void)aUrl;
333 bValid = true;
335 catch (rtl::MalformedUriException &)
338 if (aTests[i].bValid)
340 CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
342 else
344 CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
350 void testUrlConnection()
352 struct Test
354 char const * pInput;
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 < std::size(aTests); ++i)
366 bool bValid = false;
367 OUString aConnection;
370 aConnection = cppu::UnoUrl(OUString::createFromAscii(
371 aTests[i].pInput)).
372 getConnection().getDescriptor();
373 bValid = true;
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()
386 struct Test
388 char const * pInput;
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 < std::size(aTests); ++i)
400 bool bValid = false;
401 OUString aProtocol;
404 aProtocol = cppu::UnoUrl(OUString::createFromAscii(
405 aTests[i].pInput)).
406 getProtocol().getDescriptor();
407 bValid = true;
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()
420 struct Test
422 char const * pInput;
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 < std::size(aTests); ++i)
437 bool bValid = false;
438 OUString aObjectName;
441 aObjectName = cppu::UnoUrl(OUString::createFromAscii(
442 aTests[i].pInput)).getObjectName();
443 bValid = true;
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: */