LCOV - code coverage report
Current view: top level - tools/qa/cppunit - test_urlobj.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 127 131 96.9 %
Date: 2014-04-11 Functions: 19 20 95.0 %
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             : 
      10             : #include <string>
      11             : 
      12             : #include <sal/types.h>
      13             : #include "cppunit/TestFixture.h"
      14             : #include <cppunit/extensions/HelperMacros.h>
      15             : #include "tools/urlobj.hxx"
      16             : 
      17             : #define OUSTR_TO_STDSTR( oustr ) std::string( OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ).getStr() )
      18             : 
      19             : CPPUNIT_NS_BEGIN
      20             : 
      21             : template<> struct assertion_traits<INetProtocol>
      22             : {
      23           4 :     static bool equal( const INetProtocol& x, const INetProtocol& y )
      24             :     {
      25           4 :         return x == y;
      26             :     }
      27             : 
      28           0 :     static std::string toString( const INetProtocol& x )
      29             :     {
      30           0 :         OStringStream ost;
      31           0 :         ost << static_cast<unsigned int>(x);
      32           0 :         return ost.str();
      33             :     }
      34             : };
      35             : 
      36             : CPPUNIT_NS_END
      37             : 
      38             : namespace tools_urlobj
      39             : {
      40             : 
      41          24 :     class urlobjTest:public CppUnit::TestFixture
      42             :     {
      43             : 
      44             :       public:
      45             :         // initialise your test code values here.
      46           8 :         void setUp(  ) SAL_OVERRIDE
      47             :         {
      48           8 :         }
      49             : 
      50           8 :         void tearDown(  ) SAL_OVERRIDE
      51             :         {
      52           8 :         }
      53             : 
      54             :         // insert your test code here.
      55             :         // this is only demonstration code
      56           1 :         void urlobjTest_001(  )
      57             :         {
      58           1 :             INetURLObject aUrl( OUString( "file://10.10.1.1/sampledir/sample.file" ) );
      59             : #ifdef LINUX
      60           2 :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
      61             :                             equalsAscii
      62           1 :                             ( "smb://10.10.1.1/sampledir/sample.file" ) );
      63           1 :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_SMB );
      64             : #endif
      65             : #ifdef WIN
      66             :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
      67             :                             equalsAscii
      68             :                             ( "file://10.10.1.1/sampledir/sample.file" ) );
      69             :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_FILE );
      70             : #endif
      71           2 :             CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
      72           1 :                             equalsAscii( "10.10.1.1" ) );
      73           2 :             CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
      74           1 :                             equalsAscii( "/sampledir/sample.file" ) );
      75           2 :             CPPUNIT_ASSERT( aUrl.getName(  ).
      76           1 :                             equalsAscii( "sample.file" ) );
      77           1 :             CPPUNIT_ASSERT( aUrl.getBase(  ).equalsAscii( "sample" ) );
      78           1 :             CPPUNIT_ASSERT( aUrl.getExtension(  ).equalsAscii( "file" ) );
      79           1 :         }
      80             : 
      81           1 :         void urlobjTest_002(  )
      82             :         {
      83           1 :             INetURLObject aUrl;
      84             :             aUrl.
      85             :                 setFSysPath( OUString( "\\\\137.65.170.24\\c$\\Img0001.jpg" ),
      86           1 :                              INetURLObject::FSYS_DETECT );
      87             : #ifdef LINUX
      88           2 :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
      89           1 :                             equalsAscii( "smb://137.65.170.24/c$/Img0001.jpg" ) );
      90           1 :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_SMB );
      91             : #endif
      92             : #ifdef WIN
      93             :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
      94             :                             equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) ;
      95             :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_FILE );
      96             : #endif
      97           2 :             CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
      98           1 :                             equalsAscii( "137.65.170.24" ) );
      99           2 :             CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
     100           1 :                             equalsAscii( "/c$/Img0001.jpg" ) );
     101           2 :             CPPUNIT_ASSERT( aUrl.getName(  ).
     102           1 :                             equalsAscii( "Img0001.jpg" ) );
     103           1 :             CPPUNIT_ASSERT( aUrl.getBase(  ).equalsAscii( "Img0001" ) );
     104           1 :             CPPUNIT_ASSERT( aUrl.getExtension(  ).equalsAscii( "jpg" ) );
     105           1 :         }
     106             : 
     107             : 
     108           1 :         void urlobjTest_003(  )
     109             :         {
     110           1 :             INetURLObject aUrl;
     111             :             aUrl.
     112             :                 setFSysPath( OUString( "\\\\hive-winxp-x86\\pmladek\\test2.odt" ),
     113           1 :                              INetURLObject::FSYS_DETECT );
     114             : #ifdef LINUX
     115           2 :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
     116           1 :                             equalsAscii( "smb://hive-winxp-x86/pmladek/test2.odt" ) );
     117           1 :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_SMB );
     118             : #endif
     119             : #ifdef WIN
     120             :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
     121             :                             equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
     122             :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_FILE );
     123             : #endif
     124           2 :             CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
     125           1 :                             equalsAscii( "hive-winxp-x86" ) );
     126           2 :             CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
     127           2 :                             equalsAscii( "/pmladek/test2.odt" ) );
     128           1 :         }
     129             : 
     130           1 :         void urlobjTest_004(  )
     131             :         {
     132           1 :             INetURLObject aUrl( OUString( "smb://10.10.1.1/sampledir/sample.file" ) );
     133             : #ifdef LINUX
     134           2 :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
     135           1 :                             equalsAscii( "smb://10.10.1.1/sampledir/sample.file" ) );
     136           1 :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_SMB );
     137             : #endif
     138             : #ifdef WIN
     139             :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
     140             :                             equalsAscii( "file://10.10.1.1/sampledir/sample.file" ) );
     141             :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_FILE );
     142             : #endif
     143           2 :             CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
     144           1 :                             equalsAscii( "10.10.1.1" ) );
     145           2 :             CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
     146           1 :                             equalsAscii( "/sampledir/sample.file" ) );
     147           2 :             CPPUNIT_ASSERT( aUrl.getName(  ).
     148           1 :                             equalsAscii( "sample.file" ) );
     149           1 :             CPPUNIT_ASSERT( aUrl.getBase(  ).equalsAscii( "sample" ) );
     150           1 :             CPPUNIT_ASSERT( aUrl.getExtension(  ).equalsAscii( "file" ) );
     151           1 :         }
     152             : 
     153           1 :         void urlobjTest_005(  )
     154             :         {
     155           1 :             INetURLObject aUrl;
     156             :             aUrl.setFSysPath( OUString( "//137.65.170.24/c$/Img0001.jpg" ),
     157           1 :                               INetURLObject::FSYS_DETECT );
     158             : #ifdef LINUX
     159           2 :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
     160           1 :                             equalsAscii( "smb://137.65.170.24/c$/Img0001.jpg" ) );
     161           1 :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_SMB );
     162             : #endif
     163             : #ifdef WIN
     164             :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
     165             :                             equalsAscii( "file://137.65.170.24/c$/Img0001.jpg" ) );
     166             :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_FILE );
     167             : #endif
     168           2 :             CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
     169           1 :                             equalsAscii( "137.65.170.24" ) );
     170           2 :             CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
     171           1 :                             equalsAscii( "/c$/Img0001.jpg" ) );
     172           1 :             CPPUNIT_ASSERT( aUrl.getName(  ).equalsAscii( "Img0001.jpg" ) );
     173           1 :             CPPUNIT_ASSERT( aUrl.getBase(  ).equalsAscii( "Img0001" ) );
     174           1 :             CPPUNIT_ASSERT( aUrl.getExtension(  ).equalsAscii( "jpg" ) );
     175           1 :         }
     176             : 
     177             : 
     178           1 :         void urlobjTest_006(  )
     179             :         {
     180           1 :             INetURLObject aUrl;
     181             :             aUrl.setFSysPath( OUString( "//hive-winxp-x86/pmladek/test2.odt" ),
     182           1 :                               INetURLObject::FSYS_DETECT );
     183             : #ifdef LINUX
     184           2 :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
     185           1 :                             equalsAscii( "smb://hive-winxp-x86/pmladek/test2.odt" ) );
     186           1 :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_SMB );
     187             : #endif
     188             : #ifdef WIN
     189             :             CPPUNIT_ASSERT( aUrl.GetMainURL( INetURLObject::NO_DECODE ).
     190             :                             equalsAscii( "file://hive-winxp-x86/pmladek/test2.odt" ) );
     191             :             CPPUNIT_ASSERT( aUrl.GetProtocol(  ) == INET_PROT_FILE );
     192             : #endif
     193           2 :             CPPUNIT_ASSERT( aUrl.GetHost( INetURLObject::NO_DECODE ).
     194           1 :                             equalsAscii( "hive-winxp-x86" ) );
     195           2 :             CPPUNIT_ASSERT( aUrl.GetURLPath( INetURLObject::NO_DECODE ).
     196           2 :                             equalsAscii( "/pmladek/test2.odt" ) );
     197           1 :         }
     198             : 
     199           1 :         void urlobjCmisTest(  )
     200             :         {
     201             :             // Test with a username part
     202             :             {
     203           1 :                 INetURLObject aUrl( OUString( "vnd.libreoffice.cmis://username@http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
     204           2 :                 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
     205           1 :                         OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
     206           1 :                 CPPUNIT_ASSERT_EQUAL( std::string( "username" ), OUSTR_TO_STDSTR( aUrl.GetUser( ) ) );
     207           2 :                 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
     208           1 :                         OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
     209           1 :                 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INET_PROT_CMIS, aUrl.GetProtocol(  ) );
     210             :             }
     211             : 
     212             :             // Test without a username part
     213             :             {
     214             :                 INetURLObject aUrl( OUString(
     215           1 :                                 "vnd.libreoffice.cmis://http:%2F%2Ffoo.bar.com:8080%2Fmy%2Fcmis%2Fatom%23repo-id-encoded/path/to/content" ) );
     216           2 :                 CPPUNIT_ASSERT_EQUAL( std::string( "http://foo.bar.com:8080/my/cmis/atom#repo-id-encoded" ),
     217           1 :                         OUSTR_TO_STDSTR( aUrl.GetHost( INetURLObject::DECODE_WITH_CHARSET ) ) );
     218           1 :                 CPPUNIT_ASSERT( !aUrl.HasUserData() );
     219           2 :                 CPPUNIT_ASSERT_EQUAL( std::string( "/path/to/content" ),
     220           1 :                         OUSTR_TO_STDSTR( aUrl.GetURLPath( INetURLObject::NO_DECODE ) ) );
     221           1 :                 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Wrong protocol found", INET_PROT_CMIS, aUrl.GetProtocol(  ) );
     222             :             }
     223           1 :         }
     224             : 
     225           1 :         void urlobjTest_emptyPath() {
     226             :             {
     227           1 :                 INetURLObject url(OUString("http://example.com"));
     228           1 :                 CPPUNIT_ASSERT_EQUAL(INET_PROT_HTTP, url.GetProtocol());
     229           1 :                 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
     230           1 :                 CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
     231             :             }
     232             :             {
     233             :                 // This is an invalid http URL per RFC 2616:
     234           1 :                 INetURLObject url(OUString("http://example.com?query"));
     235           1 :                 CPPUNIT_ASSERT(url.HasError());
     236             :             }
     237             :             {
     238           1 :                 INetURLObject url(OUString("http://example.com#fragment"));
     239           1 :                 CPPUNIT_ASSERT_EQUAL(INET_PROT_HTTP, url.GetProtocol());
     240           1 :                 CPPUNIT_ASSERT_EQUAL(OUString("example.com"), url.GetHost());
     241           1 :                 CPPUNIT_ASSERT_EQUAL(OUString("/"), url.GetURLPath());
     242           1 :                 CPPUNIT_ASSERT_EQUAL(OUString("fragment"), url.GetMark());
     243             :             }
     244           1 :         }
     245             : 
     246             :         // Change the following lines only, if you add, remove or rename
     247             :         // member functions of the current class,
     248             :         // because these macros are need by auto register mechanism.
     249             : 
     250           2 :         CPPUNIT_TEST_SUITE( urlobjTest );
     251           1 :         CPPUNIT_TEST( urlobjTest_001 );
     252           1 :         CPPUNIT_TEST( urlobjTest_002 );
     253           1 :         CPPUNIT_TEST( urlobjTest_003 );
     254           1 :         CPPUNIT_TEST( urlobjTest_004 );
     255           1 :         CPPUNIT_TEST( urlobjTest_005 );
     256           1 :         CPPUNIT_TEST( urlobjTest_006 );
     257           1 :         CPPUNIT_TEST( urlobjCmisTest );
     258           1 :         CPPUNIT_TEST( urlobjTest_emptyPath );
     259           2 :         CPPUNIT_TEST_SUITE_END(  );
     260             :     };                          // class createPool
     261             : 
     262             : 
     263           1 :     CPPUNIT_TEST_SUITE_REGISTRATION( urlobjTest );
     264           3 : }                               // namespace rtl_random
     265             : 
     266             : 
     267             : 
     268             : 
     269             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10