1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <cppunit/TestFixture.h>
21 #include <cppunit/extensions/HelperMacros.h>
22 #include <cppunit/plugin/TestPlugIn.h>
23 
24 #include <cppuhelper/unourl.hxx>
25 #include <rtl/malformeduriexception.hxx>
26 #include <rtl/ustring.hxx>
27 #include <sal/types.h>
28 
29 namespace cppu_unourl
30 {
31     class UrlTest : public CppUnit::TestFixture
32     {
33     public:
testDescriptorParsing()34         void testDescriptorParsing()
35         {
36             struct Test
37             {
38                 char const * pInput;
39                 bool bValid;
40             };
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 < SAL_N_ELEMENTS(aTests); ++i)
71             {
72                 bool bValid = false;
73                 try
74                 {
75                     cppu::UnoUrlDescriptor aDescriptor(OUString::createFromAscii(
76                                                            aTests[i].pInput));
77                     (void)aDescriptor;
78                     bValid = true;
79                 }
80                 catch (rtl::MalformedUriException &)
81                 {}
82 
83                 if (aTests[i].bValid)
84                 {
85                     CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
86                 }
87                 else
88                 {
89                     CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
90                 }
91             }
92         }
93 
testDescriptorDescriptor()94         void testDescriptorDescriptor()
95         {
96             struct Test
97             {
98                 char const * pInput;
99                 char const * pDescriptor;
100             };
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 < SAL_N_ELEMENTS(aTests); ++i)
121             {
122                 bool bValid = false;
123                 OUString aDescriptor;
124                 try
125                 {
126                     aDescriptor = cppu::UnoUrlDescriptor(OUString::createFromAscii(
127                                                              aTests[i].pInput)).
128                         getDescriptor();
129                     bValid = true;
130                 }
131                 catch (rtl::MalformedUriException &)
132                 {}
133 
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));
138             }
139         }
140 
141 
testDescriptorName()142         void testDescriptorName()
143         {
144             struct Test
145             {
146                 char const * pInput;
147                 char const * pName;
148             };
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 < SAL_N_ELEMENTS(aTests); ++i)
169             {
170                 bool bValid = false;
171                 OUString aName;
172                 try
173                 {
174                     aName = cppu::UnoUrlDescriptor(OUString::createFromAscii(
175                                                        aTests[i].pInput)).getName();
176                     bValid = true;
177                 }
178                 catch (rtl::MalformedUriException &)
179                 {}
180 
181                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
182                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
183                                        aName.equalsAscii(aTests[i].pName));
184             }
185         }
186 
testDescriptorKey()187         void testDescriptorKey()
188         {
189             struct Test
190             {
191                 char const * pInput;
192                 char const * pKey;
193                 bool bPresent;
194             };
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)
214             {
215                 bool bValid = false;
216                 bool bPresent = false;
217                 try
218                 {
219                     bPresent = cppu::UnoUrlDescriptor(OUString::createFromAscii(
220                                                           aTests[i].pInput)).
221                         hasParameter(OUString::createFromAscii(aTests[i].pKey));
222                     bValid = true;
223                 }
224                 catch (rtl::MalformedUriException &)
225                 {}
226 
227                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
228                 CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to detect parameter correctly",
229                                        aTests[i].bPresent, bPresent);
230             }
231         }
232 
testDescriptorValue()233         void testDescriptorValue()
234         {
235             struct Test
236             {
237                 char const * pInput;
238                 char const * pKey;
239                 char const * pValue;
240             };
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)
284             {
285                 bool bValid = false;
286                 OUString aValue;
287                 try
288                 {
289                     aValue = cppu::UnoUrlDescriptor(OUString::createFromAscii(
290                                                         aTests[i].pInput)).
291                         getParameter(OUString::createFromAscii(aTests[i].pKey));
292                     bValid = true;
293                 }
294                 catch (rtl::MalformedUriException &)
295                 {}
296                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
297                 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
298                                        aValue.equalsAscii(aTests[i].pValue));
299             }
300         }
301 
testUrlParsing()302         void testUrlParsing()
303         {
304             struct Test
305             {
306                 char const * pInput;
307                 bool bValid;
308             };
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 < SAL_N_ELEMENTS(aTests); ++i)
327             {
328                 bool bValid = false;
329                 try
330                 {
331                     cppu::UnoUrl aUrl(OUString::createFromAscii(aTests[i].pInput));
332                     (void)aUrl;
333                     bValid = true;
334                 }
335                 catch (rtl::MalformedUriException &)
336                 {}
337 
338                 if (aTests[i].bValid)
339                 {
340                     CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
341                 }
342                 else
343                 {
344                     CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
345                 }
346 
347             }
348         }
349 
testUrlConnection()350         void testUrlConnection()
351         {
352             struct Test
353             {
354                 char const * pInput;
355                 char const * pConnection;
356             };
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)
365             {
366                 bool bValid = false;
367                 OUString aConnection;
368                 try
369                 {
370                     aConnection = cppu::UnoUrl(OUString::createFromAscii(
371                                                    aTests[i].pInput)).
372                         getConnection().getDescriptor();
373                     bValid = true;
374                 }
375                 catch (rtl::MalformedUriException &)
376                 {}
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));
381             }
382         }
383 
testUrlProtocol()384         void testUrlProtocol()
385         {
386             struct Test
387             {
388                 char const * pInput;
389                 char const * pProtocol;
390             };
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)
399             {
400                 bool bValid = false;
401                 OUString aProtocol;
402                 try
403                 {
404                     aProtocol = cppu::UnoUrl(OUString::createFromAscii(
405                                                  aTests[i].pInput)).
406                         getProtocol().getDescriptor();
407                     bValid = true;
408                 }
409                 catch (rtl::MalformedUriException &)
410                 {}
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));
415             }
416         }
417 
testUrlObjectName()418         void testUrlObjectName()
419         {
420             struct Test
421             {
422                 char const * pInput;
423                 char const * pObjectName;
424             };
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)
436             {
437                 bool bValid = false;
438                 OUString aObjectName;
439                 try
440                 {
441                     aObjectName = cppu::UnoUrl(OUString::createFromAscii(
442                                                    aTests[i].pInput)).getObjectName();
443                     bValid = true;
444                 }
445                 catch (rtl::MalformedUriException &)
446                 {}
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));
451             }
452         }
453 
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();
466     };
467 } // namespace cppu_ifcontainer
468 
469 CPPUNIT_TEST_SUITE_REGISTRATION(cppu_unourl::UrlTest);
470 
471 CPPUNIT_PLUGIN_IMPLEMENT();
472 
473 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
474