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