genericservices/httputils/Test/t_uriparser/CUriUtilsTest.cpp
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     1 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "CUriUtilsTest.h"
       
    17 
       
    18 #include <e32base.h>
       
    19 #include <uriutils.h>
       
    20 
       
    21 #include "UriUtilsTests.h"
       
    22 
       
    23 CUriUtilsTest* CUriUtilsTest::NewLC(CIpuTestHarness* aTestHarness)
       
    24 	{
       
    25 	CUriUtilsTest* self = new (ELeave) CUriUtilsTest(aTestHarness);
       
    26 	CleanupStack::PushL(self);
       
    27 	self->ConstructL();
       
    28 	return self;
       
    29 	}
       
    30 
       
    31 CUriUtilsTest* CUriUtilsTest::NewL(CIpuTestHarness* aTestHarness)
       
    32 	{
       
    33 	CUriUtilsTest* self = CUriUtilsTest::NewLC(aTestHarness);
       
    34 	CleanupStack::Pop(self);
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 CUriUtilsTest::CUriUtilsTest(CIpuTestHarness* aTestHarness)
       
    39 : iTestHarness(aTestHarness)
       
    40 	{
       
    41 	}
       
    42 
       
    43 void CUriUtilsTest::ConstructL()
       
    44 	{
       
    45 	}
       
    46 
       
    47 CUriUtilsTest::~CUriUtilsTest()
       
    48 	{
       
    49 	}
       
    50 
       
    51 void CUriUtilsTest::DoTestsL()
       
    52 	{
       
    53 	TestUriUtilsConvertToInternetFormL(KDisplayForm0, KInternetForm0);
       
    54 	TestUriUtilsConvertToDisplayFormDeprecatedL(KInternetForm0, KDisplayForm0);
       
    55 	TestUriUtilsConvertToDisplayFormL(KInternetForm0, KValidDisplayForm0);
       
    56 
       
    57 	TestUriUtilsConvertToInternetFormL(KDisplayForm1, KInternetForm1);
       
    58 	TestUriUtilsConvertToDisplayFormDeprecatedL(KInternetForm1, KDisplayForm1);
       
    59 	TestUriUtilsConvertToDisplayFormL(KInternetForm1, KValidDisplayForm1);
       
    60 
       
    61 	TestUriUtilsHasInvalidCharsL(KInvalidChars0, KInvalid0);
       
    62 	TestUriUtilsHasInvalidCharsL(KInvalidChars1, KInvalid0);
       
    63 	TestUriUtilsHasInvalidCharsL(KInvalidChars2, KInvalid0);
       
    64 	TestUriUtilsHasInvalidCharsL(KInvalidChars3, KInvalid0);
       
    65 	TestUriUtilsHasInvalidCharsL(KInvalidChars4, KInvalid0);
       
    66 	TestUriUtilsHasInvalidCharsL(KInvalidChars5, KInvalid0);
       
    67 	TestUriUtilsHasInvalidCharsL(KInvalidChars6, KInvalid0);
       
    68 	TestUriUtilsHasInvalidCharsL(KInvalidChars7, KInvalid0);
       
    69 	TestUriUtilsHasInvalidCharsL(KInvalidChars8, KInvalid0);
       
    70 	TestUriUtilsHasInvalidCharsL(KInvalidChars9, KInvalid0);
       
    71 	TestUriUtilsHasInvalidCharsL(KInvalidChars10, KInvalid0);
       
    72 	TestUriUtilsHasInvalidCharsL(KInvalidChars11, KInvalid0);
       
    73  
       
    74 	TestUriUtilsHasInvalidCharsL(KInvalidChars12, KInvalid1);
       
    75 	TestUriUtilsHasInvalidCharsL(KInvalidChars13, KInvalid1);
       
    76 	TestUriUtilsHasInvalidCharsL(KInvalidChars14, KInvalid1);
       
    77 	TestUriUtilsHasInvalidCharsL(KInvalidChars15, KInvalid1);
       
    78 	TestUriUtilsHasInvalidCharsL(KInvalidChars16, KInvalid1);
       
    79 	TestUriUtilsHasInvalidCharsL(KInvalidChars17, KInvalid1);
       
    80 	TestUriUtilsHasInvalidCharsL(KInvalidChars18, KInvalid1);
       
    81 	TestUriUtilsHasInvalidCharsL(KInvalidChars19, KInvalid1);
       
    82 	TestUriUtilsHasInvalidCharsL(KInvalidChars20, KInvalid1);
       
    83 	TestUriUtilsHasInvalidCharsL(KInvalidChars21, KInvalid1);
       
    84 	TestUriUtilsHasInvalidCharsL(KInvalidChars22, KInvalid1);
       
    85 	TestUriUtilsHasInvalidCharsL(KInvalidChars23, KInvalid1);
       
    86 	TestUriUtilsHasInvalidCharsL(KInvalidChars24, KInvalid1);
       
    87 	TestUriUtilsHasInvalidCharsL(KInvalidChars25, KInvalid1);
       
    88 	TestUriUtilsHasInvalidCharsL(KInvalidChars26, KInvalid1);
       
    89 	TestUriUtilsHasInvalidCharsL(KInvalidChars27, KInvalid1);
       
    90 	TestUriUtilsHasInvalidCharsL(KInvalidChars28, KInvalid1);
       
    91 
       
    92 
       
    93 	//
       
    94 	// Tests for 2.9.1.3 Host-Type checking
       
    95 
       
    96 	// All these are IPv6 tests
       
    97 	TestUriUtilsHostTypeL(KHostTypeIPv6Format1, UriUtils::EIPv6Host);			   
       
    98 	TestUriUtilsHostTypeL(KHostTypeIPv6Format2, UriUtils::EIPv6Host);
       
    99 	TestUriUtilsHostTypeL(KHostTypeIPv6Format3, UriUtils::EIPv6Host);
       
   100 	TestUriUtilsHostTypeL(KHostTypeIPv6Format4, UriUtils::EIPv6Host);
       
   101 	TestUriUtilsHostTypeL(KHostTypeIPv6Format5, UriUtils::EIPv6Host);
       
   102 	TestUriUtilsHostTypeL(KHostTypeIPv6Format6, UriUtils::EIPv6Host);
       
   103 	TestUriUtilsHostTypeL(KHostTypeIPv6Format7, UriUtils::EIPv6Host);
       
   104 
       
   105 	TestUriUtilsHostTypeL(KHostTypeIPv4Format, UriUtils::EIPv4Host);
       
   106 
       
   107 	// Invalid format IPv4 hosts should be considered Text hosts
       
   108 	TestUriUtilsHostTypeL(KHostTypeIPv4InvalidFormat1, UriUtils::ETextHost);
       
   109 	TestUriUtilsHostTypeL(KHostTypeIPv4InvalidFormat2, UriUtils::ETextHost);
       
   110 
       
   111 	TestUriUtilsHostTypeL(KHostTypeTextFormat1, UriUtils::ETextHost);
       
   112 	TestUriUtilsHostTypeL(KHostTypeTextFormat2, UriUtils::ETextHost);
       
   113 	//
       
   114 	//
       
   115 	}
       
   116 
       
   117 void CUriUtilsTest::TestUriUtilsConvertToInternetFormDeprecatedL(const TDesC& aData, const TDesC& aExpected)
       
   118 	{
       
   119 	// Make 8-bit copy
       
   120 	HBufC8* expectedBuf = HBufC8::NewLC(aExpected.Length());
       
   121 	TPtr8 expected8Bit = expectedBuf->Des();
       
   122 	expected8Bit.Copy(aExpected);
       
   123 
       
   124 	iTestHarness->StartTestL(_L("Test UriUtils ConvertToInternetForm (deprecated)"));
       
   125 
       
   126 	TUriParser16 parser;
       
   127 	parser.Parse(aData);
       
   128 	CUri8* uri = NULL;
       
   129 	TRAPD(error, uri = UriUtils::ConvertToInternetFormL(parser));
       
   130 	if( error == KErrNone )
       
   131 		{
       
   132 		if( uri->Uri().UriDes().Compare(expected8Bit) != 0 )
       
   133 			{
       
   134 			error = KErrNotFound;
       
   135 			}
       
   136 		delete uri;
       
   137 		}
       
   138 	iTestHarness->EndTest(error);
       
   139 
       
   140 	CleanupStack::PopAndDestroy(expectedBuf);	// expectedBuf
       
   141 	}
       
   142 void CUriUtilsTest::TestUriUtilsConvertToInternetFormL(const TDesC& aData, const TDesC& aExpected)
       
   143 	{
       
   144 	// Make 8-bit copy
       
   145 	HBufC8* expectedBuf = HBufC8::NewLC(aExpected.Length());
       
   146 	TPtr8 expected8Bit = expectedBuf->Des();
       
   147 	expected8Bit.Copy(aExpected);
       
   148 
       
   149 	iTestHarness->StartTestL(_L("Test UriUtils ConvertToInternetForm"));
       
   150 
       
   151 	CUri8* uri = NULL;
       
   152 	TRAPD(error, uri = UriUtils::CreateUriL(aData));
       
   153 	if( error == KErrNone )
       
   154 		{
       
   155 		if( uri->Uri().UriDes().Compare(expected8Bit) != 0 )
       
   156 			{
       
   157 			error = KErrNotFound;
       
   158 			}
       
   159 		delete uri;
       
   160 		}
       
   161 	iTestHarness->EndTest(error);
       
   162 
       
   163 	CleanupStack::PopAndDestroy(expectedBuf);	// expectedBuf	
       
   164 	TestUriUtilsConvertToInternetFormDeprecatedL(aData, aExpected );
       
   165 	}
       
   166 
       
   167 void CUriUtilsTest::TestUriUtilsConvertToDisplayFormDeprecatedL(const TDesC& aData, const TDesC& aExpected)
       
   168 	{
       
   169 	// Make 8-bit copy
       
   170 	HBufC8* dataBuf = HBufC8::NewLC(aData.Length());
       
   171 	TPtr8 data8Bit = dataBuf->Des();
       
   172 	data8Bit.Copy(aData);
       
   173 
       
   174 	iTestHarness->StartTestL(_L("Test UriUtils ConvertToInternetForm (deprecated)"));
       
   175 
       
   176 	TUriParser8 parser;
       
   177 	parser.Parse(data8Bit);
       
   178 	CUri16* uri = NULL;
       
   179 	TRAPD(error, uri = UriUtils::ConvertToDisplayFormL(parser));
       
   180 	if( error == KErrNone )
       
   181 		{
       
   182 		if( uri->Uri().UriDes().Compare(aExpected) != 0 )
       
   183 			{
       
   184 			error = KErrNotFound;
       
   185 			}
       
   186 		delete uri;
       
   187 		}
       
   188 	iTestHarness->EndTest(error);
       
   189 
       
   190 	CleanupStack::PopAndDestroy(dataBuf);	// dataBuf
       
   191 	}
       
   192 	
       
   193 void CUriUtilsTest::TestUriUtilsConvertToDisplayFormL(const TDesC& aData, const TDesC& aExpected)
       
   194 	{
       
   195 	// Make 8-bit copy
       
   196 	HBufC8* dataBuf = HBufC8::NewLC(aData.Length());
       
   197 	TPtr8 data8Bit = dataBuf->Des();
       
   198 	data8Bit.Copy(aData);
       
   199 
       
   200 	iTestHarness->StartTestL(_L("Test UriUtils ConvertToInternetForm"));
       
   201 
       
   202 	TUriParser8 parser;
       
   203 	parser.Parse(data8Bit);
       
   204 	HBufC* uri = NULL;
       
   205 	TRAPD(error, uri = parser.DisplayFormL());
       
   206 	if( error == KErrNone )
       
   207 		{
       
   208 		if( uri->Compare(aExpected) != 0 )
       
   209 			{
       
   210 			error = KErrNotFound;
       
   211 			}
       
   212 		delete uri;
       
   213 		}
       
   214 	iTestHarness->EndTest(error);
       
   215 
       
   216 	CleanupStack::PopAndDestroy(dataBuf);	// dataBuf
       
   217 	}
       
   218 
       
   219 void CUriUtilsTest::TestUriUtilsHasInvalidCharsL(const TDesC16& aData, TBool aHasInvalidChars)
       
   220 	{
       
   221 	iTestHarness->StartTestL(_L("Test UriUtils HasInvalidChars (16-bit)"));
       
   222 	
       
   223 	TBool invalid = UriUtils::HasInvalidChars(aData);
       
   224 
       
   225 	TInt error = KErrNone;
       
   226 	if( (!invalid && aHasInvalidChars) | (invalid && !aHasInvalidChars) )
       
   227 		{
       
   228 		error = KErrNotFound;
       
   229 		}
       
   230 	iTestHarness->EndTest(error);
       
   231 	}
       
   232 
       
   233 void CUriUtilsTest::TestUriUtilsHasInvalidCharsL(const TDesC8& aData, TBool aHasInvalidChars)
       
   234 	{
       
   235 	iTestHarness->StartTestL(_L("Test UriUtils HasInvalidChars (8-bit)"));
       
   236 	
       
   237 	TBool invalid = UriUtils::HasInvalidChars(aData);
       
   238 
       
   239 	TInt error = KErrNone;
       
   240 	if( (!invalid && aHasInvalidChars) | (invalid && !aHasInvalidChars) )
       
   241 		{
       
   242 		error = KErrNotFound;
       
   243 		}
       
   244 	iTestHarness->EndTest(error);
       
   245 	}
       
   246 
       
   247 void CUriUtilsTest::TestUriUtilsHostTypeL(const TDesC8& aData, UriUtils::TUriHostType aHostType)
       
   248 	{
       
   249 	iTestHarness->StartTestL(_L("Test UriUtils HostType (8-bit)"));
       
   250 	
       
   251 	UriUtils::TUriHostType hostType = UriUtils::HostType(aData);
       
   252 
       
   253 	TInt error = KErrNone;
       
   254 	if ( hostType != aHostType )
       
   255 		{
       
   256 		error = KErrNotFound;	
       
   257 		}	
       
   258 	iTestHarness->EndTest(error);
       
   259 	}
       
   260 	
       
   261 void CUriUtilsTest::TestUriUtilsHostTypeL(const TDesC16& aData, UriUtils::TUriHostType aHostType)
       
   262 	{
       
   263 	iTestHarness->StartTestL(_L("Test UriUtils HostType (16-bit)"));
       
   264 
       
   265 	UriUtils::TUriHostType hostType = UriUtils::HostType(aData);
       
   266 
       
   267 	TInt error = KErrNone;
       
   268 	if ( hostType != aHostType )
       
   269 		{
       
   270 		error = KErrNotFound;
       
   271 		}	
       
   272 	iTestHarness->EndTest(error);
       
   273 	}