LCOV - code coverage report
Current view: top level - cppuhelper/qa/unourl - cppu_unourl.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 111 118 94.1 %
Date: 2015-06-13 12:38:46 Functions: 21 22 95.5 %
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          27 :     class UrlTest : public CppUnit::TestFixture
      35             :     {
      36             :     public:
      37           1 :         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          29 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
      74             :             {
      75          28 :                 bool bValid = false;
      76             :                 try
      77             :                 {
      78             :                     cppu::UnoUrlDescriptor aDescriptor(rtl::OUString::createFromAscii(
      79          38 :                                                            aTests[i].pInput));
      80          18 :                     bValid = true;
      81             :                 }
      82          10 :                 catch (rtl::MalformedUriException &)
      83             :                 {}
      84             : 
      85          28 :                 if (aTests[i].bValid)
      86             :                 {
      87          18 :                     CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
      88             :                 }
      89             :                 else
      90             :                 {
      91          10 :                     CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
      92             :                 }
      93             :             }
      94           1 :         }
      95             : 
      96           1 :         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          19 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     123             :             {
     124          18 :                 bool bValid = false;
     125          18 :                 rtl::OUString aDescriptor;
     126             :                 try
     127             :                 {
     128             :                     aDescriptor = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
     129             :                                                              aTests[i].pInput)).
     130          18 :                         getDescriptor();
     131          18 :                     bValid = true;
     132             :                 }
     133           0 :                 catch (rtl::MalformedUriException &)
     134             :                 {}
     135             : 
     136          18 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     137          36 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
     138             :                                        aDescriptor.equalsAscii(
     139          18 :                                            aTests[i].pDescriptor));
     140          18 :             }
     141           1 :         }
     142             : 
     143             : 
     144           1 :         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          19 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     171             :             {
     172          18 :                 bool bValid = false;
     173          18 :                 rtl::OUString aName;
     174             :                 try
     175             :                 {
     176             :                     aName = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
     177          18 :                                                        aTests[i].pInput)).getName();
     178          18 :                     bValid = true;
     179             :                 }
     180           0 :                 catch (rtl::MalformedUriException &)
     181             :                 {}
     182             : 
     183          18 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     184          36 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI correctly",
     185          18 :                                        aName.equalsAscii(aTests[i].pName));
     186          18 :             }
     187           1 :         }
     188             : 
     189           1 :         void testDescriptorKey()
     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          18 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     216             :             {
     217          17 :                 bool bValid = false;
     218          17 :                 bool bPresent = false;
     219             :                 try
     220             :                 {
     221             :                     bPresent = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
     222             :                                                           aTests[i].pInput)).
     223          17 :                         hasParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
     224          17 :                     bValid = true;
     225             :                 }
     226           0 :                 catch (rtl::MalformedUriException &)
     227             :                 {}
     228             : 
     229          17 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     230          34 :                 CPPUNIT_ASSERT_MESSAGE("Failed to detect parameter correctly",
     231          17 :                                        bPresent == aTests[i].bPresent);
     232             :             }
     233           1 :         }
     234             : 
     235           1 :         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          42 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     286             :             {
     287          41 :                 bool bValid = false;
     288          41 :                 rtl::OUString aValue;
     289             :                 try
     290             :                 {
     291          82 :                     aValue = cppu::UnoUrlDescriptor(rtl::OUString::createFromAscii(
     292             :                                                         aTests[i].pInput)).
     293          41 :                         getParameter(rtl::OUString::createFromAscii(aTests[i].pKey));
     294          41 :                     bValid = true;
     295             :                 }
     296           0 :                 catch (rtl::MalformedUriException &)
     297             :                 {}
     298          41 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     299          82 :                 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
     300          41 :                                        aValue.equalsAscii(aTests[i].pValue));
     301          41 :             }
     302           1 :         }
     303             : 
     304           1 :         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          17 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     329             :             {
     330          16 :                 bool bValid = false;
     331             :                 try
     332             :                 {
     333          23 :                     cppu::UnoUrl aUrl(rtl::OUString::createFromAscii(aTests[i].pInput));
     334           9 :                     bValid = true;
     335             :                 }
     336           7 :                 catch (rtl::MalformedUriException &)
     337             :                 {}
     338             : 
     339          16 :                 if (aTests[i].bValid)
     340             :                 {
     341           9 :                     CPPUNIT_ASSERT_MESSAGE("Valid uri parsed as invalid", bValid);
     342             :                 }
     343             :                 else
     344             :                 {
     345           7 :                     CPPUNIT_ASSERT_MESSAGE("Invalid uri parsed as valid", !bValid);
     346             :                 }
     347             : 
     348             :             }
     349           1 :         }
     350             : 
     351           1 :         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           6 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     366             :             {
     367           5 :                 bool bValid = false;
     368           5 :                 rtl::OUString aConnection;
     369             :                 try
     370             :                 {
     371             :                     aConnection = cppu::UnoUrl(rtl::OUString::createFromAscii(
     372             :                                                    aTests[i].pInput)).
     373           5 :                         getConnection().getDescriptor();
     374           5 :                     bValid = true;
     375             :                 }
     376           0 :                 catch (rtl::MalformedUriException &)
     377             :                 {}
     378           5 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     379          10 :                 CPPUNIT_ASSERT_MESSAGE("Failed to get param correctly",
     380             :                                        aConnection.equalsAscii(
     381           5 :                                            aTests[i].pConnection));
     382           5 :             }
     383           1 :         }
     384             : 
     385           1 :         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           6 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     400             :             {
     401           5 :                 bool bValid = false;
     402           5 :                 rtl::OUString aProtocol;
     403             :                 try
     404             :                 {
     405             :                     aProtocol = cppu::UnoUrl(rtl::OUString::createFromAscii(
     406             :                                                  aTests[i].pInput)).
     407           5 :                         getProtocol().getDescriptor();
     408           5 :                     bValid = true;
     409             :                 }
     410           0 :                 catch (rtl::MalformedUriException &)
     411             :                 {}
     412           5 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     413          10 :                 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
     414             :                                        aProtocol.equalsAscii(
     415           5 :                                            aTests[i].pProtocol));
     416           5 :             }
     417           1 :         }
     418             : 
     419           1 :         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          10 :             for (unsigned int i = 0; i < SAL_N_ELEMENTS(aTests); ++i)
     437             :             {
     438           9 :                 bool bValid = false;
     439           9 :                 rtl::OUString aObjectName;
     440             :                 try
     441             :                 {
     442             :                     aObjectName = cppu::UnoUrl(rtl::OUString::createFromAscii(
     443           9 :                                                    aTests[i].pInput)).getObjectName();
     444           9 :                     bValid = true;
     445             :                 }
     446           0 :                 catch (rtl::MalformedUriException &)
     447             :                 {}
     448           9 :                 CPPUNIT_ASSERT_MESSAGE("Failed to parse URI", bValid);
     449          18 :                 CPPUNIT_ASSERT_MESSAGE("Failed to get protocol correctly",
     450             :                                        aObjectName.equalsAscii(
     451           9 :                                            aTests[i].pObjectName));
     452           9 :             }
     453           1 :         }
     454             : 
     455             :         // Automatic registration code
     456           2 :         CPPUNIT_TEST_SUITE(UrlTest);
     457           1 :         CPPUNIT_TEST(testDescriptorParsing);
     458           1 :         CPPUNIT_TEST(testDescriptorDescriptor);
     459           1 :         CPPUNIT_TEST(testDescriptorName);
     460           1 :         CPPUNIT_TEST(testDescriptorKey);
     461           1 :         CPPUNIT_TEST(testDescriptorValue);
     462           1 :         CPPUNIT_TEST(testUrlParsing);
     463           1 :         CPPUNIT_TEST(testUrlConnection);
     464           1 :         CPPUNIT_TEST(testUrlProtocol);
     465           1 :         CPPUNIT_TEST(testUrlObjectName);
     466           5 :         CPPUNIT_TEST_SUITE_END();
     467             :     };
     468             : } // namespace cppu_ifcontainer
     469             : 
     470           1 : CPPUNIT_TEST_SUITE_REGISTRATION(cppu_unourl::UrlTest);
     471             : 
     472           4 : CPPUNIT_PLUGIN_IMPLEMENT();
     473             : 
     474             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11