LCOV - code coverage report
Current view: top level - libreoffice/cppuhelper/qa/unourl - cppu_unourl.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 110 117 94.0 %
Date: 2012-12-27 Functions: 18 19 94.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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 <sal/types.h>
      21             : #include <cppunit/TestFixture.h>
      22             : #include <cppunit/extensions/HelperMacros.h>
      23             : #include <cppunit/plugin/TestPlugIn.h>
      24             : 
      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"
      32             : 
      33             : namespace cppu_unourl
      34             : {
      35          27 :     class UrlTest : public CppUnit::TestFixture
      36             :     {
      37             :     public:
      38           1 :         void testDescriptorParsing()
      39             :         {
      40             :             struct Test
      41             :             {
      42             :                 char const * pInput;
      43             :                 bool bValid;
      44             :             };
      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          29 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
      75             :             {
      76          28 :                 bool bValid = false;
      77             :                 try
      78             :                 {
      79             :                     cppu::UnoUrlDescriptor aDescriptor(rtl::OUString::createFromAscii(
      80          38 :                                                            aTests[i].pInput));
      81          18 :                     bValid = true;
      82             :                 }
      83          10 :                 catch (rtl::MalformedUriException &)
      84             :                 {}
      85             : 
      86          28 :                 if (aTests[i].bValid)
      87             :                 {
      88          18 :                     CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
      89             :                 }
      90             :                 else
      91             :                 {
      92          10 :                     CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
      93             :                 }
      94             :             }
      95           1 :         }
      96             : 
      97           1 :         void testDescriptorDescriptor()
      98             :         {
      99             :             struct Test
     100             :             {
     101             :                 char const * pInput;
     102             :                 char const * pDescriptor;
     103             :             };
     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          19 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     124             :             {
     125          18 :                 bool bValid = false;
     126          18 :                 rtl::OUString aDescriptor;
     127             :                 try
     128             :                 {
     129             :                     aDescriptor = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
     130             :                                                              aTests[i].pInput)).
     131          18 :                         getDescriptor();
     132          18 :                     bValid = true;
     133             :                 }
     134           0 :                 catch (rtl::MalformedUriException &)
     135             :                 {}
     136             : 
     137          18 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     138          36 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
     139             :                                        aDescriptor.equalsAscii(
     140          18 :                                            aTests[i].pDescriptor));
     141          18 :             }
     142           1 :         }
     143             : 
     144             : 
     145           1 :         void testDescriptorName()
     146             :         {
     147             :             struct Test
     148             :             {
     149             :                 char const * pInput;
     150             :                 char const * pName;
     151             :             };
     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          19 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     172             :             {
     173          18 :                 bool bValid = false;
     174          18 :                 rtl::OUString aName;
     175             :                 try
     176             :                 {
     177             :                     aName = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
     178          18 :                                                        aTests[i].pInput)).getName();
     179          18 :                     bValid = true;
     180             :                 }
     181           0 :                 catch (rtl::MalformedUriException &)
     182             :                 {}
     183             : 
     184          18 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     185          36 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
     186          18 :                                        aName.equalsAscii(aTests[i].pName));
     187          18 :             }
     188           1 :         }
     189             : 
     190           1 :         void testDescriptorKey(void)
     191             :         {
     192             :             struct Test
     193             :             {
     194             :                 char const * pInput;
     195             :                 char const * pKey;
     196             :                 bool bPresent;
     197             :             };
     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          18 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     217             :             {
     218          17 :                 bool bValid = false;
     219          17 :                 bool bPresent = false;
     220             :                 try
     221             :                 {
     222             :                     bPresent = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
     223             :                                                           aTests[i].pInput)).
     224          17 :                         hasParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
     225          17 :                     bValid = true;
     226             :                 }
     227           0 :                 catch (rtl::MalformedUriException &)
     228             :                 {}
     229             : 
     230          17 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     231          34 :                 CPPUNIT_ASSERT_MESSAGE("Failed to detect parameter correctly",
     232          17 :                                        bPresent == aTests[i].bPresent);
     233             :             }
     234           1 :         }
     235             : 
     236           1 :         void testDescriptorValue()
     237             :         {
     238             :             struct Test
     239             :             {
     240             :                 char const * pInput;
     241             :                 char const * pKey;
     242             :                 char const * pValue;
     243             :             };
     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          42 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     287             :             {
     288          41 :                 bool bValid = false;
     289          41 :                 rtl::OUString aValue;
     290             :                 try
     291             :                 {
     292             :                     aValue = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
     293             :                                                         aTests[i].pInput)).
     294          41 :                         getParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
     295          41 :                     bValid = true;
     296             :                 }
     297           0 :                 catch (rtl::MalformedUriException &)
     298             :                 {}
     299          41 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     300          82 :                 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
     301          41 :                                        aValue.equalsAscii(aTests[i].pValue));
     302          41 :             }
     303           1 :         }
     304             : 
     305           1 :         void testUrlParsing()
     306             :         {
     307             :             struct Test
     308             :             {
     309             :                 char const * pInput;
     310             :                 bool bValid;
     311             :             };
     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          17 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     330             :             {
     331          16 :                 bool bValid = false;
     332             :                 try
     333             :                 {
     334          23 :                     cppu::UnoUrl aUrl(rtl::OUString::createFromAscii(aTests[i].pInput));
     335           9 :                     bValid = true;
     336             :                 }
     337           7 :                 catch (rtl::MalformedUriException &)
     338             :                 {}
     339             : 
     340          16 :                 if (aTests[i].bValid)
     341             :                 {
     342           9 :                     CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
     343             :                 }
     344             :                 else
     345             :                 {
     346           7 :                     CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
     347             :                 }
     348             : 
     349             :             }
     350           1 :         }
     351             : 
     352           1 :         void testUrlConnection()
     353             :         {
     354             :             struct Test
     355             :             {
     356             :                 char const * pInput;
     357             :                 char const * pConnection;
     358             :             };
     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           6 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     367             :             {
     368           5 :                 bool bValid = false;
     369           5 :                 rtl::OUString aConnection;
     370             :                 try
     371             :                 {
     372             :                     aConnection = cppu::UnoUrl(rtl::OUString::createFromAscii(
     373             :                                                    aTests[i].pInput)).
     374           5 :                         getConnection().getDescriptor();
     375           5 :                     bValid = true;
     376             :                 }
     377           0 :                 catch (rtl::MalformedUriException &)
     378             :                 {}
     379           5 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     380          10 :                 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
     381             :                                        aConnection.equalsAscii(
     382           5 :                                            aTests[i].pConnection));
     383           5 :             }
     384           1 :         }
     385             : 
     386           1 :         void testUrlProtocol()
     387             :         {
     388             :             struct Test
     389             :             {
     390             :                 char const * pInput;
     391             :                 char const * pProtocol;
     392             :             };
     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           6 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     401             :             {
     402           5 :                 bool bValid = false;
     403           5 :                 rtl::OUString aProtocol;
     404             :                 try
     405             :                 {
     406             :                     aProtocol = cppu::UnoUrl(rtl::OUString::createFromAscii(
     407             :                                                  aTests[i].pInput)).
     408           5 :                         getProtocol().getDescriptor();
     409           5 :                     bValid = true;
     410             :                 }
     411           0 :                 catch (rtl::MalformedUriException &)
     412             :                 {}
     413           5 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     414          10 :                 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
     415             :                                        aProtocol.equalsAscii(
     416           5 :                                            aTests[i].pProtocol));
     417           5 :             }
     418           1 :         }
     419             : 
     420           1 :         void testUrlObjectName()
     421             :         {
     422             :             struct Test
     423             :             {
     424             :                 char const * pInput;
     425             :                 char const * pObjectName;
     426             :             };
     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          10 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     438             :             {
     439           9 :                 bool bValid = false;
     440           9 :                 rtl::OUString aObjectName;
     441             :                 try
     442             :                 {
     443             :                     aObjectName = cppu::UnoUrl(rtl::OUString::createFromAscii(
     444           9 :                                                    aTests[i].pInput)).getObjectName();
     445           9 :                     bValid = true;
     446             :                 }
     447           0 :                 catch (rtl::MalformedUriException &)
     448             :                 {}
     449           9 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     450          18 :                 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
     451             :                                        aObjectName.equalsAscii(
     452           9 :                                            aTests[i].pObjectName));
     453           9 :             }
     454           1 :         }
     455             : 
     456             :         // Automatic registration code
     457           2 :         CPPUNIT_TEST_SUITE(UrlTest);
     458           1 :         CPPUNIT_TEST(testDescriptorParsing);
     459           1 :         CPPUNIT_TEST(testDescriptorDescriptor);
     460           1 :         CPPUNIT_TEST(testDescriptorName);
     461           1 :         CPPUNIT_TEST(testDescriptorKey);
     462           1 :         CPPUNIT_TEST(testDescriptorValue);
     463           1 :         CPPUNIT_TEST(testUrlParsing);
     464           1 :         CPPUNIT_TEST(testUrlConnection);
     465           1 :         CPPUNIT_TEST(testUrlProtocol);
     466           1 :         CPPUNIT_TEST(testUrlObjectName);
     467           2 :         CPPUNIT_TEST_SUITE_END();
     468             :     };
     469             : } // namespace cppu_ifcontainer
     470             : 
     471           1 : CPPUNIT_TEST_SUITE_REGISTRATION(cppu_unourl::UrlTest);
     472             : 
     473           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     474             : 
     475             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10