1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <testshl/simpleheader.hxx>
29 
30 #include "cppuhelper/unourl.hxx"
31 #include "rtl/malformeduriexception.hxx"
32 #include "rtl/strbuf.hxx"
33 #include "rtl/string.h"
34 #include "rtl/textenc.h"
35 #include "rtl/ustring.hxx"
36 #include "sal/types.h"
37 
38 namespace cppu_unourl
39 {
40     class UrlTest : public CppUnit::TestFixture
41     {
42     public:
43         void testDescriptorParsing()
44         {
45             struct Test
46             {
47                 char const * pInput;
48                 bool bValid;
49             };
50             static Test const aTests[]
51                 = { { "", false },
52                     { "abc", true },
53                     { "Abc", true },
54                     { "aBC", true },
55                     { "ABC", true },
56                     { "1abc", true },
57                     { "123", true },
58                     { "abc-1", false },
59                     { "ab%63", false },
60                     { "abc,", false },
61                     { "abc,def=", true },
62                     { "abc,Def=", true },
63                     { "abc,DEF=", true },
64                     { "abc,1def=", true },
65                     { "abc,123=", true },
66                     { "abc,def-1=", false },
67                     { "abc,def", false },
68                     { "abc,def=xxx,def=xxx", false },
69                     { "abc,def=xxx,ghi=xxx", true },
70                     { "abc,,def=xxx", false },
71                     { "abc,def=xxx,,ghi=xxx", false },
72                     { "abc,def=xxx,ghi=xxx,", false },
73                     { "abc,def=%", true },
74                     { "abc,def=%1", true },
75                     { "abc,def=%00", true },
76                     { "abc,def=%22", true },
77                     { "abc,def=\"", true },
78                     { "abc,def=%ed%a0%80", true } };
79             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
80             {
81                 bool bValid = false;
82                 try
83                 {
84                     cppu::UnoUrlDescriptor aDescriptor(rtl::OUString::createFromAscii(
85                                                            aTests[i].pInput));
86                     bValid = true;
87                 }
88                 catch (rtl::MalformedUriException &)
89                 {}
90 
91                 if (aTests[i].bValid)
92                 {
93                     CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
94                 }
95                 else
96                 {
97                     CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
98                 }
99             }
100         }
101 
102         void testDescriptorDescriptor()
103         {
104             struct Test
105             {
106                 char const * pInput;
107                 char const * pDescriptor;
108             };
109             static Test const aTests[]
110                 = {{ "abc", "abc" },
111                    { "Abc", "Abc" },
112                    { "aBC", "aBC" },
113                    { "ABC", "ABC" },
114                    { "1abc", "1abc" },
115                    { "123", "123" },
116                    { "abc,def=", "abc,def=" },
117                    { "abc,Def=", "abc,Def=" },
118                    { "abc,DEF=", "abc,DEF=" },
119                    { "abc,1def=", "abc,1def=" },
120                    { "abc,123=", "abc,123=" },
121                    { "abc,def=xxx,ghi=xxx", "abc,def=xxx,ghi=xxx" },
122                    { "abc,def=%", "abc,def=%" },
123                    { "abc,def=%1", "abc,def=%1" },
124                    { "abc,def=%00", "abc,def=%00" },
125                    { "abc,def=%22", "abc,def=%22" },
126                    { "abc,def=\"", "abc,def=\"" },
127                    { "abc,def=%ed%a0%80", "abc,def=%ed%a0%80" } };
128             bool bResult = true;
129             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
130             {
131                 bool bValid = false;
132                 rtl::OUString aDescriptor;
133                 try
134                 {
135                     aDescriptor = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
136                                                              aTests[i].pInput)).
137                         getDescriptor();
138                     bValid = true;
139                 }
140                 catch (rtl::MalformedUriException &)
141                 {}
142 
143                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
144                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
145                                        aDescriptor.equalsAscii(
146                                            aTests[i].pDescriptor));
147             }
148         }
149 
150 
151         void testDescriptorName()
152         {
153             struct Test
154             {
155                 char const * pInput;
156                 char const * pName;
157             };
158             static Test const aTests[]
159                 = { { "abc", "abc" },
160                     { "Abc", "abc" },
161                     { "aBC", "abc" },
162                     { "ABC", "abc" },
163                     { "1abc", "1abc" },
164                     { "123", "123" },
165                     { "abc,def=", "abc" },
166                     { "abc,Def=", "abc" },
167                     { "abc,DEF=", "abc" },
168                     { "abc,1def=", "abc" },
169                     { "abc,123=", "abc" },
170                     { "abc,def=xxx,ghi=xxx", "abc" },
171                     { "abc,def=%", "abc" },
172                     { "abc,def=%1", "abc" },
173                     { "abc,def=%00", "abc" },
174                     { "abc,def=%22", "abc" },
175                     { "abc,def=\"", "abc" },
176                     { "abc,def=%ed%a0%80", "abc" } };
177             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
178             {
179                 bool bValid = false;
180                 rtl::OUString aName;
181                 try
182                 {
183                     aName = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
184                                                        aTests[i].pInput)).getName();
185                     bValid = true;
186                 }
187                 catch (rtl::MalformedUriException &)
188                 {}
189 
190                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
191                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
192                                        aName.equalsAscii(aTests[i].pName));
193             }
194         }
195 
196         void testDescriptorKey(void)
197         {
198             struct Test
199             {
200                 char const * pInput;
201                 char const * pKey;
202                 bool bPresent;
203             };
204             static Test const aTests[]
205                 = { { "abc", "abc", false },
206                     { "abc", "def", false },
207                     { "1abc", "def", false },
208                     { "123", "def", false },
209                     { "abc,def=", "abc", false },
210                     { "abc,def=", "def", true },
211                     { "abc,def=", "defg", false },
212                     { "abc,def=", "de", false },
213                     { "abc,def=", "ghi", false },
214                     { "abc,Def=", "def", true },
215                     { "abc,Def=", "Def", true },
216                     { "abc,Def=", "dEF", true },
217                     { "abc,Def=", "DEF", true },
218                     { "abc,def=xxx,ghi=xxx", "abc", false },
219                     { "abc,def=xxx,ghi=xxx", "def", true },
220                     { "abc,def=xxx,ghi=xxx", "ghi", true },
221                     { "abc,def=xxx,ghi=xxx", "jkl", false } };
222             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
223             {
224                 bool bValid = false;
225                 bool bPresent = false;
226                 try
227                 {
228                     bPresent = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
229                                                           aTests[i].pInput)).
230                         hasParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
231                     bValid = true;
232                 }
233                 catch (rtl::MalformedUriException &)
234                 {}
235 
236                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
237                 CPPUNIT_ASSERT_MESSAGE("Failed to detect parameter correctly",
238                                        bPresent == aTests[i].bPresent);
239             }
240         }
241 
242         void testDescriptorValue()
243         {
244             struct Test
245             {
246                 char const * pInput;
247                 char const * pKey;
248                 char const * pValue;
249             };
250             static Test const aTests[]
251                 = { { "abc", "abc", "" },
252                     { "abc", "def", "" },
253                     { "1abc", "def", "" },
254                     { "123", "def", "" },
255                     { "abc,def=", "abc", "" },
256                     { "abc,def=", "def", "" },
257                     { "abc,def=", "defg", "" },
258                     { "abc,def=", "de", "" },
259                     { "abc,def=", "ghi", "" },
260                     { "abc,Def=", "def", "" },
261                     { "abc,Def=", "Def", "" },
262                     { "abc,Def=", "dEF", "" },
263                     { "abc,Def=", "DEF", "" },
264                     { "abc,def=xxx,ghi=xxx", "abc", "" },
265                     { "abc,def=xxx,ghi=xxx", "def", "xxx" },
266                     { "abc,def=xxx,ghi=xxx", "ghi", "xxx" },
267                     { "abc,def=xxx,ghi=xxx", "jkl", "" },
268                     { "abc,def=%", "def", "%" },
269                     { "abc,def=%1", "def", "%1" },
270                     { "abc,def=%22", "def", "\"" },
271                     { "abc,def=\"", "def", "\"" },
272                     { "abc,def=abc", "def", "abc" },
273                     { "abc,def=Abc", "def", "Abc" },
274                     { "abc,def=aBC", "def", "aBC" },
275                     { "abc,def=ABC", "def", "ABC" },
276                     { "abc,def=%,ghi=", "def", "%" },
277                     { "abc,def=%1,ghi=", "def", "%1" },
278                     { "abc,def=%22,ghi=", "def", "\"" },
279                     { "abc,def=\",ghi=", "def", "\"" },
280                     { "abc,def=abc,ghi=", "def", "abc" },
281                     { "abc,def=Abc,ghi=", "def", "Abc" },
282                     { "abc,def=aBC,ghi=", "def", "aBC" },
283                     { "abc,def=ABC,ghi=", "def", "ABC" },
284                     { "abc,abc=,def=%", "def", "%" },
285                     { "abc,abc=,def=%1", "def", "%1" },
286                     { "abc,abc=,def=%22", "def", "\"" },
287                     { "abc,abc=,def=\"", "def", "\"" },
288                     { "abc,abc=,def=abc", "def", "abc" },
289                     { "abc,abc=,def=Abc", "def", "Abc" },
290                     { "abc,abc=,def=aBC", "def", "aBC" },
291                     { "abc,abc=,def=ABC", "def", "ABC" } };
292             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
293             {
294                 bool bValid = false;
295                 rtl::OUString aValue;
296                 try
297                 {
298                     aValue = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
299                                                         aTests[i].pInput)).
300                         getParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
301                     bValid = true;
302                 }
303                 catch (rtl::MalformedUriException &)
304                 {}
305                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
306                 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
307                                        aValue.equalsAscii(aTests[i].pValue));
308             }
309         }
310 
311         void testUrlParsing()
312         {
313             struct Test
314             {
315                 char const * pInput;
316                 bool bValid;
317             };
318             static Test const aTests[]
319                 = { { "", false },
320                     { "abc", false },
321                     { "uno", false },
322                     { "uno:", false },
323                     { "uno:abc;def;ghi", true },
324                     { "Uno:abc;def;ghi", true },
325                     { "uNO:abc;def;ghi", true },
326                     { "UNO:abc;def;ghi", true },
327                     { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", true },
328                     { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx,;ghi", false },
329                     { "uno:abc;def;", false },
330                     { "uno:abc;def;a", true },
331                     { "uno:abc;def;A", true },
332                     { "uno:abc;def;1", true },
333                     { "uno:abc;def;$&+,/:=?@", true },
334                     { "uno:abc;def;%24&+,/:=?@", false } };
335             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
336             {
337                 bool bValid = false;
338                 try
339                 {
340                     cppu::UnoUrl aUrl(rtl::OUString::createFromAscii(aTests[i].pInput));
341                     bValid = true;
342                 }
343                 catch (rtl::MalformedUriException &)
344                 {}
345 
346                 if (aTests[i].bValid)
347                 {
348                     CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
349                 }
350                 else
351                 {
352                     CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
353                 }
354 
355             }
356         }
357 
358         void testUrlConnection()
359         {
360             struct Test
361             {
362                 char const * pInput;
363                 char const * pConnection;
364             };
365             static Test const aTests[]
366                 = { { "uno:abc;def;ghi", "abc" },
367                     { "uno:Abc;def;ghi", "Abc" },
368                     { "uno:aBC;def;ghi", "aBC" },
369                     { "uno:ABC;def;ghi", "ABC" },
370                     { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
371                       "abc,def=xxx,ghi=xxx" } };
372             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
373             {
374                 bool bValid = false;
375                 rtl::OUString aConnection;
376                 try
377                 {
378                     aConnection = cppu::UnoUrl(rtl::OUString::createFromAscii(
379                                                    aTests[i].pInput)).
380                         getConnection().getDescriptor();
381                     bValid = true;
382                 }
383                 catch (rtl::MalformedUriException &)
384                 {}
385                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
386                 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
387                                        aConnection.equalsAscii(
388                                            aTests[i].pConnection));
389             }
390         }
391 
392         void testUrlProtocol()
393         {
394             struct Test
395             {
396                 char const * pInput;
397                 char const * pProtocol;
398             };
399             static Test const aTests[]
400                 = { { "uno:abc;def;ghi", "def" },
401                     { "uno:abc;Def;ghi", "Def" },
402                     { "uno:abc;dEF;ghi", "dEF" },
403                     { "uno:abc;DEF;ghi", "DEF" },
404                     { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi",
405                       "def,ghi=xxx,jkl=xxx" } };
406             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
407             {
408                 bool bValid = false;
409                 rtl::OUString aProtocol;
410                 try
411                 {
412                     aProtocol = cppu::UnoUrl(rtl::OUString::createFromAscii(
413                                                  aTests[i].pInput)).
414                         getProtocol().getDescriptor();
415                     bValid = true;
416                 }
417                 catch (rtl::MalformedUriException &)
418                 {}
419                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
420                 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
421                                        aProtocol.equalsAscii(
422                                            aTests[i].pProtocol));
423             }
424         }
425 
426         void testUrlObjectName()
427         {
428             struct Test
429             {
430                 char const * pInput;
431                 char const * pObjectName;
432             };
433             static Test const aTests[]
434                 = { { "uno:abc;def;ghi", "ghi" },
435                     { "uno:abc;def;Ghi", "Ghi" },
436                     { "uno:abc;def;gHI", "gHI" },
437                     { "uno:abc;def;GHI", "GHI" },
438                     { "uno:abc,def=xxx,ghi=xxx;def,ghi=xxx,jkl=xxx;ghi", "ghi" },
439                     { "uno:abc;def;a", "a" },
440                     { "uno:abc;def;A", "A" },
441                     { "uno:abc;def;1", "1" },
442                     { "uno:abc;def;$&+,/:=?@", "$&+,/:=?@" } };
443             for (int i = 0; i < sizeof aTests / sizeof (Test); ++i)
444             {
445                 bool bValid = false;
446                 rtl::OUString aObjectName;
447                 try
448                 {
449                     aObjectName = cppu::UnoUrl(rtl::OUString::createFromAscii(
450                                                    aTests[i].pInput)).getObjectName();
451                     bValid = true;
452                 }
453                 catch (rtl::MalformedUriException &)
454                 {}
455                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
456                 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
457                                        aObjectName.equalsAscii(
458                                            aTests[i].pObjectName));
459             }
460         }
461 
462         // Automatic registration code
463         CPPUNIT_TEST_SUITE(UrlTest);
464         CPPUNIT_TEST(testDescriptorParsing);
465         CPPUNIT_TEST(testDescriptorDescriptor);
466         CPPUNIT_TEST(testDescriptorName);
467         CPPUNIT_TEST(testDescriptorKey);
468         CPPUNIT_TEST(testDescriptorValue);
469         CPPUNIT_TEST(testUrlParsing);
470         CPPUNIT_TEST(testUrlConnection);
471         CPPUNIT_TEST(testUrlProtocol);
472         CPPUNIT_TEST(testUrlObjectName);
473         CPPUNIT_TEST_SUITE_END();
474     };
475 } // namespace cppu_ifcontainer
476 
477 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(cppu_unourl::UrlTest,
478                                       "cppu_unourl");
479 
480 NOADDITIONAL;
481 
482