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

Generated by: LCOV version 1.10